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