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