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