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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1644 - (show annotations)
Tue Nov 6 22:20:16 2012 UTC (7 years, 7 months ago) by michael
File MIME type: text/x-chdr
File size: 34866 byte(s)
- More config subsystem cleanups

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

Properties

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

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