ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 3929
Committed: Mon Jun 9 14:56:25 2014 UTC (11 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 33169 byte(s)
Log Message:
- Greatly speedup d-line lookup. Instead of testing every single client against
  every existing d-line just check the just added ban against connected clients.
  Also now check d-line _before_ looking for a matching exempt{}

File Contents

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

Properties

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