2 * IRC - Internet Relay Chat, ircd/ircd_crypt.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.
23 * @brief Core password encryption routines.
26 * This is a new look crypto API for ircu, it can handle different
27 * password formats by the grace of magic tokens at the begining of the
28 * password e.g. $SMD5 for Salted MD5, $CRYPT for native crypt(), etc.
30 * Currently crypt routines are implemented for: the native crypt()
31 * function, Salted MD5 and a plain text mechanism which should only
32 * be used for testing. I intend to add Blowish, 3DES and possibly
33 * SHA1 support as well at some point, but I'll need to check the
34 * possible problems that'll cause with stupid crypto laws.
36 * It's also designed to be "ready" for the modularisation of ircu, so
37 * someone get round to doing it, because I'm not doing it ;)
39 * The plan for Stage B is to semi-modularise the authentication
40 * mechanism to allow authentication against some other sources than
41 * the conf file (whatever takes someones fancy, kerberos, ldap, sql, etc).
43 * -- blessed be, hikari.
47 #include "ircd_crypt.h"
48 #include "ircd_alloc.h"
49 #include "ircd_features.h"
50 #include "ircd_string.h"
53 /* while we're not modular, we need their init functions */
54 #include "ircd_crypt_native.h"
55 #include "ircd_crypt_plain.h"
56 #include "ircd_crypt_smd5.h"
63 crypt_mechs_t* crypt_mechs_root;
65 /** Add a crypt mechanism to the list
66 * @param mechanism Pointer to the mechanism details struct
67 * @return 0 on success, anything else on fail.
69 * This routine registers a new crypt mechanism in the loaded mechanisms list,
70 * making it availabe for comparing passwords.
72 int ircd_crypt_register_mech(crypt_mech_t* mechanism)
74 crypt_mechs_t* crypt_mech;
76 Debug((DEBUG_INFO, "ircd_crypt_register_mech: resistering mechanism: %s", mechanism->shortname));
78 /* try to allocate some memory for the new mechanism */
79 if ((crypt_mech = (crypt_mechs_t*)MyMalloc(sizeof(crypt_mechs_t))) == NULL)
81 /* aww poot, we couldn't get any memory, scream a little then back out */
82 Debug((DEBUG_MALLOC, "ircd_crypt_register_mech: could not allocate memory for %s", mechanism->shortname));
86 /* ok, we have memory, initialise it */
87 memset(crypt_mech, 0, sizeof(crypt_mechs_t));
90 crypt_mech->mech = mechanism;
91 crypt_mech->next = crypt_mech->prev = NULL;
93 /* first of all, is there anything there already? */
94 if(crypt_mechs_root->next == NULL)
96 /* nope, just add ourself */
97 crypt_mechs_root->next = crypt_mechs_root->prev = crypt_mech;
99 /* nice and simple, put ourself at the end */
100 crypt_mech->prev = crypt_mechs_root->prev;
101 crypt_mech->next = NULL;
102 crypt_mechs_root->prev = crypt_mech->prev->next = crypt_mech;
106 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));
107 Debug((DEBUG_INFO, "ircd_crypt_register_mech: %s: %s", crypt_mech->mech->shortname, crypt_mech->mech->description));
111 /** Remove a crypt mechanism from the list
112 * @param mechanism Pointer to the mechanism we want to remove
113 * @return 0 on success, anything else on fail.
115 int ircd_crypt_unregister_mech(crypt_mech_t* mechanism)
121 /** Wrapper for generating a hashed password passed on the supplied password
122 * @param key Pointer to the password we want crypted
123 * @param salt Pointer to the password we're comparing to (for the salt)
124 * @return Pointer to the generated password (must be MyFree()'d).
126 * This is a wrapper function which attempts to establish the password
127 * format and funnel it off to the correct mechanism handler function. The
128 * returned password is compared in the oper_password_match() routine.
130 char* ircd_crypt(const char* key, const char* salt)
132 char *hashed_pass = NULL;
133 const char *temp_hashed_pass, *mysalt;
134 crypt_mechs_t* crypt_mech;
137 assert(NULL != salt);
139 Debug((DEBUG_DEBUG, "ircd_crypt: key is %s", key));
140 Debug((DEBUG_DEBUG, "ircd_crypt: salt is %s", salt));
142 crypt_mech = crypt_mechs_root->next;
144 /* by examining the first n characters of a password string we
145 * can discover what kind of password it is. hopefully. */
148 if (strlen(salt) < crypt_mech->mech->crypt_token_size)
150 /* try the next mechanism instead */
151 Debug((DEBUG_DEBUG, "ircd_crypt: salt is too short, will try next mech at 0x%X", crypt_mech->next));
152 crypt_mech = crypt_mech->next;
156 Debug((DEBUG_DEBUG, "ircd_crypt: comparing %s with %s",
157 salt, crypt_mech->mech->crypt_token));
159 if(0 == ircd_strncmp(crypt_mech->mech->crypt_token, salt, crypt_mech->mech->crypt_token_size))
161 Debug((DEBUG_DEBUG, "ircd_crypt: type is %s",
162 crypt_mech->mech->shortname));
164 /* before we send this all off to the crypt_function, we need to remove
167 /* make sure we won't end up with a password comprised entirely of
169 if(strlen(salt) < crypt_mech->mech->crypt_token_size + 1)
172 mysalt = salt + crypt_mech->mech->crypt_token_size;
174 if(NULL == (temp_hashed_pass = crypt_mech->mech->crypt_function(key, mysalt)))
177 Debug((DEBUG_DEBUG, "ircd_crypt: untagged pass is %s", temp_hashed_pass));
179 /* ok, now we need to prefix the password we just got back
180 with the right tag */
181 if(NULL == (hashed_pass = (char *)MyMalloc(sizeof(char)*strlen(temp_hashed_pass) + crypt_mech->mech->crypt_token_size + 1)))
183 Debug((DEBUG_MALLOC, "ircd_crypt: unable to allocate memory for temp_hashed_pass"));
186 memset(hashed_pass, 0, sizeof(char)*strlen(temp_hashed_pass)
187 +crypt_mech->mech->crypt_token_size + 1);
188 ircd_strncpy(hashed_pass, crypt_mech->mech->crypt_token,
189 crypt_mech->mech->crypt_token_size);
190 ircd_strncpy(hashed_pass + crypt_mech->mech->crypt_token_size, temp_hashed_pass, strlen(temp_hashed_pass));
191 Debug((DEBUG_DEBUG, "ircd_crypt: tagged pass is %s", hashed_pass));
193 Debug((DEBUG_DEBUG, "ircd_crypt: will try next mechansim at 0x%X",
195 crypt_mech = crypt_mech->next;
201 /* try to use native crypt for an old-style (untagged) password */
202 if (strlen(salt) > 2)
204 temp_hashed_pass = (char*)ircd_crypt_native(key, salt);
205 if (!ircd_strcmp(temp_hashed_pass, salt))
206 return strdup(temp_hashed_pass);
213 * This function loads initalises the crypt mechanisms linked list and
214 * currently loads the default mechanisms (Salted MD5, Crypt() and PLAIN).
215 * The last step is only needed while ircu is not properly modular.
217 * When ircu is modular this will be the entry function for the ircd_crypt
221 void ircd_crypt_init(void)
224 if((crypt_mechs_root = MyMalloc(sizeof(crypt_mechs_t))) == NULL)
226 /* awooga - can't allocate memory for the root structure */
227 Debug((DEBUG_MALLOC, "init_crypt: Could not allocate memory for crypt_mechs_root"));
231 crypt_mechs_root->mech = NULL;
232 crypt_mechs_root->next = crypt_mechs_root->prev = NULL;
234 /* temporary kludge until we're modular. manualy call the
235 register funtions for crypt mechanisms */
236 ircd_register_crypt_smd5();
237 ircd_register_crypt_plain();
238 ircd_register_crypt_native();