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