ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 1474
Committed: Sun Jul 22 14:44:07 2012 UTC (13 years, 1 month ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/client.c
File size: 37790 byte(s)
Log Message:
- removed &localchannels

File Contents

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

Properties

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