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