ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
Revision: 9157
Committed: Mon Jan 13 14:56:38 2020 UTC (6 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 16069 byte(s)
Log Message:
- Replaced most occurences of 'SSL' with 'TLS'

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2020 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file s_bsd.c
23 * \brief Network functions.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include <netinet/in_systm.h>
29 #include <netinet/ip.h>
30 #include <netinet/tcp.h>
31 #include "list.h"
32 #include "fdlist.h"
33 #include "s_bsd.h"
34 #include "client.h"
35 #include "dbuf.h"
36 #include "event.h"
37 #include "irc_string.h"
38 #include "ircd.h"
39 #include "listener.h"
40 #include "numeric.h"
41 #include "packet.h"
42 #include "res.h"
43 #include "restart.h"
44 #include "conf.h"
45 #include "log.h"
46 #include "server.h"
47 #include "send.h"
48 #include "memory.h"
49 #include "user.h"
50
51
52 static const char *const comm_err_str[] =
53 {
54 [COMM_OK] = "Comm OK",
55 [COMM_ERR_BIND] = "Error during bind()",
56 [COMM_ERR_TIMEOUT] = "connect timeout",
57 [COMM_ERR_CONNECT] = "Error during connect()",
58 [COMM_ERROR] = "Comm Error"
59 };
60
61 static void comm_connect_callback(fde_t *, int);
62 static void comm_connect_timeout(fde_t *, void *);
63 static void comm_connect_tryconnect(fde_t *, void *);
64
65
66 /* comm_get_sockerr - get the error value from the socket or the current errno
67 *
68 * Get the *real* error from the socket (well try to anyway..).
69 * This may only work when SO_DEBUG is enabled but its worth the
70 * gamble anyway.
71 */
72 int
73 comm_get_sockerr(fde_t *F)
74 {
75 int errtmp = errno;
76 #ifdef SO_ERROR
77 int err = 0;
78 socklen_t len = sizeof(err);
79
80 assert(F);
81 assert(F->flags.open == true);
82
83 if (getsockopt(F->fd, SOL_SOCKET, SO_ERROR, &err, &len) == 0)
84 {
85 if (err)
86 errtmp = err;
87 }
88
89 errno = errtmp;
90 #endif
91 return errtmp;
92 }
93
94 /*
95 * report_error - report an error from an errno.
96 * Record error to log and also send a copy to all *LOCAL* opers online.
97 *
98 * text is a *format* string for outputing error. It must
99 * contain only two '%s', the first will be replaced
100 * by the sockhost from the client_p, and the latter will
101 * be taken from sys_errlist[errno].
102 *
103 * client_p if not NULL, is the *LOCAL* client associated with
104 * the error.
105 *
106 * Cannot use perror() within daemon. stderr is closed in
107 * ircd and cannot be used. And, worse yet, it might have
108 * been reassigned to a normal connection...
109 *
110 * Actually stderr is still there IFF ircd was run with -s --Rodder
111 */
112 void
113 report_error(int level, const char *text, const char *who, int error)
114 {
115 who = (who) ? who : "";
116
117 sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE,
118 text, who, strerror(error));
119 ilog(LOG_TYPE_IRCD, text, who, strerror(error));
120 }
121
122 /*
123 * setup_socket()
124 *
125 * Set the socket non-blocking, and other wonderful bits.
126 */
127 static void
128 setup_socket(int fd)
129 {
130 int opt = 1;
131
132 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
133
134 #ifdef IPTOS_LOWDELAY
135 opt = IPTOS_LOWDELAY;
136 setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
137 #endif
138
139 #ifdef TCP_QUICKACK
140 opt = 1;
141 setsockopt(fd, SOL_SOCKET, TCP_QUICKACK, &opt, sizeof(opt));
142 #endif
143
144 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
145 }
146
147 /*
148 * ssl_handshake - let OpenSSL initialize the protocol. Register for
149 * read/write events if necessary.
150 */
151 static void
152 ssl_handshake(fde_t *F, void *data)
153 {
154 struct Client *client_p = data;
155
156 assert(client_p);
157 assert(client_p->connection);
158 assert(client_p->connection->fd);
159 assert(client_p->connection->fd == F);
160
161 tls_handshake_status_t ret = tls_handshake(&F->tls, TLS_ROLE_SERVER, NULL);
162 if (ret != TLS_HANDSHAKE_DONE)
163 {
164 if ((event_base->time.sec_monotonic - client_p->connection->created_monotonic) > TLS_HANDSHAKE_TIMEOUT)
165 {
166 exit_client(client_p, "Timeout during TLS handshake");
167 return;
168 }
169
170 switch (ret)
171 {
172 case TLS_HANDSHAKE_WANT_WRITE:
173 comm_setselect(F, COMM_SELECT_WRITE, ssl_handshake, client_p, TLS_HANDSHAKE_TIMEOUT);
174 return;
175 case TLS_HANDSHAKE_WANT_READ:
176 comm_setselect(F, COMM_SELECT_READ, ssl_handshake, client_p, TLS_HANDSHAKE_TIMEOUT);
177 return;
178 default:
179 exit_client(client_p, "Error during TLS handshake");
180 return;
181 }
182 }
183
184 comm_settimeout(F, 0, NULL, NULL);
185
186 if (tls_verify_cert(&F->tls, ConfigServerInfo.message_digest_algorithm, &client_p->certfp) == false)
187 ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad TLS client certificate",
188 client_p->name, client_p->username, client_p->host);
189
190 auth_start(client_p);
191 }
192
193 /*
194 * add_connection - creates a client which has just connected to us on
195 * the given fd. The sockhost field is initialized with the ip# of the host.
196 * An unique id is calculated now, in case it is needed for auth.
197 * The client is sent to the auth module for verification, and not put in
198 * any client list yet.
199 */
200 void
201 add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
202 {
203 struct Client *client_p = client_make(NULL);
204
205 client_p->connection->fd = fd_open(fd, true, (listener->flags & LISTENER_TLS) ?
206 "Incoming TLS connection" : "Incoming connection");
207
208 /*
209 * copy address to 'sockhost' as a string, copy it to host too
210 * so we have something valid to put into error messages...
211 */
212 client_p->ip = *irn;
213
214 getnameinfo((const struct sockaddr *)&client_p->ip,
215 client_p->ip.ss_len, client_p->sockhost,
216 sizeof(client_p->sockhost), NULL, 0, NI_NUMERICHOST);
217
218 if (client_p->sockhost[0] == ':')
219 {
220 client_p->sockhost[0] = '0';
221 memmove(client_p->sockhost + 1, client_p->sockhost, sizeof(client_p->sockhost) - 1);
222 }
223
224 strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host));
225
226 client_p->connection->listener = listener;
227 ++listener->ref_count;
228
229 if (listener->flags & LISTENER_TLS)
230 {
231 if (tls_new(&client_p->connection->fd->tls, fd, TLS_ROLE_SERVER) == false)
232 {
233 SetDead(client_p);
234 exit_client(client_p, "TLS context initialization failed");
235 return;
236 }
237
238 AddFlag(client_p, FLAGS_TLS);
239 ssl_handshake(client_p->connection->fd, client_p);
240 }
241 else
242 auth_start(client_p);
243 }
244
245 /*
246 * stolen from squid - its a neat (but overused! :) routine which we
247 * can use to see whether we can ignore this errno or not. It is
248 * generally useful for non-blocking network IO related errnos.
249 * -- adrian
250 */
251 bool
252 comm_ignore_errno(int ierrno)
253 {
254 switch (ierrno)
255 {
256 case EINPROGRESS:
257 case EWOULDBLOCK:
258 #if EAGAIN != EWOULDBLOCK
259 case EAGAIN:
260 #endif
261 case EALREADY:
262 case EINTR:
263 #ifdef ERESTART
264 case ERESTART:
265 #endif
266 return true;
267 default:
268 return false;
269 }
270 }
271
272 /*
273 * comm_settimeout() - set the socket timeout
274 *
275 * Set the timeout for the fd
276 */
277 void
278 comm_settimeout(fde_t *F, uintmax_t timeout, void (*callback)(fde_t *, void *), void *cbdata)
279 {
280 assert(F);
281 assert(F->flags.open == true);
282
283 F->timeout = event_base->time.sec_monotonic + timeout;
284 F->timeout_handler = callback;
285 F->timeout_data = cbdata;
286 }
287
288 /*
289 * comm_setflush() - set a flush function
290 *
291 * A flush function is simply a function called if found during
292 * comm_timeouts(). Its basically a second timeout, except in this case
293 * I'm too lazy to implement multiple timeout functions! :-)
294 * its kinda nice to have it separate, since this is designed for
295 * flush functions, and when comm_close() is implemented correctly
296 * with close functions, we _actually_ don't call comm_close() here ..
297 * -- originally Adrian's notes
298 * comm_close() is replaced with fd_close() in fdlist.c
299 */
300 void
301 comm_setflush(fde_t *F, uintmax_t timeout, void (*callback)(fde_t *, void *), void *cbdata)
302 {
303 assert(F);
304 assert(F->flags.open == true);
305
306 F->flush_timeout = event_base->time.sec_monotonic + timeout;
307 F->flush_handler = callback;
308 F->flush_data = cbdata;
309 }
310
311 /*
312 * comm_checktimeouts() - check the socket timeouts
313 *
314 * All this routine does is call the given callback/cbdata, without closing
315 * down the file descriptor. When close handlers have been implemented,
316 * this will happen.
317 */
318 void
319 comm_checktimeouts(void *unused)
320 {
321 void (*hdl)(fde_t *, void *);
322 void *data;
323
324 for (int fd = 0; fd <= highest_fd; ++fd)
325 {
326 fde_t *F = &fd_table[fd];
327
328 if (F->flags.open == false)
329 continue;
330
331 /* check flush functions */
332 if (F->flush_handler && F->flush_timeout > 0 &&
333 F->flush_timeout < event_base->time.sec_monotonic)
334 {
335 hdl = F->flush_handler;
336 data = F->flush_data;
337
338 comm_setflush(F, 0, NULL, NULL);
339 hdl(F, data);
340 }
341
342 /* check timeouts */
343 if (F->timeout_handler && F->timeout > 0 &&
344 F->timeout < event_base->time.sec_monotonic)
345 {
346 /* Call timeout handler */
347 hdl = F->timeout_handler;
348 data = F->timeout_data;
349
350 comm_settimeout(F, 0, NULL, NULL);
351 hdl(F, data);
352 }
353 }
354 }
355
356 /*
357 * void comm_connect_tcp(int fd, const char *host, unsigned short port,
358 * struct sockaddr *clocal, int socklen,
359 * CNCB *callback, void *data, int aftype, int timeout)
360 * Input: An fd to connect with, a host and port to connect to,
361 * a local sockaddr to connect from + length(or NULL to use the
362 * default), a callback, the data to pass into the callback, the
363 * address family.
364 * Output: None.
365 * Side-effects: A non-blocking connection to the host is started, and
366 * if necessary, set up for selection. The callback given
367 * may be called now, or it may be called later.
368 */
369 void
370 comm_connect_tcp(fde_t *F, const struct irc_ssaddr *caddr, unsigned short port, const struct irc_ssaddr *baddr,
371 void (*callback)(fde_t *, int, void *), void *data, uintmax_t timeout)
372 {
373 assert(callback);
374
375 F->connect.hostaddr = *caddr;
376 /* The cast is hacky, but safe - port offset is same on v4 and v6 */
377 ((struct sockaddr_in *)&F->connect.hostaddr)->sin_port = htons(port);
378 F->connect.callback = callback;
379 F->connect.data = data;
380
381 /* Note that we're using a passed sockaddr here. This is because
382 * generally you'll be bind()ing to a sockaddr grabbed from
383 * getsockname(), so this makes things easier.
384 * XXX If NULL is passed as local, we should later on bind() to the
385 * virtual host IP, for completeness.
386 * -- adrian
387 */
388 if (baddr && bind(F->fd, (const struct sockaddr *)baddr, baddr->ss_len) < 0)
389 {
390 /* Failure, call the callback with COMM_ERR_BIND */
391 comm_connect_callback(F, COMM_ERR_BIND);
392 return; /* ... and quit */
393 }
394
395 comm_settimeout(F, timeout, comm_connect_timeout, NULL);
396 comm_connect_tryconnect(F, NULL);
397 }
398
399 /*
400 * comm_connect_callback() - call the callback, and continue with life
401 */
402 static void
403 comm_connect_callback(fde_t *F, int status)
404 {
405 void (*hdl)(fde_t *, int, void *);
406
407 /* This check is gross..but probably necessary */
408 if (F->connect.callback == NULL)
409 return;
410
411 /* Clear the connect flag + handler */
412 hdl = F->connect.callback;
413 F->connect.callback = NULL;
414
415 /* Clear the timeout handler */
416 comm_settimeout(F, 0, NULL, NULL);
417
418 /* Call the handler */
419 hdl(F, status, F->connect.data);
420 }
421
422 /*
423 * comm_connect_timeout() - this gets called when the socket connection
424 * times out. This *only* can be called once connect() is initially
425 * called ..
426 */
427 static void
428 comm_connect_timeout(fde_t *F, void *unused)
429 {
430 /* error! */
431 comm_connect_callback(F, COMM_ERR_TIMEOUT);
432 }
433
434 /* static void comm_connect_tryconnect(fde_t *fd, void *unused)
435 * Input: The fd, the handler data(unused).
436 * Output: None.
437 * Side-effects: Try and connect with pending connect data for the FD. If
438 * we succeed or get a fatal error, call the callback.
439 * Otherwise, it is still blocking or something, so register
440 * to select for a write event on this FD.
441 */
442 static void
443 comm_connect_tryconnect(fde_t *F, void *unused)
444 {
445 /* This check is needed or re-entrant s_bsd_* like sigio break it. */
446 if (F->connect.callback == NULL)
447 return;
448
449 /* Try the connect() */
450 int retval = connect(F->fd, (struct sockaddr *)&F->connect.hostaddr, F->connect.hostaddr.ss_len);
451
452 /* Error? */
453 if (retval < 0)
454 {
455 /*
456 * If we get EISCONN, then we've already connect()ed the socket,
457 * which is a good thing.
458 * -- adrian
459 */
460 if (errno == EISCONN)
461 comm_connect_callback(F, COMM_OK);
462 else if (comm_ignore_errno(errno))
463 /* Ignore error? Reschedule */
464 comm_setselect(F, COMM_SELECT_WRITE, comm_connect_tryconnect, NULL, 0);
465 else
466 /* Error? Fail with COMM_ERR_CONNECT */
467 comm_connect_callback(F, COMM_ERR_CONNECT);
468 return;
469 }
470
471 /* If we get here, we've suceeded, so call with COMM_OK */
472 comm_connect_callback(F, COMM_OK);
473 }
474
475 /*
476 * comm_errorstr() - return an error string for the given error condition
477 */
478 const char *
479 comm_errstr(int error)
480 {
481 if (error < 0 || error >= COMM_ERR_MAX)
482 return "Invalid error number!";
483 return comm_err_str[error];
484 }
485
486 /*
487 * comm_open() - open a socket
488 *
489 * This is a highly highly cut down version of squid's comm_open() which
490 * for the most part emulates socket(), *EXCEPT* it fails if we're about
491 * to run out of file descriptors.
492 */
493 int
494 comm_socket(int family, int sock_type, int proto)
495 {
496 /* First, make sure we aren't going to run out of file descriptors */
497 if (number_fd >= hard_fdlimit)
498 {
499 errno = ENFILE;
500 return -1;
501 }
502
503 /*
504 * Next, we try to open the socket. We *should* drop the reserved FD
505 * limit if/when we get an error, but we can deal with that later.
506 * XXX !!! -- adrian
507 */
508 int fd = socket(family, sock_type, proto);
509 if (fd < 0)
510 return -1; /* errno will be passed through, yay.. */
511
512 setup_socket(fd);
513
514 return fd;
515 }
516
517 /*
518 * comm_accept() - accept an incoming connection
519 *
520 * This is a simple wrapper for accept() which enforces FD limits like
521 * comm_open() does. Returned fd must be either closed or tagged with
522 * fd_open (this function no longer does it).
523 */
524 int
525 comm_accept(fde_t *F, struct irc_ssaddr *addr)
526 {
527 socklen_t addrlen = sizeof(struct irc_ssaddr);
528
529 if (number_fd >= hard_fdlimit)
530 {
531 errno = ENFILE;
532 return -1;
533 }
534
535 memset(addr, 0, sizeof(*addr));
536
537 /*
538 * Next, do the accept(). if we get an error, we should drop the
539 * reserved fd limit, but we can deal with that when comm_open()
540 * also does it. XXX -- adrian
541 */
542 int fd = accept(F->fd, (struct sockaddr *)addr, &addrlen);
543 if (fd < 0)
544 return -1;
545
546 remove_ipv6_mapping(addr);
547
548 setup_socket(fd);
549
550 /* .. and return */
551 return fd;
552 }
553
554 /*
555 * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
556 * OSes with IPv6 mapping listening on both
557 * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
558 *
559 */
560 void
561 remove_ipv6_mapping(struct irc_ssaddr *addr)
562 {
563 if (addr->ss.ss_family == AF_INET6)
564 {
565 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
566 {
567 struct sockaddr_in6 v6;
568 struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
569
570 memcpy(&v6, addr, sizeof(v6));
571 memset(v4, 0, sizeof(struct sockaddr_in));
572 memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
573
574 addr->ss.ss_family = AF_INET;
575 addr->ss_len = sizeof(struct sockaddr_in);
576 }
577 else
578 addr->ss_len = sizeof(struct sockaddr_in6);
579 }
580 else
581 addr->ss_len = sizeof(struct sockaddr_in);
582 }

Properties

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