ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 1185
Committed: Wed Aug 17 23:45:37 2011 UTC (14 years ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/client.c
File size: 39355 byte(s)
Log Message:
- Forward-port r1183: exit_client(): We were accidentally trying to free memory
  pointed by source_p->lclient_node. Fixed.

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

Properties

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