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