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