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