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