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