ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.0.x/src/client.c
Revision: 1784
Committed: Sat Jan 26 22:40:14 2013 UTC (13 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 37252 byte(s)
Log Message:
- Fix bug where idle time sometimes is 0 even if the client didn't
  send any private message

File Contents

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

Properties

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