]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * m_challenge.c: Allows an IRC Operator to securely authenticate. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 | * USA | |
23 | * | |
24 | * $Id: m_challenge.c 3161 2007-01-25 07:23:01Z nenolod $ | |
25 | */ | |
26 | ||
27 | #include "stdinc.h" | |
28 | ||
29 | #ifdef HAVE_LIBCRYPTO | |
30 | #include <openssl/pem.h> | |
31 | #include <openssl/rand.h> | |
32 | #include <openssl/rsa.h> | |
33 | #include <openssl/md5.h> | |
34 | #include <openssl/bn.h> | |
35 | #include <openssl/evp.h> | |
36 | #include <openssl/err.h> | |
37 | #endif | |
38 | ||
39 | #include "memory.h" | |
40 | #include "client.h" | |
41 | #include "ircd.h" | |
42 | #include "modules.h" | |
43 | #include "numeric.h" | |
44 | #include "send.h" | |
45 | #include "s_conf.h" | |
46 | #include "msg.h" | |
47 | #include "parse.h" | |
48 | #include "irc_string.h" | |
49 | #include "s_log.h" | |
50 | #include "s_user.h" | |
51 | #include "cache.h" | |
52 | #include "s_newconf.h" | |
53 | ||
54 | #define CHALLENGE_WIDTH BUFSIZE - (NICKLEN + HOSTLEN + 12) | |
55 | #define CHALLENGE_EXPIRES 180 /* 180 seconds should be more than long enough */ | |
56 | #define CHALLENGE_SECRET_LENGTH 128 /* how long our challenge secret should be */ | |
57 | ||
58 | #ifndef HAVE_LIBCRYPTO | |
59 | /* Maybe this should be an error or something?-davidt */ | |
60 | /* now it is -larne */ | |
61 | static int challenge_load(void) | |
62 | { | |
63 | #ifndef STATIC_MODULES | |
64 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
65 | "Challenge module not loaded because OpenSSL is not available."); | |
66 | ilog(L_MAIN, "Challenge module not loaded because OpenSSL is not available."); | |
67 | return -1; | |
68 | #else | |
69 | return 0; | |
70 | #endif | |
71 | } | |
72 | ||
73 | DECLARE_MODULE_AV1(challenge, challenge_load, NULL, NULL, NULL, NULL, "$Revision: 3161 $"); | |
74 | #else | |
75 | ||
76 | static int m_challenge(struct Client *, struct Client *, int, const char **); | |
77 | ||
78 | /* We have openssl support, so include /CHALLENGE */ | |
79 | struct Message challenge_msgtab = { | |
80 | "CHALLENGE", 0, 0, 0, MFLG_SLOW, | |
81 | {mg_unreg, {m_challenge, 2}, mg_ignore, mg_ignore, mg_ignore, {m_challenge, 2}} | |
82 | }; | |
83 | ||
84 | mapi_clist_av1 challenge_clist[] = { &challenge_msgtab, NULL }; | |
85 | DECLARE_MODULE_AV1(challenge, NULL, NULL, challenge_clist, NULL, NULL, "$Revision: 3161 $"); | |
86 | ||
87 | static int generate_challenge(char **r_challenge, char **r_response, RSA * key); | |
88 | ||
89 | static void | |
90 | cleanup_challenge(struct Client *target_p) | |
91 | { | |
92 | if(target_p->localClient == NULL) | |
93 | return; | |
94 | ||
95 | MyFree(target_p->localClient->challenge); | |
96 | MyFree(target_p->localClient->opername); | |
97 | target_p->localClient->challenge = NULL; | |
98 | target_p->localClient->opername = NULL; | |
99 | target_p->localClient->chal_time = 0; | |
100 | } | |
101 | ||
102 | /* | |
103 | * m_challenge - generate RSA challenge for wouldbe oper | |
104 | * parv[0] = sender prefix | |
105 | * parv[1] = operator to challenge for, or +response | |
106 | * | |
107 | */ | |
108 | static int | |
109 | m_challenge(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
110 | { | |
111 | struct oper_conf *oper_p; | |
112 | char *challenge = NULL; /* to placate gcc */ | |
113 | char chal_line[CHALLENGE_WIDTH]; | |
114 | unsigned char *b_response; | |
115 | size_t cnt; | |
116 | int len = 0; | |
117 | ||
118 | /* if theyre an oper, reprint oper motd and ignore */ | |
119 | if(IsOper(source_p)) | |
120 | { | |
121 | sendto_one(source_p, form_str(RPL_YOUREOPER), me.name, source_p->name); | |
122 | send_oper_motd(source_p); | |
123 | return 0; | |
124 | } | |
125 | ||
126 | if(*parv[1] == '+') | |
127 | { | |
128 | /* Ignore it if we aren't expecting this... -A1kmm */ | |
129 | if(!source_p->localClient->challenge) | |
130 | return 0; | |
131 | ||
132 | if((CurrentTime - source_p->localClient->chal_time) > CHALLENGE_EXPIRES) | |
133 | { | |
134 | sendto_one(source_p, form_str(ERR_PASSWDMISMATCH), me.name, source_p->name); | |
135 | ilog(L_FOPER, "EXPIRED CHALLENGE (%s) by (%s!%s@%s) (%s)", | |
136 | source_p->localClient->opername, source_p->name, | |
137 | source_p->username, source_p->host, source_p->sockhost); | |
138 | ||
139 | if(ConfigFileEntry.failed_oper_notice) | |
140 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
141 | "Expired CHALLENGE attempt by %s (%s@%s)", | |
142 | source_p->name, source_p->username, | |
143 | source_p->host); | |
144 | cleanup_challenge(source_p); | |
145 | return 0; | |
146 | } | |
147 | ||
148 | b_response = ircd_base64_decode((const unsigned char *)++parv[1], strlen(parv[1]), &len); | |
149 | ||
150 | if(len != SHA_DIGEST_LENGTH || | |
151 | memcmp(source_p->localClient->challenge, b_response, SHA_DIGEST_LENGTH)) | |
152 | { | |
153 | sendto_one(source_p, form_str(ERR_PASSWDMISMATCH), me.name, source_p->name); | |
154 | ilog(L_FOPER, "FAILED CHALLENGE (%s) by (%s!%s@%s) (%s)", | |
155 | source_p->localClient->opername, source_p->name, | |
156 | source_p->username, source_p->host, source_p->sockhost); | |
157 | ||
158 | if(ConfigFileEntry.failed_oper_notice) | |
159 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
160 | "Failed CHALLENGE attempt by %s (%s@%s)", | |
161 | source_p->name, source_p->username, | |
162 | source_p->host); | |
163 | ||
164 | MyFree(b_response); | |
165 | cleanup_challenge(source_p); | |
166 | return 0; | |
167 | } | |
168 | ||
169 | MyFree(b_response); | |
170 | ||
171 | oper_p = find_oper_conf(source_p->username, source_p->orighost, | |
172 | source_p->sockhost, | |
173 | source_p->localClient->opername); | |
174 | ||
175 | if(oper_p == NULL) | |
176 | { | |
177 | sendto_one(source_p, form_str(ERR_NOOPERHOST), | |
178 | me.name, source_p->name); | |
179 | ilog(L_FOPER, "FAILED OPER (%s) by (%s!%s@%s) (%s)", | |
180 | source_p->localClient->opername, source_p->name, | |
181 | source_p->username, source_p->host, | |
182 | source_p->sockhost); | |
183 | ||
184 | if(ConfigFileEntry.failed_oper_notice) | |
185 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
186 | "Failed CHALLENGE attempt - host mismatch by %s (%s@%s)", | |
187 | source_p->name, source_p->username, | |
188 | source_p->host); | |
189 | return 0; | |
190 | } | |
191 | ||
192 | cleanup_challenge(source_p); | |
193 | ||
194 | oper_up(source_p, oper_p); | |
195 | ||
196 | ilog(L_OPERED, "OPER %s by %s!%s@%s (%s)", | |
197 | source_p->localClient->opername, source_p->name, | |
198 | source_p->username, source_p->host, source_p->sockhost); | |
199 | return 0; | |
200 | } | |
201 | ||
202 | cleanup_challenge(source_p); | |
203 | ||
204 | oper_p = find_oper_conf(source_p->username, source_p->orighost, | |
205 | source_p->sockhost, parv[1]); | |
206 | ||
207 | if(oper_p == NULL) | |
208 | { | |
209 | sendto_one(source_p, form_str(ERR_NOOPERHOST), me.name, source_p->name); | |
210 | ilog(L_FOPER, "FAILED OPER (%s) by (%s!%s@%s) (%s)", | |
211 | parv[1], source_p->name, | |
212 | source_p->username, source_p->host, source_p->sockhost); | |
213 | ||
214 | if(ConfigFileEntry.failed_oper_notice) | |
215 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
216 | "Failed CHALLENGE attempt - host mismatch by %s (%s@%s)", | |
217 | source_p->name, source_p->username, source_p->host); | |
218 | return 0; | |
219 | } | |
220 | ||
221 | if(!oper_p->rsa_pubkey) | |
222 | { | |
223 | sendto_one_notice(source_p, ":I'm sorry, PK authentication is not enabled for your oper{} block."); | |
224 | return 0; | |
225 | } | |
226 | ||
227 | if(!generate_challenge(&challenge, &(source_p->localClient->challenge), oper_p->rsa_pubkey)) | |
228 | { | |
229 | char *chal = challenge; | |
230 | source_p->localClient->chal_time = CurrentTime; | |
231 | for(;;) | |
232 | { | |
233 | cnt = strlcpy(chal_line, chal, CHALLENGE_WIDTH); | |
234 | sendto_one(source_p, form_str(RPL_RSACHALLENGE2), me.name, source_p->name, chal_line); | |
235 | if(cnt > CHALLENGE_WIDTH) | |
236 | chal += CHALLENGE_WIDTH - 1; | |
237 | else | |
238 | break; | |
239 | ||
240 | } | |
241 | sendto_one(source_p, form_str(RPL_ENDOFRSACHALLENGE2), | |
242 | me.name, source_p->name); | |
243 | MyFree(challenge); | |
244 | DupString(source_p->localClient->opername, oper_p->name); | |
245 | } | |
246 | else | |
247 | sendto_one_notice(source_p, ":Failed to generate challenge."); | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | static int | |
253 | get_randomness(unsigned char *buf, int length) | |
254 | { | |
255 | /* Seed OpenSSL PRNG with EGD enthropy pool -kre */ | |
256 | if(ConfigFileEntry.use_egd && (ConfigFileEntry.egdpool_path != NULL)) | |
257 | { | |
258 | if(RAND_egd(ConfigFileEntry.egdpool_path) == -1) | |
259 | return -1; | |
260 | } | |
261 | ||
262 | if(RAND_status()) | |
263 | { | |
264 | if(RAND_bytes(buf, length) > 0) | |
265 | return 1; | |
266 | } | |
267 | else { | |
268 | if(RAND_pseudo_bytes(buf, length) >= 0) | |
269 | return 1; | |
270 | } | |
271 | return 0; | |
272 | } | |
273 | ||
274 | static int | |
275 | generate_challenge(char **r_challenge, char **r_response, RSA * rsa) | |
276 | { | |
277 | SHA_CTX ctx; | |
278 | unsigned char secret[CHALLENGE_SECRET_LENGTH], *tmp; | |
279 | unsigned long length; | |
280 | unsigned long e = 0; | |
281 | unsigned long cnt = 0; | |
282 | int ret; | |
283 | ||
284 | if(!rsa) | |
285 | return -1; | |
286 | if(get_randomness(secret, CHALLENGE_SECRET_LENGTH)) | |
287 | { | |
288 | SHA1_Init(&ctx); | |
289 | SHA1_Update(&ctx, (u_int8_t *)secret, CHALLENGE_SECRET_LENGTH); | |
290 | *r_response = MyMalloc(SHA_DIGEST_LENGTH); | |
291 | SHA1_Final((u_int8_t *)*r_response, &ctx); | |
292 | ||
293 | length = RSA_size(rsa); | |
294 | tmp = MyMalloc(length); | |
295 | ret = RSA_public_encrypt(CHALLENGE_SECRET_LENGTH, secret, tmp, rsa, RSA_PKCS1_OAEP_PADDING); | |
296 | ||
297 | if (ret >= 0) | |
298 | { | |
299 | *r_challenge = (char *)ircd_base64_encode(tmp, ret); | |
300 | MyFree(tmp); | |
301 | return 0; | |
302 | } | |
303 | MyFree(tmp); | |
304 | MyFree(*r_response); | |
305 | *r_response = NULL; | |
306 | } | |
307 | ||
308 | ERR_load_crypto_strings(); | |
309 | while ((cnt < 100) && (e = ERR_get_error())) | |
310 | { | |
311 | ilog(L_MAIN, "SSL error: %s", ERR_error_string(e, 0)); | |
312 | cnt++; | |
313 | } | |
314 | ||
315 | return (-1); | |
316 | } | |
317 | ||
318 | #endif /* HAVE_LIBCRYPTO */ |