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