]>
jfr.im git - irc/quakenet/newserv.git/blob - chanserv/chanserv_relay.c
e3c5bc792b3271d2c58b513f218b7a315b84924a
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
);
268 account_info
[0] = '\0';
272 controlreply(sender
, "CREATEACCOUNT %s%s%s%s%s%s%s%s",
273 do_create
? "TRUE" : "FALSE",
275 email
&& error_email
? " " : "", email
&& error_email
? error_email
: "",
276 password
&& error_password
? " " : "", password
&& error_password
? error_password
: "",
277 username
&& error_username
? " " : "", username
&& error_username
? error_username
: ""
283 int csa_dosettempemail(void *source
, int cargc
, char **cargv
) {
287 nick
*sender
=(nick
*)source
;
290 controlreply(sender
, "SETTEMPEMAIL FALSE args");
294 rup
= findreguserbyID(atoi(cargv
[0]));
296 controlreply(sender
, "SETTEMPEMAIL FALSE useridnotexist");
300 if(!UIsInactive(rup
)) {
301 controlreply(sender
, "SETTEMPEMAIL FALSE accountactive");
306 error
= email_to_error(email
);
308 controlreply(sender
, "SETTEMPEMAIL FALSE %s", error
);
312 freesstring(rup
->email
);
313 rup
->email
=getsstring(email
,EMAILLEN
);
314 cs_log(sender
,"SETTEMPEMAIL OK username %s email %s",rup
->username
, rup
->email
->content
);
316 csdb_updateuser(rup
);
319 controlreply(sender
, "SETTEMPEMAIL TRUE");
324 int csa_dosetemail(void *source
, int cargc
, char **cargv
) {
328 nick
*sender
=(nick
*)source
;
331 controlreply(sender
, "SETEMAIL FALSE args");
335 rup
= findreguserbyID(atoi(cargv
[0]));
337 controlreply(sender
, "SETEMAIL FALSE useridnotexist");
341 if(UHasStaffPriv(rup
)) {
342 controlreply(sender
, "SETEMAIL FALSE privuser");
346 if(UHasSuspension(rup
)) {
347 controlreply(sender
, "SETEMAIL FALSE suspended");
351 if(rup
->lastpasschange
> atoi(cargv
[1])) {
352 controlreply(sender
, "SETEMAIL FALSE passwordchanged");
358 if(!strcmp(email
, rup
->email
->content
)) {
359 /* setting to the same thing? fine! */
360 controlreply(sender
, "SETEMAIL TRUE");
364 error
= email_to_error(email
);
366 controlreply(sender
, "SETEMAIL FALSE %s", error
);
370 freesstring(rup
->email
);
371 rup
->email
=getsstring(email
,EMAILLEN
);
372 cs_log(sender
,"SETEMAIL OK username %s email %s",rup
->username
, rup
->email
->content
);
374 csdb_updateuser(rup
);
377 controlreply(sender
, "SETEMAIL TRUE");
382 int csa_doresendemail(void *source
, int cargc
, char **cargv
) {
384 nick
*sender
=(nick
*)source
;
387 controlreply(sender
, "RESENDEMAIL FALSE args");
391 rup
= findreguserbyID(atoi(cargv
[0]));
393 controlreply(sender
, "RESENDEMAIL FALSE useridnotexist");
397 if(!UIsInactive(rup
)) {
398 controlreply(sender
, "RESENDEMAIL FALSE accountactive");
403 controlreply(sender
, "RESENDEMAIL TRUE");
404 cs_log(sender
,"RESENDEMAIL OK username %s",rup
->username
);
409 int csa_doactivateuser(void *source
, int cargc
, char **cargv
) {
411 nick
*sender
=(nick
*)source
;
414 controlreply(sender
, "ACTIVATEUSER FALSE args");
418 rup
= findreguserbyID(atoi(cargv
[0]));
420 controlreply(sender
, "ACTIVATEUSER FALSE useridnotexist");
424 if(!UIsInactive(rup
)) {
425 controlreply(sender
, "ACTIVATEUSER FALSE accountactive");
430 csdb_updateuser(rup
);
432 cs_log(sender
,"ACTIVATEUSER OK username %s",rup
->username
);
433 controlreply(sender
, "ACTIVATEUSER TRUE");
438 int csa_doaddchan(void *source
, int cargc
, char **cargv
) {
439 nick
*sender
=(nick
*)source
;
440 reguser
*rup
= getreguserfromnick(sender
), *founder
;
446 controlreply(sender
, "ADDCHAN FALSE args");
450 if (*cargv
[0] != '#' || strlen(cargv
[0]) > CHANNELLEN
) {
451 controlreply(sender
, "ADDCHAN FALSE invalidchannel");
455 if (!(cip
=findorcreatechanindex(cargv
[0]))) {
456 controlreply(sender
, "ADDCHAN FALSE invalidchannel");
460 founder
= findreguserbyID(atoi(cargv
[1]));
461 if(founder
== NULL
) {
462 controlreply(sender
, "ADDCHAN FALSE useridnotexist");
466 if(UIsInactive(founder
)) {
467 controlreply(sender
, "ADDCHAN FALSE accountinactive");
471 for(type
=CHANTYPES
-1;type
;type
--)
472 if(!ircd_strcmp(chantypes
[type
]->content
, cargv
[2]))
476 controlreply(sender
, "ADDCHAN FALSE invalidchantype");
480 rcp
= cs_addchan(cip
, sender
, rup
, founder
, QCFLAG_JOINED
| QCFLAG_AUTOOP
| QCFLAG_BITCH
| QCFLAG_FORCETOPIC
| QCFLAG_PROTECT
| QCFLAG_TOPICSAVE
, CHANMODE_NOCTCP
| CHANMODE_DELJOINS
| CHANMODE_MODNOAUTH
| CHANMODE_NONOTICE
| CHANMODE_NOEXTMSG
| CHANMODE_SINGLETARG
| CHANMODE_TOPICLIMIT
| CHANMODE_NOQUITMSG
, 0, type
);
482 controlreply(sender
, "ADDCHAN FALSE alreadyregistered");
486 cs_log(sender
, "ADDCHAN %s #%s %s %s", cip
->name
->content
, founder
->username
, printflags(rcp
->flags
, rcflags
), chantypes
[type
]->content
);
487 controlreply(sender
, "ADDCHAN TRUE %u", rcp
->ID
);
491 static int hex_to_int(char *input
, unsigned char *buf
, int buflen
) {
493 for(i
=0;i
<buflen
;i
++) {
494 if((0xff == hexlookup
[(int)input
[i
* 2]]) || (0xff == hexlookup
[(int)input
[i
* 2 + 1]])) {
497 buf
[i
] = (hexlookup
[(int)input
[i
* 2]] << 4) | hexlookup
[(int)input
[i
* 2 + 1]];
503 static int decrypt_password(unsigned char *secret
, int keybits
, char *buf
, int bufsize
, char *encrypted
) {
504 size_t ciphertextlen
, datalen
;
505 unsigned char iv
[BLOCK_SIZE
];
509 datalen
= strlen(encrypted
);
512 if(datalen
< BLOCK_SIZE
* 2 * 2)
515 if(!hex_to_int(encrypted
, iv
, BLOCK_SIZE
))
518 ciphertextlen
= (datalen
- (BLOCK_SIZE
* 2)) / 2;
519 if(ciphertextlen
> bufsize
|| ciphertextlen
% BLOCK_SIZE
!= 0)
522 if(!hex_to_int(encrypted
+ BLOCK_SIZE
* 2, (unsigned char *)buf
, ciphertextlen
))
525 c
= rijndaelcbc_init(secret
, keybits
, iv
, 1);
527 for(i
=0;i
<ciphertextlen
;i
+=BLOCK_SIZE
) {
529 unsigned char *r
= rijndaelcbc_decrypt(c
, (unsigned char *)p
);
530 memcpy(p
, r
, BLOCK_SIZE
);
532 for(j
=0;j
<BLOCK_SIZE
;j
++) {
533 if(*(p
+ j
) == '\0') {