X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=src%2Fsocket2unix.c;h=1f741b5f5680e9b97d0f09be17540f718523d2cd;hb=16e3b5325c41eee1aa88ecd4e054be7b608a7810;hp=848a40bf68745eecf0fd9dd2bf3416d65ace8e61;hpb=39b505e0c28b8ee39bef37174cd12b0981b07d20;p=socket2unix%2Fsocket2unix.git diff --git a/src/socket2unix.c b/src/socket2unix.c index 848a40b..1f741b5 100644 --- a/src/socket2unix.c +++ b/src/socket2unix.c @@ -22,11 +22,12 @@ /* Necessary for RTLD_NEXT. */ #define _GNU_SOURCE +#include + #include #include #include #include -#include #include #include #include @@ -47,22 +48,33 @@ #define LOG_LEVEL_PERROR 42 +#define OPTION_PARSED (1 << 1) +/* Don't intercept listen(), accept(). */ +#define OPTION_CLIENT_ONLY (1 << 2) +/* Don't intercept connect(). */ +#define OPTION_SERVER_ONLY (1 << 3) + /* GLOBAL VARIABLES */ struct list { - int unix_sockfd; - + int orig_sockfd; int orig_domain; int orig_type; + /* Used by listen(). */ + struct sockaddr *orig_addr; + socklen_t orig_addrlen; + struct list *next; }; static struct list socket_list = { - .unix_sockfd = -1, /* must not match a valid sockfd */ + .orig_sockfd = -1, /* must not match a valid sockfd */ }; +static int global_options; + /* LOG FUNCTIONS/MACROS */ @@ -154,15 +166,23 @@ static void log_(int level, const char *file, int line, const char *format, ...) /* OTHER FUNCTIONS */ +static void *xmalloc(size_t size) { + void *x = malloc(size); + if (!x) { + DIE("malloc(%zu)", size); + } + return x; +} + static struct list *find_sockfd(int sockfd) { struct list *e; - if (sockfd == socket_list.unix_sockfd) { + if (sockfd == socket_list.orig_sockfd) { return NULL; } for (e = &socket_list; e != NULL; e = e->next) { - if (e->unix_sockfd == sockfd) { + if (e->orig_sockfd == sockfd) { return e; } } @@ -171,12 +191,12 @@ static struct list *find_sockfd(int sockfd) { static struct list *remove_sockfd(int sockfd) { struct list *e, *p; - if (sockfd == socket_list.unix_sockfd) { + if (sockfd == socket_list.orig_sockfd) { return NULL; } for (e = &socket_list, p = NULL; e != NULL; p = e, e = e->next) { - if (e->unix_sockfd == sockfd) { + if (e->orig_sockfd == sockfd) { p->next = e->next; return e; } @@ -197,7 +217,11 @@ static const char *get_socket_path(void) { static int get_log_level(void) { const char *level = getenv("SOCKET2UNIX_DEBUG"); if (!level) { +#ifdef DEBUG + return LOG_LEVEL_DEBUG; +#else return LOG_LEVEL_WARNING; +#endif } int number = atoi(level); if (number <= 0 || number > LOG_LEVEL_DEBUG) { @@ -205,6 +229,49 @@ static int get_log_level(void) { } return number; } +static int get_options(void) { + const char *pos = getenv("SOCKET2UNIX_OPTIONS"); + if (!pos) { + return OPTION_PARSED; + } + + int options = OPTION_PARSED; + + while (*pos != '\0') { + size_t length; + + const char *end = strchr(pos, ','); + if (end == NULL) { + length = strlen(pos); + } else { + length = (size_t)(end - pos); + } + + if (!strncmp("client_only", pos, length)) { + options |= OPTION_CLIENT_ONLY; + } else if (!strncmp("server_only", pos, length)) { + options |= OPTION_SERVER_ONLY; + } else { + char option[length + 1]; + strncpy(option, pos, length); + option[length] = '\0'; + ERROR("unknown option '%s' in SOCKET2UNIX_OPTIONS\n", + option); + } + + if (end == NULL) { + break; + } + pos = end + 1; + } + + if ((options & OPTION_CLIENT_ONLY) && (options & OPTION_SERVER_ONLY)) { + ERROR("conflicting options 'client_only', 'server_only' " + "in SOCKET2UNIX_OPTIONS\n"); + } + + return options; +} static const char *af_to_name(int af) { if (af == AF_UNIX) { @@ -217,18 +284,28 @@ static const char *af_to_name(int af) { return "AF_INET6"; } else if (af == AF_IPX) { return "AF_IPX"; +#ifdef AF_NETLINK } else if (af == AF_NETLINK) { return "AF_NETLINK"; +#endif +#ifdef AF_X25 } else if (af == AF_X25) { return "AF_X25"; +#endif +#ifdef AF_AX25 } else if (af == AF_AX25) { return "AF_AX25"; +#endif +#ifdef AF_ATMPVC } else if (af == AF_ATMPVC) { return "AF_ATMPVC"; +#endif } else if (af == AF_APPLETALK) { return "AF_APPLETALK"; +#ifdef AF_PACKET } else if (af == AF_PACKET) { return "AF_PACKET"; +#endif } else { return "AF_UNKNOWN"; } @@ -244,8 +321,10 @@ static const char *sock_to_name(int sock) { return "SOCK_RAW"; } else if (sock & SOCK_RDM) { return "SOCK_RDM"; +#ifdef SOCK_PACKET } else if (sock & SOCK_PACKET) { return "SOCK_PACKET"; +#endif } else { return "SOCK_UNKNOWN"; } @@ -254,10 +333,14 @@ static const char *sock_to_name(int sock) { static const char *level_to_name(int level) { if (level == SOL_SOCKET) { return "SOL_SOCKET"; +#ifdef SOL_IP } else if (level == SOL_IP) { return "SOL_IP"; +#endif +#ifdef SOL_IPV6 } else if (level == SOL_IPV6) { return "SOL_IPV6"; +#endif } else if (level == IPPROTO_TCP) { return "IPPROTO_TCP"; } else if (level == IPPROTO_UDP) { @@ -311,6 +394,22 @@ static int set_sockaddr_un(struct sockaddr_un *sockaddr, return 0; } +static int replace_socket(int replaceefd, int replacerfd) { + static int (*real_close)(int); + LOAD_FUNCTION(real_close, "close"); + + /* Replace socket replaceefd with replacerfd. After dup2() both socket fds + * point to the same socket (replacerfd). */ + if (dup2(replacerfd, replaceefd) < 0) { + return -1; + } + /* We don't need replacerfd anymore. The program will use our replacement + * and we don't need it for anything else. Use real_close() to prevent + * unnecessary debug messages. */ + real_close(replacerfd); + return 0; +} + /* FUNCTIONS OVERWRITTEN BY LD_PRELOAD */ @@ -318,35 +417,31 @@ int socket(int domain, int type, int protocol) { static int (*real_socket)(int, int, int); LOAD_FUNCTION(real_socket, "socket"); - if (domain == AF_UNIX || domain == AF_LOCAL) { - return real_socket(domain, type, protocol); + /* We return the normal socket because we don't know yet if it's a client + * or a listen socket and therefore if we should replace it or not. This + * happens in listen() and connect(), see below. */ + + int sockfd = real_socket(domain, type, protocol); + if (sockfd < 0 + || domain == AF_UNIX + || domain == AF_LOCAL) { + return sockfd; } DBG("socket(%s, %s, %d)\n", af_to_name(domain), sock_to_name(type), protocol); - /* We must return the replacement socket in case the program uses select() - * or similar on it. */ - - int unix_sockfd = real_socket(AF_UNIX, type, 0); - if (unix_sockfd < 0) { - DIE("bind(): failed to create UNIX socket"); - } - - struct list *entry = malloc(sizeof(*entry)); - if (!entry) { - DIE("socket(): malloc"); - } + struct list *entry = xmalloc(sizeof(*entry)); memset(entry, 0, sizeof(*entry)); - entry->unix_sockfd = unix_sockfd; + entry->orig_sockfd = sockfd; entry->orig_domain = domain; entry->orig_type = type; entry->next = socket_list.next; socket_list.next = entry; - return unix_sockfd; + return sockfd; } int close(int fd) { @@ -360,7 +455,8 @@ int close(int fd) { DBG("close(%d): sockfd not found\n", fd); return real_close(fd); } - assert(fd == entry->unix_sockfd); + assert(fd == entry->orig_sockfd); + free(entry->orig_addr); free(entry); return real_close(fd); @@ -383,27 +479,63 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { DBG("bind(%d, ..): sockfd not found\n", sockfd); return real_bind(sockfd, addr, addrlen); } - assert(sockfd == entry->unix_sockfd); + assert(sockfd == entry->orig_sockfd); DBG("bind(%d, ..): %s %s\n", sockfd, af_to_name(entry->orig_domain), sock_to_name(entry->orig_type)); + /* Copy struct sockaddr, we need it later in listen(). */ + entry->orig_addr = xmalloc(addrlen); + memcpy(entry->orig_addr, addr, addrlen); + entry->orig_addrlen = addrlen; + + return real_bind(sockfd, addr, addrlen); +} + +int listen(int sockfd, int backlog) { + static int (*real_listen)(int, int); + LOAD_FUNCTION(real_listen, "listen"); + + if (!global_options) { + global_options = get_options(); + } + + if (global_options & OPTION_CLIENT_ONLY) { + DBG("listen(%d, %d): server hooking disabled\n", sockfd, backlog); + return real_listen(sockfd, backlog); + } + + struct list *entry = find_sockfd(sockfd); + if (!entry) { + DBG("listen(%d, %d): sockfd not found\n", sockfd, backlog); + return real_listen(sockfd, backlog); + } + assert(sockfd == entry->orig_sockfd); + DBG("listen(%d, %d): %s %s\n", + sockfd, backlog, + af_to_name(entry->orig_domain), sock_to_name(entry->orig_type)); + + int unix_sockfd = socket(AF_UNIX, entry->orig_type, 0); + if (unix_sockfd < 0) { + DIE("listen(): failed to create UNIX socket"); + } + struct sockaddr_un sockaddr; - if (set_sockaddr_un(&sockaddr, addr, addrlen) != 0) { - ERROR("connect(%d, ..) failed\n", sockfd); + if (set_sockaddr_un(&sockaddr, entry->orig_addr, + entry->orig_addrlen) != 0) { + ERROR("listen(%d, ..) failed\n", sockfd); } - DBG("bind(%d, ..): using path '%s'\n", sockfd, sockaddr.sun_path); + DBG("listen(%d, ..): using path '%s'\n", sockfd, sockaddr.sun_path); int attempts = 0; while (attempts < 10) { - if (real_bind(entry->unix_sockfd, (struct sockaddr *)&sockaddr, - sizeof(sockaddr)) == 0) { - /* Success. */ - return 0; + if (bind(unix_sockfd, (struct sockaddr *)&sockaddr, + sizeof(sockaddr)) == 0) { + break; } if (errno != EADDRINUSE) { - DIE("bind(%d, ..): failed to bind to '%s'", + DIE("listen(%d, ..): failed to bind to '%s'", sockfd, sockaddr.sun_path); } @@ -414,45 +546,34 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { struct stat buf; if (lstat(sockaddr.sun_path, &buf) != 0) { /* Looks like a race, better abort. */ - DIE("bind(%d, ..): lstat on UNIX socket '%s' failed", + DIE("listen(%d, ..): lstat on UNIX socket '%s' failed", sockfd, sockaddr.sun_path); } if (!S_ISSOCK(buf.st_mode)) { - ERROR("bind(%d, ..): path '%s' exits and is no socket\n", + ERROR("listen(%d, ..): path '%s' exits and is no socket\n", sockfd, sockaddr.sun_path); } - WARN("bind(%d, ..): unlinking '%s'\n", sockfd, sockaddr.sun_path); + WARN("listen(%d, ..): unlinking '%s'\n", sockfd, sockaddr.sun_path); if (unlink(sockaddr.sun_path) != 0) { - DIE("bind(%d, ..): unlink '%s' failed", + DIE("listen(%d, ..): unlink '%s' failed", sockfd, sockaddr.sun_path); } attempts++; } - ERROR("bind(%d, ..): failed to create UNIX socket file\n", sockfd); - return -1; /* never reached */ -} - -int listen(int sockfd, int backlog) { - static int (*real_listen)(int, int); - LOAD_FUNCTION(real_listen, "listen"); - - DBG("listen(%d, %d)\n", sockfd, backlog); + if (attempts == 10) { + ERROR("listen(%d, ..): failed to create UNIX socket file\n", sockfd); + } - struct list *entry = find_sockfd(sockfd); - if (!entry) { - DBG("listen(%d, %d): sockfd not found\n", sockfd, backlog); - return real_listen(sockfd, backlog); + /* Replace the original socket of the program with our socket. */ + if (replace_socket(entry->orig_sockfd, unix_sockfd)) { + DIE("listen(): failed to replace socket"); } - assert(sockfd == entry->unix_sockfd); - DBG("listen(%d, %d): %s %s\n", - sockfd, backlog, - af_to_name(entry->orig_domain), sock_to_name(entry->orig_type)); - if (real_listen(entry->unix_sockfd, backlog) != 0) { + if (real_listen(entry->orig_sockfd, backlog) != 0) { DIE("listen(): failed to listen"); } @@ -463,6 +584,15 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { static int (*real_accept)(int, struct sockaddr *, socklen_t *); LOAD_FUNCTION(real_accept, "accept"); + if (!global_options) { + global_options = get_options(); + } + + if (global_options & OPTION_CLIENT_ONLY) { + DBG("accept(%d, ..): server hooking disabled\n", sockfd); + return real_accept(sockfd, addr, addrlen); + } + DBG("accept(%d, ..)\n", sockfd); struct list *entry = find_sockfd(sockfd); @@ -470,14 +600,14 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { DBG("accept(%d, ..): sockfd not found\n", sockfd); return real_accept(sockfd, addr, addrlen); } - assert(sockfd == entry->unix_sockfd); + assert(sockfd == entry->orig_sockfd); DBG("accept(%d, ..): %s %s\n", sockfd, af_to_name(entry->orig_domain), sock_to_name(entry->orig_type)); struct sockaddr_un sockaddr; socklen_t size = sizeof(sockaddr); - int sock = real_accept(entry->unix_sockfd, (struct sockaddr *)&sockaddr, + int sock = real_accept(entry->orig_sockfd, (struct sockaddr *)&sockaddr, &size); if (sock < 0) { DIE("accept(%d, ..): failed to accept", sockfd); @@ -508,6 +638,15 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { static int (*real_connect)(int, const struct sockaddr *, socklen_t); LOAD_FUNCTION(real_connect, "connect"); + if (!global_options) { + global_options = get_options(); + } + + if (global_options & OPTION_SERVER_ONLY) { + DBG("connect(%d, ..): client hooking disabled\n", sockfd); + return real_connect(sockfd, addr, addrlen); + } + DBG("connect(%d, ..)\n", sockfd); if (addr == NULL || addrlen < sizeof(addr->sa_family) @@ -521,11 +660,21 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { DBG("connect(%d, ..): sockfd not found\n", sockfd); return real_connect(sockfd, addr, addrlen); } - assert(sockfd == entry->unix_sockfd); + assert(sockfd == entry->orig_sockfd); DBG("connect(%d, ..): %s %s\n", sockfd, af_to_name(entry->orig_domain), sock_to_name(entry->orig_type)); + int unix_sockfd = socket(AF_UNIX, entry->orig_type, 0); + if (unix_sockfd < 0) { + DIE("bind(): failed to create UNIX socket"); + } + + /* Replace the original socket of the program with our socket. */ + if (replace_socket(entry->orig_sockfd, unix_sockfd)) { + DIE("connect(): failed to replace socket"); + } + struct sockaddr_un sockaddr; if (set_sockaddr_un(&sockaddr, addr, addrlen) != 0) { ERROR("connect(%d, ..) failed\n", sockfd); @@ -533,7 +682,7 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { DBG("connect(%d, ..): using path '%s'\n", sockfd, sockaddr.sun_path); - if (real_connect(entry->unix_sockfd, (struct sockaddr *)&sockaddr, + if (real_connect(entry->orig_sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) { DIE("connect(%d, ..): failed to connect", sockfd); }