]> jfr.im git - irc/rqf/shadowircd.git/blob - src/client.c
More TS6 special cases simplified.
[irc/rqf/shadowircd.git] / src / client.c
1 /*
2 * charybdis: an advanced ircd.
3 * client.c: Controls clients.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 * Copyright (C) 2007 William Pitcock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * $Id: client.c 3514 2007-06-06 16:25:21Z nenolod $
26 */
27 #include "stdinc.h"
28 #include "config.h"
29
30 #include "client.h"
31 #include "class.h"
32 #include "common.h"
33 #include "hash.h"
34 #include "match.h"
35 #include "ircd.h"
36 #include "numeric.h"
37 #include "packet.h"
38 #include "s_auth.h"
39 #include "s_conf.h"
40 #include "s_newconf.h"
41 #include "logger.h"
42 #include "s_serv.h"
43 #include "s_stats.h"
44 #include "send.h"
45 #include "whowas.h"
46 #include "s_user.h"
47 #include "hash.h"
48 #include "hostmask.h"
49 #include "listener.h"
50 #include "hook.h"
51 #include "msg.h"
52 #include "monitor.h"
53 #include "blacklist.h"
54 #include "reject.h"
55 #include "scache.h"
56 #include "irc_dictionary.h"
57 #include "sslproc.h"
58
59 #define DEBUG_EXITED_CLIENTS
60
61 static void check_pings_list(rb_dlink_list * list);
62 static void check_unknowns_list(rb_dlink_list * list);
63 static void free_exited_clients(void *unused);
64 static void exit_aborted_clients(void *unused);
65
66 static int exit_remote_client(struct Client *, struct Client *, struct Client *,const char *);
67 static int exit_remote_server(struct Client *, struct Client *, struct Client *,const char *);
68 static int exit_local_client(struct Client *, struct Client *, struct Client *,const char *);
69 static int exit_unknown_client(struct Client *, struct Client *, struct Client *,const char *);
70 static int exit_local_server(struct Client *, struct Client *, struct Client *,const char *);
71 static int qs_server(struct Client *, struct Client *, struct Client *, const char *comment);
72
73 static EVH check_pings;
74
75 extern rb_bh *client_heap;
76 extern rb_bh *lclient_heap;
77 extern rb_bh *pclient_heap;
78 static rb_bh *away_heap = NULL;
79
80 extern char current_uid[IDLEN];
81
82 struct Dictionary *nd_dict = NULL;
83
84 enum
85 {
86 D_LINED,
87 K_LINED
88 };
89
90 rb_dlink_list dead_list;
91 #ifdef DEBUG_EXITED_CLIENTS
92 static rb_dlink_list dead_remote_list;
93 #endif
94
95 struct abort_client
96 {
97 rb_dlink_node node;
98 struct Client *client;
99 char notice[REASONLEN];
100 };
101
102 static rb_dlink_list abort_list;
103
104
105 /*
106 * init_client
107 *
108 * inputs - NONE
109 * output - NONE
110 * side effects - initialize client free memory
111 */
112 void
113 init_client(void)
114 {
115 /*
116 * start off the check ping event .. -- adrian
117 * Every 30 seconds is plenty -- db
118 */
119 client_heap = rb_bh_create(sizeof(struct Client), CLIENT_HEAP_SIZE, "client_heap");
120 lclient_heap = rb_bh_create(sizeof(struct LocalUser), LCLIENT_HEAP_SIZE, "lclient_heap");
121 pclient_heap = rb_bh_create(sizeof(struct PreClient), PCLIENT_HEAP_SIZE, "pclient_heap");
122 away_heap = rb_bh_create(AWAYLEN, AWAY_HEAP_SIZE, "away_heap");
123
124 rb_event_addish("check_pings", check_pings, NULL, 30);
125 rb_event_addish("free_exited_clients", &free_exited_clients, NULL, 4);
126 rb_event_addish("exit_aborted_clients", exit_aborted_clients, NULL, 1);
127 rb_event_add("flood_recalc", flood_recalc, NULL, 1);
128
129 nd_dict = irc_dictionary_create(irccmp);
130 }
131
132
133 /*
134 * make_client - create a new Client struct and set it to initial state.
135 *
136 * from == NULL, create local client (a client connected
137 * to a socket).
138 *
139 * from, create remote client (behind a socket
140 * associated with the client defined by
141 * 'from'). ('from' is a local client!!).
142 */
143 struct Client *
144 make_client(struct Client *from)
145 {
146 struct Client *client_p = NULL;
147 struct LocalUser *localClient;
148
149 client_p = rb_bh_alloc(client_heap);
150
151 if(from == NULL)
152 {
153 client_p->from = client_p; /* 'from' of local client is self! */
154
155 localClient = (struct LocalUser *) rb_bh_alloc(lclient_heap);
156 SetMyConnect(client_p);
157 client_p->localClient = localClient;
158
159 client_p->localClient->lasttime = client_p->localClient->firsttime = rb_current_time();
160
161 client_p->localClient->F = NULL;
162
163 client_p->preClient = (struct PreClient *) rb_bh_alloc(pclient_heap);
164
165 /* as good a place as any... */
166 rb_dlinkAdd(client_p, &client_p->localClient->tnode, &unknown_list);
167 }
168 else
169 { /* from is not NULL */
170 client_p->localClient = NULL;
171 client_p->preClient = NULL;
172 client_p->from = from; /* 'from' of local client is self! */
173 }
174
175 SetUnknown(client_p);
176 strcpy(client_p->username, "unknown");
177
178 return client_p;
179 }
180
181 void
182 free_pre_client(struct Client *client_p)
183 {
184 struct Blacklist *blptr;
185
186 s_assert(NULL != client_p);
187
188 if(client_p->preClient == NULL)
189 return;
190
191 blptr = client_p->preClient->dnsbl_listed;
192 if (blptr != NULL)
193 unref_blacklist(blptr);
194 abort_blacklist_queries(client_p);
195 rb_bh_free(pclient_heap, client_p->preClient);
196 client_p->preClient = NULL;
197 }
198
199 static void
200 free_local_client(struct Client *client_p)
201 {
202 s_assert(NULL != client_p);
203 s_assert(&me != client_p);
204
205 if(client_p->localClient == NULL)
206 return;
207
208 /*
209 * clean up extra sockets from P-lines which have been discarded.
210 */
211 if(client_p->localClient->listener)
212 {
213 s_assert(0 < client_p->localClient->listener->ref_count);
214 if(0 == --client_p->localClient->listener->ref_count
215 && !client_p->localClient->listener->active)
216 free_listener(client_p->localClient->listener);
217 client_p->localClient->listener = 0;
218 }
219
220 if(client_p->localClient->F != NULL)
221 {
222 del_from_cli_fd_hash(client_p);
223 rb_close(client_p->localClient->F);
224 }
225
226 if(client_p->localClient->passwd)
227 {
228 memset(client_p->localClient->passwd, 0,
229 strlen(client_p->localClient->passwd));
230 rb_free(client_p->localClient->passwd);
231 }
232
233 rb_free(client_p->localClient->challenge);
234 rb_free(client_p->localClient->fullcaps);
235 rb_free(client_p->localClient->opername);
236 rb_free(client_p->localClient->mangledhost);
237
238 ssld_decrement_clicount(client_p->localClient->ssl_ctl);
239
240 rb_bh_free(lclient_heap, client_p->localClient);
241 client_p->localClient = NULL;
242 }
243
244 void
245 free_client(struct Client *client_p)
246 {
247 s_assert(NULL != client_p);
248 s_assert(&me != client_p);
249 free_local_client(client_p);
250 free_pre_client(client_p);
251 rb_bh_free(client_heap, client_p);
252 }
253
254 /*
255 * check_pings - go through the local client list and check activity
256 * kill off stuff that should die
257 *
258 * inputs - NOT USED (from event)
259 * output - next time_t when check_pings() should be called again
260 * side effects -
261 *
262 *
263 * A PING can be sent to clients as necessary.
264 *
265 * Client/Server ping outs are handled.
266 */
267
268 /*
269 * Addon from adrian. We used to call this after nextping seconds,
270 * however I've changed it to run once a second. This is only for
271 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
272 * run once a second makes life a lot easier - when a new client connects
273 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
274 * we end up waiting 20 seconds. This is stupid. :-)
275 * I will optimise (hah!) check_pings() once I've finished working on
276 * tidying up other network IO evilnesses.
277 * -- adrian
278 */
279
280 static void
281 check_pings(void *notused)
282 {
283 check_pings_list(&lclient_list);
284 check_pings_list(&serv_list);
285 check_unknowns_list(&unknown_list);
286 }
287
288 /*
289 * Check_pings_list()
290 *
291 * inputs - pointer to list to check
292 * output - NONE
293 * side effects -
294 */
295 static void
296 check_pings_list(rb_dlink_list * list)
297 {
298 char scratch[32]; /* way too generous but... */
299 struct Client *client_p; /* current local client_p being examined */
300 int ping = 0; /* ping time value from client */
301 rb_dlink_node *ptr, *next_ptr;
302
303 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
304 {
305 client_p = ptr->data;
306
307 if(!MyConnect(client_p) || IsDead(client_p))
308 continue;
309
310 ping = get_client_ping(client_p);
311
312 if(ping < (rb_current_time() - client_p->localClient->lasttime))
313 {
314 /*
315 * If the client/server hasnt talked to us in 2*ping seconds
316 * and it has a ping time, then close its connection.
317 */
318 if(((rb_current_time() - client_p->localClient->lasttime) >= (2 * ping)
319 && (client_p->flags & FLAGS_PINGSENT)))
320 {
321 if(IsServer(client_p))
322 {
323 sendto_realops_snomask(SNO_GENERAL, L_ALL,
324 "No response from %s, closing link",
325 get_server_name(client_p, HIDE_IP));
326 ilog(L_SERVER,
327 "No response from %s, closing link",
328 log_client_name(client_p, HIDE_IP));
329 }
330 (void) rb_snprintf(scratch, sizeof(scratch),
331 "Ping timeout: %d seconds",
332 (int) (rb_current_time() - client_p->localClient->lasttime));
333
334 exit_client(client_p, client_p, &me, scratch);
335 continue;
336 }
337 else if((client_p->flags & FLAGS_PINGSENT) == 0)
338 {
339 /*
340 * if we havent PINGed the connection and we havent
341 * heard from it in a while, PING it to make sure
342 * it is still alive.
343 */
344 client_p->flags |= FLAGS_PINGSENT;
345 /* not nice but does the job */
346 client_p->localClient->lasttime = rb_current_time() - ping;
347 sendto_one(client_p, "PING :%s", me.name);
348 }
349 }
350 /* ping_timeout: */
351
352 }
353 }
354
355 /*
356 * check_unknowns_list
357 *
358 * inputs - pointer to list of unknown clients
359 * output - NONE
360 * side effects - unknown clients get marked for termination after n seconds
361 */
362 static void
363 check_unknowns_list(rb_dlink_list * list)
364 {
365 rb_dlink_node *ptr, *next_ptr;
366 struct Client *client_p;
367 int timeout;
368
369 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
370 {
371 client_p = ptr->data;
372
373 if(IsDead(client_p) || IsClosing(client_p))
374 continue;
375
376 /*
377 * Check UNKNOWN connections - if they have been in this state
378 * for > 30s, close them.
379 */
380
381 timeout = IsAnyServer(client_p) ? ConfigFileEntry.connect_timeout : 30;
382 if((rb_current_time() - client_p->localClient->firsttime) > timeout)
383 {
384 if(IsAnyServer(client_p))
385 {
386 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
387 "No response from %s, closing link",
388 get_server_name(client_p, HIDE_IP));
389 ilog(L_SERVER,
390 "No response from %s, closing link",
391 log_client_name(client_p, HIDE_IP));
392 }
393 exit_client(client_p, client_p, &me, "Connection timed out");
394 }
395 }
396 }
397
398 static void
399 notify_banned_client(struct Client *client_p, struct ConfItem *aconf, int ban)
400 {
401 static const char conn_closed[] = "Connection closed";
402 static const char d_lined[] = "D-lined";
403 static const char k_lined[] = "K-lined";
404 const char *reason = NULL;
405 const char *exit_reason = conn_closed;
406
407 if(ConfigFileEntry.kline_with_reason && !EmptyString(aconf->passwd))
408 {
409 reason = aconf->passwd;
410 exit_reason = aconf->passwd;
411 }
412 else
413 {
414 switch (aconf->status)
415 {
416 case D_LINED:
417 reason = d_lined;
418 break;
419 default:
420 reason = k_lined;
421 break;
422 }
423 }
424
425 if(ban == D_LINED && !IsPerson(client_p))
426 sendto_one(client_p, "NOTICE DLINE :*** You have been D-lined");
427 else
428 sendto_one(client_p, form_str(ERR_YOUREBANNEDCREEP),
429 me.name, client_p->name, reason);
430
431 exit_client(client_p, client_p, &me,
432 EmptyString(ConfigFileEntry.kline_reason) ? exit_reason :
433 ConfigFileEntry.kline_reason);
434 }
435
436 /*
437 * check_banned_lines
438 * inputs - NONE
439 * output - NONE
440 * side effects - Check all connections for a pending k/dline against the
441 * client, exit the client if found.
442 */
443 void
444 check_banned_lines(void)
445 {
446 struct Client *client_p; /* current local client_p being examined */
447 struct ConfItem *aconf = NULL;
448 rb_dlink_node *ptr, *next_ptr;
449
450 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
451 {
452 client_p = ptr->data;
453
454 if(IsMe(client_p))
455 continue;
456
457 /* if there is a returned struct ConfItem then kill it */
458 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, client_p->localClient->ip.ss_family)))
459 {
460 if(aconf->status & CONF_EXEMPTDLINE)
461 continue;
462
463 sendto_realops_snomask(SNO_GENERAL, L_ALL,
464 "DLINE active for %s",
465 get_client_name(client_p, HIDE_IP));
466
467 notify_banned_client(client_p, aconf, D_LINED);
468 continue; /* and go examine next fd/client_p */
469 }
470
471 if(!IsPerson(client_p))
472 continue;
473
474 if((aconf = find_kline(client_p)) != NULL)
475 {
476 if(IsExemptKline(client_p))
477 {
478 sendto_realops_snomask(SNO_GENERAL, L_ALL,
479 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
480 get_client_name(client_p, HIDE_IP),
481 aconf->user, aconf->host);
482 continue;
483 }
484
485 sendto_realops_snomask(SNO_GENERAL, L_ALL,
486 "KLINE active for %s",
487 get_client_name(client_p, HIDE_IP));
488 notify_banned_client(client_p, aconf, K_LINED);
489 continue;
490 }
491 else if((aconf = find_xline(client_p->info, 1)) != NULL)
492 {
493 if(IsExemptKline(client_p))
494 {
495 sendto_realops_snomask(SNO_GENERAL, L_ALL,
496 "XLINE over-ruled for %s, client is kline_exempt [%s]",
497 get_client_name(client_p, HIDE_IP),
498 aconf->name);
499 continue;
500 }
501
502 sendto_realops_snomask(SNO_GENERAL, L_ALL, "XLINE active for %s",
503 get_client_name(client_p, HIDE_IP));
504
505 (void) exit_client(client_p, client_p, &me, "Bad user info");
506 continue;
507 }
508 }
509
510 /* also check the unknowns list for new dlines */
511 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
512 {
513 client_p = ptr->data;
514
515 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)))
516 {
517 if(aconf->status & CONF_EXEMPTDLINE)
518 continue;
519
520 notify_banned_client(client_p, aconf, D_LINED);
521 }
522 }
523
524 }
525
526 /* check_klines_event()
527 *
528 * inputs -
529 * outputs -
530 * side effects - check_klines() is called, kline_queued unset
531 */
532 void
533 check_klines_event(void *unused)
534 {
535 kline_queued = 0;
536 check_klines();
537 }
538
539 /* check_klines
540 *
541 * inputs -
542 * outputs -
543 * side effects - all clients will be checked for klines
544 */
545 void
546 check_klines(void)
547 {
548 struct Client *client_p;
549 struct ConfItem *aconf;
550 rb_dlink_node *ptr;
551 rb_dlink_node *next_ptr;
552
553 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
554 {
555 client_p = ptr->data;
556
557 if(IsMe(client_p) || !IsPerson(client_p))
558 continue;
559
560 if((aconf = find_kline(client_p)) != NULL)
561 {
562 if(IsExemptKline(client_p))
563 {
564 sendto_realops_snomask(SNO_GENERAL, L_ALL,
565 "KLINE over-ruled for %s, client is kline_exempt",
566 get_client_name(client_p, HIDE_IP));
567 continue;
568 }
569
570 sendto_realops_snomask(SNO_GENERAL, L_ALL,
571 "KLINE active for %s",
572 get_client_name(client_p, HIDE_IP));
573
574 notify_banned_client(client_p, aconf, K_LINED);
575 continue;
576 }
577 }
578 }
579
580 /* check_dlines()
581 *
582 * inputs -
583 * outputs -
584 * side effects - all clients will be checked for dlines
585 */
586 void
587 check_dlines(void)
588 {
589 struct Client *client_p;
590 struct ConfItem *aconf;
591 rb_dlink_node *ptr;
592 rb_dlink_node *next_ptr;
593
594 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
595 {
596 client_p = ptr->data;
597
598 if(IsMe(client_p))
599 continue;
600
601 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)) != NULL)
602 {
603 if(aconf->status & CONF_EXEMPTDLINE)
604 continue;
605
606 sendto_realops_snomask(SNO_GENERAL, L_ALL,
607 "DLINE active for %s",
608 get_client_name(client_p, HIDE_IP));
609
610 notify_banned_client(client_p, aconf, D_LINED);
611 continue;
612 }
613 }
614
615 /* dlines need to be checked against unknowns too */
616 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
617 {
618 client_p = ptr->data;
619
620 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)) != NULL)
621 {
622 if(aconf->status & CONF_EXEMPTDLINE)
623 continue;
624
625 notify_banned_client(client_p, aconf, D_LINED);
626 }
627 }
628 }
629
630 /* check_xlines
631 *
632 * inputs -
633 * outputs -
634 * side effects - all clients will be checked for xlines
635 */
636 void
637 check_xlines(void)
638 {
639 struct Client *client_p;
640 struct ConfItem *aconf;
641 rb_dlink_node *ptr;
642 rb_dlink_node *next_ptr;
643
644 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
645 {
646 client_p = ptr->data;
647
648 if(IsMe(client_p) || !IsPerson(client_p))
649 continue;
650
651 if((aconf = find_xline(client_p->info, 1)) != NULL)
652 {
653 if(IsExemptKline(client_p))
654 {
655 sendto_realops_snomask(SNO_GENERAL, L_ALL,
656 "XLINE over-ruled for %s, client is kline_exempt",
657 get_client_name(client_p, HIDE_IP));
658 continue;
659 }
660
661 sendto_realops_snomask(SNO_GENERAL, L_ALL, "XLINE active for %s",
662 get_client_name(client_p, HIDE_IP));
663
664 (void) exit_client(client_p, client_p, &me, "Bad user info");
665 continue;
666 }
667 }
668 }
669
670 /*
671 * update_client_exit_stats
672 *
673 * input - pointer to client
674 * output - NONE
675 * side effects -
676 */
677 static void
678 update_client_exit_stats(struct Client *client_p)
679 {
680 if(IsServer(client_p))
681 {
682 sendto_realops_snomask(SNO_EXTERNAL, L_ALL,
683 "Server %s split from %s",
684 client_p->name, client_p->servptr->name);
685 if(HasSentEob(client_p))
686 eob_count--;
687 }
688 else if(IsClient(client_p))
689 {
690 --Count.total;
691 if(IsOper(client_p))
692 --Count.oper;
693 if(IsInvisible(client_p))
694 --Count.invisi;
695 }
696
697 if(splitchecking && !splitmode)
698 check_splitmode(NULL);
699 }
700
701 /*
702 * release_client_state
703 *
704 * input - pointer to client to release
705 * output - NONE
706 * side effects -
707 */
708 static void
709 release_client_state(struct Client *client_p)
710 {
711 if(client_p->user != NULL)
712 {
713 free_user(client_p->user, client_p); /* try this here */
714 }
715 if(client_p->serv)
716 {
717 if(client_p->serv->user != NULL)
718 free_user(client_p->serv->user, client_p);
719 if(client_p->serv->fullcaps)
720 rb_free(client_p->serv->fullcaps);
721 rb_free(client_p->serv);
722 }
723 }
724
725 /*
726 * remove_client_from_list
727 * inputs - point to client to remove
728 * output - NONE
729 * side effects - taken the code from ExitOneClient() for this
730 * and placed it here. - avalon
731 */
732 static void
733 remove_client_from_list(struct Client *client_p)
734 {
735 s_assert(NULL != client_p);
736
737 if(client_p == NULL)
738 return;
739
740 /* A client made with make_client()
741 * is on the unknown_list until removed.
742 * If it =does= happen to exit before its removed from that list
743 * and its =not= on the global_client_list, it will core here.
744 * short circuit that case now -db
745 */
746 if(client_p->node.prev == NULL && client_p->node.next == NULL)
747 return;
748
749 rb_dlinkDelete(&client_p->node, &global_client_list);
750
751 update_client_exit_stats(client_p);
752 }
753
754
755 /*
756 * find_person - find person by (nick)name.
757 * inputs - pointer to name
758 * output - return client pointer
759 * side effects -
760 */
761 struct Client *
762 find_person(const char *name)
763 {
764 struct Client *c2ptr;
765
766 c2ptr = find_client(name);
767
768 if(c2ptr && IsPerson(c2ptr))
769 return (c2ptr);
770 return (NULL);
771 }
772
773 struct Client *
774 find_named_person(const char *name)
775 {
776 struct Client *c2ptr;
777
778 c2ptr = find_named_client(name);
779
780 if(c2ptr && IsPerson(c2ptr))
781 return (c2ptr);
782 return (NULL);
783 }
784
785
786 /*
787 * find_chasing - find the client structure for a nick name (user)
788 * using history mechanism if necessary. If the client is not found,
789 * an error message (NO SUCH NICK) is generated. If the client was found
790 * through the history, chasing will be 1 and otherwise 0.
791 */
792 struct Client *
793 find_chasing(struct Client *source_p, const char *user, int *chasing)
794 {
795 struct Client *who;
796
797 if(MyClient(source_p))
798 who = find_named_person(user);
799 else
800 who = find_person(user);
801
802 if(chasing)
803 *chasing = 0;
804
805 if(who || IsDigit(*user))
806 return who;
807
808 if(!(who = get_history(user, (long) KILLCHASETIMELIMIT)))
809 {
810 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
811 form_str(ERR_NOSUCHNICK), user);
812 return (NULL);
813 }
814 if(chasing)
815 *chasing = 1;
816 return who;
817 }
818
819 /*
820 * get_client_name - Return the name of the client
821 * for various tracking and
822 * admin purposes. The main purpose of this function is to
823 * return the "socket host" name of the client, if that
824 * differs from the advertised name (other than case).
825 * But, this can be used to any client structure.
826 *
827 * NOTE 1:
828 * Watch out the allocation of "nbuf", if either source_p->name
829 * or source_p->sockhost gets changed into pointers instead of
830 * directly allocated within the structure...
831 *
832 * NOTE 2:
833 * Function return either a pointer to the structure (source_p) or
834 * to internal buffer (nbuf). *NEVER* use the returned pointer
835 * to modify what it points!!!
836 */
837
838 const char *
839 get_client_name(struct Client *client, int showip)
840 {
841 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
842
843 s_assert(NULL != client);
844 if(client == NULL)
845 return NULL;
846
847 if(MyConnect(client))
848 {
849 if(!irccmp(client->name, client->host))
850 return client->name;
851
852 if(ConfigFileEntry.hide_spoof_ips &&
853 showip == SHOW_IP && IsIPSpoof(client))
854 showip = MASK_IP;
855 #ifdef HIDE_SERVERS_IPS
856 if(IsAnyServer(client))
857 showip = MASK_IP;
858 #endif
859
860 /* And finally, let's get the host information, ip or name */
861 switch (showip)
862 {
863 case SHOW_IP:
864 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
865 client->name, client->username,
866 client->sockhost);
867 break;
868 case MASK_IP:
869 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
870 client->name, client->username);
871 break;
872 default:
873 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
874 client->name, client->username, client->host);
875 }
876 return nbuf;
877 }
878
879 /* As pointed out by Adel Mezibra
880 * Neph|l|m@EFnet. Was missing a return here.
881 */
882 return client->name;
883 }
884
885 const char *
886 get_server_name(struct Client *target_p, int showip)
887 {
888 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
889
890 if(target_p == NULL)
891 return NULL;
892
893 if(!MyConnect(target_p) || !irccmp(target_p->name, target_p->host))
894 return target_p->name;
895
896 #ifdef HIDE_SERVERS_IPS
897 if(EmptyString(target_p->name))
898 {
899 rb_snprintf(nbuf, sizeof(nbuf), "[%s@255.255.255.255]",
900 target_p->username);
901 return nbuf;
902 }
903 else
904 return target_p->name;
905 #endif
906
907 switch (showip)
908 {
909 case SHOW_IP:
910 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
911 target_p->name, target_p->username,
912 target_p->sockhost);
913 break;
914
915 case MASK_IP:
916 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
917 target_p->name, target_p->username);
918
919 default:
920 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
921 target_p->name, target_p->username,
922 target_p->host);
923 }
924
925 return nbuf;
926 }
927
928 /* log_client_name()
929 *
930 * This version is the same as get_client_name, but doesnt contain the
931 * code that will hide IPs always. This should be used for logfiles.
932 */
933 const char *
934 log_client_name(struct Client *target_p, int showip)
935 {
936 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
937
938 if(target_p == NULL)
939 return NULL;
940
941 if(MyConnect(target_p))
942 {
943 if(irccmp(target_p->name, target_p->host) == 0)
944 return target_p->name;
945
946 switch (showip)
947 {
948 case SHOW_IP:
949 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name,
950 target_p->username, target_p->sockhost);
951 break;
952
953 case MASK_IP:
954 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
955 target_p->name, target_p->username);
956
957 default:
958 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name,
959 target_p->username, target_p->host);
960 }
961
962 return nbuf;
963 }
964
965 return target_p->name;
966 }
967
968 /* is_remote_connect - Returns whether a server was /connect'ed by a remote
969 * oper (send notices netwide) */
970 int
971 is_remote_connect(struct Client *client_p)
972 {
973 struct Client *oper;
974
975 if (client_p->serv == NULL)
976 return FALSE;
977 oper = find_named_person(client_p->serv->by);
978 return oper != NULL && IsOper(oper) && !MyConnect(oper);
979 }
980
981 static void
982 free_exited_clients(void *unused)
983 {
984 rb_dlink_node *ptr, *next;
985 struct Client *target_p;
986
987 RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
988 {
989 target_p = ptr->data;
990
991 #ifdef DEBUG_EXITED_CLIENTS
992 {
993 struct abort_client *abt;
994 rb_dlink_node *aptr;
995 int found = 0;
996
997 RB_DLINK_FOREACH(aptr, abort_list.head)
998 {
999 abt = aptr->data;
1000 if(abt->client == target_p)
1001 {
1002 s_assert(0);
1003 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1004 "On abort_list: %s stat: %u flags: %u/%u handler: %c",
1005 target_p->name, (unsigned int) target_p->status,
1006 target_p->flags, target_p->flags2, target_p->handler);
1007 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1008 "Please report this to the charybdis developers!");
1009 found++;
1010 }
1011 }
1012
1013 if(found)
1014 {
1015 rb_dlinkDestroy(ptr, &dead_list);
1016 continue;
1017 }
1018 }
1019 #endif
1020
1021 if(ptr->data == NULL)
1022 {
1023 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1024 "Warning: null client on dead_list!");
1025 rb_dlinkDestroy(ptr, &dead_list);
1026 continue;
1027 }
1028 release_client_state(target_p);
1029 free_client(target_p);
1030 rb_dlinkDestroy(ptr, &dead_list);
1031 }
1032
1033 #ifdef DEBUG_EXITED_CLIENTS
1034 RB_DLINK_FOREACH_SAFE(ptr, next, dead_remote_list.head)
1035 {
1036 target_p = ptr->data;
1037
1038 if(ptr->data == NULL)
1039 {
1040 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1041 "Warning: null client on dead_list!");
1042 rb_dlinkDestroy(ptr, &dead_list);
1043 continue;
1044 }
1045 release_client_state(target_p);
1046 free_client(target_p);
1047 rb_dlinkDestroy(ptr, &dead_remote_list);
1048 }
1049 #endif
1050
1051 }
1052
1053 /*
1054 ** Recursively send QUITs and SQUITs for source_p and all its dependent clients
1055 ** and servers to those servers that need them. A server needs the client
1056 ** QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
1057 ** isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
1058 ** a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
1059 ** on that one -orabidoo
1060 */
1061 static void
1062 recurse_send_quits(struct Client *client_p, struct Client *source_p,
1063 struct Client *to, const char *comment1,
1064 const char *comment)
1065 {
1066 struct Client *target_p;
1067 rb_dlink_node *ptr, *ptr_next;
1068 /* If this server can handle quit storm (QS) removal
1069 * of dependents, just send the SQUIT
1070 */
1071
1072 if(IsCapable(to, CAP_QS))
1073 {
1074 sendto_one(to, "SQUIT %s :%s",
1075 get_id(source_p, to), comment);
1076 }
1077 else
1078 {
1079 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1080 {
1081 target_p = ptr->data;
1082 sendto_one(to, ":%s QUIT :%s", target_p->name, comment1);
1083 }
1084 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
1085 {
1086 target_p = ptr->data;
1087 recurse_send_quits(client_p, target_p, to, comment1, comment);
1088 }
1089 sendto_one(to, "SQUIT %s :%s", source_p->name, comment);
1090 }
1091 }
1092
1093 /*
1094 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1095 ** already been sent. we make sure to exit a server's dependent clients
1096 ** and servers before the server itself; exit_one_client takes care of
1097 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1098 */
1099 /*
1100 * added sanity test code.... source_p->serv might be NULL...
1101 */
1102 static void
1103 recurse_remove_clients(struct Client *source_p, const char *comment)
1104 {
1105 struct Client *target_p;
1106 rb_dlink_node *ptr, *ptr_next;
1107
1108 if(IsMe(source_p))
1109 return;
1110
1111 if(source_p->serv == NULL) /* oooops. uh this is actually a major bug */
1112 return;
1113
1114 /* this is very ugly, but it saves cpu :P */
1115 if(ConfigFileEntry.nick_delay > 0)
1116 {
1117 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1118 {
1119 target_p = ptr->data;
1120 target_p->flags |= FLAGS_KILLED;
1121 add_nd_entry(target_p->name);
1122
1123 if(!IsDead(target_p) && !IsClosing(target_p))
1124 exit_remote_client(NULL, target_p, &me, comment);
1125 }
1126 }
1127 else
1128 {
1129 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1130 {
1131 target_p = ptr->data;
1132 target_p->flags |= FLAGS_KILLED;
1133
1134 if(!IsDead(target_p) && !IsClosing(target_p))
1135 exit_remote_client(NULL, target_p, &me, comment);
1136 }
1137 }
1138
1139 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
1140 {
1141 target_p = ptr->data;
1142 recurse_remove_clients(target_p, comment);
1143 qs_server(NULL, target_p, &me, comment);
1144 }
1145 }
1146
1147 /*
1148 ** Remove *everything* that depends on source_p, from all lists, and sending
1149 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
1150 ** and its SQUITs have been sent except for the upstream one -orabidoo
1151 */
1152 static void
1153 remove_dependents(struct Client *client_p,
1154 struct Client *source_p,
1155 struct Client *from, const char *comment, const char *comment1)
1156 {
1157 struct Client *to;
1158 rb_dlink_node *ptr, *next;
1159
1160 RB_DLINK_FOREACH_SAFE(ptr, next, serv_list.head)
1161 {
1162 to = ptr->data;
1163
1164 if(IsMe(to) || to == source_p->from ||
1165 (to == client_p && IsCapable(to, CAP_QS)))
1166 continue;
1167
1168 recurse_send_quits(client_p, source_p, to, comment1, comment);
1169 }
1170
1171 recurse_remove_clients(source_p, comment1);
1172 }
1173
1174 void
1175 exit_aborted_clients(void *unused)
1176 {
1177 struct abort_client *abt;
1178 rb_dlink_node *ptr, *next;
1179 RB_DLINK_FOREACH_SAFE(ptr, next, abort_list.head)
1180 {
1181 abt = ptr->data;
1182
1183 #ifdef DEBUG_EXITED_CLIENTS
1184 {
1185 if(rb_dlinkFind(abt->client, &dead_list))
1186 {
1187 s_assert(0);
1188 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1189 "On dead_list: %s stat: %u flags: %u/%u handler: %c",
1190 abt->client->name, (unsigned int) abt->client->status,
1191 abt->client->flags, abt->client->flags2, abt->client->handler);
1192 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1193 "Please report this to the charybdis developers!");
1194 continue;
1195 }
1196 }
1197 #endif
1198
1199 s_assert(*((unsigned long*)abt->client) != 0xdeadbeef); /* This is lame but its a debug thing */
1200 rb_dlinkDelete(ptr, &abort_list);
1201
1202 if(IsAnyServer(abt->client))
1203 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1204 "Closing link to %s: %s",
1205 get_server_name(abt->client, HIDE_IP), abt->notice);
1206
1207 /* its no longer on abort list - we *must* remove
1208 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1209 */
1210 abt->client->flags &= ~FLAGS_CLOSING;
1211 exit_client(abt->client, abt->client, &me, abt->notice);
1212 rb_free(abt);
1213 }
1214 }
1215
1216
1217 /*
1218 * dead_link - Adds client to a list of clients that need an exit_client()
1219 *
1220 */
1221 void
1222 dead_link(struct Client *client_p)
1223 {
1224 struct abort_client *abt;
1225
1226 s_assert(!IsMe(client_p));
1227 if(IsDead(client_p) || IsClosing(client_p) || IsMe(client_p))
1228 return;
1229
1230 abt = (struct abort_client *) rb_malloc(sizeof(struct abort_client));
1231
1232 if(client_p->flags & FLAGS_SENDQEX)
1233 rb_strlcpy(abt->notice, "Max SendQ exceeded", sizeof(abt->notice));
1234 else
1235 rb_snprintf(abt->notice, sizeof(abt->notice), "Write error: %s", strerror(errno));
1236
1237 abt->client = client_p;
1238 SetIOError(client_p);
1239 SetDead(client_p);
1240 SetClosing(client_p);
1241 rb_dlinkAdd(abt, &abt->node, &abort_list);
1242 }
1243
1244
1245 /* This does the remove of the user from channels..local or remote */
1246 static inline void
1247 exit_generic_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1248 const char *comment)
1249 {
1250 rb_dlink_node *ptr, *next_ptr;
1251
1252 if(IsOper(source_p))
1253 rb_dlinkFindDestroy(source_p, &oper_list);
1254
1255 sendto_common_channels_local(source_p, ":%s!%s@%s QUIT :%s",
1256 source_p->name,
1257 source_p->username, source_p->host, comment);
1258
1259 remove_user_from_channels(source_p);
1260
1261 /* Should not be in any channels now */
1262 s_assert(source_p->user->channel.head == NULL);
1263
1264 /* Clean up invitefield */
1265 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->user->invited.head)
1266 {
1267 del_invite(ptr->data, source_p);
1268 }
1269
1270 /* Clean up allow lists */
1271 del_all_accepts(source_p);
1272
1273 add_history(source_p, 0);
1274 off_history(source_p);
1275
1276 monitor_signoff(source_p);
1277
1278 if(has_id(source_p))
1279 del_from_id_hash(source_p->id, source_p);
1280
1281 del_from_hostname_hash(source_p->orighost, source_p);
1282 del_from_client_hash(source_p->name, source_p);
1283 remove_client_from_list(source_p);
1284 }
1285
1286 /*
1287 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1288 */
1289
1290 static int
1291 exit_remote_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1292 const char *comment)
1293 {
1294 exit_generic_client(client_p, source_p, from, comment);
1295
1296 if(source_p->servptr && source_p->servptr->serv)
1297 {
1298 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users);
1299 }
1300
1301 if((source_p->flags & FLAGS_KILLED) == 0)
1302 {
1303 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1304 ":%s QUIT :%s", use_id(source_p), comment);
1305 }
1306
1307 SetDead(source_p);
1308 #ifdef DEBUG_EXITED_CLIENTS
1309 rb_dlinkAddAlloc(source_p, &dead_remote_list);
1310 #else
1311 rb_dlinkAddAlloc(source_p, &dead_list);
1312 #endif
1313 return(CLIENT_EXITED);
1314 }
1315
1316 /*
1317 * This assumes IsUnknown(source_p) == TRUE and MyConnect(source_p) == TRUE
1318 */
1319
1320 static int
1321 exit_unknown_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1322 const char *comment)
1323 {
1324 delete_auth_queries(source_p);
1325 if (source_p->localClient->dnsquery)
1326 {
1327 delete_resolver_queries(source_p->localClient->dnsquery);
1328 rb_free(source_p->localClient->dnsquery);
1329 }
1330 del_unknown_ip(source_p);
1331 rb_dlinkDelete(&source_p->localClient->tnode, &unknown_list);
1332
1333 if(!IsIOError(source_p))
1334 sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
1335 source_p->user != NULL ? source_p->host : "127.0.0.1",
1336 comment);
1337
1338 close_connection(source_p);
1339
1340 if(has_id(source_p))
1341 del_from_id_hash(source_p->id, source_p);
1342
1343 del_from_hostname_hash(source_p->host, source_p);
1344 del_from_client_hash(source_p->name, source_p);
1345 remove_client_from_list(source_p);
1346 SetDead(source_p);
1347 rb_dlinkAddAlloc(source_p, &dead_list);
1348
1349 /* Note that we don't need to add unknowns to the dead_list */
1350 return(CLIENT_EXITED);
1351 }
1352
1353 static int
1354 exit_remote_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1355 const char *comment)
1356 {
1357 static char comment1[(HOSTLEN*2)+2];
1358 static char newcomment[BUFSIZE];
1359 struct Client *target_p;
1360
1361 if(ConfigServerHide.flatten_links)
1362 strcpy(comment1, "*.net *.split");
1363 else
1364 {
1365 strcpy(comment1, source_p->servptr->name);
1366 strcat(comment1, " ");
1367 strcat(comment1, source_p->name);
1368 }
1369 if (IsPerson(from))
1370 rb_snprintf(newcomment, sizeof(newcomment), "by %s: %s",
1371 from->name, comment);
1372
1373 if(source_p->serv != NULL)
1374 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1375
1376 if(source_p->servptr && source_p->servptr->serv)
1377 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1378 else
1379 s_assert(0);
1380
1381 rb_dlinkFindDestroy(source_p, &global_serv_list);
1382 target_p = source_p->from;
1383
1384 if(target_p != NULL && IsServer(target_p) && target_p != client_p &&
1385 !IsMe(target_p) && (source_p->flags & FLAGS_KILLED) == 0)
1386 {
1387 sendto_one(target_p, ":%s SQUIT %s :%s",
1388 get_id(from, target_p), get_id(source_p, target_p),
1389 comment);
1390 }
1391
1392 if(has_id(source_p))
1393 del_from_id_hash(source_p->id, source_p);
1394
1395 del_from_client_hash(source_p->name, source_p);
1396 remove_client_from_list(source_p);
1397 scache_split(source_p->serv->nameinfo);
1398
1399 SetDead(source_p);
1400 #ifdef DEBUG_EXITED_CLIENTS
1401 rb_dlinkAddAlloc(source_p, &dead_remote_list);
1402 #else
1403 rb_dlinkAddAlloc(source_p, &dead_list);
1404 #endif
1405 return 0;
1406 }
1407
1408 static int
1409 qs_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1410 const char *comment)
1411 {
1412 struct Client *target_p;
1413
1414 if(source_p->servptr && source_p->servptr->serv)
1415 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1416 else
1417 s_assert(0);
1418
1419 rb_dlinkFindDestroy(source_p, &global_serv_list);
1420 target_p = source_p->from;
1421
1422 if(has_id(source_p))
1423 del_from_id_hash(source_p->id, source_p);
1424
1425 del_from_client_hash(source_p->name, source_p);
1426 remove_client_from_list(source_p);
1427 scache_split(source_p->serv->nameinfo);
1428
1429 SetDead(source_p);
1430 rb_dlinkAddAlloc(source_p, &dead_list);
1431 return 0;
1432 }
1433
1434 static int
1435 exit_local_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1436 const char *comment)
1437 {
1438 static char comment1[(HOSTLEN*2)+2];
1439 static char newcomment[BUFSIZE];
1440 unsigned int sendk, recvk;
1441
1442 rb_dlinkDelete(&source_p->localClient->tnode, &serv_list);
1443 rb_dlinkFindDestroy(source_p, &global_serv_list);
1444
1445 unset_chcap_usage_counts(source_p);
1446 sendk = source_p->localClient->sendK;
1447 recvk = source_p->localClient->receiveK;
1448
1449 /* Always show source here, so the server notices show
1450 * which side initiated the split -- jilles
1451 */
1452 rb_snprintf(newcomment, sizeof(newcomment), "by %s: %s",
1453 from == source_p ? me.name : from->name, comment);
1454 if (!IsIOError(source_p))
1455 sendto_one(source_p, "SQUIT %s :%s", use_id(source_p),
1456 newcomment);
1457 if(client_p != NULL && source_p != client_p && !IsIOError(source_p))
1458 {
1459 sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1460 source_p->name, comment);
1461 }
1462
1463 if(source_p->servptr && source_p->servptr->serv)
1464 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1465 else
1466 s_assert(0);
1467
1468
1469 close_connection(source_p);
1470
1471 if(ConfigServerHide.flatten_links)
1472 strcpy(comment1, "*.net *.split");
1473 else
1474 {
1475 strcpy(comment1, source_p->servptr->name);
1476 strcat(comment1, " ");
1477 strcat(comment1, source_p->name);
1478 }
1479
1480 if(source_p->serv != NULL)
1481 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1482
1483 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s was connected"
1484 " for %ld seconds. %d/%d sendK/recvK.",
1485 source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
1486
1487 ilog(L_SERVER, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1488 source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
1489
1490 if(has_id(source_p))
1491 del_from_id_hash(source_p->id, source_p);
1492
1493 del_from_client_hash(source_p->name, source_p);
1494 remove_client_from_list(source_p);
1495 scache_split(source_p->serv->nameinfo);
1496
1497 SetDead(source_p);
1498 rb_dlinkAddAlloc(source_p, &dead_list);
1499 return 0;
1500 }
1501
1502
1503 /*
1504 * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE
1505 */
1506
1507 static int
1508 exit_local_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1509 const char *comment)
1510 {
1511 unsigned long on_for;
1512 char tbuf[26];
1513
1514 exit_generic_client(client_p, source_p, from, comment);
1515 clear_monitor(source_p);
1516
1517 s_assert(IsPerson(source_p));
1518 rb_dlinkDelete(&source_p->localClient->tnode, &lclient_list);
1519 rb_dlinkDelete(&source_p->lnode, &me.serv->users);
1520
1521 if(IsOper(source_p))
1522 rb_dlinkFindDestroy(source_p, &local_oper_list);
1523
1524 sendto_realops_snomask(SNO_CCONN, L_ALL,
1525 "Client exiting: %s (%s@%s) [%s] [%s]",
1526 source_p->name,
1527 source_p->username, source_p->host, comment,
1528 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255");
1529
1530 sendto_realops_snomask(SNO_CCONNEXT, L_ALL,
1531 "CLIEXIT %s %s %s %s 0 %s",
1532 source_p->name, source_p->username, source_p->host,
1533 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255",
1534 comment);
1535
1536 on_for = rb_current_time() - source_p->localClient->firsttime;
1537
1538 ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %d/%d",
1539 rb_ctime(rb_current_time(), tbuf, sizeof(tbuf)), on_for / 3600,
1540 (on_for % 3600) / 60, on_for % 60,
1541 source_p->name, source_p->username, source_p->host,
1542 source_p->localClient->sendK, source_p->localClient->receiveK);
1543
1544 sendto_one(source_p, "ERROR :Closing Link: %s (%s)", source_p->host, comment);
1545 close_connection(source_p);
1546
1547 if((source_p->flags & FLAGS_KILLED) == 0)
1548 {
1549 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1550 ":%s QUIT :%s", use_id(source_p), comment);
1551 }
1552
1553 SetDead(source_p);
1554 rb_dlinkAddAlloc(source_p, &dead_list);
1555 return(CLIENT_EXITED);
1556 }
1557
1558
1559 /*
1560 ** exit_client - This is old "m_bye". Name changed, because this is not a
1561 ** protocol function, but a general server utility function.
1562 **
1563 ** This function exits a client of *any* type (user, server, etc)
1564 ** from this server. Also, this generates all necessary prototol
1565 ** messages that this exit may cause.
1566 **
1567 ** 1) If the client is a local client, then this implicitly
1568 ** exits all other clients depending on this connection (e.g.
1569 ** remote clients having 'from'-field that points to this.
1570 **
1571 ** 2) If the client is a remote client, then only this is exited.
1572 **
1573 ** For convenience, this function returns a suitable value for
1574 ** m_function return value:
1575 **
1576 ** CLIENT_EXITED if (client_p == source_p)
1577 ** 0 if (client_p != source_p)
1578 */
1579 int
1580 exit_client(struct Client *client_p, /* The local client originating the
1581 * exit or NULL, if this exit is
1582 * generated by this server for
1583 * internal reasons.
1584 * This will not get any of the
1585 * generated messages. */
1586 struct Client *source_p, /* Client exiting */
1587 struct Client *from, /* Client firing off this Exit,
1588 * never NULL! */
1589 const char *comment /* Reason for the exit */
1590 )
1591 {
1592 hook_data_client_exit hdata;
1593 if(IsClosing(source_p))
1594 return -1;
1595
1596 /* note, this HAS to be here, when we exit a client we attempt to
1597 * send them data, if this generates a write error we must *not* add
1598 * them to the abort list --fl
1599 */
1600 SetClosing(source_p);
1601
1602 hdata.local_link = client_p;
1603 hdata.target = source_p;
1604 hdata.from = from;
1605 hdata.comment = comment;
1606 call_hook(h_client_exit, &hdata);
1607
1608 if(MyConnect(source_p))
1609 {
1610 /* Local clients of various types */
1611 if(IsPerson(source_p))
1612 return exit_local_client(client_p, source_p, from, comment);
1613 else if(IsServer(source_p))
1614 return exit_local_server(client_p, source_p, from, comment);
1615 /* IsUnknown || IsConnecting || IsHandShake */
1616 else if(!IsReject(source_p))
1617 return exit_unknown_client(client_p, source_p, from, comment);
1618 }
1619 else
1620 {
1621 /* Remotes */
1622 if(IsPerson(source_p))
1623 return exit_remote_client(client_p, source_p, from, comment);
1624 else if(IsServer(source_p))
1625 return exit_remote_server(client_p, source_p, from, comment);
1626 }
1627
1628 return -1;
1629 }
1630
1631 /*
1632 * Count up local client memory
1633 */
1634
1635 /* XXX one common Client list now */
1636 void
1637 count_local_client_memory(size_t * count, size_t * local_client_memory_used)
1638 {
1639 size_t lusage;
1640 rb_bh_usage(lclient_heap, count, NULL, &lusage, NULL);
1641 *local_client_memory_used = lusage + (*count * (sizeof(void *) + sizeof(struct Client)));
1642 }
1643
1644 /*
1645 * Count up remote client memory
1646 */
1647 void
1648 count_remote_client_memory(size_t * count, size_t * remote_client_memory_used)
1649 {
1650 size_t lcount, rcount;
1651 rb_bh_usage(lclient_heap, &lcount, NULL, NULL, NULL);
1652 rb_bh_usage(client_heap, &rcount, NULL, NULL, NULL);
1653 *count = rcount - lcount;
1654 *remote_client_memory_used = *count * (sizeof(void *) + sizeof(struct Client));
1655 }
1656
1657
1658 /*
1659 * accept processing, this adds a form of "caller ID" to ircd
1660 *
1661 * If a client puts themselves into "caller ID only" mode,
1662 * only clients that match a client pointer they have put on
1663 * the accept list will be allowed to message them.
1664 *
1665 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1666 *
1667 * [target.allow_list] -> [ source1 ] -> [source2 ]
1668 *
1669 * i.e. a target will have a link list of source pointers it will allow
1670 * each source client then has a back pointer pointing back
1671 * to the client that has it on its accept list.
1672 * This allows for exit_one_client to remove these now bogus entries
1673 * from any client having an accept on them.
1674 */
1675 /*
1676 * del_all_accepts
1677 *
1678 * inputs - pointer to exiting client
1679 * output - NONE
1680 * side effects - Walk through given clients allow_list and on_allow_list
1681 * remove all references to this client
1682 */
1683 void
1684 del_all_accepts(struct Client *client_p)
1685 {
1686 rb_dlink_node *ptr;
1687 rb_dlink_node *next_ptr;
1688 struct Client *target_p;
1689
1690 if(MyClient(client_p) && client_p->localClient->allow_list.head)
1691 {
1692 /* clear this clients accept list, and remove them from
1693 * everyones on_accept_list
1694 */
1695 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head)
1696 {
1697 target_p = ptr->data;
1698 rb_dlinkFindDestroy(client_p, &target_p->on_allow_list);
1699 rb_dlinkDestroy(ptr, &client_p->localClient->allow_list);
1700 }
1701 }
1702
1703 /* remove this client from everyones accept list */
1704 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
1705 {
1706 target_p = ptr->data;
1707 rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
1708 rb_dlinkDestroy(ptr, &client_p->on_allow_list);
1709 }
1710 }
1711
1712 /*
1713 * show_ip() - asks if the true IP shoudl be shown when source is
1714 * askin for info about target
1715 *
1716 * Inputs - source_p who is asking
1717 * - target_p who do we want the info on
1718 * Output - returns 1 if clear IP can be showed, otherwise 0
1719 * Side Effects - none
1720 */
1721
1722 int
1723 show_ip(struct Client *source_p, struct Client *target_p)
1724 {
1725 if(IsAnyServer(target_p))
1726 {
1727 #ifndef HIDE_SERVERS_IPS
1728 if(source_p == NULL || IsOper(source_p))
1729 return 1;
1730 #endif
1731 return 0;
1732 }
1733 else if(IsIPSpoof(target_p))
1734 {
1735 /* source == NULL indicates message is being sent
1736 * to local opers.
1737 */
1738 if(!ConfigFileEntry.hide_spoof_ips &&
1739 (source_p == NULL || MyOper(source_p)))
1740 return 1;
1741 return 0;
1742 }
1743 else if(IsDynSpoof(target_p) && (source_p != NULL && !IsOper(source_p)))
1744 return 0;
1745 else
1746 return 1;
1747 }
1748
1749 int
1750 show_ip_conf(struct ConfItem *aconf, struct Client *source_p)
1751 {
1752 if(IsConfDoSpoofIp(aconf))
1753 {
1754 if(!ConfigFileEntry.hide_spoof_ips && MyOper(source_p))
1755 return 1;
1756
1757 return 0;
1758 }
1759 else
1760 return 1;
1761 }
1762
1763 /*
1764 * initUser
1765 *
1766 * inputs - none
1767 * outputs - none
1768 *
1769 * side effects - Creates a block heap for struct Users
1770 *
1771 */
1772 static rb_bh *user_heap;
1773 void
1774 initUser(void)
1775 {
1776 user_heap = rb_bh_create(sizeof(struct User), USER_HEAP_SIZE, "user_heap");
1777 if(!user_heap)
1778 rb_outofmemory();
1779 }
1780
1781 /*
1782 * make_user
1783 *
1784 * inputs - pointer to client struct
1785 * output - pointer to struct User
1786 * side effects - add's an User information block to a client
1787 * if it was not previously allocated.
1788 */
1789 struct User *
1790 make_user(struct Client *client_p)
1791 {
1792 struct User *user;
1793
1794 user = client_p->user;
1795 if(!user)
1796 {
1797 user = (struct User *) rb_bh_alloc(user_heap);
1798 user->refcnt = 1;
1799 client_p->user = user;
1800 }
1801 return user;
1802 }
1803
1804 /*
1805 * make_server
1806 *
1807 * inputs - pointer to client struct
1808 * output - pointer to server_t
1809 * side effects - add's an Server information block to a client
1810 * if it was not previously allocated.
1811 */
1812 server_t *
1813 make_server(struct Client *client_p)
1814 {
1815 server_t *serv = client_p->serv;
1816
1817 if(!serv)
1818 {
1819 serv = (server_t *) rb_malloc(sizeof(server_t));
1820 client_p->serv = serv;
1821 }
1822 return client_p->serv;
1823 }
1824
1825 /*
1826 * free_user
1827 *
1828 * inputs - pointer to user struct
1829 * - pointer to client struct
1830 * output - none
1831 * side effects - Decrease user reference count by one and release block,
1832 * if count reaches 0
1833 */
1834 void
1835 free_user(struct User *user, struct Client *client_p)
1836 {
1837 free_away(client_p);
1838
1839 if(--user->refcnt <= 0)
1840 {
1841 if(user->away)
1842 rb_free((char *) user->away);
1843 /*
1844 * sanity check
1845 */
1846 if(user->refcnt < 0 || user->invited.head || user->channel.head)
1847 {
1848 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1849 "* %#lx user (%s!%s@%s) %#lx %#lx %#lx %lu %d *",
1850 (unsigned long) client_p,
1851 client_p ? client_p->
1852 name : "<noname>",
1853 client_p->username,
1854 client_p->host,
1855 (unsigned long) user,
1856 (unsigned long) user->invited.head,
1857 (unsigned long) user->channel.head,
1858 rb_dlink_list_length(&user->channel),
1859 user->refcnt);
1860 s_assert(!user->refcnt);
1861 s_assert(!user->invited.head);
1862 s_assert(!user->channel.head);
1863 }
1864
1865 rb_bh_free(user_heap, user);
1866 }
1867 }
1868
1869 void
1870 allocate_away(struct Client *client_p)
1871 {
1872 if(client_p->user->away == NULL)
1873 client_p->user->away = rb_bh_alloc(away_heap);
1874 }
1875
1876
1877 void
1878 free_away(struct Client *client_p)
1879 {
1880 if(client_p->user != NULL && client_p->user->away != NULL) {
1881 rb_bh_free(away_heap, client_p->user->away);
1882 client_p->user->away = NULL;
1883 }
1884 }
1885
1886 void
1887 init_uid(void)
1888 {
1889 int i;
1890
1891 for(i = 0; i < 3; i++)
1892 current_uid[i] = me.id[i];
1893
1894 for(i = 3; i < 9; i++)
1895 current_uid[i] = 'A';
1896
1897 current_uid[9] = '\0';
1898 }
1899
1900
1901 char *
1902 generate_uid(void)
1903 {
1904 int i;
1905
1906 for(i = 8; i > 3; i--)
1907 {
1908 if(current_uid[i] == 'Z')
1909 {
1910 current_uid[i] = '0';
1911 return current_uid;
1912 }
1913 else if(current_uid[i] != '9')
1914 {
1915 current_uid[i]++;
1916 return current_uid;
1917 }
1918 else
1919 current_uid[i] = 'A';
1920 }
1921
1922 /* if this next if() triggers, we're fucked. */
1923 if(current_uid[3] == 'Z')
1924 {
1925 current_uid[i] = 'A';
1926 s_assert(0);
1927 }
1928 else
1929 current_uid[i]++;
1930
1931 return current_uid;
1932 }
1933
1934 /*
1935 * close_connection
1936 * Close the physical connection. This function must make
1937 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
1938 */
1939 void
1940 close_connection(struct Client *client_p)
1941 {
1942 s_assert(client_p != NULL);
1943 if(client_p == NULL)
1944 return;
1945
1946 s_assert(MyConnect(client_p));
1947 if(!MyConnect(client_p))
1948 return;
1949
1950 if(IsServer(client_p))
1951 {
1952 struct server_conf *server_p;
1953
1954 ServerStats.is_sv++;
1955 ServerStats.is_sbs += client_p->localClient->sendB;
1956 ServerStats.is_sbr += client_p->localClient->receiveB;
1957 ServerStats.is_sti += rb_current_time() - client_p->localClient->firsttime;
1958
1959 /*
1960 * If the connection has been up for a long amount of time, schedule
1961 * a 'quick' reconnect, else reset the next-connect cycle.
1962 */
1963 if((server_p = find_server_conf(client_p->name)) != NULL)
1964 {
1965 /*
1966 * Reschedule a faster reconnect, if this was a automatically
1967 * connected configuration entry. (Note that if we have had
1968 * a rehash in between, the status has been changed to
1969 * CONF_ILLEGAL). But only do this if it was a "good" link.
1970 */
1971 server_p->hold = time(NULL);
1972 server_p->hold +=
1973 (server_p->hold - client_p->localClient->lasttime >
1974 HANGONGOODLINK) ? HANGONRETRYDELAY : ConFreq(server_p->class);
1975 }
1976
1977 }
1978 else if(IsClient(client_p))
1979 {
1980 ServerStats.is_cl++;
1981 ServerStats.is_cbs += client_p->localClient->sendB;
1982 ServerStats.is_cbr += client_p->localClient->receiveB;
1983 ServerStats.is_cti += rb_current_time() - client_p->localClient->firsttime;
1984 }
1985 else
1986 ServerStats.is_ni++;
1987
1988 if(client_p->localClient->F != NULL)
1989 {
1990 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
1991 if(!IsIOError(client_p))
1992 send_queued(client_p);
1993
1994 del_from_cli_fd_hash(client_p);
1995 rb_close(client_p->localClient->F);
1996 client_p->localClient->F = NULL;
1997 }
1998
1999 rb_linebuf_donebuf(&client_p->localClient->buf_sendq);
2000 rb_linebuf_donebuf(&client_p->localClient->buf_recvq);
2001 detach_conf(client_p);
2002
2003 /* XXX shouldnt really be done here. */
2004 detach_server_conf(client_p);
2005
2006 client_p->from = NULL; /* ...this should catch them! >:) --msa */
2007 ClearMyConnect(client_p);
2008 SetIOError(client_p);
2009 }
2010
2011
2012
2013 void
2014 error_exit_client(struct Client *client_p, int error)
2015 {
2016 /*
2017 * ...hmm, with non-blocking sockets we might get
2018 * here from quite valid reasons, although.. why
2019 * would select report "data available" when there
2020 * wasn't... so, this must be an error anyway... --msa
2021 * actually, EOF occurs when read() returns 0 and
2022 * in due course, select() returns that fd as ready
2023 * for reading even though it ends up being an EOF. -avalon
2024 */
2025 char errmsg[255];
2026 int current_error = rb_get_sockerr(client_p->localClient->F);
2027
2028 SetIOError(client_p);
2029
2030 if(IsServer(client_p) || IsHandshake(client_p))
2031 {
2032 if(error == 0)
2033 {
2034 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL,
2035 "Server %s closed the connection",
2036 get_server_name(client_p, SHOW_IP));
2037
2038 ilog(L_SERVER, "Server %s closed the connection",
2039 log_client_name(client_p, SHOW_IP));
2040 }
2041 else
2042 {
2043 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL,
2044 "Lost connection to %s: %s",
2045 client_p->name, strerror(current_error));
2046 ilog(L_SERVER, "Lost connection to %s: %s",
2047 log_client_name(client_p, SHOW_IP), strerror(current_error));
2048 }
2049 }
2050
2051 if(error == 0)
2052 rb_strlcpy(errmsg, "Remote host closed the connection", sizeof(errmsg));
2053 else
2054 rb_snprintf(errmsg, sizeof(errmsg), "Read error: %s", strerror(current_error));
2055
2056 exit_client(client_p, client_p, &me, errmsg);
2057 }