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