ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 1340
Committed: Fri Apr 6 10:43:23 2012 UTC (12 years ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/client.c
File size: 37535 byte(s)
Log Message:
- get_client_name(): only compare client->name with client->host if it's
  any type of server. Saves thousands of irccmp calls.

File Contents

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

Properties

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