ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 1632
Committed: Sun Nov 4 15:37:10 2012 UTC (12 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 35921 byte(s)
Log Message:
- Initial rewrite of the configuration subsystem

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 MaskItem *);
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 strcpy(client_p->svid, "0");
137
138 return client_p;
139 }
140
141 /*
142 * free_client
143 *
144 * inputs - pointer to client
145 * output - NONE
146 * side effects - client pointed to has its memory freed
147 */
148 static void
149 free_client(struct Client *client_p)
150 {
151 assert(client_p != NULL);
152 assert(client_p != &me);
153 assert(client_p->hnext == client_p);
154 assert(client_p->idhnext == client_p);
155 assert(client_p->channel.head == NULL);
156 assert(dlink_list_length(&client_p->channel) == 0);
157 assert(dlink_list_length(&client_p->whowas) == 0);
158 assert(!IsServer(client_p) || IsServer(client_p) && client_p->serv);
159
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 (!IsRegistered(client_p))
255 ping = CONNECTTIMEOUT, pingwarn = 0;
256 else
257 ping = get_client_ping(&client_p->localClient->confs, &pingwarn);
258
259 if (ping < CurrentTime - client_p->localClient->lasttime)
260 {
261 if (!IsPingSent(client_p))
262 {
263 /*
264 * if we havent PINGed the connection and we havent
265 * heard from it in a while, PING it to make sure
266 * it is still alive.
267 */
268 SetPingSent(client_p);
269 ClearPingWarning(client_p);
270 client_p->localClient->lasttime = CurrentTime - ping;
271 sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
272 }
273 else
274 {
275 if (CurrentTime - client_p->localClient->lasttime >= 2 * ping)
276 {
277 /*
278 * If the client/server hasn't talked to us in 2*ping seconds
279 * and it has a ping time, then close its connection.
280 */
281 if (IsServer(client_p) || IsHandshake(client_p))
282 {
283 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
284 "No response from %s, closing link",
285 get_client_name(client_p, HIDE_IP));
286 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
287 "No response from %s, closing link",
288 get_client_name(client_p, MASK_IP));
289 ilog(LOG_TYPE_IRCD, "No response from %s, closing link",
290 get_client_name(client_p, HIDE_IP));
291 }
292
293 snprintf(scratch, sizeof(scratch), "Ping timeout: %d seconds",
294 (int)(CurrentTime - client_p->localClient->lasttime));
295 exit_client(client_p, &me, scratch);
296 }
297 else if (!IsPingWarning(client_p) && pingwarn > 0 &&
298 (IsServer(client_p) || IsHandshake(client_p)) &&
299 CurrentTime - client_p->localClient->lasttime >= ping + pingwarn)
300 {
301 /*
302 * If the server hasn't replied in pingwarn seconds after sending
303 * the PING, notify the opers so that they are aware of the problem.
304 */
305 SetPingWarning(client_p);
306 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
307 "Warning, no response from %s in %d seconds",
308 get_client_name(client_p, HIDE_IP), pingwarn);
309 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
310 "Warning, no response from %s in %d seconds",
311 get_client_name(client_p, MASK_IP), pingwarn);
312 ilog(LOG_TYPE_IRCD, "No response from %s in %d seconds",
313 get_client_name(client_p, HIDE_IP), pingwarn);
314 }
315 }
316 }
317 }
318 }
319
320 /* check_unknowns_list()
321 *
322 * inputs - pointer to list of unknown clients
323 * output - NONE
324 * side effects - unknown clients get marked for termination after n seconds
325 */
326 static void
327 check_unknowns_list(void)
328 {
329 dlink_node *ptr, *next_ptr;
330
331 DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
332 {
333 struct Client *client_p = ptr->data;
334
335 /*
336 * Check UNKNOWN connections - if they have been in this state
337 * for > 30s, close them.
338 */
339 if (IsAuthFinished(client_p) && (CurrentTime - client_p->localClient->firsttime) > 30)
340 exit_client(client_p, &me, "Registration timed out");
341 }
342 }
343
344 /* check_conf_klines()
345 *
346 * inputs - NONE
347 * output - NONE
348 * side effects - Check all connections for a pending kline against the
349 * client, exit the client if a kline matches.
350 */
351 void
352 check_conf_klines(void)
353 {
354 struct Client *client_p = NULL; /* current local client_p being examined */
355 struct MaskItem *conf = NULL;
356 dlink_node *ptr, *next_ptr;
357
358 DLINK_FOREACH_SAFE(ptr, next_ptr, local_client_list.head)
359 {
360 client_p = ptr->data;
361
362 /* If a client is already being exited
363 */
364 if (IsDead(client_p) || !IsClient(client_p))
365 continue;
366
367 if ((conf = find_dline_conf(&client_p->localClient->ip,
368 client_p->localClient->aftype)) != NULL)
369 {
370 if (conf->status & CONF_EXEMPT)
371 continue;
372
373 ban_them(client_p, conf);
374 continue; /* and go examine next fd/client_p */
375 }
376
377 if (ConfigFileEntry.glines && (conf = find_gline(client_p)))
378 {
379 if (IsExemptKline(client_p) ||
380 IsExemptGline(client_p))
381 {
382 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
383 "GLINE over-ruled for %s, client is %sline_exempt",
384 get_client_name(client_p, HIDE_IP), IsExemptKline(client_p) ? "k" : "g");
385 continue;
386 }
387
388 ban_them(client_p, conf);
389 /* and go examine next fd/client_p */
390 continue;
391 }
392
393 if ((conf = find_kill(client_p)) != NULL)
394 {
395 if (IsExemptKline(client_p))
396 {
397 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
398 "KLINE over-ruled for %s, client is kline_exempt",
399 get_client_name(client_p, HIDE_IP));
400 continue;
401 }
402
403 ban_them(client_p, conf);
404 continue;
405 }
406
407 if ((conf = find_matching_name_conf(CONF_XLINE, client_p->info,
408 NULL, NULL, 0)) != NULL ||
409 (conf = find_matching_name_conf(CONF_RXLINE, client_p->info,
410 NULL, NULL, 0)) != NULL)
411 {
412 ban_them(client_p, conf);
413 continue;
414 }
415 }
416
417 /* also check the unknowns list for new dlines */
418 DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
419 {
420 client_p = ptr->data;
421
422 if ((conf = find_dline_conf(&client_p->localClient->ip,
423 client_p->localClient->aftype)))
424 {
425 if (conf->status & CONF_EXEMPT)
426 continue;
427
428 exit_client(client_p, &me, "D-lined");
429 }
430 }
431 }
432
433 /*
434 * ban_them
435 *
436 * inputs - pointer to client to ban
437 * - pointer to MaskItem
438 * output - NONE
439 * side effects - given client_p is banned
440 */
441 static void
442 ban_them(struct Client *client_p, struct MaskItem *conf)
443 {
444 const char *user_reason = NULL; /* What is sent to user */
445 const char *type_string = NULL;
446 const char dline_string[] = "D-line";
447 const char kline_string[] = "K-line";
448 const char gline_string[] = "G-line";
449 const char xline_string[] = "X-line";
450
451 switch (conf->type)
452 {
453 case CONF_RKLINE:
454 case CONF_KLINE:
455 type_string = kline_string;
456 break;
457 case CONF_DLINE:
458 type_string = dline_string;
459 break;
460 case CONF_GLINE:
461 type_string = gline_string;
462 break;
463 case CONF_RXLINE:
464 case CONF_XLINE:
465 type_string = xline_string;
466 ++conf->count;
467 break;
468 default:
469 assert(0);
470 break;
471 }
472
473 user_reason = conf->reason ? conf->reason : type_string;
474
475 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "%s active for %s",
476 type_string, get_client_name(client_p, HIDE_IP));
477
478 if (IsClient(client_p))
479 sendto_one(client_p, form_str(ERR_YOUREBANNEDCREEP),
480 me.name, client_p->name, user_reason);
481
482 exit_client(client_p, &me, user_reason);
483 }
484
485 /* update_client_exit_stats()
486 *
487 * input - pointer to client
488 * output - NONE
489 * side effects -
490 */
491 static void
492 update_client_exit_stats(struct Client *client_p)
493 {
494 if (IsClient(client_p))
495 {
496 assert(Count.total > 0);
497 --Count.total;
498 if (HasUMode(client_p, UMODE_OPER))
499 --Count.oper;
500 if (HasUMode(client_p, UMODE_INVISIBLE))
501 --Count.invisi;
502 }
503 else if (IsServer(client_p))
504 sendto_realops_flags(UMODE_EXTERNAL, L_ALL, SEND_NOTICE,
505 "Server %s split from %s",
506 client_p->name, client_p->servptr->name);
507
508 if (splitchecking && !splitmode)
509 check_splitmode(NULL);
510 }
511
512 /* find_person()
513 *
514 * inputs - pointer to name
515 * output - return client pointer
516 * side effects - find person by (nick)name
517 */
518 struct Client *
519 find_person(const struct Client *client_p, const char *name)
520 {
521 struct Client *c2ptr = NULL;
522
523 if (IsDigit(*name))
524 {
525 if ((c2ptr = hash_find_id(name)) != NULL)
526 {
527 /* invisible users shall not be found by UID guessing */
528 if (HasUMode(c2ptr, UMODE_INVISIBLE))
529 if (!IsServer(client_p) && !HasFlag(client_p, FLAGS_SERVICE))
530 c2ptr = NULL;
531 }
532 }
533 else
534 c2ptr = hash_find_client(name);
535
536 return ((c2ptr != NULL && IsClient(c2ptr)) ? c2ptr : NULL);
537 }
538
539 /*
540 * find_chasing - find the client structure for a nick name (user)
541 * using history mechanism if necessary. If the client is not found,
542 * an error message (NO SUCH NICK) is generated. If the client was found
543 * through the history, chasing will be 1 and otherwise 0.
544 */
545 struct Client *
546 find_chasing(struct Client *client_p, struct Client *source_p, const char *user, int *chasing)
547 {
548 struct Client *who = find_person(client_p, user);
549
550 if (chasing)
551 *chasing = 0;
552
553 if (who)
554 return who;
555
556 if (IsDigit(*user))
557 return NULL;
558
559 if ((who = get_history(user,
560 (time_t)ConfigFileEntry.kill_chase_time_limit))
561 == NULL)
562 {
563 sendto_one(source_p, form_str(ERR_NOSUCHNICK),
564 me.name, source_p->name, user);
565 return NULL;
566 }
567
568 if (chasing)
569 *chasing = 1;
570
571 return who;
572 }
573
574 /*
575 * get_client_name - Return the name of the client
576 * for various tracking and
577 * admin purposes. The main purpose of this function is to
578 * return the "socket host" name of the client, if that
579 * differs from the advertised name (other than case).
580 * But, this can be used to any client structure.
581 *
582 * NOTE 1:
583 * Watch out the allocation of "nbuf", if either source_p->name
584 * or source_p->sockhost gets changed into pointers instead of
585 * directly allocated within the structure...
586 *
587 * NOTE 2:
588 * Function return either a pointer to the structure (source_p) or
589 * to internal buffer (nbuf). *NEVER* use the returned pointer
590 * to modify what it points!!!
591 */
592 const char *
593 get_client_name(const struct Client *client, enum addr_mask_type type)
594 {
595 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
596
597 assert(client != NULL);
598
599 if (!MyConnect(client))
600 return client->name;
601
602 if (IsServer(client) || IsConnecting(client) || IsHandshake(client))
603 {
604 if (!irccmp(client->name, client->host))
605 return client->name;
606 else if (ConfigServerHide.hide_server_ips)
607 type = MASK_IP;
608 }
609
610 if (ConfigFileEntry.hide_spoof_ips)
611 if (type == SHOW_IP && IsIPSpoof(client))
612 type = MASK_IP;
613
614 /* And finally, let's get the host information, ip or name */
615 switch (type)
616 {
617 case SHOW_IP:
618 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
619 client->name,
620 client->username, client->sockhost);
621 break;
622 case MASK_IP:
623 if (client->localClient->aftype == AF_INET)
624 snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
625 client->name, client->username);
626 else
627 snprintf(nbuf, sizeof(nbuf), "%s[%s@ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]",
628 client->name, client->username);
629 break;
630 default:
631 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
632 client->name,
633 client->username, client->host);
634 }
635
636 return nbuf;
637 }
638
639 void
640 free_exited_clients(void)
641 {
642 dlink_node *ptr = NULL, *next = NULL;
643
644 DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
645 {
646 free_client(ptr->data);
647 dlinkDelete(ptr, &dead_list);
648 free_dlink_node(ptr);
649 }
650 }
651
652 /*
653 * Exit one client, local or remote. Assuming all dependents have
654 * been already removed, and socket closed for local client.
655 *
656 * The only messages generated are QUITs on channels.
657 */
658 static void
659 exit_one_client(struct Client *source_p, const char *quitmsg)
660 {
661 dlink_node *lp = NULL, *next_lp = NULL;
662
663 assert(!IsMe(source_p));
664
665 if (IsClient(source_p))
666 {
667 if (source_p->servptr->serv != NULL)
668 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->client_list);
669
670 /*
671 * If a person is on a channel, send a QUIT notice
672 * to every client (person) on the same channel (so
673 * that the client can show the "**signoff" message).
674 * (Note: The notice is to the local clients *only*)
675 */
676 sendto_common_channels_local(source_p, 0, ":%s!%s@%s QUIT :%s",
677 source_p->name, source_p->username,
678 source_p->host, quitmsg);
679 DLINK_FOREACH_SAFE(lp, next_lp, source_p->channel.head)
680 remove_user_from_channel(lp->data);
681
682 add_history(source_p, 0);
683 off_history(source_p);
684
685 watch_check_hash(source_p, RPL_LOGOFF);
686
687 if (MyConnect(source_p))
688 {
689 /* Clean up invitefield */
690 DLINK_FOREACH_SAFE(lp, next_lp, source_p->localClient->invited.head)
691 del_invite(lp->data, source_p);
692
693 del_all_accepts(source_p);
694 }
695 }
696 else if (IsServer(source_p))
697 {
698 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->server_list);
699
700 if ((lp = dlinkFindDelete(&global_serv_list, source_p)) != NULL)
701 free_dlink_node(lp);
702 }
703
704 /* Remove source_p from the client lists */
705 if (HasID(source_p))
706 hash_del_id(source_p);
707 if (source_p->name[0])
708 hash_del_client(source_p);
709
710 if (IsUserHostIp(source_p))
711 delete_user_host(source_p->username, source_p->host, !MyConnect(source_p));
712
713 /* remove from global client list
714 * NOTE: source_p->node.next cannot be NULL if the client is added
715 * to global_client_list (there is always &me at its end)
716 */
717 if (source_p != NULL && source_p->node.next != NULL)
718 dlinkDelete(&source_p->node, &global_client_list);
719
720 update_client_exit_stats(source_p);
721
722 /* Check to see if the client isn't already on the dead list */
723 assert(dlinkFind(&dead_list, source_p) == NULL);
724
725 /* add to dead client dlist */
726 SetDead(source_p);
727 dlinkAdd(source_p, make_dlink_node(), &dead_list);
728 }
729
730 /* Recursively send QUITs and SQUITs for source_p and all its dependent clients
731 * and servers to those servers that need them. A server needs the client
732 * QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
733 * isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
734 * a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
735 * on that one -orabidoo
736 *
737 * This is now called on each local server -adx
738 */
739 static void
740 recurse_send_quits(struct Client *original_source_p, struct Client *source_p,
741 struct Client *from, struct Client *to, const char *comment,
742 const char *splitstr)
743 {
744 dlink_node *ptr, *next;
745 struct Client *target_p;
746
747 assert(to != source_p); /* should be already removed from serv_list */
748
749 /* If this server can handle quit storm (QS) removal
750 * of dependents, just send the SQUIT
751 */
752 if (!IsCapable(to, CAP_QS))
753 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->client_list.head)
754 {
755 target_p = ptr->data;
756 sendto_one(to, ":%s QUIT :%s", target_p->name, splitstr);
757 }
758
759 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->server_list.head)
760 recurse_send_quits(original_source_p, ptr->data, from, to,
761 comment, splitstr);
762
763 if ((source_p == original_source_p && to != from) ||
764 !IsCapable(to, CAP_QS))
765 {
766 /* don't use a prefix here - we have to be 100% sure the message
767 * will be accepted without Unknown prefix etc.. */
768 sendto_one(to, "SQUIT %s :%s", ID_or_name(source_p, to), comment);
769 }
770 }
771
772 /*
773 * Remove all clients that depend on source_p; assumes all (S)QUITs have
774 * already been sent. we make sure to exit a server's dependent clients
775 * and servers before the server itself; exit_one_client takes care of
776 * actually removing things off llists. tweaked from +CSr31 -orabidoo
777 */
778 static void
779 recurse_remove_clients(struct Client *source_p, const char *quitmsg)
780 {
781 dlink_node *ptr, *next;
782
783 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->client_list.head)
784 exit_one_client(ptr->data, quitmsg);
785
786 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->server_list.head)
787 {
788 recurse_remove_clients(ptr->data, quitmsg);
789 exit_one_client(ptr->data, quitmsg);
790 }
791 }
792
793 /*
794 ** Remove *everything* that depends on source_p, from all lists, and sending
795 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
796 ** and its SQUITs have been sent except for the upstream one -orabidoo
797 */
798 static void
799 remove_dependents(struct Client *source_p, struct Client *from,
800 const char *comment, const char *splitstr)
801 {
802 dlink_node *ptr = NULL;
803
804 DLINK_FOREACH(ptr, serv_list.head)
805 recurse_send_quits(source_p, source_p, from, ptr->data,
806 comment, splitstr);
807
808 recurse_remove_clients(source_p, splitstr);
809 }
810
811 /*
812 * exit_client - exit a client of any type. Generally, you can use
813 * this on any struct Client, regardless of its state.
814 *
815 * Note, you shouldn't exit remote _users_ without first doing
816 * AddFlag(x, FLAGS_KILLED) and propagating a kill or similar message.
817 * However, it is perfectly correct to call exit_client to force a _server_
818 * quit (either local or remote one).
819 *
820 * inputs: - a client pointer that is going to be exited
821 * - for servers, the second argument is a pointer to who
822 * is firing the server. This side won't get any generated
823 * messages. NEVER NULL!
824 * output: none
825 * side effects: the client is delinked from all lists, disconnected,
826 * and the rest of IRC network is notified of the exit.
827 * Client memory is scheduled to be freed
828 */
829 void
830 exit_client(struct Client *source_p, struct Client *from, const char *comment)
831 {
832 dlink_node *m = NULL;
833
834 if (MyConnect(source_p))
835 {
836 /* DO NOT REMOVE. exit_client can be called twice after a failed
837 * read/write.
838 */
839 if (IsClosing(source_p))
840 return;
841
842 SetClosing(source_p);
843
844 if (IsIpHash(source_p))
845 remove_one_ip(&source_p->localClient->ip);
846
847 if (source_p->localClient->auth)
848 {
849 delete_auth(source_p->localClient->auth);
850 source_p->localClient->auth = NULL;
851 }
852
853 /*
854 * This source_p could have status of one of STAT_UNKNOWN, STAT_CONNECTING
855 * STAT_HANDSHAKE or STAT_UNKNOWN
856 * all of which are lumped together into unknown_list
857 *
858 * In all above cases IsRegistered() will not be true.
859 */
860 if (!IsRegistered(source_p))
861 {
862 assert(dlinkFind(&unknown_list, source_p));
863
864 dlinkDelete(&source_p->localClient->lclient_node, &unknown_list);
865 }
866 else if (IsClient(source_p))
867 {
868 time_t on_for = CurrentTime - source_p->localClient->firsttime;
869 assert(Count.local > 0);
870 Count.local--;
871
872 if (HasUMode(source_p, UMODE_OPER))
873 if ((m = dlinkFindDelete(&oper_list, source_p)) != NULL)
874 free_dlink_node(m);
875
876 assert(dlinkFind(&local_client_list, source_p));
877 dlinkDelete(&source_p->localClient->lclient_node, &local_client_list);
878
879 if (source_p->localClient->list_task != NULL)
880 free_list_task(source_p->localClient->list_task, source_p);
881
882 watch_del_watch_list(source_p);
883 sendto_realops_flags(UMODE_CCONN, L_ALL, SEND_NOTICE,
884 "Client exiting: %s (%s@%s) [%s] [%s]",
885 source_p->name, source_p->username, source_p->host, comment,
886 ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
887 "255.255.255.255" : source_p->sockhost);
888 sendto_realops_flags(UMODE_CCONN_FULL, L_ALL, SEND_NOTICE,
889 "CLIEXIT: %s %s %s %s 0 %s",
890 source_p->name,
891 source_p->username,
892 source_p->host,
893 ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
894 "255.255.255.255" : source_p->sockhost,
895 comment);
896 ilog(LOG_TYPE_USER, "%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu",
897 myctime(source_p->localClient->firsttime), (unsigned int)(on_for / 3600),
898 (unsigned int)((on_for % 3600)/60), (unsigned int)(on_for % 60),
899 source_p->name, source_p->username, source_p->host,
900 source_p->localClient->send.bytes>>10,
901 source_p->localClient->recv.bytes>>10);
902 }
903 else if (IsServer(source_p))
904 {
905 assert(Count.myserver > 0);
906 --Count.myserver;
907
908 assert(dlinkFind(&serv_list, source_p));
909 dlinkDelete(&source_p->localClient->lclient_node, &serv_list);
910 unset_chcap_usage_counts(source_p);
911 }
912
913 if (!IsDead(source_p))
914 {
915 if (IsServer(source_p))
916 {
917 /* for them, we are exiting the network */
918 sendto_one(source_p, ":%s SQUIT %s :%s",
919 ID_or_name(from, source_p), me.name, comment);
920 }
921
922 sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
923 source_p->host, comment);
924 }
925
926 /*
927 ** Currently only server connections can have
928 ** depending remote clients here, but it does no
929 ** harm to check for all local clients. In
930 ** future some other clients than servers might
931 ** have remotes too...
932 **
933 ** Close the Client connection first and mark it
934 ** so that no messages are attempted to send to it.
935 ** Remember it makes source_p->from == NULL.
936 */
937 close_connection(source_p);
938 }
939
940 if (IsServer(source_p))
941 {
942 char splitstr[HOSTLEN + HOSTLEN + 2];
943
944 /* This shouldn't ever happen */
945 assert(source_p->serv != NULL && source_p->servptr != NULL);
946
947 if (ConfigServerHide.hide_servers)
948 /*
949 * Set netsplit message to "*.net *.split" to still show
950 * that its a split, but hide the servers splitting
951 */
952 strcpy(splitstr, "*.net *.split");
953 else
954 snprintf(splitstr, sizeof(splitstr), "%s %s",
955 source_p->servptr->name, source_p->name);
956
957 remove_dependents(source_p, from->from, comment, splitstr);
958
959 if (source_p->servptr == &me)
960 {
961 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
962 "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
963 source_p->name, (int)(CurrentTime - source_p->localClient->firsttime),
964 source_p->localClient->send.bytes >> 10,
965 source_p->localClient->recv.bytes >> 10);
966 ilog(LOG_TYPE_IRCD, "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
967 source_p->name, (int)(CurrentTime - source_p->localClient->firsttime),
968 source_p->localClient->send.bytes >> 10,
969 source_p->localClient->recv.bytes >> 10);
970 }
971 }
972 else if (IsClient(source_p) && !HasFlag(source_p, FLAGS_KILLED))
973 {
974 sendto_server(from->from, CAP_TS6, NOCAPS,
975 ":%s QUIT :%s", ID(source_p), comment);
976 sendto_server(from->from, NOCAPS, CAP_TS6,
977 ":%s QUIT :%s", source_p->name, comment);
978 }
979
980 /* The client *better* be off all of the lists */
981 assert(dlinkFind(&unknown_list, source_p) == NULL);
982 assert(dlinkFind(&local_client_list, source_p) == NULL);
983 assert(dlinkFind(&serv_list, source_p) == NULL);
984 assert(dlinkFind(&oper_list, source_p) == NULL);
985
986 exit_one_client(source_p, comment);
987 }
988
989 /*
990 * dead_link_on_write - report a write error if not already dead,
991 * mark it as dead then exit it
992 */
993 void
994 dead_link_on_write(struct Client *client_p, int ierrno)
995 {
996 dlink_node *ptr;
997
998 if (IsDefunct(client_p))
999 return;
1000
1001 dbuf_clear(&client_p->localClient->buf_recvq);
1002 dbuf_clear(&client_p->localClient->buf_sendq);
1003
1004 assert(dlinkFind(&abort_list, client_p) == NULL);
1005 ptr = make_dlink_node();
1006 /* don't let exit_aborted_clients() finish yet */
1007 dlinkAddTail(client_p, ptr, &abort_list);
1008
1009 if (eac_next == NULL)
1010 eac_next = ptr;
1011
1012 SetDead(client_p); /* You are dead my friend */
1013 }
1014
1015 /*
1016 * dead_link_on_read - report a read error if not already dead,
1017 * mark it as dead then exit it
1018 */
1019 void
1020 dead_link_on_read(struct Client *client_p, int error)
1021 {
1022 char errmsg[255];
1023 int current_error;
1024
1025 if (IsDefunct(client_p))
1026 return;
1027
1028 dbuf_clear(&client_p->localClient->buf_recvq);
1029 dbuf_clear(&client_p->localClient->buf_sendq);
1030
1031 current_error = get_sockerr(client_p->localClient->fd.fd);
1032
1033 if (IsServer(client_p) || IsHandshake(client_p))
1034 {
1035 int connected = CurrentTime - client_p->localClient->firsttime;
1036
1037 if (error == 0)
1038 {
1039 /* Admins get the real IP */
1040 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1041 "Server %s closed the connection",
1042 get_client_name(client_p, SHOW_IP));
1043
1044 /* Opers get a masked IP */
1045 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1046 "Server %s closed the connection",
1047 get_client_name(client_p, MASK_IP));
1048
1049 ilog(LOG_TYPE_IRCD, "Server %s closed the connection",
1050 get_client_name(client_p, SHOW_IP));
1051 }
1052 else
1053 {
1054 report_error(L_ADMIN, "Lost connection to %s: %s",
1055 get_client_name(client_p, SHOW_IP), current_error);
1056 report_error(L_OPER, "Lost connection to %s: %s",
1057 get_client_name(client_p, MASK_IP), current_error);
1058 }
1059
1060 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1061 "%s had been connected for %d day%s, %2d:%02d:%02d",
1062 client_p->name, connected/86400,
1063 (connected/86400 == 1) ? "" : "s",
1064 (connected % 86400) / 3600, (connected % 3600) / 60,
1065 connected % 60);
1066 }
1067
1068 if (error == 0)
1069 strlcpy(errmsg, "Remote host closed the connection",
1070 sizeof(errmsg));
1071 else
1072 snprintf(errmsg, sizeof(errmsg), "Read error: %s",
1073 strerror(current_error));
1074
1075 exit_client(client_p, &me, errmsg);
1076 }
1077
1078 void
1079 exit_aborted_clients(void)
1080 {
1081 dlink_node *ptr;
1082 struct Client *target_p;
1083 const char *notice;
1084
1085 DLINK_FOREACH_SAFE(ptr, eac_next, abort_list.head)
1086 {
1087 target_p = ptr->data;
1088 eac_next = ptr->next;
1089
1090 if (target_p == NULL)
1091 {
1092 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1093 "Warning: null client on abort_list!");
1094 dlinkDelete(ptr, &abort_list);
1095 free_dlink_node(ptr);
1096 continue;
1097 }
1098
1099 dlinkDelete(ptr, &abort_list);
1100
1101 if (IsSendQExceeded(target_p))
1102 notice = "Max SendQ exceeded";
1103 else
1104 notice = "Write error: connection closed";
1105
1106 exit_client(target_p, &me, notice);
1107 free_dlink_node(ptr);
1108 }
1109 }
1110
1111 /*
1112 * accept processing, this adds a form of "caller ID" to ircd
1113 *
1114 * If a client puts themselves into "caller ID only" mode,
1115 * only clients that match a client pointer they have put on
1116 * the accept list will be allowed to message them.
1117 *
1118 * Diane Bruce, "Dianora" db@db.net
1119 */
1120
1121 void
1122 del_accept(struct split_nuh_item *accept_p, struct Client *client_p)
1123 {
1124 dlinkDelete(&accept_p->node, &client_p->localClient->acceptlist);
1125
1126 MyFree(accept_p->nickptr);
1127 MyFree(accept_p->userptr);
1128 MyFree(accept_p->hostptr);
1129 MyFree(accept_p);
1130 }
1131
1132 struct split_nuh_item *
1133 find_accept(const char *nick, const char *user,
1134 const char *host, struct Client *client_p, int do_match)
1135 {
1136 dlink_node *ptr = NULL;
1137 /* XXX We wouldn't need that if match() would return 0 on match */
1138 int (*cmpfunc)(const char *, const char *) = do_match ? match : irccmp;
1139
1140 DLINK_FOREACH(ptr, client_p->localClient->acceptlist.head)
1141 {
1142 struct split_nuh_item *accept_p = ptr->data;
1143
1144 if (cmpfunc(accept_p->nickptr, nick) == do_match &&
1145 cmpfunc(accept_p->userptr, user) == do_match &&
1146 cmpfunc(accept_p->hostptr, host) == do_match)
1147 return accept_p;
1148 }
1149
1150 return NULL;
1151 }
1152
1153 /* accept_message()
1154 *
1155 * inputs - pointer to source client
1156 * - pointer to target client
1157 * output - 1 if accept this message 0 if not
1158 * side effects - See if source is on target's allow list
1159 */
1160 int
1161 accept_message(struct Client *source,
1162 struct Client *target)
1163 {
1164 dlink_node *ptr = NULL;
1165
1166 if (source == target || find_accept(source->name, source->username,
1167 source->host, target, 1))
1168 return 1;
1169
1170 if (HasUMode(target, UMODE_SOFTCALLERID))
1171 DLINK_FOREACH(ptr, target->channel.head)
1172 if (IsMember(source, ((struct Membership *)ptr->data)->chptr))
1173 return 1;
1174
1175 return 0;
1176 }
1177
1178 /* del_all_accepts()
1179 *
1180 * inputs - pointer to exiting client
1181 * output - NONE
1182 * side effects - Walk through given clients acceptlist and remove all entries
1183 */
1184 void
1185 del_all_accepts(struct Client *client_p)
1186 {
1187 dlink_node *ptr = NULL, *next_ptr = NULL;
1188
1189 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->acceptlist.head)
1190 del_accept(ptr->data, client_p);
1191 }

Properties

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