]>
jfr.im git - irc/quakenet/newserv.git/blob - chanserv/chanserv_relay.c
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"
17 MODULE_VERSION(QVERSION
);
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_dosetemail(void *source
, int cargc
, char **cargv
);
23 int csa_doresendemail(void *source
, int cargc
, char **cargv
);
24 int csa_doactivateuser(void *source
, int cargc
, char **cargv
);
25 int csa_doaddchan(void *source
, int argc
, char **argv
);
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
);
29 static unsigned char createaccountsecret
[KEY_BITS
/ 8];
30 static int createaccountsecret_ok
= 0;
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
64 registercontrolhelpcmd("checkhashpass", NO_RELAY
, 3, csa_docheckhashpass
, "Usage: checkhashpass <username> <digest> ?junk?");
65 registercontrolhelpcmd("settempemail", NO_RELAY
, 2, csa_dosettempemail
, "Usage: settempemail <userid> <email address>");
66 registercontrolhelpcmd("setemail", NO_RELAY
, 3, csa_dosetemail
, "Usage: setmail <userid> <timestamp> <email address>");
67 registercontrolhelpcmd("resendemail", NO_RELAY
, 1, csa_doresendemail
, "Usage: resendemail <userid>");
68 registercontrolhelpcmd("activateuser", NO_RELAY
, 1, csa_doactivateuser
, "Usage: activateuser <userid>");
69 registercontrolhelpcmd("addchan", NO_RELAY
, 3, csa_doaddchan
, "Usage: addchan <channel> <userid> <channel type>");
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);
77 registercontrolhelpcmd("createaccount", NO_RELAY
, 5, csa_docreateaccount
, "Usage: createaccount <execute> <username> <email address> <encrypted password> <activate user>");
78 createaccountsecret_ok
= 1;
85 deregistercontrolcmd("checkhashpass", csa_docheckhashpass
);
86 deregistercontrolcmd("settempemail", csa_dosettempemail
);
87 deregistercontrolcmd("setemail", csa_dosetemail
);
88 deregistercontrolcmd("resendemail", csa_doresendemail
);
89 deregistercontrolcmd("activateuser", csa_doactivateuser
);
90 deregistercontrolcmd("addchan", csa_doaddchan
);
92 if(createaccountsecret_ok
)
93 deregistercontrolcmd("createaccount", csa_docreateaccount
);
96 int csa_docheckhashpass(void *source
, int cargc
, char **cargv
) {
97 nick
*sender
=(nick
*)source
;
102 controlreply(sender
, "CHECKHASHPASS FAIL args");
106 if (!(rup
=findreguserbynick(cargv
[0]))) {
107 controlreply(sender
, "CHECKHASHPASS FAIL user");
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
);
114 } else if(UIsInactive(rup
)) {
115 controlreply(sender
, "CHECKHASHPASS FAIL inactive %s %s %u", rup
->username
, flags
, rup
->ID
);
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
);
119 controlreply(sender
, "CHECKHASHPASS OK %s %s %u %s", rup
->username
, flags
, rup
->ID
, rup
->email
?rup
->email
->content
:"-");
125 static char *email_to_error(char *email
) {
126 maildomain
*mdp
, *smdp
;
133 switch(csa_checkeboy_r(email
)) {
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";
144 /* maildomain BS... c&p from hello.c */
145 for(mlp
=maillocks
;mlp
;mlp
=mlp
->next
) {
146 if(!match(mlp
->pattern
->content
, email
)) {
147 return "emaillocked";
151 dupemail
= strdup(email
);
152 local
=strchr(dupemail
, '@');
155 return "emailunknown";
159 mdp
=findnearestmaildomain(local
);
161 for(smdp
=mdp
; smdp
; smdp
=smdp
->parent
) {
162 if(MDIsBanned(smdp
)) {
164 return "emaillocked";
166 if((smdp
->count
>= smdp
->limit
) && (smdp
->limit
> 0)) {
168 return "emaildomainlimit";
173 mdp
=findmaildomainbydomain(local
);
175 for (ruh
=mdp
->users
; ruh
; ruh
=ruh
->nextbydomain
) {
177 if (!strcasecmp(dupemail
, ruh
->localpart
->content
)) {
182 if((found
>= mdp
->actlimit
) && (mdp
->actlimit
> 0)) {
184 return "emailaddresslimit";
193 static void sendemail(reguser
*rup
) {
194 csdb_createmail(rup
, QMAIL_ACTIVATEEMAIL
);
197 int csa_docreateaccount(void *source
, int cargc
, char **cargv
) {
198 nick
*sender
=(nick
*)source
;
200 char *error_username
= NULL
, *error_password
= NULL
, *error_email
= NULL
;
201 char *username
= NULL
, *password
= NULL
, *email
= NULL
;
202 char account_info
[512];
208 controlreply(sender
, "CREATEACCOUNT FALSE args");
212 execute
= cargv
[0][0] == '1';
213 if(strcmp(cargv
[1], "0"))
215 if(strcmp(cargv
[2], "0"))
217 if(strcmp(cargv
[3], "0")) {
218 int errorcode
= decrypt_password(createaccountsecret
, KEY_BITS
, passbuf
, sizeof(passbuf
), cargv
[3]);
220 Error("chanserv_relay",ERR_WARNING
,"createaccount unable to decrypt password, error code: %d", errorcode
);
221 controlreply(sender
, "CREATEACCOUNT FALSE args");
226 activate
= cargv
[4][0] == '1';
229 if (findreguserbynick(username
)) {
230 error_username
= "usernameinuse";
231 } else if(csa_checkaccountname_r(username
)) {
232 error_username
= "usernameinvalid";
237 error_email
= email_to_error(email
);
240 int r
= csa_checkpasswordquality(password
);
241 if(r
== QM_PWTOSHORT
) {
242 error_password
= "passwordshort";
243 } else if(r
== QM_PWTOLONG
) {
244 error_password
= "passwordlong";
245 } else if(r
== QM_PWTOWEAK
) {
246 error_password
= "passwordweak";
248 error_password
= "passwordunknown";
252 if(execute
&& email
&& password
&& username
&& !error_email
&& !error_password
&& !error_username
) {
256 rup
= csa_createaccount(username
, password
, email
);
261 cs_log(sender
,"CREATEACCOUNT created auth %s (%s) %s",rup
->username
,rup
->email
->content
,activate
?"(active)": "(inactive)");
262 csdb_createuser(rup
);
263 snprintf(account_info
, sizeof(account_info
), " %u %lu", rup
->ID
, (unsigned long)rup
->lastpasschange
);
267 account_info
[0] = '\0';
271 controlreply(sender
, "CREATEACCOUNT %s%s%s%s%s%s%s%s",
272 do_create
? "TRUE" : "FALSE",
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
: ""
282 int csa_dosettempemail(void *source
, int cargc
, char **cargv
) {
286 nick
*sender
=(nick
*)source
;
289 controlreply(sender
, "SETTEMPEMAIL FALSE args");
293 rup
= findreguserbyID(atoi(cargv
[0]));
295 controlreply(sender
, "SETTEMPEMAIL FALSE useridnotexist");
299 if(!UIsInactive(rup
)) {
300 controlreply(sender
, "SETTEMPEMAIL FALSE accountactive");
305 error
= email_to_error(email
);
307 controlreply(sender
, "SETTEMPEMAIL FALSE %s", error
);
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
);
315 csdb_updateuser(rup
);
318 controlreply(sender
, "SETTEMPEMAIL TRUE");
323 int csa_dosetemail(void *source
, int cargc
, char **cargv
) {
327 nick
*sender
=(nick
*)source
;
330 controlreply(sender
, "SETEMAIL FALSE args");
334 rup
= findreguserbyID(atoi(cargv
[0]));
336 controlreply(sender
, "SETEMAIL FALSE useridnotexist");
340 if(UHasStaffPriv(rup
)) {
341 controlreply(sender
, "SETEMAIL FALSE privuser");
345 if(UHasSuspension(rup
)) {
346 controlreply(sender
, "SETEMAIL FALSE suspended");
350 if(rup
->lastpasschange
> atoi(cargv
[1])) {
351 controlreply(sender
, "SETEMAIL FALSE passwordchanged");
356 error
= email_to_error(email
);
358 controlreply(sender
, "SETEMAIL FALSE %s", error
);
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
);
366 csdb_updateuser(rup
);
369 controlreply(sender
, "SETEMAIL TRUE");
374 int csa_doresendemail(void *source
, int cargc
, char **cargv
) {
376 nick
*sender
=(nick
*)source
;
379 controlreply(sender
, "RESENDEMAIL FALSE args");
383 rup
= findreguserbyID(atoi(cargv
[0]));
385 controlreply(sender
, "RESENDEMAIL FALSE useridnotexist");
389 if(!UIsInactive(rup
)) {
390 controlreply(sender
, "RESENDEMAIL FALSE accountactive");
395 controlreply(sender
, "RESENDEMAIL TRUE");
396 cs_log(sender
,"RESENDEMAIL OK username %s",rup
->username
);
401 int csa_doactivateuser(void *source
, int cargc
, char **cargv
) {
403 nick
*sender
=(nick
*)source
;
406 controlreply(sender
, "ACTIVATEUSER FALSE args");
410 rup
= findreguserbyID(atoi(cargv
[0]));
412 controlreply(sender
, "ACTIVATEUSER FALSE useridnotexist");
416 if(!UIsInactive(rup
)) {
417 controlreply(sender
, "ACTIVATEUSER FALSE accountactive");
422 csdb_updateuser(rup
);
424 cs_log(sender
,"ACTIVATEUSER OK username %s",rup
->username
);
425 controlreply(sender
, "ACTIVATEUSER TRUE");
430 int csa_doaddchan(void *source
, int cargc
, char **cargv
) {
431 nick
*sender
=(nick
*)source
;
432 reguser
*rup
= getreguserfromnick(sender
), *founder
;
438 controlreply(sender
, "ADDCHAN FALSE args");
442 if (*cargv
[0] != '#' || strlen(cargv
[0]) > CHANNELLEN
) {
443 controlreply(sender
, "ADDCHAN FALSE invalidchannel");
447 if (!(cip
=findorcreatechanindex(cargv
[0]))) {
448 controlreply(sender
, "ADDCHAN FALSE invalidchannel");
452 founder
= findreguserbyID(atoi(cargv
[1]));
453 if(founder
== NULL
) {
454 controlreply(sender
, "ADDCHAN FALSE useridnotexist");
458 if(UIsInactive(founder
)) {
459 controlreply(sender
, "ADDCHAN FALSE accountinactive");
463 for(type
=CHANTYPES
-1;type
;type
--)
464 if(!ircd_strcmp(chantypes
[type
]->content
, cargv
[2]))
468 controlreply(sender
, "ADDCHAN FALSE invalidchantype");
472 rcp
= cs_addchan(cip
, sender
, rup
, founder
, QCFLAG_JOINED
, type
);
474 controlreply(sender
, "ADDCHAN FALSE alreadyregistered");
478 cs_log(sender
, "ADDCHAN %s #%s %s %s", cip
->name
->content
, founder
->username
, printflags(rcp
->flags
, rcflags
), chantypes
[type
]->content
);
479 controlreply(sender
, "ADDCHAN TRUE");
483 static int hex_to_int(char *input
, unsigned char *buf
, int buflen
) {
485 for(i
=0;i
<buflen
;i
++) {
486 if((0xff == hexlookup
[(int)input
[i
* 2]]) || (0xff == hexlookup
[(int)input
[i
* 2 + 1]])) {
489 buf
[i
] = (hexlookup
[(int)input
[i
* 2]] << 4) | hexlookup
[(int)input
[i
* 2 + 1]];
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
];
501 datalen
= strlen(encrypted
);
504 if(datalen
< BLOCK_SIZE
* 2 * 2)
507 if(!hex_to_int(encrypted
, iv
, BLOCK_SIZE
))
510 ciphertextlen
= (datalen
- (BLOCK_SIZE
* 2)) / 2;
511 if(ciphertextlen
> bufsize
|| ciphertextlen
% BLOCK_SIZE
!= 0)
514 if(!hex_to_int(encrypted
+ BLOCK_SIZE
* 2, (unsigned char *)buf
, ciphertextlen
))
517 c
= rijndaelcbc_init(secret
, keybits
, iv
, 1);
519 for(i
=0;i
<ciphertextlen
;i
+=BLOCK_SIZE
) {
521 unsigned char *r
= rijndaelcbc_decrypt(c
, (unsigned char *)p
);
522 memcpy(p
, r
, BLOCK_SIZE
);
524 for(j
=0;j
<BLOCK_SIZE
;j
++) {
525 if(*(p
+ j
) == '\0') {