ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 4214
Committed: Mon Jul 14 12:35:00 2014 UTC (11 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 32472 byte(s)
Log Message:
- client.c: cleaned up style; sprinkled some assert() statements

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

Properties

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