Annotation of src/usr.bin/encrypt/encrypt.c, Revision 1.2
1.2 ! downsj 1: /* $OpenBSD: encrypt.c,v 1.1 1996/08/08 02:07:22 downsj Exp $ */
1.1 downsj 2:
3: /*
4: * Copyright (c) 1996, Jason Downs. All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
19: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25: * SUCH DAMAGE.
26: */
27:
28: #include <stdio.h>
29: #include <sys/types.h>
30: #include <err.h>
31: #include <string.h>
32: #include <unistd.h>
33:
34: /*
35: * Very simple little program, for encrypting passwords from the command
36: * line. Useful for scripts and such.
37: */
38:
39: extern char *__progname;
40: extern char *optarg;
41: extern int optind;
42:
43: void usage()
44: {
45: errx(1, "usage: %s [-m] [-s salt] [string]", __progname);
46: }
47:
1.2 ! downsj 48: char *trim(line)
! 49: char *line;
! 50: {
! 51: char *ptr;
! 52:
! 53: for (ptr = &line[strlen(line)-1]; ptr > line; ptr--) {
! 54: if (!isspace(*ptr))
! 55: break;
! 56: }
! 57: ptr[1] = '\0';
! 58:
! 59: for (ptr = line; *ptr && isspace(*ptr); ptr++);
! 60:
! 61: return(ptr);
! 62: }
! 63:
1.1 downsj 64: int main(argc, argv)
65: int argc;
66: char *argv[];
67: {
68: int opt;
69: int do_md5 = 0;
70: char *salt = (char *)NULL;
71:
72: while ((opt = getopt(argc, argv, "ms:")) != -1) {
73: switch (opt) {
74: case 'm':
75: do_md5 = 1;
76: break;
77: case 's':
78: salt = optarg;
79: break;
80: default:
81: usage();
82: }
83: }
84:
85: if (do_md5 && (salt != (char *)NULL))
86: usage();
87:
88: if (!do_md5 && (salt == (char *)NULL))
89: usage();
90:
91: if ((argc - optind) < 1) {
1.2 ! downsj 92: char line[BUFSIZ], *string;
1.1 downsj 93:
94: /* Encrypt stdin to stdout. */
95: while (!feof(stdin) && (fgets(line, sizeof(line), stdin) != NULL)) {
1.2 ! downsj 96: /* Kill the whitesapce. */
! 97: string = trim(line);
! 98: if (*string == '\0')
1.1 downsj 99: continue;
100:
1.2 ! downsj 101: fputs(crypt(string, (do_md5 ? "$1$" : salt)), stdout);
1.1 downsj 102: fputc('\n', stdout);
103: }
104: } else {
105: char *string;
106:
107: /* Perhaps it isn't worth worrying about, but... */
108: string = strdup(argv[optind]);
109: if (string == (char *)NULL)
110: err(1, NULL);
111: /* Wipe the argument. */
112: bzero(argv[optind], strlen(argv[optind]));
113:
114: fputs(crypt(string, (do_md5 ? "$1$" : salt)), stdout);
115: fputc('\n', stdout);
116:
117: /* Wipe our copy, before we free it. */
118: bzero(string, strlen(string));
119: free(string);
120: }
121: exit(0);
122: }