Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / umkpasswd.c
1 /*
2  * IRC - Internet Relay Chat, ircd/umkpasswd.c
3  * Copyright (C) 2002 hikari
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * $Id$
20 */
21 #include "config.h"
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 /* #include <assert.h> -- Now using assert in ircd_log.h */
30
31 /* ircu headers */
32 #include "ircd_alloc.h"
33 #include "ircd_log.h" /* for ircd's assert.h */
34 #include "ircd_string.h"
35 #include "umkpasswd.h"
36 #include "s_debug.h"
37 #include "ircd_md5.h"
38
39 /* crypto mech headers */
40 #include "ircd_crypt.h"
41 #include "ircd_crypt_smd5.h"
42 #include "ircd_crypt_native.h"
43 #include "ircd_crypt_plain.h"
44
45 /* bleah, evil globals */
46 umkpasswd_conf_t* umkpasswd_conf;
47 crypt_mechs_t* crypt_mechs_root;
48 int log_inassert = 0;
49
50 void copyright(void)
51 {
52   fprintf(stderr, "umkpasswd - Copyright (c) 2002 hikari\n");
53 return;
54 }
55
56 void show_help(void)
57 {
58 #ifdef DEBUGMODE
59  char *debughelp = "[-d <level>] ";
60 #else
61  char *debughelp = "";
62 #endif
63
64  copyright();
65  /*fprintf(stderr, "umkpasswd [-l] [[[-a]||[-u]] <username>] [-y <class>] %s[-c <file>] -m <mech> [password]\n\n", debughelp);*/
66  fprintf(stderr, "umkpasswd [-l] %s-m <mech> [password]\n\n", debughelp);
67  fprintf(stderr, "  -l            List mechanisms available.\n");
68 #if 0
69  fprintf(stderr, "  -a <user>     Add user to conf file.\n");
70  fprintf(stderr, "  -u <user>     Update user's password field.\n");
71  fprintf(stderr, "  -y <class>    Class to place oper in.\n");
72 #endif
73  fprintf(stderr, "  -m <mech>     Mechanism to use [MANDATORY].\n");
74 #ifdef DEBUGMODE
75  fprintf(stderr, "  -d <level>    Debug level to run at.\n");
76 #endif
77 /*
78  fprintf(stderr, "  -c <file>     Conf file to use, default is DPATH/CPATH.\n\n");
79 */
80 return;
81 }
82
83 /* our implementation of debug() */
84 void debug(int level, const char *form, ...)
85 {
86 va_list vl;
87 int err = errno;
88
89   if (level <= (umkpasswd_conf->debuglevel))
90   {
91     va_start(vl, form);
92     vfprintf(stderr, form, vl);
93     fprintf(stderr, "\n");
94     va_end(vl);
95   }
96   errno = err;
97 }
98
99 /* quick implementation of log_write() for assert() call */
100 void log_write(enum LogSys subsys, enum LogLevel severity,
101                unsigned int flags, const char *fmt, ...)
102 {
103   va_list vl;
104   va_start(vl, fmt);
105   vfprintf(stderr, fmt, vl);
106   fprintf(stderr, "\n");
107   va_end(vl);
108 }
109
110 /* quick and dirty salt generator */
111 char *make_salt(const char *salts)
112 {
113 char *tmp = NULL;
114 long int n = 0;
115
116  /* try and get around them running this time after time in quick sucession */
117  sleep(1);
118  srandom((unsigned int)time(NULL));
119
120  if((tmp = calloc(3, sizeof(char))) != NULL)
121  {
122   /* can't optimize this much more than just doing it twice */
123   n = ((float)(strlen(salts))*random()/(RAND_MAX+1.0));
124   memcpy(tmp, (salts+n), 1);
125   sleep(2);
126   n = ((float)(strlen(salts))*random()/(RAND_MAX+1.0));
127   memcpy((tmp+1), (salts+n), 1);
128
129   Debug((DEBUG_DEBUG, "salts = %s", salts));
130   Debug((DEBUG_DEBUG, "strlen(salts) = %d", strlen(salts)));
131  }
132
133 return tmp;
134 }
135
136 /* our implemenation of ircd_crypt_register_mech() */
137 int ircd_crypt_register_mech(crypt_mech_t* mechanism)
138 {
139 crypt_mechs_t* crypt_mech;
140
141  Debug((DEBUG_INFO, "ircd_crypt_register_mech: resistering mechanism: %s", mechanism->shortname));
142
143  /* try to allocate some memory for the new mechanism */
144  if ((crypt_mech = (crypt_mechs_t*)MyMalloc(sizeof(crypt_mechs_t))) == NULL)
145  {
146   /* aww poot, we couldn't get any memory, scream a little then back out */
147   Debug((DEBUG_MALLOC, "ircd_crypt_register_mech: could not allocate memory for %s", mechanism->shortname));
148   return -1;
149  }
150
151  /* ok, we have memory, initialise it */
152  memset(crypt_mech, 0, sizeof(crypt_mechs_t));
153
154  /* assign the data */
155  crypt_mech->mech = mechanism;
156  crypt_mech->next = crypt_mech->prev = NULL;
157
158  /* first of all, is there anything there already? */
159  if(crypt_mechs_root->next == NULL)
160  {
161   /* nope, just add ourself */
162   crypt_mechs_root->next = crypt_mechs_root->prev = crypt_mech;
163  } else {
164   /* nice and simple, put ourself at the end */
165   crypt_mech->prev = crypt_mechs_root->prev;
166   crypt_mech->next = NULL;
167   crypt_mechs_root->prev = crypt_mech->prev->next = crypt_mech;
168  }
169
170  /* we're done */
171  Debug((DEBUG_INFO, "ircd_crypt_register_mech: resistered mechanism: %s, crypt_function is at 0x%X.", crypt_mech->mech->shortname, &crypt_mech->mech->crypt_function));
172  Debug((DEBUG_INFO, "ircd_crypt_register_mech: %s: %s", crypt_mech->mech->shortname, crypt_mech->mech->description));
173
174 return 0;
175 }
176
177 char *basename_into(char *tmp, char *target)
178 {
179   unsigned int len, ii;
180
181   len = strlen(tmp);
182   for (ii = len; ii > 0; )
183     if (tmp[--ii] == '/')
184       break;
185   if (ii < len - 1)
186     return tmp + ii + (tmp[ii] == '/');
187   else if (tmp[ii] != '/')
188     return tmp;
189   else if (ii == 0)
190     return tmp;
191   else
192   {
193     while (ii > 0)
194       if (tmp[--ii] == '/')
195          break;
196     if (tmp[ii] == '/')
197         ii++;
198     for (len = 0; tmp[ii] != '/'; )
199       target[len++] = tmp[ii++];
200     target[len] = '\0';
201     return target;
202   }
203 }
204
205 void sum(char* tmp)
206 {
207 FILE* file;
208 MD5_CTX context;
209 int len;
210 unsigned char buffer[1024], digest[16];
211
212  if (NULL == (file = fopen(tmp, "r")))
213   exit(0);
214  MD5Init(&context);
215  while ((len = fread (buffer, 1, 1024, file)))
216   MD5Update(&context, buffer, len);
217  MD5Final(digest, &context);
218  fclose(file);
219
220  printf("%s: ", basename_into(tmp, (char*)buffer));
221  for (len = 0; len < 16; len++)
222   printf ("%02x", digest[len]);
223  printf("\n");
224  exit(0);
225 }
226
227 /* dump the loaded mechs list */
228 void show_mechs(void)
229 {
230 crypt_mechs_t* mechs;
231
232  copyright();
233  printf("\nAvailable mechanisms:\n");
234
235  if(crypt_mechs_root == NULL)
236   return;
237
238  mechs = crypt_mechs_root->next;
239
240  for(;;)
241  {
242   if(mechs == NULL)
243    return;
244
245   printf(" %s\t\t%s\n", mechs->mech->mechname, mechs->mech->description);
246
247   mechs = mechs->next;
248  }
249 }
250
251 /* load in the mech "modules" */
252 void load_mechs(void)
253 {
254  /* we need these loaded by hand for now */
255
256  ircd_register_crypt_native();
257  ircd_register_crypt_smd5();
258  ircd_register_crypt_plain(); /* yes I know it's slightly pointless */
259
260 return;
261 }
262
263 crypt_mechs_t* hunt_mech(const char* mechname)
264 {
265 crypt_mechs_t* mech;
266
267  assert(NULL != mechname);
268
269  if(crypt_mechs_root == NULL)
270   return NULL;
271
272  mech = crypt_mechs_root->next;
273
274  for(;;)
275  {
276   if(mech == NULL)
277    return NULL;
278
279   if(0 == (ircd_strcmp(mech->mech->mechname, mechname)))
280    return mech;
281
282   mech = mech->next;
283  }
284 }
285
286 char* crypt_pass(const char* pw, const char* mech)
287 {
288 crypt_mechs_t* crypt_mech;
289 char* salt, *untagged, *tagged;
290
291  assert(NULL != pw);
292  assert(NULL != mech);
293
294  Debug((DEBUG_DEBUG, "pw = %s\n", pw));
295  Debug((DEBUG_DEBUG, "mech = %s\n", mech));
296
297  if (NULL == (crypt_mech = hunt_mech(mech)))
298  {
299   printf("Unable to find mechanism %s\n", mech);
300   return NULL;
301  }
302
303  salt = make_salt(default_salts);
304
305  untagged = (char *)CryptFunc(crypt_mech->mech)(pw, salt);
306  tagged = (char *)MyMalloc(strlen(salt)+CryptTokSize(crypt_mech->mech)+1);
307  memset(tagged, 0, strlen(untagged)+CryptTokSize(crypt_mech->mech)+1);
308  strncpy(tagged, CryptTok(crypt_mech->mech), CryptTokSize(crypt_mech->mech));
309  strncpy(tagged+CryptTokSize(crypt_mech->mech), untagged, strlen(untagged));
310
311 return tagged;
312 }
313
314 char* parse_arguments(int argc, char **argv)
315 {
316 int len = 0, c = 0;
317 const char* options = "a:d:lm:u:y:5:";
318
319  umkpasswd_conf = (umkpasswd_conf_t*)MyMalloc(sizeof(umkpasswd_conf_t));
320
321  umkpasswd_conf->flags = 0;
322  umkpasswd_conf->debuglevel = 0;
323  umkpasswd_conf->operclass = 0;
324  umkpasswd_conf->user = NULL;
325  umkpasswd_conf->mech = NULL;
326
327
328  len = strlen(DPATH) + strlen(CPATH) + 2;
329  umkpasswd_conf->conf = (char *)MyMalloc(len*sizeof(char));
330  memset(umkpasswd_conf->conf, 0, len*sizeof(char));
331  ircd_strncpy(umkpasswd_conf->conf, DPATH, strlen(DPATH));
332  *((umkpasswd_conf->conf) + strlen(DPATH)) = '/';
333  ircd_strncpy((umkpasswd_conf->conf) + strlen(DPATH) + 1, CPATH, strlen(CPATH));
334
335  len = 0;
336
337  while ((EOF != (c = getopt(argc, argv, options))) && !len)
338  {
339   switch (c)
340   {
341    case '5':
342     sum(optarg);
343    break;
344
345    case 'y':
346     umkpasswd_conf->operclass = atoi(optarg);
347     if (umkpasswd_conf->operclass < 0)
348      umkpasswd_conf->operclass = 0;
349    break;
350
351    case 'u':
352     if(umkpasswd_conf->flags && ACT_UPDOPER)
353     {
354      fprintf(stderr, "-a and -u are mutually exclussive.  Use either or neither.\n");
355      abort(); /* b0rk b0rk b0rk */
356     }
357
358     umkpasswd_conf->flags |= ACT_UPDOPER;
359     umkpasswd_conf->user = optarg;
360    break;
361
362    case 'm':
363     umkpasswd_conf->mech = optarg;
364    break;
365
366    case 'l':
367     load_mechs();
368     show_mechs();
369     exit(0);
370    break;
371
372    case 'd':
373     umkpasswd_conf->debuglevel = atoi(optarg);
374     if (umkpasswd_conf->debuglevel < 0)
375      umkpasswd_conf->debuglevel = 0;
376    break;
377
378    case 'c':
379     umkpasswd_conf->conf = optarg;
380    break;
381
382    case 'a':
383     if(umkpasswd_conf->flags && ACT_UPDOPER) 
384     {
385      fprintf(stderr, "-a and -u are mutually exclussive.  Use either or neither.\n");
386      abort(); /* b0rk b0rk b0rk */
387     }
388
389     umkpasswd_conf->flags |= ACT_ADDOPER;
390     umkpasswd_conf->user = optarg;
391    break;
392
393    default:
394     /* unknown option - spit out syntax and b0rk */
395     show_help();
396     abort();
397    break;
398   }
399  }
400
401  Debug((DEBUG_DEBUG, "flags = %d", umkpasswd_conf->flags));
402  Debug((DEBUG_DEBUG, "operclass = %d", umkpasswd_conf->operclass));
403  Debug((DEBUG_DEBUG, "debug = %d", umkpasswd_conf->debuglevel));
404
405  if (NULL != umkpasswd_conf->mech)
406   Debug((DEBUG_DEBUG, "mech = %s", umkpasswd_conf->mech));
407  else
408   Debug((DEBUG_DEBUG, "mech is unset"));
409
410  if (NULL != umkpasswd_conf->conf)
411   Debug((DEBUG_DEBUG, "conf = %s", umkpasswd_conf->conf));
412  else
413   Debug((DEBUG_DEBUG, "conf is unset"));
414
415  if (NULL != umkpasswd_conf->user)
416   Debug((DEBUG_DEBUG, "user = %s", umkpasswd_conf->user));
417  else
418   Debug((DEBUG_DEBUG, "user is unset"));
419
420 /* anything left over should be password */
421 return argv[optind];
422 }
423
424 int main(int argc, char **argv)
425 {
426 char* pw, *crypted_pw;
427
428  crypt_mechs_root = (crypt_mechs_t*)MyMalloc(sizeof(crypt_mechs_t));
429  crypt_mechs_root->mech = NULL;
430  crypt_mechs_root->next = crypt_mechs_root->prev = NULL;
431
432  if (argc < 2)
433  {
434   show_help();
435   exit(0);
436  }
437
438  pw = parse_arguments(argc, argv);
439  load_mechs();
440
441  if (NULL == umkpasswd_conf->mech)
442  {
443   fprintf(stderr, "No mechanism specified.\n");
444   abort();
445  }
446
447  if (NULL == pw)
448  {
449   pw = getpass("Password: ");
450  }
451  crypted_pw = crypt_pass(pw, umkpasswd_conf->mech);
452
453  printf("Crypted Pass: %s\n", crypted_pw);
454  memset(pw, 0, strlen(pw));
455
456 return 0;
457 }