25 #include <freeradius-devel/bio/fd_priv.h>
26 #include <freeradius-devel/util/file.h>
27 #include <freeradius-devel/util/cap.h>
33 #include <netinet/tcp.h>
47 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on,
sizeof(on)) < 0) {
57 # if !defined(SOL_TCP) && defined(IPPROTO_TCP)
58 # define SOL_TCP IPPROTO_TCP
68 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on,
sizeof(on)) < 0) {
92 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, &on,
sizeof(
int)) < 0) {
97 #elif defined(SO_TIMESTAMP)
103 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &on,
sizeof(
int)) < 0) {
113 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt,
sizeof(opt)) < 0) {
124 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt,
sizeof(opt)) < 0) {
146 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on,
sizeof(on)) < 0) {
162 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) < 0) {
177 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
182 flag = IP_PMTUDISC_DONT;
184 if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &flag,
sizeof(flag)) < 0) {
190 #if defined(IP_DONTFRAG)
196 if (setsockopt(fd, IPPROTO_IP, IP_DONTFRAG, &flag,
sizeof(flag)) < 0) {
219 if (IN6_IS_ADDR_UNSPECIFIED(
UNCONST(
struct in6_addr *, &sock->inet.src_ipaddr.addr.v6))) {
222 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (
char *)&on,
sizeof(on)) < 0) {
250 if (fstatat(dirfd, filename, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
251 if (errno != ENOENT) {
262 if (!S_ISSOCK(buf.st_mode)) {
271 if (buf.st_uid != cfg->
uid) {
294 if (unlinkat(dirfd, filename, 0) < 0) {
311 char const *path = cfg->
path;
312 char *p, *dir = NULL;
315 perm = S_IREAD | S_IWRITE | S_IEXEC;
316 perm |= S_IRGRP | S_IWGRP | S_IXGRP;
324 if (
fr_dirfd(dirfd, filename, path) == 0) {
327 if (fstat(*dirfd, &buf) < 0) {
335 if (buf.st_uid != cfg->
uid) {
336 fr_strerror_printf(
"Failed reading parent directory for file %s: Incorrect UID", path);
340 if (buf.st_gid != cfg->
gid) {
341 fr_strerror_printf(
"Failed reading parent directory for file %s: Incorrect GID", path);
350 if (fchmod(*dirfd, perm) < 0) {
358 dir = talloc_strdup(NULL, path);
364 slashes[0] = slashes[1] = NULL;
365 for (p = dir; *p !=
'\0'; p++) {
367 slashes[0] = slashes[1];
400 parent_fd = open(dir, O_DIRECTORY | O_NOFOLLOW);
411 if (mkdirat(parent_fd, dir, 0700) < 0) {
418 fd = openat(parent_fd, dir, O_DIRECTORY);
424 if (fchmod(fd, perm) < 0) {
436 if (fchown(fd, cfg->
uid, cfg->
gid) < 0) {
465 if (
my->user_shutdown)
my->user_shutdown(bio);
467 (void) unlink(
my->info.socket.unix.path);
484 char const *filename, *p;
486 struct sockaddr_un sun;
497 if (cfg->
uid == (uid_t) -1) {
502 if (cfg->
gid == (gid_t) -1) {
510 p = strrchr(cfg->
path,
'/');
513 filename = cfg->
path;
515 }
else if (p == cfg->
path) {
519 fr_strerror_printf(
"Failed opening domain socket %s: cannot exist at file system root", p);
531 if (dirfd != AT_FDCWD)
close(dirfd);
544 rcode = bindat(dirfd,
my->info.socket.fd, (
struct sockaddr *) &sun, sunlen);
551 rcode = bind(
my->info.socket.fd, (
struct sockaddr *) &sun, sunlen);
566 if (fchmod(
my->info.socket.fd, S_IREAD | S_IWRITE | S_IEXEC | S_IRGRP | S_IWGRP | S_IXGRP) < 0) {
576 if (fchown(
my->info.socket.fd, cfg->
uid, cfg->
gid) < 0) {
586 if (
my->cb.shutdown)
my->user_shutdown =
my->cb.shutdown;
592 #ifdef SO_BINDTODEVICE
601 if (!
my->info.socket.inet.ifindex)
return 0;
612 #ifdef HAVE_CAPABILITY_H
613 (void)fr_cap_enable(CAP_NET_RAW, CAP_EFFECTIVE);
616 if (setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_BINDTODEVICE, cfg->
interface, strlen(cfg->
interface)) < 0) {
624 #elif defined(IP_BOUND_IF) || defined(IPV6_BOUND_IF)
632 if (!
my->info.socket.inet.ifindex)
return 0;
634 opt =
my->info.socket.inet.ifindex;
636 switch (
my->info.socket.af) {
638 rcode = setsockopt(
my->info.socket.fd, IPPROTO_IP, IP_BOUND_IF, &opt,
sizeof(opt));
642 rcode = setsockopt(
my->info.socket.fd, IPPROTO_IPV6, IPV6_BOUND_IF, &opt,
sizeof(opt));
647 errno = EAFNOSUPPORT;
678 struct sockaddr_storage salocal;
680 fr_assert((
my->info.socket.af == AF_INET) || (
my->info.socket.af == AF_INET6));
682 #ifdef HAVE_CAPABILITY_H
688 if ((
my->info.socket.inet.src_port < 1024) && (geteuid() != 0)) {
689 (void)fr_cap_enable(CAP_NET_BIND_SERVICE, CAP_EFFECTIVE);
698 if (
fr_ipaddr_to_sockaddr(&salocal, &salen, &
my->info.socket.inet.src_ipaddr,
my->info.socket.inet.src_port) < 0)
return -1;
700 if (bind(
my->info.socket.fd, (
struct sockaddr *) &salocal, salen) < 0) {
736 my->info.socket.inet.src_port = cfg->
src_port;
737 my->info.socket.inet.dst_port = cfg->
dst_port;
740 protocol = IPPROTO_TCP;
742 protocol = IPPROTO_UDP;
746 my->info.socket.inet.ifindex = if_nametoindex(cfg->
interface);
748 if (!
my->info.socket.inet.ifindex) {
758 fd =
my->info.socket.fd;
762 fd = socket(
my->info.socket.af,
my->info.socket.type, protocol);
769 }
else if (cfg->
path) {
770 my->info.socket.af = AF_LOCAL;
771 my->info.socket.type = SOCK_STREAM;
772 my->info.socket.unix.path = cfg->
path;
774 fd = socket(
my->info.socket.af,
my->info.socket.type, 0);
790 my->info.socket.type = SOCK_STREAM;
796 if (strcmp(cfg->
filename,
"/dev/stdout") == 0) {
797 if (cfg->
flags != O_WRONLY) {
803 fd = dup(STDOUT_FILENO);
805 }
else if (strcmp(cfg->
filename,
"/dev/stderr") == 0) {
806 if (cfg->
flags != O_WRONLY)
goto fail_dev;
808 fd = dup(STDERR_FILENO);
814 my->info.socket.fd = -1;
844 rcode = fcntl(fd, F_GETFD);
846 if (fcntl(fd, F_SETFD, rcode |
FD_CLOEXEC) < 0) {
861 my->info.socket.fd = fd;
866 my->info.type = cfg->
type;
877 if (
my->info.socket.type != SOCK_DGRAM) {
882 switch (
my->info.socket.af) {
911 if (
my->info.socket.type == SOCK_DGRAM) {
913 if (rcode < 0)
goto fail;
915 }
else if ((
my->info.socket.af == AF_INET) || (
my->info.socket.af == AF_INET6)) {
917 if (rcode < 0)
goto fail;
920 switch (
my->info.socket.af) {
952 setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_NOSIGPIPE, &on,
sizeof(on));
967 switch (
my->info.socket.af) {
1029 if (
my->info.socket.fd < 0) {
1037 if (dup2(fd,
my->info.socket.fd) < 0) {
1044 return my->info.socket.fd;
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
int fr_bio_fd_socket_name(fr_bio_fd_t *my)
int fr_bio_fd_init_connected(fr_bio_fd_t *my)
int fr_filename_to_sockaddr(struct sockaddr_un *sun, socklen_t *sunlen, char const *filename)
int fr_bio_fd_init_listen(fr_bio_fd_t *my)
int fr_bio_fd_init_common(fr_bio_fd_t *my)
uint16_t src_port
our port
@ FR_BIO_FD_ACCEPTED
temporarily until it's connected.
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram only
@ FR_BIO_FD_LISTEN
returns new fd in buffer on fr_bio_read() or fr_bio_fd_accept()
uint32_t recv_buff
How big the kernel's receive buffer should be.
bool mkdir
make intermediate directories
@ FR_BIO_FD_STATE_CONNECTING
@ FR_BIO_FD_STATE_OPEN
error states must be before this
fr_ipaddr_t dst_ipaddr
their IP address
fr_bio_fd_type_t type
accept, connected, unconnected, etc.
char const * path
for Unix domain sockets
uint32_t send_buff
How big the kernel's send buffer should be.
char const * filename
for files
uid_t uid
who owns the socket
gid_t gid
who owns the socket
char const * interface
for binding to an interface
mode_t perm
permissions for domain sockets
int socket_type
SOCK_STREAM or SOCK_DGRAM.
uint16_t dst_port
their port
bool tcp_delay
We do tcp_nodelay by default.
fr_ipaddr_t src_ipaddr
our IP address
Configuration for sockets.
Run-time status of the socket.
static int fr_bio_fd_socket_unix_mkdir(int *dirfd, char const **filename, fr_bio_fd_config_t const *cfg)
static int fr_bio_fd_socket_unix_verify(int dirfd, char const *filename, fr_bio_fd_config_t const *cfg)
Verify or clean up a pre-existing domain socket.
static void fr_bio_fd_unix_shutdown(fr_bio_t *bio)
static int fr_bio_fd_server_ipv6(int fd, fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize an IPv6 server socket.
int fr_bio_fd_reopen(fr_bio_t *bio)
Reopen a file BIO.
static int fr_bio_fd_server_tcp(int fd, UNUSED fr_socket_t const *sock)
Initialize a TCP server socket.
static int fr_bio_fd_socket_bind_unix(fr_bio_fd_t *my, fr_bio_fd_config_t const *cfg)
Bind to a Unix domain socket.
static int fr_bio_fd_socket_bind(fr_bio_fd_t *my, fr_bio_fd_config_t const *cfg)
static int fr_bio_fd_server_ipv4(int fd, fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize an IPv4 server socket.
static int fr_bio_fd_common_tcp(int fd, UNUSED fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize common datagram information.
static int fr_bio_fd_socket_bind_to_device(fr_bio_fd_t *my, fr_bio_fd_config_t const *cfg)
This system is missing SO_BINDTODEVICE, IP_BOUND_IF, IPV6_BOUND_IF.
int fr_bio_fd_open(fr_bio_t *bio, fr_bio_fd_config_t const *cfg)
Opens a socket and updates sock->fd.
static int fr_bio_fd_server_udp(int fd, fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize a UDP server socket.
static int fr_bio_fd_common_datagram(int fd, UNUSED fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize common datagram information.
ssize_t fr_mkdir(int *fd_out, char const *path, ssize_t len, mode_t mode, fr_mkdir_func_t func, void *uctx)
Create directories that are missing in the specified path.
int fr_mkdir_chown(int fd, char const *path, void *uctx)
Callback for the common case of chown() of the directory.
int fr_dirfd(int *dirfd, char const **filename, char const *pathname)
From a pathname, return fd and filename needed for *at() functions.
int fr_ipaddr_to_sockaddr(struct sockaddr_storage *sa, socklen_t *salen, fr_ipaddr_t const *ipaddr, uint16_t port)
Convert our internal ip address representation to a sockaddr.
int fr_nonblock(UNUSED int fd)
int fr_socket_client_unix(UNUSED char const *path, UNUSED bool async)
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
int type
SOCK_STREAM, SOCK_DGRAM, etc.
Holds information necessary for binding or connecting to a socket.
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
#define fr_strerror_const(_msg)