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