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