ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 9234
Committed: Fri Jan 31 17:38:34 2020 UTC (6 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 34056 byte(s)
Log Message:
- Extbans have been implemented. Main implementation done by Adam for p4.
  Currently supported extbans:

  Matching:

   $a:<account>   Matches users logged into a matching account.
   $c:<channel>   Matches users that are on the given channel. An additional
                  prefix of either @, %, or + can be specified to test for
                  certain channel privileges.
   $o:<class>     Matches IRC operators that have joined a class
                  matching the mask.
   $r:<realname>  Matches users with a matching realname.
   $s:<server>    Matches users that are connected to a server matching the mask.
   $u:<modes>     Matches users having the specified user modes set or not set.
   $z:<certfp>    Matches users having the given TLS certificate fingerprint.

  Acting:

   $j:<banmask>   Prevents matching users from joining the channel.
   $m:<banmask>   Blocks messages from matching users. Users with voice
                  or above are not affected.

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