]> jfr.im git - irc/freenode/solanum.git/blame - ircd/client.c
make more snotes L_NETWIDE
[irc/freenode/solanum.git] / ircd / client.c
CommitLineData
212380e3 1/*
a6f63a82 2 * Solanum: a slightly advanced ircd
212380e3
WP
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
WP
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
WP
24 */
25#include "stdinc.h"
9b8e9eb3 26#include "defaults.h"
212380e3 27
212380e3
WP
28#include "client.h"
29#include "class.h"
212380e3 30#include "hash.h"
4562c604 31#include "match.h"
212380e3 32#include "ircd.h"
212380e3
WP
33#include "numeric.h"
34#include "packet.h"
64fae260 35#include "authproc.h"
212380e3
WP
36#include "s_conf.h"
37#include "s_newconf.h"
4016731b 38#include "logger.h"
212380e3
WP
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
WP
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
WP
56
57#define DEBUG_EXITED_CLIENTS
58
330fc5c1
WP
59static void check_pings_list(rb_dlink_list * list);
60static void check_unknowns_list(rb_dlink_list * list);
212380e3
WP
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
WP
86#endif
87
88struct abort_client
89{
330fc5c1 90 rb_dlink_node node;
212380e3
WP
91 struct Client *client;
92 char notice[REASONLEN];
93};
94
330fc5c1 95static rb_dlink_list abort_list;
212380e3
WP
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
WP
124}
125
de7cf7e0
WP
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
WP
137 s_assert(MyConnect(client_p));
138 if (!MyConnect(client_p))
de7cf7e0
WP
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
WP
197 RB_DLINK_FOREACH_SAFE(ptr, ptr2, client_p->localClient->connids.head)
198 connid_put(RB_POINTER_TO_UINT(ptr->data));
199}
212380e3
WP
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
WP
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
WP
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
WP
232
233 /* as good a place as any... */
330fc5c1 234 rb_dlinkAdd(client_p, &client_p->localClient->tnode, &unknown_list);
212380e3
WP
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
WP
245
246 return client_p;
247}
248
249void
250free_pre_client(struct Client *client_p)
251{
212380e3
WP
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
WP
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
WP
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
WP
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
WP
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
WP
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
WP
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
WP
319 client_p->localClient = NULL;
320}
321
50b1e598 322static void
212380e3
WP
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
WP
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
WP
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
WP
373 */
374static void
330fc5c1 375check_pings_list(rb_dlink_list * list)
212380e3
WP
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
WP
383 {
384 client_p = ptr->data;
385
212380e3
WP
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
WP
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
WP
398 && (client_p->flags & FLAGS_PINGSENT)))
399 {
e2a98043 400 if(IsServer(client_p))
212380e3 401 {
a9227555 402 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
212380e3 403 "No response from %s, closing link",
b3ebc7ab 404 client_p->name);
212380e3
WP
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
WP
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
WP
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
WP
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
WP
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 {
a9227555 469 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
35cf4c79 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
WP
478 }
479}
480
10df26d0 481void
212380e3
WP
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
WP
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
WP
494 }
495 else
496 {
405ae5ce 497 reason = aconf->status == D_LINED ? d_lined : k_lined;
212380e3
WP
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
WP
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
WP
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
WP
524}
525
212380e3
WP
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
WP
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 {
a9227555 551 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
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
WP
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:
40e76ac7
EK
605 case HM_IPV6:
606 if (IsConfDoSpoofIp(client_p->localClient->att_conf) &&
607 IsConfKlineSpoof(client_p->localClient->att_conf))
608 continue;
609 if (client_p->localClient->ip.ss_family == AF_INET6 && sockaddr.ss_family == AF_INET &&
485d245e
EK
610 rb_ipv4_from_ipv6((struct sockaddr_in6 *)&client_p->localClient->ip, &ip4)
611 && comp_with_mask_sock((struct sockaddr *)&ip4, (struct sockaddr *)&sockaddr, bits))
612 matched = 1;
40e76ac7 613 else if (client_p->localClient->ip.ss_family == sockaddr.ss_family &&
485d245e
EK
614 comp_with_mask_sock((struct sockaddr *)&client_p->localClient->ip,
615 (struct sockaddr *)&sockaddr, bits))
f53d2f45 616 matched = 1;
9274c0f3 617 break;
f53d2f45
EK
618 case HM_HOST:
619 if (match(kline->host, client_p->orighost))
620 matched = 1;
40e76ac7
EK
621 if (IsConfDoSpoofIp(client_p->localClient->att_conf) &&
622 IsConfKlineSpoof(client_p->localClient->att_conf))
623 continue;
9274c0f3
EK
624 if (match(kline->host, client_p->sockhost))
625 matched = 1;
626 break;
10df26d0
EK
627 }
628
f53d2f45
EK
629 if (!matched)
630 continue;
631
10df26d0
EK
632 if(IsExemptKline(client_p))
633 {
634 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
635 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
636 get_client_name(client_p, HIDE_IP),
637 kline->user, kline->host);
638 continue;
639 }
640
641 sendto_realops_snomask(SNO_GENERAL, L_ALL,
642 "KLINE active for %s",
643 get_client_name(client_p, HIDE_IP));
644
645 notify_banned_client(client_p, kline, K_LINED);
646 }
647}
648
649
212380e3
WP
650/* check_dlines()
651 *
652 * inputs -
653 * outputs -
654 * side effects - all clients will be checked for dlines
655 */
656void
657check_dlines(void)
658{
659 struct Client *client_p;
660 struct ConfItem *aconf;
330fc5c1 661 rb_dlink_node *ptr;
637c4932 662 rb_dlink_node *next_ptr;
212380e3 663
637c4932 664 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
212380e3
WP
665 {
666 client_p = ptr->data;
667
668 if(IsMe(client_p))
669 continue;
670
e867208d 671 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, GET_SS_FAMILY(&client_p->localClient->ip))) != NULL)
212380e3
WP
672 {
673 if(aconf->status & CONF_EXEMPTDLINE)
674 continue;
675
676 sendto_realops_snomask(SNO_GENERAL, L_ALL,
677 "DLINE active for %s",
678 get_client_name(client_p, HIDE_IP));
679
680 notify_banned_client(client_p, aconf, D_LINED);
681 continue;
682 }
683 }
684
685 /* dlines need to be checked against unknowns too */
637c4932 686 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
212380e3
WP
687 {
688 client_p = ptr->data;
689
e867208d 690 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, GET_SS_FAMILY(&client_p->localClient->ip))) != NULL)
212380e3
WP
691 {
692 if(aconf->status & CONF_EXEMPTDLINE)
693 continue;
694
695 notify_banned_client(client_p, aconf, D_LINED);
696 }
697 }
698}
699
700/* check_xlines
701 *
702 * inputs -
703 * outputs -
704 * side effects - all clients will be checked for xlines
705 */
706void
707check_xlines(void)
708{
709 struct Client *client_p;
710 struct ConfItem *aconf;
330fc5c1 711 rb_dlink_node *ptr;
637c4932 712 rb_dlink_node *next_ptr;
212380e3 713
637c4932 714 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
212380e3
WP
715 {
716 client_p = ptr->data;
717
718 if(IsMe(client_p) || !IsPerson(client_p))
719 continue;
720
721 if((aconf = find_xline(client_p->info, 1)) != NULL)
722 {
723 if(IsExemptKline(client_p))
724 {
a9227555 725 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
408024fa
JT
726 "XLINE over-ruled for %s, client is kline_exempt [%s]",
727 get_client_name(client_p, HIDE_IP),
70ea02eb 728 aconf->host);
212380e3
WP
729 continue;
730 }
731
732 sendto_realops_snomask(SNO_GENERAL, L_ALL, "XLINE active for %s",
733 get_client_name(client_p, HIDE_IP));
734
735 (void) exit_client(client_p, client_p, &me, "Bad user info");
736 continue;
737 }
738 }
739}
740
330692a1
KB
741/* resv_nick_fnc
742 *
743 * inputs - resv, reason, time
744 * outputs - NONE
745 * side effects - all local clients matching resv will be FNC'd
746 */
747void
748resv_nick_fnc(const char *mask, const char *reason, int temp_time)
749{
750 struct Client *client_p, *target_p;
751 rb_dlink_node *ptr;
752 rb_dlink_node *next_ptr;
753 char *nick;
754 char note[NICKLEN+10];
755
756 if (!ConfigFileEntry.resv_fnc)
757 return;
758
759 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
760 {
761 client_p = ptr->data;
762
763 if(IsMe(client_p) || !IsPerson(client_p) || IsExemptResv(client_p))
764 continue;
765
07d3283f
KB
766 /* Skip users that already have UID nicks. */
767 if(IsDigit(client_p->name[0]))
768 continue;
769
330692a1
KB
770 if(match_esc(mask, client_p->name))
771 {
772 nick = client_p->id;
773
774 /* Tell opers. */
a9227555 775 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
330692a1
KB
776 "RESV forced nick change for %s!%s@%s to %s; nick matched [%s] (%s)",
777 client_p->name, client_p->username, client_p->host, nick, mask, reason);
778
a9227555 779 sendto_realops_snomask(SNO_NCHANGE, L_NETWIDE,
330692a1
KB
780 "Nick change: From %s to %s [%s@%s]",
781 client_p->name, nick, client_p->username, client_p->host);
782
783 /* Tell the user. */
784 if (temp_time > 0)
785 {
786 sendto_one_notice(client_p,
787 ":*** Nick %s is temporarily unavailable on this server.",
788 client_p->name);
789 }
790 else
791 {
792 sendto_one_notice(client_p,
793 ":*** Nick %s is no longer available on this server.",
794 client_p->name);
795 }
796
797 /* Do all of the nick-changing gymnastics. */
798 client_p->tsinfo = rb_current_time();
b47f8a4f 799 whowas_add_history(client_p, 1);
330692a1 800
07d3283f
KB
801 monitor_signoff(client_p);
802
330692a1
KB
803 invalidate_bancache_user(client_p);
804
583f064f 805 sendto_common_channels_local(client_p, NOCAPS, NOCAPS, ":%s!%s@%s NICK :%s",
330692a1
KB
806 client_p->name, client_p->username, client_p->host, nick);
807 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
808 use_id(client_p), nick, (long) client_p->tsinfo);
809
810 del_from_client_hash(client_p->name, client_p);
811 rb_strlcpy(client_p->name, nick, sizeof(client_p->name));
812 add_to_client_hash(nick, client_p);
813
07d3283f
KB
814 monitor_signon(client_p);
815
330692a1
KB
816 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
817 {
818 target_p = ptr->data;
819 rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
820 rb_dlinkDestroy(ptr, &client_p->on_allow_list);
821 }
822
5203cba5 823 snprintf(note, sizeof(note), "Nick: %s", nick);
330692a1
KB
824 rb_note(client_p->localClient->F, note);
825 }
826 }
827}
828
212380e3
WP
829/*
830 * update_client_exit_stats
831 *
832 * input - pointer to client
833 * output - NONE
55abcbb2 834 * side effects -
212380e3
WP
835 */
836static void
837update_client_exit_stats(struct Client *client_p)
838{
839 if(IsServer(client_p))
840 {
841 sendto_realops_snomask(SNO_EXTERNAL, L_ALL,
842 "Server %s split from %s",
843 client_p->name, client_p->servptr->name);
844 if(HasSentEob(client_p))
845 eob_count--;
846 }
847 else if(IsClient(client_p))
848 {
849 --Count.total;
850 if(IsOper(client_p))
851 --Count.oper;
852 if(IsInvisible(client_p))
853 --Count.invisi;
854 }
855
856 if(splitchecking && !splitmode)
857 check_splitmode(NULL);
858}
859
860/*
861 * release_client_state
862 *
863 * input - pointer to client to release
864 * output - NONE
55abcbb2 865 * side effects -
212380e3
WP
866 */
867static void
868release_client_state(struct Client *client_p)
869{
870 if(client_p->user != NULL)
871 {
872 free_user(client_p->user, client_p); /* try this here */
873 }
874 if(client_p->serv)
875 {
876 if(client_p->serv->user != NULL)
877 free_user(client_p->serv->user, client_p);
878 if(client_p->serv->fullcaps)
637c4932
VY
879 rb_free(client_p->serv->fullcaps);
880 rb_free(client_p->serv);
212380e3
WP
881 }
882}
883
884/*
885 * remove_client_from_list
886 * inputs - point to client to remove
887 * output - NONE
888 * side effects - taken the code from ExitOneClient() for this
889 * and placed it here. - avalon
890 */
891static void
892remove_client_from_list(struct Client *client_p)
893{
894 s_assert(NULL != client_p);
895
896 if(client_p == NULL)
897 return;
898
899 /* A client made with make_client()
900 * is on the unknown_list until removed.
901 * If it =does= happen to exit before its removed from that list
902 * and its =not= on the global_client_list, it will core here.
903 * short circuit that case now -db
904 */
905 if(client_p->node.prev == NULL && client_p->node.next == NULL)
906 return;
907
330fc5c1 908 rb_dlinkDelete(&client_p->node, &global_client_list);
212380e3
WP
909
910 update_client_exit_stats(client_p);
911}
912
913
2d28539c
JT
914/* clean_nick()
915 *
916 * input - nickname to check, flag for nick from local client
917 * output - 0 if erroneous, else 1
918 * side effects -
919 */
920int
921clean_nick(const char *nick, int loc_client)
922{
923 int len = 0;
924
925 /* nicks cant start with a digit or -, and must have a length */
926 if(*nick == '-' || *nick == '\0')
927 return 0;
928
929 if(loc_client && IsDigit(*nick))
930 return 0;
931
932 for(; *nick; nick++)
933 {
934 len++;
935 if(!IsNickChar(*nick))
936 return 0;
937 }
938
939 /* nicklen is +1 */
940 if(len >= NICKLEN && (unsigned int)len >= ConfigFileEntry.nicklen)
941 return 0;
942
943 return 1;
944}
945
212380e3
WP
946/*
947 * find_person - find person by (nick)name.
948 * inputs - pointer to name
949 * output - return client pointer
950 * side effects -
951 */
952struct Client *
953find_person(const char *name)
954{
955 struct Client *c2ptr;
956
957 c2ptr = find_client(name);
958
959 if(c2ptr && IsPerson(c2ptr))
960 return (c2ptr);
961 return (NULL);
962}
963
964struct Client *
965find_named_person(const char *name)
966{
967 struct Client *c2ptr;
968
969 c2ptr = find_named_client(name);
970
971 if(c2ptr && IsPerson(c2ptr))
972 return (c2ptr);
973 return (NULL);
974}
975
976
977/*
55abcbb2
KB
978 * find_chasing - find the client structure for a nick name (user)
979 * using history mechanism if necessary. If the client is not found,
212380e3
WP
980 * an error message (NO SUCH NICK) is generated. If the client was found
981 * through the history, chasing will be 1 and otherwise 0.
982 */
983struct Client *
984find_chasing(struct Client *source_p, const char *user, int *chasing)
985{
986 struct Client *who;
987
988 if(MyClient(source_p))
989 who = find_named_person(user);
990 else
991 who = find_person(user);
992
993 if(chasing)
994 *chasing = 0;
995
996 if(who || IsDigit(*user))
997 return who;
998
b47f8a4f 999 if(!(who = whowas_get_history(user, (long) KILLCHASETIMELIMIT)))
212380e3
WP
1000 {
1001 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
1002 form_str(ERR_NOSUCHNICK), user);
1003 return (NULL);
1004 }
1005 if(chasing)
1006 *chasing = 1;
1007 return who;
1008}
1009
1010/*
1011 * get_client_name - Return the name of the client
1012 * for various tracking and
1013 * admin purposes. The main purpose of this function is to
1014 * return the "socket host" name of the client, if that
1015 * differs from the advertised name (other than case).
1016 * But, this can be used to any client structure.
1017 *
1018 * NOTE 1:
1019 * Watch out the allocation of "nbuf", if either source_p->name
1020 * or source_p->sockhost gets changed into pointers instead of
1021 * directly allocated within the structure...
1022 *
1023 * NOTE 2:
1024 * Function return either a pointer to the structure (source_p) or
1025 * to internal buffer (nbuf). *NEVER* use the returned pointer
1026 * to modify what it points!!!
1027 */
1028
1029const char *
1030get_client_name(struct Client *client, int showip)
1031{
1032 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
1033
1034 s_assert(NULL != client);
1035 if(client == NULL)
1036 return NULL;
1037
1038 if(MyConnect(client))
1039 {
1040 if(!irccmp(client->name, client->host))
1041 return client->name;
1042
55abcbb2 1043 if(ConfigFileEntry.hide_spoof_ips &&
212380e3
WP
1044 showip == SHOW_IP && IsIPSpoof(client))
1045 showip = MASK_IP;
212380e3
WP
1046 if(IsAnyServer(client))
1047 showip = MASK_IP;
212380e3
WP
1048
1049 /* And finally, let's get the host information, ip or name */
1050 switch (showip)
1051 {
1052 case SHOW_IP:
5203cba5 1053 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
55abcbb2 1054 client->name, client->username,
212380e3
WP
1055 client->sockhost);
1056 break;
1057 case MASK_IP:
5203cba5 1058 snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
212380e3
WP
1059 client->name, client->username);
1060 break;
1061 default:
5203cba5 1062 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
212380e3
WP
1063 client->name, client->username, client->host);
1064 }
1065 return nbuf;
1066 }
1067
55abcbb2 1068 /* As pointed out by Adel Mezibra
212380e3
WP
1069 * Neph|l|m@EFnet. Was missing a return here.
1070 */
1071 return client->name;
1072}
55abcbb2 1073
212380e3
WP
1074/* log_client_name()
1075 *
1076 * This version is the same as get_client_name, but doesnt contain the
1077 * code that will hide IPs always. This should be used for logfiles.
1078 */
1079const char *
1080log_client_name(struct Client *target_p, int showip)
1081{
1082 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
1083
1084 if(target_p == NULL)
1085 return NULL;
1086
1087 if(MyConnect(target_p))
1088 {
1089 if(irccmp(target_p->name, target_p->host) == 0)
1090 return target_p->name;
1091
1092 switch (showip)
1093 {
1094 case SHOW_IP:
5203cba5 1095 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name,
212380e3
WP
1096 target_p->username, target_p->sockhost);
1097 break;
1098
212380e3 1099 default:
5203cba5 1100 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name,
212380e3
WP
1101 target_p->username, target_p->host);
1102 }
1103
1104 return nbuf;
1105 }
1106
1107 return target_p->name;
1108}
1109
1110/* is_remote_connect - Returns whether a server was /connect'ed by a remote
1111 * oper (send notices netwide) */
1112int
1113is_remote_connect(struct Client *client_p)
1114{
1115 struct Client *oper;
1116
1117 if (client_p->serv == NULL)
1118 return FALSE;
1119 oper = find_named_person(client_p->serv->by);
1120 return oper != NULL && IsOper(oper) && !MyConnect(oper);
1121}
1122
1123static void
1124free_exited_clients(void *unused)
1125{
330fc5c1 1126 rb_dlink_node *ptr, *next;
212380e3
WP
1127 struct Client *target_p;
1128
5cefa1d6 1129 RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
212380e3
WP
1130 {
1131 target_p = ptr->data;
1132
1133#ifdef DEBUG_EXITED_CLIENTS
1134 {
1135 struct abort_client *abt;
330fc5c1 1136 rb_dlink_node *aptr;
212380e3
WP
1137 int found = 0;
1138
5cefa1d6 1139 RB_DLINK_FOREACH(aptr, abort_list.head)
212380e3
WP
1140 {
1141 abt = aptr->data;
1142 if(abt->client == target_p)
1143 {
1144 s_assert(0);
55abcbb2 1145 sendto_realops_snomask(SNO_GENERAL, L_ALL,
66f7fe67 1146 "On abort_list: %s stat: %u flags: %llu handler: %c",
212380e3 1147 target_p->name, (unsigned int) target_p->status,
d19aab33 1148 (unsigned long long)target_p->flags, target_p->handler);
212380e3 1149 sendto_realops_snomask(SNO_GENERAL, L_ALL,
a6f63a82 1150 "Please report this to the solanum developers!");
212380e3
WP
1151 found++;
1152 }
1153 }
1154
1155 if(found)
1156 {
330fc5c1 1157 rb_dlinkDestroy(ptr, &dead_list);
212380e3
WP
1158 continue;
1159 }
1160 }
1161#endif
1162
1163 if(ptr->data == NULL)
1164 {
1165 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1166 "Warning: null client on dead_list!");
330fc5c1 1167 rb_dlinkDestroy(ptr, &dead_list);
212380e3
WP
1168 continue;
1169 }
1170 release_client_state(target_p);
1171 free_client(target_p);
330fc5c1 1172 rb_dlinkDestroy(ptr, &dead_list);
212380e3
WP
1173 }
1174
1175#ifdef DEBUG_EXITED_CLIENTS
5cefa1d6 1176 RB_DLINK_FOREACH_SAFE(ptr, next, dead_remote_list.head)
212380e3
WP
1177 {
1178 target_p = ptr->data;
1179
1180 if(ptr->data == NULL)
1181 {
1182 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1183 "Warning: null client on dead_list!");
330fc5c1 1184 rb_dlinkDestroy(ptr, &dead_list);
212380e3
WP
1185 continue;
1186 }
1187 release_client_state(target_p);
1188 free_client(target_p);
330fc5c1 1189 rb_dlinkDestroy(ptr, &dead_remote_list);
212380e3
WP
1190 }
1191#endif
55abcbb2 1192
212380e3
WP
1193}
1194
55abcbb2 1195/*
212380e3 1196** Remove all clients that depend on source_p; assumes all (S)QUITs have
55abcbb2
KB
1197** already been sent. we make sure to exit a server's dependent clients
1198** and servers before the server itself; exit_one_client takes care of
212380e3 1199** actually removing things off llists. tweaked from +CSr31 -orabidoo
cbeab4bc 1200 */
212380e3
WP
1201/*
1202 * added sanity test code.... source_p->serv might be NULL...
1203 */
1204static void
1205recurse_remove_clients(struct Client *source_p, const char *comment)
1206{
1207 struct Client *target_p;
330fc5c1 1208 rb_dlink_node *ptr, *ptr_next;
212380e3
WP
1209
1210 if(IsMe(source_p))
1211 return;
1212
1213 if(source_p->serv == NULL) /* oooops. uh this is actually a major bug */
1214 return;
1215
1216 /* this is very ugly, but it saves cpu :P */
1217 if(ConfigFileEntry.nick_delay > 0)
1218 {
5cefa1d6 1219 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
212380e3
WP
1220 {
1221 target_p = ptr->data;
1222 target_p->flags |= FLAGS_KILLED;
1223 add_nd_entry(target_p->name);
1224
1225 if(!IsDead(target_p) && !IsClosing(target_p))
1226 exit_remote_client(NULL, target_p, &me, comment);
1227 }
1228 }
1229 else
1230 {
5cefa1d6 1231 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
212380e3
WP
1232 {
1233 target_p = ptr->data;
1234 target_p->flags |= FLAGS_KILLED;
1235
1236 if(!IsDead(target_p) && !IsClosing(target_p))
1237 exit_remote_client(NULL, target_p, &me, comment);
1238 }
55abcbb2 1239 }
212380e3 1240
5cefa1d6 1241 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
212380e3
WP
1242 {
1243 target_p = ptr->data;
1244 recurse_remove_clients(target_p, comment);
1245 qs_server(NULL, target_p, &me, comment);
1246 }
1247}
1248
1249/*
1250** Remove *everything* that depends on source_p, from all lists, and sending
856df9a3 1251** all necessary SQUITs. source_p itself is still on the lists,
212380e3 1252** and its SQUITs have been sent except for the upstream one -orabidoo
cbeab4bc 1253 */
212380e3
WP
1254static void
1255remove_dependents(struct Client *client_p,
1256 struct Client *source_p,
1257 struct Client *from, const char *comment, const char *comment1)
1258{
1259 struct Client *to;
330fc5c1 1260 rb_dlink_node *ptr, *next;
212380e3 1261
5cefa1d6 1262 RB_DLINK_FOREACH_SAFE(ptr, next, serv_list.head)
212380e3
WP
1263 {
1264 to = ptr->data;
1265
856df9a3 1266 if(IsMe(to) || to == source_p->from || to == client_p)
212380e3
WP
1267 continue;
1268
856df9a3 1269 sendto_one(to, "SQUIT %s :%s", get_id(source_p, to), comment);
212380e3
WP
1270 }
1271
1272 recurse_remove_clients(source_p, comment1);
1273}
1274
1275void
1276exit_aborted_clients(void *unused)
1277{
1278 struct abort_client *abt;
330fc5c1 1279 rb_dlink_node *ptr, *next;
5cefa1d6 1280 RB_DLINK_FOREACH_SAFE(ptr, next, abort_list.head)
212380e3
WP
1281 {
1282 abt = ptr->data;
1283
1284#ifdef DEBUG_EXITED_CLIENTS
1285 {
330fc5c1 1286 if(rb_dlinkFind(abt->client, &dead_list))
212380e3
WP
1287 {
1288 s_assert(0);
55abcbb2 1289 sendto_realops_snomask(SNO_GENERAL, L_ALL,
66f7fe67 1290 "On dead_list: %s stat: %u flags: %llu handler: %c",
212380e3 1291 abt->client->name, (unsigned int) abt->client->status,
d19aab33 1292 (unsigned long long)abt->client->flags, abt->client->handler);
212380e3 1293 sendto_realops_snomask(SNO_GENERAL, L_ALL,
a6f63a82 1294 "Please report this to the solanum developers!");
212380e3
WP
1295 continue;
1296 }
1297 }
1298#endif
1299
1300 s_assert(*((unsigned long*)abt->client) != 0xdeadbeef); /* This is lame but its a debug thing */
330fc5c1 1301 rb_dlinkDelete(ptr, &abort_list);
212380e3
WP
1302
1303 if(IsAnyServer(abt->client))
a9227555 1304 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
212380e3 1305 "Closing link to %s: %s",
b3ebc7ab 1306 abt->client->name, abt->notice);
212380e3
WP
1307
1308 /* its no longer on abort list - we *must* remove
1309 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1310 */
1311 abt->client->flags &= ~FLAGS_CLOSING;
1312 exit_client(abt->client, abt->client, &me, abt->notice);
637c4932 1313 rb_free(abt);
212380e3
WP
1314 }
1315}
1316
1317
1318/*
1319 * dead_link - Adds client to a list of clients that need an exit_client()
212380e3
WP
1320 */
1321void
cef7a7bc 1322dead_link(struct Client *client_p, int sendqex)
212380e3
WP
1323{
1324 struct abort_client *abt;
1325
1326 s_assert(!IsMe(client_p));
1327 if(IsDead(client_p) || IsClosing(client_p) || IsMe(client_p))
1328 return;
1329
eddc2ab6 1330 abt = (struct abort_client *) rb_malloc(sizeof(struct abort_client));
212380e3 1331
cef7a7bc 1332 if(sendqex)
f427c8b0 1333 rb_strlcpy(abt->notice, "Max SendQ exceeded", sizeof(abt->notice));
212380e3 1334 else
5203cba5 1335 snprintf(abt->notice, sizeof(abt->notice), "Write error: %s", strerror(errno));
212380e3
WP
1336
1337 abt->client = client_p;
1338 SetIOError(client_p);
1339 SetDead(client_p);
1340 SetClosing(client_p);
330fc5c1 1341 rb_dlinkAdd(abt, &abt->node, &abort_list);
212380e3
WP
1342}
1343
1344
1345/* This does the remove of the user from channels..local or remote */
1346static inline void
1347exit_generic_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1348 const char *comment)
1349{
637c4932 1350 rb_dlink_node *ptr, *next_ptr;
212380e3
WP
1351
1352 if(IsOper(source_p))
330fc5c1 1353 rb_dlinkFindDestroy(source_p, &oper_list);
212380e3 1354
583f064f 1355 sendto_common_channels_local(source_p, NOCAPS, NOCAPS, ":%s!%s@%s QUIT :%s",
212380e3
WP
1356 source_p->name,
1357 source_p->username, source_p->host, comment);
1358
1359 remove_user_from_channels(source_p);
1360
1361 /* Should not be in any channels now */
1362 s_assert(source_p->user->channel.head == NULL);
1363
1364 /* Clean up invitefield */
637c4932 1365 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->user->invited.head)
212380e3
WP
1366 {
1367 del_invite(ptr->data, source_p);
1368 }
1369
1370 /* Clean up allow lists */
1371 del_all_accepts(source_p);
1372
b47f8a4f
WP
1373 whowas_add_history(source_p, 0);
1374 whowas_off_history(source_p);
212380e3
WP
1375
1376 monitor_signoff(source_p);
1377
1378 if(has_id(source_p))
1379 del_from_id_hash(source_p->id, source_p);
1380
1381 del_from_hostname_hash(source_p->orighost, source_p);
1382 del_from_client_hash(source_p->name, source_p);
1383 remove_client_from_list(source_p);
1384}
1385
55abcbb2 1386/*
212380e3
WP
1387 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1388 */
1389
1390static int
1391exit_remote_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1392 const char *comment)
1393{
1394 exit_generic_client(client_p, source_p, from, comment);
55abcbb2 1395
212380e3
WP
1396 if(source_p->servptr && source_p->servptr->serv)
1397 {
330fc5c1 1398 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users);
212380e3
WP
1399 }
1400
1401 if((source_p->flags & FLAGS_KILLED) == 0)
1402 {
1403 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1404 ":%s QUIT :%s", use_id(source_p), comment);
212380e3
WP
1405 }
1406
1407 SetDead(source_p);
1408#ifdef DEBUG_EXITED_CLIENTS
330fc5c1 1409 rb_dlinkAddAlloc(source_p, &dead_remote_list);
212380e3 1410#else
330fc5c1 1411 rb_dlinkAddAlloc(source_p, &dead_list);
212380e3
WP
1412#endif
1413 return(CLIENT_EXITED);
1414}
1415
1416/*
07554369 1417 * This assumes IsUnknown(source_p) == true and MyConnect(source_p) == true
212380e3
WP
1418 */
1419
1420static int
d2a4981a
SA
1421exit_unknown_client(struct Client *client_p, /* The local client originating the
1422 * exit or NULL, if this exit is
1423 * generated by this server for
1424 * internal reasons.
1425 * This will not get any of the
1426 * generated messages. */
1427 struct Client *source_p, /* Client exiting */
1428 struct Client *from, /* Client firing off this Exit,
1429 * never NULL! */
1430 const char *comment)
212380e3 1431{
d2a4981a 1432 authd_abort_client(source_p);
330fc5c1 1433 rb_dlinkDelete(&source_p->localClient->tnode, &unknown_list);
212380e3
WP
1434
1435 if(!IsIOError(source_p))
dd12a19c
JT
1436 sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
1437 source_p->user != NULL ? source_p->host : "127.0.0.1",
1438 comment);
212380e3
WP
1439
1440 close_connection(source_p);
1441
1442 if(has_id(source_p))
1443 del_from_id_hash(source_p->id, source_p);
1444
1445 del_from_hostname_hash(source_p->host, source_p);
365e1366
SA
1446 if (!IsAnyServer(source_p))
1447 del_from_client_hash(source_p->name, source_p);
212380e3 1448 remove_client_from_list(source_p);
212380e3 1449 SetDead(source_p);
330fc5c1 1450 rb_dlinkAddAlloc(source_p, &dead_list);
212380e3
WP
1451
1452 /* Note that we don't need to add unknowns to the dead_list */
1453 return(CLIENT_EXITED);
1454}
1455
1456static int
55abcbb2 1457exit_remote_server(struct Client *client_p, struct Client *source_p, struct Client *from,
212380e3
WP
1458 const char *comment)
1459{
1460 static char comment1[(HOSTLEN*2)+2];
1461 static char newcomment[BUFSIZE];
1462 struct Client *target_p;
55abcbb2 1463
212380e3
WP
1464 if(ConfigServerHide.flatten_links)
1465 strcpy(comment1, "*.net *.split");
1466 else
1467 {
66c8fdd2 1468 strcpy(comment1, source_p->servptr->name);
212380e3
WP
1469 strcat(comment1, " ");
1470 strcat(comment1, source_p->name);
1471 }
1472 if (IsPerson(from))
5203cba5 1473 snprintf(newcomment, sizeof(newcomment), "by %s: %s",
212380e3
WP
1474 from->name, comment);
1475
6c1e15a4 1476 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
212380e3 1477
6c1e15a4 1478 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
212380e3 1479
330fc5c1 1480 rb_dlinkFindDestroy(source_p, &global_serv_list);
212380e3 1481 target_p = source_p->from;
55abcbb2 1482
212380e3
WP
1483 if(target_p != NULL && IsServer(target_p) && target_p != client_p &&
1484 !IsMe(target_p) && (source_p->flags & FLAGS_KILLED) == 0)
1485 {
55abcbb2 1486 sendto_one(target_p, ":%s SQUIT %s :%s",
212380e3
WP
1487 get_id(from, target_p), get_id(source_p, target_p),
1488 comment);
1489 }
1490
1491 if(has_id(source_p))
1492 del_from_id_hash(source_p->id, source_p);
1493
1494 del_from_client_hash(source_p->name, source_p);
55abcbb2 1495 remove_client_from_list(source_p);
6c1e15a4 1496
994544c2 1497 scache_split(source_p->serv->nameinfo);
55abcbb2 1498
212380e3
WP
1499 SetDead(source_p);
1500#ifdef DEBUG_EXITED_CLIENTS
330fc5c1 1501 rb_dlinkAddAlloc(source_p, &dead_remote_list);
212380e3 1502#else
330fc5c1 1503 rb_dlinkAddAlloc(source_p, &dead_list);
212380e3
WP
1504#endif
1505 return 0;
1506}
1507
1508static int
55abcbb2 1509qs_server(struct Client *client_p, struct Client *source_p, struct Client *from,
212380e3
WP
1510 const char *comment)
1511{
212380e3 1512 if(source_p->servptr && source_p->servptr->serv)
330fc5c1 1513 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
212380e3
WP
1514 else
1515 s_assert(0);
1516
330fc5c1 1517 rb_dlinkFindDestroy(source_p, &global_serv_list);
55abcbb2 1518
212380e3
WP
1519 if(has_id(source_p))
1520 del_from_id_hash(source_p->id, source_p);
1521
1522 del_from_client_hash(source_p->name, source_p);
55abcbb2 1523 remove_client_from_list(source_p);
19807b5b 1524 scache_split(source_p->serv->nameinfo);
55abcbb2 1525
212380e3 1526 SetDead(source_p);
55abcbb2 1527 rb_dlinkAddAlloc(source_p, &dead_list);
212380e3
WP
1528 return 0;
1529}
1530
1531static int
55abcbb2 1532exit_local_server(struct Client *client_p, struct Client *source_p, struct Client *from,
212380e3
WP
1533 const char *comment)
1534{
1535 static char comment1[(HOSTLEN*2)+2];
1536 static char newcomment[BUFSIZE];
1537 unsigned int sendk, recvk;
55abcbb2 1538
330fc5c1
WP
1539 rb_dlinkDelete(&source_p->localClient->tnode, &serv_list);
1540 rb_dlinkFindDestroy(source_p, &global_serv_list);
55abcbb2 1541
212380e3
WP
1542 sendk = source_p->localClient->sendK;
1543 recvk = source_p->localClient->receiveK;
1544
1545 /* Always show source here, so the server notices show
1546 * which side initiated the split -- jilles
1547 */
5203cba5 1548 snprintf(newcomment, sizeof(newcomment), "by %s: %s",
212380e3
WP
1549 from == source_p ? me.name : from->name, comment);
1550 if (!IsIOError(source_p))
1551 sendto_one(source_p, "SQUIT %s :%s", use_id(source_p),
1552 newcomment);
1553 if(client_p != NULL && source_p != client_p && !IsIOError(source_p))
1554 {
1555 sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1556 source_p->name, comment);
1557 }
55abcbb2 1558
212380e3 1559 if(source_p->servptr && source_p->servptr->serv)
330fc5c1 1560 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
212380e3
WP
1561 else
1562 s_assert(0);
1563
1564
1565 close_connection(source_p);
55abcbb2 1566
212380e3
WP
1567 if(ConfigServerHide.flatten_links)
1568 strcpy(comment1, "*.net *.split");
1569 else
1570 {
66c8fdd2 1571 strcpy(comment1, source_p->servptr->name);
212380e3
WP
1572 strcat(comment1, " ");
1573 strcat(comment1, source_p->name);
1574 }
1575
1576 if(source_p->serv != NULL)
1577 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1578
1579 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s was connected"
1580 " for %ld seconds. %d/%d sendK/recvK.",
adc6cc42 1581 source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
212380e3
WP
1582
1583 ilog(L_SERVER, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
adc6cc42 1584 source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
55abcbb2 1585
212380e3
WP
1586 if(has_id(source_p))
1587 del_from_id_hash(source_p->id, source_p);
1588
1589 del_from_client_hash(source_p->name, source_p);
1590 remove_client_from_list(source_p);
994544c2 1591 scache_split(source_p->serv->nameinfo);
55abcbb2 1592
212380e3 1593 SetDead(source_p);
330fc5c1 1594 rb_dlinkAddAlloc(source_p, &dead_list);
212380e3
WP
1595 return 0;
1596}
1597
1598
55abcbb2 1599/*
07554369 1600 * This assumes IsPerson(source_p) == true && MyConnect(source_p) == true
212380e3
WP
1601 */
1602
1603static int
1604exit_local_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1605 const char *comment)
1606{
1607 unsigned long on_for;
08d75d97 1608 char tbuf[26];
212380e3
WP
1609
1610 exit_generic_client(client_p, source_p, from, comment);
1611 clear_monitor(source_p);
1612
1613 s_assert(IsPerson(source_p));
330fc5c1
WP
1614 rb_dlinkDelete(&source_p->localClient->tnode, &lclient_list);
1615 rb_dlinkDelete(&source_p->lnode, &me.serv->users);
212380e3
WP
1616
1617 if(IsOper(source_p))
330fc5c1 1618 rb_dlinkFindDestroy(source_p, &local_oper_list);
212380e3
WP
1619
1620 sendto_realops_snomask(SNO_CCONN, L_ALL,
1621 "Client exiting: %s (%s@%s) [%s] [%s]",
1622 source_p->name,
1623 source_p->username, source_p->host, comment,
1624 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255");
1625
1626 sendto_realops_snomask(SNO_CCONNEXT, L_ALL,
1627 "CLIEXIT %s %s %s %s 0 %s",
1628 source_p->name, source_p->username, source_p->host,
1629 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255",
1630 comment);
1631
e3354945 1632 on_for = rb_current_time() - source_p->localClient->firsttime;
212380e3 1633
9641f156 1634 ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %s %d/%d",
08d75d97 1635 rb_ctime(rb_current_time(), tbuf, sizeof(tbuf)), on_for / 3600,
212380e3
WP
1636 (on_for % 3600) / 60, on_for % 60,
1637 source_p->name, source_p->username, source_p->host,
9641f156 1638 source_p->sockhost,
212380e3
WP
1639 source_p->localClient->sendK, source_p->localClient->receiveK);
1640
1641 sendto_one(source_p, "ERROR :Closing Link: %s (%s)", source_p->host, comment);
1642 close_connection(source_p);
1643
1644 if((source_p->flags & FLAGS_KILLED) == 0)
1645 {
1646 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1647 ":%s QUIT :%s", use_id(source_p), comment);
212380e3
WP
1648 }
1649
1650 SetDead(source_p);
330fc5c1 1651 rb_dlinkAddAlloc(source_p, &dead_list);
212380e3
WP
1652 return(CLIENT_EXITED);
1653}
1654
1655
1656/*
1657** exit_client - This is old "m_bye". Name changed, because this is not a
1658** protocol function, but a general server utility function.
1659**
1660** This function exits a client of *any* type (user, server, etc)
1661** from this server. Also, this generates all necessary prototol
1662** messages that this exit may cause.
1663**
1664** 1) If the client is a local client, then this implicitly
1665** exits all other clients depending on this connection (e.g.
1666** remote clients having 'from'-field that points to this.
1667**
1668** 2) If the client is a remote client, then only this is exited.
1669**
1670** For convenience, this function returns a suitable value for
1671** m_function return value:
1672**
1673** CLIENT_EXITED if (client_p == source_p)
1674** 0 if (client_p != source_p)
cbeab4bc 1675 */
212380e3
WP
1676int
1677exit_client(struct Client *client_p, /* The local client originating the
1678 * exit or NULL, if this exit is
1679 * generated by this server for
1680 * internal reasons.
1681 * This will not get any of the
1682 * generated messages. */
1683 struct Client *source_p, /* Client exiting */
1684 struct Client *from, /* Client firing off this Exit,
1685 * never NULL! */
1686 const char *comment /* Reason for the exit */
1687 )
1688{
15b05f95
SA
1689 int ret = -1;
1690
212380e3
WP
1691 hook_data_client_exit hdata;
1692 if(IsClosing(source_p))
1693 return -1;
1694
1695 /* note, this HAS to be here, when we exit a client we attempt to
1696 * send them data, if this generates a write error we must *not* add
1697 * them to the abort list --fl
1698 */
1699 SetClosing(source_p);
1700
1701 hdata.local_link = client_p;
1702 hdata.target = source_p;
1703 hdata.from = from;
1704 hdata.comment = comment;
1705 call_hook(h_client_exit, &hdata);
1706
1707 if(MyConnect(source_p))
1708 {
1709 /* Local clients of various types */
1710 if(IsPerson(source_p))
15b05f95 1711 ret = exit_local_client(client_p, source_p, from, comment);
212380e3 1712 else if(IsServer(source_p))
15b05f95 1713 ret = exit_local_server(client_p, source_p, from, comment);
212380e3
WP
1714 /* IsUnknown || IsConnecting || IsHandShake */
1715 else if(!IsReject(source_p))
15b05f95 1716 ret = exit_unknown_client(client_p, source_p, from, comment);
55abcbb2
KB
1717 }
1718 else
212380e3
WP
1719 {
1720 /* Remotes */
1721 if(IsPerson(source_p))
15b05f95 1722 ret = exit_remote_client(client_p, source_p, from, comment);
212380e3 1723 else if(IsServer(source_p))
15b05f95 1724 ret = exit_remote_server(client_p, source_p, from, comment);
212380e3
WP
1725 }
1726
15b05f95
SA
1727 call_hook(h_after_client_exit, NULL);
1728
1729 return ret;
212380e3
WP
1730}
1731
1732/*
1733 * Count up local client memory
1734 */
1735
1736/* XXX one common Client list now */
1737void
1738count_local_client_memory(size_t * count, size_t * local_client_memory_used)
1739{
1087485c
JT
1740 size_t lusage;
1741 rb_bh_usage(lclient_heap, count, NULL, &lusage, NULL);
adc6cc42 1742 *local_client_memory_used = lusage + (*count * (sizeof(void *) + sizeof(struct Client)));
212380e3
WP
1743}
1744
1745/*
1746 * Count up remote client memory
1747 */
1748void
1749count_remote_client_memory(size_t * count, size_t * remote_client_memory_used)
1750{
1087485c
JT
1751 size_t lcount, rcount;
1752 rb_bh_usage(lclient_heap, &lcount, NULL, NULL, NULL);
1753 rb_bh_usage(client_heap, &rcount, NULL, NULL, NULL);
1754 *count = rcount - lcount;
adc6cc42 1755 *remote_client_memory_used = *count * (sizeof(void *) + sizeof(struct Client));
212380e3
WP
1756}
1757
1758
1759/*
1760 * accept processing, this adds a form of "caller ID" to ircd
55abcbb2 1761 *
212380e3 1762 * If a client puts themselves into "caller ID only" mode,
55abcbb2 1763 * only clients that match a client pointer they have put on
212380e3
WP
1764 * the accept list will be allowed to message them.
1765 *
1766 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1767 *
1768 * [target.allow_list] -> [ source1 ] -> [source2 ]
1769 *
1770 * i.e. a target will have a link list of source pointers it will allow
1771 * each source client then has a back pointer pointing back
1772 * to the client that has it on its accept list.
1773 * This allows for exit_one_client to remove these now bogus entries
55abcbb2 1774 * from any client having an accept on them.
212380e3
WP
1775 */
1776/*
1777 * del_all_accepts
1778 *
1779 * inputs - pointer to exiting client
1780 * output - NONE
1781 * side effects - Walk through given clients allow_list and on_allow_list
1782 * remove all references to this client
1783 */
1784void
1785del_all_accepts(struct Client *client_p)
1786{
330fc5c1 1787 rb_dlink_node *ptr;
637c4932 1788 rb_dlink_node *next_ptr;
212380e3
WP
1789 struct Client *target_p;
1790
1791 if(MyClient(client_p) && client_p->localClient->allow_list.head)
1792 {
1793 /* clear this clients accept list, and remove them from
1794 * everyones on_accept_list
1795 */
637c4932 1796 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head)
212380e3
WP
1797 {
1798 target_p = ptr->data;
330fc5c1
WP
1799 rb_dlinkFindDestroy(client_p, &target_p->on_allow_list);
1800 rb_dlinkDestroy(ptr, &client_p->localClient->allow_list);
212380e3
WP
1801 }
1802 }
1803
1804 /* remove this client from everyones accept list */
637c4932 1805 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
212380e3
WP
1806 {
1807 target_p = ptr->data;
330fc5c1
WP
1808 rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
1809 rb_dlinkDestroy(ptr, &client_p->on_allow_list);
212380e3
WP
1810 }
1811}
1812
1813/*
521f9d63 1814 * show_ip() - asks if the true IP should be shown when source is
55abcbb2 1815 * asking for info about target
212380e3
WP
1816 *
1817 * Inputs - source_p who is asking
1818 * - target_p who do we want the info on
521f9d63 1819 * Output - returns 1 if clear IP can be shown, otherwise 0
212380e3
WP
1820 * Side Effects - none
1821 */
1822
1823int
1824show_ip(struct Client *source_p, struct Client *target_p)
1825{
1826 if(IsAnyServer(target_p))
1827 {
212380e3
WP
1828 return 0;
1829 }
1830 else if(IsIPSpoof(target_p))
1831 {
1832 /* source == NULL indicates message is being sent
1833 * to local opers.
1834 */
1835 if(!ConfigFileEntry.hide_spoof_ips &&
458c8538 1836 (source_p == NULL || (MyConnect(source_p) && HasPrivilege(source_p, "auspex:hostname"))))
212380e3
WP
1837 return 1;
1838 return 0;
1839 }
458c8538 1840 else if(IsDynSpoof(target_p) && (source_p != NULL && !HasPrivilege(source_p, "auspex:hostname")))
212380e3
WP
1841 return 0;
1842 else
1843 return 1;
1844}
1845
1846int
1847show_ip_conf(struct ConfItem *aconf, struct Client *source_p)
1848{
1849 if(IsConfDoSpoofIp(aconf))
1850 {
1851 if(!ConfigFileEntry.hide_spoof_ips && MyOper(source_p))
1852 return 1;
1853
1854 return 0;
1855 }
1856 else
1857 return 1;
1858}
1859
364e59f8
JT
1860int
1861show_ip_whowas(struct Whowas *whowas, struct Client *source_p)
1862{
1863 if(whowas->flags & WHOWAS_IP_SPOOFING)
1864 if(ConfigFileEntry.hide_spoof_ips || !MyOper(source_p))
1865 return 0;
1866 if(whowas->flags & WHOWAS_DYNSPOOF)
1867 if(!IsOper(source_p))
1868 return 0;
1869 return 1;
1870}
1871
212380e3
WP
1872/*
1873 * make_user
1874 *
1875 * inputs - pointer to client struct
1876 * output - pointer to struct User
1877 * side effects - add's an User information block to a client
1878 * if it was not previously allocated.
1879 */
1880struct User *
1881make_user(struct Client *client_p)
1882{
1883 struct User *user;
1884
1885 user = client_p->user;
1886 if(!user)
1887 {
398b6a73 1888 user = (struct User *) rb_bh_alloc(user_heap);
212380e3
WP
1889 user->refcnt = 1;
1890 client_p->user = user;
1891 }
1892 return user;
1893}
1894
1895/*
1896 * make_server
1897 *
1898 * inputs - pointer to client struct
1899 * output - pointer to server_t
1900 * side effects - add's an Server information block to a client
1901 * if it was not previously allocated.
1902 */
8f103562 1903struct Server *
212380e3
WP
1904make_server(struct Client *client_p)
1905{
8f103562 1906 struct Server *serv = client_p->serv;
212380e3
WP
1907
1908 if(!serv)
1909 {
8f103562 1910 serv = (struct Server *) rb_malloc(sizeof(struct Server));
212380e3
WP
1911 client_p->serv = serv;
1912 }
1913 return client_p->serv;
1914}
1915
1916/*
1917 * free_user
55abcbb2 1918 *
212380e3
WP
1919 * inputs - pointer to user struct
1920 * - pointer to client struct
1921 * output - none
1922 * side effects - Decrease user reference count by one and release block,
1923 * if count reaches 0
1924 */
1925void
1926free_user(struct User *user, struct Client *client_p)
1927{
c127b45b
SB
1928 free_away(client_p);
1929
212380e3
WP
1930 if(--user->refcnt <= 0)
1931 {
c127b45b
SB
1932 if(user->away)
1933 rb_free((char *) user->away);
ed3ca2ff
EK
1934 rb_free(user->opername);
1935 if (user->privset)
1936 privilegeset_unref(user->privset);
212380e3
WP
1937 /*
1938 * sanity check
1939 */
1940 if(user->refcnt < 0 || user->invited.head || user->channel.head)
1941 {
1942 sendto_realops_snomask(SNO_GENERAL, L_ALL,
0b9aca38
WP
1943 "* %p user (%s!%s@%s) %p %p %p %lu %d *",
1944 client_p,
212380e3
WP
1945 client_p ? client_p->
1946 name : "<noname>",
1947 client_p->username,
1948 client_p->host,
0b9aca38
WP
1949 user,
1950 user->invited.head,
1951 user->channel.head,
330fc5c1 1952 rb_dlink_list_length(&user->channel),
212380e3
WP
1953 user->refcnt);
1954 s_assert(!user->refcnt);
1955 s_assert(!user->invited.head);
1956 s_assert(!user->channel.head);
1957 }
1958
398b6a73 1959 rb_bh_free(user_heap, user);
212380e3
WP
1960 }
1961}
1962
e581ea8e 1963void
c127b45b 1964allocate_away(struct Client *client_p)
e581ea8e 1965{
c127b45b 1966 if(client_p->user->away == NULL)
55abcbb2 1967 client_p->user->away = rb_bh_alloc(away_heap);
e581ea8e
WP
1968}
1969
c127b45b 1970
e581ea8e 1971void
c127b45b 1972free_away(struct Client *client_p)
e581ea8e 1973{
c127b45b
SB
1974 if(client_p->user != NULL && client_p->user->away != NULL) {
1975 rb_bh_free(away_heap, client_p->user->away);
1976 client_p->user->away = NULL;
e581ea8e
WP
1977 }
1978}
1979
212380e3
WP
1980void
1981init_uid(void)
1982{
1983 int i;
1984
1985 for(i = 0; i < 3; i++)
1986 current_uid[i] = me.id[i];
1987
1988 for(i = 3; i < 9; i++)
1989 current_uid[i] = 'A';
1990
1991 current_uid[9] = '\0';
1992}
1993
1994
1995char *
1996generate_uid(void)
1997{
428f9bc1 1998 static int flipped = 0;
212380e3
WP
1999 int i;
2000
428f9bc1 2001uid_restart:
212380e3
WP
2002 for(i = 8; i > 3; i--)
2003 {
2004 if(current_uid[i] == 'Z')
2005 {
2006 current_uid[i] = '0';
428f9bc1 2007 goto out;
212380e3
WP
2008 }
2009 else if(current_uid[i] != '9')
2010 {
2011 current_uid[i]++;
428f9bc1 2012 goto out;
212380e3
WP
2013 }
2014 else
2015 current_uid[i] = 'A';
2016 }
2017
2018 /* if this next if() triggers, we're fucked. */
2019 if(current_uid[3] == 'Z')
2020 {
2021 current_uid[i] = 'A';
428f9bc1 2022 flipped = 1;
212380e3
WP
2023 }
2024 else
2025 current_uid[i]++;
428f9bc1
WP
2026out:
2027 /* if this happens..well, i'm not sure what to say, but lets handle it correctly */
2028 if(rb_unlikely(flipped))
2029 {
2030 /* this slows down uid generation a bit... */
2031 if(find_id(current_uid) != NULL)
2032 goto uid_restart;
2033 }
212380e3
WP
2034 return current_uid;
2035}
2036
2037/*
2038 * close_connection
2039 * Close the physical connection. This function must make
2040 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
2041 */
2042void
2043close_connection(struct Client *client_p)
2044{
2045 s_assert(client_p != NULL);
2046 if(client_p == NULL)
2047 return;
2048
2049 s_assert(MyConnect(client_p));
2050 if(!MyConnect(client_p))
2051 return;
55abcbb2 2052
212380e3
WP
2053 if(IsServer(client_p))
2054 {
2055 struct server_conf *server_p;
2056
8bd5767b
JT
2057 ServerStats.is_sv++;
2058 ServerStats.is_sbs += client_p->localClient->sendB;
2059 ServerStats.is_sbr += client_p->localClient->receiveB;
679ccbe5 2060 ServerStats.is_sti += (unsigned long long)(rb_current_time() - client_p->localClient->firsttime);
212380e3
WP
2061
2062 /*
2063 * If the connection has been up for a long amount of time, schedule
2064 * a 'quick' reconnect, else reset the next-connect cycle.
2065 */
2066 if((server_p = find_server_conf(client_p->name)) != NULL)
2067 {
2068 /*
2069 * Reschedule a faster reconnect, if this was a automatically
2070 * connected configuration entry. (Note that if we have had
2071 * a rehash in between, the status has been changed to
2072 * CONF_ILLEGAL). But only do this if it was a "good" link.
2073 */
2074 server_p->hold = time(NULL);
2075 server_p->hold +=
2076 (server_p->hold - client_p->localClient->lasttime >
2077 HANGONGOODLINK) ? HANGONRETRYDELAY : ConFreq(server_p->class);
2078 }
2079
2080 }
2081 else if(IsClient(client_p))
2082 {
8bd5767b
JT
2083 ServerStats.is_cl++;
2084 ServerStats.is_cbs += client_p->localClient->sendB;
2085 ServerStats.is_cbr += client_p->localClient->receiveB;
679ccbe5 2086 ServerStats.is_cti += (unsigned long long)(rb_current_time() - client_p->localClient->firsttime);
212380e3
WP
2087 }
2088 else
47adde3d 2089 ServerStats.is_ni++;
54ac8b60 2090
de7cf7e0 2091 client_release_connids(client_p);
36f0ec74 2092
c6d72037 2093 if(client_p->localClient->F != NULL)
54ac8b60
VY
2094 {
2095 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
2096 if(!IsIOError(client_p))
2097 send_queued(client_p);
2098
2099 rb_close(client_p->localClient->F);
2100 client_p->localClient->F = NULL;
2101 }
2102
734d420e
JT
2103 rb_linebuf_donebuf(&client_p->localClient->buf_sendq);
2104 rb_linebuf_donebuf(&client_p->localClient->buf_recvq);
212380e3
WP
2105 detach_conf(client_p);
2106
2107 /* XXX shouldnt really be done here. */
2108 detach_server_conf(client_p);
2109
2110 client_p->from = NULL; /* ...this should catch them! >:) --msa */
2111 ClearMyConnect(client_p);
2112 SetIOError(client_p);
2113}
2114
2115
2116
2117void
2118error_exit_client(struct Client *client_p, int error)
2119{
2120 /*
2121 * ...hmm, with non-blocking sockets we might get
2122 * here from quite valid reasons, although.. why
2123 * would select report "data available" when there
2124 * wasn't... so, this must be an error anyway... --msa
2125 * actually, EOF occurs when read() returns 0 and
2126 * in due course, select() returns that fd as ready
2127 * for reading even though it ends up being an EOF. -avalon
2128 */
2129 char errmsg[255];
734d420e 2130 int current_error = rb_get_sockerr(client_p->localClient->F);
212380e3
WP
2131
2132 SetIOError(client_p);
2133
2134 if(IsServer(client_p) || IsHandshake(client_p))
2135 {
212380e3
WP
2136 if(error == 0)
2137 {
a9227555 2138 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
212380e3 2139 "Server %s closed the connection",
b3ebc7ab 2140 client_p->name);
212380e3
WP
2141
2142 ilog(L_SERVER, "Server %s closed the connection",
2143 log_client_name(client_p, SHOW_IP));
2144 }
2145 else
2146 {
a9227555 2147 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
212380e3
WP
2148 "Lost connection to %s: %s",
2149 client_p->name, strerror(current_error));
2150 ilog(L_SERVER, "Lost connection to %s: %s",
2151 log_client_name(client_p, SHOW_IP), strerror(current_error));
2152 }
212380e3
WP
2153 }
2154
2155 if(error == 0)
f427c8b0 2156 rb_strlcpy(errmsg, "Remote host closed the connection", sizeof(errmsg));
212380e3 2157 else
5203cba5 2158 snprintf(errmsg, sizeof(errmsg), "Read error: %s", strerror(current_error));
212380e3
WP
2159
2160 exit_client(client_p, client_p, &me, errmsg);
2161}