ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/client.c
Revision: 1483
Committed: Wed Jul 25 19:15:48 2012 UTC (11 years, 8 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/client.c
File size: 37764 byte(s)
Log Message:
- Made Client::away a fixed-size array at the expense of a somewhat higher
  memory consumption

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

Properties

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