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.
33 #include "ircd_alloc.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;
51 fprintf(stderr, "umkpasswd - Copyright (c) 2002 hikari\n");
58 char *debughelp = "[-d <level>] ";
64 /*fprintf(stderr, "umkpasswd [-l] [[[-a]||[-u]] <username>] [-y <class>] %s[-c <file>] -m <mech> [password]\n\n", debughelp);*/
65 fprintf(stderr, "umkpasswd [-l] %s-m <mech> [password]\n\n", debughelp);
66 fprintf(stderr, " -l List mechanisms available.\n");
68 fprintf(stderr, " -a <user> Add user to conf file.\n");
69 fprintf(stderr, " -u <user> Update user's password field.\n");
70 fprintf(stderr, " -y <class> Class to place oper in.\n");
72 fprintf(stderr, " -m <mech> Mechanism to use [MANDATORY].\n");
74 fprintf(stderr, " -d <level> Debug level to run at.\n");
77 fprintf(stderr, " -c <file> Conf file to use, default is DPATH/CPATH.\n\n");
82 /* our implementation of debug() */
83 void debug(int level, const char *form, ...)
88 if (level <= (umkpasswd_conf->debuglevel))
91 vfprintf(stderr, form, vl);
92 fprintf(stderr, "\n");
98 /* quick and dirty salt generator */
99 char *make_salt(const char *salts)
104 /* try and get around them running this time after time in quick sucession */
106 srandom((unsigned int)time(NULL));
108 if((tmp = calloc(3, sizeof(char))) != NULL)
110 /* can't optimize this much more than just doing it twice */
111 n = ((float)(strlen(salts))*random()/(RAND_MAX+1.0));
112 memcpy(tmp, (salts+n), 1);
114 n = ((float)(strlen(salts))*random()/(RAND_MAX+1.0));
115 memcpy((tmp+1), (salts+n), 1);
117 Debug((DEBUG_DEBUG, "salts = %s", salts));
118 Debug((DEBUG_DEBUG, "strlen(salts) = %d", strlen(salts)));
124 /* our implemenation of ircd_crypt_register_mech() */
125 int ircd_crypt_register_mech(crypt_mech_t* mechanism)
127 crypt_mechs_t* crypt_mech;
129 Debug((DEBUG_INFO, "ircd_crypt_register_mech: resistering mechanism: %s", mechanism->shortname));
131 /* try to allocate some memory for the new mechanism */
132 if ((crypt_mech = (crypt_mechs_t*)MyMalloc(sizeof(crypt_mechs_t))) == NULL)
134 /* aww poot, we couldn't get any memory, scream a little then back out */
135 Debug((DEBUG_MALLOC, "ircd_crypt_register_mech: could not allocate memory for %s", mechanism->shortname));
139 /* ok, we have memory, initialise it */
140 memset(crypt_mech, 0, sizeof(crypt_mechs_t));
142 /* assign the data */
143 crypt_mech->mech = mechanism;
144 crypt_mech->next = crypt_mech->prev = NULL;
146 /* first of all, is there anything there already? */
147 if(crypt_mechs_root->next == NULL)
149 /* nope, just add ourself */
150 crypt_mechs_root->next = crypt_mechs_root->prev = crypt_mech;
152 /* nice and simple, put ourself at the end */
153 crypt_mech->prev = crypt_mechs_root->prev;
154 crypt_mech->next = NULL;
155 crypt_mechs_root->prev = crypt_mech->prev->next = crypt_mech;
159 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));
160 Debug((DEBUG_INFO, "ircd_crypt_register_mech: %s: %s", crypt_mech->mech->shortname, crypt_mech->mech->description));
170 unsigned char buffer[1024], digest[16];
172 if (NULL == (file = fopen(tmp, "r")))
174 MD5Name(MD5Init)(&context);
175 while ((len = fread (buffer, 1, 1024, file)))
176 MD5Name(MD5Update)(&context, buffer, len);
177 MD5Name(MD5Final)(digest, &context);
180 printf("%s: ", basename(tmp));
181 for (len = 0; len < 16; len++)
182 printf ("%02x", digest[len]);
187 /* dump the loaded mechs list */
188 void show_mechs(void)
190 crypt_mechs_t* mechs;
193 printf("\nAvailable mechanisms:\n");
195 if(crypt_mechs_root == NULL)
198 mechs = crypt_mechs_root->next;
205 printf(" %s\t\t%s\n", mechs->mech->mechname, mechs->mech->description);
211 /* load in the mech "modules" */
212 void load_mechs(void)
214 /* we need these loaded by hand for now */
216 ircd_register_crypt_native();
217 ircd_register_crypt_smd5();
218 ircd_register_crypt_plain(); /* yes I know it's slightly pointless */
223 crypt_mechs_t* hunt_mech(const char* mechname)
227 assert(NULL != mechname);
229 if(crypt_mechs_root == NULL)
232 mech = crypt_mechs_root->next;
239 if(0 == (ircd_strcmp(mech->mech->mechname, mechname)))
246 char* crypt_pass(const char* pw, const char* mech)
248 crypt_mechs_t* crypt_mech;
249 char* salt, *untagged, *tagged;
252 assert(NULL != mech);
254 Debug((DEBUG_DEBUG, "pw = %s\n", pw));
255 Debug((DEBUG_DEBUG, "mech = %s\n", mech));
257 if (NULL == (crypt_mech = hunt_mech(mech)))
259 printf("Unable to find mechanism %s\n", mech);
263 salt = make_salt(default_salts);
265 untagged = (char *)CryptFunc(crypt_mech->mech)(pw, salt);
266 tagged = (char *)MyMalloc(strlen(salt)+CryptTokSize(crypt_mech->mech)+1);
267 memset(tagged, 0, strlen(untagged)+CryptTokSize(crypt_mech->mech)+1);
268 strncpy(tagged, CryptTok(crypt_mech->mech), CryptTokSize(crypt_mech->mech));
269 strncpy(tagged+CryptTokSize(crypt_mech->mech), untagged, strlen(untagged));
274 char* parse_arguments(int argc, char **argv)
277 const char* options = "a:d:lm:u:y:5:";
279 umkpasswd_conf = (umkpasswd_conf_t*)MyMalloc(sizeof(umkpasswd_conf_t));
281 umkpasswd_conf->flags = 0;
282 umkpasswd_conf->debuglevel = 0;
283 umkpasswd_conf->operclass = 0;
284 umkpasswd_conf->user = NULL;
285 umkpasswd_conf->mech = NULL;
288 len = strlen(DPATH) + strlen(CPATH) + 2;
289 umkpasswd_conf->conf = (char *)MyMalloc(len*sizeof(char));
290 memset(umkpasswd_conf->conf, 0, len*sizeof(char));
291 ircd_strncpy(umkpasswd_conf->conf, DPATH, strlen(DPATH));
292 *((umkpasswd_conf->conf) + strlen(DPATH)) = '/';
293 ircd_strncpy((umkpasswd_conf->conf) + strlen(DPATH) + 1, CPATH, strlen(CPATH));
297 while ((EOF != (c = getopt(argc, argv, options))) && !len)
306 umkpasswd_conf->operclass = atoi(optarg);
307 if (umkpasswd_conf->operclass < 0)
308 umkpasswd_conf->operclass = 0;
312 if(umkpasswd_conf->flags && ACT_UPDOPER)
314 fprintf(stderr, "-a and -u are mutually exclussive. Use either or neither.\n");
315 abort(); /* b0rk b0rk b0rk */
318 umkpasswd_conf->flags |= ACT_UPDOPER;
319 umkpasswd_conf->user = optarg;
323 umkpasswd_conf->mech = optarg;
333 umkpasswd_conf->debuglevel = atoi(optarg);
334 if (umkpasswd_conf->debuglevel < 0)
335 umkpasswd_conf->debuglevel = 0;
339 umkpasswd_conf->conf = optarg;
343 if(umkpasswd_conf->flags && ACT_UPDOPER)
345 fprintf(stderr, "-a and -u are mutually exclussive. Use either or neither.\n");
346 abort(); /* b0rk b0rk b0rk */
349 umkpasswd_conf->flags |= ACT_ADDOPER;
350 umkpasswd_conf->user = optarg;
354 /* unknown option - spit out syntax and b0rk */
361 Debug((DEBUG_DEBUG, "flags = %d", umkpasswd_conf->flags));
362 Debug((DEBUG_DEBUG, "operclass = %d", umkpasswd_conf->operclass));
363 Debug((DEBUG_DEBUG, "debug = %d", umkpasswd_conf->debuglevel));
365 if (NULL != umkpasswd_conf->mech)
366 Debug((DEBUG_DEBUG, "mech = %s", umkpasswd_conf->mech));
368 Debug((DEBUG_DEBUG, "mech is unset"));
370 if (NULL != umkpasswd_conf->conf)
371 Debug((DEBUG_DEBUG, "conf = %s", umkpasswd_conf->conf));
373 Debug((DEBUG_DEBUG, "conf is unset"));
375 if (NULL != umkpasswd_conf->user)
376 Debug((DEBUG_DEBUG, "user = %s", umkpasswd_conf->user));
378 Debug((DEBUG_DEBUG, "user is unset"));
380 /* anything left over should be password */
384 int main(int argc, char **argv)
386 char* pw, *crypted_pw;
388 crypt_mechs_root = (crypt_mechs_t*)MyMalloc(sizeof(crypt_mechs_t));
389 crypt_mechs_root->mech = NULL;
390 crypt_mechs_root->next = crypt_mechs_root->prev = NULL;
398 pw = parse_arguments(argc, argv);
401 if (NULL == umkpasswd_conf->mech)
403 fprintf(stderr, "No mechanism specified.\n");
409 pw = getpass("Password: ");
411 crypted_pw = crypt_pass(pw, umkpasswd_conf->mech);
413 printf("Crypted Pass: %s\n", crypted_pw);
414 memset(pw, 0, strlen(pw));