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