]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/chanserv_relay.c
CHANSERV: add encryption to createaccount (thanks Cruicky for spotting my bug).
[irc/quakenet/newserv.git] / chanserv / chanserv_relay.c
1 #include "chanserv.h"
2 #include "../control/control.h"
3 #include "../lib/version.h"
4 #include "../lib/irc_string.h"
5 #include "../core/config.h"
6 #include "../lib/hmac.h"
7 #include "../lib/strlfunc.h"
8 #include "../lib/cbc.h"
9 #include "authlib.h"
10
11 #include <stdio.h>
12 #include <string.h>
13
14 #define KEY_BITS 256
15 #define BLOCK_SIZE 16
16
17 MODULE_VERSION(QVERSION);
18
19 int csa_docheckhashpass(void *source, int cargc, char **cargv);
20 int csa_docreateaccount(void *source, int cargc, char **cargv);
21 int csa_dosettempemail(void *source, int cargc, char **cargv);
22 int csa_doresendemail(void *source, int cargc, char **cargv);
23 int csa_doactivateuser(void *source, int cargc, char **cargv);
24 static int decrypt_password(unsigned char *secret, int keybits, char *buf, int bufsize, char *encrypted);
25 static int hex_to_int(char *input, unsigned char *buf, int buflen);
26
27 static unsigned char createaccountsecret[KEY_BITS / 8];
28 static int createaccountsecret_ok = 0;
29
30 static unsigned char hexlookup[256] = {
31 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
32 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
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, 0x00, 0x01,
36 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xff, 0xff,
37 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
38 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c,
41 0x0d, 0x0e, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 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
57 };
58
59 void _init(void) {
60 sstring *s;
61
62 registercontrolhelpcmd("checkhashpass", NO_RELAY, 3, csa_docheckhashpass, "Usage: checkhashpass <username> <digest> ?junk?");
63 registercontrolhelpcmd("settempemail", NO_RELAY, 2, csa_dosettempemail, "Usage: settempemail <userid> <email address>");
64 registercontrolhelpcmd("resendemail", NO_RELAY, 1, csa_doresendemail, "Usage: resendemail <userid>");
65 registercontrolhelpcmd("activateuser", NO_RELAY, 1, csa_doactivateuser, "Usage: activateuser <userid>");
66
67 s=getcopyconfigitem("chanserv","createaccountsecret","",128);
68 if(!s || !s->content || !s->content[0]) {
69 Error("chanserv_relay",ERR_WARNING,"createaccountsecret not set, createaccount disabled.");
70 } else if(s->length != KEY_BITS / 8 * 2 || !hex_to_int(s->content, createaccountsecret, sizeof(createaccountsecret))) {
71 Error("chanserv_relay",ERR_WARNING,"createaccountsecret must be a %d character hex string.", KEY_BITS / 8 * 2);
72 } else {
73 registercontrolhelpcmd("createaccount", NO_RELAY, 4, csa_docreateaccount, "Usage: createaccount <execute> <username> <email address> <encrypted password>");
74 createaccountsecret_ok = 1;
75 }
76
77 freesstring(s);
78 }
79
80 void _fini(void) {
81 deregistercontrolcmd("checkhashpass", csa_docheckhashpass);
82 deregistercontrolcmd("settempemail", csa_dosettempemail);
83 deregistercontrolcmd("resendemail", csa_doresendemail);
84 deregistercontrolcmd("activateuser", csa_doactivateuser);
85
86 if(createaccountsecret_ok)
87 deregistercontrolcmd("createaccount", csa_docreateaccount);
88 }
89
90 int csa_docheckhashpass(void *source, int cargc, char **cargv) {
91 nick *sender=(nick *)source;
92 reguser *rup;
93 char *flags;
94
95 if(cargc<3) {
96 controlreply(sender, "CHECKHASHPASS FAIL args");
97 return CMD_ERROR;
98 }
99
100 if (!(rup=findreguserbynick(cargv[0]))) {
101 controlreply(sender, "CHECKHASHPASS FAIL user");
102 return CMD_OK;
103 }
104
105 flags = printflags(QUFLAG_ALL & rup->flags, ruflags);
106 if(UHasSuspension(rup)) {
107 controlreply(sender, "CHECKHASHPASS FAIL suspended %s %s %u", rup->username, flags, rup->ID);
108 } else if(UIsInactive(rup)) {
109 controlreply(sender, "CHECKHASHPASS FAIL inactive %s %s %u", rup->username, flags, rup->ID);
110 } else if(!checkhashpass(rup, cargc<3?NULL:cargv[2], cargv[1])) {
111 controlreply(sender, "CHECKHASHPASS FAIL digest %s %s %u", rup->username, flags, rup->ID);
112 } else {
113 controlreply(sender, "CHECKHASHPASS OK %s %s %u %s", rup->username, flags, rup->ID, rup->email?rup->email->content:"-");
114 }
115
116 return CMD_OK;
117 }
118
119 static char *email_to_error(char *email) {
120 maildomain *mdp, *smdp;
121 char *local;
122 char *dupemail;
123 int found = 0;
124 maillock *mlp;
125 reguser *ruh;
126
127 switch(csa_checkeboy_r(email)) {
128 case -1: break;
129 case QM_EMAILTOOSHORT: return "emailshort";
130 case QM_EMAILNOAT: return "emailinvalid";
131 case QM_EMAILATEND: return "emailinvalid";
132 case QM_EMAILINVCHR: return "emailinvalid";
133 case QM_NOTYOUREMAIL: return "emailnotyours";
134 case QM_INVALIDEMAIL: return "emailinvalid";
135 default: return "emailunknown";
136 }
137
138 /* maildomain BS... c&p from hello.c */
139 for(mlp=maillocks;mlp;mlp=mlp->next) {
140 if(!match(mlp->pattern->content, email)) {
141 return "emaillocked";
142 }
143 }
144
145 dupemail = strdup(email);
146 local=strchr(dupemail, '@');
147 if(!local) {
148 free(dupemail);
149 return "emailunknown";
150 }
151 *(local++)='\0';
152
153 mdp=findnearestmaildomain(local);
154 if(mdp) {
155 for(smdp=mdp; smdp; smdp=smdp->parent) {
156 if(MDIsBanned(smdp)) {
157 free(dupemail);
158 return "emaillocked";
159 }
160 if((smdp->count >= smdp->limit) && (smdp->limit > 0)) {
161 free(dupemail);
162 return "emaildomainlimit";
163 }
164 }
165 }
166
167 mdp=findmaildomainbydomain(local);
168 if(mdp) {
169 for (ruh=mdp->users; ruh; ruh=ruh->nextbydomain) {
170 if (ruh->localpart)
171 if (!strcasecmp(dupemail, ruh->localpart->content)) {
172 found++;
173 }
174 }
175
176 if((found >= mdp->actlimit) && (mdp->actlimit > 0)) {
177 free(dupemail);
178 return "emailaddresslimit";
179 }
180 }
181
182 return NULL;
183 }
184
185 static void sendemail(reguser *rup) {
186 csdb_createmail(rup, QMAIL_ACTIVATEEMAIL);
187 }
188
189 int csa_docreateaccount(void *source, int cargc, char **cargv) {
190 nick *sender=(nick *)source;
191 int execute;
192 char *error_username = NULL, *error_password = NULL, *error_email = NULL;
193 char *username = NULL, *password = NULL, *email = NULL;
194 char account_info[512];
195 char passbuf[512];
196 int do_create;
197
198 if(cargc<4) {
199 controlreply(sender, "CREATEACCOUNT FALSE args");
200 return CMD_ERROR;
201 }
202
203 execute = cargv[0][0] == '1';
204 if(strcmp(cargv[1], "0"))
205 username = cargv[1];
206 if(strcmp(cargv[2], "0"))
207 email = cargv[2];
208 if(strcmp(cargv[3], "0")) {
209 int errorcode = decrypt_password(createaccountsecret, KEY_BITS, passbuf, sizeof(passbuf), cargv[3]);
210 if(errorcode) {
211 Error("chanserv_relay",ERR_WARNING,"createaccount unable to decrypt password, error code: %d", errorcode);
212 controlreply(sender, "CREATEACCOUNT FALSE args");
213 return CMD_ERROR;
214 }
215 password = passbuf;
216 }
217
218 if(username) {
219 if (findreguserbynick(username)) {
220 error_username = "usernameinuse";
221 } else if(csa_checkaccountname_r(username)) {
222 error_username = "usernameinvalid";
223 }
224 }
225
226 if(email)
227 error_email = email_to_error(email);
228
229 if(password) {
230 int r = csa_checkpasswordquality(password);
231 if(r == QM_PWTOSHORT) {
232 error_password = "passwordshort";
233 } else if(r == QM_PWTOWEAK) {
234 error_password = "passwordweak";
235 } else if(r != -1) {
236 error_password = "passwordunknown";
237 }
238 }
239
240 if(execute && email && password && username && !error_email && !error_password && !error_username) {
241 reguser *rup;
242 do_create = 1;
243
244 rup = csa_createaccount(username, password, email);
245 USetInactive(rup);
246
247 cs_log(sender,"CREATEACCOUNT created auth %s (%s)",rup->username,rup->email->content);
248 csdb_createuser(rup);
249 snprintf(account_info, sizeof(account_info), " %u", rup->ID);
250
251 sendemail(rup);
252 } else {
253 account_info[0] = '\0';
254 do_create = 0;
255 }
256
257 controlreply(sender, "CREATEACCOUNT %s%s%s%s%s%s%s%s",
258 do_create ? "TRUE" : "FALSE",
259 account_info,
260 email && error_email ? " " : "", email && error_email ? error_email : "",
261 password && error_password ? " " : "", password && error_password ? error_password : "",
262 username && error_username ? " " : "", username && error_username ? error_username : ""
263 );
264
265 return CMD_OK;
266 }
267
268 int csa_dosettempemail(void *source, int cargc, char **cargv) {
269 char *email;
270 char *error;
271 reguser *rup;
272 nick *sender=(nick *)source;
273
274 if(cargc<2) {
275 controlreply(sender, "SETTEMPEMAIL FALSE args");
276 return CMD_ERROR;
277 }
278
279 rup = findreguserbyID(atoi(cargv[0]));
280 if(rup == NULL) {
281 controlreply(sender, "SETTEMPEMAIL FALSE useridnotexist");
282 return CMD_ERROR;
283 }
284
285 if(!UIsInactive(rup)) {
286 controlreply(sender, "SETTEMPEMAIL FALSE accountactive");
287 return CMD_ERROR;
288 }
289
290 email = cargv[1];
291 error = email_to_error(email);
292 if(error) {
293 controlreply(sender, "SETTEMPEMAIL FALSE %s", error);
294 return CMD_ERROR;
295 }
296
297 freesstring(rup->email);
298 rup->email=getsstring(email,EMAILLEN);
299 cs_log(sender,"SETTEMPEMAIL OK username %s email %s",rup->username, rup->email->content);
300
301 csdb_updateuser(rup);
302 sendemail(rup);
303
304 controlreply(sender, "SETTEMPEMAIL TRUE");
305
306 return CMD_OK;
307 }
308
309 int csa_doresendemail(void *source, int cargc, char **cargv) {
310 reguser *rup;
311 nick *sender=(nick *)source;
312
313 if(cargc<1) {
314 controlreply(sender, "RESENDEMAIL FALSE args");
315 return CMD_ERROR;
316 }
317
318 rup = findreguserbyID(atoi(cargv[0]));
319 if(rup == NULL) {
320 controlreply(sender, "RESENDEMAIL FALSE useridnotexist");
321 return CMD_ERROR;
322 }
323
324 if(!UIsInactive(rup)) {
325 controlreply(sender, "RESENDEMAIL FALSE accountactive");
326 return CMD_ERROR;
327 }
328
329 sendemail(rup);
330 controlreply(sender, "RESENDEMAIL TRUE");
331 cs_log(sender,"RESENDEMAIL OK username %s",rup->username);
332
333 return CMD_OK;
334 }
335
336 int csa_doactivateuser(void *source, int cargc, char **cargv) {
337 reguser *rup;
338 nick *sender=(nick *)source;
339
340 if(cargc<1) {
341 controlreply(sender, "ACTIVATEUSER FALSE args");
342 return CMD_ERROR;
343 }
344
345 rup = findreguserbyID(atoi(cargv[0]));
346 if(rup == NULL) {
347 controlreply(sender, "ACTIVATEUSER FALSE useridnotexist");
348 return CMD_ERROR;
349 }
350
351 if(!UIsInactive(rup)) {
352 controlreply(sender, "ACTIVATEUSER FALSE accountactive");
353 return CMD_ERROR;
354 }
355
356 UClearInactive(rup);
357 csdb_updateuser(rup);
358
359 cs_log(sender,"ACTIVATEUSER OK username %s",rup->username);
360 controlreply(sender, "ACTIVATEUSER TRUE");
361
362 return CMD_OK;
363 }
364
365 static int hex_to_int(char *input, unsigned char *buf, int buflen) {
366 int i;
367 for(i=0;i<buflen;i++) {
368 if((0xff == hexlookup[(int)input[i * 2]]) || (0xff == hexlookup[(int)input[i * 2 + 1]])) {
369 return 0;
370 } else {
371 buf[i] = (hexlookup[(int)input[i * 2]] << 4) | hexlookup[(int)input[i * 2 + 1]];
372 }
373 }
374 return 1;
375 }
376
377 static int decrypt_password(unsigned char *secret, int keybits, char *buf, int bufsize, char *encrypted) {
378 size_t ciphertextlen, datalen;
379 unsigned char iv[BLOCK_SIZE];
380 rijndaelcbc *c;
381 int i, j;
382
383 datalen = strlen(encrypted);
384 if(datalen % 2 != 0)
385 return 1;
386 if(datalen < BLOCK_SIZE * 2 * 2)
387 return 2;
388
389 if(!hex_to_int(encrypted, iv, BLOCK_SIZE))
390 return 3;
391
392 ciphertextlen = (datalen - (BLOCK_SIZE * 2)) / 2;
393 if(ciphertextlen > bufsize || ciphertextlen % BLOCK_SIZE != 0)
394 return 4;
395
396 if(!hex_to_int(encrypted + BLOCK_SIZE * 2, (unsigned char *)buf, ciphertextlen))
397 return 5;
398
399 c = rijndaelcbc_init(secret, keybits, iv, 1);
400
401 for(i=0;i<ciphertextlen;i+=BLOCK_SIZE) {
402 char *p = &buf[i];
403 unsigned char *r = rijndaelcbc_decrypt(c, (unsigned char *)p);
404 memcpy(p, r, BLOCK_SIZE);
405
406 for(j=0;j<BLOCK_SIZE;j++) {
407 if(*(p + j) == '\0') {
408 rijndaelcbc_free(c);
409 return 0;
410 }
411 }
412 }
413
414 rijndaelcbc_free(c);
415 return 6;
416 }