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