ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 1309
Committed: Sun Mar 25 11:24:18 2012 UTC (12 years ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/client.c
File size: 37315 byte(s)
Log Message:
- renaming files:

  ircd_parser.y -> conf_parser.y
  ircd_lexer.l  -> conf_lexer.l
  s_conf.c      -> conf.c
  s_conf.h      -> conf.h
  s_log.c       -> log.c
  s_log.h       -> log.h

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 1124 get_client_name(const struct Client *client, int showip)
630 adx 30 {
631     static char nbuf[HOSTLEN * 2 + USERLEN + 5];
632    
633     assert(client != NULL);
634    
635     if (irccmp(client->name, client->host) == 0)
636 michael 1124 return client->name;
637 adx 30
638     if (ConfigServerHide.hide_server_ips)
639     if (IsServer(client) || IsConnecting(client) || IsHandshake(client))
640     showip = MASK_IP;
641    
642     if (ConfigFileEntry.hide_spoof_ips)
643     if (showip == SHOW_IP && IsIPSpoof(client))
644     showip = MASK_IP;
645    
646     /* And finally, let's get the host information, ip or name */
647     switch (showip)
648     {
649     case SHOW_IP:
650     if (MyConnect(client))
651     {
652 michael 1124 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
653     client->name,
654     client->username, client->sockhost);
655 adx 30 break;
656     }
657     case MASK_IP:
658 michael 1124 snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
659     client->name, client->username);
660 adx 30 break;
661     default:
662 michael 1124 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
663     client->name,
664     client->username, client->host);
665 adx 30 }
666    
667 michael 1124 return nbuf;
668 adx 30 }
669    
670     void
671     free_exited_clients(void)
672     {
673 michael 887 dlink_node *ptr = NULL, *next = NULL;
674 adx 30
675     DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
676     {
677 michael 887 free_client(ptr->data);
678 adx 30 dlinkDelete(ptr, &dead_list);
679     free_dlink_node(ptr);
680     }
681     }
682    
683     /*
684     * Exit one client, local or remote. Assuming all dependents have
685     * been already removed, and socket closed for local client.
686     *
687     * The only messages generated are QUITs on channels.
688     */
689     static void
690     exit_one_client(struct Client *source_p, const char *quitmsg)
691     {
692     dlink_node *lp = NULL, *next_lp = NULL;
693    
694     assert(!IsMe(source_p));
695    
696 michael 1118 if (IsClient(source_p))
697 adx 30 {
698     if (source_p->servptr->serv != NULL)
699 michael 889 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->client_list);
700 adx 30
701 michael 1118 /*
702     * If a person is on a channel, send a QUIT notice
703     * to every client (person) on the same channel (so
704     * that the client can show the "**signoff" message).
705     * (Note: The notice is to the local clients *only*)
706     */
707 adx 30 sendto_common_channels_local(source_p, 0, ":%s!%s@%s QUIT :%s",
708     source_p->name, source_p->username,
709     source_p->host, quitmsg);
710     DLINK_FOREACH_SAFE(lp, next_lp, source_p->channel.head)
711     remove_user_from_channel(lp->data);
712    
713     add_history(source_p, 0);
714     off_history(source_p);
715    
716 michael 876 watch_check_hash(source_p, RPL_LOGOFF);
717    
718 michael 889 if (MyConnect(source_p))
719 adx 30 {
720 michael 317 /* Clean up invitefield */
721     DLINK_FOREACH_SAFE(lp, next_lp, source_p->localClient->invited.head)
722     del_invite(lp->data, source_p);
723 michael 887
724     del_all_accepts(source_p);
725 michael 317 }
726 adx 30 }
727 michael 1118 else if (IsServer(source_p))
728     {
729     dlinkDelete(&source_p->lnode, &source_p->servptr->serv->server_list);
730 adx 30
731 michael 1118 if ((lp = dlinkFindDelete(&global_serv_list, source_p)) != NULL)
732     free_dlink_node(lp);
733     }
734    
735 adx 30 /* Remove source_p from the client lists */
736     if (HasID(source_p))
737     hash_del_id(source_p);
738     if (source_p->name[0])
739     hash_del_client(source_p);
740    
741     if (IsUserHostIp(source_p))
742     delete_user_host(source_p->username, source_p->host, !MyConnect(source_p));
743    
744     /* remove from global client list
745     * NOTE: source_p->node.next cannot be NULL if the client is added
746     * to global_client_list (there is always &me at its end)
747     */
748     if (source_p != NULL && source_p->node.next != NULL)
749     dlinkDelete(&source_p->node, &global_client_list);
750    
751     update_client_exit_stats(source_p);
752    
753     /* Check to see if the client isn't already on the dead list */
754     assert(dlinkFind(&dead_list, source_p) == NULL);
755    
756     /* add to dead client dlist */
757     SetDead(source_p);
758     dlinkAdd(source_p, make_dlink_node(), &dead_list);
759     }
760    
761     /* Recursively send QUITs and SQUITs for source_p and all its dependent clients
762     * and servers to those servers that need them. A server needs the client
763     * QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
764     * isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
765     * a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
766     * on that one -orabidoo
767     *
768     * This is now called on each local server -adx
769     */
770     static void
771     recurse_send_quits(struct Client *original_source_p, struct Client *source_p,
772     struct Client *from, struct Client *to, const char *comment,
773 michael 1118 const char *splitstr)
774 adx 30 {
775     dlink_node *ptr, *next;
776     struct Client *target_p;
777 michael 1118 int hidden = match(me.name, source_p->name); /* XXX */
778 adx 30
779     assert(to != source_p); /* should be already removed from serv_list */
780    
781     /* If this server can handle quit storm (QS) removal
782     * of dependents, just send the SQUIT
783     *
784     * Always check *all* dependent servers if some of them are
785     * hidden behind fakename. If so, send out the QUITs -adx
786     */
787     if (hidden || !IsCapable(to, CAP_QS))
788 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->client_list.head)
789 adx 30 {
790     target_p = ptr->data;
791     sendto_one(to, ":%s QUIT :%s", target_p->name, splitstr);
792     }
793    
794 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->server_list.head)
795 adx 30 recurse_send_quits(original_source_p, ptr->data, from, to,
796 michael 1118 comment, splitstr);
797 adx 30
798     if (!hidden && ((source_p == original_source_p && to != from) ||
799     !IsCapable(to, CAP_QS)))
800     {
801     /* don't use a prefix here - we have to be 100% sure the message
802     * will be accepted without Unknown prefix etc.. */
803     sendto_one(to, "SQUIT %s :%s", ID_or_name(source_p, to), comment);
804     }
805     }
806    
807     /*
808     * Remove all clients that depend on source_p; assumes all (S)QUITs have
809     * already been sent. we make sure to exit a server's dependent clients
810     * and servers before the server itself; exit_one_client takes care of
811     * actually removing things off llists. tweaked from +CSr31 -orabidoo
812     */
813     static void
814     recurse_remove_clients(struct Client *source_p, const char *quitmsg)
815     {
816     dlink_node *ptr, *next;
817    
818 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->client_list.head)
819 adx 30 exit_one_client(ptr->data, quitmsg);
820    
821 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->server_list.head)
822 adx 30 {
823     recurse_remove_clients(ptr->data, quitmsg);
824     exit_one_client(ptr->data, quitmsg);
825     }
826     }
827    
828     /*
829     ** Remove *everything* that depends on source_p, from all lists, and sending
830     ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
831     ** and its SQUITs have been sent except for the upstream one -orabidoo
832     */
833     static void
834     remove_dependents(struct Client *source_p, struct Client *from,
835     const char *comment, const char *splitstr)
836     {
837 michael 1118 dlink_node *ptr = NULL;
838 adx 30
839     DLINK_FOREACH(ptr, serv_list.head)
840 michael 1118 recurse_send_quits(source_p, source_p, from, ptr->data,
841     comment, splitstr);
842 adx 30
843     recurse_remove_clients(source_p, splitstr);
844     }
845    
846     /*
847     * exit_client - exit a client of any type. Generally, you can use
848     * this on any struct Client, regardless of its state.
849     *
850     * Note, you shouldn't exit remote _users_ without first doing
851 michael 1219 * AddFlag(x, FLAGS_KILLED) and propagating a kill or similar message.
852     * However, it is perfectly correct to call exit_client to force a _server_
853 adx 30 * quit (either local or remote one).
854     *
855     * inputs: - a client pointer that is going to be exited
856     * - for servers, the second argument is a pointer to who
857     * is firing the server. This side won't get any generated
858     * messages. NEVER NULL!
859     * output: none
860     * side effects: the client is delinked from all lists, disconnected,
861     * and the rest of IRC network is notified of the exit.
862     * Client memory is scheduled to be freed
863     */
864     void
865     exit_client(struct Client *source_p, struct Client *from, const char *comment)
866     {
867 michael 1118 dlink_node *m = NULL;
868 adx 30
869     if (MyConnect(source_p))
870     {
871     /* DO NOT REMOVE. exit_client can be called twice after a failed
872     * read/write.
873     */
874     if (IsClosing(source_p))
875     return;
876    
877     SetClosing(source_p);
878    
879     if (IsIpHash(source_p))
880     remove_one_ip(&source_p->localClient->ip);
881    
882 michael 992 if (source_p->localClient->auth)
883     {
884     delete_auth(source_p->localClient->auth);
885     source_p->localClient->auth = NULL;
886     }
887 adx 30
888     /* This source_p could have status of one of STAT_UNKNOWN, STAT_CONNECTING
889     * STAT_HANDSHAKE or STAT_UNKNOWN
890     * all of which are lumped together into unknown_list
891     *
892     * In all above cases IsRegistered() will not be true.
893     */
894     if (!IsRegistered(source_p))
895     {
896 michael 1126 assert(dlinkFind(&unknown_list, source_p));
897    
898     dlinkDelete(&source_p->localClient->lclient_node, &unknown_list);
899 adx 30 }
900     else if (IsClient(source_p))
901     {
902 michael 1247 time_t on_for = CurrentTime - source_p->localClient->firsttime;
903 michael 1013 assert(Count.local > 0);
904 adx 30 Count.local--;
905    
906 michael 1219 if (HasUMode(source_p, UMODE_OPER))
907 adx 30 {
908     if ((m = dlinkFindDelete(&oper_list, source_p)) != NULL)
909     free_dlink_node(m);
910     }
911    
912 michael 1126 assert(dlinkFind(&local_client_list, source_p));
913 adx 30 dlinkDelete(&source_p->localClient->lclient_node, &local_client_list);
914 michael 1126
915 adx 30 if (source_p->localClient->list_task != NULL)
916     free_list_task(source_p->localClient->list_task, source_p);
917    
918 michael 876 watch_del_watch_list(source_p);
919 adx 30 sendto_realops_flags(UMODE_CCONN, L_ALL, "Client exiting: %s (%s@%s) [%s] [%s]",
920     source_p->name, source_p->username, source_p->host, comment,
921     ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
922     "255.255.255.255" : source_p->sockhost);
923 db 853 sendto_realops_flags(UMODE_CCONN_FULL, L_ALL, "CLIEXIT: %s %s %s %s 0 %s",
924     source_p->name,
925     source_p->username,
926     source_p->host,
927 db 849 ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
928 db 853 "255.255.255.255" : source_p->sockhost,
929     comment);
930 michael 1247 ilog(LOG_TYPE_USER, "%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu",
931     myctime(source_p->localClient->firsttime), (unsigned int)(on_for / 3600),
932     (unsigned int)((on_for % 3600)/60), (unsigned int)(on_for % 60),
933     source_p->name, source_p->username, source_p->host,
934     source_p->localClient->send.bytes>>10,
935     source_p->localClient->recv.bytes>>10);
936 adx 30 }
937    
938     /* As soon as a client is known to be a server of some sort
939     * it has to be put on the serv_list, or SJOIN's to this new server
940     * from the connect burst will not be seen.
941 michael 1185 * XXX - TBV. This is not true. The only place where we put a server on
942     * serv_list is in server_estab right now after registration process.
943     * And only after this, a burst is sent to the remote server, i.e. we never
944     * send a burst to STAT_CONNECTING, or STAT_HANDSHAKE. This will need
945     * more investigation later on, but for now, it's not a problem after all.
946 adx 30 */
947     if (IsServer(source_p) || IsConnecting(source_p) ||
948     IsHandshake(source_p))
949     {
950 michael 1185 if (dlinkFind(&serv_list, source_p))
951 adx 30 {
952 michael 1185 dlinkDelete(&source_p->localClient->lclient_node, &serv_list);
953 adx 30 unset_chcap_usage_counts(source_p);
954     }
955    
956     if (IsServer(source_p))
957     Count.myserver--;
958     }
959    
960     if (!IsDead(source_p))
961     {
962     if (IsServer(source_p))
963     {
964     /* for them, we are exiting the network */
965     sendto_one(source_p, ":%s SQUIT %s :%s",
966     ID_or_name(from, source_p), me.name, comment);
967     }
968    
969     sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
970     source_p->host, comment);
971     }
972    
973     /*
974     ** Currently only server connections can have
975     ** depending remote clients here, but it does no
976     ** harm to check for all local clients. In
977     ** future some other clients than servers might
978     ** have remotes too...
979     **
980     ** Close the Client connection first and mark it
981     ** so that no messages are attempted to send to it.
982     ** Remember it makes source_p->from == NULL.
983     */
984     close_connection(source_p);
985     }
986    
987     if (IsServer(source_p))
988     {
989     char splitstr[HOSTLEN + HOSTLEN + 2];
990    
991     /* This shouldn't ever happen */
992     assert(source_p->serv != NULL && source_p->servptr != NULL);
993    
994     if (ConfigServerHide.hide_servers)
995 michael 887 /*
996     * Set netsplit message to "*.net *.split" to still show
997 adx 30 * that its a split, but hide the servers splitting
998     */
999     strcpy(splitstr, "*.net *.split");
1000     else
1001     snprintf(splitstr, sizeof(splitstr), "%s %s",
1002     source_p->servptr->name, source_p->name);
1003    
1004     remove_dependents(source_p, from->from, comment, splitstr);
1005    
1006     if (source_p->servptr == &me)
1007     {
1008     sendto_realops_flags(UMODE_ALL, L_ALL,
1009     "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
1010 michael 1241 source_p->name, (int)(CurrentTime - source_p->localClient->firsttime),
1011 adx 30 source_p->localClient->send.bytes >> 10,
1012     source_p->localClient->recv.bytes >> 10);
1013 michael 1247 ilog(LOG_TYPE_IRCD, "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
1014 michael 1241 source_p->name, (int)(CurrentTime - source_p->localClient->firsttime),
1015 adx 30 source_p->localClient->send.bytes >> 10,
1016     source_p->localClient->recv.bytes >> 10);
1017     }
1018     }
1019 michael 1219 else if (IsClient(source_p) && !HasFlag(source_p, FLAGS_KILLED))
1020 adx 30 {
1021 michael 885 sendto_server(from->from, NULL, CAP_TS6, NOCAPS,
1022 adx 30 ":%s QUIT :%s", ID(source_p), comment);
1023 michael 885 sendto_server(from->from, NULL, NOCAPS, CAP_TS6,
1024 adx 30 ":%s QUIT :%s", source_p->name, comment);
1025     }
1026    
1027     /* The client *better* be off all of the lists */
1028     assert(dlinkFind(&unknown_list, source_p) == NULL);
1029     assert(dlinkFind(&local_client_list, source_p) == NULL);
1030     assert(dlinkFind(&serv_list, source_p) == NULL);
1031     assert(dlinkFind(&oper_list, source_p) == NULL);
1032    
1033     exit_one_client(source_p, comment);
1034     }
1035    
1036     /*
1037     * dead_link_on_write - report a write error if not already dead,
1038     * mark it as dead then exit it
1039     */
1040     void
1041     dead_link_on_write(struct Client *client_p, int ierrno)
1042     {
1043     dlink_node *ptr;
1044    
1045     if (IsDefunct(client_p))
1046     return;
1047    
1048     dbuf_clear(&client_p->localClient->buf_recvq);
1049     dbuf_clear(&client_p->localClient->buf_sendq);
1050    
1051     assert(dlinkFind(&abort_list, client_p) == NULL);
1052     ptr = make_dlink_node();
1053     /* don't let exit_aborted_clients() finish yet */
1054     dlinkAddTail(client_p, ptr, &abort_list);
1055    
1056     if (eac_next == NULL)
1057     eac_next = ptr;
1058    
1059     SetDead(client_p); /* You are dead my friend */
1060     }
1061    
1062     /*
1063     * dead_link_on_read - report a read error if not already dead,
1064     * mark it as dead then exit it
1065     */
1066     void
1067     dead_link_on_read(struct Client *client_p, int error)
1068     {
1069     char errmsg[255];
1070     int current_error;
1071    
1072     if (IsDefunct(client_p))
1073     return;
1074    
1075     dbuf_clear(&client_p->localClient->buf_recvq);
1076     dbuf_clear(&client_p->localClient->buf_sendq);
1077    
1078     current_error = get_sockerr(client_p->localClient->fd.fd);
1079    
1080     if (IsServer(client_p) || IsHandshake(client_p))
1081     {
1082 michael 1241 int connected = CurrentTime - client_p->localClient->firsttime;
1083 adx 30
1084     if (error == 0)
1085     {
1086     /* Admins get the real IP */
1087     sendto_realops_flags(UMODE_ALL, L_ADMIN,
1088     "Server %s closed the connection",
1089     get_client_name(client_p, SHOW_IP));
1090    
1091     /* Opers get a masked IP */
1092     sendto_realops_flags(UMODE_ALL, L_OPER,
1093     "Server %s closed the connection",
1094     get_client_name(client_p, MASK_IP));
1095    
1096 michael 1247 ilog(LOG_TYPE_IRCD, "Server %s closed the connection",
1097 adx 30 get_client_name(client_p, SHOW_IP));
1098     }
1099     else
1100     {
1101 michael 617 report_error(L_ADMIN, "Lost connection to %s: %s",
1102 adx 30 get_client_name(client_p, SHOW_IP), current_error);
1103 michael 617 report_error(L_OPER, "Lost connection to %s: %s",
1104 adx 30 get_client_name(client_p, MASK_IP), current_error);
1105     }
1106    
1107     sendto_realops_flags(UMODE_ALL, L_ALL,
1108     "%s had been connected for %d day%s, %2d:%02d:%02d",
1109     client_p->name, connected/86400,
1110     (connected/86400 == 1) ? "" : "s",
1111     (connected % 86400) / 3600, (connected % 3600) / 60,
1112     connected % 60);
1113     }
1114    
1115     if (error == 0)
1116     strlcpy(errmsg, "Remote host closed the connection",
1117     sizeof(errmsg));
1118     else
1119 michael 1124 snprintf(errmsg, sizeof(errmsg), "Read error: %s",
1120     strerror(current_error));
1121 adx 30
1122     exit_client(client_p, &me, errmsg);
1123     }
1124    
1125     void
1126     exit_aborted_clients(void)
1127     {
1128     dlink_node *ptr;
1129     struct Client *target_p;
1130     const char *notice;
1131    
1132     DLINK_FOREACH_SAFE(ptr, eac_next, abort_list.head)
1133     {
1134     target_p = ptr->data;
1135     eac_next = ptr->next;
1136    
1137     if (target_p == NULL)
1138     {
1139     sendto_realops_flags(UMODE_ALL, L_ALL,
1140     "Warning: null client on abort_list!");
1141     dlinkDelete(ptr, &abort_list);
1142     free_dlink_node(ptr);
1143     continue;
1144     }
1145    
1146     dlinkDelete(ptr, &abort_list);
1147    
1148     if (IsSendQExceeded(target_p))
1149     notice = "Max SendQ exceeded";
1150     else
1151     notice = "Write error: connection closed";
1152    
1153     exit_client(target_p, &me, notice);
1154     free_dlink_node(ptr);
1155     }
1156     }
1157    
1158     /*
1159     * accept processing, this adds a form of "caller ID" to ircd
1160 michael 887 *
1161 adx 30 * If a client puts themselves into "caller ID only" mode,
1162 michael 887 * only clients that match a client pointer they have put on
1163 adx 30 * the accept list will be allowed to message them.
1164     *
1165 michael 887 * Diane Bruce, "Dianora" db@db.net
1166 adx 30 */
1167    
1168 michael 887 void
1169     del_accept(struct split_nuh_item *accept_p, struct Client *client_p)
1170 adx 30 {
1171 michael 887 dlinkDelete(&accept_p->node, &client_p->localClient->acceptlist);
1172 adx 30
1173 michael 887 MyFree(accept_p->nickptr);
1174     MyFree(accept_p->userptr);
1175     MyFree(accept_p->hostptr);
1176     MyFree(accept_p);
1177     }
1178 adx 30
1179 michael 887 struct split_nuh_item *
1180     find_accept(const char *nick, const char *user,
1181     const char *host, struct Client *client_p, int do_match)
1182     {
1183     dlink_node *ptr = NULL;
1184     /* XXX We wouldn't need that if match() would return 0 on match */
1185     int (*cmpfunc)(const char *, const char *) = do_match ? match : irccmp;
1186 adx 30
1187 michael 887 DLINK_FOREACH(ptr, client_p->localClient->acceptlist.head)
1188 adx 30 {
1189 michael 887 struct split_nuh_item *accept_p = ptr->data;
1190    
1191     if (cmpfunc(accept_p->nickptr, nick) == do_match &&
1192     cmpfunc(accept_p->userptr, user) == do_match &&
1193     cmpfunc(accept_p->hostptr, host) == do_match)
1194     return accept_p;
1195 adx 30 }
1196    
1197 michael 887 return NULL;
1198 adx 30 }
1199    
1200 michael 887 /* accept_message()
1201 adx 30 *
1202 michael 887 * inputs - pointer to source client
1203     * - pointer to target client
1204     * output - 1 if accept this message 0 if not
1205     * side effects - See if source is on target's allow list
1206 adx 30 */
1207 michael 887 int
1208     accept_message(struct Client *source,
1209     struct Client *target)
1210 adx 30 {
1211 michael 887 dlink_node *ptr = NULL;
1212 adx 30
1213 michael 887 if (source == target || find_accept(source->name, source->username,
1214     source->host, target, 1))
1215     return 1;
1216 adx 30
1217 michael 1219 if (HasUMode(target, UMODE_SOFTCALLERID))
1218 michael 887 DLINK_FOREACH(ptr, target->channel.head)
1219     if (IsMember(source, ((struct Membership *)ptr->data)->chptr))
1220     return 1;
1221 adx 30
1222 michael 887 return 0;
1223 adx 30 }
1224    
1225     /* del_all_accepts()
1226     *
1227 michael 887 * inputs - pointer to exiting client
1228     * output - NONE
1229     * side effects - Walk through given clients acceptlist and remove all entries
1230 adx 30 */
1231     void
1232     del_all_accepts(struct Client *client_p)
1233     {
1234 michael 887 dlink_node *ptr = NULL, *next_ptr = NULL;
1235 adx 30
1236 michael 887 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->acceptlist.head)
1237     del_accept(ptr->data, client_p);
1238 adx 30 }

Properties

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