ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 8399
Committed: Sun Mar 18 14:43:15 2018 UTC (7 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 35029 byte(s)
Log Message:
- client.c:dead_link_on_read(): replace two report_error with a sendto_realops_flags/ilog combo

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 8279 * Copyright (c) 1997-2018 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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * 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 michael 7304 #include "conf_gecos.h"
39 michael 1309 #include "log.h"
40 michael 3347 #include "misc.h"
41     #include "server.h"
42 adx 30 #include "send.h"
43     #include "whowas.h"
44 michael 3347 #include "user.h"
45 adx 30 #include "memory.h"
46     #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 michael 4325 #include "ipcache.h"
53 adx 30
54    
55 michael 4213 dlink_list listing_client_list;
56     dlink_list unknown_list;
57     dlink_list local_client_list;
58     dlink_list local_server_list;
59     dlink_list global_client_list;
60     dlink_list global_server_list;
61     dlink_list oper_list;
62    
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 michael 7957 client_make(struct Client *from)
84 adx 30 {
85 michael 8385 struct Client *const client_p = xcalloc(sizeof(*client_p));
86 adx 30
87 michael 7797 if (from)
88     client_p->from = from;
89     else
90 adx 30 {
91 michael 5545 client_p->from = client_p; /* 'from' of local client is self! */
92 michael 8385 client_p->connection = xcalloc(sizeof(*client_p->connection));
93 michael 5545 client_p->connection->since = CurrentTime;
94     client_p->connection->lasttime = CurrentTime;
95     client_p->connection->firsttime = CurrentTime;
96 michael 4588 client_p->connection->registration = REG_INIT;
97 adx 30
98     /* as good a place as any... */
99 michael 4588 dlinkAdd(client_p, &client_p->connection->lclient_node, &unknown_list);
100 adx 30 }
101    
102 michael 1360 client_p->idhnext = client_p;
103 michael 5545 client_p->hnext = client_p;
104 michael 2678 SetUnknown(client_p);
105 adx 30 strcpy(client_p->username, "unknown");
106 michael 5731 strcpy(client_p->account, "*");
107 adx 30
108     return client_p;
109     }
110    
111     /*
112 michael 7957 * client_free
113 adx 30 *
114     * inputs - pointer to client
115     * output - NONE
116     * side effects - client pointed to has its memory freed
117     */
118     static void
119 michael 7957 client_free(struct Client *client_p)
120 adx 30 {
121 michael 8286 assert(!IsMe(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 michael 8335
126 michael 8337 assert(client_p->node.data == NULL);
127     assert(client_p->node.prev == NULL);
128     assert(client_p->node.next == NULL);
129 michael 8335
130 michael 8337 assert(client_p->lnode.data == NULL);
131     assert(client_p->lnode.prev == NULL);
132     assert(client_p->lnode.next == NULL);
133 michael 8335
134     assert(dlink_list_length(&client_p->whowas_list) == 0);
135 michael 8286 assert(client_p->whowas_list.head == NULL);
136 michael 8335 assert(client_p->whowas_list.tail == NULL);
137    
138 adx 30 assert(dlink_list_length(&client_p->channel) == 0);
139 michael 8335 assert(client_p->channel.head == NULL);
140     assert(client_p->channel.tail == NULL);
141    
142 michael 5590 assert(dlink_list_length(&client_p->svstags) == 0);
143 michael 8335 assert(client_p->svstags.head == NULL);
144     assert(client_p->svstags.tail == NULL);
145 adx 30
146 michael 8335
147 michael 7032 xfree(client_p->serv);
148     xfree(client_p->certfp);
149 adx 30
150     if (MyConnect(client_p))
151     {
152 michael 8335 assert(client_p->connection->lclient_node.data == NULL);
153     assert(client_p->connection->lclient_node.prev == NULL);
154 michael 8288 assert(client_p->connection->lclient_node.next == NULL);
155 michael 8335
156 michael 8286 assert(client_p->connection->list_task == NULL);
157 michael 8339 assert(client_p->connection->auth == NULL);
158 michael 8335
159     assert(dlink_list_length(&client_p->connection->acceptlist) == 0);
160 michael 8286 assert(client_p->connection->acceptlist.head == NULL);
161 michael 8335 assert(client_p->connection->acceptlist.tail == NULL);
162    
163    
164     assert(dlink_list_length(&client_p->connection->watches) == 0);
165 michael 8286 assert(client_p->connection->watches.head == NULL);
166 michael 8335 assert(client_p->connection->watches.tail == NULL);
167    
168 michael 8286 assert(dlink_list_length(&client_p->connection->confs) == 0);
169 michael 8335 assert(client_p->connection->confs.head == NULL);
170     assert(client_p->connection->confs.tail == NULL);
171    
172 michael 4588 assert(dlink_list_length(&client_p->connection->invited) == 0);
173 michael 8335 assert(client_p->connection->invited.head == NULL);
174     assert(client_p->connection->invited.tail == NULL);
175    
176 michael 8339 assert(client_p->connection->fd == NULL);
177 michael 8335
178 michael 6470 assert(HasFlag(client_p, FLAGS_CLOSING) && IsDead(client_p));
179 adx 30
180     /*
181 michael 5589 * Clean up extra sockets from listen {} blocks which have been discarded.
182 adx 30 */
183 michael 4588 if (client_p->connection->listener)
184 adx 30 {
185 michael 4588 listener_release(client_p->connection->listener);
186     client_p->connection->listener = NULL;
187 adx 30 }
188    
189 michael 4588 dbuf_clear(&client_p->connection->buf_recvq);
190     dbuf_clear(&client_p->connection->buf_sendq);
191 adx 30
192 michael 8385 xfree(client_p->connection);
193 michael 8311 client_p->connection = NULL;
194 adx 30 }
195    
196 michael 8385 xfree(client_p);
197 adx 30 }
198    
199 michael 8373 static void
200     svstag_free(struct ServicesTag *svstag, dlink_list *list)
201     {
202     dlinkDelete(&svstag->node, list);
203     xfree(svstag->tag);
204     xfree(svstag);
205     }
206    
207 michael 5551 void
208 michael 8373 client_detach_svstag(dlink_list *list, unsigned int numeric)
209     {
210     dlink_node *node, *node_next;
211    
212     DLINK_FOREACH_SAFE(node, node_next, list->head)
213     {
214     struct ServicesTag *svstag = node->data;
215    
216     if (svstag->numeric == numeric)
217     svstag_free(svstag, list);
218     }
219     }
220    
221     void
222     client_attach_svstag(dlink_list *list, unsigned int numeric,
223 michael 7340 const char *umodes, const char *tag)
224 michael 5555 {
225 michael 5558 const struct user_modes *tab = NULL;
226 michael 5555
227 michael 5558 if (numeric >= ERR_LAST_ERR_MSG || *umodes != '+')
228 michael 5555 return;
229    
230 michael 7076 struct ServicesTag *svstag = xcalloc(sizeof(*svstag));
231 michael 5555 svstag->numeric = numeric;
232     svstag->tag = xstrdup(tag);
233    
234 michael 5558 for (const char *m = umodes + 1; *m; ++m)
235     if ((tab = umode_map[(unsigned char)*m]))
236     svstag->umodes |= tab->flag;
237    
238 michael 5555 if (numeric != RPL_WHOISOPERATOR)
239 michael 8373 dlinkAddTail(svstag, &svstag->node, list);
240 michael 5555 else
241 michael 8373 dlinkAdd(svstag, &svstag->node, list);
242 michael 5555 }
243    
244     void
245 michael 8373 client_clear_svstags(dlink_list *list)
246 michael 5551 {
247 michael 8373 while (list->head)
248     svstag_free(list->head->data, list);
249 michael 5551 }
250    
251 adx 30 /* check_pings_list()
252     *
253     * inputs - pointer to list to check
254     * output - NONE
255 michael 2345 * side effects -
256 adx 30 */
257     static void
258     check_pings_list(dlink_list *list)
259     {
260 michael 6961 char buf[32] = ""; /* 32 = sizeof("Ping timeout: 999999999 seconds") */
261 michael 7330 unsigned int ping = 0; /* ping time value from client */
262 michael 4800 dlink_node *node = NULL, *node_next = NULL;
263 adx 30
264 michael 4800 DLINK_FOREACH_SAFE(node, node_next, list->head)
265 adx 30 {
266 michael 4800 struct Client *client_p = node->data;
267 adx 30
268     if (IsDead(client_p))
269 michael 4214 continue; /* Ignore it, its been exited already */
270 adx 30
271 michael 6325 if (IsClient(client_p) || IsServer(client_p))
272     ping = get_client_ping(&client_p->connection->confs);
273     else
274 michael 1644 ping = CONNECTTIMEOUT;
275 adx 30
276 michael 4588 if (ping < CurrentTime - client_p->connection->lasttime)
277 adx 30 {
278 michael 6313 if (!HasFlag(client_p, FLAGS_PINGSENT))
279 adx 30 {
280 michael 2182 /*
281 michael 4298 * If we haven't PINGed the connection and we haven't
282 michael 2182 * heard from it in a while, PING it to make sure
283     * it is still alive.
284     */
285 michael 6313 AddFlag(client_p, FLAGS_PINGSENT);
286 michael 4588 client_p->connection->lasttime = CurrentTime - ping;
287 michael 2182 sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
288 adx 30 }
289     else
290     {
291 michael 4588 if (CurrentTime - client_p->connection->lasttime >= 2 * ping)
292 adx 30 {
293     /*
294     * If the client/server hasn't talked to us in 2*ping seconds
295     * and it has a ping time, then close its connection.
296     */
297     if (IsServer(client_p) || IsHandshake(client_p))
298 michael 2182 {
299 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
300 michael 2182 "No response from %s, closing link",
301 michael 7997 client_get_name(client_p, SHOW_IP));
302 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
303 michael 2182 "No response from %s, closing link",
304 michael 7997 client_get_name(client_p, MASK_IP));
305 michael 2182 ilog(LOG_TYPE_IRCD, "No response from %s, closing link",
306 michael 7997 client_get_name(client_p, SHOW_IP));
307 michael 2182 }
308 michael 650
309 michael 6854 snprintf(buf, sizeof(buf), "Ping timeout: %ji seconds",
310     (CurrentTime - client_p->connection->lasttime));
311 michael 4214 exit_client(client_p, buf);
312 adx 30 }
313     }
314     }
315     }
316     }
317    
318     /* check_unknowns_list()
319     *
320     * inputs - pointer to list of unknown clients
321     * output - NONE
322     * side effects - unknown clients get marked for termination after n seconds
323     */
324     static void
325     check_unknowns_list(void)
326     {
327 michael 4800 dlink_node *node = NULL, *node_next = NULL;
328 adx 30
329 michael 4800 DLINK_FOREACH_SAFE(node, node_next, unknown_list.head)
330 adx 30 {
331 michael 4800 struct Client *client_p = node->data;
332 adx 30
333 michael 650 /*
334     * Check UNKNOWN connections - if they have been in this state
335 adx 30 * for > 30s, close them.
336     */
337 michael 6470 if (HasFlag(client_p, FLAGS_FINISHED_AUTH) && (CurrentTime - client_p->connection->firsttime) > 30)
338 michael 3171 exit_client(client_p, "Registration timed out");
339 adx 30 }
340     }
341    
342 michael 4214 /*
343     * check_pings - go through the local client list and check activity
344     * kill off stuff that should die
345     *
346     * inputs - NOT USED (from event)
347     * side effects -
348     *
349     *
350     * A PING can be sent to clients as necessary.
351     *
352     * Client/Server ping outs are handled.
353     */
354    
355     /*
356     * Addon from adrian. We used to call this after nextping seconds,
357     * however I've changed it to run once a second. This is only for
358     * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
359     * run once a second makes life a lot easier - when a new client connects
360     * and they need a ping in 4 seconds, if nextping was set to 20 seconds
361     * we end up waiting 20 seconds. This is stupid. :-)
362     * I will optimise (hah!) check_pings() once I've finished working on
363     * tidying up other network IO evilnesses.
364     * -- adrian
365     */
366    
367     static void
368 michael 4439 check_pings(void *unused)
369 michael 4214 {
370     check_pings_list(&local_client_list);
371     check_pings_list(&local_server_list);
372     check_unknowns_list();
373     }
374    
375 adx 30 /* check_conf_klines()
376     *
377     * inputs - NONE
378     * output - NONE
379     * side effects - Check all connections for a pending kline against the
380     * client, exit the client if a kline matches.
381     */
382 michael 2345 void
383 adx 30 check_conf_klines(void)
384 michael 2345 {
385 michael 4800 dlink_node *node = NULL, *node_next = NULL;
386 michael 7431 const void *ptr;
387 adx 30
388 michael 4800 DLINK_FOREACH_SAFE(node, node_next, local_client_list.head)
389 adx 30 {
390 michael 4800 struct Client *client_p = node->data;
391 adx 30
392 michael 4823 /* If a client is already being exited */
393 michael 4725 if (IsDead(client_p))
394 adx 30 continue;
395    
396 michael 7304 if ((ptr = find_conf_by_address(NULL, &client_p->connection->ip, CONF_DLINE,
397     client_p->connection->aftype, NULL, NULL, 1)))
398 adx 30 {
399 michael 7304 const struct MaskItem *conf = ptr;
400     conf_try_ban(client_p, CLIENT_BAN_DLINE, conf->reason);
401 michael 5732 continue; /* and go examine next Client */
402 adx 30 }
403    
404 michael 7304 if ((ptr = find_conf_by_address(client_p->host, &client_p->connection->ip,
405     CONF_KLINE, client_p->connection->aftype,
406     client_p->username, NULL, 1)))
407 adx 30 {
408 michael 7304 const struct MaskItem *conf = ptr;
409     conf_try_ban(client_p, CLIENT_BAN_KLINE, conf->reason);
410 michael 5732 continue; /* and go examine next Client */
411 adx 30 }
412    
413 michael 7304 if ((ptr = gecos_find(client_p->info, match)))
414 adx 30 {
415 michael 7304 const struct GecosItem *conf = ptr;
416     conf_try_ban(client_p, CLIENT_BAN_XLINE, conf->reason);
417 michael 5732 continue; /* and go examine next Client */
418 adx 30 }
419     }
420    
421 michael 5732 /* Also check the unknowns list for new dlines */
422 michael 4800 DLINK_FOREACH_SAFE(node, node_next, unknown_list.head)
423 adx 30 {
424 michael 4800 struct Client *client_p = node->data;
425 adx 30
426 michael 7304 if ((ptr = find_conf_by_address(NULL, &client_p->connection->ip, CONF_DLINE,
427     client_p->connection->aftype, NULL, NULL, 1)))
428 adx 30 {
429 michael 7304 const struct MaskItem *conf = ptr;
430     conf_try_ban(client_p, CLIENT_BAN_DLINE, conf->reason);
431 michael 5732 continue; /* and go examine next Client */
432 adx 30 }
433     }
434     }
435    
436     /*
437 michael 2813 * conf_try_ban
438 adx 30 *
439     * inputs - pointer to client to ban
440 michael 1632 * - pointer to MaskItem
441 adx 30 * output - NONE
442     * side effects - given client_p is banned
443     */
444 michael 2811 void
445 michael 7304 conf_try_ban(struct Client *client_p, int type, const char *reason)
446 adx 30 {
447 michael 6943 char ban_type = '?';
448 adx 30
449 michael 7304 switch (type)
450 adx 30 {
451 michael 7304 case CLIENT_BAN_KLINE:
452 michael 6313 if (HasFlag(client_p, FLAGS_EXEMPTKLINE))
453 michael 2811 {
454 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
455 michael 2811 "KLINE over-ruled for %s, client is kline_exempt",
456 michael 7997 client_get_name(client_p, HIDE_IP));
457 michael 2811 return;
458     }
459    
460 michael 5979 ban_type = 'K';
461 adx 30 break;
462 michael 7304 case CLIENT_BAN_DLINE:
463 michael 4588 if (find_conf_by_address(NULL, &client_p->connection->ip, CONF_EXEMPT,
464     client_p->connection->aftype, NULL, NULL, 1))
465 michael 3929 return;
466 michael 5979 ban_type = 'D';
467 adx 30 break;
468 michael 7304 case CLIENT_BAN_XLINE:
469 michael 6313 if (HasFlag(client_p, FLAGS_EXEMPTXLINE))
470 michael 5985 {
471 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
472 michael 5985 "XLINE over-ruled for %s, client is xline_exempt",
473 michael 7997 client_get_name(client_p, HIDE_IP));
474 michael 5985 return;
475     }
476 michael 6313
477 michael 5979 ban_type = 'X';
478 adx 30 break;
479     default:
480     assert(0);
481     break;
482     }
483    
484 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, "%c-line active for %s",
485 michael 7997 ban_type, client_get_name(client_p, HIDE_IP));
486 adx 30
487     if (IsClient(client_p))
488 michael 7304 sendto_one_numeric(client_p, &me, ERR_YOUREBANNEDCREEP, reason);
489 adx 30
490 michael 7304 exit_client(client_p, reason);
491 adx 30 }
492    
493     /* find_person()
494     *
495     * inputs - pointer to name
496     * output - return client pointer
497     * side effects - find person by (nick)name
498     */
499     struct Client *
500 michael 7345 find_person(const struct Client *source_p, const char *name)
501 adx 30 {
502 michael 2475 struct Client *target_p = NULL;
503 adx 30
504     if (IsDigit(*name))
505     {
506 michael 3207 if (IsServer(source_p->from))
507 michael 2586 target_p = hash_find_id(name);
508 adx 30 }
509     else
510 michael 2475 target_p = hash_find_client(name);
511 adx 30
512 michael 2475 return (target_p && IsClient(target_p)) ? target_p : NULL;
513 adx 30 }
514    
515     /*
516 michael 2475 * find_chasing - find the client structure for a nick name (name)
517 michael 2345 * using history mechanism if necessary. If the client is not found,
518 michael 3192 * an error message (NO SUCH NICK) is generated.
519 adx 30 */
520     struct Client *
521 michael 3192 find_chasing(struct Client *source_p, const char *name)
522 adx 30 {
523 michael 4884 struct Client *target_p = find_person(source_p, name);
524 adx 30
525 michael 4884 if (target_p)
526     return target_p;
527 adx 30
528 michael 2475 if (IsDigit(*name))
529 michael 1169 return NULL;
530 adx 30
531 michael 7464 target_p = whowas_get_history(name, ConfigGeneral.kill_chase_time_limit);
532 michael 4884 if (!target_p)
533 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOSUCHNICK, name);
534 adx 30
535 michael 4884 return target_p;
536 adx 30 }
537    
538     /*
539 michael 7997 * client_get_name - Return the name of the client
540 adx 30 * for various tracking and
541     * admin purposes. The main purpose of this function is to
542     * return the "socket host" name of the client, if that
543     * differs from the advertised name (other than case).
544     * But, this can be used to any client structure.
545     *
546     * NOTE 1:
547 michael 4214 * Watch out the allocation of "buf", if either source_p->name
548 adx 30 * or source_p->sockhost gets changed into pointers instead of
549     * directly allocated within the structure...
550     *
551     * NOTE 2:
552     * Function return either a pointer to the structure (source_p) or
553 michael 4214 * to internal buffer (buf). *NEVER* use the returned pointer
554 adx 30 * to modify what it points!!!
555     */
556     const char *
557 michael 7997 client_get_name(const struct Client *client_p, enum addr_mask_type type)
558 adx 30 {
559 michael 6963 static char buf[HOSTLEN * 2 + USERLEN + 4]; /* +4 for [,@,],\0 */
560 adx 30
561 michael 3235 if (!MyConnect(client_p))
562     return client_p->name;
563 adx 30
564 michael 3235 if (IsServer(client_p) || IsConnecting(client_p) || IsHandshake(client_p))
565 michael 1340 {
566 michael 3235 if (!irccmp(client_p->name, client_p->host))
567     return client_p->name;
568 michael 1340 else if (ConfigServerHide.hide_server_ips)
569 michael 1328 type = MASK_IP;
570 michael 1340 }
571 adx 30
572     /* And finally, let's get the host information, ip or name */
573 michael 1328 switch (type)
574 adx 30 {
575     case SHOW_IP:
576 michael 4214 snprintf(buf, sizeof(buf), "%s[%s@%s]",
577 michael 3235 client_p->name,
578     client_p->username, client_p->sockhost);
579 michael 1339 break;
580 adx 30 case MASK_IP:
581 michael 4588 if (client_p->connection->aftype == AF_INET)
582 michael 4214 snprintf(buf, sizeof(buf), "%s[%s@255.255.255.255]",
583 michael 3235 client_p->name, client_p->username);
584 michael 1339 else
585 michael 4214 snprintf(buf, sizeof(buf), "%s[%s@ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]",
586 michael 3235 client_p->name, client_p->username);
587 adx 30 break;
588 michael 6310 default: /* HIDE_IP */
589 michael 4214 snprintf(buf, sizeof(buf), "%s[%s@%s]",
590 michael 3235 client_p->name,
591     client_p->username, client_p->host);
592 adx 30 }
593    
594 michael 4214 return buf;
595 adx 30 }
596    
597     void
598     free_exited_clients(void)
599     {
600 michael 8059 dlink_node *node, *node_next;
601 michael 2345
602 michael 4800 DLINK_FOREACH_SAFE(node, node_next, dead_list.head)
603 adx 30 {
604 michael 7957 client_free(node->data);
605 michael 4800 dlinkDelete(node, &dead_list);
606     free_dlink_node(node);
607 adx 30 }
608     }
609    
610     /*
611 michael 8393 * client_close_connection
612     * Close the physical connection. This function must make
613     * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
614     */
615     static void
616     client_close_connection(struct Client *client_p)
617     {
618     assert(client_p);
619    
620     if (!IsDead(client_p))
621     {
622     /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
623     /* there is still a chance that we might send data to this socket
624     * even if it is marked as blocked (COMM_SELECT_READ handler is called
625     * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
626     */
627     DelFlag(client_p, FLAGS_BLOCKED);
628     send_queued_write(client_p);
629     }
630    
631     if (IsClient(client_p))
632     {
633     ++ServerStats.is_cl;
634     ServerStats.is_cbs += client_p->connection->send.bytes;
635     ServerStats.is_cbr += client_p->connection->recv.bytes;
636     ServerStats.is_cti += CurrentTime - client_p->connection->firsttime;
637     }
638     else if (IsServer(client_p))
639     {
640     dlink_node *node = NULL;
641    
642     ++ServerStats.is_sv;
643     ServerStats.is_sbs += client_p->connection->send.bytes;
644     ServerStats.is_sbr += client_p->connection->recv.bytes;
645     ServerStats.is_sti += CurrentTime - client_p->connection->firsttime;
646    
647     DLINK_FOREACH(node, connect_items.head)
648     {
649     struct MaskItem *conf = node->data;
650    
651     if (irccmp(conf->name, client_p->name))
652     continue;
653    
654     /*
655     * Reset next-connect cycle of all connect{} blocks that match
656     * this servername.
657     */
658     conf->until = CurrentTime + conf->class->con_freq;
659     }
660     }
661     else
662     ++ServerStats.is_ni;
663    
664     if (tls_isusing(&client_p->connection->fd->ssl))
665     tls_shutdown(&client_p->connection->fd->ssl);
666    
667     if (client_p->connection->fd)
668     {
669     fd_close(client_p->connection->fd);
670     client_p->connection->fd = NULL;
671     }
672    
673     dbuf_clear(&client_p->connection->buf_sendq);
674     dbuf_clear(&client_p->connection->buf_recvq);
675    
676     xfree(client_p->connection->password);
677     client_p->connection->password = NULL;
678    
679 michael 8395 conf_detach(client_p, CONF_CLIENT | CONF_OPER | CONF_SERVER);
680 michael 8393 }
681    
682     /*
683 adx 30 * Exit one client, local or remote. Assuming all dependents have
684     * been already removed, and socket closed for local client.
685     *
686     * The only messages generated are QUITs on channels.
687     */
688     static void
689 michael 4214 exit_one_client(struct Client *source_p, const char *comment)
690 adx 30 {
691 michael 8059 dlink_node *node, *node_next;
692 adx 30
693     assert(!IsMe(source_p));
694 michael 4214 assert(source_p != &me);
695 adx 30
696 michael 1118 if (IsClient(source_p))
697 adx 30 {
698 michael 7961 if (HasUMode(source_p, UMODE_OPER))
699     --Count.oper;
700     if (HasUMode(source_p, UMODE_INVISIBLE))
701     --Count.invisi;
702    
703 michael 3202 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->client_list);
704 michael 4189 dlinkDelete(&source_p->node, &global_client_list);
705 adx 30
706 michael 1118 /*
707     * If a person is on a channel, send a QUIT notice
708     * to every client (person) on the same channel (so
709     * that the client can show the "**signoff" message).
710     * (Note: The notice is to the local clients *only*)
711     */
712 michael 6774 sendto_common_channels_local(source_p, 0, 0, 0, ":%s!%s@%s QUIT :%s",
713 adx 30 source_p->name, source_p->username,
714 michael 4214 source_p->host, comment);
715 michael 4884
716 michael 4800 DLINK_FOREACH_SAFE(node, node_next, source_p->channel.head)
717     remove_user_from_channel(node->data);
718 adx 30
719 michael 8373 client_clear_svstags(&source_p->svstags);
720 michael 6942
721 michael 2300 whowas_add_history(source_p, 0);
722     whowas_off_history(source_p);
723 adx 30
724 michael 876 watch_check_hash(source_p, RPL_LOGOFF);
725 adx 30 }
726 michael 1118 else if (IsServer(source_p))
727     {
728 michael 7961 sendto_realops_flags(UMODE_EXTERNAL, L_ALL, SEND_NOTICE,
729     "Server %s split from %s",
730     source_p->name, source_p->servptr->name);
731    
732 michael 1118 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->server_list);
733 michael 7963 dlinkDelete(&source_p->node, &global_server_list);
734 michael 1118 }
735    
736 adx 30 /* Remove source_p from the client lists */
737 michael 3187 if (source_p->id[0])
738 adx 30 hash_del_id(source_p);
739 michael 4884
740 adx 30 if (source_p->name[0])
741     hash_del_client(source_p);
742    
743 michael 6313 if (HasFlag(source_p, FLAGS_USERHOST))
744 michael 7624 userhost_del(source_p->sockhost, !MyConnect(source_p));
745 adx 30
746     /* Check to see if the client isn't already on the dead list */
747     assert(dlinkFind(&dead_list, source_p) == NULL);
748    
749     /* add to dead client dlist */
750     SetDead(source_p);
751     dlinkAdd(source_p, make_dlink_node(), &dead_list);
752     }
753    
754 michael 2345 /*
755 adx 30 * Remove all clients that depend on source_p; assumes all (S)QUITs have
756 michael 2345 * already been sent. we make sure to exit a server's dependent clients
757     * and servers before the server itself; exit_one_client takes care of
758 adx 30 * actually removing things off llists. tweaked from +CSr31 -orabidoo
759     */
760     static void
761 michael 4214 recurse_remove_clients(struct Client *source_p, const char *comment)
762 adx 30 {
763 michael 8059 dlink_node *node, *node_next;
764 adx 30
765 michael 4800 DLINK_FOREACH_SAFE(node, node_next, source_p->serv->client_list.head)
766     exit_one_client(node->data, comment);
767 adx 30
768 michael 4800 DLINK_FOREACH_SAFE(node, node_next, source_p->serv->server_list.head)
769 adx 30 {
770 michael 4800 recurse_remove_clients(node->data, comment);
771     exit_one_client(node->data, comment);
772 adx 30 }
773     }
774    
775     /*
776     * exit_client - exit a client of any type. Generally, you can use
777     * this on any struct Client, regardless of its state.
778     *
779     * Note, you shouldn't exit remote _users_ without first doing
780 michael 1219 * AddFlag(x, FLAGS_KILLED) and propagating a kill or similar message.
781 michael 3171 *
782 michael 1219 * However, it is perfectly correct to call exit_client to force a _server_
783 adx 30 * quit (either local or remote one).
784     *
785 michael 3171 *
786 adx 30 * inputs: - a client pointer that is going to be exited
787     * output: none
788     * side effects: the client is delinked from all lists, disconnected,
789     * and the rest of IRC network is notified of the exit.
790     * Client memory is scheduled to be freed
791     */
792     void
793 michael 3171 exit_client(struct Client *source_p, const char *comment)
794 adx 30 {
795 michael 4214 assert(!IsMe(source_p));
796     assert(source_p != &me);
797    
798 adx 30 if (MyConnect(source_p))
799     {
800 michael 8059 /*
801     * DO NOT REMOVE. exit_client can be called twice after a failed read/write.
802 adx 30 */
803 michael 6470 if (HasFlag(source_p, FLAGS_CLOSING))
804 adx 30 return;
805    
806 michael 6470 AddFlag(source_p, FLAGS_CLOSING);
807 adx 30
808 michael 4400 if (HasFlag(source_p, FLAGS_IPHASH))
809     {
810     DelFlag(source_p, FLAGS_IPHASH);
811 michael 4588 ipcache_remove_address(&source_p->connection->ip);
812 michael 4400 }
813 adx 30
814 michael 8339 if (source_p->connection->auth)
815     {
816     auth_delete(source_p->connection->auth);
817     source_p->connection->auth = NULL;
818     }
819 adx 30
820 michael 6325 if (IsClient(source_p))
821 adx 30 {
822 michael 8294 dlink_node *node;
823    
824 michael 1219 if (HasUMode(source_p, UMODE_OPER))
825 michael 4800 if ((node = dlinkFindDelete(&oper_list, source_p)))
826     free_dlink_node(node);
827 adx 30
828 michael 1126 assert(dlinkFind(&local_client_list, source_p));
829 michael 4588 dlinkDelete(&source_p->connection->lclient_node, &local_client_list);
830 michael 1126
831 michael 4588 if (source_p->connection->list_task)
832 michael 3289 free_list_task(source_p);
833 adx 30
834 michael 8377 clear_invite_list(&source_p->connection->invited);
835     del_all_accepts(source_p);
836 michael 876 watch_del_watch_list(source_p);
837 michael 5551
838 michael 1618 sendto_realops_flags(UMODE_CCONN, L_ALL, SEND_NOTICE,
839     "Client exiting: %s (%s@%s) [%s] [%s]",
840 michael 8223 source_p->name, source_p->username, source_p->realhost,
841 michael 6853 source_p->sockhost, comment);
842 michael 4884
843 michael 7337 ilog(LOG_TYPE_USER, "%s (%ju): %s!%s@%s %s %s %ju/%ju :%s",
844     date_ctime(source_p->connection->firsttime),
845     CurrentTime - source_p->connection->firsttime,
846 michael 1247 source_p->name, source_p->username, source_p->host,
847 michael 7337 source_p->sockhost, source_p->account,
848     source_p->connection->send.bytes >> 10,
849     source_p->connection->recv.bytes >> 10, source_p->info);
850 adx 30 }
851 michael 1571 else if (IsServer(source_p))
852 adx 30 {
853 michael 4213 assert(dlinkFind(&local_server_list, source_p));
854 michael 4588 dlinkDelete(&source_p->connection->lclient_node, &local_server_list);
855 adx 30 }
856 michael 6325 else
857     {
858     assert(dlinkFind(&unknown_list, source_p));
859     dlinkDelete(&source_p->connection->lclient_node, &unknown_list);
860     }
861 adx 30
862     if (!IsDead(source_p))
863     {
864     if (IsServer(source_p))
865     {
866 michael 3171 if (!HasFlag(source_p, FLAGS_SQUIT))
867     {
868     /* for them, we are exiting the network */
869     sendto_one(source_p, ":%s SQUIT %s :%s",
870 michael 3186 me.id, me.id, comment);
871 michael 3171 }
872 adx 30 }
873    
874     sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
875     source_p->host, comment);
876     }
877    
878 michael 8393 client_close_connection(source_p);
879 adx 30 }
880 michael 1991 else if (IsClient(source_p) && HasFlag(source_p->servptr, FLAGS_EOB))
881 michael 1976 sendto_realops_flags(UMODE_FARCONNECT, L_ALL, SEND_NOTICE,
882 michael 6853 "Client exiting at %s: %s (%s@%s) [%s] [%s]",
883 michael 1976 source_p->servptr->name, source_p->name,
884 michael 8223 source_p->username, source_p->realhost, source_p->sockhost, comment);
885 adx 30
886     if (IsServer(source_p))
887     {
888 michael 3171 char splitstr[HOSTLEN + HOSTLEN + 2] = "";
889 adx 30
890 michael 5590 assert(source_p->serv);
891     assert(source_p->servptr);
892 adx 30
893     if (ConfigServerHide.hide_servers)
894 michael 887 /*
895 michael 2345 * Set netsplit message to "*.net *.split" to still show
896 adx 30 * that its a split, but hide the servers splitting
897     */
898 michael 3171 strlcpy(splitstr, "*.net *.split", sizeof(splitstr));
899 adx 30 else
900     snprintf(splitstr, sizeof(splitstr), "%s %s",
901     source_p->servptr->name, source_p->name);
902    
903 michael 4213 /* Send SQUIT for source_p in every direction. source_p is already off of local_server_list here */
904 michael 3171 if (!HasFlag(source_p, FLAGS_SQUIT))
905 michael 4962 sendto_server(NULL, 0, 0, "SQUIT %s :%s", source_p->id, comment);
906 adx 30
907 michael 3171 /* Now exit the clients internally */
908     recurse_remove_clients(source_p, splitstr);
909    
910 michael 3247 if (MyConnect(source_p))
911 adx 30 {
912 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
913 michael 6782 "%s was connected for %s. %ju/%ju sendK/recvK.",
914 michael 6569 source_p->name, time_dissect(CurrentTime - source_p->connection->firsttime),
915 michael 4588 source_p->connection->send.bytes >> 10,
916     source_p->connection->recv.bytes >> 10);
917 michael 6782 ilog(LOG_TYPE_IRCD, "%s was connected for %s. %ju/%ju sendK/recvK.",
918 michael 6569 source_p->name, time_dissect(CurrentTime - source_p->connection->firsttime),
919 michael 5589 source_p->connection->send.bytes >> 10,
920     source_p->connection->recv.bytes >> 10);
921 adx 30 }
922     }
923 michael 1219 else if (IsClient(source_p) && !HasFlag(source_p, FLAGS_KILLED))
924 michael 7870 sendto_server(source_p->from, 0, 0, ":%s QUIT :%s", source_p->id, comment);
925 adx 30
926     /* The client *better* be off all of the lists */
927     assert(dlinkFind(&unknown_list, source_p) == NULL);
928     assert(dlinkFind(&local_client_list, source_p) == NULL);
929 michael 4213 assert(dlinkFind(&local_server_list, source_p) == NULL);
930 adx 30 assert(dlinkFind(&oper_list, source_p) == NULL);
931 michael 8294 assert(dlinkFind(&listing_client_list, source_p) == NULL);
932 adx 30
933     exit_one_client(source_p, comment);
934     }
935    
936     /*
937     * dead_link_on_write - report a write error if not already dead,
938     * mark it as dead then exit it
939     */
940     void
941     dead_link_on_write(struct Client *client_p, int ierrno)
942     {
943 michael 4800 dlink_node *node;
944 adx 30
945     if (IsDefunct(client_p))
946     return;
947    
948 michael 4588 dbuf_clear(&client_p->connection->buf_recvq);
949     dbuf_clear(&client_p->connection->buf_sendq);
950 adx 30
951     assert(dlinkFind(&abort_list, client_p) == NULL);
952 michael 4800 node = make_dlink_node();
953 adx 30 /* don't let exit_aborted_clients() finish yet */
954 michael 4800 dlinkAddTail(client_p, node, &abort_list);
955 adx 30
956     if (eac_next == NULL)
957 michael 4800 eac_next = node;
958 adx 30
959     SetDead(client_p); /* You are dead my friend */
960     }
961    
962     /*
963     * dead_link_on_read - report a read error if not already dead,
964     * mark it as dead then exit it
965     */
966     void
967     dead_link_on_read(struct Client *client_p, int error)
968     {
969 michael 2691 char errmsg[IRCD_BUFSIZE];
970 adx 30 int current_error;
971    
972     if (IsDefunct(client_p))
973     return;
974    
975 michael 4588 dbuf_clear(&client_p->connection->buf_recvq);
976     dbuf_clear(&client_p->connection->buf_sendq);
977 adx 30
978 michael 8339 current_error = get_sockerr(client_p->connection->fd->fd);
979 adx 30
980     if (IsServer(client_p) || IsHandshake(client_p))
981     {
982     if (error == 0)
983     {
984 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
985 michael 2182 "Server %s closed the connection",
986 michael 7997 client_get_name(client_p, SHOW_IP));
987 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
988 michael 2182 "Server %s closed the connection",
989 michael 7997 client_get_name(client_p, MASK_IP));
990 michael 1247 ilog(LOG_TYPE_IRCD, "Server %s closed the connection",
991 michael 7997 client_get_name(client_p, SHOW_IP));
992 adx 30 }
993     else
994     {
995 michael 8399 sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE,
996     "Lost connection to %s: %s",
997     client_get_name(client_p, SHOW_IP), strerror(current_error));
998     sendto_realops_flags(UMODE_SERVNOTICE, L_OPER, SEND_NOTICE,
999     "Lost connection to %s: %s",
1000     client_get_name(client_p, MASK_IP), strerror(current_error));
1001     ilog(LOG_TYPE_IRCD, "Lost connection to %s: %s",
1002     client_get_name(client_p, SHOW_IP), strerror(current_error));
1003 adx 30 }
1004    
1005 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
1006 michael 6569 "%s was connected for %s",
1007     client_p->name, time_dissect(CurrentTime - client_p->connection->firsttime));
1008 adx 30 }
1009    
1010     if (error == 0)
1011     strlcpy(errmsg, "Remote host closed the connection",
1012     sizeof(errmsg));
1013     else
1014 michael 1124 snprintf(errmsg, sizeof(errmsg), "Read error: %s",
1015     strerror(current_error));
1016 adx 30
1017 michael 3171 exit_client(client_p, errmsg);
1018 adx 30 }
1019    
1020     void
1021     exit_aborted_clients(void)
1022     {
1023     dlink_node *ptr;
1024     struct Client *target_p;
1025     const char *notice;
1026    
1027     DLINK_FOREACH_SAFE(ptr, eac_next, abort_list.head)
1028     {
1029     target_p = ptr->data;
1030     eac_next = ptr->next;
1031    
1032 michael 8292 dlinkDelete(ptr, &abort_list);
1033     free_dlink_node(ptr);
1034    
1035 adx 30 if (target_p == NULL)
1036     {
1037 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
1038 adx 30 "Warning: null client on abort_list!");
1039     continue;
1040     }
1041    
1042 michael 6313 if (HasFlag(target_p, FLAGS_SENDQEX))
1043 adx 30 notice = "Max SendQ exceeded";
1044     else
1045     notice = "Write error: connection closed";
1046    
1047 michael 3171 exit_client(target_p, notice);
1048 adx 30 }
1049     }
1050    
1051     /*
1052     * accept processing, this adds a form of "caller ID" to ircd
1053 michael 887 *
1054 adx 30 * If a client puts themselves into "caller ID only" mode,
1055 michael 887 * only clients that match a client pointer they have put on
1056 adx 30 * the accept list will be allowed to message them.
1057     *
1058 michael 887 * Diane Bruce, "Dianora" db@db.net
1059 adx 30 */
1060    
1061 michael 887 void
1062 michael 4975 del_accept(struct split_nuh_item *accept_p, struct Client *client_p)
1063 adx 30 {
1064 michael 4975 dlinkDelete(&accept_p->node, &client_p->connection->acceptlist);
1065 adx 30
1066 michael 7032 xfree(accept_p->nickptr);
1067     xfree(accept_p->userptr);
1068     xfree(accept_p->hostptr);
1069     xfree(accept_p);
1070 michael 887 }
1071 adx 30
1072 michael 887 struct split_nuh_item *
1073     find_accept(const char *nick, const char *user,
1074 michael 2363 const char *host, struct Client *client_p,
1075     int (*cmpfunc)(const char *, const char *))
1076 michael 887 {
1077 michael 7987 dlink_node *node;
1078 adx 30
1079 michael 4800 DLINK_FOREACH(node, client_p->connection->acceptlist.head)
1080 adx 30 {
1081 michael 4975 struct split_nuh_item *accept_p = node->data;
1082 michael 887
1083 michael 4975 if (!cmpfunc(accept_p->nickptr, nick) &&
1084     !cmpfunc(accept_p->userptr, user) &&
1085     !cmpfunc(accept_p->hostptr, host))
1086     return accept_p;
1087 adx 30 }
1088    
1089 michael 887 return NULL;
1090 adx 30 }
1091    
1092 michael 887 /* accept_message()
1093 adx 30 *
1094 michael 887 * inputs - pointer to source client
1095     * - pointer to target client
1096     * output - 1 if accept this message 0 if not
1097     * side effects - See if source is on target's allow list
1098 adx 30 */
1099 michael 887 int
1100     accept_message(struct Client *source,
1101     struct Client *target)
1102 adx 30 {
1103 michael 8059 dlink_node *node;
1104 adx 30
1105 michael 5451 if (HasFlag(source, FLAGS_SERVICE) ||
1106     (HasUMode(source, UMODE_OPER) && ConfigGeneral.opers_bypass_callerid))
1107     return 1;
1108    
1109 michael 887 if (source == target || find_accept(source->name, source->username,
1110 michael 2363 source->host, target, match))
1111 michael 887 return 1;
1112 adx 30
1113 michael 4660 if (!HasUMode(target, UMODE_CALLERID) && HasUMode(target, UMODE_SOFTCALLERID))
1114 michael 4800 DLINK_FOREACH(node, target->channel.head)
1115     if (IsMember(source, ((struct Membership *)node->data)->chptr))
1116 michael 887 return 1;
1117 adx 30
1118 michael 887 return 0;
1119 adx 30 }
1120    
1121     /* del_all_accepts()
1122     *
1123 michael 887 * inputs - pointer to exiting client
1124     * output - NONE
1125     * side effects - Walk through given clients acceptlist and remove all entries
1126 adx 30 */
1127     void
1128     del_all_accepts(struct Client *client_p)
1129     {
1130 michael 7987 dlink_node *node, *node_next;
1131 adx 30
1132 michael 4800 DLINK_FOREACH_SAFE(node, node_next, client_p->connection->acceptlist.head)
1133     del_accept(node->data, client_p);
1134 adx 30 }
1135 michael 1783
1136     unsigned int
1137 michael 5545 client_get_idle_time(const struct Client *source_p,
1138     const struct Client *target_p)
1139 michael 1783 {
1140     unsigned int idle = 0;
1141 michael 4833 const struct ClassItem *const class = get_class_ptr(&target_p->connection->confs);
1142 michael 1783
1143 michael 1785 if (!(class->flags & CLASS_FLAGS_FAKE_IDLE) || target_p == source_p)
1144 michael 4588 return CurrentTime - target_p->connection->last_privmsg;
1145 michael 4884
1146 michael 1783 if (HasUMode(source_p, UMODE_OPER) &&
1147 michael 1785 !(class->flags & CLASS_FLAGS_HIDE_IDLE_FROM_OPERS))
1148 michael 4588 return CurrentTime - target_p->connection->last_privmsg;
1149 michael 1783
1150 michael 7987 const unsigned int min_idle = class->min_idle;
1151     const unsigned int max_idle = class->max_idle;
1152 michael 1783
1153     if (min_idle == max_idle)
1154     return min_idle;
1155    
1156     if (class->flags & CLASS_FLAGS_RANDOM_IDLE)
1157     idle = genrand_int32();
1158     else
1159 michael 4588 idle = CurrentTime - target_p->connection->last_privmsg;
1160 michael 1783
1161 michael 7870 if (max_idle)
1162     idle %= max_idle;
1163     else
1164 michael 1783 idle = 0;
1165    
1166     if (idle < min_idle)
1167     idle = min_idle + (idle % (max_idle - min_idle));
1168    
1169     return idle;
1170     }
1171 michael 4214
1172     /* client_init()
1173     *
1174     * inputs - NONE
1175     * output - NONE
1176     * side effects - initialize client free memory
1177     */
1178     void
1179     client_init(void)
1180     {
1181     static struct event event_ping =
1182     {
1183     .name = "check_pings",
1184     .handler = check_pings,
1185     .when = 5
1186     };
1187    
1188     event_add(&event_ping, NULL);
1189     }

Properties

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