/[svn]/branches/newio/src/client.c
ViewVC logotype

Contents of /branches/newio/src/client.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2408 - (show annotations)
Thu Jul 18 19:57:58 2013 UTC (6 years, 11 months ago) by michael
File MIME type: text/x-chdr
File size: 36368 byte(s)
- ioengine changes as of 18JUL13

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

Properties

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

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