ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 1514
Committed: Sun Aug 26 09:11:17 2012 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/client.c
File size: 37739 byte(s)
Log Message:
- Removed two outdated/invalid XXX tags

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

Properties

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