ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 3929
Committed: Mon Jun 9 14:56:25 2014 UTC (12 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 33169 byte(s)
Log Message:
- Greatly speedup d-line lookup. Instead of testing every single client against
  every existing d-line just check the just added ban against connected clients.
  Also now check d-line _before_ looking for a matching exempt{}

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 2916 * Copyright (c) 1997-2014 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * USA
20     */
21    
22 michael 2916 /*! \file client.c
23     * \brief Controls clients.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 1011 #include "list.h"
29 adx 30 #include "client.h"
30     #include "event.h"
31     #include "hash.h"
32     #include "irc_string.h"
33     #include "ircd.h"
34     #include "numeric.h"
35 michael 3324 #include "auth.h"
36 adx 30 #include "s_bsd.h"
37 michael 1309 #include "conf.h"
38     #include "log.h"
39 michael 3347 #include "misc.h"
40     #include "server.h"
41 adx 30 #include "send.h"
42     #include "whowas.h"
43 michael 3347 #include "user.h"
44 adx 30 #include "memory.h"
45 michael 1654 #include "mempool.h"
46 adx 30 #include "hostmask.h"
47     #include "listener.h"
48     #include "userhost.h"
49 michael 876 #include "watch.h"
50 michael 1783 #include "rng_mt.h"
51 michael 2682 #include "parse.h"
52 adx 30
53     dlink_list listing_client_list = { NULL, NULL, 0 };
54     /* Pointer to beginning of Client list */
55     dlink_list global_client_list = {NULL, NULL, 0};
56     /* unknown/client pointer lists */
57     dlink_list unknown_list = {NULL, NULL, 0};
58     dlink_list local_client_list = {NULL, NULL, 0};
59     dlink_list serv_list = {NULL, NULL, 0};
60     dlink_list global_serv_list = {NULL, NULL, 0};
61     dlink_list oper_list = {NULL, NULL, 0};
62    
63     static EVH check_pings;
64    
65 michael 1654 static mp_pool_t *client_pool = NULL;
66     static mp_pool_t *lclient_pool = NULL;
67 adx 30
68     static dlink_list dead_list = { NULL, NULL, 0};
69     static dlink_list abort_list = { NULL, NULL, 0};
70    
71     static dlink_node *eac_next; /* next aborted client to exit */
72    
73     static void check_pings_list(dlink_list *);
74     static void check_unknowns_list(void);
75    
76    
77 michael 1798 /* client_init()
78 adx 30 *
79     * inputs - NONE
80     * output - NONE
81     * side effects - initialize client free memory
82     */
83     void
84 michael 1798 client_init(void)
85 adx 30 {
86     /* start off the check ping event .. -- adrian
87     * Every 30 seconds is plenty -- db
88     */
89 michael 1654 client_pool = mp_pool_new(sizeof(struct Client), MP_CHUNK_SIZE_CLIENT);
90     lclient_pool = mp_pool_new(sizeof(struct LocalUser), MP_CHUNK_SIZE_LCLIENT);
91 adx 30 eventAdd("check_pings", check_pings, NULL, 5);
92     }
93    
94     /*
95     * make_client - create a new Client struct and set it to initial state.
96     *
97     * from == NULL, create local client (a client connected
98     * to a socket).
99     * WARNING: This leaves the client in a dangerous
100     * state where fd == -1, dead flag is not set and
101     * the client is on the unknown_list; therefore,
102     * the first thing to do after calling make_client(NULL)
103     * is setting fd to something reasonable. -adx
104     *
105     * from, create remote client (behind a socket
106     * associated with the client defined by
107     * 'from'). ('from' is a local client!!).
108     */
109     struct Client *
110     make_client(struct Client *from)
111     {
112 michael 1654 struct Client *client_p = mp_pool_get(client_pool);
113 adx 30
114 michael 1654 memset(client_p, 0, sizeof(*client_p));
115    
116 michael 3235 if (!from)
117 adx 30 {
118 michael 1241 client_p->from = client_p; /* 'from' of local client is self! */
119 michael 1654 client_p->localClient = mp_pool_get(lclient_pool);
120    
121     memset(client_p->localClient, 0, sizeof(*client_p->localClient));
122    
123 michael 1241 client_p->localClient->since = CurrentTime;
124     client_p->localClient->lasttime = CurrentTime;
125     client_p->localClient->firsttime = CurrentTime;
126     client_p->localClient->registration = REG_INIT;
127 adx 30
128     /* as good a place as any... */
129 michael 1126 dlinkAdd(client_p, &client_p->localClient->lclient_node, &unknown_list);
130 adx 30 }
131     else
132     client_p->from = from; /* 'from' of local client is self! */
133    
134 michael 1360 client_p->idhnext = client_p;
135 adx 30 client_p->hnext = client_p;
136 michael 2678 SetUnknown(client_p);
137 adx 30 strcpy(client_p->username, "unknown");
138 michael 1559 strcpy(client_p->svid, "0");
139 adx 30
140     return client_p;
141     }
142    
143     /*
144     * free_client
145     *
146     * inputs - pointer to client
147     * output - NONE
148     * side effects - client pointed to has its memory freed
149     */
150     static void
151     free_client(struct Client *client_p)
152     {
153 michael 3235 assert(client_p);
154 adx 30 assert(client_p != &me);
155     assert(client_p->hnext == client_p);
156 michael 1360 assert(client_p->idhnext == client_p);
157 adx 30 assert(client_p->channel.head == NULL);
158     assert(dlink_list_length(&client_p->channel) == 0);
159 michael 1360 assert(dlink_list_length(&client_p->whowas) == 0);
160 michael 3502 assert(!IsServer(client_p) || client_p->serv);
161 adx 30
162     MyFree(client_p->serv);
163 michael 2229 MyFree(client_p->certfp);
164 adx 30
165     if (MyConnect(client_p))
166     {
167 michael 317 assert(client_p->localClient->invited.head == NULL);
168     assert(dlink_list_length(&client_p->localClient->invited) == 0);
169 michael 2345 assert(dlink_list_length(&client_p->localClient->watches) == 0);
170 adx 30 assert(IsClosing(client_p) && IsDead(client_p));
171    
172     MyFree(client_p->localClient->response);
173     MyFree(client_p->localClient->auth_oper);
174    
175     /*
176     * clean up extra sockets from P-lines which have been discarded.
177     */
178     if (client_p->localClient->listener)
179     {
180     assert(0 < client_p->localClient->listener->ref_count);
181     if (0 == --client_p->localClient->listener->ref_count &&
182 michael 2345 !client_p->localClient->listener->active)
183 adx 30 free_listener(client_p->localClient->listener);
184     }
185    
186     dbuf_clear(&client_p->localClient->buf_recvq);
187     dbuf_clear(&client_p->localClient->buf_sendq);
188    
189 michael 1654 mp_pool_release(client_p->localClient);
190 adx 30 }
191    
192 michael 1654 mp_pool_release(client_p);
193 adx 30 }
194    
195     /*
196     * check_pings - go through the local client list and check activity
197     * kill off stuff that should die
198     *
199     * inputs - NOT USED (from event)
200     * output - next time_t when check_pings() should be called again
201 michael 2345 * side effects -
202 adx 30 *
203     *
204     * A PING can be sent to clients as necessary.
205     *
206     * Client/Server ping outs are handled.
207     */
208    
209     /*
210     * Addon from adrian. We used to call this after nextping seconds,
211     * however I've changed it to run once a second. This is only for
212     * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
213     * run once a second makes life a lot easier - when a new client connects
214     * and they need a ping in 4 seconds, if nextping was set to 20 seconds
215     * we end up waiting 20 seconds. This is stupid. :-)
216     * I will optimise (hah!) check_pings() once I've finished working on
217     * tidying up other network IO evilnesses.
218     * -- adrian
219     */
220    
221     static void
222     check_pings(void *notused)
223 michael 2345 {
224 adx 30 check_pings_list(&local_client_list);
225     check_pings_list(&serv_list);
226     check_unknowns_list();
227     }
228    
229     /* check_pings_list()
230     *
231     * inputs - pointer to list to check
232     * output - NONE
233 michael 2345 * side effects -
234 adx 30 */
235     static void
236     check_pings_list(dlink_list *list)
237     {
238 michael 2691 char scratch[IRCD_BUFSIZE];
239 michael 1644 int ping = 0; /* ping time value from client */
240     dlink_node *ptr = NULL, *next_ptr = NULL;
241 adx 30
242     DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
243     {
244 michael 1644 struct Client *client_p = ptr->data;
245 adx 30
246     /*
247     ** Note: No need to notify opers here. It's
248     ** already done when "FLAGS_DEADSOCKET" is set.
249     */
250     if (IsDead(client_p))
251     {
252     /* Ignore it, its been exited already */
253 michael 2345 continue;
254 adx 30 }
255    
256     if (!IsRegistered(client_p))
257 michael 1644 ping = CONNECTTIMEOUT;
258 adx 30 else
259 michael 1644 ping = get_client_ping(&client_p->localClient->confs);
260 adx 30
261 michael 1241 if (ping < CurrentTime - client_p->localClient->lasttime)
262 adx 30 {
263     if (!IsPingSent(client_p))
264     {
265 michael 2182 /*
266     * if we havent PINGed the connection and we havent
267     * heard from it in a while, PING it to make sure
268     * it is still alive.
269     */
270     SetPingSent(client_p);
271     client_p->localClient->lasttime = CurrentTime - ping;
272     sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
273 adx 30 }
274     else
275     {
276 michael 1241 if (CurrentTime - client_p->localClient->lasttime >= 2 * ping)
277 adx 30 {
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 michael 2182 {
284     sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
285     "No response from %s, closing link",
286     get_client_name(client_p, HIDE_IP));
287     sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
288     "No response from %s, closing link",
289     get_client_name(client_p, MASK_IP));
290     ilog(LOG_TYPE_IRCD, "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 michael 1241 (int)(CurrentTime - client_p->localClient->lasttime));
296 michael 3171 exit_client(client_p, scratch);
297 adx 30 }
298     }
299     }
300     }
301     }
302    
303     /* check_unknowns_list()
304     *
305     * inputs - pointer to list of unknown clients
306     * output - NONE
307     * side effects - unknown clients get marked for termination after n seconds
308     */
309     static void
310     check_unknowns_list(void)
311     {
312 michael 3235 dlink_node *ptr = NULL, *ptr_next = NULL;
313 adx 30
314 michael 3235 DLINK_FOREACH_SAFE(ptr, ptr_next, unknown_list.head)
315 adx 30 {
316     struct Client *client_p = ptr->data;
317    
318 michael 650 /*
319     * Check UNKNOWN connections - if they have been in this state
320 adx 30 * for > 30s, close them.
321     */
322 michael 1241 if (IsAuthFinished(client_p) && (CurrentTime - client_p->localClient->firsttime) > 30)
323 michael 3171 exit_client(client_p, "Registration timed out");
324 adx 30 }
325     }
326    
327     /* check_conf_klines()
328     *
329     * inputs - NONE
330     * output - NONE
331     * side effects - Check all connections for a pending kline against the
332     * client, exit the client if a kline matches.
333     */
334 michael 2345 void
335 adx 30 check_conf_klines(void)
336 michael 2345 {
337 michael 1632 struct MaskItem *conf = NULL;
338 michael 3929 dlink_node *ptr = NULL, *ptr_next = NULL;
339 adx 30
340 michael 3929 DLINK_FOREACH_SAFE(ptr, ptr_next, local_client_list.head)
341 adx 30 {
342 michael 3929 struct Client *client_p = ptr->data;
343 adx 30
344     /* If a client is already being exited
345     */
346     if (IsDead(client_p) || !IsClient(client_p))
347     continue;
348    
349 michael 3929 if ((conf = find_conf_by_address(NULL, &client_p->localClient->ip, CONF_DLINE,
350     client_p->localClient->aftype, NULL, NULL, 1)))
351 adx 30 {
352 michael 2813 conf_try_ban(client_p, conf);
353 adx 30 continue; /* and go examine next fd/client_p */
354     }
355    
356 michael 2522 if (ConfigFileEntry.glines)
357 adx 30 {
358 michael 2739 if ((conf = find_conf_by_address(client_p->host, &client_p->localClient->ip,
359     CONF_GLINE, client_p->localClient->aftype,
360     client_p->username, NULL, 1)))
361     {
362 michael 2813 conf_try_ban(client_p, conf);
363 michael 2739 /* and go examine next fd/client_p */
364 adx 30 continue;
365     }
366 michael 2345 }
367 adx 30
368 michael 2522 if ((conf = find_conf_by_address(client_p->host, &client_p->localClient->ip,
369     CONF_KLINE, client_p->localClient->aftype,
370 michael 2739 client_p->username, NULL, 1)))
371 adx 30 {
372 michael 2813 conf_try_ban(client_p, conf);
373 michael 2345 continue;
374 adx 30 }
375    
376 michael 1632 if ((conf = find_matching_name_conf(CONF_XLINE, client_p->info,
377 michael 1921 NULL, NULL, 0)))
378 adx 30 {
379 michael 2813 conf_try_ban(client_p, conf);
380 adx 30 continue;
381     }
382     }
383    
384     /* also check the unknowns list for new dlines */
385 michael 3929 DLINK_FOREACH_SAFE(ptr, ptr_next, unknown_list.head)
386 adx 30 {
387 michael 3929 struct Client *client_p = ptr->data;
388 adx 30
389 michael 3929 if ((conf = find_conf_by_address(NULL, &client_p->localClient->ip, CONF_DLINE,
390     client_p->localClient->aftype, NULL, NULL, 1)))
391 adx 30 {
392 michael 3929 conf_try_ban(client_p, conf);
393     continue; /* and go examine next fd/client_p */
394 adx 30 }
395     }
396     }
397    
398     /*
399 michael 2813 * conf_try_ban
400 adx 30 *
401     * inputs - pointer to client to ban
402 michael 1632 * - pointer to MaskItem
403 adx 30 * output - NONE
404     * side effects - given client_p is banned
405     */
406 michael 2811 void
407 michael 2813 conf_try_ban(struct Client *client_p, struct MaskItem *conf)
408 adx 30 {
409 michael 2182 const char *user_reason = NULL; /* What is sent to user */
410 adx 30 const char *type_string = NULL;
411     const char dline_string[] = "D-line";
412     const char kline_string[] = "K-line";
413     const char gline_string[] = "G-line";
414     const char xline_string[] = "X-line";
415    
416     switch (conf->type)
417     {
418 michael 1632 case CONF_KLINE:
419 michael 2811 if (IsExemptKline(client_p))
420     {
421     sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
422     "KLINE over-ruled for %s, client is kline_exempt",
423     get_client_name(client_p, HIDE_IP));
424     return;
425     }
426    
427 adx 30 type_string = kline_string;
428     break;
429 michael 1632 case CONF_DLINE:
430 michael 3929 if (find_conf_by_address(NULL, &client_p->localClient->ip, CONF_EXEMPT,
431     client_p->localClient->aftype, NULL, NULL, 1))
432     return;
433 adx 30 type_string = dline_string;
434     break;
435 michael 1632 case CONF_GLINE:
436 michael 2811 if (IsExemptKline(client_p) ||
437     IsExemptGline(client_p))
438     {
439     sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
440     "GLINE over-ruled for %s, client is %sline_exempt",
441     get_client_name(client_p, HIDE_IP), IsExemptKline(client_p) ? "k" : "g");
442     return;
443     }
444 michael 2916
445 adx 30 type_string = gline_string;
446     break;
447 michael 1632 case CONF_XLINE:
448 adx 30 type_string = xline_string;
449 michael 1632 ++conf->count;
450 adx 30 break;
451     default:
452     assert(0);
453     break;
454     }
455    
456 michael 1632 user_reason = conf->reason ? conf->reason : type_string;
457 adx 30
458 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "%s active for %s",
459 adx 30 type_string, get_client_name(client_p, HIDE_IP));
460    
461     if (IsClient(client_p))
462 michael 3109 sendto_one_numeric(client_p, &me, ERR_YOUREBANNEDCREEP, user_reason);
463 adx 30
464 michael 3171 exit_client(client_p, user_reason);
465 adx 30 }
466    
467     /* update_client_exit_stats()
468     *
469     * input - pointer to client
470     * output - NONE
471 michael 2345 * side effects -
472 adx 30 */
473     static void
474     update_client_exit_stats(struct Client *client_p)
475     {
476 michael 1143 if (IsClient(client_p))
477 adx 30 {
478 michael 1013 assert(Count.total > 0);
479 adx 30 --Count.total;
480 michael 1219 if (HasUMode(client_p, UMODE_OPER))
481 adx 30 --Count.oper;
482 michael 1219 if (HasUMode(client_p, UMODE_INVISIBLE))
483 adx 30 --Count.invisi;
484     }
485 michael 1143 else if (IsServer(client_p))
486 michael 1618 sendto_realops_flags(UMODE_EXTERNAL, L_ALL, SEND_NOTICE,
487     "Server %s split from %s",
488 michael 1143 client_p->name, client_p->servptr->name);
489 adx 30
490     if (splitchecking && !splitmode)
491     check_splitmode(NULL);
492     }
493    
494     /* find_person()
495     *
496     * inputs - pointer to name
497     * output - return client pointer
498     * side effects - find person by (nick)name
499     */
500     struct Client *
501 michael 3170 find_person(const struct Client *const source_p, const char *name)
502 adx 30 {
503 michael 2475 struct Client *target_p = NULL;
504 adx 30
505     if (IsDigit(*name))
506     {
507 michael 3207 if (IsServer(source_p->from))
508 michael 2586 target_p = hash_find_id(name);
509 adx 30 }
510     else
511 michael 2475 target_p = hash_find_client(name);
512 adx 30
513 michael 2475 return (target_p && IsClient(target_p)) ? target_p : NULL;
514 adx 30 }
515    
516     /*
517 michael 2475 * find_chasing - find the client structure for a nick name (name)
518 michael 2345 * using history mechanism if necessary. If the client is not found,
519 michael 3192 * an error message (NO SUCH NICK) is generated.
520 adx 30 */
521     struct Client *
522 michael 3192 find_chasing(struct Client *source_p, const char *name)
523 adx 30 {
524 michael 3160 struct Client *who = find_person(source_p, name);
525 adx 30
526     if (who)
527 michael 1169 return who;
528 adx 30
529 michael 2475 if (IsDigit(*name))
530 michael 1169 return NULL;
531 adx 30
532 michael 2475 if ((who = whowas_get_history(name,
533 michael 2182 (time_t)ConfigFileEntry.kill_chase_time_limit))
534     == NULL)
535 adx 30 {
536 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOSUCHNICK, name);
537 michael 1169 return NULL;
538 adx 30 }
539    
540 michael 1169 return who;
541 adx 30 }
542    
543     /*
544     * get_client_name - Return the name of the client
545     * for various tracking and
546     * admin purposes. The main purpose of this function is to
547     * return the "socket host" name of the client, if that
548     * differs from the advertised name (other than case).
549     * But, this can be used to any client structure.
550     *
551     * NOTE 1:
552     * Watch out the allocation of "nbuf", if either source_p->name
553     * or source_p->sockhost gets changed into pointers instead of
554     * directly allocated within the structure...
555     *
556     * NOTE 2:
557     * Function return either a pointer to the structure (source_p) or
558     * to internal buffer (nbuf). *NEVER* use the returned pointer
559     * to modify what it points!!!
560     */
561     const char *
562 michael 3235 get_client_name(const struct Client *client_p, enum addr_mask_type type)
563 adx 30 {
564     static char nbuf[HOSTLEN * 2 + USERLEN + 5];
565    
566 michael 3235 assert(client_p);
567 adx 30
568 michael 3235 if (!MyConnect(client_p))
569     return client_p->name;
570 adx 30
571 michael 3235 if (IsServer(client_p) || IsConnecting(client_p) || IsHandshake(client_p))
572 michael 1340 {
573 michael 3235 if (!irccmp(client_p->name, client_p->host))
574     return client_p->name;
575 michael 1340 else if (ConfigServerHide.hide_server_ips)
576 michael 1328 type = MASK_IP;
577 michael 1340 }
578 adx 30
579     if (ConfigFileEntry.hide_spoof_ips)
580 michael 3730 if (IsIPSpoof(client_p) && type == SHOW_IP)
581 michael 1328 type = MASK_IP;
582 adx 30
583     /* And finally, let's get the host information, ip or name */
584 michael 1328 switch (type)
585 adx 30 {
586     case SHOW_IP:
587 michael 1339 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
588 michael 3235 client_p->name,
589     client_p->username, client_p->sockhost);
590 michael 1339 break;
591 adx 30 case MASK_IP:
592 michael 3235 if (client_p->localClient->aftype == AF_INET)
593 michael 1339 snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
594 michael 3235 client_p->name, client_p->username);
595 michael 1339 else
596     snprintf(nbuf, sizeof(nbuf), "%s[%s@ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]",
597 michael 3235 client_p->name, client_p->username);
598 adx 30 break;
599     default:
600 michael 1124 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
601 michael 3235 client_p->name,
602     client_p->username, client_p->host);
603 adx 30 }
604    
605 michael 1124 return nbuf;
606 adx 30 }
607    
608     void
609     free_exited_clients(void)
610     {
611 michael 887 dlink_node *ptr = NULL, *next = NULL;
612 michael 2345
613 adx 30 DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
614     {
615 michael 887 free_client(ptr->data);
616 adx 30 dlinkDelete(ptr, &dead_list);
617     free_dlink_node(ptr);
618     }
619     }
620    
621     /*
622     * Exit one client, local or remote. Assuming all dependents have
623     * been already removed, and socket closed for local client.
624     *
625     * The only messages generated are QUITs on channels.
626     */
627     static void
628     exit_one_client(struct Client *source_p, const char *quitmsg)
629     {
630 michael 3235 dlink_node *ptr = NULL, *ptr_next = NULL;
631 adx 30
632     assert(!IsMe(source_p));
633    
634 michael 1118 if (IsClient(source_p))
635 adx 30 {
636 michael 3202 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->client_list);
637 adx 30
638 michael 1118 /*
639     * If a person is on a channel, send a QUIT notice
640     * to every client (person) on the same channel (so
641     * that the client can show the "**signoff" message).
642     * (Note: The notice is to the local clients *only*)
643     */
644 michael 1734 sendto_common_channels_local(source_p, 0, 0, ":%s!%s@%s QUIT :%s",
645 adx 30 source_p->name, source_p->username,
646     source_p->host, quitmsg);
647 michael 3235 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->channel.head)
648     remove_user_from_channel(ptr->data);
649 adx 30
650 michael 2300 whowas_add_history(source_p, 0);
651     whowas_off_history(source_p);
652 adx 30
653 michael 876 watch_check_hash(source_p, RPL_LOGOFF);
654    
655 michael 889 if (MyConnect(source_p))
656 adx 30 {
657 michael 317 /* Clean up invitefield */
658 michael 3235 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->localClient->invited.head)
659     del_invite(ptr->data, source_p);
660 michael 887
661     del_all_accepts(source_p);
662 michael 317 }
663 adx 30 }
664 michael 1118 else if (IsServer(source_p))
665     {
666     dlinkDelete(&source_p->lnode, &source_p->servptr->serv->server_list);
667 adx 30
668 michael 3235 if ((ptr = dlinkFindDelete(&global_serv_list, source_p)))
669     free_dlink_node(ptr);
670 michael 1118 }
671    
672 adx 30 /* Remove source_p from the client lists */
673 michael 3187 if (source_p->id[0])
674 adx 30 hash_del_id(source_p);
675     if (source_p->name[0])
676     hash_del_client(source_p);
677    
678     if (IsUserHostIp(source_p))
679     delete_user_host(source_p->username, source_p->host, !MyConnect(source_p));
680    
681     /* remove from global client list
682     * NOTE: source_p->node.next cannot be NULL if the client is added
683     * to global_client_list (there is always &me at its end)
684     */
685 michael 3235 if (source_p->node.next) /* XXX: not needed? */
686 adx 30 dlinkDelete(&source_p->node, &global_client_list);
687    
688     update_client_exit_stats(source_p);
689    
690     /* Check to see if the client isn't already on the dead list */
691     assert(dlinkFind(&dead_list, source_p) == NULL);
692    
693     /* add to dead client dlist */
694     SetDead(source_p);
695     dlinkAdd(source_p, make_dlink_node(), &dead_list);
696     }
697    
698 michael 2345 /*
699 adx 30 * Remove all clients that depend on source_p; assumes all (S)QUITs have
700 michael 2345 * already been sent. we make sure to exit a server's dependent clients
701     * and servers before the server itself; exit_one_client takes care of
702 adx 30 * actually removing things off llists. tweaked from +CSr31 -orabidoo
703     */
704     static void
705     recurse_remove_clients(struct Client *source_p, const char *quitmsg)
706     {
707 michael 3235 dlink_node *ptr = NULL, *ptr_next = NULL;
708 adx 30
709 michael 3235 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->client_list.head)
710 adx 30 exit_one_client(ptr->data, quitmsg);
711    
712 michael 3235 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->server_list.head)
713 adx 30 {
714     recurse_remove_clients(ptr->data, quitmsg);
715     exit_one_client(ptr->data, quitmsg);
716     }
717     }
718    
719     /*
720     * exit_client - exit a client of any type. Generally, you can use
721     * this on any struct Client, regardless of its state.
722     *
723     * Note, you shouldn't exit remote _users_ without first doing
724 michael 1219 * AddFlag(x, FLAGS_KILLED) and propagating a kill or similar message.
725 michael 3171 *
726 michael 1219 * However, it is perfectly correct to call exit_client to force a _server_
727 adx 30 * quit (either local or remote one).
728     *
729 michael 3171 *
730 adx 30 * inputs: - a client pointer that is going to be exited
731     * output: none
732     * side effects: the client is delinked from all lists, disconnected,
733     * and the rest of IRC network is notified of the exit.
734     * Client memory is scheduled to be freed
735     */
736     void
737 michael 3171 exit_client(struct Client *source_p, const char *comment)
738 adx 30 {
739 michael 1118 dlink_node *m = NULL;
740 adx 30
741     if (MyConnect(source_p))
742     {
743     /* DO NOT REMOVE. exit_client can be called twice after a failed
744     * read/write.
745     */
746     if (IsClosing(source_p))
747     return;
748    
749     SetClosing(source_p);
750    
751     if (IsIpHash(source_p))
752     remove_one_ip(&source_p->localClient->ip);
753    
754 michael 2181 delete_auth(&source_p->localClient->auth);
755 adx 30
756 michael 1571 /*
757     * This source_p could have status of one of STAT_UNKNOWN, STAT_CONNECTING
758 adx 30 * STAT_HANDSHAKE or STAT_UNKNOWN
759     * all of which are lumped together into unknown_list
760     *
761     * In all above cases IsRegistered() will not be true.
762     */
763     if (!IsRegistered(source_p))
764     {
765 michael 1126 assert(dlinkFind(&unknown_list, source_p));
766    
767     dlinkDelete(&source_p->localClient->lclient_node, &unknown_list);
768 adx 30 }
769     else if (IsClient(source_p))
770     {
771 michael 1247 time_t on_for = CurrentTime - source_p->localClient->firsttime;
772 michael 1013 assert(Count.local > 0);
773 adx 30 Count.local--;
774    
775 michael 1219 if (HasUMode(source_p, UMODE_OPER))
776 michael 3235 if ((m = dlinkFindDelete(&oper_list, source_p)))
777 adx 30 free_dlink_node(m);
778    
779 michael 1126 assert(dlinkFind(&local_client_list, source_p));
780 adx 30 dlinkDelete(&source_p->localClient->lclient_node, &local_client_list);
781 michael 1126
782 michael 3235 if (source_p->localClient->list_task)
783 michael 3289 free_list_task(source_p);
784 adx 30
785 michael 876 watch_del_watch_list(source_p);
786 michael 1618 sendto_realops_flags(UMODE_CCONN, L_ALL, SEND_NOTICE,
787     "Client exiting: %s (%s@%s) [%s] [%s]",
788 adx 30 source_p->name, source_p->username, source_p->host, comment,
789     ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
790     "255.255.255.255" : source_p->sockhost);
791 michael 1247 ilog(LOG_TYPE_USER, "%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu",
792     myctime(source_p->localClient->firsttime), (unsigned int)(on_for / 3600),
793     (unsigned int)((on_for % 3600)/60), (unsigned int)(on_for % 60),
794     source_p->name, source_p->username, source_p->host,
795     source_p->localClient->send.bytes>>10,
796     source_p->localClient->recv.bytes>>10);
797 adx 30 }
798 michael 1571 else if (IsServer(source_p))
799 adx 30 {
800 michael 1571 assert(Count.myserver > 0);
801     --Count.myserver;
802 adx 30
803 michael 1571 assert(dlinkFind(&serv_list, source_p));
804     dlinkDelete(&source_p->localClient->lclient_node, &serv_list);
805 adx 30 }
806    
807     if (!IsDead(source_p))
808     {
809     if (IsServer(source_p))
810     {
811 michael 3171 if (!HasFlag(source_p, FLAGS_SQUIT))
812     {
813     /* for them, we are exiting the network */
814     sendto_one(source_p, ":%s SQUIT %s :%s",
815 michael 3186 me.id, me.id, comment);
816 michael 3171 }
817 adx 30 }
818    
819     sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
820     source_p->host, comment);
821     }
822    
823     close_connection(source_p);
824     }
825 michael 1991 else if (IsClient(source_p) && HasFlag(source_p->servptr, FLAGS_EOB))
826 michael 1976 sendto_realops_flags(UMODE_FARCONNECT, L_ALL, SEND_NOTICE,
827     "Client exiting at %s: %s (%s@%s) [%s]",
828     source_p->servptr->name, source_p->name,
829     source_p->username, source_p->host, comment);
830 adx 30
831     if (IsServer(source_p))
832     {
833 michael 3171 char splitstr[HOSTLEN + HOSTLEN + 2] = "";
834 adx 30
835     /* This shouldn't ever happen */
836 michael 3235 assert(source_p->serv && source_p->servptr);
837 adx 30
838     if (ConfigServerHide.hide_servers)
839 michael 887 /*
840 michael 2345 * Set netsplit message to "*.net *.split" to still show
841 adx 30 * that its a split, but hide the servers splitting
842     */
843 michael 3171 strlcpy(splitstr, "*.net *.split", sizeof(splitstr));
844 adx 30 else
845     snprintf(splitstr, sizeof(splitstr), "%s %s",
846     source_p->servptr->name, source_p->name);
847    
848 michael 3171 /* Send SQUIT for source_p in every direction. source_p is already off of serv_list here */
849     if (!HasFlag(source_p, FLAGS_SQUIT))
850 michael 3186 sendto_server(NULL, NOCAPS, NOCAPS, "SQUIT %s :%s", source_p->id, comment);
851 adx 30
852 michael 3171 /* Now exit the clients internally */
853     recurse_remove_clients(source_p, splitstr);
854    
855 michael 3247 if (MyConnect(source_p))
856 adx 30 {
857 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
858 adx 30 "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
859 michael 1241 source_p->name, (int)(CurrentTime - source_p->localClient->firsttime),
860 adx 30 source_p->localClient->send.bytes >> 10,
861     source_p->localClient->recv.bytes >> 10);
862 michael 1247 ilog(LOG_TYPE_IRCD, "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
863 michael 2345 source_p->name, (int)(CurrentTime - source_p->localClient->firsttime),
864 adx 30 source_p->localClient->send.bytes >> 10,
865     source_p->localClient->recv.bytes >> 10);
866     }
867     }
868 michael 1219 else if (IsClient(source_p) && !HasFlag(source_p, FLAGS_KILLED))
869 michael 3247 sendto_server(source_p->from, NOCAPS, NOCAPS, ":%s QUIT :%s",
870 michael 3186 source_p->id, comment);
871 adx 30
872     /* The client *better* be off all of the lists */
873     assert(dlinkFind(&unknown_list, source_p) == NULL);
874     assert(dlinkFind(&local_client_list, source_p) == NULL);
875     assert(dlinkFind(&serv_list, source_p) == NULL);
876     assert(dlinkFind(&oper_list, source_p) == NULL);
877    
878     exit_one_client(source_p, comment);
879     }
880    
881     /*
882     * dead_link_on_write - report a write error if not already dead,
883     * mark it as dead then exit it
884     */
885     void
886     dead_link_on_write(struct Client *client_p, int ierrno)
887     {
888     dlink_node *ptr;
889    
890     if (IsDefunct(client_p))
891     return;
892    
893     dbuf_clear(&client_p->localClient->buf_recvq);
894     dbuf_clear(&client_p->localClient->buf_sendq);
895    
896     assert(dlinkFind(&abort_list, client_p) == NULL);
897     ptr = make_dlink_node();
898     /* don't let exit_aborted_clients() finish yet */
899     dlinkAddTail(client_p, ptr, &abort_list);
900    
901     if (eac_next == NULL)
902     eac_next = ptr;
903    
904     SetDead(client_p); /* You are dead my friend */
905     }
906    
907     /*
908     * dead_link_on_read - report a read error if not already dead,
909     * mark it as dead then exit it
910     */
911     void
912     dead_link_on_read(struct Client *client_p, int error)
913     {
914 michael 2691 char errmsg[IRCD_BUFSIZE];
915 adx 30 int current_error;
916    
917     if (IsDefunct(client_p))
918     return;
919    
920     dbuf_clear(&client_p->localClient->buf_recvq);
921     dbuf_clear(&client_p->localClient->buf_sendq);
922    
923     current_error = get_sockerr(client_p->localClient->fd.fd);
924    
925     if (IsServer(client_p) || IsHandshake(client_p))
926     {
927 michael 1241 int connected = CurrentTime - client_p->localClient->firsttime;
928 michael 2345
929 adx 30 if (error == 0)
930     {
931     /* Admins get the real IP */
932 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
933 michael 2182 "Server %s closed the connection",
934     get_client_name(client_p, SHOW_IP));
935 adx 30
936     /* Opers get a masked IP */
937 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
938 michael 2182 "Server %s closed the connection",
939     get_client_name(client_p, MASK_IP));
940 adx 30
941 michael 1247 ilog(LOG_TYPE_IRCD, "Server %s closed the connection",
942 michael 2182 get_client_name(client_p, SHOW_IP));
943 adx 30 }
944     else
945     {
946 michael 617 report_error(L_ADMIN, "Lost connection to %s: %s",
947 michael 2182 get_client_name(client_p, SHOW_IP), current_error);
948 michael 617 report_error(L_OPER, "Lost connection to %s: %s",
949 michael 2182 get_client_name(client_p, MASK_IP), current_error);
950 adx 30 }
951    
952 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
953 michael 2182 "%s had been connected for %d day%s, %2d:%02d:%02d",
954     client_p->name, connected/86400,
955     (connected/86400 == 1) ? "" : "s",
956     (connected % 86400) / 3600, (connected % 3600) / 60,
957     connected % 60);
958 adx 30 }
959    
960     if (error == 0)
961     strlcpy(errmsg, "Remote host closed the connection",
962     sizeof(errmsg));
963     else
964 michael 1124 snprintf(errmsg, sizeof(errmsg), "Read error: %s",
965     strerror(current_error));
966 adx 30
967 michael 3171 exit_client(client_p, errmsg);
968 adx 30 }
969    
970     void
971     exit_aborted_clients(void)
972     {
973     dlink_node *ptr;
974     struct Client *target_p;
975     const char *notice;
976    
977     DLINK_FOREACH_SAFE(ptr, eac_next, abort_list.head)
978     {
979     target_p = ptr->data;
980     eac_next = ptr->next;
981    
982     if (target_p == NULL)
983     {
984 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
985 adx 30 "Warning: null client on abort_list!");
986     dlinkDelete(ptr, &abort_list);
987     free_dlink_node(ptr);
988     continue;
989     }
990    
991     dlinkDelete(ptr, &abort_list);
992    
993     if (IsSendQExceeded(target_p))
994     notice = "Max SendQ exceeded";
995     else
996     notice = "Write error: connection closed";
997    
998 michael 3171 exit_client(target_p, notice);
999 adx 30 free_dlink_node(ptr);
1000     }
1001     }
1002    
1003     /*
1004     * accept processing, this adds a form of "caller ID" to ircd
1005 michael 887 *
1006 adx 30 * If a client puts themselves into "caller ID only" mode,
1007 michael 887 * only clients that match a client pointer they have put on
1008 adx 30 * the accept list will be allowed to message them.
1009     *
1010 michael 887 * Diane Bruce, "Dianora" db@db.net
1011 adx 30 */
1012    
1013 michael 887 void
1014     del_accept(struct split_nuh_item *accept_p, struct Client *client_p)
1015 adx 30 {
1016 michael 887 dlinkDelete(&accept_p->node, &client_p->localClient->acceptlist);
1017 adx 30
1018 michael 887 MyFree(accept_p->nickptr);
1019     MyFree(accept_p->userptr);
1020     MyFree(accept_p->hostptr);
1021     MyFree(accept_p);
1022     }
1023 adx 30
1024 michael 887 struct split_nuh_item *
1025     find_accept(const char *nick, const char *user,
1026 michael 2363 const char *host, struct Client *client_p,
1027     int (*cmpfunc)(const char *, const char *))
1028 michael 887 {
1029     dlink_node *ptr = NULL;
1030 adx 30
1031 michael 887 DLINK_FOREACH(ptr, client_p->localClient->acceptlist.head)
1032 adx 30 {
1033 michael 887 struct split_nuh_item *accept_p = ptr->data;
1034    
1035 michael 1652 if (!cmpfunc(accept_p->nickptr, nick) &&
1036     !cmpfunc(accept_p->userptr, user) &&
1037     !cmpfunc(accept_p->hostptr, host))
1038 michael 887 return accept_p;
1039 adx 30 }
1040    
1041 michael 887 return NULL;
1042 adx 30 }
1043    
1044 michael 887 /* accept_message()
1045 adx 30 *
1046 michael 887 * inputs - pointer to source client
1047     * - pointer to target client
1048     * output - 1 if accept this message 0 if not
1049     * side effects - See if source is on target's allow list
1050 adx 30 */
1051 michael 887 int
1052     accept_message(struct Client *source,
1053     struct Client *target)
1054 adx 30 {
1055 michael 887 dlink_node *ptr = NULL;
1056 adx 30
1057 michael 887 if (source == target || find_accept(source->name, source->username,
1058 michael 2363 source->host, target, match))
1059 michael 887 return 1;
1060 adx 30
1061 michael 1219 if (HasUMode(target, UMODE_SOFTCALLERID))
1062 michael 887 DLINK_FOREACH(ptr, target->channel.head)
1063     if (IsMember(source, ((struct Membership *)ptr->data)->chptr))
1064     return 1;
1065 adx 30
1066 michael 887 return 0;
1067 adx 30 }
1068    
1069     /* del_all_accepts()
1070     *
1071 michael 887 * inputs - pointer to exiting client
1072     * output - NONE
1073     * side effects - Walk through given clients acceptlist and remove all entries
1074 adx 30 */
1075     void
1076     del_all_accepts(struct Client *client_p)
1077     {
1078 michael 3235 dlink_node *ptr = NULL, *ptr_next = NULL;
1079 adx 30
1080 michael 3235 DLINK_FOREACH_SAFE(ptr, ptr_next, client_p->localClient->acceptlist.head)
1081 michael 887 del_accept(ptr->data, client_p);
1082 adx 30 }
1083 michael 1783
1084     unsigned int
1085     idle_time_get(const struct Client *source_p, const struct Client *target_p)
1086     {
1087     unsigned int idle = 0;
1088     unsigned int min_idle = 0;
1089     unsigned int max_idle = 0;
1090 michael 1847 const struct ClassItem *class = get_class_ptr(&target_p->localClient->confs);
1091 michael 1783
1092 michael 1785 if (!(class->flags & CLASS_FLAGS_FAKE_IDLE) || target_p == source_p)
1093 michael 1783 return CurrentTime - target_p->localClient->last_privmsg;
1094     if (HasUMode(source_p, UMODE_OPER) &&
1095 michael 1785 !(class->flags & CLASS_FLAGS_HIDE_IDLE_FROM_OPERS))
1096 michael 1783 return CurrentTime - target_p->localClient->last_privmsg;
1097    
1098     min_idle = class->min_idle;
1099     max_idle = class->max_idle;
1100    
1101     if (min_idle == max_idle)
1102     return min_idle;
1103    
1104     if (class->flags & CLASS_FLAGS_RANDOM_IDLE)
1105     idle = genrand_int32();
1106     else
1107     idle = CurrentTime - target_p->localClient->last_privmsg;
1108    
1109 michael 3235 if (!max_idle)
1110 michael 1783 idle = 0;
1111     else
1112     idle %= max_idle;
1113    
1114     if (idle < min_idle)
1115     idle = min_idle + (idle % (max_idle - min_idle));
1116    
1117     return idle;
1118     }

Properties

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