]>
Commit | Line | Data |
---|---|---|
354b3c0a CP |
1 | #include "chanserv.h" |
2 | #include "../control/control.h" | |
7f32dbdf | 3 | #include "../lib/version.h" |
07bed5f4 | 4 | #include "../lib/irc_string.h" |
d96acfa8 CP |
5 | #include "../core/config.h" |
6 | #include "../lib/hmac.h" | |
7 | #include "../lib/strlfunc.h" | |
8 | #include "../lib/cbc.h" | |
07bed5f4 CP |
9 | #include "authlib.h" |
10 | ||
11 | #include <stdio.h> | |
12 | #include <string.h> | |
7f32dbdf | 13 | |
d96acfa8 CP |
14 | #define KEY_BITS 256 |
15 | #define BLOCK_SIZE 16 | |
16 | ||
7f32dbdf | 17 | MODULE_VERSION(QVERSION); |
354b3c0a CP |
18 | |
19 | int csa_docheckhashpass(void *source, int cargc, char **cargv); | |
07bed5f4 CP |
20 | int csa_docreateaccount(void *source, int cargc, char **cargv); |
21 | int csa_dosettempemail(void *source, int cargc, char **cargv); | |
8f129eab | 22 | int csa_dosetemail(void *source, int cargc, char **cargv); |
07bed5f4 CP |
23 | int csa_doresendemail(void *source, int cargc, char **cargv); |
24 | int csa_doactivateuser(void *source, int cargc, char **cargv); | |
bdeb548f | 25 | int csa_doaddchan(void *source, int argc, char **argv); |
d96acfa8 CP |
26 | static int decrypt_password(unsigned char *secret, int keybits, char *buf, int bufsize, char *encrypted); |
27 | static int hex_to_int(char *input, unsigned char *buf, int buflen); | |
28 | ||
29 | static unsigned char createaccountsecret[KEY_BITS / 8]; | |
30 | static int createaccountsecret_ok = 0; | |
31 | ||
32 | static unsigned char hexlookup[256] = { | |
33 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
34 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
35 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
36 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
37 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, | |
38 | 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xff, 0xff, | |
39 | 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, | |
40 | 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
41 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
42 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c, | |
43 | 0x0d, 0x0e, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
44 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
45 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
46 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
47 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
48 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
49 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
50 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
51 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
52 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
53 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
54 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
55 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
56 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
57 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
58 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff | |
59 | }; | |
354b3c0a CP |
60 | |
61 | void _init(void) { | |
d96acfa8 CP |
62 | sstring *s; |
63 | ||
354b3c0a | 64 | registercontrolhelpcmd("checkhashpass", NO_RELAY, 3, csa_docheckhashpass, "Usage: checkhashpass <username> <digest> ?junk?"); |
b86998d2 | 65 | registercontrolhelpcmd("settempemail", NO_RELAY, 2, csa_dosettempemail, "Usage: settempemail <userid> <email address>"); |
8f129eab | 66 | registercontrolhelpcmd("setemail", NO_RELAY, 3, csa_dosetemail, "Usage: setmail <userid> <timestamp> <email address>"); |
b86998d2 CP |
67 | registercontrolhelpcmd("resendemail", NO_RELAY, 1, csa_doresendemail, "Usage: resendemail <userid>"); |
68 | registercontrolhelpcmd("activateuser", NO_RELAY, 1, csa_doactivateuser, "Usage: activateuser <userid>"); | |
bdeb548f | 69 | registercontrolhelpcmd("addchan", NO_RELAY, 3, csa_doaddchan, "Usage: addchan <channel> <userid> <channel type>"); |
d96acfa8 CP |
70 | |
71 | s=getcopyconfigitem("chanserv","createaccountsecret","",128); | |
72 | if(!s || !s->content || !s->content[0]) { | |
73 | Error("chanserv_relay",ERR_WARNING,"createaccountsecret not set, createaccount disabled."); | |
74 | } else if(s->length != KEY_BITS / 8 * 2 || !hex_to_int(s->content, createaccountsecret, sizeof(createaccountsecret))) { | |
75 | Error("chanserv_relay",ERR_WARNING,"createaccountsecret must be a %d character hex string.", KEY_BITS / 8 * 2); | |
76 | } else { | |
8f129eab | 77 | registercontrolhelpcmd("createaccount", NO_RELAY, 5, csa_docreateaccount, "Usage: createaccount <execute> <username> <email address> <encrypted password> <activate user>"); |
d96acfa8 CP |
78 | createaccountsecret_ok = 1; |
79 | } | |
80 | ||
81 | freesstring(s); | |
354b3c0a CP |
82 | } |
83 | ||
84 | void _fini(void) { | |
85 | deregistercontrolcmd("checkhashpass", csa_docheckhashpass); | |
07bed5f4 | 86 | deregistercontrolcmd("settempemail", csa_dosettempemail); |
8f129eab | 87 | deregistercontrolcmd("setemail", csa_dosetemail); |
07bed5f4 CP |
88 | deregistercontrolcmd("resendemail", csa_doresendemail); |
89 | deregistercontrolcmd("activateuser", csa_doactivateuser); | |
bdeb548f | 90 | deregistercontrolcmd("addchan", csa_doaddchan); |
d96acfa8 CP |
91 | |
92 | if(createaccountsecret_ok) | |
93 | deregistercontrolcmd("createaccount", csa_docreateaccount); | |
354b3c0a CP |
94 | } |
95 | ||
96 | int csa_docheckhashpass(void *source, int cargc, char **cargv) { | |
97 | nick *sender=(nick *)source; | |
caee16ec | 98 | reguser *rup; |
354b3c0a CP |
99 | char *flags; |
100 | ||
101 | if(cargc<3) { | |
102 | controlreply(sender, "CHECKHASHPASS FAIL args"); | |
103 | return CMD_ERROR; | |
104 | } | |
105 | ||
106 | if (!(rup=findreguserbynick(cargv[0]))) { | |
107 | controlreply(sender, "CHECKHASHPASS FAIL user"); | |
108 | return CMD_OK; | |
109 | } | |
110 | ||
111 | flags = printflags(QUFLAG_ALL & rup->flags, ruflags); | |
112 | if(UHasSuspension(rup)) { | |
113 | controlreply(sender, "CHECKHASHPASS FAIL suspended %s %s %u", rup->username, flags, rup->ID); | |
caee16ec CP |
114 | } else if(UIsInactive(rup)) { |
115 | controlreply(sender, "CHECKHASHPASS FAIL inactive %s %s %u", rup->username, flags, rup->ID); | |
354b3c0a CP |
116 | } else if(!checkhashpass(rup, cargc<3?NULL:cargv[2], cargv[1])) { |
117 | controlreply(sender, "CHECKHASHPASS FAIL digest %s %s %u", rup->username, flags, rup->ID); | |
118 | } else { | |
119 | controlreply(sender, "CHECKHASHPASS OK %s %s %u %s", rup->username, flags, rup->ID, rup->email?rup->email->content:"-"); | |
120 | } | |
121 | ||
122 | return CMD_OK; | |
123 | } | |
07bed5f4 CP |
124 | |
125 | static char *email_to_error(char *email) { | |
126 | maildomain *mdp, *smdp; | |
127 | char *local; | |
128 | char *dupemail; | |
129 | int found = 0; | |
130 | maillock *mlp; | |
131 | reguser *ruh; | |
132 | ||
3cfa791f | 133 | switch(csa_checkeboy_r(email)) { |
07bed5f4 | 134 | case -1: break; |
3cfa791f CP |
135 | case QM_EMAILTOOSHORT: return "emailshort"; |
136 | case QM_EMAILNOAT: return "emailinvalid"; | |
137 | case QM_EMAILATEND: return "emailinvalid"; | |
138 | case QM_EMAILINVCHR: return "emailinvalid"; | |
139 | case QM_NOTYOUREMAIL: return "emailnotyours"; | |
140 | case QM_INVALIDEMAIL: return "emailinvalid"; | |
141 | default: return "emailunknown"; | |
07bed5f4 CP |
142 | } |
143 | ||
144 | /* maildomain BS... c&p from hello.c */ | |
145 | for(mlp=maillocks;mlp;mlp=mlp->next) { | |
146 | if(!match(mlp->pattern->content, email)) { | |
3cfa791f | 147 | return "emaillocked"; |
07bed5f4 CP |
148 | } |
149 | } | |
150 | ||
151 | dupemail = strdup(email); | |
152 | local=strchr(dupemail, '@'); | |
153 | if(!local) { | |
154 | free(dupemail); | |
3cfa791f | 155 | return "emailunknown"; |
07bed5f4 CP |
156 | } |
157 | *(local++)='\0'; | |
158 | ||
159 | mdp=findnearestmaildomain(local); | |
160 | if(mdp) { | |
161 | for(smdp=mdp; smdp; smdp=smdp->parent) { | |
162 | if(MDIsBanned(smdp)) { | |
163 | free(dupemail); | |
3cfa791f | 164 | return "emaillocked"; |
07bed5f4 CP |
165 | } |
166 | if((smdp->count >= smdp->limit) && (smdp->limit > 0)) { | |
167 | free(dupemail); | |
3cfa791f | 168 | return "emaildomainlimit"; |
07bed5f4 CP |
169 | } |
170 | } | |
171 | } | |
172 | ||
173 | mdp=findmaildomainbydomain(local); | |
174 | if(mdp) { | |
175 | for (ruh=mdp->users; ruh; ruh=ruh->nextbydomain) { | |
176 | if (ruh->localpart) | |
177 | if (!strcasecmp(dupemail, ruh->localpart->content)) { | |
178 | found++; | |
179 | } | |
180 | } | |
181 | ||
182 | if((found >= mdp->actlimit) && (mdp->actlimit > 0)) { | |
183 | free(dupemail); | |
3cfa791f | 184 | return "emailaddresslimit"; |
07bed5f4 CP |
185 | } |
186 | } | |
187 | ||
2fd779e3 GB |
188 | free(dupemail); |
189 | ||
07bed5f4 CP |
190 | return NULL; |
191 | } | |
192 | ||
193 | static void sendemail(reguser *rup) { | |
194 | csdb_createmail(rup, QMAIL_ACTIVATEEMAIL); | |
195 | } | |
196 | ||
197 | int csa_docreateaccount(void *source, int cargc, char **cargv) { | |
198 | nick *sender=(nick *)source; | |
199 | int execute; | |
200 | char *error_username = NULL, *error_password = NULL, *error_email = NULL; | |
201 | char *username = NULL, *password = NULL, *email = NULL; | |
b86998d2 | 202 | char account_info[512]; |
d96acfa8 | 203 | char passbuf[512]; |
07bed5f4 | 204 | int do_create; |
8f129eab | 205 | int activate; |
07bed5f4 | 206 | |
8f129eab | 207 | if(cargc<5) { |
07bed5f4 CP |
208 | controlreply(sender, "CREATEACCOUNT FALSE args"); |
209 | return CMD_ERROR; | |
210 | } | |
211 | ||
212 | execute = cargv[0][0] == '1'; | |
213 | if(strcmp(cargv[1], "0")) | |
214 | username = cargv[1]; | |
215 | if(strcmp(cargv[2], "0")) | |
216 | email = cargv[2]; | |
d96acfa8 CP |
217 | if(strcmp(cargv[3], "0")) { |
218 | int errorcode = decrypt_password(createaccountsecret, KEY_BITS, passbuf, sizeof(passbuf), cargv[3]); | |
219 | if(errorcode) { | |
220 | Error("chanserv_relay",ERR_WARNING,"createaccount unable to decrypt password, error code: %d", errorcode); | |
221 | controlreply(sender, "CREATEACCOUNT FALSE args"); | |
222 | return CMD_ERROR; | |
223 | } | |
224 | password = passbuf; | |
225 | } | |
8f129eab | 226 | activate = cargv[4][0] == '1'; |
07bed5f4 CP |
227 | |
228 | if(username) { | |
229 | if (findreguserbynick(username)) { | |
3cfa791f | 230 | error_username = "usernameinuse"; |
07bed5f4 | 231 | } else if(csa_checkaccountname_r(username)) { |
3cfa791f | 232 | error_username = "usernameinvalid"; |
07bed5f4 CP |
233 | } |
234 | } | |
235 | ||
236 | if(email) | |
237 | error_email = email_to_error(email); | |
238 | ||
239 | if(password) { | |
240 | int r = csa_checkpasswordquality(password); | |
241 | if(r == QM_PWTOSHORT) { | |
3cfa791f | 242 | error_password = "passwordshort"; |
14dc6de9 CP |
243 | } else if(r == QM_PWTOLONG) { |
244 | error_password = "passwordlong"; | |
07bed5f4 | 245 | } else if(r == QM_PWTOWEAK) { |
3cfa791f | 246 | error_password = "passwordweak"; |
07bed5f4 | 247 | } else if(r != -1) { |
3cfa791f | 248 | error_password = "passwordunknown"; |
07bed5f4 CP |
249 | } |
250 | } | |
251 | ||
252 | if(execute && email && password && username && !error_email && !error_password && !error_username) { | |
253 | reguser *rup; | |
254 | do_create = 1; | |
255 | ||
256 | rup = csa_createaccount(username, password, email); | |
07bed5f4 | 257 | |
8f129eab CP |
258 | if(!activate) |
259 | USetInactive(rup); | |
260 | ||
261 | cs_log(sender,"CREATEACCOUNT created auth %s (%s) %s",rup->username,rup->email->content,activate?"(active)": "(inactive)"); | |
07bed5f4 | 262 | csdb_createuser(rup); |
8f129eab | 263 | snprintf(account_info, sizeof(account_info), " %u %lu", rup->ID, (unsigned long)rup->lastpasschange); |
07bed5f4 CP |
264 | |
265 | sendemail(rup); | |
266 | } else { | |
b86998d2 | 267 | account_info[0] = '\0'; |
07bed5f4 CP |
268 | do_create = 0; |
269 | } | |
270 | ||
b86998d2 | 271 | controlreply(sender, "CREATEACCOUNT %s%s%s%s%s%s%s%s", |
07bed5f4 | 272 | do_create ? "TRUE" : "FALSE", |
b86998d2 | 273 | account_info, |
3cfa791f CP |
274 | email && error_email ? " " : "", email && error_email ? error_email : "", |
275 | password && error_password ? " " : "", password && error_password ? error_password : "", | |
276 | username && error_username ? " " : "", username && error_username ? error_username : "" | |
07bed5f4 CP |
277 | ); |
278 | ||
279 | return CMD_OK; | |
280 | } | |
281 | ||
282 | int csa_dosettempemail(void *source, int cargc, char **cargv) { | |
283 | char *email; | |
284 | char *error; | |
285 | reguser *rup; | |
286 | nick *sender=(nick *)source; | |
287 | ||
288 | if(cargc<2) { | |
289 | controlreply(sender, "SETTEMPEMAIL FALSE args"); | |
290 | return CMD_ERROR; | |
291 | } | |
292 | ||
b86998d2 | 293 | rup = findreguserbyID(atoi(cargv[0])); |
07bed5f4 | 294 | if(rup == NULL) { |
b86998d2 | 295 | controlreply(sender, "SETTEMPEMAIL FALSE useridnotexist"); |
07bed5f4 CP |
296 | return CMD_ERROR; |
297 | } | |
298 | ||
299 | if(!UIsInactive(rup)) { | |
3cfa791f | 300 | controlreply(sender, "SETTEMPEMAIL FALSE accountactive"); |
07bed5f4 CP |
301 | return CMD_ERROR; |
302 | } | |
303 | ||
304 | email = cargv[1]; | |
305 | error = email_to_error(email); | |
306 | if(error) { | |
307 | controlreply(sender, "SETTEMPEMAIL FALSE %s", error); | |
308 | return CMD_ERROR; | |
309 | } | |
310 | ||
311 | freesstring(rup->email); | |
312 | rup->email=getsstring(email,EMAILLEN); | |
313 | cs_log(sender,"SETTEMPEMAIL OK username %s email %s",rup->username, rup->email->content); | |
314 | ||
315 | csdb_updateuser(rup); | |
316 | sendemail(rup); | |
317 | ||
318 | controlreply(sender, "SETTEMPEMAIL TRUE"); | |
319 | ||
320 | return CMD_OK; | |
321 | } | |
322 | ||
8f129eab CP |
323 | int csa_dosetemail(void *source, int cargc, char **cargv) { |
324 | char *email; | |
325 | char *error; | |
326 | reguser *rup; | |
327 | nick *sender=(nick *)source; | |
328 | ||
329 | if(cargc<3) { | |
330 | controlreply(sender, "SETEMAIL FALSE args"); | |
331 | return CMD_ERROR; | |
332 | } | |
333 | ||
334 | rup = findreguserbyID(atoi(cargv[0])); | |
335 | if(rup == NULL) { | |
336 | controlreply(sender, "SETEMAIL FALSE useridnotexist"); | |
337 | return CMD_ERROR; | |
338 | } | |
339 | ||
340 | if(UHasStaffPriv(rup)) { | |
341 | controlreply(sender, "SETEMAIL FALSE privuser"); | |
342 | return CMD_ERROR; | |
343 | } | |
344 | ||
345 | if(UHasSuspension(rup)) { | |
346 | controlreply(sender, "SETEMAIL FALSE suspended"); | |
347 | return CMD_ERROR; | |
348 | } | |
349 | ||
350 | if(rup->lastpasschange > atoi(cargv[1])) { | |
351 | controlreply(sender, "SETEMAIL FALSE passwordchanged"); | |
352 | return CMD_ERROR; | |
353 | } | |
354 | ||
355 | email = cargv[2]; | |
356 | error = email_to_error(email); | |
357 | if(error) { | |
358 | controlreply(sender, "SETEMAIL FALSE %s", error); | |
359 | return CMD_ERROR; | |
360 | } | |
361 | ||
362 | freesstring(rup->email); | |
363 | rup->email=getsstring(email,EMAILLEN); | |
364 | cs_log(sender,"SETEMAIL OK username %s email %s",rup->username, rup->email->content); | |
365 | ||
366 | csdb_updateuser(rup); | |
367 | sendemail(rup); | |
368 | ||
369 | controlreply(sender, "SETEMAIL TRUE"); | |
370 | ||
371 | return CMD_OK; | |
372 | } | |
373 | ||
07bed5f4 CP |
374 | int csa_doresendemail(void *source, int cargc, char **cargv) { |
375 | reguser *rup; | |
376 | nick *sender=(nick *)source; | |
377 | ||
378 | if(cargc<1) { | |
379 | controlreply(sender, "RESENDEMAIL FALSE args"); | |
380 | return CMD_ERROR; | |
381 | } | |
382 | ||
b86998d2 | 383 | rup = findreguserbyID(atoi(cargv[0])); |
07bed5f4 | 384 | if(rup == NULL) { |
b86998d2 | 385 | controlreply(sender, "RESENDEMAIL FALSE useridnotexist"); |
07bed5f4 CP |
386 | return CMD_ERROR; |
387 | } | |
388 | ||
389 | if(!UIsInactive(rup)) { | |
3cfa791f | 390 | controlreply(sender, "RESENDEMAIL FALSE accountactive"); |
07bed5f4 CP |
391 | return CMD_ERROR; |
392 | } | |
393 | ||
394 | sendemail(rup); | |
395 | controlreply(sender, "RESENDEMAIL TRUE"); | |
396 | cs_log(sender,"RESENDEMAIL OK username %s",rup->username); | |
397 | ||
398 | return CMD_OK; | |
399 | } | |
400 | ||
401 | int csa_doactivateuser(void *source, int cargc, char **cargv) { | |
402 | reguser *rup; | |
403 | nick *sender=(nick *)source; | |
404 | ||
405 | if(cargc<1) { | |
406 | controlreply(sender, "ACTIVATEUSER FALSE args"); | |
407 | return CMD_ERROR; | |
408 | } | |
409 | ||
b86998d2 | 410 | rup = findreguserbyID(atoi(cargv[0])); |
07bed5f4 | 411 | if(rup == NULL) { |
b86998d2 | 412 | controlreply(sender, "ACTIVATEUSER FALSE useridnotexist"); |
07bed5f4 CP |
413 | return CMD_ERROR; |
414 | } | |
415 | ||
416 | if(!UIsInactive(rup)) { | |
3cfa791f | 417 | controlreply(sender, "ACTIVATEUSER FALSE accountactive"); |
07bed5f4 CP |
418 | return CMD_ERROR; |
419 | } | |
420 | ||
421 | UClearInactive(rup); | |
422 | csdb_updateuser(rup); | |
423 | ||
424 | cs_log(sender,"ACTIVATEUSER OK username %s",rup->username); | |
425 | controlreply(sender, "ACTIVATEUSER TRUE"); | |
426 | ||
427 | return CMD_OK; | |
428 | } | |
d96acfa8 | 429 | |
bdeb548f CP |
430 | int csa_doaddchan(void *source, int cargc, char **cargv) { |
431 | nick *sender=(nick *)source; | |
432 | reguser *rup = getreguserfromnick(sender), *founder; | |
433 | chanindex *cip; | |
434 | short type; | |
435 | regchan *rcp; | |
436 | ||
437 | if(cargc<3) { | |
438 | controlreply(sender, "ADDCHAN FALSE args"); | |
439 | return CMD_ERROR; | |
440 | } | |
441 | ||
442 | if (*cargv[0] != '#' || strlen(cargv[0]) > CHANNELLEN) { | |
443 | controlreply(sender, "ADDCHAN FALSE invalidchannel"); | |
444 | return CMD_ERROR; | |
445 | } | |
446 | ||
447 | if (!(cip=findorcreatechanindex(cargv[0]))) { | |
448 | controlreply(sender, "ADDCHAN FALSE invalidchannel"); | |
449 | return CMD_ERROR; | |
450 | } | |
451 | ||
452 | founder = findreguserbyID(atoi(cargv[1])); | |
453 | if(founder == NULL) { | |
454 | controlreply(sender, "ADDCHAN FALSE useridnotexist"); | |
455 | return CMD_ERROR; | |
456 | } | |
457 | ||
458 | if(UIsInactive(founder)) { | |
459 | controlreply(sender, "ADDCHAN FALSE accountinactive"); | |
460 | return CMD_ERROR; | |
461 | } | |
462 | ||
463 | for(type=CHANTYPES-1;type;type--) | |
464 | if(!ircd_strcmp(chantypes[type]->content, cargv[2])) | |
465 | break; | |
466 | ||
467 | if(!type) { | |
468 | controlreply(sender, "ADDCHAN FALSE invalidchantype"); | |
469 | return CMD_ERROR; | |
470 | } | |
471 | ||
472 | rcp = cs_addchan(cip, sender, rup, founder, QCFLAG_JOINED, type); | |
473 | if(!rcp) { | |
474 | controlreply(sender, "ADDCHAN FALSE alreadyregistered"); | |
475 | return CMD_ERROR; | |
476 | } | |
477 | ||
478 | cs_log(sender, "ADDCHAN %s #%s %s %s", cip->name->content, founder->username, printflags(rcp->flags, rcflags), chantypes[type]->content); | |
e67c3226 | 479 | controlreply(sender, "ADDCHAN TRUE %u", rcp->ID); |
bdeb548f CP |
480 | return CMD_OK; |
481 | } | |
482 | ||
d96acfa8 CP |
483 | static int hex_to_int(char *input, unsigned char *buf, int buflen) { |
484 | int i; | |
485 | for(i=0;i<buflen;i++) { | |
486 | if((0xff == hexlookup[(int)input[i * 2]]) || (0xff == hexlookup[(int)input[i * 2 + 1]])) { | |
487 | return 0; | |
488 | } else { | |
489 | buf[i] = (hexlookup[(int)input[i * 2]] << 4) | hexlookup[(int)input[i * 2 + 1]]; | |
490 | } | |
491 | } | |
492 | return 1; | |
493 | } | |
494 | ||
495 | static int decrypt_password(unsigned char *secret, int keybits, char *buf, int bufsize, char *encrypted) { | |
496 | size_t ciphertextlen, datalen; | |
497 | unsigned char iv[BLOCK_SIZE]; | |
498 | rijndaelcbc *c; | |
499 | int i, j; | |
500 | ||
501 | datalen = strlen(encrypted); | |
502 | if(datalen % 2 != 0) | |
503 | return 1; | |
504 | if(datalen < BLOCK_SIZE * 2 * 2) | |
505 | return 2; | |
506 | ||
507 | if(!hex_to_int(encrypted, iv, BLOCK_SIZE)) | |
508 | return 3; | |
509 | ||
510 | ciphertextlen = (datalen - (BLOCK_SIZE * 2)) / 2; | |
511 | if(ciphertextlen > bufsize || ciphertextlen % BLOCK_SIZE != 0) | |
512 | return 4; | |
513 | ||
514 | if(!hex_to_int(encrypted + BLOCK_SIZE * 2, (unsigned char *)buf, ciphertextlen)) | |
515 | return 5; | |
516 | ||
517 | c = rijndaelcbc_init(secret, keybits, iv, 1); | |
518 | ||
519 | for(i=0;i<ciphertextlen;i+=BLOCK_SIZE) { | |
520 | char *p = &buf[i]; | |
521 | unsigned char *r = rijndaelcbc_decrypt(c, (unsigned char *)p); | |
522 | memcpy(p, r, BLOCK_SIZE); | |
523 | ||
524 | for(j=0;j<BLOCK_SIZE;j++) { | |
525 | if(*(p + j) == '\0') { | |
526 | rijndaelcbc_free(c); | |
527 | return 0; | |
528 | } | |
529 | } | |
530 | } | |
531 | ||
532 | rijndaelcbc_free(c); | |
533 | return 6; | |
534 | } |