]> jfr.im git - irc/rqf/shadowircd.git/blob - src/client.c
Some global structs cleaned up a bit in their initalization and declarations.
[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 del_unknown_ip(source_p);
1406 rb_dlinkDelete(&source_p->localClient->tnode, &unknown_list);
1407
1408 if(!IsIOError(source_p))
1409 sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
1410 source_p->user != NULL ? source_p->host : "127.0.0.1",
1411 comment);
1412
1413 close_connection(source_p);
1414
1415 if(has_id(source_p))
1416 del_from_id_hash(source_p->id, source_p);
1417
1418 del_from_hostname_hash(source_p->host, source_p);
1419 del_from_client_hash(source_p->name, source_p);
1420 remove_client_from_list(source_p);
1421 SetDead(source_p);
1422 rb_dlinkAddAlloc(source_p, &dead_list);
1423
1424 /* Note that we don't need to add unknowns to the dead_list */
1425 return(CLIENT_EXITED);
1426 }
1427
1428 static int
1429 exit_remote_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1430 const char *comment)
1431 {
1432 static char comment1[(HOSTLEN*2)+2];
1433 static char newcomment[BUFSIZE];
1434 struct Client *target_p;
1435
1436 if(ConfigServerHide.flatten_links)
1437 strcpy(comment1, "*.net *.split");
1438 else
1439 {
1440 strcpy(comment1, source_p->servptr->name);
1441 strcat(comment1, " ");
1442 strcat(comment1, source_p->name);
1443 }
1444 if (IsPerson(from))
1445 rb_snprintf(newcomment, sizeof(newcomment), "by %s: %s",
1446 from->name, comment);
1447
1448 if(source_p->serv != NULL)
1449 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1450
1451 if(source_p->servptr && source_p->servptr->serv)
1452 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1453 else
1454 s_assert(0);
1455
1456 rb_dlinkFindDestroy(source_p, &global_serv_list);
1457 target_p = source_p->from;
1458
1459 if(target_p != NULL && IsServer(target_p) && target_p != client_p &&
1460 !IsMe(target_p) && (source_p->flags & FLAGS_KILLED) == 0)
1461 {
1462 sendto_one(target_p, ":%s SQUIT %s :%s",
1463 get_id(from, target_p), get_id(source_p, target_p),
1464 comment);
1465 }
1466
1467 if(has_id(source_p))
1468 del_from_id_hash(source_p->id, source_p);
1469
1470 del_from_client_hash(source_p->name, source_p);
1471 remove_client_from_list(source_p);
1472 scache_split(source_p->serv->nameinfo);
1473
1474 SetDead(source_p);
1475 #ifdef DEBUG_EXITED_CLIENTS
1476 rb_dlinkAddAlloc(source_p, &dead_remote_list);
1477 #else
1478 rb_dlinkAddAlloc(source_p, &dead_list);
1479 #endif
1480 return 0;
1481 }
1482
1483 static int
1484 qs_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1485 const char *comment)
1486 {
1487 struct Client *target_p;
1488
1489 if(source_p->servptr && source_p->servptr->serv)
1490 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1491 else
1492 s_assert(0);
1493
1494 rb_dlinkFindDestroy(source_p, &global_serv_list);
1495 target_p = source_p->from;
1496
1497 if(has_id(source_p))
1498 del_from_id_hash(source_p->id, source_p);
1499
1500 del_from_client_hash(source_p->name, source_p);
1501 remove_client_from_list(source_p);
1502 scache_split(source_p->serv->nameinfo);
1503
1504 SetDead(source_p);
1505 rb_dlinkAddAlloc(source_p, &dead_list);
1506 return 0;
1507 }
1508
1509 static int
1510 exit_local_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1511 const char *comment)
1512 {
1513 static char comment1[(HOSTLEN*2)+2];
1514 static char newcomment[BUFSIZE];
1515 unsigned int sendk, recvk;
1516
1517 rb_dlinkDelete(&source_p->localClient->tnode, &serv_list);
1518 rb_dlinkFindDestroy(source_p, &global_serv_list);
1519
1520 unset_chcap_usage_counts(source_p);
1521 sendk = source_p->localClient->sendK;
1522 recvk = source_p->localClient->receiveK;
1523
1524 /* Always show source here, so the server notices show
1525 * which side initiated the split -- jilles
1526 */
1527 rb_snprintf(newcomment, sizeof(newcomment), "by %s: %s",
1528 from == source_p ? me.name : from->name, comment);
1529 if (!IsIOError(source_p))
1530 sendto_one(source_p, "SQUIT %s :%s", use_id(source_p),
1531 newcomment);
1532 if(client_p != NULL && source_p != client_p && !IsIOError(source_p))
1533 {
1534 sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1535 source_p->name, comment);
1536 }
1537
1538 if(source_p->servptr && source_p->servptr->serv)
1539 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1540 else
1541 s_assert(0);
1542
1543
1544 close_connection(source_p);
1545
1546 if(ConfigServerHide.flatten_links)
1547 strcpy(comment1, "*.net *.split");
1548 else
1549 {
1550 strcpy(comment1, source_p->servptr->name);
1551 strcat(comment1, " ");
1552 strcat(comment1, source_p->name);
1553 }
1554
1555 if(source_p->serv != NULL)
1556 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1557
1558 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s was connected"
1559 " for %ld seconds. %d/%d sendK/recvK.",
1560 source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
1561
1562 ilog(L_SERVER, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1563 source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
1564
1565 if(has_id(source_p))
1566 del_from_id_hash(source_p->id, source_p);
1567
1568 del_from_client_hash(source_p->name, source_p);
1569 remove_client_from_list(source_p);
1570 scache_split(source_p->serv->nameinfo);
1571
1572 SetDead(source_p);
1573 rb_dlinkAddAlloc(source_p, &dead_list);
1574 return 0;
1575 }
1576
1577
1578 /*
1579 * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE
1580 */
1581
1582 static int
1583 exit_local_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1584 const char *comment)
1585 {
1586 unsigned long on_for;
1587
1588 exit_generic_client(client_p, source_p, from, comment);
1589 clear_monitor(source_p);
1590
1591 s_assert(IsPerson(source_p));
1592 rb_dlinkDelete(&source_p->localClient->tnode, &lclient_list);
1593 rb_dlinkDelete(&source_p->lnode, &me.serv->users);
1594
1595 if(IsOper(source_p))
1596 rb_dlinkFindDestroy(source_p, &local_oper_list);
1597
1598 sendto_realops_snomask(SNO_CCONN, L_ALL,
1599 "Client exiting: %s (%s@%s) [%s] [%s]",
1600 source_p->name,
1601 source_p->username, source_p->host, comment,
1602 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255");
1603
1604 sendto_realops_snomask(SNO_CCONNEXT, L_ALL,
1605 "CLIEXIT %s %s %s %s 0 %s",
1606 source_p->name, source_p->username, source_p->host,
1607 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255",
1608 comment);
1609
1610 on_for = rb_current_time() - source_p->localClient->firsttime;
1611
1612 ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %d/%d",
1613 myctime(rb_current_time()), on_for / 3600,
1614 (on_for % 3600) / 60, on_for % 60,
1615 source_p->name, source_p->username, source_p->host,
1616 source_p->localClient->sendK, source_p->localClient->receiveK);
1617
1618 sendto_one(source_p, "ERROR :Closing Link: %s (%s)", source_p->host, comment);
1619 close_connection(source_p);
1620
1621 if((source_p->flags & FLAGS_KILLED) == 0)
1622 {
1623 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1624 ":%s QUIT :%s", use_id(source_p), comment);
1625 sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
1626 ":%s QUIT :%s", source_p->name, comment);
1627 }
1628
1629 SetDead(source_p);
1630 rb_dlinkAddAlloc(source_p, &dead_list);
1631 return(CLIENT_EXITED);
1632 }
1633
1634
1635 /*
1636 ** exit_client - This is old "m_bye". Name changed, because this is not a
1637 ** protocol function, but a general server utility function.
1638 **
1639 ** This function exits a client of *any* type (user, server, etc)
1640 ** from this server. Also, this generates all necessary prototol
1641 ** messages that this exit may cause.
1642 **
1643 ** 1) If the client is a local client, then this implicitly
1644 ** exits all other clients depending on this connection (e.g.
1645 ** remote clients having 'from'-field that points to this.
1646 **
1647 ** 2) If the client is a remote client, then only this is exited.
1648 **
1649 ** For convenience, this function returns a suitable value for
1650 ** m_function return value:
1651 **
1652 ** CLIENT_EXITED if (client_p == source_p)
1653 ** 0 if (client_p != source_p)
1654 */
1655 int
1656 exit_client(struct Client *client_p, /* The local client originating the
1657 * exit or NULL, if this exit is
1658 * generated by this server for
1659 * internal reasons.
1660 * This will not get any of the
1661 * generated messages. */
1662 struct Client *source_p, /* Client exiting */
1663 struct Client *from, /* Client firing off this Exit,
1664 * never NULL! */
1665 const char *comment /* Reason for the exit */
1666 )
1667 {
1668 hook_data_client_exit hdata;
1669 if(IsClosing(source_p))
1670 return -1;
1671
1672 /* note, this HAS to be here, when we exit a client we attempt to
1673 * send them data, if this generates a write error we must *not* add
1674 * them to the abort list --fl
1675 */
1676 SetClosing(source_p);
1677
1678 hdata.local_link = client_p;
1679 hdata.target = source_p;
1680 hdata.from = from;
1681 hdata.comment = comment;
1682 call_hook(h_client_exit, &hdata);
1683
1684 if(MyConnect(source_p))
1685 {
1686 /* Local clients of various types */
1687 if(IsPerson(source_p))
1688 return exit_local_client(client_p, source_p, from, comment);
1689 else if(IsServer(source_p))
1690 return exit_local_server(client_p, source_p, from, comment);
1691 /* IsUnknown || IsConnecting || IsHandShake */
1692 else if(!IsReject(source_p))
1693 return exit_unknown_client(client_p, source_p, from, comment);
1694 }
1695 else
1696 {
1697 /* Remotes */
1698 if(IsPerson(source_p))
1699 return exit_remote_client(client_p, source_p, from, comment);
1700 else if(IsServer(source_p))
1701 return exit_remote_server(client_p, source_p, from, comment);
1702 }
1703
1704 return -1;
1705 }
1706
1707 /*
1708 * Count up local client memory
1709 */
1710
1711 /* XXX one common Client list now */
1712 void
1713 count_local_client_memory(size_t * count, size_t * local_client_memory_used)
1714 {
1715 size_t lusage;
1716 rb_bh_usage(lclient_heap, count, NULL, &lusage, NULL);
1717 *local_client_memory_used = lusage + (*count * (sizeof(void *) + sizeof(struct Client)));
1718 }
1719
1720 /*
1721 * Count up remote client memory
1722 */
1723 void
1724 count_remote_client_memory(size_t * count, size_t * remote_client_memory_used)
1725 {
1726 size_t lcount, rcount;
1727 rb_bh_usage(lclient_heap, &lcount, NULL, NULL, NULL);
1728 rb_bh_usage(client_heap, &rcount, NULL, NULL, NULL);
1729 *count = rcount - lcount;
1730 *remote_client_memory_used = *count * (sizeof(void *) + sizeof(struct Client));
1731 }
1732
1733
1734 /*
1735 * accept processing, this adds a form of "caller ID" to ircd
1736 *
1737 * If a client puts themselves into "caller ID only" mode,
1738 * only clients that match a client pointer they have put on
1739 * the accept list will be allowed to message them.
1740 *
1741 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1742 *
1743 * [target.allow_list] -> [ source1 ] -> [source2 ]
1744 *
1745 * i.e. a target will have a link list of source pointers it will allow
1746 * each source client then has a back pointer pointing back
1747 * to the client that has it on its accept list.
1748 * This allows for exit_one_client to remove these now bogus entries
1749 * from any client having an accept on them.
1750 */
1751 /*
1752 * del_all_accepts
1753 *
1754 * inputs - pointer to exiting client
1755 * output - NONE
1756 * side effects - Walk through given clients allow_list and on_allow_list
1757 * remove all references to this client
1758 */
1759 void
1760 del_all_accepts(struct Client *client_p)
1761 {
1762 rb_dlink_node *ptr;
1763 rb_dlink_node *next_ptr;
1764 struct Client *target_p;
1765
1766 if(MyClient(client_p) && client_p->localClient->allow_list.head)
1767 {
1768 /* clear this clients accept list, and remove them from
1769 * everyones on_accept_list
1770 */
1771 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head)
1772 {
1773 target_p = ptr->data;
1774 rb_dlinkFindDestroy(client_p, &target_p->on_allow_list);
1775 rb_dlinkDestroy(ptr, &client_p->localClient->allow_list);
1776 }
1777 }
1778
1779 /* remove this client from everyones accept list */
1780 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
1781 {
1782 target_p = ptr->data;
1783 rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
1784 rb_dlinkDestroy(ptr, &client_p->on_allow_list);
1785 }
1786 }
1787
1788 /*
1789 * show_ip() - asks if the true IP shoudl be shown when source is
1790 * askin for info about target
1791 *
1792 * Inputs - source_p who is asking
1793 * - target_p who do we want the info on
1794 * Output - returns 1 if clear IP can be showed, otherwise 0
1795 * Side Effects - none
1796 */
1797
1798 int
1799 show_ip(struct Client *source_p, struct Client *target_p)
1800 {
1801 if(IsAnyServer(target_p))
1802 {
1803 #ifndef HIDE_SERVERS_IPS
1804 if(source_p == NULL || IsOper(source_p))
1805 return 1;
1806 #endif
1807 return 0;
1808 }
1809 else if(IsIPSpoof(target_p))
1810 {
1811 /* source == NULL indicates message is being sent
1812 * to local opers.
1813 */
1814 if(!ConfigFileEntry.hide_spoof_ips &&
1815 (source_p == NULL || MyOper(source_p)))
1816 return 1;
1817 return 0;
1818 }
1819 else if(IsDynSpoof(target_p) && (source_p != NULL && !IsOper(source_p)))
1820 return 0;
1821 else
1822 return 1;
1823 }
1824
1825 int
1826 show_ip_conf(struct ConfItem *aconf, struct Client *source_p)
1827 {
1828 if(IsConfDoSpoofIp(aconf))
1829 {
1830 if(!ConfigFileEntry.hide_spoof_ips && MyOper(source_p))
1831 return 1;
1832
1833 return 0;
1834 }
1835 else
1836 return 1;
1837 }
1838
1839 /*
1840 * initUser
1841 *
1842 * inputs - none
1843 * outputs - none
1844 *
1845 * side effects - Creates a block heap for struct Users
1846 *
1847 */
1848 static rb_bh *user_heap;
1849 void
1850 initUser(void)
1851 {
1852 user_heap = rb_bh_create(sizeof(struct User), USER_HEAP_SIZE, "user_heap");
1853 if(!user_heap)
1854 rb_outofmemory();
1855 }
1856
1857 /*
1858 * make_user
1859 *
1860 * inputs - pointer to client struct
1861 * output - pointer to struct User
1862 * side effects - add's an User information block to a client
1863 * if it was not previously allocated.
1864 */
1865 struct User *
1866 make_user(struct Client *client_p)
1867 {
1868 struct User *user;
1869
1870 user = client_p->user;
1871 if(!user)
1872 {
1873 user = (struct User *) rb_bh_alloc(user_heap);
1874 user->refcnt = 1;
1875 client_p->user = user;
1876 }
1877 return user;
1878 }
1879
1880 /*
1881 * make_server
1882 *
1883 * inputs - pointer to client struct
1884 * output - pointer to server_t
1885 * side effects - add's an Server information block to a client
1886 * if it was not previously allocated.
1887 */
1888 server_t *
1889 make_server(struct Client *client_p)
1890 {
1891 server_t *serv = client_p->serv;
1892
1893 if(!serv)
1894 {
1895 serv = (server_t *) rb_malloc(sizeof(server_t));
1896 client_p->serv = serv;
1897 }
1898 return client_p->serv;
1899 }
1900
1901 /*
1902 * free_user
1903 *
1904 * inputs - pointer to user struct
1905 * - pointer to client struct
1906 * output - none
1907 * side effects - Decrease user reference count by one and release block,
1908 * if count reaches 0
1909 */
1910 void
1911 free_user(struct User *user, struct Client *client_p)
1912 {
1913 free_away(client_p);
1914
1915 if(--user->refcnt <= 0)
1916 {
1917 if(user->away)
1918 rb_free((char *) user->away);
1919 /*
1920 * sanity check
1921 */
1922 if(user->refcnt < 0 || user->invited.head || user->channel.head)
1923 {
1924 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1925 "* %#lx user (%s!%s@%s) %#lx %#lx %#lx %lu %d *",
1926 (unsigned long) client_p,
1927 client_p ? client_p->
1928 name : "<noname>",
1929 client_p->username,
1930 client_p->host,
1931 (unsigned long) user,
1932 (unsigned long) user->invited.head,
1933 (unsigned long) user->channel.head,
1934 rb_dlink_list_length(&user->channel),
1935 user->refcnt);
1936 s_assert(!user->refcnt);
1937 s_assert(!user->invited.head);
1938 s_assert(!user->channel.head);
1939 }
1940
1941 rb_bh_free(user_heap, user);
1942 }
1943 }
1944
1945 void
1946 allocate_away(struct Client *client_p)
1947 {
1948 if(client_p->user->away == NULL)
1949 client_p->user->away = rb_bh_alloc(away_heap);
1950 }
1951
1952
1953 void
1954 free_away(struct Client *client_p)
1955 {
1956 if(client_p->user != NULL && client_p->user->away != NULL) {
1957 rb_bh_free(away_heap, client_p->user->away);
1958 client_p->user->away = NULL;
1959 }
1960 }
1961
1962 void
1963 init_uid(void)
1964 {
1965 int i;
1966
1967 for(i = 0; i < 3; i++)
1968 current_uid[i] = me.id[i];
1969
1970 for(i = 3; i < 9; i++)
1971 current_uid[i] = 'A';
1972
1973 current_uid[9] = '\0';
1974 }
1975
1976
1977 char *
1978 generate_uid(void)
1979 {
1980 int i;
1981
1982 for(i = 8; i > 3; i--)
1983 {
1984 if(current_uid[i] == 'Z')
1985 {
1986 current_uid[i] = '0';
1987 return current_uid;
1988 }
1989 else if(current_uid[i] != '9')
1990 {
1991 current_uid[i]++;
1992 return current_uid;
1993 }
1994 else
1995 current_uid[i] = 'A';
1996 }
1997
1998 /* if this next if() triggers, we're fucked. */
1999 if(current_uid[3] == 'Z')
2000 {
2001 current_uid[i] = 'A';
2002 s_assert(0);
2003 }
2004 else
2005 current_uid[i]++;
2006
2007 return current_uid;
2008 }
2009
2010 /*
2011 * close_connection
2012 * Close the physical connection. This function must make
2013 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
2014 */
2015 void
2016 close_connection(struct Client *client_p)
2017 {
2018 s_assert(client_p != NULL);
2019 if(client_p == NULL)
2020 return;
2021
2022 s_assert(MyConnect(client_p));
2023 if(!MyConnect(client_p))
2024 return;
2025
2026 if(IsServer(client_p))
2027 {
2028 struct server_conf *server_p;
2029
2030 ServerStats.is_sv++;
2031 ServerStats.is_sbs += client_p->localClient->sendB;
2032 ServerStats.is_sbr += client_p->localClient->receiveB;
2033 ServerStats.is_sks += client_p->localClient->sendK;
2034 ServerStats.is_skr += client_p->localClient->receiveK;
2035 ServerStats.is_sti += rb_current_time() - client_p->localClient->firsttime;
2036 if(ServerStats.is_sbs > 2047)
2037 {
2038 ServerStats.is_sks += (ServerStats.is_sbs >> 10);
2039 ServerStats.is_sbs &= 0x3ff;
2040 }
2041 if(ServerStats.is_sbr > 2047)
2042 {
2043 ServerStats.is_skr += (ServerStats.is_sbr >> 10);
2044 ServerStats.is_sbr &= 0x3ff;
2045 }
2046
2047 /*
2048 * If the connection has been up for a long amount of time, schedule
2049 * a 'quick' reconnect, else reset the next-connect cycle.
2050 */
2051 if((server_p = find_server_conf(client_p->name)) != NULL)
2052 {
2053 /*
2054 * Reschedule a faster reconnect, if this was a automatically
2055 * connected configuration entry. (Note that if we have had
2056 * a rehash in between, the status has been changed to
2057 * CONF_ILLEGAL). But only do this if it was a "good" link.
2058 */
2059 server_p->hold = time(NULL);
2060 server_p->hold +=
2061 (server_p->hold - client_p->localClient->lasttime >
2062 HANGONGOODLINK) ? HANGONRETRYDELAY : ConFreq(server_p->class);
2063 }
2064
2065 }
2066 else if(IsClient(client_p))
2067 {
2068 ServerStats.is_cl++;
2069 ServerStats.is_cbs += client_p->localClient->sendB;
2070 ServerStats.is_cbr += client_p->localClient->receiveB;
2071 ServerStats.is_cks += client_p->localClient->sendK;
2072 ServerStats.is_ckr += client_p->localClient->receiveK;
2073 ServerStats.is_cti += rb_current_time() - client_p->localClient->firsttime;
2074 if(ServerStats.is_cbs > 2047)
2075 {
2076 ServerStats.is_cks += (ServerStats.is_cbs >> 10);
2077 ServerStats.is_cbs &= 0x3ff;
2078 }
2079 if(ServerStats.is_cbr > 2047)
2080 {
2081 ServerStats.is_ckr += (ServerStats.is_cbr >> 10);
2082 ServerStats.is_cbr &= 0x3ff;
2083 }
2084 }
2085 else
2086 ServerStats.is_ni++;
2087
2088 if(client_p->localClient->F)
2089 {
2090 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
2091 if(!IsIOError(client_p))
2092 send_queued(client_p);
2093
2094 rb_close(client_p->localClient->F);
2095 client_p->localClient->F = NULL;
2096 }
2097
2098 if(client_p->localClient->ctrlF)
2099 {
2100 rb_close(client_p->localClient->ctrlF);
2101 client_p->localClient->ctrlF = NULL;
2102 }
2103
2104 rb_linebuf_donebuf(&client_p->localClient->buf_sendq);
2105 rb_linebuf_donebuf(&client_p->localClient->buf_recvq);
2106 detach_conf(client_p);
2107
2108 /* XXX shouldnt really be done here. */
2109 detach_server_conf(client_p);
2110
2111 client_p->from = NULL; /* ...this should catch them! >:) --msa */
2112 ClearMyConnect(client_p);
2113 SetIOError(client_p);
2114 }
2115
2116
2117
2118 void
2119 error_exit_client(struct Client *client_p, int error)
2120 {
2121 /*
2122 * ...hmm, with non-blocking sockets we might get
2123 * here from quite valid reasons, although.. why
2124 * would select report "data available" when there
2125 * wasn't... so, this must be an error anyway... --msa
2126 * actually, EOF occurs when read() returns 0 and
2127 * in due course, select() returns that fd as ready
2128 * for reading even though it ends up being an EOF. -avalon
2129 */
2130 char errmsg[255];
2131 int current_error = rb_get_sockerr(client_p->localClient->F);
2132
2133 SetIOError(client_p);
2134
2135 if(IsServer(client_p) || IsHandshake(client_p))
2136 {
2137 if(error == 0)
2138 {
2139 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL,
2140 "Server %s closed the connection",
2141 get_server_name(client_p, SHOW_IP));
2142
2143 ilog(L_SERVER, "Server %s closed the connection",
2144 log_client_name(client_p, SHOW_IP));
2145 }
2146 else
2147 {
2148 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL,
2149 "Lost connection to %s: %s",
2150 client_p->name, strerror(current_error));
2151 ilog(L_SERVER, "Lost connection to %s: %s",
2152 log_client_name(client_p, SHOW_IP), strerror(current_error));
2153 }
2154 }
2155
2156 if(error == 0)
2157 strlcpy(errmsg, "Remote host closed the connection", sizeof(errmsg));
2158 else
2159 rb_snprintf(errmsg, sizeof(errmsg), "Read error: %s", strerror(current_error));
2160
2161 exit_client(client_p, client_p, &me, errmsg);
2162 }