]> jfr.im git - irc/rqf/shadowircd.git/blob - src/client.c
re-enable ziplinks + ssl
[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)))
463 {
464 if(aconf->status & CONF_EXEMPTDLINE)
465 continue;
466
467 sendto_realops_snomask(SNO_GENERAL, L_ALL,
468 "DLINE active for %s",
469 get_client_name(client_p, HIDE_IP));
470
471 notify_banned_client(client_p, aconf, D_LINED);
472 continue; /* and go examine next fd/client_p */
473 }
474
475 if(!IsPerson(client_p))
476 continue;
477
478 if((aconf = find_kline(client_p)) != NULL)
479 {
480 if(IsExemptKline(client_p))
481 {
482 sendto_realops_snomask(SNO_GENERAL, L_ALL,
483 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
484 get_client_name(client_p, HIDE_IP),
485 aconf->user, aconf->host);
486 continue;
487 }
488
489 sendto_realops_snomask(SNO_GENERAL, L_ALL,
490 "KLINE active for %s",
491 get_client_name(client_p, HIDE_IP));
492 notify_banned_client(client_p, aconf, K_LINED);
493 continue;
494 }
495 else if((aconf = find_xline(client_p->info, 1)) != NULL)
496 {
497 if(IsExemptKline(client_p))
498 {
499 sendto_realops_snomask(SNO_GENERAL, L_ALL,
500 "XLINE over-ruled for %s, client is kline_exempt [%s]",
501 get_client_name(client_p, HIDE_IP),
502 aconf->name);
503 continue;
504 }
505
506 sendto_realops_snomask(SNO_GENERAL, L_ALL, "XLINE active for %s",
507 get_client_name(client_p, HIDE_IP));
508
509 (void) exit_client(client_p, client_p, &me, "Bad user info");
510 continue;
511 }
512 }
513
514 /* also check the unknowns list for new dlines */
515 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
516 {
517 client_p = ptr->data;
518
519 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)))
520 {
521 if(aconf->status & CONF_EXEMPTDLINE)
522 continue;
523
524 notify_banned_client(client_p, aconf, D_LINED);
525 }
526 }
527
528 }
529
530 /* check_klines_event()
531 *
532 * inputs -
533 * outputs -
534 * side effects - check_klines() is called, kline_queued unset
535 */
536 void
537 check_klines_event(void *unused)
538 {
539 kline_queued = 0;
540 check_klines();
541 }
542
543 /* check_klines
544 *
545 * inputs -
546 * outputs -
547 * side effects - all clients will be checked for klines
548 */
549 void
550 check_klines(void)
551 {
552 struct Client *client_p;
553 struct ConfItem *aconf;
554 rb_dlink_node *ptr;
555 rb_dlink_node *next_ptr;
556
557 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
558 {
559 client_p = ptr->data;
560
561 if(IsMe(client_p) || !IsPerson(client_p))
562 continue;
563
564 if((aconf = find_kline(client_p)) != NULL)
565 {
566 if(IsExemptKline(client_p))
567 {
568 sendto_realops_snomask(SNO_GENERAL, L_ALL,
569 "KLINE over-ruled for %s, client is kline_exempt",
570 get_client_name(client_p, HIDE_IP));
571 continue;
572 }
573
574 sendto_realops_snomask(SNO_GENERAL, L_ALL,
575 "KLINE active for %s",
576 get_client_name(client_p, HIDE_IP));
577
578 notify_banned_client(client_p, aconf, K_LINED);
579 continue;
580 }
581 }
582 }
583
584 /* check_dlines()
585 *
586 * inputs -
587 * outputs -
588 * side effects - all clients will be checked for dlines
589 */
590 void
591 check_dlines(void)
592 {
593 struct Client *client_p;
594 struct ConfItem *aconf;
595 rb_dlink_node *ptr;
596 rb_dlink_node *next_ptr;
597
598 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
599 {
600 client_p = ptr->data;
601
602 if(IsMe(client_p))
603 continue;
604
605 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)) != NULL)
606 {
607 if(aconf->status & CONF_EXEMPTDLINE)
608 continue;
609
610 sendto_realops_snomask(SNO_GENERAL, L_ALL,
611 "DLINE active for %s",
612 get_client_name(client_p, HIDE_IP));
613
614 notify_banned_client(client_p, aconf, D_LINED);
615 continue;
616 }
617 }
618
619 /* dlines need to be checked against unknowns too */
620 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
621 {
622 client_p = ptr->data;
623
624 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)) != NULL)
625 {
626 if(aconf->status & CONF_EXEMPTDLINE)
627 continue;
628
629 notify_banned_client(client_p, aconf, D_LINED);
630 }
631 }
632 }
633
634 /* check_xlines
635 *
636 * inputs -
637 * outputs -
638 * side effects - all clients will be checked for xlines
639 */
640 void
641 check_xlines(void)
642 {
643 struct Client *client_p;
644 struct ConfItem *aconf;
645 rb_dlink_node *ptr;
646 rb_dlink_node *next_ptr;
647
648 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
649 {
650 client_p = ptr->data;
651
652 if(IsMe(client_p) || !IsPerson(client_p))
653 continue;
654
655 if((aconf = find_xline(client_p->info, 1)) != NULL)
656 {
657 if(IsExemptKline(client_p))
658 {
659 sendto_realops_snomask(SNO_GENERAL, L_ALL,
660 "XLINE over-ruled for %s, client is kline_exempt",
661 get_client_name(client_p, HIDE_IP));
662 continue;
663 }
664
665 sendto_realops_snomask(SNO_GENERAL, L_ALL, "XLINE active for %s",
666 get_client_name(client_p, HIDE_IP));
667
668 (void) exit_client(client_p, client_p, &me, "Bad user info");
669 continue;
670 }
671 }
672 }
673
674 /*
675 * update_client_exit_stats
676 *
677 * input - pointer to client
678 * output - NONE
679 * side effects -
680 */
681 static void
682 update_client_exit_stats(struct Client *client_p)
683 {
684 if(IsServer(client_p))
685 {
686 sendto_realops_snomask(SNO_EXTERNAL, L_ALL,
687 "Server %s split from %s",
688 client_p->name, client_p->servptr->name);
689 if(HasSentEob(client_p))
690 eob_count--;
691 }
692 else if(IsClient(client_p))
693 {
694 --Count.total;
695 if(IsOper(client_p))
696 --Count.oper;
697 if(IsInvisible(client_p))
698 --Count.invisi;
699 }
700
701 if(splitchecking && !splitmode)
702 check_splitmode(NULL);
703 }
704
705 /*
706 * release_client_state
707 *
708 * input - pointer to client to release
709 * output - NONE
710 * side effects -
711 */
712 static void
713 release_client_state(struct Client *client_p)
714 {
715 if(client_p->user != NULL)
716 {
717 free_user(client_p->user, client_p); /* try this here */
718 }
719 if(client_p->serv)
720 {
721 if(client_p->serv->user != NULL)
722 free_user(client_p->serv->user, client_p);
723 if(client_p->serv->fullcaps)
724 rb_free(client_p->serv->fullcaps);
725 rb_free(client_p->serv);
726 }
727 }
728
729 /*
730 * remove_client_from_list
731 * inputs - point to client to remove
732 * output - NONE
733 * side effects - taken the code from ExitOneClient() for this
734 * and placed it here. - avalon
735 */
736 static void
737 remove_client_from_list(struct Client *client_p)
738 {
739 s_assert(NULL != client_p);
740
741 if(client_p == NULL)
742 return;
743
744 /* A client made with make_client()
745 * is on the unknown_list until removed.
746 * If it =does= happen to exit before its removed from that list
747 * and its =not= on the global_client_list, it will core here.
748 * short circuit that case now -db
749 */
750 if(client_p->node.prev == NULL && client_p->node.next == NULL)
751 return;
752
753 rb_dlinkDelete(&client_p->node, &global_client_list);
754
755 update_client_exit_stats(client_p);
756 }
757
758
759 /*
760 * find_person - find person by (nick)name.
761 * inputs - pointer to name
762 * output - return client pointer
763 * side effects -
764 */
765 struct Client *
766 find_person(const char *name)
767 {
768 struct Client *c2ptr;
769
770 c2ptr = find_client(name);
771
772 if(c2ptr && IsPerson(c2ptr))
773 return (c2ptr);
774 return (NULL);
775 }
776
777 struct Client *
778 find_named_person(const char *name)
779 {
780 struct Client *c2ptr;
781
782 c2ptr = find_named_client(name);
783
784 if(c2ptr && IsPerson(c2ptr))
785 return (c2ptr);
786 return (NULL);
787 }
788
789
790 /*
791 * find_chasing - find the client structure for a nick name (user)
792 * using history mechanism if necessary. If the client is not found,
793 * an error message (NO SUCH NICK) is generated. If the client was found
794 * through the history, chasing will be 1 and otherwise 0.
795 */
796 struct Client *
797 find_chasing(struct Client *source_p, const char *user, int *chasing)
798 {
799 struct Client *who;
800
801 if(MyClient(source_p))
802 who = find_named_person(user);
803 else
804 who = find_person(user);
805
806 if(chasing)
807 *chasing = 0;
808
809 if(who || IsDigit(*user))
810 return who;
811
812 if(!(who = get_history(user, (long) KILLCHASETIMELIMIT)))
813 {
814 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
815 form_str(ERR_NOSUCHNICK), user);
816 return (NULL);
817 }
818 if(chasing)
819 *chasing = 1;
820 return who;
821 }
822
823 /*
824 * get_client_name - Return the name of the client
825 * for various tracking and
826 * admin purposes. The main purpose of this function is to
827 * return the "socket host" name of the client, if that
828 * differs from the advertised name (other than case).
829 * But, this can be used to any client structure.
830 *
831 * NOTE 1:
832 * Watch out the allocation of "nbuf", if either source_p->name
833 * or source_p->sockhost gets changed into pointers instead of
834 * directly allocated within the structure...
835 *
836 * NOTE 2:
837 * Function return either a pointer to the structure (source_p) or
838 * to internal buffer (nbuf). *NEVER* use the returned pointer
839 * to modify what it points!!!
840 */
841
842 const char *
843 get_client_name(struct Client *client, int showip)
844 {
845 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
846
847 s_assert(NULL != client);
848 if(client == NULL)
849 return NULL;
850
851 if(MyConnect(client))
852 {
853 if(!irccmp(client->name, client->host))
854 return client->name;
855
856 if(ConfigFileEntry.hide_spoof_ips &&
857 showip == SHOW_IP && IsIPSpoof(client))
858 showip = MASK_IP;
859 if(IsAnyServer(client))
860 showip = MASK_IP;
861
862 /* And finally, let's get the host information, ip or name */
863 switch (showip)
864 {
865 case SHOW_IP:
866 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
867 client->name, client->username,
868 client->sockhost);
869 break;
870 case MASK_IP:
871 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
872 client->name, client->username);
873 break;
874 default:
875 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
876 client->name, client->username, client->host);
877 }
878 return nbuf;
879 }
880
881 /* As pointed out by Adel Mezibra
882 * Neph|l|m@EFnet. Was missing a return here.
883 */
884 return client->name;
885 }
886
887 /* log_client_name()
888 *
889 * This version is the same as get_client_name, but doesnt contain the
890 * code that will hide IPs always. This should be used for logfiles.
891 */
892 const char *
893 log_client_name(struct Client *target_p, int showip)
894 {
895 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
896
897 if(target_p == NULL)
898 return NULL;
899
900 if(MyConnect(target_p))
901 {
902 if(irccmp(target_p->name, target_p->host) == 0)
903 return target_p->name;
904
905 switch (showip)
906 {
907 case SHOW_IP:
908 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name,
909 target_p->username, target_p->sockhost);
910 break;
911
912 case MASK_IP:
913 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
914 target_p->name, target_p->username);
915
916 default:
917 rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name,
918 target_p->username, target_p->host);
919 }
920
921 return nbuf;
922 }
923
924 return target_p->name;
925 }
926
927 /* is_remote_connect - Returns whether a server was /connect'ed by a remote
928 * oper (send notices netwide) */
929 int
930 is_remote_connect(struct Client *client_p)
931 {
932 struct Client *oper;
933
934 if (client_p->serv == NULL)
935 return FALSE;
936 oper = find_named_person(client_p->serv->by);
937 return oper != NULL && IsOper(oper) && !MyConnect(oper);
938 }
939
940 static void
941 free_exited_clients(void *unused)
942 {
943 rb_dlink_node *ptr, *next;
944 struct Client *target_p;
945
946 RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
947 {
948 target_p = ptr->data;
949
950 #ifdef DEBUG_EXITED_CLIENTS
951 {
952 struct abort_client *abt;
953 rb_dlink_node *aptr;
954 int found = 0;
955
956 RB_DLINK_FOREACH(aptr, abort_list.head)
957 {
958 abt = aptr->data;
959 if(abt->client == target_p)
960 {
961 s_assert(0);
962 sendto_realops_snomask(SNO_GENERAL, L_ALL,
963 "On abort_list: %s stat: %u flags: %u/%u handler: %c",
964 target_p->name, (unsigned int) target_p->status,
965 target_p->flags, target_p->flags2, target_p->handler);
966 sendto_realops_snomask(SNO_GENERAL, L_ALL,
967 "Please report this to the charybdis developers!");
968 found++;
969 }
970 }
971
972 if(found)
973 {
974 rb_dlinkDestroy(ptr, &dead_list);
975 continue;
976 }
977 }
978 #endif
979
980 if(ptr->data == NULL)
981 {
982 sendto_realops_snomask(SNO_GENERAL, L_ALL,
983 "Warning: null client on dead_list!");
984 rb_dlinkDestroy(ptr, &dead_list);
985 continue;
986 }
987 release_client_state(target_p);
988 free_client(target_p);
989 rb_dlinkDestroy(ptr, &dead_list);
990 }
991
992 #ifdef DEBUG_EXITED_CLIENTS
993 RB_DLINK_FOREACH_SAFE(ptr, next, dead_remote_list.head)
994 {
995 target_p = ptr->data;
996
997 if(ptr->data == NULL)
998 {
999 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1000 "Warning: null client on dead_list!");
1001 rb_dlinkDestroy(ptr, &dead_list);
1002 continue;
1003 }
1004 release_client_state(target_p);
1005 free_client(target_p);
1006 rb_dlinkDestroy(ptr, &dead_remote_list);
1007 }
1008 #endif
1009
1010 }
1011
1012 /*
1013 ** Recursively send QUITs and SQUITs for source_p and all its dependent clients
1014 ** and servers to those servers that need them. A server needs the client
1015 ** QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
1016 ** isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
1017 ** a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
1018 ** on that one -orabidoo
1019 */
1020 static void
1021 recurse_send_quits(struct Client *client_p, struct Client *source_p,
1022 struct Client *to, const char *comment1,
1023 const char *comment)
1024 {
1025 struct Client *target_p;
1026 rb_dlink_node *ptr, *ptr_next;
1027 /* If this server can handle quit storm (QS) removal
1028 * of dependents, just send the SQUIT
1029 */
1030
1031 if(IsCapable(to, CAP_QS))
1032 {
1033 sendto_one(to, "SQUIT %s :%s",
1034 get_id(source_p, to), comment);
1035 }
1036 else
1037 {
1038 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1039 {
1040 target_p = ptr->data;
1041 sendto_one(to, ":%s QUIT :%s", target_p->name, comment1);
1042 }
1043 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
1044 {
1045 target_p = ptr->data;
1046 recurse_send_quits(client_p, target_p, to, comment1, comment);
1047 }
1048 sendto_one(to, "SQUIT %s :%s", source_p->name, comment);
1049 }
1050 }
1051
1052 /*
1053 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1054 ** already been sent. we make sure to exit a server's dependent clients
1055 ** and servers before the server itself; exit_one_client takes care of
1056 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1057 */
1058 /*
1059 * added sanity test code.... source_p->serv might be NULL...
1060 */
1061 static void
1062 recurse_remove_clients(struct Client *source_p, const char *comment)
1063 {
1064 struct Client *target_p;
1065 rb_dlink_node *ptr, *ptr_next;
1066
1067 if(IsMe(source_p))
1068 return;
1069
1070 if(source_p->serv == NULL) /* oooops. uh this is actually a major bug */
1071 return;
1072
1073 /* this is very ugly, but it saves cpu :P */
1074 if(ConfigFileEntry.nick_delay > 0)
1075 {
1076 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1077 {
1078 target_p = ptr->data;
1079 target_p->flags |= FLAGS_KILLED;
1080 add_nd_entry(target_p->name);
1081
1082 if(!IsDead(target_p) && !IsClosing(target_p))
1083 exit_remote_client(NULL, target_p, &me, comment);
1084 }
1085 }
1086 else
1087 {
1088 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1089 {
1090 target_p = ptr->data;
1091 target_p->flags |= FLAGS_KILLED;
1092
1093 if(!IsDead(target_p) && !IsClosing(target_p))
1094 exit_remote_client(NULL, target_p, &me, comment);
1095 }
1096 }
1097
1098 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
1099 {
1100 target_p = ptr->data;
1101 recurse_remove_clients(target_p, comment);
1102 qs_server(NULL, target_p, &me, comment);
1103 }
1104 }
1105
1106 /*
1107 ** Remove *everything* that depends on source_p, from all lists, and sending
1108 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
1109 ** and its SQUITs have been sent except for the upstream one -orabidoo
1110 */
1111 static void
1112 remove_dependents(struct Client *client_p,
1113 struct Client *source_p,
1114 struct Client *from, const char *comment, const char *comment1)
1115 {
1116 struct Client *to;
1117 rb_dlink_node *ptr, *next;
1118
1119 RB_DLINK_FOREACH_SAFE(ptr, next, serv_list.head)
1120 {
1121 to = ptr->data;
1122
1123 if(IsMe(to) || to == source_p->from ||
1124 (to == client_p && IsCapable(to, CAP_QS)))
1125 continue;
1126
1127 recurse_send_quits(client_p, source_p, to, comment1, comment);
1128 }
1129
1130 recurse_remove_clients(source_p, comment1);
1131 }
1132
1133 void
1134 exit_aborted_clients(void *unused)
1135 {
1136 struct abort_client *abt;
1137 rb_dlink_node *ptr, *next;
1138 RB_DLINK_FOREACH_SAFE(ptr, next, abort_list.head)
1139 {
1140 abt = ptr->data;
1141
1142 #ifdef DEBUG_EXITED_CLIENTS
1143 {
1144 if(rb_dlinkFind(abt->client, &dead_list))
1145 {
1146 s_assert(0);
1147 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1148 "On dead_list: %s stat: %u flags: %u/%u handler: %c",
1149 abt->client->name, (unsigned int) abt->client->status,
1150 abt->client->flags, abt->client->flags2, abt->client->handler);
1151 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1152 "Please report this to the charybdis developers!");
1153 continue;
1154 }
1155 }
1156 #endif
1157
1158 s_assert(*((unsigned long*)abt->client) != 0xdeadbeef); /* This is lame but its a debug thing */
1159 rb_dlinkDelete(ptr, &abort_list);
1160
1161 if(IsAnyServer(abt->client))
1162 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1163 "Closing link to %s: %s",
1164 abt->client->name, abt->notice);
1165
1166 /* its no longer on abort list - we *must* remove
1167 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1168 */
1169 abt->client->flags &= ~FLAGS_CLOSING;
1170 exit_client(abt->client, abt->client, &me, abt->notice);
1171 rb_free(abt);
1172 }
1173 }
1174
1175
1176 /*
1177 * dead_link - Adds client to a list of clients that need an exit_client()
1178 *
1179 */
1180 void
1181 dead_link(struct Client *client_p)
1182 {
1183 struct abort_client *abt;
1184
1185 s_assert(!IsMe(client_p));
1186 if(IsDead(client_p) || IsClosing(client_p) || IsMe(client_p))
1187 return;
1188
1189 abt = (struct abort_client *) rb_malloc(sizeof(struct abort_client));
1190
1191 if(client_p->flags & FLAGS_SENDQEX)
1192 rb_strlcpy(abt->notice, "Max SendQ exceeded", sizeof(abt->notice));
1193 else
1194 rb_snprintf(abt->notice, sizeof(abt->notice), "Write error: %s", strerror(errno));
1195
1196 abt->client = client_p;
1197 SetIOError(client_p);
1198 SetDead(client_p);
1199 SetClosing(client_p);
1200 rb_dlinkAdd(abt, &abt->node, &abort_list);
1201 }
1202
1203
1204 /* This does the remove of the user from channels..local or remote */
1205 static inline void
1206 exit_generic_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1207 const char *comment)
1208 {
1209 rb_dlink_node *ptr, *next_ptr;
1210
1211 if(IsOper(source_p))
1212 rb_dlinkFindDestroy(source_p, &oper_list);
1213
1214 sendto_common_channels_local(source_p, ":%s!%s@%s QUIT :%s",
1215 source_p->name,
1216 source_p->username, source_p->host, comment);
1217
1218 remove_user_from_channels(source_p);
1219
1220 /* Should not be in any channels now */
1221 s_assert(source_p->user->channel.head == NULL);
1222
1223 /* Clean up invitefield */
1224 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->user->invited.head)
1225 {
1226 del_invite(ptr->data, source_p);
1227 }
1228
1229 /* Clean up allow lists */
1230 del_all_accepts(source_p);
1231
1232 add_history(source_p, 0);
1233 off_history(source_p);
1234
1235 monitor_signoff(source_p);
1236
1237 if(has_id(source_p))
1238 del_from_id_hash(source_p->id, source_p);
1239
1240 del_from_hostname_hash(source_p->orighost, source_p);
1241 del_from_client_hash(source_p->name, source_p);
1242 remove_client_from_list(source_p);
1243 }
1244
1245 /*
1246 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1247 */
1248
1249 static int
1250 exit_remote_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1251 const char *comment)
1252 {
1253 exit_generic_client(client_p, source_p, from, comment);
1254
1255 if(source_p->servptr && source_p->servptr->serv)
1256 {
1257 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users);
1258 }
1259
1260 if((source_p->flags & FLAGS_KILLED) == 0)
1261 {
1262 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1263 ":%s QUIT :%s", use_id(source_p), comment);
1264 }
1265
1266 SetDead(source_p);
1267 #ifdef DEBUG_EXITED_CLIENTS
1268 rb_dlinkAddAlloc(source_p, &dead_remote_list);
1269 #else
1270 rb_dlinkAddAlloc(source_p, &dead_list);
1271 #endif
1272 return(CLIENT_EXITED);
1273 }
1274
1275 /*
1276 * This assumes IsUnknown(source_p) == TRUE and MyConnect(source_p) == TRUE
1277 */
1278
1279 static int
1280 exit_unknown_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1281 const char *comment)
1282 {
1283 delete_auth_queries(source_p);
1284 if (source_p->localClient->dnsquery)
1285 {
1286 delete_resolver_queries(source_p->localClient->dnsquery);
1287 rb_free(source_p->localClient->dnsquery);
1288 }
1289 rb_dlinkDelete(&source_p->localClient->tnode, &unknown_list);
1290
1291 if(!IsIOError(source_p))
1292 sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
1293 source_p->user != NULL ? source_p->host : "127.0.0.1",
1294 comment);
1295
1296 close_connection(source_p);
1297
1298 if(has_id(source_p))
1299 del_from_id_hash(source_p->id, source_p);
1300
1301 del_from_hostname_hash(source_p->host, source_p);
1302 del_from_client_hash(source_p->name, source_p);
1303 remove_client_from_list(source_p);
1304 SetDead(source_p);
1305 rb_dlinkAddAlloc(source_p, &dead_list);
1306
1307 /* Note that we don't need to add unknowns to the dead_list */
1308 return(CLIENT_EXITED);
1309 }
1310
1311 static int
1312 exit_remote_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1313 const char *comment)
1314 {
1315 static char comment1[(HOSTLEN*2)+2];
1316 static char newcomment[BUFSIZE];
1317 struct Client *target_p;
1318
1319 if(ConfigServerHide.flatten_links)
1320 strcpy(comment1, "*.net *.split");
1321 else
1322 {
1323 strcpy(comment1, source_p->servptr->name);
1324 strcat(comment1, " ");
1325 strcat(comment1, source_p->name);
1326 }
1327 if (IsPerson(from))
1328 rb_snprintf(newcomment, sizeof(newcomment), "by %s: %s",
1329 from->name, comment);
1330
1331 if(source_p->serv != NULL)
1332 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1333
1334 if(source_p->servptr && source_p->servptr->serv)
1335 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1336 else
1337 s_assert(0);
1338
1339 rb_dlinkFindDestroy(source_p, &global_serv_list);
1340 target_p = source_p->from;
1341
1342 if(target_p != NULL && IsServer(target_p) && target_p != client_p &&
1343 !IsMe(target_p) && (source_p->flags & FLAGS_KILLED) == 0)
1344 {
1345 sendto_one(target_p, ":%s SQUIT %s :%s",
1346 get_id(from, target_p), get_id(source_p, target_p),
1347 comment);
1348 }
1349
1350 if(has_id(source_p))
1351 del_from_id_hash(source_p->id, source_p);
1352
1353 del_from_client_hash(source_p->name, source_p);
1354 remove_client_from_list(source_p);
1355 scache_split(source_p->serv->nameinfo);
1356
1357 SetDead(source_p);
1358 #ifdef DEBUG_EXITED_CLIENTS
1359 rb_dlinkAddAlloc(source_p, &dead_remote_list);
1360 #else
1361 rb_dlinkAddAlloc(source_p, &dead_list);
1362 #endif
1363 return 0;
1364 }
1365
1366 static int
1367 qs_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1368 const char *comment)
1369 {
1370 if(source_p->servptr && source_p->servptr->serv)
1371 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1372 else
1373 s_assert(0);
1374
1375 rb_dlinkFindDestroy(source_p, &global_serv_list);
1376
1377 if(has_id(source_p))
1378 del_from_id_hash(source_p->id, source_p);
1379
1380 del_from_client_hash(source_p->name, source_p);
1381 remove_client_from_list(source_p);
1382 scache_split(source_p->serv->nameinfo);
1383
1384 SetDead(source_p);
1385 rb_dlinkAddAlloc(source_p, &dead_list);
1386 return 0;
1387 }
1388
1389 static int
1390 exit_local_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1391 const char *comment)
1392 {
1393 static char comment1[(HOSTLEN*2)+2];
1394 static char newcomment[BUFSIZE];
1395 unsigned int sendk, recvk;
1396
1397 rb_dlinkDelete(&source_p->localClient->tnode, &serv_list);
1398 rb_dlinkFindDestroy(source_p, &global_serv_list);
1399
1400 unset_chcap_usage_counts(source_p);
1401 sendk = source_p->localClient->sendK;
1402 recvk = source_p->localClient->receiveK;
1403
1404 /* Always show source here, so the server notices show
1405 * which side initiated the split -- jilles
1406 */
1407 rb_snprintf(newcomment, sizeof(newcomment), "by %s: %s",
1408 from == source_p ? me.name : from->name, comment);
1409 if (!IsIOError(source_p))
1410 sendto_one(source_p, "SQUIT %s :%s", use_id(source_p),
1411 newcomment);
1412 if(client_p != NULL && source_p != client_p && !IsIOError(source_p))
1413 {
1414 sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1415 source_p->name, comment);
1416 }
1417
1418 if(source_p->servptr && source_p->servptr->serv)
1419 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1420 else
1421 s_assert(0);
1422
1423
1424 close_connection(source_p);
1425
1426 if(ConfigServerHide.flatten_links)
1427 strcpy(comment1, "*.net *.split");
1428 else
1429 {
1430 strcpy(comment1, source_p->servptr->name);
1431 strcat(comment1, " ");
1432 strcat(comment1, source_p->name);
1433 }
1434
1435 if(source_p->serv != NULL)
1436 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1437
1438 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s was connected"
1439 " for %ld seconds. %d/%d sendK/recvK.",
1440 source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
1441
1442 ilog(L_SERVER, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1443 source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
1444
1445 if(has_id(source_p))
1446 del_from_id_hash(source_p->id, source_p);
1447
1448 del_from_client_hash(source_p->name, source_p);
1449 remove_client_from_list(source_p);
1450 scache_split(source_p->serv->nameinfo);
1451
1452 SetDead(source_p);
1453 rb_dlinkAddAlloc(source_p, &dead_list);
1454 return 0;
1455 }
1456
1457
1458 /*
1459 * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE
1460 */
1461
1462 static int
1463 exit_local_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1464 const char *comment)
1465 {
1466 unsigned long on_for;
1467 char tbuf[26];
1468
1469 exit_generic_client(client_p, source_p, from, comment);
1470 clear_monitor(source_p);
1471
1472 s_assert(IsPerson(source_p));
1473 rb_dlinkDelete(&source_p->localClient->tnode, &lclient_list);
1474 rb_dlinkDelete(&source_p->lnode, &me.serv->users);
1475
1476 if(IsOper(source_p))
1477 rb_dlinkFindDestroy(source_p, &local_oper_list);
1478
1479 sendto_realops_snomask(SNO_CCONN, L_ALL,
1480 "Client exiting: %s (%s@%s) [%s] [%s]",
1481 source_p->name,
1482 source_p->username, source_p->host, comment,
1483 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255");
1484
1485 sendto_realops_snomask(SNO_CCONNEXT, L_ALL,
1486 "CLIEXIT %s %s %s %s 0 %s",
1487 source_p->name, source_p->username, source_p->host,
1488 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255",
1489 comment);
1490
1491 on_for = rb_current_time() - source_p->localClient->firsttime;
1492
1493 ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %s %d/%d",
1494 rb_ctime(rb_current_time(), tbuf, sizeof(tbuf)), on_for / 3600,
1495 (on_for % 3600) / 60, on_for % 60,
1496 source_p->name, source_p->username, source_p->host,
1497 source_p->sockhost,
1498 source_p->localClient->sendK, source_p->localClient->receiveK);
1499
1500 sendto_one(source_p, "ERROR :Closing Link: %s (%s)", source_p->host, comment);
1501 close_connection(source_p);
1502
1503 if((source_p->flags & FLAGS_KILLED) == 0)
1504 {
1505 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1506 ":%s QUIT :%s", use_id(source_p), comment);
1507 }
1508
1509 SetDead(source_p);
1510 rb_dlinkAddAlloc(source_p, &dead_list);
1511 return(CLIENT_EXITED);
1512 }
1513
1514
1515 /*
1516 ** exit_client - This is old "m_bye". Name changed, because this is not a
1517 ** protocol function, but a general server utility function.
1518 **
1519 ** This function exits a client of *any* type (user, server, etc)
1520 ** from this server. Also, this generates all necessary prototol
1521 ** messages that this exit may cause.
1522 **
1523 ** 1) If the client is a local client, then this implicitly
1524 ** exits all other clients depending on this connection (e.g.
1525 ** remote clients having 'from'-field that points to this.
1526 **
1527 ** 2) If the client is a remote client, then only this is exited.
1528 **
1529 ** For convenience, this function returns a suitable value for
1530 ** m_function return value:
1531 **
1532 ** CLIENT_EXITED if (client_p == source_p)
1533 ** 0 if (client_p != source_p)
1534 */
1535 int
1536 exit_client(struct Client *client_p, /* The local client originating the
1537 * exit or NULL, if this exit is
1538 * generated by this server for
1539 * internal reasons.
1540 * This will not get any of the
1541 * generated messages. */
1542 struct Client *source_p, /* Client exiting */
1543 struct Client *from, /* Client firing off this Exit,
1544 * never NULL! */
1545 const char *comment /* Reason for the exit */
1546 )
1547 {
1548 hook_data_client_exit hdata;
1549 if(IsClosing(source_p))
1550 return -1;
1551
1552 /* note, this HAS to be here, when we exit a client we attempt to
1553 * send them data, if this generates a write error we must *not* add
1554 * them to the abort list --fl
1555 */
1556 SetClosing(source_p);
1557
1558 hdata.local_link = client_p;
1559 hdata.target = source_p;
1560 hdata.from = from;
1561 hdata.comment = comment;
1562 call_hook(h_client_exit, &hdata);
1563
1564 if(MyConnect(source_p))
1565 {
1566 /* Local clients of various types */
1567 if(IsPerson(source_p))
1568 return exit_local_client(client_p, source_p, from, comment);
1569 else if(IsServer(source_p))
1570 return exit_local_server(client_p, source_p, from, comment);
1571 /* IsUnknown || IsConnecting || IsHandShake */
1572 else if(!IsReject(source_p))
1573 return exit_unknown_client(client_p, source_p, from, comment);
1574 }
1575 else
1576 {
1577 /* Remotes */
1578 if(IsPerson(source_p))
1579 return exit_remote_client(client_p, source_p, from, comment);
1580 else if(IsServer(source_p))
1581 return exit_remote_server(client_p, source_p, from, comment);
1582 }
1583
1584 return -1;
1585 }
1586
1587 /*
1588 * Count up local client memory
1589 */
1590
1591 /* XXX one common Client list now */
1592 void
1593 count_local_client_memory(size_t * count, size_t * local_client_memory_used)
1594 {
1595 size_t lusage;
1596 rb_bh_usage(lclient_heap, count, NULL, &lusage, NULL);
1597 *local_client_memory_used = lusage + (*count * (sizeof(void *) + sizeof(struct Client)));
1598 }
1599
1600 /*
1601 * Count up remote client memory
1602 */
1603 void
1604 count_remote_client_memory(size_t * count, size_t * remote_client_memory_used)
1605 {
1606 size_t lcount, rcount;
1607 rb_bh_usage(lclient_heap, &lcount, NULL, NULL, NULL);
1608 rb_bh_usage(client_heap, &rcount, NULL, NULL, NULL);
1609 *count = rcount - lcount;
1610 *remote_client_memory_used = *count * (sizeof(void *) + sizeof(struct Client));
1611 }
1612
1613
1614 /*
1615 * accept processing, this adds a form of "caller ID" to ircd
1616 *
1617 * If a client puts themselves into "caller ID only" mode,
1618 * only clients that match a client pointer they have put on
1619 * the accept list will be allowed to message them.
1620 *
1621 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1622 *
1623 * [target.allow_list] -> [ source1 ] -> [source2 ]
1624 *
1625 * i.e. a target will have a link list of source pointers it will allow
1626 * each source client then has a back pointer pointing back
1627 * to the client that has it on its accept list.
1628 * This allows for exit_one_client to remove these now bogus entries
1629 * from any client having an accept on them.
1630 */
1631 /*
1632 * del_all_accepts
1633 *
1634 * inputs - pointer to exiting client
1635 * output - NONE
1636 * side effects - Walk through given clients allow_list and on_allow_list
1637 * remove all references to this client
1638 */
1639 void
1640 del_all_accepts(struct Client *client_p)
1641 {
1642 rb_dlink_node *ptr;
1643 rb_dlink_node *next_ptr;
1644 struct Client *target_p;
1645
1646 if(MyClient(client_p) && client_p->localClient->allow_list.head)
1647 {
1648 /* clear this clients accept list, and remove them from
1649 * everyones on_accept_list
1650 */
1651 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head)
1652 {
1653 target_p = ptr->data;
1654 rb_dlinkFindDestroy(client_p, &target_p->on_allow_list);
1655 rb_dlinkDestroy(ptr, &client_p->localClient->allow_list);
1656 }
1657 }
1658
1659 /* remove this client from everyones accept list */
1660 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
1661 {
1662 target_p = ptr->data;
1663 rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
1664 rb_dlinkDestroy(ptr, &client_p->on_allow_list);
1665 }
1666 }
1667
1668 /*
1669 * show_ip() - asks if the true IP shoudl be shown when source is
1670 * askin for info about target
1671 *
1672 * Inputs - source_p who is asking
1673 * - target_p who do we want the info on
1674 * Output - returns 1 if clear IP can be showed, otherwise 0
1675 * Side Effects - none
1676 */
1677
1678 int
1679 show_ip(struct Client *source_p, struct Client *target_p)
1680 {
1681 if(IsAnyServer(target_p))
1682 {
1683 return 0;
1684 }
1685 else if(IsIPSpoof(target_p))
1686 {
1687 /* source == NULL indicates message is being sent
1688 * to local opers.
1689 */
1690 if(!ConfigFileEntry.hide_spoof_ips &&
1691 (source_p == NULL || MyOper(source_p)))
1692 return 1;
1693 return 0;
1694 }
1695 else if(IsDynSpoof(target_p) && (source_p != NULL && !IsOper(source_p)))
1696 return 0;
1697 else
1698 return 1;
1699 }
1700
1701 int
1702 show_ip_conf(struct ConfItem *aconf, struct Client *source_p)
1703 {
1704 if(IsConfDoSpoofIp(aconf))
1705 {
1706 if(!ConfigFileEntry.hide_spoof_ips && MyOper(source_p))
1707 return 1;
1708
1709 return 0;
1710 }
1711 else
1712 return 1;
1713 }
1714
1715 /*
1716 * make_user
1717 *
1718 * inputs - pointer to client struct
1719 * output - pointer to struct User
1720 * side effects - add's an User information block to a client
1721 * if it was not previously allocated.
1722 */
1723 struct User *
1724 make_user(struct Client *client_p)
1725 {
1726 struct User *user;
1727
1728 user = client_p->user;
1729 if(!user)
1730 {
1731 user = (struct User *) rb_bh_alloc(user_heap);
1732 user->refcnt = 1;
1733 client_p->user = user;
1734 }
1735 return user;
1736 }
1737
1738 /*
1739 * make_server
1740 *
1741 * inputs - pointer to client struct
1742 * output - pointer to server_t
1743 * side effects - add's an Server information block to a client
1744 * if it was not previously allocated.
1745 */
1746 server_t *
1747 make_server(struct Client *client_p)
1748 {
1749 server_t *serv = client_p->serv;
1750
1751 if(!serv)
1752 {
1753 serv = (server_t *) rb_malloc(sizeof(server_t));
1754 client_p->serv = serv;
1755 }
1756 return client_p->serv;
1757 }
1758
1759 /*
1760 * free_user
1761 *
1762 * inputs - pointer to user struct
1763 * - pointer to client struct
1764 * output - none
1765 * side effects - Decrease user reference count by one and release block,
1766 * if count reaches 0
1767 */
1768 void
1769 free_user(struct User *user, struct Client *client_p)
1770 {
1771 free_away(client_p);
1772
1773 if(--user->refcnt <= 0)
1774 {
1775 if(user->away)
1776 rb_free((char *) user->away);
1777 /*
1778 * sanity check
1779 */
1780 if(user->refcnt < 0 || user->invited.head || user->channel.head)
1781 {
1782 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1783 "* %#lx user (%s!%s@%s) %#lx %#lx %#lx %lu %d *",
1784 (unsigned long) client_p,
1785 client_p ? client_p->
1786 name : "<noname>",
1787 client_p->username,
1788 client_p->host,
1789 (unsigned long) user,
1790 (unsigned long) user->invited.head,
1791 (unsigned long) user->channel.head,
1792 rb_dlink_list_length(&user->channel),
1793 user->refcnt);
1794 s_assert(!user->refcnt);
1795 s_assert(!user->invited.head);
1796 s_assert(!user->channel.head);
1797 }
1798
1799 rb_bh_free(user_heap, user);
1800 }
1801 }
1802
1803 void
1804 allocate_away(struct Client *client_p)
1805 {
1806 if(client_p->user->away == NULL)
1807 client_p->user->away = rb_bh_alloc(away_heap);
1808 }
1809
1810
1811 void
1812 free_away(struct Client *client_p)
1813 {
1814 if(client_p->user != NULL && client_p->user->away != NULL) {
1815 rb_bh_free(away_heap, client_p->user->away);
1816 client_p->user->away = NULL;
1817 }
1818 }
1819
1820 void
1821 init_uid(void)
1822 {
1823 int i;
1824
1825 for(i = 0; i < 3; i++)
1826 current_uid[i] = me.id[i];
1827
1828 for(i = 3; i < 9; i++)
1829 current_uid[i] = 'A';
1830
1831 current_uid[9] = '\0';
1832 }
1833
1834
1835 char *
1836 generate_uid(void)
1837 {
1838 int i;
1839
1840 for(i = 8; i > 3; i--)
1841 {
1842 if(current_uid[i] == 'Z')
1843 {
1844 current_uid[i] = '0';
1845 return current_uid;
1846 }
1847 else if(current_uid[i] != '9')
1848 {
1849 current_uid[i]++;
1850 return current_uid;
1851 }
1852 else
1853 current_uid[i] = 'A';
1854 }
1855
1856 /* if this next if() triggers, we're fucked. */
1857 if(current_uid[3] == 'Z')
1858 {
1859 current_uid[i] = 'A';
1860 s_assert(0);
1861 }
1862 else
1863 current_uid[i]++;
1864
1865 return current_uid;
1866 }
1867
1868 /*
1869 * close_connection
1870 * Close the physical connection. This function must make
1871 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
1872 */
1873 void
1874 close_connection(struct Client *client_p)
1875 {
1876 s_assert(client_p != NULL);
1877 if(client_p == NULL)
1878 return;
1879
1880 s_assert(MyConnect(client_p));
1881 if(!MyConnect(client_p))
1882 return;
1883
1884 if(IsServer(client_p))
1885 {
1886 struct server_conf *server_p;
1887
1888 ServerStats.is_sv++;
1889 ServerStats.is_sbs += client_p->localClient->sendB;
1890 ServerStats.is_sbr += client_p->localClient->receiveB;
1891 ServerStats.is_sti += (unsigned long long)(rb_current_time() - client_p->localClient->firsttime);
1892
1893 /*
1894 * If the connection has been up for a long amount of time, schedule
1895 * a 'quick' reconnect, else reset the next-connect cycle.
1896 */
1897 if((server_p = find_server_conf(client_p->name)) != NULL)
1898 {
1899 /*
1900 * Reschedule a faster reconnect, if this was a automatically
1901 * connected configuration entry. (Note that if we have had
1902 * a rehash in between, the status has been changed to
1903 * CONF_ILLEGAL). But only do this if it was a "good" link.
1904 */
1905 server_p->hold = time(NULL);
1906 server_p->hold +=
1907 (server_p->hold - client_p->localClient->lasttime >
1908 HANGONGOODLINK) ? HANGONRETRYDELAY : ConFreq(server_p->class);
1909 }
1910
1911 }
1912 else if(IsClient(client_p))
1913 {
1914 ServerStats.is_cl++;
1915 ServerStats.is_cbs += client_p->localClient->sendB;
1916 ServerStats.is_cbr += client_p->localClient->receiveB;
1917 ServerStats.is_cti += (unsigned long long)(rb_current_time() - client_p->localClient->firsttime);
1918 }
1919 else
1920 ServerStats.is_ni++;
1921
1922 if(client_p->localClient->F != NULL)
1923 {
1924 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
1925 if(!IsIOError(client_p))
1926 send_queued(client_p);
1927
1928 del_from_cli_fd_hash(client_p);
1929 rb_close(client_p->localClient->F);
1930 client_p->localClient->F = NULL;
1931 }
1932
1933 rb_linebuf_donebuf(&client_p->localClient->buf_sendq);
1934 rb_linebuf_donebuf(&client_p->localClient->buf_recvq);
1935 detach_conf(client_p);
1936
1937 /* XXX shouldnt really be done here. */
1938 detach_server_conf(client_p);
1939
1940 client_p->from = NULL; /* ...this should catch them! >:) --msa */
1941 ClearMyConnect(client_p);
1942 SetIOError(client_p);
1943 }
1944
1945
1946
1947 void
1948 error_exit_client(struct Client *client_p, int error)
1949 {
1950 /*
1951 * ...hmm, with non-blocking sockets we might get
1952 * here from quite valid reasons, although.. why
1953 * would select report "data available" when there
1954 * wasn't... so, this must be an error anyway... --msa
1955 * actually, EOF occurs when read() returns 0 and
1956 * in due course, select() returns that fd as ready
1957 * for reading even though it ends up being an EOF. -avalon
1958 */
1959 char errmsg[255];
1960 int current_error = rb_get_sockerr(client_p->localClient->F);
1961
1962 SetIOError(client_p);
1963
1964 if(IsServer(client_p) || IsHandshake(client_p))
1965 {
1966 if(error == 0)
1967 {
1968 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL,
1969 "Server %s closed the connection",
1970 client_p->name);
1971
1972 ilog(L_SERVER, "Server %s closed the connection",
1973 log_client_name(client_p, SHOW_IP));
1974 }
1975 else
1976 {
1977 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL,
1978 "Lost connection to %s: %s",
1979 client_p->name, strerror(current_error));
1980 ilog(L_SERVER, "Lost connection to %s: %s",
1981 log_client_name(client_p, SHOW_IP), strerror(current_error));
1982 }
1983 }
1984
1985 if(error == 0)
1986 rb_strlcpy(errmsg, "Remote host closed the connection", sizeof(errmsg));
1987 else
1988 rb_snprintf(errmsg, sizeof(errmsg), "Read error: %s", strerror(current_error));
1989
1990 exit_client(client_p, client_p, &me, errmsg);
1991 }