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