2 * IRC - Internet Relay Chat, ircd/umkpasswd.c
3 * Copyright (C) 2002 hikari
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)
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.
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.
29 /* #include <assert.h> -- Now using assert in ircd_log.h */
32 #include "ircd_alloc.h"
33 #include "ircd_log.h" /* for ircd's assert.h */
34 #include "ircd_string.h"
35 #include "umkpasswd.h"
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"
45 /* bleah, evil globals */
46 umkpasswd_conf_t* umkpasswd_conf;
47 crypt_mechs_t* crypt_mechs_root;
52 fprintf(stderr, "umkpasswd - Copyright (c) 2002 hikari\n");
59 char *debughelp = "[-d <level>] ";
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");
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");
73 fprintf(stderr, " -m <mech> Mechanism to use [MANDATORY].\n");
75 fprintf(stderr, " -d <level> Debug level to run at.\n");
78 fprintf(stderr, " -c <file> Conf file to use, default is DPATH/CPATH.\n\n");
83 /* our implementation of debug() */
84 void debug(int level, const char *form, ...)
89 if (level <= (umkpasswd_conf->debuglevel))
92 vfprintf(stderr, form, vl);
93 fprintf(stderr, "\n");
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, ...)
105 vfprintf(stderr, fmt, vl);
106 fprintf(stderr, "\n");
110 /* quick and dirty salt generator */
111 char *make_salt(const char *salts)
116 /* try and get around them running this time after time in quick sucession */
118 srandom((unsigned int)time(NULL));
120 if((tmp = calloc(3, sizeof(char))) != NULL)
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);
126 n = ((float)(strlen(salts))*random()/(RAND_MAX+1.0));
127 memcpy((tmp+1), (salts+n), 1);
129 Debug((DEBUG_DEBUG, "salts = %s", salts));
130 Debug((DEBUG_DEBUG, "strlen(salts) = %d", strlen(salts)));
136 /* our implemenation of ircd_crypt_register_mech() */
137 int ircd_crypt_register_mech(crypt_mech_t* mechanism)
139 crypt_mechs_t* crypt_mech;
141 Debug((DEBUG_INFO, "ircd_crypt_register_mech: resistering mechanism: %s", mechanism->shortname));
143 /* try to allocate some memory for the new mechanism */
144 if ((crypt_mech = (crypt_mechs_t*)MyMalloc(sizeof(crypt_mechs_t))) == NULL)
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));
151 /* ok, we have memory, initialise it */
152 memset(crypt_mech, 0, sizeof(crypt_mechs_t));
154 /* assign the data */
155 crypt_mech->mech = mechanism;
156 crypt_mech->next = crypt_mech->prev = NULL;
158 /* first of all, is there anything there already? */
159 if(crypt_mechs_root->next == NULL)
161 /* nope, just add ourself */
162 crypt_mechs_root->next = crypt_mechs_root->prev = crypt_mech;
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;
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));
177 char *basename_into(char *tmp, char *target)
179 unsigned int len, ii;
182 for (ii = len; ii > 0; )
183 if (tmp[--ii] == '/')
186 return tmp + ii + (tmp[ii] == '/');
187 else if (tmp[ii] != '/')
194 if (tmp[--ii] == '/')
198 for (len = 0; tmp[ii] != '/'; )
199 target[len++] = tmp[ii++];
210 unsigned char buffer[1024], digest[16];
212 if (NULL == (file = fopen(tmp, "r")))
215 while ((len = fread (buffer, 1, 1024, file)))
216 MD5Update(&context, buffer, len);
217 MD5Final(digest, &context);
220 printf("%s: ", basename_into(tmp, (char*)buffer));
221 for (len = 0; len < 16; len++)
222 printf ("%02x", digest[len]);
227 /* dump the loaded mechs list */
228 void show_mechs(void)
230 crypt_mechs_t* mechs;
233 printf("\nAvailable mechanisms:\n");
235 if(crypt_mechs_root == NULL)
238 mechs = crypt_mechs_root->next;
245 printf(" %s\t\t%s\n", mechs->mech->mechname, mechs->mech->description);
251 /* load in the mech "modules" */
252 void load_mechs(void)
254 /* we need these loaded by hand for now */
256 ircd_register_crypt_native();
257 ircd_register_crypt_smd5();
258 ircd_register_crypt_plain(); /* yes I know it's slightly pointless */
263 crypt_mechs_t* hunt_mech(const char* mechname)
267 assert(NULL != mechname);
269 if(crypt_mechs_root == NULL)
272 mech = crypt_mechs_root->next;
279 if(0 == (ircd_strcmp(mech->mech->mechname, mechname)))
286 char* crypt_pass(const char* pw, const char* mech)
288 crypt_mechs_t* crypt_mech;
289 char* salt, *untagged, *tagged;
292 assert(NULL != mech);
294 Debug((DEBUG_DEBUG, "pw = %s\n", pw));
295 Debug((DEBUG_DEBUG, "mech = %s\n", mech));
297 if (NULL == (crypt_mech = hunt_mech(mech)))
299 printf("Unable to find mechanism %s\n", mech);
303 salt = make_salt(default_salts);
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));
314 char* parse_arguments(int argc, char **argv)
317 const char* options = "a:d:lm:u:y:5:";
319 umkpasswd_conf = (umkpasswd_conf_t*)MyMalloc(sizeof(umkpasswd_conf_t));
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;
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));
337 while ((EOF != (c = getopt(argc, argv, options))) && !len)
346 umkpasswd_conf->operclass = atoi(optarg);
347 if (umkpasswd_conf->operclass < 0)
348 umkpasswd_conf->operclass = 0;
352 if(umkpasswd_conf->flags && ACT_UPDOPER)
354 fprintf(stderr, "-a and -u are mutually exclussive. Use either or neither.\n");
355 abort(); /* b0rk b0rk b0rk */
358 umkpasswd_conf->flags |= ACT_UPDOPER;
359 umkpasswd_conf->user = optarg;
363 umkpasswd_conf->mech = optarg;
373 umkpasswd_conf->debuglevel = atoi(optarg);
374 if (umkpasswd_conf->debuglevel < 0)
375 umkpasswd_conf->debuglevel = 0;
379 umkpasswd_conf->conf = optarg;
383 if(umkpasswd_conf->flags && ACT_UPDOPER)
385 fprintf(stderr, "-a and -u are mutually exclussive. Use either or neither.\n");
386 abort(); /* b0rk b0rk b0rk */
389 umkpasswd_conf->flags |= ACT_ADDOPER;
390 umkpasswd_conf->user = optarg;
394 /* unknown option - spit out syntax and b0rk */
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));
405 if (NULL != umkpasswd_conf->mech)
406 Debug((DEBUG_DEBUG, "mech = %s", umkpasswd_conf->mech));
408 Debug((DEBUG_DEBUG, "mech is unset"));
410 if (NULL != umkpasswd_conf->conf)
411 Debug((DEBUG_DEBUG, "conf = %s", umkpasswd_conf->conf));
413 Debug((DEBUG_DEBUG, "conf is unset"));
415 if (NULL != umkpasswd_conf->user)
416 Debug((DEBUG_DEBUG, "user = %s", umkpasswd_conf->user));
418 Debug((DEBUG_DEBUG, "user is unset"));
420 /* anything left over should be password */
424 int main(int argc, char **argv)
426 char* pw, *crypted_pw;
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;
438 pw = parse_arguments(argc, argv);
441 if (NULL == umkpasswd_conf->mech)
443 fprintf(stderr, "No mechanism specified.\n");
449 pw = getpass("Password: ");
451 crypted_pw = crypt_pass(pw, umkpasswd_conf->mech);
453 printf("Crypted Pass: %s\n", crypted_pw);
454 memset(pw, 0, strlen(pw));