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