/[svn]/ircd-hybrid-7.2/src/client.c
ViewVC logotype

Contents of /ircd-hybrid-7.2/src/client.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 503 - (show annotations)
Fri Mar 3 19:53:47 2006 UTC (15 years, 1 month ago) by michael
File MIME type: text/x-chdr
File size: 42393 byte(s)
- Backported CAP changes from HEAD since it doesn't affect
  any of the ircd's core components and should be supported
  as soon as possible.

1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * client.c: Controls clients.
4 *
5 * Copyright (C) 2002 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 #include "stdinc.h"
26 #include "tools.h"
27 #include "client.h"
28 #include "channel_mode.h"
29 #include "common.h"
30 #include "event.h"
31 #include "fdlist.h"
32 #include "hash.h"
33 #include "irc_string.h"
34 #include "sprintf_irc.h"
35 #include "ircd.h"
36 #include "list.h"
37 #include "s_gline.h"
38 #include "numeric.h"
39 #include "packet.h"
40 #include "s_auth.h"
41 #include "s_bsd.h"
42 #include "s_conf.h"
43 #include "s_log.h"
44 #include "s_misc.h"
45 #include "s_serv.h"
46 #include "send.h"
47 #include "whowas.h"
48 #include "s_user.h"
49 #include "dbuf.h"
50 #include "memory.h"
51 #include "hostmask.h"
52 #include "balloc.h"
53 #include "listener.h"
54 #include "irc_res.h"
55 #include "userhost.h"
56
57 dlink_list listing_client_list = { NULL, NULL, 0 };
58 /* Pointer to beginning of Client list */
59 dlink_list global_client_list = {NULL, NULL, 0};
60 /* unknown/client pointer lists */
61 dlink_list unknown_list = {NULL, NULL, 0};
62 dlink_list local_client_list = {NULL, NULL, 0};
63 dlink_list serv_list = {NULL, NULL, 0};
64 dlink_list global_serv_list = {NULL, NULL, 0};
65 dlink_list oper_list = {NULL, NULL, 0};
66
67 static EVH check_pings;
68
69 static BlockHeap *client_heap = NULL;
70 static BlockHeap *lclient_heap = NULL;
71
72 static dlink_list dead_list = { NULL, NULL, 0};
73 static dlink_list abort_list = { NULL, NULL, 0};
74
75 static dlink_node *eac_next; /* next aborted client to exit */
76
77 static void check_pings_list(dlink_list *);
78 static void check_unknowns_list(void);
79 static void ban_them(struct Client *client_p, struct ConfItem *conf);
80
81
82 /* init_client()
83 *
84 * inputs - NONE
85 * output - NONE
86 * side effects - initialize client free memory
87 */
88 void
89 init_client(void)
90 {
91 /* start off the check ping event .. -- adrian
92 * Every 30 seconds is plenty -- db
93 */
94 client_heap = BlockHeapCreate("client", sizeof(struct Client), CLIENT_HEAP_SIZE);
95 lclient_heap = BlockHeapCreate("local client", sizeof(struct LocalUser), LCLIENT_HEAP_SIZE);
96 eventAdd("check_pings", check_pings, NULL, 5);
97 }
98
99 /*
100 * make_client - create a new Client struct and set it to initial state.
101 *
102 * from == NULL, create local client (a client connected
103 * to a socket).
104 * WARNING: This leaves the client in a dangerous
105 * state where fd == -1, dead flag is not set and
106 * the client is on the unknown_list; therefore,
107 * the first thing to do after calling make_client(NULL)
108 * is setting fd to something reasonable. -adx
109 *
110 * from, create remote client (behind a socket
111 * associated with the client defined by
112 * 'from'). ('from' is a local client!!).
113 */
114 struct Client *
115 make_client(struct Client *from)
116 {
117 struct Client *client_p = BlockHeapAlloc(client_heap);
118
119 if (from == NULL)
120 {
121 client_p->from = client_p; /* 'from' of local client is self! */
122 client_p->since = client_p->lasttime = client_p->firsttime = CurrentTime;
123
124 client_p->localClient = BlockHeapAlloc(lclient_heap);
125 client_p->localClient->registration = REG_INIT;
126 /* as good a place as any... */
127 dlinkAdd(client_p, make_dlink_node(), &unknown_list);
128 }
129 else
130 client_p->from = from; /* 'from' of local client is self! */
131
132 client_p->hnext = client_p;
133 client_p->status = STAT_UNKNOWN;
134 strcpy(client_p->username, "unknown");
135
136 return client_p;
137 }
138
139 /*
140 * free_client
141 *
142 * inputs - pointer to client
143 * output - NONE
144 * side effects - client pointed to has its memory freed
145 */
146 static void
147 free_client(struct Client *client_p)
148 {
149 assert(client_p != NULL);
150 assert(client_p != &me);
151 assert(client_p->hnext == client_p);
152 assert(client_p->channel.head == NULL);
153 assert(dlink_list_length(&client_p->channel) == 0);
154
155 MyFree(client_p->away);
156 MyFree(client_p->serv);
157
158 if (MyConnect(client_p))
159 {
160 assert(client_p->localClient->invited.head == NULL);
161 assert(dlink_list_length(&client_p->localClient->invited) == 0);
162 assert(IsClosing(client_p) && IsDead(client_p));
163
164 MyFree(client_p->localClient->response);
165 MyFree(client_p->localClient->auth_oper);
166
167 /*
168 * clean up extra sockets from P-lines which have been discarded.
169 */
170 if (client_p->localClient->listener)
171 {
172 assert(0 < client_p->localClient->listener->ref_count);
173 if (0 == --client_p->localClient->listener->ref_count &&
174 !client_p->localClient->listener->active)
175 free_listener(client_p->localClient->listener);
176 }
177
178 dbuf_clear(&client_p->localClient->buf_recvq);
179 dbuf_clear(&client_p->localClient->buf_sendq);
180
181 BlockHeapFree(lclient_heap, client_p->localClient);
182 }
183
184 BlockHeapFree(client_heap, client_p);
185 }
186
187 /*
188 * check_pings - go through the local client list and check activity
189 * kill off stuff that should die
190 *
191 * inputs - NOT USED (from event)
192 * output - next time_t when check_pings() should be called again
193 * side effects -
194 *
195 *
196 * A PING can be sent to clients as necessary.
197 *
198 * Client/Server ping outs are handled.
199 */
200
201 /*
202 * Addon from adrian. We used to call this after nextping seconds,
203 * however I've changed it to run once a second. This is only for
204 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
205 * run once a second makes life a lot easier - when a new client connects
206 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
207 * we end up waiting 20 seconds. This is stupid. :-)
208 * I will optimise (hah!) check_pings() once I've finished working on
209 * tidying up other network IO evilnesses.
210 * -- adrian
211 */
212
213 static void
214 check_pings(void *notused)
215 {
216 check_pings_list(&local_client_list);
217 check_pings_list(&serv_list);
218 check_unknowns_list();
219 }
220
221 /* check_pings_list()
222 *
223 * inputs - pointer to list to check
224 * output - NONE
225 * side effects -
226 */
227 static void
228 check_pings_list(dlink_list *list)
229 {
230 char scratch[32]; /* way too generous but... */
231 struct Client *client_p; /* current local client_p being examined */
232 int ping, pingwarn; /* ping time value from client */
233 dlink_node *ptr, *next_ptr;
234
235 DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
236 {
237 client_p = ptr->data;
238
239 /*
240 ** Note: No need to notify opers here. It's
241 ** already done when "FLAGS_DEADSOCKET" is set.
242 */
243 if (IsDead(client_p))
244 {
245 /* Ignore it, its been exited already */
246 continue;
247 }
248
249 if (client_p->localClient->reject_delay > 0)
250 {
251 if (client_p->localClient->reject_delay <= CurrentTime)
252 exit_client(client_p, &me, "Rejected");
253 continue;
254 }
255
256 if (GlobalSetOptions.idletime && IsClient(client_p))
257 {
258 if (!IsExemptKline(client_p) && !IsOper(client_p) &&
259 !IsIdlelined(client_p) &&
260 ((CurrentTime - client_p->localClient->last) > GlobalSetOptions.idletime))
261 {
262 struct ConfItem *conf;
263 struct AccessItem *aconf;
264
265 conf = make_conf_item(KLINE_TYPE);
266 aconf = (struct AccessItem *)map_to_conf(conf);
267
268 DupString(aconf->host, client_p->host);
269 DupString(aconf->reason, "idle exceeder");
270 DupString(aconf->user, client_p->username);
271 aconf->hold = CurrentTime + 60;
272 add_temp_line(conf);
273
274 sendto_realops_flags(UMODE_ALL, L_ALL,
275 "Idle time limit exceeded for %s - temp k-lining",
276 get_client_name(client_p, HIDE_IP));
277 exit_client(client_p, &me, aconf->reason);
278 continue;
279 }
280 }
281
282 if (!IsRegistered(client_p))
283 ping = CONNECTTIMEOUT, pingwarn = 0;
284 else
285 ping = get_client_ping(client_p, &pingwarn);
286
287 if (ping < CurrentTime - client_p->lasttime)
288 {
289 if (!IsPingSent(client_p))
290 {
291 /*
292 * if we havent PINGed the connection and we havent
293 * heard from it in a while, PING it to make sure
294 * it is still alive.
295 */
296 SetPingSent(client_p);
297 ClearPingWarning(client_p);
298 client_p->lasttime = CurrentTime - ping;
299 sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
300 }
301 else
302 {
303 if (CurrentTime - client_p->lasttime >= 2 * ping)
304 {
305 /*
306 * If the client/server hasn't talked to us in 2*ping seconds
307 * and it has a ping time, then close its connection.
308 */
309 if (IsServer(client_p) || IsHandshake(client_p))
310 {
311 sendto_realops_flags(UMODE_ALL, L_ADMIN,
312 "No response from %s, closing link",
313 get_client_name(client_p, HIDE_IP));
314 sendto_realops_flags(UMODE_ALL, L_OPER,
315 "No response from %s, closing link",
316 get_client_name(client_p, MASK_IP));
317 ilog(L_NOTICE, "No response from %s, closing link",
318 get_client_name(client_p, HIDE_IP));
319 }
320 ircsprintf(scratch, "Ping timeout: %d seconds",
321 (int)(CurrentTime - client_p->lasttime));
322
323 exit_client(client_p, &me, scratch);
324 }
325 else if (!IsPingWarning(client_p) && pingwarn > 0 &&
326 (IsServer(client_p) || IsHandshake(client_p)) &&
327 CurrentTime - client_p->lasttime >= ping + pingwarn)
328 {
329 /*
330 * If the server hasn't replied in pingwarn seconds after sending
331 * the PING, notify the opers so that they are aware of the problem.
332 */
333 SetPingWarning(client_p);
334 sendto_realops_flags(UMODE_ALL, L_ADMIN,
335 "Warning, no response from %s in %d seconds",
336 get_client_name(client_p, HIDE_IP), pingwarn);
337 sendto_realops_flags(UMODE_ALL, L_OPER,
338 "Warning, no response from %s in %d seconds",
339 get_client_name(client_p, MASK_IP), pingwarn);
340 ilog(L_NOTICE, "No response from %s in %d seconds",
341 get_client_name(client_p, HIDE_IP), pingwarn);
342 }
343 }
344 }
345 }
346 }
347
348 /* check_unknowns_list()
349 *
350 * inputs - pointer to list of unknown clients
351 * output - NONE
352 * side effects - unknown clients get marked for termination after n seconds
353 */
354 static void
355 check_unknowns_list(void)
356 {
357 dlink_node *ptr, *next_ptr;
358
359 DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
360 {
361 struct Client *client_p = ptr->data;
362
363 if (client_p->localClient->reject_delay > 0)
364 {
365 if (client_p->localClient->reject_delay <= CurrentTime)
366 exit_client(client_p, &me, "Rejected");
367 continue;
368 }
369
370 /* Check UNKNOWN connections - if they have been in this state
371 * for > 30s, close them.
372 */
373 if (client_p->firsttime ? ((CurrentTime - client_p->firsttime) > 30) : 0)
374 exit_client(client_p, &me, "Connection timed out");
375 }
376 }
377
378 /* check_conf_klines()
379 *
380 * inputs - NONE
381 * output - NONE
382 * side effects - Check all connections for a pending kline against the
383 * client, exit the client if a kline matches.
384 */
385 void
386 check_conf_klines(void)
387 {
388 struct Client *client_p = NULL; /* current local client_p being examined */
389 struct AccessItem *aconf = NULL;
390 struct ConfItem *conf = NULL;
391 dlink_node *ptr, *next_ptr;
392
393 DLINK_FOREACH_SAFE(ptr, next_ptr, local_client_list.head)
394 {
395 client_p = ptr->data;
396
397 /* If a client is already being exited
398 */
399 if (IsDead(client_p) || !IsClient(client_p))
400 continue;
401
402 /* if there is a returned struct ConfItem then kill it */
403 if ((aconf = find_dline_conf(&client_p->localClient->ip,
404 client_p->localClient->aftype)) != NULL)
405 {
406 if (aconf->status & CONF_EXEMPTDLINE)
407 continue;
408
409 conf = unmap_conf_item(aconf);
410 ban_them(client_p, conf);
411 continue; /* and go examine next fd/client_p */
412 }
413
414 if (ConfigFileEntry.glines && (aconf = find_gline(client_p)))
415 {
416 if (IsExemptKline(client_p) ||
417 IsExemptGline(client_p))
418 {
419 sendto_realops_flags(UMODE_ALL, L_ALL,
420 "GLINE over-ruled for %s, client is %sline_exempt",
421 get_client_name(client_p, HIDE_IP), IsExemptKline(client_p) ? "k" : "g");
422 continue;
423 }
424
425 conf = unmap_conf_item(aconf);
426 ban_them(client_p, conf);
427 /* and go examine next fd/client_p */
428 continue;
429 }
430
431 if ((aconf = find_kill(client_p)) != NULL)
432 {
433
434 /* if there is a returned struct AccessItem.. then kill it */
435 if (IsExemptKline(client_p))
436 {
437 sendto_realops_flags(UMODE_ALL, L_ALL,
438 "KLINE over-ruled for %s, client is kline_exempt",
439 get_client_name(client_p, HIDE_IP));
440 continue;
441 }
442
443 conf = unmap_conf_item(aconf);
444 ban_them(client_p, conf);
445 continue;
446 }
447
448 /* if there is a returned struct MatchItem then kill it */
449 if ((conf = find_matching_name_conf(XLINE_TYPE, client_p->info,
450 NULL, NULL, 0)) != NULL ||
451 (conf = find_matching_name_conf(RXLINE_TYPE, client_p->info,
452 NULL, NULL, 0)) != NULL)
453 {
454 ban_them(client_p, conf);
455 continue;
456 }
457 }
458
459 /* also check the unknowns list for new dlines */
460 DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
461 {
462 client_p = ptr->data;
463
464 if ((aconf = find_dline_conf(&client_p->localClient->ip,
465 client_p->localClient->aftype)))
466 {
467 if (aconf->status & CONF_EXEMPTDLINE)
468 continue;
469
470 exit_client(client_p, &me, "D-lined");
471 }
472 }
473 }
474
475 /*
476 * ban_them
477 *
478 * inputs - pointer to client to ban
479 * - pointer to ConfItem
480 * output - NONE
481 * side effects - given client_p is banned
482 */
483 static void
484 ban_them(struct Client *client_p, struct ConfItem *conf)
485 {
486 const char *user_reason = NULL; /* What is sent to user */
487 const char *channel_reason = NULL; /* What is sent to channel */
488 struct AccessItem *aconf = NULL;
489 struct MatchItem *xconf = NULL;
490 const char *type_string = NULL;
491 const char dline_string[] = "D-line";
492 const char kline_string[] = "K-line";
493 const char gline_string[] = "G-line";
494 const char xline_string[] = "X-line";
495
496 switch (conf->type)
497 {
498 case RKLINE_TYPE:
499 case KLINE_TYPE:
500 type_string = kline_string;
501 aconf = map_to_conf(conf);
502 break;
503 case DLINE_TYPE:
504 type_string = dline_string;
505 aconf = map_to_conf(conf);
506 break;
507 case GLINE_TYPE:
508 type_string = gline_string;
509 aconf = map_to_conf(conf);
510 break;
511 case RXLINE_TYPE:
512 case XLINE_TYPE:
513 type_string = xline_string;
514 xconf = map_to_conf(conf);
515 ++xconf->count;
516 break;
517 default:
518 assert(0);
519 break;
520 }
521
522 if (ConfigFileEntry.kline_with_reason)
523 {
524 if (aconf != NULL)
525 user_reason = aconf->reason ? aconf->reason : type_string;
526 if (xconf != NULL)
527 user_reason = xconf->reason ? xconf->reason : type_string;
528 }
529 else
530 user_reason = type_string;
531
532 if (ConfigFileEntry.kline_reason != NULL)
533 channel_reason = ConfigFileEntry.kline_reason;
534 else
535 channel_reason = user_reason;
536
537 sendto_realops_flags(UMODE_ALL, L_ALL, "%s active for %s",
538 type_string, get_client_name(client_p, HIDE_IP));
539
540 if (IsClient(client_p))
541 sendto_one(client_p, form_str(ERR_YOUREBANNEDCREEP),
542 me.name, client_p->name, user_reason);
543
544 exit_client(client_p, &me, channel_reason);
545 }
546
547 /* update_client_exit_stats()
548 *
549 * input - pointer to client
550 * output - NONE
551 * side effects -
552 */
553 static void
554 update_client_exit_stats(struct Client *client_p)
555 {
556 if (IsServer(client_p))
557 {
558 sendto_realops_flags(UMODE_EXTERNAL, L_ALL,
559 "Server %s split from %s",
560 client_p->name, client_p->servptr->name);
561 }
562 else if (IsClient(client_p))
563 {
564 --Count.total;
565 if (IsOper(client_p))
566 --Count.oper;
567 if (IsInvisible(client_p))
568 --Count.invisi;
569 }
570
571 if (splitchecking && !splitmode)
572 check_splitmode(NULL);
573 }
574
575 /* find_person()
576 *
577 * inputs - pointer to name
578 * output - return client pointer
579 * side effects - find person by (nick)name
580 */
581 /* XXX - ugly wrapper */
582 struct Client *
583 find_person(const struct Client *client_p, const char *name)
584 {
585 struct Client *c2ptr;
586
587 if (IsDigit(*name))
588 {
589 if ((c2ptr = hash_find_id(name)) != NULL)
590 {
591 /* invisible users shall not be found by UID guessing */
592 if (IsInvisible(c2ptr) && !IsServer(client_p))
593 c2ptr = NULL;
594 }
595 }
596 else
597 c2ptr = find_client(name);
598
599 return ((c2ptr != NULL && IsClient(c2ptr)) ? c2ptr : NULL);
600 }
601
602 /*
603 * find_chasing - find the client structure for a nick name (user)
604 * using history mechanism if necessary. If the client is not found,
605 * an error message (NO SUCH NICK) is generated. If the client was found
606 * through the history, chasing will be 1 and otherwise 0.
607 */
608 struct Client *
609 find_chasing(struct Client *client_p, struct Client *source_p, const char *user, int *chasing)
610 {
611 struct Client *who = find_person(client_p, user);
612
613 if (chasing)
614 *chasing = 0;
615
616 if (who)
617 return(who);
618
619 if (IsDigit(*user))
620 return(NULL);
621
622 if ((who = get_history(user,
623 (time_t)ConfigFileEntry.kill_chase_time_limit))
624 == NULL)
625 {
626 sendto_one(source_p, form_str(ERR_NOSUCHNICK),
627 me.name, source_p->name, user);
628 return(NULL);
629 }
630
631 if (chasing)
632 *chasing = 1;
633
634 return(who);
635 }
636
637 /*
638 * get_client_name - Return the name of the client
639 * for various tracking and
640 * admin purposes. The main purpose of this function is to
641 * return the "socket host" name of the client, if that
642 * differs from the advertised name (other than case).
643 * But, this can be used to any client structure.
644 *
645 * NOTE 1:
646 * Watch out the allocation of "nbuf", if either source_p->name
647 * or source_p->sockhost gets changed into pointers instead of
648 * directly allocated within the structure...
649 *
650 * NOTE 2:
651 * Function return either a pointer to the structure (source_p) or
652 * to internal buffer (nbuf). *NEVER* use the returned pointer
653 * to modify what it points!!!
654 */
655 const char *
656 get_client_name(struct Client *client, int showip)
657 {
658 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
659
660 assert(client != NULL);
661
662 if (irccmp(client->name, client->host) == 0)
663 return(client->name);
664
665 if (ConfigServerHide.hide_server_ips)
666 if (IsServer(client) || IsConnecting(client) || IsHandshake(client))
667 showip = MASK_IP;
668
669 if (ConfigFileEntry.hide_spoof_ips)
670 if (showip == SHOW_IP && IsIPSpoof(client))
671 showip = MASK_IP;
672
673 /* And finally, let's get the host information, ip or name */
674 switch (showip)
675 {
676 case SHOW_IP:
677 if (MyConnect(client))
678 {
679 ircsprintf(nbuf, "%s[%s@%s]", client->name, client->username,
680 client->sockhost);
681 break;
682 }
683 case MASK_IP:
684 ircsprintf(nbuf, "%s[%s@255.255.255.255]", client->name,
685 client->username);
686 break;
687 default:
688 ircsprintf(nbuf, "%s[%s@%s]", client->name, client->username,
689 client->host);
690 }
691
692 return(nbuf);
693 }
694
695 void
696 free_exited_clients(void)
697 {
698 dlink_node *ptr, *next;
699 struct Client *target_p;
700
701 DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
702 {
703 target_p = ptr->data;
704
705 if (ptr->data == NULL)
706 {
707 sendto_realops_flags(UMODE_ALL, L_ALL,
708 "Warning: null client on dead_list!");
709 dlinkDelete(ptr, &dead_list);
710 free_dlink_node(ptr);
711 continue;
712 }
713
714 free_client(target_p);
715 dlinkDelete(ptr, &dead_list);
716 free_dlink_node(ptr);
717 }
718 }
719
720 /*
721 * Exit one client, local or remote. Assuming all dependents have
722 * been already removed, and socket closed for local client.
723 *
724 * The only messages generated are QUITs on channels.
725 */
726 static void
727 exit_one_client(struct Client *source_p, const char *quitmsg)
728 {
729 dlink_node *lp = NULL, *next_lp = NULL;
730
731 assert(!IsMe(source_p));
732
733 if (IsServer(source_p))
734 {
735 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
736
737 if ((lp = dlinkFindDelete(&global_serv_list, source_p)) != NULL)
738 free_dlink_node(lp);
739
740 if (!MyConnect(source_p))
741 {
742 source_p->from->serv->dep_servers--;
743 assert(source_p->from->serv->dep_servers > 0);
744 }
745 }
746 else if (IsClient(source_p))
747 {
748 if (source_p->servptr->serv != NULL)
749 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users);
750
751 /* If a person is on a channel, send a QUIT notice
752 ** to every client (person) on the same channel (so
753 ** that the client can show the "**signoff" message).
754 ** (Note: The notice is to the local clients *only*)
755 */
756 sendto_common_channels_local(source_p, 0, ":%s!%s@%s QUIT :%s",
757 source_p->name, source_p->username,
758 source_p->host, quitmsg);
759 DLINK_FOREACH_SAFE(lp, next_lp, source_p->channel.head)
760 remove_user_from_channel(lp->data);
761
762 /* Clean up allow lists */
763 del_all_accepts(source_p);
764 add_history(source_p, 0);
765 off_history(source_p);
766
767 if (!MyConnect(source_p))
768 {
769 source_p->from->serv->dep_users--;
770 assert(source_p->from->serv->dep_users >= 0);
771 }
772 else
773 {
774 /* Clean up invitefield */
775 DLINK_FOREACH_SAFE(lp, next_lp, source_p->localClient->invited.head)
776 del_invite(lp->data, source_p);
777 }
778 }
779
780 /* Remove source_p from the client lists */
781 if (HasID(source_p))
782 hash_del_id(source_p);
783 if (source_p->name[0])
784 hash_del_client(source_p);
785
786 if (IsUserHostIp(source_p))
787 delete_user_host(source_p->username, source_p->host, !MyConnect(source_p));
788
789 /* remove from global client list
790 * NOTE: source_p->node.next cannot be NULL if the client is added
791 * to global_client_list (there is always &me at its end)
792 */
793 if (source_p != NULL && source_p->node.next != NULL)
794 dlinkDelete(&source_p->node, &global_client_list);
795
796 update_client_exit_stats(source_p);
797
798 /* Check to see if the client isn't already on the dead list */
799 assert(dlinkFind(&dead_list, source_p) == NULL);
800
801 /* add to dead client dlist */
802 SetDead(source_p);
803 dlinkAdd(source_p, make_dlink_node(), &dead_list);
804 }
805
806 /* Recursively send QUITs and SQUITs for source_p and all its dependent clients
807 * and servers to those servers that need them. A server needs the client
808 * QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
809 * isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
810 * a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
811 * on that one -orabidoo
812 *
813 * This is now called on each local server -adx
814 */
815 static void
816 recurse_send_quits(struct Client *original_source_p, struct Client *source_p,
817 struct Client *from, struct Client *to, const char *comment,
818 const char *splitstr, const char *myname)
819 {
820 dlink_node *ptr, *next;
821 struct Client *target_p;
822 int hidden = match(myname, source_p->name);
823
824 assert(to != source_p); /* should be already removed from serv_list */
825
826 /* If this server can handle quit storm (QS) removal
827 * of dependents, just send the SQUIT
828 *
829 * Always check *all* dependent servers if some of them are
830 * hidden behind fakename. If so, send out the QUITs -adx
831 */
832 if (hidden || !IsCapable(to, CAP_QS))
833 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->users.head)
834 {
835 target_p = ptr->data;
836 sendto_one(to, ":%s QUIT :%s", target_p->name, splitstr);
837 }
838
839 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->servers.head)
840 recurse_send_quits(original_source_p, ptr->data, from, to,
841 comment, splitstr, myname);
842
843 if (!hidden && ((source_p == original_source_p && to != from) ||
844 !IsCapable(to, CAP_QS)))
845 {
846 /* don't use a prefix here - we have to be 100% sure the message
847 * will be accepted without Unknown prefix etc.. */
848 sendto_one(to, "SQUIT %s :%s", ID_or_name(source_p, to), comment);
849 }
850 }
851
852 /*
853 * Remove all clients that depend on source_p; assumes all (S)QUITs have
854 * already been sent. we make sure to exit a server's dependent clients
855 * and servers before the server itself; exit_one_client takes care of
856 * actually removing things off llists. tweaked from +CSr31 -orabidoo
857 */
858 static void
859 recurse_remove_clients(struct Client *source_p, const char *quitmsg)
860 {
861 dlink_node *ptr, *next;
862
863 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->users.head)
864 exit_one_client(ptr->data, quitmsg);
865
866 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->servers.head)
867 {
868 recurse_remove_clients(ptr->data, quitmsg);
869 exit_one_client(ptr->data, quitmsg);
870 }
871
872 assert(source_p->serv->dep_servers == 1);
873 assert(source_p->serv->dep_users == 0);
874 }
875
876 /*
877 ** Remove *everything* that depends on source_p, from all lists, and sending
878 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
879 ** and its SQUITs have been sent except for the upstream one -orabidoo
880 */
881 static void
882 remove_dependents(struct Client *source_p, struct Client *from,
883 const char *comment, const char *splitstr)
884 {
885 struct Client *to;
886 struct ConfItem *conf;
887 static char myname[HOSTLEN+1];
888 dlink_node *ptr;
889
890 DLINK_FOREACH(ptr, serv_list.head)
891 {
892 to = ptr->data;
893
894 if ((conf = to->serv->sconf) != NULL)
895 strlcpy(myname, my_name_for_link(conf), sizeof(myname));
896 else
897 strlcpy(myname, me.name, sizeof(myname));
898 recurse_send_quits(source_p, source_p, from, to,
899 comment, splitstr, myname);
900 }
901
902 recurse_remove_clients(source_p, splitstr);
903 }
904
905 /*
906 * exit_client - exit a client of any type. Generally, you can use
907 * this on any struct Client, regardless of its state.
908 *
909 * Note, you shouldn't exit remote _users_ without first doing
910 * SetKilled and propagating a kill or similar message. However,
911 * it is perfectly correct to call exit_client to force a _server_
912 * quit (either local or remote one).
913 *
914 * inputs: - a client pointer that is going to be exited
915 * - for servers, the second argument is a pointer to who
916 * is firing the server. This side won't get any generated
917 * messages. NEVER NULL!
918 * output: none
919 * side effects: the client is delinked from all lists, disconnected,
920 * and the rest of IRC network is notified of the exit.
921 * Client memory is scheduled to be freed
922 */
923 void
924 exit_client(struct Client *source_p, struct Client *from, const char *comment)
925 {
926 dlink_node *m;
927
928 if (MyConnect(source_p))
929 {
930 /* DO NOT REMOVE. exit_client can be called twice after a failed
931 * read/write.
932 */
933 if (IsClosing(source_p))
934 return;
935
936 SetClosing(source_p);
937
938 if (IsIpHash(source_p))
939 remove_one_ip(&source_p->localClient->ip);
940
941 delete_auth(source_p);
942
943 /* This source_p could have status of one of STAT_UNKNOWN, STAT_CONNECTING
944 * STAT_HANDSHAKE or STAT_UNKNOWN
945 * all of which are lumped together into unknown_list
946 *
947 * In all above cases IsRegistered() will not be true.
948 */
949 if (!IsRegistered(source_p))
950 {
951 if ((m = dlinkFindDelete(&unknown_list, source_p)) != NULL)
952 free_dlink_node(m);
953 }
954 else if (IsClient(source_p))
955 {
956 Count.local--;
957
958 if (IsOper(source_p))
959 {
960 if ((m = dlinkFindDelete(&oper_list, source_p)) != NULL)
961 free_dlink_node(m);
962 }
963
964 dlinkDelete(&source_p->localClient->lclient_node, &local_client_list);
965 if (source_p->localClient->list_task != NULL)
966 free_list_task(source_p->localClient->list_task, source_p);
967
968 sendto_realops_flags(UMODE_CCONN, L_ALL, "Client exiting: %s (%s@%s) [%s] [%s]",
969 source_p->name, source_p->username, source_p->host, comment,
970 ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
971 "255.255.255.255" : source_p->sockhost);
972 }
973
974 /* As soon as a client is known to be a server of some sort
975 * it has to be put on the serv_list, or SJOIN's to this new server
976 * from the connect burst will not be seen.
977 */
978 if (IsServer(source_p) || IsConnecting(source_p) ||
979 IsHandshake(source_p))
980 {
981 if ((m = dlinkFindDelete(&serv_list, source_p)) != NULL)
982 {
983 free_dlink_node(m);
984 unset_chcap_usage_counts(source_p);
985 }
986
987 if (IsServer(source_p))
988 {
989 Count.myserver--;
990 if (ServerInfo.hub)
991 remove_lazylink_flags(source_p->localClient->serverMask);
992 else
993 uplink = NULL;
994 }
995 }
996
997 log_user_exit(source_p);
998
999 if (!IsDead(source_p))
1000 {
1001 if (IsServer(source_p))
1002 {
1003 /* for them, we are exiting the network */
1004 sendto_one(source_p, ":%s SQUIT %s :%s",
1005 ID_or_name(from, source_p), me.name, comment);
1006 }
1007
1008 sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
1009 source_p->host, comment);
1010 }
1011
1012 /*
1013 ** Currently only server connections can have
1014 ** depending remote clients here, but it does no
1015 ** harm to check for all local clients. In
1016 ** future some other clients than servers might
1017 ** have remotes too...
1018 **
1019 ** Close the Client connection first and mark it
1020 ** so that no messages are attempted to send to it.
1021 ** Remember it makes source_p->from == NULL.
1022 */
1023 close_connection(source_p);
1024 }
1025
1026 if (IsServer(source_p))
1027 {
1028 char splitstr[HOSTLEN + HOSTLEN + 2];
1029
1030 /* This shouldn't ever happen */
1031 assert(source_p->serv != NULL && source_p->servptr != NULL);
1032
1033 if (ConfigServerHide.hide_servers)
1034 /* set netsplit message to "*.net *.split" to still show
1035 * that its a split, but hide the servers splitting
1036 */
1037 strcpy(splitstr, "*.net *.split");
1038 else
1039 snprintf(splitstr, sizeof(splitstr), "%s %s",
1040 source_p->servptr->name, source_p->name);
1041
1042 remove_dependents(source_p, from->from, comment, splitstr);
1043
1044 if (source_p->servptr == &me)
1045 {
1046 sendto_realops_flags(UMODE_ALL, L_ALL,
1047 "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
1048 source_p->name, (int)(CurrentTime - source_p->firsttime),
1049 source_p->localClient->send.bytes >> 10,
1050 source_p->localClient->recv.bytes >> 10);
1051 ilog(L_NOTICE, "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
1052 source_p->name, (int)(CurrentTime - source_p->firsttime),
1053 source_p->localClient->send.bytes >> 10,
1054 source_p->localClient->recv.bytes >> 10);
1055 }
1056 }
1057 else if (IsClient(source_p) && !IsKilled(source_p))
1058 {
1059 sendto_server(from->from, source_p, NULL, CAP_TS6, NOCAPS, NOFLAGS,
1060 ":%s QUIT :%s", ID(source_p), comment);
1061 sendto_server(from->from, source_p, NULL, NOCAPS, CAP_TS6, NOFLAGS,
1062 ":%s QUIT :%s", source_p->name, comment);
1063 }
1064
1065 /* The client *better* be off all of the lists */
1066 assert(dlinkFind(&unknown_list, source_p) == NULL);
1067 assert(dlinkFind(&local_client_list, source_p) == NULL);
1068 assert(dlinkFind(&serv_list, source_p) == NULL);
1069 assert(dlinkFind(&oper_list, source_p) == NULL);
1070
1071 exit_one_client(source_p, comment);
1072 }
1073
1074 /*
1075 * dead_link_on_write - report a write error if not already dead,
1076 * mark it as dead then exit it
1077 */
1078 void
1079 dead_link_on_write(struct Client *client_p, int ierrno)
1080 {
1081 dlink_node *ptr;
1082
1083 if (IsDefunct(client_p))
1084 return;
1085
1086 dbuf_clear(&client_p->localClient->buf_recvq);
1087 dbuf_clear(&client_p->localClient->buf_sendq);
1088
1089 assert(dlinkFind(&abort_list, client_p) == NULL);
1090 ptr = make_dlink_node();
1091 /* don't let exit_aborted_clients() finish yet */
1092 dlinkAddTail(client_p, ptr, &abort_list);
1093
1094 if (eac_next == NULL)
1095 eac_next = ptr;
1096
1097 SetDead(client_p); /* You are dead my friend */
1098 }
1099
1100 /*
1101 * dead_link_on_read - report a read error if not already dead,
1102 * mark it as dead then exit it
1103 */
1104 void
1105 dead_link_on_read(struct Client *client_p, int error)
1106 {
1107 char errmsg[255];
1108 int current_error;
1109
1110 if (IsDefunct(client_p))
1111 return;
1112
1113 dbuf_clear(&client_p->localClient->buf_recvq);
1114 dbuf_clear(&client_p->localClient->buf_sendq);
1115
1116 current_error = get_sockerr(client_p->localClient->fd.fd);
1117
1118 if (IsServer(client_p) || IsHandshake(client_p))
1119 {
1120 int connected = CurrentTime - client_p->firsttime;
1121
1122 if (error == 0)
1123 {
1124 /* Admins get the real IP */
1125 sendto_realops_flags(UMODE_ALL, L_ADMIN,
1126 "Server %s closed the connection",
1127 get_client_name(client_p, SHOW_IP));
1128
1129 /* Opers get a masked IP */
1130 sendto_realops_flags(UMODE_ALL, L_OPER,
1131 "Server %s closed the connection",
1132 get_client_name(client_p, MASK_IP));
1133
1134 ilog(L_NOTICE, "Server %s closed the connection",
1135 get_client_name(client_p, SHOW_IP));
1136 }
1137 else
1138 {
1139 report_error(L_ADMIN, "Lost connection to %s: %d",
1140 get_client_name(client_p, SHOW_IP), current_error);
1141 report_error(L_OPER, "Lost connection to %s: %d",
1142 get_client_name(client_p, MASK_IP), current_error);
1143 }
1144
1145 sendto_realops_flags(UMODE_ALL, L_ALL,
1146 "%s had been connected for %d day%s, %2d:%02d:%02d",
1147 client_p->name, connected/86400,
1148 (connected/86400 == 1) ? "" : "s",
1149 (connected % 86400) / 3600, (connected % 3600) / 60,
1150 connected % 60);
1151 }
1152
1153 if (error == 0)
1154 strlcpy(errmsg, "Remote host closed the connection",
1155 sizeof(errmsg));
1156 else
1157 ircsprintf(errmsg, "Read error: %s",
1158 strerror(current_error));
1159
1160 exit_client(client_p, &me, errmsg);
1161 }
1162
1163 void
1164 exit_aborted_clients(void)
1165 {
1166 dlink_node *ptr;
1167 struct Client *target_p;
1168 const char *notice;
1169
1170 DLINK_FOREACH_SAFE(ptr, eac_next, abort_list.head)
1171 {
1172 target_p = ptr->data;
1173 eac_next = ptr->next;
1174
1175 if (target_p == NULL)
1176 {
1177 sendto_realops_flags(UMODE_ALL, L_ALL,
1178 "Warning: null client on abort_list!");
1179 dlinkDelete(ptr, &abort_list);
1180 free_dlink_node(ptr);
1181 continue;
1182 }
1183
1184 dlinkDelete(ptr, &abort_list);
1185
1186 if (IsSendQExceeded(target_p))
1187 notice = "Max SendQ exceeded";
1188 else
1189 notice = "Write error: connection closed";
1190
1191 exit_client(target_p, &me, notice);
1192 free_dlink_node(ptr);
1193 }
1194 }
1195
1196 /*
1197 * accept processing, this adds a form of "caller ID" to ircd
1198 *
1199 * If a client puts themselves into "caller ID only" mode,
1200 * only clients that match a client pointer they have put on
1201 * the accept list will be allowed to message them.
1202 *
1203 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1204 *
1205 * [target.allow_list] -> [ source1 ] -> [source2 ]
1206 *
1207 * i.e. a target will have a link list of source pointers it will allow
1208 * each source client then has a back pointer pointing back
1209 * to the client that has it on its accept list.
1210 * This allows for exit_one_client to remove these now bogus entries
1211 * from any client having an accept on them.
1212 */
1213
1214 /* accept_message()
1215 *
1216 * inputs - pointer to source client
1217 * - pointer to target client
1218 * output - 1 if accept this message 0 if not
1219 * side effects - See if source is on target's allow list
1220 */
1221 int
1222 accept_message(struct Client *source, struct Client *target)
1223 {
1224 dlink_node *ptr;
1225
1226 DLINK_FOREACH(ptr, target->allow_list.head)
1227 {
1228 struct Client *target_p = ptr->data;
1229
1230 if (source == target_p)
1231 return (1);
1232 }
1233
1234 if (IsSoftCallerId(target))
1235 {
1236 DLINK_FOREACH(ptr, target->channel.head)
1237 if (IsMember(source, ((struct Membership *)ptr->data)->chptr))
1238 return (1);
1239 }
1240
1241 return (0);
1242 }
1243
1244 /* del_from_accept()
1245 *
1246 * inputs - pointer to source client
1247 * - pointer to target client
1248 * output - NONE
1249 * side effects - Delete's source pointer to targets allow list
1250 *
1251 * Walk through the target's accept list, remove if source is found,
1252 * Then walk through the source's on_accept_list remove target if found.
1253 */
1254 void
1255 del_from_accept(struct Client *source, struct Client *target)
1256 {
1257 dlink_node *ptr;
1258 dlink_node *ptr2;
1259 dlink_node *next_ptr;
1260 dlink_node *next_ptr2;
1261 struct Client *target_p;
1262
1263 DLINK_FOREACH_SAFE(ptr, next_ptr, target->allow_list.head)
1264 {
1265 target_p = ptr->data;
1266
1267 if (source == target_p)
1268 {
1269 dlinkDelete(ptr, &target->allow_list);
1270 free_dlink_node(ptr);
1271
1272 DLINK_FOREACH_SAFE(ptr2, next_ptr2, source->on_allow_list.head)
1273 {
1274 target_p = ptr2->data;
1275
1276 if (target == target_p)
1277 {
1278 dlinkDelete(ptr2, &source->on_allow_list);
1279 free_dlink_node(ptr2);
1280 }
1281 }
1282 }
1283 }
1284 }
1285
1286 /* del_all_accepts()
1287 *
1288 * inputs - pointer to exiting client
1289 * output - NONE
1290 * side effects - Walk through given clients allow_list and on_allow_list
1291 * remove all references to this client
1292 */
1293 void
1294 del_all_accepts(struct Client *client_p)
1295 {
1296 dlink_node *ptr, *next_ptr;
1297
1298 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->allow_list.head)
1299 del_from_accept(ptr->data, client_p);
1300
1301 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
1302 del_from_accept(client_p, ptr->data);
1303 }
1304
1305 /* del_all_their_accepts()
1306 *
1307 * inputs - pointer to exiting client
1308 * output - NONE
1309 * side effects - Walk through given clients on_allow_list
1310 * remove all references to this client,
1311 * allow this client to keep their own allow_list
1312 */
1313 void
1314 del_all_their_accepts(struct Client *client_p)
1315 {
1316 dlink_node *ptr, *next_ptr;
1317
1318 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
1319 del_from_accept(client_p, ptr->data);
1320 }
1321
1322 /* set_initial_nick()
1323 *
1324 * inputs
1325 * output
1326 * side effects -
1327 *
1328 * This function is only called to set up an initially registering
1329 * client.
1330 */
1331 void
1332 set_initial_nick(struct Client *client_p, struct Client *source_p,
1333 const char *nick)
1334 {
1335 char buf[USERLEN + 1];
1336
1337 /* Client setting NICK the first time */
1338
1339 /* This had to be copied here to avoid problems.. */
1340 source_p->tsinfo = CurrentTime;
1341 source_p->localClient->registration &= ~REG_NEED_NICK;
1342
1343 if (source_p->name[0])
1344 hash_del_client(source_p);
1345
1346 strlcpy(source_p->name, nick, sizeof(source_p->name));
1347 hash_add_client(source_p);
1348
1349 /* fd_desc is long enough */
1350 fd_note(&client_p->localClient->fd, "Nick: %s", nick);
1351
1352 /* They have the nick they want now.. */
1353 client_p->llname[0] = '\0';
1354
1355 if (!source_p->localClient->registration)
1356 {
1357 strlcpy(buf, source_p->username, sizeof(buf));
1358
1359 /*
1360 * USER already received, now we have NICK.
1361 * *NOTE* For servers "NICK" *must* precede the
1362 * user message (giving USER before NICK is possible
1363 * only for local client connection!). register_user
1364 * may reject the client and call exit_client for it
1365 * --must test this and exit m_nick too!!!
1366 */
1367 register_local_user(client_p, source_p, nick, buf);
1368 }
1369 }
1370
1371 /* change_local_nick()
1372 *
1373 * inputs - pointer to server
1374 * - pointer to client
1375 * - nick
1376 * output -
1377 * side effects - changes nick of a LOCAL user
1378 */
1379 void
1380 change_local_nick(struct Client *client_p, struct Client *source_p, const char *nick)
1381 {
1382 /*
1383 ** Client just changing his/her nick. If he/she is
1384 ** on a channel, send note of change to all clients
1385 ** on that channel. Propagate notice to other servers.
1386 */
1387 if ((source_p->localClient->last_nick_change +
1388 ConfigFileEntry.max_nick_time) < CurrentTime)
1389 source_p->localClient->number_of_nick_changes = 0;
1390 source_p->localClient->last_nick_change = CurrentTime;
1391 source_p->localClient->number_of_nick_changes++;
1392
1393 if ((ConfigFileEntry.anti_nick_flood &&
1394 (source_p->localClient->number_of_nick_changes
1395 <= ConfigFileEntry.max_nick_changes)) ||
1396 !ConfigFileEntry.anti_nick_flood ||
1397 (IsOper(source_p) && ConfigFileEntry.no_oper_flood))
1398 {
1399 if (irccmp(source_p->name, nick))
1400 source_p->tsinfo = CurrentTime;
1401
1402 /* XXX - the format of this notice should eventually be changed
1403 * to either %s[%s@%s], or even better would be get_client_name() -bill
1404 */
1405 sendto_realops_flags(UMODE_NCHANGE, L_ALL, "Nick change: From %s to %s [%s@%s]",
1406 source_p->name, nick, source_p->username, source_p->host);
1407 sendto_common_channels_local(source_p, 1, ":%s!%s@%s NICK :%s",
1408 source_p->name, source_p->username,
1409 source_p->host, nick);
1410
1411 add_history(source_p, 1);
1412
1413 /* Only hubs care about lazy link nicks not being sent on yet
1414 * lazylink leafs/leafs always send their nicks up to hub,
1415 * hence must always propagate nick changes.
1416 * hubs might not propagate a nick change, if the leaf
1417 * does not know about that client yet.
1418 */
1419 sendto_server(client_p, source_p, NULL, CAP_TS6, NOCAPS, NOFLAGS,
1420 ":%s NICK %s :%lu",
1421 ID(source_p), nick, (unsigned long)source_p->tsinfo);
1422 sendto_server(client_p, source_p, NULL, NOCAPS, CAP_TS6, NOFLAGS,
1423 ":%s NICK %s :%lu",
1424 source_p->name, nick, (unsigned long)source_p->tsinfo);
1425 }
1426 else
1427 {
1428 sendto_one(source_p, form_str(ERR_NICKTOOFAST),
1429 me.name, source_p->name, source_p->name,
1430 nick, ConfigFileEntry.max_nick_time);
1431 return;
1432 }
1433
1434 /* Finally, add to hash */
1435 if (source_p->name[0])
1436 hash_del_client(source_p);
1437
1438 strcpy(source_p->name, nick);
1439 hash_add_client(source_p);
1440
1441 /* Make sure everyone that has this client on its accept list
1442 * loses that reference.
1443 */
1444 del_all_their_accepts(source_p);
1445
1446 /* fd_desc is long enough */
1447 fd_note(&client_p->localClient->fd, "Nick: %s", nick);
1448 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28