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