ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 9435
Committed: Thu Jun 25 12:16:20 2020 UTC (6 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 33426 byte(s)
Log Message:
- Replace remaining occurrences of IsMember with find_channel_link and remove IsMember entirely

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

Properties

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