ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 4189
Committed: Mon Jul 7 18:30:34 2014 UTC (11 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 32893 byte(s)
Log Message:
- Don't add/remove unregistered connections to/from global_client_list

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

Properties

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