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