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