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