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