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