Warn about truncation also in the case when a single password is read using
[openssl.git] / apps / passwd.c
1 /* apps/passwd.c */
2
3 #if defined NO_MD5 || defined CHARSET_EBCDIC
4 # define NO_APR1
5 #endif
6
7 #if !defined(NO_DES) || !defined(NO_APR1)
8
9 #include <assert.h>
10 #include <string.h>
11
12 #include "apps.h"
13
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/rand.h>
18
19 #ifndef NO_DES
20 # include <openssl/des.h>
21 #endif
22 #ifndef NO_APR1
23 # include <openssl/md5.h>
24 #endif
25
26
27 #undef PROG
28 #define PROG passwd_main
29
30
31 static unsigned const char cov_2char[64]={
32         /* from crypto/des/fcrypt.c */
33         0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
34         0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
35         0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
36         0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
37         0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
38         0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
39         0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
40         0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
41 };
42
43 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
44         char *passwd, BIO *out, int quiet, int table, int reverse,
45         size_t pw_maxlen, int usecrypt, int useapr1);
46
47 /* -crypt        - standard Unix password algorithm (default, only choice)
48  * -apr1         - MD5-based password algorithm
49  * -salt string  - salt
50  * -in file      - read passwords from file
51  * -stdin        - read passwords from stdin
52  * -quiet        - no warnings
53  * -table        - format output as table
54  * -reverse      - switch table columns
55  */
56
57 int MAIN(int, char **);
58
59 int MAIN(int argc, char **argv)
60         {
61         int ret = 1;
62         char *infile = NULL;
63         int in_stdin = 0;
64         char *salt = NULL, *passwd = NULL, **passwds = NULL;
65         char *salt_malloc = NULL, *passwd_malloc = NULL;
66         int pw_source_defined = 0;
67         BIO *in = NULL, *out = NULL;
68         int i, badopt, opt_done;
69         int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
70         int usecrypt = 0, useapr1 = 0;
71         size_t pw_maxlen = 0;
72
73         apps_startup();
74
75         if (bio_err == NULL)
76                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
77                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
78         out = BIO_new(BIO_s_file());
79         if (out == NULL)
80                 goto err;
81         BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
82
83         badopt = 0, opt_done = 0;
84         i = 0;
85         while (!badopt && !opt_done && argv[++i] != NULL)
86                 {
87                 if (strcmp(argv[i], "-crypt") == 0)
88                         usecrypt = 1;
89                 else if (strcmp(argv[i], "-apr1") == 0)
90                         useapr1 = 1;
91                 else if (strcmp(argv[i], "-salt") == 0)
92                         {
93                         if ((argv[i+1] != NULL) && (salt == NULL))
94                                 {
95                                 passed_salt = 1;
96                                 salt = argv[++i];
97                                 }
98                         else
99                                 badopt = 1;
100                         }
101                 else if (strcmp(argv[i], "-in") == 0)
102                         {
103                         if ((argv[i+1] != NULL) && !pw_source_defined)
104                                 {
105                                 pw_source_defined = 1;
106                                 infile = argv[++i];
107                                 }
108                         else
109                                 badopt = 1;
110                         }
111                 else if (strcmp(argv[i], "-stdin") == 0)
112                         {
113                         if (!pw_source_defined)
114                                 {
115                                 pw_source_defined = 1;
116                                 in_stdin = 1;
117                                 }
118                         else
119                                 badopt = 1;
120                         }
121                 else if (strcmp(argv[i], "-quiet") == 0)
122                         quiet = 1;
123                 else if (strcmp(argv[i], "-table") == 0)
124                         table = 1;
125                 else if (strcmp(argv[i], "-reverse") == 0)
126                         reverse = 1;
127                 else if (argv[i][0] == '-')
128                         badopt = 1;
129                 else if (!pw_source_defined)
130                         /* non-option arguments, use as passwords */
131                         {
132                         pw_source_defined = 1;
133                         passwds = &argv[i];
134                         opt_done = 1;
135                         }
136                 else
137                         badopt = 1;
138                 }
139
140         if (!usecrypt && !useapr1) /* use default */
141                 usecrypt = 1;
142         if (usecrypt + useapr1 > 1) /* conflict */
143                 badopt = 1;
144
145         /* reject unsupported algorithms */
146 #ifdef NO_DES
147         if (usecrypt) badopt = 1;
148 #endif
149 #ifdef NO_APR1
150         if (useapr1) badopt = 1;
151 #endif
152
153         if (badopt) 
154                 {
155                 BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n");
156                 BIO_printf(bio_err, "where options are\n");
157 #ifndef NO_DES
158                 BIO_printf(bio_err, "-crypt             standard Unix password algorithm (default)\n");
159 #endif
160 #ifndef NO_APR1
161                 BIO_printf(bio_err, "-apr1              MD5-based password algorithm\n");
162 #endif
163                 BIO_printf(bio_err, "-salt string       use provided salt\n");
164                 BIO_printf(bio_err, "-in file           read passwords from file\n");
165                 BIO_printf(bio_err, "-stdin             read passwords from stdin\n");
166                 BIO_printf(bio_err, "-quiet             no warnings\n");
167                 BIO_printf(bio_err, "-table             format output as table\n");
168                 BIO_printf(bio_err, "-reverse           switch table columns\n");
169                 
170                 goto err;
171                 }
172
173         if ((infile != NULL) || in_stdin)
174                 {
175                 in = BIO_new(BIO_s_file());
176                 if (in == NULL)
177                         goto err;
178                 if (infile != NULL)
179                         {
180                         assert(in_stdin == 0);
181                         if (BIO_read_filename(in, infile) <= 0)
182                                 goto err;
183                         }
184                 else
185                         {
186                         assert(in_stdin);
187                         BIO_set_fp(in, stdin, BIO_NOCLOSE);
188                         }
189                 }
190         
191         if (usecrypt)
192                 pw_maxlen = 8;
193         else if (useapr1)
194                 pw_maxlen = 256; /* arbitrary limit, should be enough for most passwords */
195
196         if (passwds == NULL)
197                 {
198                 /* no passwords on the command line */
199 #define PASSWD_MALLOC_SIZE (pw_maxlen + 2)
200                 /* longer than necessary so that we can warn about truncation */
201                 passwd = passwd_malloc = Malloc(PASSWD_MALLOC_SIZE);
202                 if (passwd_malloc == NULL)
203                         goto err;
204                 }
205
206         if ((in == NULL) && (passwds == NULL))
207                 {
208                 /* build a null-terminated list */
209                 static char *passwds_static[2] = {NULL, NULL};
210                 
211                 passwds = passwds_static;
212                 if (in == NULL)
213                         if (EVP_read_pw_string(passwd_malloc, PASSWD_MALLOC_SIZE, "Password: ", 0) != 0)
214                                 goto err;
215                 passwds[0] = passwd_malloc;
216                 }
217
218         if (in == NULL)
219                 {
220                 assert(passwds != NULL);
221                 assert(*passwds != NULL);
222                 
223                 do /* loop over list of passwords */
224                         {
225                         passwd = *passwds++;
226                         if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
227                                 quiet, table, reverse, pw_maxlen, usecrypt, useapr1))
228                                 goto err;
229                         }
230                 while (*passwds != NULL);
231                 }
232         else
233                 /* in != NULL */
234                 {
235                 int done;
236
237                 assert (passwd != NULL);
238                 do
239                         {
240                         int r = BIO_gets(in, passwd, pw_maxlen + 1);
241                         if (r > 0)
242                                 {
243                                 char *c = (strchr(passwd, '\n')) ;
244                                 if (c != NULL)
245                                         *c = 0; /* truncate at newline */
246                                 else
247                                         {
248                                         /* ignore rest of line */
249                                         char trash[BUFSIZ];
250                                         do
251                                                 r = BIO_gets(in, trash, sizeof trash);
252                                         while ((r > 0) && (!strchr(trash, '\n')));
253                                         }
254                                 
255                                 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
256                                         quiet, table, reverse, pw_maxlen, usecrypt, useapr1))
257                                         goto err;
258                                 }
259                         done = (r <= 0);
260                         }
261                 while (!done);
262                 }
263
264 err:
265         ERR_print_errors(bio_err);
266         if (salt_malloc)
267                 Free(salt_malloc);
268         if (passwd_malloc)
269                 Free(passwd_malloc);
270         if (in)
271                 BIO_free(in);
272         if (out)
273                 BIO_free(out);
274         EXIT(ret);
275         }
276
277
278 #ifndef NO_APR1
279 /* MD5-based password algorithm compatible to the one found in Apache
280  * (should probably be available as a library function;
281  * then the static buffer would not be acceptable) */
282 static char *apr1_crypt(const char *passwd, const char *salt)
283         {
284         static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5hash..........\0" */
285         unsigned char buf[MD5_DIGEST_LENGTH];
286         char *salt_out;
287         int n, i;
288         MD5_CTX md;
289         size_t passwd_len, salt_len;
290
291         passwd_len = strlen(passwd);
292         strcpy(out_buf, "$apr1$");
293         strncat(out_buf, salt, 8);
294         assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
295         salt_out = out_buf + 6;
296         salt_len = strlen(salt_out);
297         assert(salt_len <= 8);
298         
299         MD5_Init(&md);
300         MD5_Update(&md, passwd, passwd_len);
301         MD5_Update(&md, "$apr1$", 6);
302         MD5_Update(&md, salt_out, salt_len);
303         
304          {
305                 MD5_CTX md2;
306
307                 MD5_Init(&md2);
308                 MD5_Update(&md2, passwd, passwd_len);
309                 MD5_Update(&md2, salt_out, salt_len);
310                 MD5_Update(&md2, passwd, passwd_len);
311                 MD5_Final(buf, &md2);
312          }
313         for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
314                 MD5_Update(&md, buf, sizeof buf);
315         MD5_Update(&md, buf, i);
316         
317         n = passwd_len;
318         while (n)
319                 {
320                 MD5_Update(&md, (n & 1) ? "\0" : passwd, 1);
321                 n >>= 1;
322                 }
323         MD5_Final(buf, &md);
324
325         for (i = 0; i < 1000; i++)
326                 {
327                 MD5_CTX md2;
328
329                 MD5_Init(&md2);
330                 MD5_Update(&md2, (i & 1) ? (unsigned char *) passwd : buf,
331                                  (i & 1) ? passwd_len : sizeof buf);
332                 if (i % 3)
333                         MD5_Update(&md2, salt_out, salt_len);
334                 if (i % 7)
335                         MD5_Update(&md2, passwd, passwd_len);
336                 MD5_Update(&md2, (i & 1) ? buf : (unsigned char *) passwd,
337                                  (i & 1) ? sizeof buf : passwd_len);
338                 MD5_Final(buf, &md2);
339                 }
340         
341          {
342                 /* transform buf into output string */
343         
344                 unsigned char buf_perm[sizeof buf];
345                 int dest, source;
346                 char *output;
347
348                 /* silly output permutation */
349                 for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
350                         buf_perm[dest] = buf[source];
351                 buf_perm[14] = buf[5];
352                 buf_perm[15] = buf[11];
353 #ifndef PEDANTIC /* Unfortunately, this generates a "no effect" warning */
354                 assert(16 == sizeof buf_perm);
355 #endif
356                 
357                 output = salt_out + salt_len;
358                 assert(output == out_buf + strlen(out_buf));
359                 
360                 *output++ = '$';
361
362                 for (i = 0; i < 15; i += 3)
363                         {
364                         *output++ = cov_2char[buf_perm[i+2] & 0x3f];
365                         *output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
366                                                   (buf_perm[i+2] >> 6)];
367                         *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
368                                                   (buf_perm[i+1] >> 4)];
369                         *output++ = cov_2char[buf_perm[i] >> 2];
370                         }
371                 assert(i == 15);
372                 *output++ = cov_2char[buf_perm[i] & 0x3f];
373                 *output++ = cov_2char[buf_perm[i] >> 6];
374                 *output = 0;
375                 assert(strlen(out_buf) < sizeof(out_buf));
376          }
377
378         return out_buf;
379         }
380 #endif
381
382
383 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
384         char *passwd, BIO *out, int quiet, int table, int reverse,
385         size_t pw_maxlen, int usecrypt, int useapr1)
386         {
387         char *hash = NULL;
388
389         assert(salt_p != NULL);
390         assert(salt_malloc_p != NULL);
391
392         /* first make sure we have a salt */
393         if (!passed_salt)
394                 {
395 #ifndef NO_DES
396                 if (usecrypt)
397                         {
398                         if (*salt_malloc_p == NULL)
399                                 {
400                                 *salt_p = *salt_malloc_p = Malloc(3);
401                                 if (*salt_malloc_p == NULL)
402                                         goto err;
403                                 }
404                         if (RAND_pseudo_bytes((unsigned char *)*salt_p, 2) < 0)
405                                 goto err;
406                         (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
407                         (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
408                         (*salt_p)[2] = 0;
409 #ifdef CHARSET_EBCDIC
410                         ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert
411                                                             * back to ASCII */
412 #endif
413                         }
414 #endif /* !NO_DES */
415
416 #ifndef NO_APR1
417                 if (useapr1)
418                         {
419                         int i;
420                         
421                         if (*salt_malloc_p == NULL)
422                                 {
423                                 *salt_p = *salt_malloc_p = Malloc(9);
424                                 if (*salt_malloc_p == NULL)
425                                         goto err;
426                                 }
427                         if (RAND_pseudo_bytes((unsigned char *)*salt_p, 8) < 0)
428                                 goto err;
429                         
430                         for (i = 0; i < 8; i++)
431                                 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
432                         (*salt_p)[8] = 0;
433                         }
434 #endif /* !NO_APR1 */
435                 }
436         
437         assert(*salt_p != NULL);
438         
439         /* truncate password if necessary */
440         if ((strlen(passwd) > pw_maxlen))
441                 {
442                 if (!quiet)
443                         BIO_printf(bio_err, "Warning: truncating password to %u characters\n", pw_maxlen);
444                 passwd[pw_maxlen] = 0;
445                 }
446         assert(strlen(passwd) <= pw_maxlen);
447         
448         /* now compute password hash */
449 #ifndef NO_DES
450         if (usecrypt)
451                 hash = des_crypt(passwd, *salt_p);
452 #endif
453 #ifndef NO_APR1
454         if (useapr1)
455                 hash = apr1_crypt(passwd, *salt_p);
456 #endif
457         assert(hash != NULL);
458
459         if (table && !reverse)
460                 BIO_printf(out, "%s\t%s\n", passwd, hash);
461         else if (table && reverse)
462                 BIO_printf(out, "%s\t%s\n", hash, passwd);
463         else
464                 BIO_printf(out, "%s\n", hash);
465         return 1;
466         
467 err:
468         return 0;
469         }
470 #else
471
472 int MAIN(int argc, char **argv)
473         {
474         fputs("Program not available.\n", stderr)
475         EXIT(1);
476         }
477 #endif