2 * ircd-ratbox: A slightly useful ircd.
3 * m_oper.c: Makes a user an IRC Operator.
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
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.
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.
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
24 * $Id: m_oper.c 22407 2006-05-07 15:39:43Z androsyn $
30 #include <openssl/pem.h>
31 #include <openssl/rand.h>
32 #include <openssl/rsa.h>
33 #include <openssl/sha.h>
34 #include <openssl/bn.h>
35 #include <openssl/evp.h>
36 #include <openssl/err.h>
42 #include "irc_string.h"
47 #include "s_newconf.h"
57 #define CHALLENGE_WIDTH BUFSIZE - (NICKLEN + HOSTLEN + 12)
58 #define CHALLENGE_EXPIRES 180 /* 180 seconds should be more than long enough */
59 #define CHALLENGE_SECRET_LENGTH 128 /* how long our challenge secret should be */
61 static int m_oper(struct Client
*, struct Client
*, int, const char **);
62 static int m_challenge(struct Client
*, struct Client
*, int, const char **);
64 struct Message oper_msgtab
= {
65 "OPER", 0, 0, 0, MFLG_SLOW
,
66 {mg_unreg
, {m_oper
, 3}, mg_ignore
, mg_ignore
, mg_ignore
, {m_oper
, 3}}
68 struct Message challenge_msgtab
= {
69 "CHALLENGE", 0, 0, 0, MFLG_SLOW
,
70 {mg_unreg
, {m_challenge
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_challenge
, 2}}
74 mapi_clist_av1 oper_clist
[] = { &oper_msgtab
, &challenge_msgtab
, NULL
};
75 DECLARE_MODULE_AV1(oper
, NULL
, NULL
, oper_clist
, NULL
, NULL
, "$Revision: 22407 $");
77 static int match_oper_password(const char *password
, struct oper_conf
*oper_p
);
82 * parv[0] = sender prefix
84 * parv[2] = oper password
87 m_oper(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
89 struct oper_conf
*oper_p
;
98 sendto_one(source_p
, form_str(RPL_YOUREOPER
), me
.name
, source_p
->name
);
99 send_oper_motd(source_p
);
103 /* end the grace period */
104 if(!IsFloodDone(source_p
))
105 flood_endgrace(source_p
);
107 oper_p
= find_oper_conf(source_p
->username
, source_p
->host
,
108 source_p
->sockhost
, name
);
112 sendto_one(source_p
, form_str(ERR_NOOPERHOST
), me
.name
, source_p
->name
);
113 ilog(L_FOPER
, "FAILED OPER (%s) by (%s!%s@%s)",
114 name
, source_p
->name
,
115 source_p
->username
, source_p
->host
);
117 if(ConfigFileEntry
.failed_oper_notice
)
119 sendto_realops_flags(UMODE_ALL
, L_ALL
,
120 "Failed OPER attempt - host mismatch by %s (%s@%s)",
121 source_p
->name
, source_p
->username
, source_p
->host
);
127 if(match_oper_password(password
, oper_p
))
129 oper_up(source_p
, oper_p
);
131 ilog(L_OPERED
, "OPER %s by %s!%s@%s",
132 name
, source_p
->name
, source_p
->username
, source_p
->host
);
137 sendto_one(source_p
, form_str(ERR_PASSWDMISMATCH
),
138 me
.name
, source_p
->name
);
140 ilog(L_FOPER
, "FAILED OPER (%s) by (%s!%s@%s)",
141 name
, source_p
->name
, source_p
->username
, source_p
->host
);
143 if(ConfigFileEntry
.failed_oper_notice
)
145 sendto_realops_flags(UMODE_ALL
, L_ALL
,
146 "Failed OPER attempt by %s (%s@%s)",
147 source_p
->name
, source_p
->username
, source_p
->host
);
155 * match_oper_password
157 * inputs - pointer to given password
159 * output - YES or NO if match
160 * side effects - none
163 match_oper_password(const char *password
, struct oper_conf
*oper_p
)
167 /* passwd may be NULL pointer. Head it off at the pass... */
168 if(EmptyString(oper_p
->passwd
))
171 if(IsOperConfEncrypted(oper_p
))
173 /* use first two chars of the password they send in as salt */
174 /* If the password in the conf is MD5, and ircd is linked
175 * to scrypt on FreeBSD, or the standard crypt library on
176 * glibc Linux, then this code will work fine on generating
177 * the proper encrypted hash for comparison.
179 if(!EmptyString(password
))
180 encr
= crypt(password
, oper_p
->passwd
);
187 if(strcmp(encr
, oper_p
->passwd
) == 0)
193 #ifndef HAVE_LIBCRYPTO
195 m_challenge(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
197 sendto_one(source_p
, ":%s NOTICE %s :Challenge not implemented ",
198 me
.name
, source_p
->name
);
204 static int generate_challenge(char **r_challenge
, char **r_response
, RSA
* rsa
);
207 cleanup_challenge(struct Client
*target_p
)
209 if(target_p
->localClient
== NULL
)
212 MyFree(target_p
->localClient
->challenge
);
213 MyFree(target_p
->localClient
->opername
);
214 target_p
->localClient
->challenge
= NULL
;
215 target_p
->localClient
->opername
= NULL
;
216 target_p
->localClient
->chal_time
= 0;
220 * m_challenge - generate RSA challenge for wouldbe oper
221 * parv[0] = sender prefix
222 * parv[1] = operator to challenge for, or +response
227 m_challenge(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
229 struct oper_conf
*oper_p
;
231 char chal_line
[CHALLENGE_WIDTH
];
232 unsigned char *b_response
;
238 sendto_one(source_p
, form_str(RPL_YOUREOPER
), me
.name
, source_p
->name
);
239 send_oper_motd(source_p
);
245 if(source_p
->localClient
->challenge
== NULL
)
248 if((CurrentTime
- source_p
->localClient
->chal_time
) > CHALLENGE_EXPIRES
)
250 sendto_one(source_p
, form_str(ERR_PASSWDMISMATCH
), me
.name
, source_p
->name
);
251 ilog(L_FOPER
, "EXPIRED CHALLENGE (%s) by (%s!%s@%s)",
252 source_p
->localClient
->opername
, source_p
->name
,
253 source_p
->username
, source_p
->host
);
255 if(ConfigFileEntry
.failed_oper_notice
)
256 sendto_realops_flags(UMODE_ALL
, L_ALL
,
257 "Expired CHALLENGE attempt by %s (%s@%s)",
258 source_p
->name
, source_p
->username
,
260 cleanup_challenge(source_p
);
264 b_response
= ircd_base64_decode((const unsigned char *)++parv
[1], strlen(parv
[1]), &len
);
266 if(len
!= SHA_DIGEST_LENGTH
||
267 memcmp(source_p
->localClient
->challenge
, b_response
, SHA_DIGEST_LENGTH
))
269 sendto_one(source_p
, form_str(ERR_PASSWDMISMATCH
), me
.name
, source_p
->name
);
270 ilog(L_FOPER
, "FAILED CHALLENGE (%s) by (%s!%s@%s)",
271 source_p
->localClient
->opername
, source_p
->name
,
272 source_p
->username
, source_p
->host
);
274 if(ConfigFileEntry
.failed_oper_notice
)
275 sendto_realops_flags(UMODE_ALL
, L_ALL
,
276 "Failed CHALLENGE attempt by %s (%s@%s)",
277 source_p
->name
, source_p
->username
,
281 cleanup_challenge(source_p
);
287 oper_p
= find_oper_conf(source_p
->username
, source_p
->host
,
289 source_p
->localClient
->opername
);
293 sendto_one(source_p
, form_str(ERR_NOOPERHOST
),
294 me
.name
, source_p
->name
);
295 ilog(L_FOPER
, "FAILED OPER (%s) by (%s!%s@%s)",
296 source_p
->localClient
->opername
, source_p
->name
,
297 source_p
->username
, source_p
->host
);
299 if(ConfigFileEntry
.failed_oper_notice
)
300 sendto_realops_flags(UMODE_ALL
, L_ALL
,
301 "Failed CHALLENGE attempt - host mismatch by %s (%s@%s)",
302 source_p
->name
, source_p
->username
,
307 cleanup_challenge(source_p
);
309 oper_up(source_p
, oper_p
);
311 ilog(L_OPERED
, "OPER %s by %s!%s@%s",
312 source_p
->localClient
->opername
, source_p
->name
,
313 source_p
->username
, source_p
->host
);
317 cleanup_challenge(source_p
);
319 oper_p
= find_oper_conf(source_p
->username
, source_p
->host
,
320 source_p
->sockhost
, parv
[1]);
324 sendto_one(source_p
, form_str(ERR_NOOPERHOST
), me
.name
, source_p
->name
);
325 ilog(L_FOPER
, "FAILED OPER (%s) by (%s!%s@%s)",
326 parv
[1], source_p
->name
,
327 source_p
->username
, source_p
->host
);
329 if(ConfigFileEntry
.failed_oper_notice
)
330 sendto_realops_flags(UMODE_ALL
, L_ALL
,
331 "Failed CHALLENGE attempt - host mismatch by %s (%s@%s)",
332 source_p
->name
, source_p
->username
, source_p
->host
);
336 if(!oper_p
->rsa_pubkey
)
338 sendto_one(source_p
, ":%s NOTICE %s :I'm sorry, PK authentication "
339 "is not enabled for your oper{} block.", me
.name
, parv
[0]);
343 if(!generate_challenge(&challenge
, &(source_p
->localClient
->challenge
), oper_p
->rsa_pubkey
))
345 char *chal
= challenge
;
346 source_p
->localClient
->chal_time
= CurrentTime
;
349 cnt
= strlcpy(chal_line
, chal
, CHALLENGE_WIDTH
);
350 sendto_one(source_p
, form_str(RPL_RSACHALLENGE2
), me
.name
, source_p
->name
, chal_line
);
351 if(cnt
> CHALLENGE_WIDTH
)
352 chal
+= CHALLENGE_WIDTH
- 1;
357 sendto_one(source_p
, form_str(RPL_ENDOFRSACHALLENGE2
),
358 me
.name
, source_p
->name
);
360 DupString(source_p
->localClient
->opername
, oper_p
->name
);
363 sendto_one_notice(source_p
, ":Failed to generate challenge.");
370 get_randomness(unsigned char *buf
, int length
)
372 /* Seed OpenSSL PRNG with EGD enthropy pool -kre */
373 if(ConfigFileEntry
.use_egd
&& (ConfigFileEntry
.egdpool_path
!= NULL
))
375 if(RAND_egd(ConfigFileEntry
.egdpool_path
) == -1)
381 if(RAND_bytes(buf
, length
) > 0)
386 if(RAND_pseudo_bytes(buf
, length
) >= 0)
393 generate_challenge(char **r_challenge
, char **r_response
, RSA
* rsa
)
396 unsigned char secret
[CHALLENGE_SECRET_LENGTH
], *tmp
;
397 unsigned long length
;
399 unsigned long cnt
= 0;
404 if(get_randomness(secret
, CHALLENGE_SECRET_LENGTH
))
407 SHA1_Update(&ctx
, (uint8_t *)secret
, CHALLENGE_SECRET_LENGTH
);
408 *r_response
= MyMalloc(SHA_DIGEST_LENGTH
);
409 SHA1_Final((uint8_t *)*r_response
, &ctx
);
411 length
= RSA_size(rsa
);
412 tmp
= MyMalloc(length
);
413 ret
= RSA_public_encrypt(CHALLENGE_SECRET_LENGTH
, secret
, tmp
, rsa
, RSA_PKCS1_OAEP_PADDING
);
417 *r_challenge
= (char *)ircd_base64_encode(tmp
, ret
);
426 ERR_load_crypto_strings();
427 while ((cnt
< 100) && (e
= ERR_get_error()))
429 ilog(L_MAIN
, "SSL error: %s", ERR_error_string(e
, 0));
436 #endif /* HAVE_LIBCRYPTO */