ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 8826
Committed: Sat Feb 2 00:00:17 2019 UTC (7 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 33480 byte(s)
Log Message:
- Made comm_get_sockerr() and comm_accept() take a fde_t pointer as argument

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

Properties

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