]> jfr.im git - irc/quakenet/newserv.git/blob - localuser/localuser.c
Use nsmalloc/nsfree for proxyscan.
[irc/quakenet/newserv.git] / localuser / localuser.c
1 /* localuser.c */
2
3 #include "../nick/nick.h"
4 #include "../lib/base64.h"
5 #include "../lib/sstring.h"
6 #include "../irc/irc.h"
7 #include "../irc/irc_config.h"
8 #include "../core/hooks.h"
9 #include "../core/error.h"
10 #include "../lib/version.h"
11 #include "localuser.h"
12
13 #include <string.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <inttypes.h>
17
18 MODULE_VERSION("");
19
20 int currentlocalunum;
21 UserMessageHandler umhandlers[MAXLOCALUSER+1];
22
23 typedef struct pendingkill {
24 nick *source, *target;
25 sstring *reason;
26 struct pendingkill *next;
27 } pendingkill;
28
29 pendingkill *pendingkilllist;
30
31 void checklocalkill(int hooknum, void *nick);
32 void clearpendingkills(int hooknum, void *arg);
33 void checkpendingkills(int hooknum, void *arg);
34 void _killuser(nick *source, nick *target, char *reason);
35
36 void _init() {
37 int i;
38
39 for (i=0;i<=MAXLOCALUSER;i++) {
40 umhandlers[i]=NULL;
41 }
42 currentlocalunum=1;
43 pendingkilllist=NULL;
44 registerhook(HOOK_IRC_SENDBURSTNICKS,&sendnickburst);
45 registerhook(HOOK_NICK_KILL,&checklocalkill);
46 registerhook(HOOK_NICK_LOSTNICK,&checkpendingkills); /* CHECK ME -> should this hook KILL or LOSTNICK or BOTH */
47 registerhook(HOOK_CORE_ENDOFHOOKSQUEUE,&clearpendingkills);
48 registerserverhandler("P",&handleprivatemsgcmd,2);
49 registerserverhandler("O",&handleprivatenoticecmd, 2);
50 }
51
52 void _fini() {
53 pendingkill *pk;
54
55 for (pk=pendingkilllist;pk;pk=pendingkilllist) {
56 pendingkilllist = pk->next;
57 freesstring(pk->reason);
58 free(pk);
59 }
60
61 deregisterhook(HOOK_IRC_SENDBURSTNICKS,&sendnickburst);
62 deregisterhook(HOOK_NICK_KILL,&checklocalkill);
63 deregisterhook(HOOK_NICK_LOSTNICK,&checkpendingkills); /* CHECK ME -> should this hook KILL or LOSTNICK or BOTH */
64 deregisterhook(HOOK_CORE_ENDOFHOOKSQUEUE,&clearpendingkills);
65
66 deregisterserverhandler("P",&handleprivatemsgcmd);
67 deregisterserverhandler("O",&handleprivatenoticecmd);
68 }
69
70 /*
71 * registerlocaluserflags:
72 * This function creates a local user, and broadcasts it's existence to the net (if connected).
73 */
74
75 nick *registerlocaluserflags(char *nickname, char *ident, char *host, char *realname, char *authname, unsigned long authid, flag_t accountflags, flag_t umodes, UserMessageHandler handler) {
76 int i;
77 nick *newuser,*np;
78 struct irc_in_addr ipaddress;
79
80 i=0;
81 currentlocalunum=(currentlocalunum+1)%262142;
82 while (servernicks[numerictolong(mynumeric->content,2)][currentlocalunum&MAXLOCALUSER]!=NULL) {
83 /* Numeric 262143 on our server is used for "nouser" by the channels module, so cannot be allocated */
84 currentlocalunum=(currentlocalunum+1)%262142;
85 if (++i>MAXLOCALUSER) {
86 return NULL;
87 }
88 }
89
90 /* This code is very similar to stuff in nick.c... */
91 newuser=newnick();
92 newuser->numeric=(numerictolong(mynumeric->content,2)<<18)|(currentlocalunum);
93 strncpy(newuser->nick,nickname,NICKLEN);
94 newuser->nick[NICKLEN]='\0';
95 strncpy(newuser->ident,ident,USERLEN);
96 newuser->ident[USERLEN]='\0';
97 newuser->host=findorcreatehost(host);
98 newuser->realname=findorcreaterealname(realname);
99 newuser->nextbyhost=newuser->host->nicks;
100 newuser->host->nicks=newuser;
101 newuser->nextbyrealname=newuser->realname->nicks;
102 newuser->realname->nicks=newuser;
103 newuser->umodes=umodes;
104
105 memset(&ipaddress, 0, sizeof(ipaddress));
106 ((unsigned short *)(ipaddress.in6_16))[5] = 65535;
107 ((unsigned short *)(ipaddress.in6_16))[6] = 127;
108 ((unsigned char *)(ipaddress.in6_16))[14] = 1;
109 ((unsigned char *)(ipaddress.in6_16))[15] = (currentlocalunum%253)+1;
110
111 newuser->ipnode = refnode(iptree, &ipaddress, PATRICIA_MAXBITS);
112 node_increment_usercount(newuser->ipnode);
113
114 newuser->timestamp=getnettime();
115 newuser->shident=NULL;
116 newuser->sethost=NULL;
117 newuser->away=NULL;
118 newuser->marker=0;
119 memset(newuser->exts, 0, MAXNICKEXTS * sizeof(void *));
120
121 if (IsOper(newuser)) {
122 newuser->opername = getsstring("-", ACCOUNTLEN);
123 } else {
124 newuser->opername = NULL;
125 }
126
127 newuser->accountts=0;
128 newuser->auth=NULL;
129 newuser->authname=NULLAUTHNAME;
130 if (IsAccount(newuser)) {
131 newuser->accountts=newuser->timestamp;
132 if (authid) {
133 newuser->auth=findorcreateauthname(authid, authname);
134 newuser->authname=newuser->auth->name;
135 newuser->auth->usercount++;
136 newuser->nextbyauthname=newuser->auth->nicks;
137 newuser->auth->nicks=newuser;
138 newuser->auth->flags=accountflags;
139 } else {
140 /*
141 this is done for three reasons:
142 1: so I don't have to change 500 pieces of code
143 2: so services can be authed with special reserved ids
144 3: saves space over the old authname per user method
145 */
146 newuser->authname=malloc(strlen(authname) + 1);
147 strcpy(newuser->authname,authname);
148 }
149 }
150
151 newuser->cloak_count = 0;
152 newuser->cloak_extra = NULL;
153
154 if (connected) {
155 /* Check for nick collision */
156 if ((np=getnickbynick(nickname))!=NULL) {
157 /* Make sure we will win the collision */
158 newuser->timestamp=(np->timestamp-1);
159 killuser(NULL, np, "Nick collision");
160 }
161 sendnickmsg(newuser);
162 }
163
164 if (handler!=NULL) {
165 umhandlers[(currentlocalunum&MAXLOCALUSER)]=handler;
166 }
167
168 *(gethandlebynumeric(newuser->numeric))=newuser;
169 addnicktohash(newuser);
170 triggerhook(HOOK_NICK_NEWNICK,newuser);
171
172 return newuser;
173 }
174
175 /*
176 * renamelocaluser:
177 * This function changes the name of a given local user
178 */
179
180 int renamelocaluser(nick *np, char *newnick) {
181 nick *np2;
182 char ipbuf[25];
183 time_t timestamp=getnettime();
184 void *harg[2];
185 char oldnick[NICKLEN+1];
186
187 if (!np)
188 return -1;
189
190 if (strlen(newnick) > NICKLEN)
191 return -1;
192
193 if (homeserver(np->numeric)!=mylongnum)
194 return -1;
195
196 strncpy(oldnick,np->nick,NICKLEN);
197 oldnick[NICKLEN]='\0';
198 harg[0]=(void *)np;
199 harg[1]=(void *)oldnick;
200
201 if ((np2=getnickbynick(newnick))) {
202 if (np2==np) {
203 /* Case only name change */
204 strncpy(np->nick,newnick,NICKLEN);
205 np->nick[NICKLEN]='\0';
206 irc_send("%s N %s %jd",iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),np->nick,(intmax_t)np->timestamp);
207 triggerhook(HOOK_NICK_RENAME,harg);
208 return 0;
209 } else {
210 /* Kill other user and drop through */
211 timestamp=np2->timestamp-1;
212 killuser(NULL, np2, "Nick collision");
213 }
214 }
215
216 np->timestamp=timestamp;
217 removenickfromhash(np);
218 strncpy(np->nick,newnick,NICKLEN);
219 np->nick[NICKLEN]='\0';
220 addnicktohash(np);
221 irc_send("%s N %s %jd",longtonumeric(np->numeric,5),np->nick,(intmax_t)np->timestamp);
222 triggerhook(HOOK_NICK_RENAME,harg);
223
224 return 0;
225 }
226
227 /*
228 * deregisterlocaluser:
229 * This function removes the given local user from the network
230 */
231
232 int deregisterlocaluser(nick *np, char *reason) {
233 long numeric;
234 char reasonstr[512];
235 void *harg[2];
236
237 if (np==NULL || (homeserver(np->numeric)!=mylongnum)) {
238 /* Non-existent user, or user not on this server */
239 return -1;
240 }
241
242 if (reason==NULL || *reason=='\0') {
243 sprintf(reasonstr,"Quit");
244 } else {
245 snprintf(reasonstr,510,"Quit: %s",reason);
246 }
247
248 harg[0]=np;
249 harg[1]=reasonstr;
250
251 triggerhook(HOOK_NICK_QUIT, harg);
252
253 numeric=np->numeric;
254 umhandlers[np->numeric&MAXLOCALUSER]=NULL;
255 deletenick(np);
256 if (connected) {
257 irc_send("%s Q :%s",longtonumeric(numeric,5),reasonstr);
258 }
259
260 return 0;
261 }
262
263 /*
264 * hooklocaluserhandler:
265 * This function adds a new handler to the hook chain for a local user
266 * THIS RELIES ON MODULES BEING UNLOADED IN THE CORRECT ORDER.
267 */
268 UserMessageHandler hooklocaluserhandler(nick *np, UserMessageHandler newhandler) {
269 UserMessageHandler oldhandler = NULL;
270 if (np==NULL || (homeserver(np->numeric)!=mylongnum)) {
271 /* Non-existent user, or user not on this server */
272 return NULL;
273 }
274 oldhandler = umhandlers[np->numeric&MAXLOCALUSER];
275 umhandlers[np->numeric&MAXLOCALUSER]=newhandler;
276 return oldhandler;
277 }
278
279 /*
280 * sendnickmsg:
281 * Sends details of a given local nick to the network.
282 */
283
284 void sendnickmsg(nick *np) {
285 char numericbuf[6];
286 char ipbuf[25], operbuf[ACCOUNTLEN + 5];
287 char accountbuf[100];
288
289 strncpy(numericbuf,longtonumeric(np->numeric,5),5);
290 numericbuf[5]='\0';
291
292 if(IsOper(np) && (serverlist[myhub].flags & SMODE_OPERNAME)) {
293 snprintf(operbuf,sizeof(operbuf)," %s",np->opername?np->opername->content:"-");
294 } else {
295 operbuf[0] = '\0';
296 }
297
298 accountbuf[0]='\0';
299 if (IsAccount(np)) {
300 if (np->auth) {
301 if(np->auth->flags) {
302 snprintf(accountbuf,sizeof(accountbuf)," %s:%ld:%lu:%"PRIu64,np->authname,np->accountts,np->auth->userid,np->auth->flags);
303 } else {
304 snprintf(accountbuf,sizeof(accountbuf)," %s:%ld:%lu",np->authname,np->accountts,np->auth->userid);
305 }
306 } else if(np->authname) {
307 snprintf(accountbuf,sizeof(accountbuf)," %s:%ld:0",np->authname,np->accountts);
308 }
309 }
310
311 irc_send("%s N %s 1 %ld %s %s %s%s%s %s %s :%s",
312 mynumeric->content,np->nick,np->timestamp,np->ident,np->host->name->content,
313 printflags(np->umodes,umodeflags),operbuf,accountbuf,iptobase64(ipbuf,&(np->p_ipaddr),
314 sizeof(ipbuf),1),numericbuf,np->realname->name->content);
315 }
316
317 void sendnickburst(int hooknum, void *arg) {
318 /* Send nick messages for all local users */
319 nick **nh;
320 int i;
321
322 nh=servernicks[numerictolong(mynumeric->content,2)];
323 for (i=0;i<=MAXLOCALUSER;i++) {
324 if (nh[i]!=NULL) {
325 sendnickmsg(nh[i]);
326 }
327 }
328 }
329
330 /* Check for a kill of a local user */
331 void checklocalkill(int hooknum, void *arg) {
332 void **args=arg;
333 nick *target=args[0];
334 char *reason=args[1];
335 long numeric;
336
337 void *myargs[1];
338 myargs[0]=reason;
339
340
341 numeric=((nick *)target)->numeric;
342
343 if (homeserver(numeric)==mylongnum) {
344 if (umhandlers[(numeric)&MAXLOCALUSER]!=NULL) {
345 (umhandlers[(numeric)&MAXLOCALUSER])((nick *)target,LU_KILLED,myargs);
346 }
347 }
348 }
349
350 int handleprivatemsgcmd(void *source, int cargc, char **cargv) {
351 return handlemessageornotice(source, cargc, cargv, 0);
352 }
353
354 int handleprivatenoticecmd(void *source, int cargc, char **cargv) {
355 return handlemessageornotice(source, cargc, cargv, 1);
356 }
357
358 /* Handle privmsg or notice command addressed to user */
359 int handlemessageornotice(void *source, int cargc, char **cargv, int isnotice) {
360 nick *sender;
361 nick *target;
362 char *ch;
363 char targetnick[NICKLEN+1];
364 int foundat;
365 void *nargs[2];
366 int i;
367
368 /* Should have target and message */
369 if (cargc<2) {
370 return CMD_OK;
371 }
372
373 if (cargv[0][0]=='#' || cargv[0][0]=='+') {
374 /* Channel message/notice */
375 return CMD_OK;
376 }
377
378 if ((sender=getnickbynumericstr((char *)source))==NULL) {
379 Error("localuser",ERR_WARNING,"PRIVMSG from non existant user %s",(char *)source);
380 return CMD_OK;
381 }
382
383 /* Check for a "secure" message (foo@bar) */
384 foundat=0;
385 for (i=0,ch=cargv[0];(i<=NICKLEN) && (*ch);i++,ch++) {
386 if (*ch=='@') {
387 targetnick[i]='\0';
388 foundat=1;
389 break;
390 } else {
391 targetnick[i]=*ch;
392 }
393 }
394
395 if (!foundat) { /* Didn't find an @ sign, assume it's a numeric */
396 if ((target=getnickbynumericstr(cargv[0]))==NULL) {
397 Error("localuser",ERR_DEBUG,"Couldn't find target for %s",cargv[0]);
398 return CMD_OK;
399 }
400 } else { /* Did find @, do a lookup by nick */
401 if ((target=getnickbynick(targetnick))==NULL) {
402 Error("localuser",ERR_DEBUG,"Couldn't find target for %s",cargv[0]);
403 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]);
404 return CMD_OK;
405 }
406 }
407
408 if (homeserver(target->numeric)!=mylongnum) {
409 Error("localuser",ERR_WARNING,"Got message/notice for someone not on my server");
410 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]);
411 return CMD_OK;
412 }
413
414 if (foundat && !IsService(target)) {
415 Error("localuser",ERR_DEBUG,"Received secure message for %s, but user is not a service",cargv[0]);
416 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]);
417 return CMD_OK;
418 }
419
420 if (umhandlers[(target->numeric)&MAXLOCALUSER]==NULL) {
421 /* No handler anyhow.. */
422 return CMD_OK;
423 }
424
425 /* Dispatch the message. */
426 nargs[0]=(void *)sender;
427 nargs[1]=(void *)cargv[1];
428 (umhandlers[(target->numeric)&MAXLOCALUSER])(target,isnotice?LU_PRIVNOTICE:(foundat?LU_SECUREMSG:LU_PRIVMSG),nargs);
429
430 return CMD_OK;
431 }
432
433 /* Send message to user */
434 void sendmessagetouser(nick *source, nick *target, char *format, ... ) {
435 char buf[BUFSIZE];
436 char senderstr[6];
437 va_list va;
438
439 longtonumeric2(source->numeric,5,senderstr);
440
441 va_start(va,format);
442 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
443 /* So max sendable message is 495 bytes. Of course, a client won't be able
444 * to receive this.. */
445
446 vsnprintf(buf,BUFSIZE-17,format,va);
447 va_end(va);
448
449 if (homeserver(target->numeric)!=mylongnum)
450 irc_send("%s P %s :%s",senderstr,longtonumeric(target->numeric,5),buf);
451 }
452
453 /* Send messageto server, we don't check, but noones going to want to put a server pointer in anyway... */
454 void sendsecuremessagetouser(nick *source, nick *target, char *servername, char *format, ... ) {
455 char buf[BUFSIZE];
456 char senderstr[6];
457 va_list va;
458
459 longtonumeric2(source->numeric,5,senderstr);
460
461 va_start(va,format);
462 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
463 /* So max sendable message is 495 bytes. Of course, a client won't be able
464 * to receive this.. */
465
466 vsnprintf(buf,BUFSIZE-17,format,va);
467 va_end(va);
468
469 if (homeserver(target->numeric)!=mylongnum)
470 irc_send("%s P %s@%s :%s",senderstr,target->nick,servername,buf);
471 }
472
473 /* Send notice to user */
474 void sendnoticetouser(nick *source, nick *target, char *format, ... ) {
475 char buf[BUFSIZE];
476 char senderstr[6];
477 va_list va;
478
479 longtonumeric2(source->numeric,5,senderstr);
480
481 va_start(va,format);
482 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
483 /* So max sendable message is 495 bytes. Of course, a client won't be able
484 * to receive this.. */
485
486 vsnprintf(buf,BUFSIZE-17,format,va);
487 va_end(va);
488
489 if (homeserver(target->numeric)!=mylongnum)
490 irc_send("%s O %s :%s",senderstr,longtonumeric(target->numeric,5),buf);
491 }
492
493 /* Kill user */
494 void killuser(nick *source, nick *target, char *format, ... ) {
495 char buf[BUFSIZE];
496 va_list va;
497 pendingkill *pk;
498
499 va_start(va, format);
500 vsnprintf(buf, BUFSIZE-17, format, va);
501 va_end (va);
502
503 if (hookqueuelength) {
504 for (pk = pendingkilllist; pk; pk = pk->next)
505 if (pk->target == target)
506 return;
507
508 Error("localuser", ERR_DEBUG, "Adding pending kill for %s", target->nick);
509 pk = (pendingkill *)malloc(sizeof(pendingkill));
510 pk->source = source;
511 pk->target = target;
512 pk->reason = getsstring(buf, BUFSIZE);
513 pk->next = pendingkilllist;
514 pendingkilllist = pk;
515 } else {
516 _killuser(source, target, buf);
517 }
518 }
519
520 void sethostuser(nick *target, char *ident, char *host) {
521 irc_send("%s SH %s %s %s", mynumeric->content, longtonumeric(target->numeric, 5), ident, host);
522 }
523
524 void _killuser(nick *source, nick *target, char *reason) {
525 char senderstr[6];
526 char sourcestring[HOSTLEN+NICKLEN+3];
527 char reasonstr[512];
528 void *args[2];
529
530 if (!source) {
531 /* If we have a null nick, use the server.. */
532 strcpy(senderstr, mynumeric->content);
533 strcpy(sourcestring, myserver->content);
534 } else {
535 strcpy(senderstr, longtonumeric(source->numeric,5));
536 sprintf(sourcestring,"%s!%s",source->host->name->content, source->nick);
537 }
538
539 snprintf(reasonstr,512,"%s (%s)",sourcestring,reason);
540 reasonstr[511]='\0';
541
542 irc_send("%s D %s :%s",senderstr,longtonumeric(target->numeric,5),reasonstr);
543
544 args[0]=target;
545 args[1]=reasonstr;
546 triggerhook(HOOK_NICK_KILL, args);
547
548 deletenick(target);
549 }
550
551 void clearpendingkills(int hooknum, void *arg) {
552 pendingkill *pk;
553
554 pk = pendingkilllist;
555 while (pk) {
556 pendingkilllist = pk->next;
557
558 if (pk->target) {
559 Error("localuser", ERR_DEBUG, "Processing pending kill for %s", pk->target->nick);
560 _killuser(pk->source, pk->target, pk->reason->content);
561 }
562
563 freesstring(pk->reason);
564 free(pk);
565 pk = pendingkilllist;
566 }
567 }
568
569 void checkpendingkills(int hooknum, void *arg) {
570 nick *np = (nick *)arg;
571 pendingkill *pk;
572
573 for (pk=pendingkilllist; pk; pk = pk->next) {
574 if (pk->source == np) {
575 Error("localuser", ERR_INFO, "Pending kill source %s got deleted, NULL'ing source for pending kill", np->nick);
576 pk->source = NULL;
577 }
578 if (pk->target == np) {
579 Error("localuser", ERR_INFO, "Pending kill target %s got deleted, NULL'ing target for pending kill", np->nick);
580 pk->target = NULL;
581 }
582 }
583 }
584
585 void sendaccountmessage(nick *np) {
586 if (connected && IsAccount(np)) {
587 if (np->auth) {
588 if (np->auth->flags) {
589 irc_send("%s AC %s %s %ld %lu %"PRIu64,mynumeric->content, longtonumeric(np->numeric,5), np->authname, np->accountts, np->auth->userid, np->auth->flags);
590 } else {
591 irc_send("%s AC %s %s %ld %lu",mynumeric->content, longtonumeric(np->numeric,5), np->authname, np->accountts, np->auth->userid);
592 }
593 } else {
594 irc_send("%s AC %s %s %ld 0",mynumeric->content, longtonumeric(np->numeric,5), np->authname, np->accountts);
595 }
596 }
597 }
598
599 /* Auth user, don't use to set flags after authing */
600 void localusersetaccount(nick *np, char *accname, unsigned long accid, u_int64_t accountflags, time_t authTS) {
601 if (IsAccount(np)) {
602 Error("localuser",ERR_WARNING,"Tried to set account on user %s already authed", np->nick);
603 return;
604 }
605
606 SetAccount(np);
607 np->accountts=authTS?authTS:getnettime();
608
609 if (accid) {
610 np->auth=findorcreateauthname(accid, accname);
611 np->auth->usercount++;
612 np->authname=np->auth->name;
613 np->nextbyauthname=np->auth->nicks;
614 np->auth->nicks=np;
615 np->auth->flags=accountflags;
616 } else {
617 np->auth=NULL;
618 np->authname=malloc(strlen(accname) + 1);
619 strcpy(np->authname,accname);
620 }
621
622 sendaccountmessage(np);
623
624 triggerhook(HOOK_NICK_ACCOUNT, np);
625 }
626
627 void localusersetumodes(nick *np, flag_t newmodes) {
628 if (connected) {
629 irc_send("%s M %s %s", longtonumeric(np->numeric,5), np->nick, printflagdiff(np->umodes, newmodes, umodeflags));
630 }
631
632 np->umodes = newmodes;
633 }
634
635 void localusersetaccountflags(authname *anp, u_int64_t accountflags) {
636 void *arg[2];
637 nick *np;
638 u_int64_t oldflags = anp->flags;
639
640 arg[0] = anp;
641 arg[1] = &oldflags;
642 anp->flags = accountflags;
643
644 for(np=anp->nicks;np;np=np->nextbyauthname)
645 sendaccountmessage(np);
646
647 triggerhook(HOOK_AUTH_FLAGSUPDATED, arg);
648 }
649
650 void localuseraddcloaktarget(nick *np, nick *target) {
651 char snumeric[10], tnumeric[10];
652
653 strcpy(snumeric, longtonumeric(np->numeric,5));
654 strcpy(tnumeric, longtonumeric(target->numeric,5));
655
656 irc_send("%s CA %s", snumeric, tnumeric);
657
658 addcloaktarget(np, target);
659 }
660
661 void localuserclearcloaktargets(nick *np) {
662 irc_send("%s CU", longtonumeric(np->numeric,5));
663 clearcloaktargets(np);
664 }
665