Annotation of src/usr.bin/encrypt/encrypt.c, Revision 1.4
1.4 ! downsj 1: /* $OpenBSD: encrypt.c,v 1.3 1996/08/26 08:41:26 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>
1.3 downsj 31: #include <errno.h>
1.1 downsj 32: #include <string.h>
33: #include <unistd.h>
34:
35: /*
36: * Very simple little program, for encrypting passwords from the command
37: * line. Useful for scripts and such.
38: */
39:
40: extern char *optarg;
41: extern int optind;
42:
1.3 downsj 43: char *progname;
44:
1.1 downsj 45: void usage()
46: {
1.3 downsj 47: errx(1, "usage: %s [-k] [-m] [-s salt] [string]", progname);
1.1 downsj 48: }
49:
1.2 downsj 50: char *trim(line)
51: char *line;
52: {
53: char *ptr;
54:
55: for (ptr = &line[strlen(line)-1]; ptr > line; ptr--) {
56: if (!isspace(*ptr))
57: break;
58: }
59: ptr[1] = '\0';
60:
61: for (ptr = line; *ptr && isspace(*ptr); ptr++);
62:
63: return(ptr);
64: }
65:
1.1 downsj 66: int main(argc, argv)
67: int argc;
68: char *argv[];
69: {
70: int opt;
71: int do_md5 = 0;
1.3 downsj 72: int do_makekey = 0;
1.1 downsj 73: char *salt = (char *)NULL;
74:
1.3 downsj 75: if ((progname = strrchr(argv[0], '/')))
76: progname++;
77: else
78: progname = argv[0];
79:
80: if (strcmp(progname, "makekey") == 0)
81: do_makekey = 1;
82:
83: while ((opt = getopt(argc, argv, "kms:")) != -1) {
1.1 downsj 84: switch (opt) {
1.3 downsj 85: case 'k':
86: do_makekey = 1;
87: break;
1.1 downsj 88: case 'm':
89: do_md5 = 1;
90: break;
91: case 's':
92: salt = optarg;
1.4 ! downsj 93: if (salt[0] == '$') /* -s is only for DES. */
! 94: usage();
1.1 downsj 95: break;
96: default:
97: usage();
98: }
99: }
100:
1.3 downsj 101: if (do_md5 && !do_makekey && (salt != (char *)NULL))
1.1 downsj 102: usage();
103:
1.3 downsj 104: if (!do_md5 && !do_makekey && (salt == (char *)NULL))
1.1 downsj 105: usage();
106:
1.3 downsj 107: if (do_makekey && (do_md5 || (salt != (char *)NULL)))
108: usage();
109:
1.4 ! downsj 110: if (((argc - optind) < 1) || do_makekey) {
1.3 downsj 111: char line[BUFSIZ], *string, msalt[3];
1.1 downsj 112:
113: /* Encrypt stdin to stdout. */
114: while (!feof(stdin) && (fgets(line, sizeof(line), stdin) != NULL)) {
1.2 downsj 115: /* Kill the whitesapce. */
116: string = trim(line);
117: if (*string == '\0')
1.1 downsj 118: continue;
1.3 downsj 119: if (do_makekey) {
120: /*
121: * makekey mode: parse string into seperate DES key and salt.
122: */
123: if (strlen(string) != 10) {
124: /* To be compatible... */
125: fprintf (stderr, "%s: %s\n", progname, strerror(EFTYPE));
126: exit (1);
127: }
128: strcpy(msalt, &string[8]);
129: salt = msalt;
130: }
1.1 downsj 131:
1.2 downsj 132: fputs(crypt(string, (do_md5 ? "$1$" : salt)), stdout);
1.3 downsj 133: if (do_makekey) {
134: fflush(stdout);
135: break;
136: }
1.1 downsj 137: fputc('\n', stdout);
138: }
139: } else {
140: char *string;
141:
142: /* Perhaps it isn't worth worrying about, but... */
143: string = strdup(argv[optind]);
144: if (string == (char *)NULL)
145: err(1, NULL);
146: /* Wipe the argument. */
147: bzero(argv[optind], strlen(argv[optind]));
148:
149: fputs(crypt(string, (do_md5 ? "$1$" : salt)), stdout);
150: fputc('\n', stdout);
151:
152: /* Wipe our copy, before we free it. */
153: bzero(string, strlen(string));
154: free(string);
155: }
156: exit(0);
157: }