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