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