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