Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ Lesser General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "macro.h"
#include "util.h"
#include "mkdir.h"
+#include "path-util.h"
#include "socket-util.h"
#include "missing.h"
-#include "label.h"
+#include "fileio.h"
int socket_address_parse(SocketAddress *a, const char *s) {
int r;
errno = 0;
if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0) {
free(n);
- return errno != 0 ? -errno : -EINVAL;
+ return errno > 0 ? -errno : -EINVAL;
}
free(n);
} else {
/* Just a port */
- if ((r = safe_atou(s, &u)) < 0)
+ r = safe_atou(s, &u);
+ if (r < 0)
return r;
if (u <= 0 || u > 0xFFFF)
int socket_address_parse_netlink(SocketAddress *a, const char *s) {
int family;
unsigned group = 0;
- char* sfamily = NULL;
+ _cleanup_free_ char *sfamily = NULL;
assert(a);
assert(s);
errno = 0;
if (sscanf(s, "%ms %u", &sfamily, &group) < 1)
- return errno ? -errno : -EINVAL;
+ return errno > 0 ? -errno : -EINVAL;
- if ((family = netlink_family_from_string(sfamily)) < 0)
- if (safe_atoi(sfamily, &family) < 0) {
- free(sfamily);
- return -EINVAL;
- }
-
- free(sfamily);
+ family = netlink_family_from_string(sfamily);
+ if (family < 0)
+ return -EINVAL;
a->sockaddr.nl.nl_family = AF_NETLINK;
a->sockaddr.nl.nl_groups = group;
case AF_INET: {
char *ret;
- if (!(ret = new(char, INET_ADDRSTRLEN+1+5+1)))
+ ret = new(char, INET_ADDRSTRLEN+1+5+1);
+ if (!ret)
return -ENOMEM;
if (!inet_ntop(AF_INET, &a->sockaddr.in4.sin_addr, ret, INET_ADDRSTRLEN)) {
case AF_INET6: {
char *ret;
- if (!(ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1)))
+ ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1);
+ if (!ret)
return -ENOMEM;
ret[0] = '[';
char *ret;
if (a->size <= offsetof(struct sockaddr_un, sun_path)) {
-
- if (!(ret = strdup("<unnamed>")))
+ ret = strdup("<unnamed>");
+ if (!ret)
return -ENOMEM;
} else if (a->sockaddr.un.sun_path[0] == 0) {
* more than one NUL byte. That is
* actually an invalid assumption */
- if (!(ret = new(char, sizeof(a->sockaddr.un.sun_path)+1)))
+ ret = new(char, sizeof(a->sockaddr.un.sun_path)+1);
+ if (!ret)
return -ENOMEM;
ret[0] = '@';
ret[sizeof(a->sockaddr.un.sun_path)] = 0;
} else {
-
- if (!(ret = strdup(a->sockaddr.un.sun_path)))
+ ret = strdup(a->sockaddr.un.sun_path);
+ if (!ret)
return -ENOMEM;
}
}
case AF_NETLINK: {
- const char *sfamily;
-
- if ((sfamily = netlink_family_to_string(a->protocol)))
- r = asprintf(p, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
- else
- r = asprintf(p, "%i %u", a->protocol, a->sockaddr.nl.nl_groups);
+ _cleanup_free_ char *sfamily = NULL;
+ r = netlink_family_to_string_alloc(a->protocol, &sfamily);
+ if (r < 0)
+ return r;
+ r = asprintf(p, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
if (r < 0)
return -ENOMEM;
}
}
-int socket_address_listen(
- const SocketAddress *a,
- int backlog,
- SocketAddressBindIPv6Only only,
- const char *bind_to_device,
- bool free_bind,
- bool transparent,
- mode_t directory_mode,
- mode_t socket_mode,
- const char *label,
- int *ret) {
-
- int r, fd, one;
- assert(a);
- assert(ret);
-
- if ((r = socket_address_verify(a)) < 0)
- return r;
-
- if (socket_address_family(a) == AF_INET6 && !socket_ipv6_is_supported())
- return -EAFNOSUPPORT;
-
- r = label_socket_set(label);
- if (r < 0)
- return r;
-
- fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK | SOCK_CLOEXEC, a->protocol);
- r = fd < 0 ? -errno : 0;
-
- label_socket_clear();
-
- if (r < 0)
- return r;
-
- if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
- int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
-
- if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
- goto fail;
- }
-
- if (socket_address_family(a) == AF_INET || socket_address_family(a) == AF_INET6) {
- if (bind_to_device)
- if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
- goto fail;
-
- if (free_bind) {
- one = 1;
- if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) < 0)
- log_warning("IP_FREEBIND failed: %m");
- }
-
- if (transparent) {
- one = 1;
- if (setsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &one, sizeof(one)) < 0)
- log_warning("IP_TRANSPARENT failed: %m");
- }
- }
-
- one = 1;
- if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
- goto fail;
-
- if (socket_address_family(a) == AF_UNIX && a->sockaddr.un.sun_path[0] != 0) {
- mode_t old_mask;
-
- /* Create parents */
- mkdir_parents(a->sockaddr.un.sun_path, directory_mode);
-
- /* Enforce the right access mode for the socket*/
- old_mask = umask(~ socket_mode);
-
- /* Include the original umask in our mask */
- umask(~socket_mode | old_mask);
-
- r = label_bind(fd, &a->sockaddr.sa, a->size);
-
- if (r < 0 && errno == EADDRINUSE) {
- /* Unlink and try again */
- unlink(a->sockaddr.un.sun_path);
- r = bind(fd, &a->sockaddr.sa, a->size);
- }
-
- umask(old_mask);
- } else
- r = bind(fd, &a->sockaddr.sa, a->size);
-
- if (r < 0)
- goto fail;
-
- if (socket_address_can_accept(a))
- if (listen(fd, backlog) < 0)
- goto fail;
-
- *ret = fd;
- return 0;
-
-fail:
- r = -errno;
- close_nointr_nofail(fd);
- return r;
-}
-
bool socket_address_can_accept(const SocketAddress *a) {
assert(a);
return false;
if (a->sockaddr.un.sun_path[0]) {
- if (strncmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, sizeof(a->sockaddr.un.sun_path)) != 0)
+ if (!strneq(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, sizeof(a->sockaddr.un.sun_path)))
return false;
} else {
if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
return socket_address_equal(a, &b);
}
-bool socket_address_needs_mount(const SocketAddress *a, const char *prefix) {
+const char* socket_address_get_path(const SocketAddress *a) {
assert(a);
if (socket_address_family(a) != AF_UNIX)
- return false;
+ return NULL;
if (a->sockaddr.un.sun_path[0] == 0)
- return false;
+ return NULL;
- return path_startswith(a->sockaddr.un.sun_path, prefix);
+ return a->sockaddr.un.sun_path;
}
bool socket_ipv6_is_supported(void) {
return enabled;
}
+bool socket_address_matches_fd(const SocketAddress *a, int fd) {
+ union sockaddr_union sa;
+ socklen_t salen = sizeof(sa), solen;
+ int protocol, type;
+
+ assert(a);
+ assert(fd >= 0);
+
+ if (getsockname(fd, &sa.sa, &salen) < 0)
+ return false;
+
+ if (sa.sa.sa_family != a->sockaddr.sa.sa_family)
+ return false;
+
+ solen = sizeof(type);
+ if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &solen) < 0)
+ return false;
+
+ if (type != a->type)
+ return false;
+
+ if (a->protocol != 0) {
+ solen = sizeof(protocol);
+ if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &solen) < 0)
+ return false;
+
+ if (protocol != a->protocol)
+ return false;
+ }
+
+ switch (sa.sa.sa_family) {
+
+ case AF_INET:
+ return sa.in4.sin_port == a->sockaddr.in4.sin_port &&
+ sa.in4.sin_addr.s_addr == a->sockaddr.in4.sin_addr.s_addr;
+
+ case AF_INET6:
+ return sa.in6.sin6_port == a->sockaddr.in6.sin6_port &&
+ memcmp(&sa.in6.sin6_addr, &a->sockaddr.in6.sin6_addr, sizeof(struct in6_addr)) == 0;
+
+ case AF_UNIX:
+ return salen == a->size &&
+ memcmp(sa.un.sun_path, a->sockaddr.un.sun_path, salen - offsetof(struct sockaddr_un, sun_path)) == 0;
+
+ }
+
+ return false;
+}
+
+int getpeername_pretty(int fd, char **ret) {
+
+ union {
+ struct sockaddr sa;
+ struct sockaddr_un un;
+ struct sockaddr_in in;
+ struct sockaddr_in6 in6;
+ struct sockaddr_storage storage;
+ } sa;
+
+ socklen_t salen;
+ char *p;
+
+ assert(fd >= 0);
+ assert(ret);
+
+ salen = sizeof(sa);
+ if (getpeername(fd, &sa.sa, &salen) < 0)
+ return -errno;
+
+ switch (sa.sa.sa_family) {
+
+ case AF_INET: {
+ uint32_t a;
+
+ a = ntohl(sa.in.sin_addr.s_addr);
+
+ if (asprintf(&p,
+ "%u.%u.%u.%u:%u",
+ a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
+ ntohs(sa.in.sin_port)) < 0)
+ return -ENOMEM;
+
+ break;
+ }
+
+ case AF_INET6: {
+ static const unsigned char ipv4_prefix[] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
+ };
+
+ if (memcmp(&sa.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
+ const uint8_t *a = sa.in6.sin6_addr.s6_addr+12;
+
+ if (asprintf(&p,
+ "%u.%u.%u.%u:%u",
+ a[0], a[1], a[2], a[3],
+ ntohs(sa.in6.sin6_port)) < 0)
+ return -ENOMEM;
+ } else {
+ char a[INET6_ADDRSTRLEN];
+
+ if (asprintf(&p,
+ "%s:%u",
+ inet_ntop(AF_INET6, &sa.in6.sin6_addr, a, sizeof(a)),
+ ntohs(sa.in6.sin6_port)) < 0)
+ return -ENOMEM;
+ }
+
+ break;
+ }
+
+ case AF_UNIX: {
+ struct ucred ucred;
+
+ salen = sizeof(ucred);
+ if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &salen) < 0)
+ return -errno;
+
+ if (asprintf(&p, "PID %lu/UID %lu", (unsigned long) ucred.pid, (unsigned long) ucred.pid) < 0)
+ return -ENOMEM;
+
+ break;
+ }
+
+ default:
+ return -ENOTSUP;
+ }
+
+ *ret = p;
+ return 0;
+}
+
static const char* const netlink_family_table[] = {
[NETLINK_ROUTE] = "route",
[NETLINK_FIREWALL] = "firewall",
[NETLINK_ECRYPTFS] = "ecryptfs"
};
-DEFINE_STRING_TABLE_LOOKUP(netlink_family, int);
+DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX);
static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = {
[SOCKET_ADDRESS_DEFAULT] = "default",