3 #if !defined(NO_DES) /* && !defined(prerequisites of other algorithms) */
10 #include <openssl/bio.h>
11 #include <openssl/err.h>
12 #include <openssl/evp.h>
13 #include <openssl/rand.h>
16 # include <openssl/des.h>
20 #define PROG passwd_main
23 static unsigned const char cov_2char[64]={
24 /* from crypto/des/fcrypt.c */
25 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
26 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
27 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
28 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
29 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
30 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
31 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
32 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
35 /* -crypt - standard Unix password algorithm (default, only choice)
37 * -quiet - no warnings
38 * -table - format output as table
41 int MAIN(int argc, char **argv)
44 char *salt = NULL, *passwd, **passwds = NULL;
45 char *salt_malloc = NULL, *passwd_malloc = NULL;
47 int i, badopt, opt_done;
48 int passed_salt = 0, quiet = 0, table = 0;
55 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
56 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
57 out = BIO_new(BIO_s_file());
60 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
62 badopt = 0, opt_done = 0;
64 while (!badopt && !opt_done && argv[++i] != NULL)
66 if (strcmp(argv[i], "-crypt") == 0)
68 else if (strcmp(argv[i], "-salt") == 0)
70 if ((argv[i+1] != NULL) && (salt == NULL))
77 else if (strcmp(argv[i], "-quiet") == 0)
79 else if (strcmp(argv[i], "-table") == 0)
81 else if (argv[i][0] == '-')
84 /* non-option argument */
91 if (crypt /* + algo2 + algo3 + ... */ == 0) /* use default */
93 if (crypt /* + algo2 + algo3 */ > 1) /* conflict */
98 BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n");
99 BIO_printf(bio_err, "where options are\n");
100 BIO_printf(bio_err, "-crypt standard Unix password algorithm (default)\n");
101 BIO_printf(bio_err, "-salt string use provided salt\n");
102 BIO_printf(bio_err, "-quiet no warnings\n");
103 BIO_printf(bio_err, "-table format output as table\n");
114 /* build a null-terminated list */
115 static char *passwds_static[2] = {NULL, NULL};
117 passwds = passwds_static;
118 passwd_malloc = Malloc(pw_maxlen + 1);
119 if (passwd_malloc == NULL)
121 if (EVP_read_pw_string(passwd_malloc, pw_maxlen + 1, "Password: ", 0) != 0)
123 passwds[0] = passwd_malloc;
126 assert(passwds != NULL);
127 assert(*passwds != NULL);
129 do /* loop over list of passwords */
131 /* first make sure we have a salt */
136 if (salt_malloc == NULL)
138 salt = salt_malloc = Malloc(3);
139 if (salt_malloc == NULL)
142 if (RAND_pseudo_bytes((unsigned char *)salt, 2) < 0)
144 salt[0] = cov_2char[salt[0] & 0x3f]; /* 6 bits */
145 salt[1] = cov_2char[salt[1] & 0x3f]; /* 6 bits */
147 #ifdef CHARSET_EBCDIC
148 ascii2ebcdic(salt, salt, 2); /* des_crypt will convert
152 /* else if (algo2) ... */
155 assert(salt != NULL);
157 /* truncate password if necessary */
159 if ((strlen(passwd) > pw_maxlen))
162 BIO_printf(bio_err, "Warning: truncating password to %u characters\n", pw_maxlen);
163 passwd[pw_maxlen] = 0;
165 assert(strlen(passwd) <= pw_maxlen);
167 /* now compute password hash */
170 char *hash = des_crypt(passwd, salt);
172 BIO_printf(out, "%s\t%s\n", passwd, hash);
174 BIO_printf(out, "%s\n", hash);
176 /* else if (algo2) { ... } else if (algo3) { ... } */
180 while (*passwds != NULL);
183 ERR_print_errors(bio_err);