ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/tools/mkpasswd.c
(Generate patch)

Comparing ircd-hybrid/trunk/tools/mkpasswd.c (file contents):
Revision 6581 by michael, Sun Oct 18 16:58:34 2015 UTC vs.
Revision 6582 by michael, Tue Oct 20 18:55:49 2015 UTC

# Line 19 | Line 19
19   #include <unistd.h>
20   #include <fcntl.h>
21  
22 < #define FLAG_MD5      0x00000001
23 < #define FLAG_DES      0x00000002
24 < #define FLAG_SALT     0x00000004
25 < #define FLAG_PASS     0x00000008
26 < #define FLAG_LENGTH   0x00000010
27 < #define FLAG_BLOWFISH 0x00000020
28 < #define FLAG_ROUNDS   0x00000040
29 < #define FLAG_EXT      0x00000080
30 < #define FLAG_RAW      0x00000100
31 < #define FLAG_SHA256   0x00000200
32 < #define FLAG_SHA512   0x00000400
22 > enum
23 > {
24 >  FLAG_MD5      = 0x00000001,
25 >  FLAG_SALT     = 0x00000002,
26 >  FLAG_PASS     = 0x00000004,
27 >  FLAG_LENGTH   = 0x00000008,
28 >  FLAG_BLOWFISH = 0x00000010,
29 >  FLAG_ROUNDS   = 0x00000020,
30 >  FLAG_RAW      = 0x00000040,
31 >  FLAG_SHA256   = 0x00000080,
32 >  FLAG_SHA512   = 0x00000100
33 > };
34  
35  
36   extern char *crypt();
# Line 39 | Line 40 | static const char *make_sha256_salt(unsi
40   static const char *make_sha256_salt_para(const char *);
41   static const char *make_sha512_salt(unsigned int);
42   static const char *make_sha512_salt_para(const char *);
42 static const char *make_des_salt(void);
43 static const char *make_ext_salt(unsigned int);
44 static const char *make_ext_salt_para(unsigned int, const char *);
43   static const char *make_md5_salt(unsigned int);
44   static const char *make_md5_salt_para(const char *);
45 < static const char *make_bf_salt(unsigned int, unsigned int);
46 < static const char *make_bf_salt_para(unsigned int, const char *);
49 < static const char *int_to_base64(unsigned int);
45 > static const char *make_blowfish_salt(unsigned int, unsigned int);
46 > static const char *make_blowfish_salt_para(unsigned int, const char *);
47   static const char *generate_random_salt(char *, unsigned int);
48   static const char *generate_poor_salt(char *, unsigned int);
49   static void full_usage(void);
50   static void brief_usage(void);
51  
52   static const char saltChars[] =
53 <       "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
53 >       "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
54         /* 0 .. 63, ascii - 64 */
55  
56   int
# Line 63 | Line 60 | main(int argc, char *argv[])
60    const char *saltpara = NULL;
61    const char *salt = NULL;
62    const char *ret = NULL;
63 <  int c;
67 <  int flag = 0;
63 >  unsigned int flag = 0;
64    int length = 0; /* Not Set */
65    int rounds = 0; /* Not set, since extended DES needs 25 and blowfish needs
66                    ** 4 by default, a side effect of this being the encryption
# Line 72 | Line 68 | main(int argc, char *argv[])
68                    ** parameter.
69                    */
70  
71 <  while ((c = getopt(argc, argv, "56mdber:h?l:s:p:R:")) != -1)
71 >  for (int c = 0; (c = getopt(argc, argv, "56mbr:h?l:s:p:R:")) != -1; )
72    {
73      switch (c)
74      {
# Line 85 | Line 81 | main(int argc, char *argv[])
81        case 'm':
82          flag |= FLAG_MD5;
83          break;
88      case 'd':
89        flag |= FLAG_DES;
90        break;
84        case 'b':
85          flag |= FLAG_BLOWFISH;
86          rounds = 4;
87          break;
95      case 'e':
96        flag |= FLAG_EXT;
97        rounds = 25;
98        break;
88        case 'l':
89          flag |= FLAG_LENGTH;
90          if ((length = atoi(optarg)) < 0)
# Line 132 | Line 121 | main(int argc, char *argv[])
121      }
122    }
123  
124 <  if (flag & FLAG_DES)
136 <  {
137 <    if (flag & FLAG_SALT)
138 <    {
139 <      if (strlen(saltpara) == 2)
140 <        salt = saltpara;
141 <      else
142 <      {
143 <        printf("Invalid salt, please enter 2 alphanumeric characters\n");
144 <        exit(1);
145 <      }
146 <    }
147 <    else
148 <      salt = make_des_salt();
149 <  }
150 <  else if (flag & FLAG_SHA256)
124 >  if (flag & FLAG_SHA256)
125    {
126      if (length == 0)
127        length = 16;
# Line 170 | Line 144 | main(int argc, char *argv[])
144      if (length == 0)
145        length = 22;
146      if (flag & FLAG_SALT)
147 <      salt = make_bf_salt_para(rounds, saltpara);
147 >      salt = make_blowfish_salt_para(rounds, saltpara);
148      else
149 <      salt = make_bf_salt(rounds, length);
176 <  }
177 <  else if (flag & FLAG_EXT)
178 <  {
179 <    /* XXX - rounds needs to be done */
180 <    if (flag & FLAG_SALT)
181 <    {
182 <      if (strlen(saltpara) == 4)
183 <      {
184 <        salt = make_ext_salt_para(rounds, saltpara);
185 <      }
186 <      else
187 <      {
188 <        printf("Invalid salt, please enter 4 alphanumeric characters\n");
189 <        exit(1);
190 <      }
191 <    }
192 <    else
193 <    {
194 <      salt = make_ext_salt(rounds);
195 <    }
149 >      salt = make_blowfish_salt(rounds, length);
150    }
151    else if (flag & FLAG_RAW)
198  {
152      salt = saltpara;
200  }
153    else  /* Default to MD5 */
154    {
155      if (length == 0)
# Line 225 | Line 177 | main(int argc, char *argv[])
177   }
178  
179   static const char *
228 make_des_salt(void)
229 {
230  static char salt[3];
231
232  generate_random_salt(salt, 2);
233  salt[2] = '\0';
234  return salt;
235 }
236
237 static const char *
238 int_to_base64(unsigned int value)
239 {
240  static char buf[5];
241  unsigned int i;
242
243  for (i = 0; i < 4; ++i)
244  {
245    buf[i] = saltChars[value & 63];
246    value >>= 6;  /* Right shifting 6 places is the same as dividing by 64 */
247  }
248
249  buf[i] = '\0';
250
251  return buf;
252 }
253
254 static const char *
255 make_ext_salt(unsigned int rounds)
256 {
257  static char salt[10];
258
259  snprintf(salt, sizeof(salt), "_%s", int_to_base64(rounds));
260
261  generate_random_salt(&salt[5], 4);
262  salt[9] = '\0';
263
264  return salt;
265 }
266
267 static const char *
268 make_ext_salt_para(unsigned int rounds, const char *saltpara)
269 {
270  static char salt[10];
271
272  snprintf(salt, sizeof(salt), "_%s%s", int_to_base64(rounds), saltpara);
273  return salt;
274 }
275
276 static const char *
180   make_sha256_salt_para(const char *saltpara)
181   {
182    static char salt[21];
# Line 397 | Line 300 | make_md5_salt(unsigned int length)
300   }
301  
302   static const char *
303 < make_bf_salt_para(unsigned int rounds, const char *saltpara)
303 > make_blowfish_salt_para(unsigned int rounds, const char *saltpara)
304   {
305    static char salt[31];
306    char tbuf[3];
# Line 417 | Line 320 | make_bf_salt_para(unsigned int rounds, c
320   }
321  
322   static const char *
323 < make_bf_salt(unsigned int rounds, unsigned int length)
323 > make_blowfish_salt(unsigned int rounds, unsigned int length)
324   {
325    static char salt[31];
326    char tbuf[3];
# Line 481 | Line 384 | generate_random_salt(char *salt, unsigne
384   static void
385   full_usage(void)
386   {
387 <  printf("mkpasswd [-5|-6|-m|-d|-b|-e] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n");
387 >  printf("mkpasswd [-5|-6|-b|-m] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n");
388    printf("         [-R rawsalt]\n");
389    printf("-5 Generate a SHA-256 password\n");
390    printf("-6 Generate a SHA-512 password\n");
488  printf("-m Generate an MD5 password\n");
489  printf("-d Generate a DES password\n");
391    printf("-b Generate a Blowfish password\n");
392 <  printf("-e Generate an Extended DES password\n");
392 >  printf("-m Generate an MD5 password\n");
393    printf("-l Specify a length for a random MD5 or Blowfish salt\n");
394 <  printf("-r Specify a number of rounds for a Blowfish or Extended DES password\n");
395 <  printf("   Blowfish:  default 4, no more than 6 recommended\n");
396 <  printf("   Extended DES:  default 25\n");
397 <  printf("-s Specify a salt, 2 alphanumeric characters for DES, up to 16 for SHA/MD5,\n");
497 <  printf("   up to 22 for Blowfish, and 4 for Extended DES\n");
394 >  printf("-r Specify a number of rounds for a Blowfish password;\n");
395 >  printf("   default is 4, no more than 6 recommended\n");
396 >  printf("-s Specify a salt, up to 16 alphanumeric characters for SHA/MD5,\n");
397 >  printf("   and at least 22 for Blowfish\n");
398    printf("-R Specify a raw salt passed directly to crypt()\n");
399    printf("-p Specify a plaintext password to use\n");
400 <  printf("Example: mkpasswd -m -s 3dr -p test\n");
400 >  printf("Example: mkpasswd -6 -s 3dr -p test\n");
401    exit(0);
402   }
403  
# Line 505 | Line 405 | static void
405   brief_usage(void)
406   {
407    printf("mkpasswd - password hash generator\n");
408 <  printf("Standard DES:  mkpasswd [-d] [-s salt] [-p plaintext]\n");
409 <  printf("Extended DES:  mkpasswd -e [-r rounds] [-s salt] [-p plaintext]\n");
410 <  printf("     SHA-256:  mkpasswd -5 [-l saltlength] [-s salt] [-p plaintext]\n");
411 <  printf("     SHA-512:  mkpasswd -6 [-l saltlength] [-s salt] [-p plaintext]\n");
512 <  printf("         MD5:  mkpasswd -m [-l saltlength] [-s salt] [-p plaintext]\n");
513 <  printf("    Blowfish:  mkpasswd -b [-r rounds] [-l saltlength] [-s salt]\n");
408 >  printf("     MD5:  mkpasswd [-m] [-l saltlength] [-s salt] [-p plaintext]\n");
409 >  printf(" SHA-256:  mkpasswd -5 [-l saltlength] [-s salt] [-p plaintext]\n");
410 >  printf(" SHA-512:  mkpasswd -6 [-l saltlength] [-s salt] [-p plaintext]\n");
411 >  printf("Blowfish:  mkpasswd -b [-r rounds] [-l saltlength] [-s salt]\n");
412    printf("                           [-p plaintext]\n");
413 <  printf("         Raw:  mkpasswd -R <rawsalt> [-p plaintext]\n");
413 >  printf("     Raw:  mkpasswd -R <rawsalt> [-p plaintext]\n");
414    printf("Use -h for full usage\n");
415    exit(0);
416   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines