]> jfr.im git - solanum.git/blob - ircd/client.c
Remove unused kline delay machinery
[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->opername);
304 rb_free(client_p->localClient->mangledhost);
305 if (client_p->localClient->privset)
306 privilegeset_unref(client_p->localClient->privset);
307
308 if (IsSSL(client_p))
309 ssld_decrement_clicount(client_p->localClient->ssl_ctl);
310
311 rb_free(client_p->localClient->cipher_string);
312
313 if (IsCapable(client_p, CAP_ZIP))
314 ssld_decrement_clicount(client_p->localClient->z_ctl);
315
316 rb_free(client_p->localClient->zipstats);
317
318 if (client_p->localClient->ws_ctl != NULL)
319 wsockd_decrement_clicount(client_p->localClient->ws_ctl);
320
321 rb_bh_free(lclient_heap, client_p->localClient);
322 client_p->localClient = NULL;
323 }
324
325 static void
326 free_client(struct Client *client_p)
327 {
328 s_assert(NULL != client_p);
329 s_assert(&me != client_p);
330 free_local_client(client_p);
331 free_pre_client(client_p);
332 rb_free(client_p->certfp);
333 rb_bh_free(client_heap, client_p);
334 }
335
336 /*
337 * check_pings - go through the local client list and check activity
338 * kill off stuff that should die
339 *
340 * inputs - NOT USED (from event)
341 * output - next time_t when check_pings() should be called again
342 * side effects -
343 *
344 *
345 * A PING can be sent to clients as necessary.
346 *
347 * Client/Server ping outs are handled.
348 */
349
350 /*
351 * Addon from adrian. We used to call this after nextping seconds,
352 * however I've changed it to run once a second. This is only for
353 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
354 * run once a second makes life a lot easier - when a new client connects
355 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
356 * we end up waiting 20 seconds. This is stupid. :-)
357 * I will optimise (hah!) check_pings() once I've finished working on
358 * tidying up other network IO evilnesses.
359 * -- adrian
360 */
361
362 static void
363 check_pings(void *notused)
364 {
365 check_pings_list(&lclient_list);
366 check_pings_list(&serv_list);
367 check_unknowns_list(&unknown_list);
368 }
369
370 /*
371 * Check_pings_list()
372 *
373 * inputs - pointer to list to check
374 * output - NONE
375 * side effects -
376 */
377 static void
378 check_pings_list(rb_dlink_list * list)
379 {
380 char scratch[32]; /* way too generous but... */
381 struct Client *client_p; /* current local client_p being examined */
382 int ping = 0; /* ping time value from client */
383 rb_dlink_node *ptr, *next_ptr;
384
385 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
386 {
387 client_p = ptr->data;
388
389 if(!MyConnect(client_p) || IsDead(client_p))
390 continue;
391
392 ping = get_client_ping(client_p);
393
394 if(ping < (rb_current_time() - client_p->localClient->lasttime))
395 {
396 /*
397 * If the client/server hasnt talked to us in 2*ping seconds
398 * and it has a ping time, then close its connection.
399 */
400 if(((rb_current_time() - client_p->localClient->lasttime) >= (2 * ping)
401 && (client_p->flags & FLAGS_PINGSENT)))
402 {
403 if(IsServer(client_p))
404 {
405 sendto_realops_snomask(SNO_GENERAL, L_ALL,
406 "No response from %s, closing link",
407 client_p->name);
408 ilog(L_SERVER,
409 "No response from %s, closing link",
410 log_client_name(client_p, HIDE_IP));
411 }
412 (void) snprintf(scratch, sizeof(scratch),
413 "Ping timeout: %d seconds",
414 (int) (rb_current_time() - client_p->localClient->lasttime));
415
416 exit_client(client_p, client_p, &me, scratch);
417 continue;
418 }
419 else if((client_p->flags & FLAGS_PINGSENT) == 0)
420 {
421 /*
422 * if we havent PINGed the connection and we havent
423 * heard from it in a while, PING it to make sure
424 * it is still alive.
425 */
426 client_p->flags |= FLAGS_PINGSENT;
427 /* not nice but does the job */
428 client_p->localClient->lasttime = rb_current_time() - ping;
429 sendto_one(client_p, "PING :%s", me.name);
430 }
431 }
432 /* ping_timeout: */
433
434 }
435 }
436
437 /*
438 * check_unknowns_list
439 *
440 * inputs - pointer to list of unknown clients
441 * output - NONE
442 * side effects - unknown clients get marked for termination after n seconds
443 */
444 static void
445 check_unknowns_list(rb_dlink_list * list)
446 {
447 rb_dlink_node *ptr, *next_ptr;
448 struct Client *client_p;
449 int timeout;
450
451 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
452 {
453 client_p = ptr->data;
454
455 if(IsDead(client_p) || IsClosing(client_p))
456 continue;
457
458 /* Still querying with authd */
459 if(client_p->preClient != NULL && client_p->preClient->auth.cid != 0)
460 continue;
461
462 /*
463 * Check UNKNOWN connections - if they have been in this state
464 * for > 30s, close them.
465 */
466
467 timeout = IsAnyServer(client_p) ? ConfigFileEntry.connect_timeout : 30;
468 if((rb_current_time() - client_p->localClient->firsttime) > timeout)
469 {
470 if(IsAnyServer(client_p))
471 {
472 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
473 "No response from %s, closing link",
474 client_p->name);
475 ilog(L_SERVER,
476 "No response from %s, closing link",
477 log_client_name(client_p, HIDE_IP));
478 }
479 exit_client(client_p, client_p, &me, "Connection timed out");
480 }
481 }
482 }
483
484 void
485 notify_banned_client(struct Client *client_p, struct ConfItem *aconf, int ban)
486 {
487 static const char conn_closed[] = "Connection closed";
488 static const char d_lined[] = "D-lined";
489 static const char k_lined[] = "K-lined";
490 const char *reason = NULL;
491 const char *exit_reason = conn_closed;
492
493 if(ConfigFileEntry.kline_with_reason)
494 {
495 reason = get_user_ban_reason(aconf);
496 exit_reason = reason;
497 }
498 else
499 {
500 reason = aconf->status == D_LINED ? d_lined : k_lined;
501 }
502
503 if(ban == D_LINED && !IsPerson(client_p))
504 sendto_one(client_p, "NOTICE DLINE :*** You have been D-lined");
505 else
506 sendto_one(client_p, form_str(ERR_YOUREBANNEDCREEP),
507 me.name, client_p->name, reason);
508
509 exit_client(client_p, client_p, &me,
510 EmptyString(ConfigFileEntry.kline_reason) ? exit_reason :
511 ConfigFileEntry.kline_reason);
512 }
513
514 /*
515 * check_banned_lines
516 * inputs - NONE
517 * output - NONE
518 * side effects - Check all connections for a pending k/dline against the
519 * client, exit the client if found.
520 */
521 void
522 check_banned_lines(void)
523 {
524 check_dlines();
525 check_klines();
526 check_xlines();
527 }
528
529 /* check_klines
530 *
531 * inputs -
532 * outputs -
533 * side effects - all clients will be checked for klines
534 */
535 void
536 check_klines(void)
537 {
538 struct Client *client_p;
539 struct ConfItem *aconf;
540 rb_dlink_node *ptr;
541 rb_dlink_node *next_ptr;
542
543 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
544 {
545 client_p = ptr->data;
546
547 if(IsMe(client_p) || !IsPerson(client_p))
548 continue;
549
550 if((aconf = find_kline(client_p)) != NULL)
551 {
552 if(IsExemptKline(client_p))
553 {
554 sendto_realops_snomask(SNO_GENERAL, L_ALL,
555 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
556 get_client_name(client_p, HIDE_IP),
557 aconf->user, aconf->host);
558 continue;
559 }
560
561 sendto_realops_snomask(SNO_GENERAL, L_ALL,
562 "KLINE active for %s",
563 get_client_name(client_p, HIDE_IP));
564
565 notify_banned_client(client_p, aconf, K_LINED);
566 continue;
567 }
568 }
569 }
570
571
572 /* check_one_kline()
573 *
574 * inputs - pointer to kline to check
575 * outputs -
576 * side effects - all clients will be checked against given kline
577 */
578 void
579 check_one_kline(struct ConfItem *kline)
580 {
581 struct Client *client_p;
582 rb_dlink_node *ptr;
583 rb_dlink_node *next_ptr;
584
585 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
586 {
587 client_p = ptr->data;
588
589 if(IsMe(client_p) || !IsPerson(client_p))
590 continue;
591
592 if(!match(kline->user, client_p->username))
593 continue;
594
595 /* match one kline */
596 {
597 int matched = 0;
598 int masktype;
599 int bits;
600 struct rb_sockaddr_storage sockaddr;
601
602 masktype = parse_netmask(kline->host, (struct sockaddr *)&sockaddr, &bits);
603
604 switch (masktype) {
605 case HM_IPV4:
606 case HM_IPV6:
607 if(comp_with_mask_sock((struct sockaddr *)&client_p->localClient->ip,
608 (struct sockaddr *)&sockaddr, bits))
609 matched = 1;
610 case HM_HOST:
611 if (match(kline->host, client_p->orighost))
612 matched = 1;
613 }
614
615 if (!matched)
616 continue;
617 }
618
619 if(IsExemptKline(client_p))
620 {
621 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
622 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
623 get_client_name(client_p, HIDE_IP),
624 kline->user, kline->host);
625 continue;
626 }
627
628 sendto_realops_snomask(SNO_GENERAL, L_ALL,
629 "KLINE active for %s",
630 get_client_name(client_p, HIDE_IP));
631
632 notify_banned_client(client_p, kline, K_LINED);
633 }
634 }
635
636
637 /* check_dlines()
638 *
639 * inputs -
640 * outputs -
641 * side effects - all clients will be checked for dlines
642 */
643 void
644 check_dlines(void)
645 {
646 struct Client *client_p;
647 struct ConfItem *aconf;
648 rb_dlink_node *ptr;
649 rb_dlink_node *next_ptr;
650
651 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
652 {
653 client_p = ptr->data;
654
655 if(IsMe(client_p))
656 continue;
657
658 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, GET_SS_FAMILY(&client_p->localClient->ip))) != NULL)
659 {
660 if(aconf->status & CONF_EXEMPTDLINE)
661 continue;
662
663 sendto_realops_snomask(SNO_GENERAL, L_ALL,
664 "DLINE active for %s",
665 get_client_name(client_p, HIDE_IP));
666
667 notify_banned_client(client_p, aconf, D_LINED);
668 continue;
669 }
670 }
671
672 /* dlines need to be checked against unknowns too */
673 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
674 {
675 client_p = ptr->data;
676
677 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, GET_SS_FAMILY(&client_p->localClient->ip))) != NULL)
678 {
679 if(aconf->status & CONF_EXEMPTDLINE)
680 continue;
681
682 notify_banned_client(client_p, aconf, D_LINED);
683 }
684 }
685 }
686
687 /* check_xlines
688 *
689 * inputs -
690 * outputs -
691 * side effects - all clients will be checked for xlines
692 */
693 void
694 check_xlines(void)
695 {
696 struct Client *client_p;
697 struct ConfItem *aconf;
698 rb_dlink_node *ptr;
699 rb_dlink_node *next_ptr;
700
701 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
702 {
703 client_p = ptr->data;
704
705 if(IsMe(client_p) || !IsPerson(client_p))
706 continue;
707
708 if((aconf = find_xline(client_p->info, 1)) != NULL)
709 {
710 if(IsExemptKline(client_p))
711 {
712 sendto_realops_snomask(SNO_GENERAL, L_ALL,
713 "XLINE over-ruled for %s, client is kline_exempt [%s]",
714 get_client_name(client_p, HIDE_IP),
715 aconf->host);
716 continue;
717 }
718
719 sendto_realops_snomask(SNO_GENERAL, L_ALL, "XLINE active for %s",
720 get_client_name(client_p, HIDE_IP));
721
722 (void) exit_client(client_p, client_p, &me, "Bad user info");
723 continue;
724 }
725 }
726 }
727
728 /* resv_nick_fnc
729 *
730 * inputs - resv, reason, time
731 * outputs - NONE
732 * side effects - all local clients matching resv will be FNC'd
733 */
734 void
735 resv_nick_fnc(const char *mask, const char *reason, int temp_time)
736 {
737 struct Client *client_p, *target_p;
738 rb_dlink_node *ptr;
739 rb_dlink_node *next_ptr;
740 char *nick;
741 char note[NICKLEN+10];
742
743 if (!ConfigFileEntry.resv_fnc)
744 return;
745
746 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
747 {
748 client_p = ptr->data;
749
750 if(IsMe(client_p) || !IsPerson(client_p) || IsExemptResv(client_p))
751 continue;
752
753 /* Skip users that already have UID nicks. */
754 if(IsDigit(client_p->name[0]))
755 continue;
756
757 if(match_esc(mask, client_p->name))
758 {
759 nick = client_p->id;
760
761 /* Tell opers. */
762 sendto_realops_snomask(SNO_GENERAL, L_ALL,
763 "RESV forced nick change for %s!%s@%s to %s; nick matched [%s] (%s)",
764 client_p->name, client_p->username, client_p->host, nick, mask, reason);
765
766 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
767 "Nick change: From %s to %s [%s@%s]",
768 client_p->name, nick, client_p->username, client_p->host);
769
770 /* Tell the user. */
771 if (temp_time > 0)
772 {
773 sendto_one_notice(client_p,
774 ":*** Nick %s is temporarily unavailable on this server.",
775 client_p->name);
776 }
777 else
778 {
779 sendto_one_notice(client_p,
780 ":*** Nick %s is no longer available on this server.",
781 client_p->name);
782 }
783
784 /* Do all of the nick-changing gymnastics. */
785 client_p->tsinfo = rb_current_time();
786 whowas_add_history(client_p, 1);
787
788 monitor_signoff(client_p);
789
790 invalidate_bancache_user(client_p);
791
792 sendto_common_channels_local(client_p, NOCAPS, NOCAPS, ":%s!%s@%s NICK :%s",
793 client_p->name, client_p->username, client_p->host, nick);
794 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
795 use_id(client_p), nick, (long) client_p->tsinfo);
796
797 del_from_client_hash(client_p->name, client_p);
798 rb_strlcpy(client_p->name, nick, sizeof(client_p->name));
799 add_to_client_hash(nick, client_p);
800
801 monitor_signon(client_p);
802
803 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
804 {
805 target_p = ptr->data;
806 rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
807 rb_dlinkDestroy(ptr, &client_p->on_allow_list);
808 }
809
810 snprintf(note, sizeof(note), "Nick: %s", nick);
811 rb_note(client_p->localClient->F, note);
812 }
813 }
814 }
815
816 /*
817 * update_client_exit_stats
818 *
819 * input - pointer to client
820 * output - NONE
821 * side effects -
822 */
823 static void
824 update_client_exit_stats(struct Client *client_p)
825 {
826 if(IsServer(client_p))
827 {
828 sendto_realops_snomask(SNO_EXTERNAL, L_ALL,
829 "Server %s split from %s",
830 client_p->name, client_p->servptr->name);
831 if(HasSentEob(client_p))
832 eob_count--;
833 }
834 else if(IsClient(client_p))
835 {
836 --Count.total;
837 if(IsOper(client_p))
838 --Count.oper;
839 if(IsInvisible(client_p))
840 --Count.invisi;
841 }
842
843 if(splitchecking && !splitmode)
844 check_splitmode(NULL);
845 }
846
847 /*
848 * release_client_state
849 *
850 * input - pointer to client to release
851 * output - NONE
852 * side effects -
853 */
854 static void
855 release_client_state(struct Client *client_p)
856 {
857 if(client_p->user != NULL)
858 {
859 free_user(client_p->user, client_p); /* try this here */
860 }
861 if(client_p->serv)
862 {
863 if(client_p->serv->user != NULL)
864 free_user(client_p->serv->user, client_p);
865 if(client_p->serv->fullcaps)
866 rb_free(client_p->serv->fullcaps);
867 rb_free(client_p->serv);
868 }
869 }
870
871 /*
872 * remove_client_from_list
873 * inputs - point to client to remove
874 * output - NONE
875 * side effects - taken the code from ExitOneClient() for this
876 * and placed it here. - avalon
877 */
878 static void
879 remove_client_from_list(struct Client *client_p)
880 {
881 s_assert(NULL != client_p);
882
883 if(client_p == NULL)
884 return;
885
886 /* A client made with make_client()
887 * is on the unknown_list until removed.
888 * If it =does= happen to exit before its removed from that list
889 * and its =not= on the global_client_list, it will core here.
890 * short circuit that case now -db
891 */
892 if(client_p->node.prev == NULL && client_p->node.next == NULL)
893 return;
894
895 rb_dlinkDelete(&client_p->node, &global_client_list);
896
897 update_client_exit_stats(client_p);
898 }
899
900
901 /* clean_nick()
902 *
903 * input - nickname to check, flag for nick from local client
904 * output - 0 if erroneous, else 1
905 * side effects -
906 */
907 int
908 clean_nick(const char *nick, int loc_client)
909 {
910 int len = 0;
911
912 /* nicks cant start with a digit or -, and must have a length */
913 if(*nick == '-' || *nick == '\0')
914 return 0;
915
916 if(loc_client && IsDigit(*nick))
917 return 0;
918
919 for(; *nick; nick++)
920 {
921 len++;
922 if(!IsNickChar(*nick))
923 return 0;
924 }
925
926 /* nicklen is +1 */
927 if(len >= NICKLEN && (unsigned int)len >= ConfigFileEntry.nicklen)
928 return 0;
929
930 return 1;
931 }
932
933 /*
934 * find_person - find person by (nick)name.
935 * inputs - pointer to name
936 * output - return client pointer
937 * side effects -
938 */
939 struct Client *
940 find_person(const char *name)
941 {
942 struct Client *c2ptr;
943
944 c2ptr = find_client(name);
945
946 if(c2ptr && IsPerson(c2ptr))
947 return (c2ptr);
948 return (NULL);
949 }
950
951 struct Client *
952 find_named_person(const char *name)
953 {
954 struct Client *c2ptr;
955
956 c2ptr = find_named_client(name);
957
958 if(c2ptr && IsPerson(c2ptr))
959 return (c2ptr);
960 return (NULL);
961 }
962
963
964 /*
965 * find_chasing - find the client structure for a nick name (user)
966 * using history mechanism if necessary. If the client is not found,
967 * an error message (NO SUCH NICK) is generated. If the client was found
968 * through the history, chasing will be 1 and otherwise 0.
969 */
970 struct Client *
971 find_chasing(struct Client *source_p, const char *user, int *chasing)
972 {
973 struct Client *who;
974
975 if(MyClient(source_p))
976 who = find_named_person(user);
977 else
978 who = find_person(user);
979
980 if(chasing)
981 *chasing = 0;
982
983 if(who || IsDigit(*user))
984 return who;
985
986 if(!(who = whowas_get_history(user, (long) KILLCHASETIMELIMIT)))
987 {
988 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
989 form_str(ERR_NOSUCHNICK), user);
990 return (NULL);
991 }
992 if(chasing)
993 *chasing = 1;
994 return who;
995 }
996
997 /*
998 * get_client_name - Return the name of the client
999 * for various tracking and
1000 * admin purposes. The main purpose of this function is to
1001 * return the "socket host" name of the client, if that
1002 * differs from the advertised name (other than case).
1003 * But, this can be used to any client structure.
1004 *
1005 * NOTE 1:
1006 * Watch out the allocation of "nbuf", if either source_p->name
1007 * or source_p->sockhost gets changed into pointers instead of
1008 * directly allocated within the structure...
1009 *
1010 * NOTE 2:
1011 * Function return either a pointer to the structure (source_p) or
1012 * to internal buffer (nbuf). *NEVER* use the returned pointer
1013 * to modify what it points!!!
1014 */
1015
1016 const char *
1017 get_client_name(struct Client *client, int showip)
1018 {
1019 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
1020
1021 s_assert(NULL != client);
1022 if(client == NULL)
1023 return NULL;
1024
1025 if(MyConnect(client))
1026 {
1027 if(!irccmp(client->name, client->host))
1028 return client->name;
1029
1030 if(ConfigFileEntry.hide_spoof_ips &&
1031 showip == SHOW_IP && IsIPSpoof(client))
1032 showip = MASK_IP;
1033 if(IsAnyServer(client))
1034 showip = MASK_IP;
1035
1036 /* And finally, let's get the host information, ip or name */
1037 switch (showip)
1038 {
1039 case SHOW_IP:
1040 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
1041 client->name, client->username,
1042 client->sockhost);
1043 break;
1044 case MASK_IP:
1045 snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
1046 client->name, client->username);
1047 break;
1048 default:
1049 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
1050 client->name, client->username, client->host);
1051 }
1052 return nbuf;
1053 }
1054
1055 /* As pointed out by Adel Mezibra
1056 * Neph|l|m@EFnet. Was missing a return here.
1057 */
1058 return client->name;
1059 }
1060
1061 /* log_client_name()
1062 *
1063 * This version is the same as get_client_name, but doesnt contain the
1064 * code that will hide IPs always. This should be used for logfiles.
1065 */
1066 const char *
1067 log_client_name(struct Client *target_p, int showip)
1068 {
1069 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
1070
1071 if(target_p == NULL)
1072 return NULL;
1073
1074 if(MyConnect(target_p))
1075 {
1076 if(irccmp(target_p->name, target_p->host) == 0)
1077 return target_p->name;
1078
1079 switch (showip)
1080 {
1081 case SHOW_IP:
1082 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name,
1083 target_p->username, target_p->sockhost);
1084 break;
1085
1086 default:
1087 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name,
1088 target_p->username, target_p->host);
1089 }
1090
1091 return nbuf;
1092 }
1093
1094 return target_p->name;
1095 }
1096
1097 /* is_remote_connect - Returns whether a server was /connect'ed by a remote
1098 * oper (send notices netwide) */
1099 int
1100 is_remote_connect(struct Client *client_p)
1101 {
1102 struct Client *oper;
1103
1104 if (client_p->serv == NULL)
1105 return FALSE;
1106 oper = find_named_person(client_p->serv->by);
1107 return oper != NULL && IsOper(oper) && !MyConnect(oper);
1108 }
1109
1110 static void
1111 free_exited_clients(void *unused)
1112 {
1113 rb_dlink_node *ptr, *next;
1114 struct Client *target_p;
1115
1116 RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
1117 {
1118 target_p = ptr->data;
1119
1120 #ifdef DEBUG_EXITED_CLIENTS
1121 {
1122 struct abort_client *abt;
1123 rb_dlink_node *aptr;
1124 int found = 0;
1125
1126 RB_DLINK_FOREACH(aptr, abort_list.head)
1127 {
1128 abt = aptr->data;
1129 if(abt->client == target_p)
1130 {
1131 s_assert(0);
1132 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1133 "On abort_list: %s stat: %u flags: %llu handler: %c",
1134 target_p->name, (unsigned int) target_p->status,
1135 (unsigned long long)target_p->flags, target_p->handler);
1136 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1137 "Please report this to the charybdis developers!");
1138 found++;
1139 }
1140 }
1141
1142 if(found)
1143 {
1144 rb_dlinkDestroy(ptr, &dead_list);
1145 continue;
1146 }
1147 }
1148 #endif
1149
1150 if(ptr->data == NULL)
1151 {
1152 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1153 "Warning: null client on dead_list!");
1154 rb_dlinkDestroy(ptr, &dead_list);
1155 continue;
1156 }
1157 release_client_state(target_p);
1158 free_client(target_p);
1159 rb_dlinkDestroy(ptr, &dead_list);
1160 }
1161
1162 #ifdef DEBUG_EXITED_CLIENTS
1163 RB_DLINK_FOREACH_SAFE(ptr, next, dead_remote_list.head)
1164 {
1165 target_p = ptr->data;
1166
1167 if(ptr->data == NULL)
1168 {
1169 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1170 "Warning: null client on dead_list!");
1171 rb_dlinkDestroy(ptr, &dead_list);
1172 continue;
1173 }
1174 release_client_state(target_p);
1175 free_client(target_p);
1176 rb_dlinkDestroy(ptr, &dead_remote_list);
1177 }
1178 #endif
1179
1180 }
1181
1182 /*
1183 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1184 ** already been sent. we make sure to exit a server's dependent clients
1185 ** and servers before the server itself; exit_one_client takes care of
1186 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1187 */
1188 /*
1189 * added sanity test code.... source_p->serv might be NULL...
1190 */
1191 static void
1192 recurse_remove_clients(struct Client *source_p, const char *comment)
1193 {
1194 struct Client *target_p;
1195 rb_dlink_node *ptr, *ptr_next;
1196
1197 if(IsMe(source_p))
1198 return;
1199
1200 if(source_p->serv == NULL) /* oooops. uh this is actually a major bug */
1201 return;
1202
1203 /* this is very ugly, but it saves cpu :P */
1204 if(ConfigFileEntry.nick_delay > 0)
1205 {
1206 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1207 {
1208 target_p = ptr->data;
1209 target_p->flags |= FLAGS_KILLED;
1210 add_nd_entry(target_p->name);
1211
1212 if(!IsDead(target_p) && !IsClosing(target_p))
1213 exit_remote_client(NULL, target_p, &me, comment);
1214 }
1215 }
1216 else
1217 {
1218 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1219 {
1220 target_p = ptr->data;
1221 target_p->flags |= FLAGS_KILLED;
1222
1223 if(!IsDead(target_p) && !IsClosing(target_p))
1224 exit_remote_client(NULL, target_p, &me, comment);
1225 }
1226 }
1227
1228 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
1229 {
1230 target_p = ptr->data;
1231 recurse_remove_clients(target_p, comment);
1232 qs_server(NULL, target_p, &me, comment);
1233 }
1234 }
1235
1236 /*
1237 ** Remove *everything* that depends on source_p, from all lists, and sending
1238 ** all necessary SQUITs. source_p itself is still on the lists,
1239 ** and its SQUITs have been sent except for the upstream one -orabidoo
1240 */
1241 static void
1242 remove_dependents(struct Client *client_p,
1243 struct Client *source_p,
1244 struct Client *from, const char *comment, const char *comment1)
1245 {
1246 struct Client *to;
1247 rb_dlink_node *ptr, *next;
1248
1249 RB_DLINK_FOREACH_SAFE(ptr, next, serv_list.head)
1250 {
1251 to = ptr->data;
1252
1253 if(IsMe(to) || to == source_p->from || to == client_p)
1254 continue;
1255
1256 sendto_one(to, "SQUIT %s :%s", get_id(source_p, to), comment);
1257 }
1258
1259 recurse_remove_clients(source_p, comment1);
1260 }
1261
1262 void
1263 exit_aborted_clients(void *unused)
1264 {
1265 struct abort_client *abt;
1266 rb_dlink_node *ptr, *next;
1267 RB_DLINK_FOREACH_SAFE(ptr, next, abort_list.head)
1268 {
1269 abt = ptr->data;
1270
1271 #ifdef DEBUG_EXITED_CLIENTS
1272 {
1273 if(rb_dlinkFind(abt->client, &dead_list))
1274 {
1275 s_assert(0);
1276 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1277 "On dead_list: %s stat: %u flags: %llu handler: %c",
1278 abt->client->name, (unsigned int) abt->client->status,
1279 (unsigned long long)abt->client->flags, abt->client->handler);
1280 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1281 "Please report this to the charybdis developers!");
1282 continue;
1283 }
1284 }
1285 #endif
1286
1287 s_assert(*((unsigned long*)abt->client) != 0xdeadbeef); /* This is lame but its a debug thing */
1288 rb_dlinkDelete(ptr, &abort_list);
1289
1290 if(IsAnyServer(abt->client))
1291 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1292 "Closing link to %s: %s",
1293 abt->client->name, abt->notice);
1294
1295 /* its no longer on abort list - we *must* remove
1296 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1297 */
1298 abt->client->flags &= ~FLAGS_CLOSING;
1299 exit_client(abt->client, abt->client, &me, abt->notice);
1300 rb_free(abt);
1301 }
1302 }
1303
1304
1305 /*
1306 * dead_link - Adds client to a list of clients that need an exit_client()
1307 */
1308 void
1309 dead_link(struct Client *client_p, int sendqex)
1310 {
1311 struct abort_client *abt;
1312
1313 s_assert(!IsMe(client_p));
1314 if(IsDead(client_p) || IsClosing(client_p) || IsMe(client_p))
1315 return;
1316
1317 abt = (struct abort_client *) rb_malloc(sizeof(struct abort_client));
1318
1319 if(sendqex)
1320 rb_strlcpy(abt->notice, "Max SendQ exceeded", sizeof(abt->notice));
1321 else
1322 snprintf(abt->notice, sizeof(abt->notice), "Write error: %s", strerror(errno));
1323
1324 abt->client = client_p;
1325 SetIOError(client_p);
1326 SetDead(client_p);
1327 SetClosing(client_p);
1328 rb_dlinkAdd(abt, &abt->node, &abort_list);
1329 }
1330
1331
1332 /* This does the remove of the user from channels..local or remote */
1333 static inline void
1334 exit_generic_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1335 const char *comment)
1336 {
1337 rb_dlink_node *ptr, *next_ptr;
1338
1339 if(IsOper(source_p))
1340 rb_dlinkFindDestroy(source_p, &oper_list);
1341
1342 sendto_common_channels_local(source_p, NOCAPS, NOCAPS, ":%s!%s@%s QUIT :%s",
1343 source_p->name,
1344 source_p->username, source_p->host, comment);
1345
1346 remove_user_from_channels(source_p);
1347
1348 /* Should not be in any channels now */
1349 s_assert(source_p->user->channel.head == NULL);
1350
1351 /* Clean up invitefield */
1352 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->user->invited.head)
1353 {
1354 del_invite(ptr->data, source_p);
1355 }
1356
1357 /* Clean up allow lists */
1358 del_all_accepts(source_p);
1359
1360 whowas_add_history(source_p, 0);
1361 whowas_off_history(source_p);
1362
1363 monitor_signoff(source_p);
1364
1365 if(has_id(source_p))
1366 del_from_id_hash(source_p->id, source_p);
1367
1368 del_from_hostname_hash(source_p->orighost, source_p);
1369 del_from_client_hash(source_p->name, source_p);
1370 remove_client_from_list(source_p);
1371 }
1372
1373 /*
1374 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1375 */
1376
1377 static int
1378 exit_remote_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1379 const char *comment)
1380 {
1381 exit_generic_client(client_p, source_p, from, comment);
1382
1383 if(source_p->servptr && source_p->servptr->serv)
1384 {
1385 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users);
1386 }
1387
1388 if((source_p->flags & FLAGS_KILLED) == 0)
1389 {
1390 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1391 ":%s QUIT :%s", use_id(source_p), comment);
1392 }
1393
1394 SetDead(source_p);
1395 #ifdef DEBUG_EXITED_CLIENTS
1396 rb_dlinkAddAlloc(source_p, &dead_remote_list);
1397 #else
1398 rb_dlinkAddAlloc(source_p, &dead_list);
1399 #endif
1400 return(CLIENT_EXITED);
1401 }
1402
1403 /*
1404 * This assumes IsUnknown(source_p) == true and MyConnect(source_p) == true
1405 */
1406
1407 static int
1408 exit_unknown_client(struct Client *client_p, /* The local client originating the
1409 * exit or NULL, if this exit is
1410 * generated by this server for
1411 * internal reasons.
1412 * This will not get any of the
1413 * generated messages. */
1414 struct Client *source_p, /* Client exiting */
1415 struct Client *from, /* Client firing off this Exit,
1416 * never NULL! */
1417 const char *comment)
1418 {
1419 authd_abort_client(source_p);
1420 rb_dlinkDelete(&source_p->localClient->tnode, &unknown_list);
1421
1422 if(!IsIOError(source_p))
1423 sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
1424 source_p->user != NULL ? source_p->host : "127.0.0.1",
1425 comment);
1426
1427 close_connection(source_p);
1428
1429 if(has_id(source_p))
1430 del_from_id_hash(source_p->id, source_p);
1431
1432 del_from_hostname_hash(source_p->host, source_p);
1433 if (!IsAnyServer(source_p))
1434 del_from_client_hash(source_p->name, source_p);
1435 remove_client_from_list(source_p);
1436 SetDead(source_p);
1437 rb_dlinkAddAlloc(source_p, &dead_list);
1438
1439 /* Note that we don't need to add unknowns to the dead_list */
1440 return(CLIENT_EXITED);
1441 }
1442
1443 static int
1444 exit_remote_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1445 const char *comment)
1446 {
1447 static char comment1[(HOSTLEN*2)+2];
1448 static char newcomment[BUFSIZE];
1449 struct Client *target_p;
1450
1451 if(ConfigServerHide.flatten_links)
1452 strcpy(comment1, "*.net *.split");
1453 else
1454 {
1455 strcpy(comment1, source_p->servptr->name);
1456 strcat(comment1, " ");
1457 strcat(comment1, source_p->name);
1458 }
1459 if (IsPerson(from))
1460 snprintf(newcomment, sizeof(newcomment), "by %s: %s",
1461 from->name, comment);
1462
1463 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1464
1465 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1466
1467 rb_dlinkFindDestroy(source_p, &global_serv_list);
1468 target_p = source_p->from;
1469
1470 if(target_p != NULL && IsServer(target_p) && target_p != client_p &&
1471 !IsMe(target_p) && (source_p->flags & FLAGS_KILLED) == 0)
1472 {
1473 sendto_one(target_p, ":%s SQUIT %s :%s",
1474 get_id(from, target_p), get_id(source_p, target_p),
1475 comment);
1476 }
1477
1478 if(has_id(source_p))
1479 del_from_id_hash(source_p->id, source_p);
1480
1481 del_from_client_hash(source_p->name, source_p);
1482 remove_client_from_list(source_p);
1483
1484 scache_split(source_p->serv->nameinfo);
1485
1486 SetDead(source_p);
1487 #ifdef DEBUG_EXITED_CLIENTS
1488 rb_dlinkAddAlloc(source_p, &dead_remote_list);
1489 #else
1490 rb_dlinkAddAlloc(source_p, &dead_list);
1491 #endif
1492 return 0;
1493 }
1494
1495 static int
1496 qs_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1497 const char *comment)
1498 {
1499 if(source_p->servptr && source_p->servptr->serv)
1500 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1501 else
1502 s_assert(0);
1503
1504 rb_dlinkFindDestroy(source_p, &global_serv_list);
1505
1506 if(has_id(source_p))
1507 del_from_id_hash(source_p->id, source_p);
1508
1509 del_from_client_hash(source_p->name, source_p);
1510 remove_client_from_list(source_p);
1511 scache_split(source_p->serv->nameinfo);
1512
1513 SetDead(source_p);
1514 rb_dlinkAddAlloc(source_p, &dead_list);
1515 return 0;
1516 }
1517
1518 static int
1519 exit_local_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1520 const char *comment)
1521 {
1522 static char comment1[(HOSTLEN*2)+2];
1523 static char newcomment[BUFSIZE];
1524 unsigned int sendk, recvk;
1525
1526 rb_dlinkDelete(&source_p->localClient->tnode, &serv_list);
1527 rb_dlinkFindDestroy(source_p, &global_serv_list);
1528
1529 sendk = source_p->localClient->sendK;
1530 recvk = source_p->localClient->receiveK;
1531
1532 /* Always show source here, so the server notices show
1533 * which side initiated the split -- jilles
1534 */
1535 snprintf(newcomment, sizeof(newcomment), "by %s: %s",
1536 from == source_p ? me.name : from->name, comment);
1537 if (!IsIOError(source_p))
1538 sendto_one(source_p, "SQUIT %s :%s", use_id(source_p),
1539 newcomment);
1540 if(client_p != NULL && source_p != client_p && !IsIOError(source_p))
1541 {
1542 sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1543 source_p->name, comment);
1544 }
1545
1546 if(source_p->servptr && source_p->servptr->serv)
1547 rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1548 else
1549 s_assert(0);
1550
1551
1552 close_connection(source_p);
1553
1554 if(ConfigServerHide.flatten_links)
1555 strcpy(comment1, "*.net *.split");
1556 else
1557 {
1558 strcpy(comment1, source_p->servptr->name);
1559 strcat(comment1, " ");
1560 strcat(comment1, source_p->name);
1561 }
1562
1563 if(source_p->serv != NULL)
1564 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1565
1566 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s was connected"
1567 " for %ld seconds. %d/%d sendK/recvK.",
1568 source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
1569
1570 ilog(L_SERVER, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1571 source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
1572
1573 if(has_id(source_p))
1574 del_from_id_hash(source_p->id, source_p);
1575
1576 del_from_client_hash(source_p->name, source_p);
1577 remove_client_from_list(source_p);
1578 scache_split(source_p->serv->nameinfo);
1579
1580 SetDead(source_p);
1581 rb_dlinkAddAlloc(source_p, &dead_list);
1582 return 0;
1583 }
1584
1585
1586 /*
1587 * This assumes IsPerson(source_p) == true && MyConnect(source_p) == true
1588 */
1589
1590 static int
1591 exit_local_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1592 const char *comment)
1593 {
1594 unsigned long on_for;
1595 char tbuf[26];
1596
1597 exit_generic_client(client_p, source_p, from, comment);
1598 clear_monitor(source_p);
1599
1600 s_assert(IsPerson(source_p));
1601 rb_dlinkDelete(&source_p->localClient->tnode, &lclient_list);
1602 rb_dlinkDelete(&source_p->lnode, &me.serv->users);
1603
1604 if(IsOper(source_p))
1605 rb_dlinkFindDestroy(source_p, &local_oper_list);
1606
1607 sendto_realops_snomask(SNO_CCONN, L_ALL,
1608 "Client exiting: %s (%s@%s) [%s] [%s]",
1609 source_p->name,
1610 source_p->username, source_p->host, comment,
1611 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255");
1612
1613 sendto_realops_snomask(SNO_CCONNEXT, L_ALL,
1614 "CLIEXIT %s %s %s %s 0 %s",
1615 source_p->name, source_p->username, source_p->host,
1616 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255",
1617 comment);
1618
1619 on_for = rb_current_time() - source_p->localClient->firsttime;
1620
1621 ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %s %d/%d",
1622 rb_ctime(rb_current_time(), tbuf, sizeof(tbuf)), on_for / 3600,
1623 (on_for % 3600) / 60, on_for % 60,
1624 source_p->name, source_p->username, source_p->host,
1625 source_p->sockhost,
1626 source_p->localClient->sendK, source_p->localClient->receiveK);
1627
1628 sendto_one(source_p, "ERROR :Closing Link: %s (%s)", source_p->host, comment);
1629 close_connection(source_p);
1630
1631 if((source_p->flags & FLAGS_KILLED) == 0)
1632 {
1633 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1634 ":%s QUIT :%s", use_id(source_p), comment);
1635 }
1636
1637 SetDead(source_p);
1638 rb_dlinkAddAlloc(source_p, &dead_list);
1639 return(CLIENT_EXITED);
1640 }
1641
1642
1643 /*
1644 ** exit_client - This is old "m_bye". Name changed, because this is not a
1645 ** protocol function, but a general server utility function.
1646 **
1647 ** This function exits a client of *any* type (user, server, etc)
1648 ** from this server. Also, this generates all necessary prototol
1649 ** messages that this exit may cause.
1650 **
1651 ** 1) If the client is a local client, then this implicitly
1652 ** exits all other clients depending on this connection (e.g.
1653 ** remote clients having 'from'-field that points to this.
1654 **
1655 ** 2) If the client is a remote client, then only this is exited.
1656 **
1657 ** For convenience, this function returns a suitable value for
1658 ** m_function return value:
1659 **
1660 ** CLIENT_EXITED if (client_p == source_p)
1661 ** 0 if (client_p != source_p)
1662 */
1663 int
1664 exit_client(struct Client *client_p, /* The local client originating the
1665 * exit or NULL, if this exit is
1666 * generated by this server for
1667 * internal reasons.
1668 * This will not get any of the
1669 * generated messages. */
1670 struct Client *source_p, /* Client exiting */
1671 struct Client *from, /* Client firing off this Exit,
1672 * never NULL! */
1673 const char *comment /* Reason for the exit */
1674 )
1675 {
1676 int ret = -1;
1677
1678 hook_data_client_exit hdata;
1679 if(IsClosing(source_p))
1680 return -1;
1681
1682 /* note, this HAS to be here, when we exit a client we attempt to
1683 * send them data, if this generates a write error we must *not* add
1684 * them to the abort list --fl
1685 */
1686 SetClosing(source_p);
1687
1688 hdata.local_link = client_p;
1689 hdata.target = source_p;
1690 hdata.from = from;
1691 hdata.comment = comment;
1692 call_hook(h_client_exit, &hdata);
1693
1694 if(MyConnect(source_p))
1695 {
1696 /* Local clients of various types */
1697 if(IsPerson(source_p))
1698 ret = exit_local_client(client_p, source_p, from, comment);
1699 else if(IsServer(source_p))
1700 ret = exit_local_server(client_p, source_p, from, comment);
1701 /* IsUnknown || IsConnecting || IsHandShake */
1702 else if(!IsReject(source_p))
1703 ret = exit_unknown_client(client_p, source_p, from, comment);
1704 }
1705 else
1706 {
1707 /* Remotes */
1708 if(IsPerson(source_p))
1709 ret = exit_remote_client(client_p, source_p, from, comment);
1710 else if(IsServer(source_p))
1711 ret = exit_remote_server(client_p, source_p, from, comment);
1712 }
1713
1714 call_hook(h_after_client_exit, NULL);
1715
1716 return ret;
1717 }
1718
1719 /*
1720 * Count up local client memory
1721 */
1722
1723 /* XXX one common Client list now */
1724 void
1725 count_local_client_memory(size_t * count, size_t * local_client_memory_used)
1726 {
1727 size_t lusage;
1728 rb_bh_usage(lclient_heap, count, NULL, &lusage, NULL);
1729 *local_client_memory_used = lusage + (*count * (sizeof(void *) + sizeof(struct Client)));
1730 }
1731
1732 /*
1733 * Count up remote client memory
1734 */
1735 void
1736 count_remote_client_memory(size_t * count, size_t * remote_client_memory_used)
1737 {
1738 size_t lcount, rcount;
1739 rb_bh_usage(lclient_heap, &lcount, NULL, NULL, NULL);
1740 rb_bh_usage(client_heap, &rcount, NULL, NULL, NULL);
1741 *count = rcount - lcount;
1742 *remote_client_memory_used = *count * (sizeof(void *) + sizeof(struct Client));
1743 }
1744
1745
1746 /*
1747 * accept processing, this adds a form of "caller ID" to ircd
1748 *
1749 * If a client puts themselves into "caller ID only" mode,
1750 * only clients that match a client pointer they have put on
1751 * the accept list will be allowed to message them.
1752 *
1753 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1754 *
1755 * [target.allow_list] -> [ source1 ] -> [source2 ]
1756 *
1757 * i.e. a target will have a link list of source pointers it will allow
1758 * each source client then has a back pointer pointing back
1759 * to the client that has it on its accept list.
1760 * This allows for exit_one_client to remove these now bogus entries
1761 * from any client having an accept on them.
1762 */
1763 /*
1764 * del_all_accepts
1765 *
1766 * inputs - pointer to exiting client
1767 * output - NONE
1768 * side effects - Walk through given clients allow_list and on_allow_list
1769 * remove all references to this client
1770 */
1771 void
1772 del_all_accepts(struct Client *client_p)
1773 {
1774 rb_dlink_node *ptr;
1775 rb_dlink_node *next_ptr;
1776 struct Client *target_p;
1777
1778 if(MyClient(client_p) && client_p->localClient->allow_list.head)
1779 {
1780 /* clear this clients accept list, and remove them from
1781 * everyones on_accept_list
1782 */
1783 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head)
1784 {
1785 target_p = ptr->data;
1786 rb_dlinkFindDestroy(client_p, &target_p->on_allow_list);
1787 rb_dlinkDestroy(ptr, &client_p->localClient->allow_list);
1788 }
1789 }
1790
1791 /* remove this client from everyones accept list */
1792 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
1793 {
1794 target_p = ptr->data;
1795 rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
1796 rb_dlinkDestroy(ptr, &client_p->on_allow_list);
1797 }
1798 }
1799
1800 /*
1801 * show_ip() - asks if the true IP should be shown when source is
1802 * asking for info about target
1803 *
1804 * Inputs - source_p who is asking
1805 * - target_p who do we want the info on
1806 * Output - returns 1 if clear IP can be shown, otherwise 0
1807 * Side Effects - none
1808 */
1809
1810 int
1811 show_ip(struct Client *source_p, struct Client *target_p)
1812 {
1813 if(IsAnyServer(target_p))
1814 {
1815 return 0;
1816 }
1817 else if(IsIPSpoof(target_p))
1818 {
1819 /* source == NULL indicates message is being sent
1820 * to local opers.
1821 */
1822 if(!ConfigFileEntry.hide_spoof_ips &&
1823 (source_p == NULL || MyOper(source_p)))
1824 return 1;
1825 return 0;
1826 }
1827 else if(IsDynSpoof(target_p) && (source_p != NULL && !IsOper(source_p)))
1828 return 0;
1829 else
1830 return 1;
1831 }
1832
1833 int
1834 show_ip_conf(struct ConfItem *aconf, struct Client *source_p)
1835 {
1836 if(IsConfDoSpoofIp(aconf))
1837 {
1838 if(!ConfigFileEntry.hide_spoof_ips && MyOper(source_p))
1839 return 1;
1840
1841 return 0;
1842 }
1843 else
1844 return 1;
1845 }
1846
1847 int
1848 show_ip_whowas(struct Whowas *whowas, struct Client *source_p)
1849 {
1850 if(whowas->flags & WHOWAS_IP_SPOOFING)
1851 if(ConfigFileEntry.hide_spoof_ips || !MyOper(source_p))
1852 return 0;
1853 if(whowas->flags & WHOWAS_DYNSPOOF)
1854 if(!IsOper(source_p))
1855 return 0;
1856 return 1;
1857 }
1858
1859 /*
1860 * make_user
1861 *
1862 * inputs - pointer to client struct
1863 * output - pointer to struct User
1864 * side effects - add's an User information block to a client
1865 * if it was not previously allocated.
1866 */
1867 struct User *
1868 make_user(struct Client *client_p)
1869 {
1870 struct User *user;
1871
1872 user = client_p->user;
1873 if(!user)
1874 {
1875 user = (struct User *) rb_bh_alloc(user_heap);
1876 user->refcnt = 1;
1877 client_p->user = user;
1878 }
1879 return user;
1880 }
1881
1882 /*
1883 * make_server
1884 *
1885 * inputs - pointer to client struct
1886 * output - pointer to server_t
1887 * side effects - add's an Server information block to a client
1888 * if it was not previously allocated.
1889 */
1890 struct Server *
1891 make_server(struct Client *client_p)
1892 {
1893 struct Server *serv = client_p->serv;
1894
1895 if(!serv)
1896 {
1897 serv = (struct Server *) rb_malloc(sizeof(struct Server));
1898 client_p->serv = serv;
1899 }
1900 return client_p->serv;
1901 }
1902
1903 /*
1904 * free_user
1905 *
1906 * inputs - pointer to user struct
1907 * - pointer to client struct
1908 * output - none
1909 * side effects - Decrease user reference count by one and release block,
1910 * if count reaches 0
1911 */
1912 void
1913 free_user(struct User *user, struct Client *client_p)
1914 {
1915 free_away(client_p);
1916
1917 if(--user->refcnt <= 0)
1918 {
1919 if(user->away)
1920 rb_free((char *) user->away);
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 }