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