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