diff options
| author | Mattias Andrée <maandree@kth.se> | 2017-05-25 15:30:17 +0200 |
|---|---|---|
| committer | Mattias Andrée <maandree@kth.se> | 2017-05-25 17:47:23 +0200 |
| commit | 7b147cb0c1eaa728f244cc8390ccfcc3f4aee03a (patch) | |
| tree | 80e23538c1d8b364ec9c8c7e58e895b9a471dfef /src/blind-to-named.c | |
| parent | Fix errors and warnings and make the code cleaner (diff) | |
| download | blind-7b147cb0c1eaa728f244cc8390ccfcc3f4aee03a.tar.gz blind-7b147cb0c1eaa728f244cc8390ccfcc3f4aee03a.tar.bz2 blind-7b147cb0c1eaa728f244cc8390ccfcc3f4aee03a.tar.xz | |
Add blind-from-named and blind-to-named
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/blind-to-named.c')
| -rw-r--r-- | src/blind-to-named.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/blind-to-named.c b/src/blind-to-named.c new file mode 100644 index 0000000..a42f771 --- /dev/null +++ b/src/blind-to-named.c @@ -0,0 +1,84 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + +USAGE("[-a] path") + +static void +esend_fd(int sock, int fd) +{ + char buf[1]; + struct iovec iov; + struct msghdr msg; + struct cmsghdr *cmsg; + char cms[CMSG_SPACE(sizeof(fd))]; + + buf[0] = 0; + iov.iov_base = buf; + iov.iov_len = 1; + + memset(&msg, 0, sizeof(msg)); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + msg.msg_control = (caddr_t)cms; + msg.msg_controllen = CMSG_LEN(sizeof(fd)); + + cmsg = CMSG_FIRSTHDR(&msg); + cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); + + if (sendmsg(sock, &msg, 0) != iov.iov_len) + eprintf("sendmsg:"); +} + +int +main(int argc, char *argv[]) +{ + struct sockaddr_un addr; + int abstract = 0; + int serverfd, connfd; + + ARGBEGIN { + case 'a': + abstract = 1; + break; + default: + usage(); + } ARGEND; + if (argc != 1) + usage(); + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + if (strlen(argv[0]) + 1 + abstract > sizeof(addr.sun_path)) { + errno = ENAMETOOLONG; + eprintf("%s:", argv[0]); + } + strcpy(addr.sun_path + abstract, argv[0]); + + serverfd = socket(PF_UNIX, SOCK_SEQPACKET, 0); + if (serverfd < 0) + eprintf("socket PF_UNIX SOCK_SEQPACKET:"); + + if (bind(serverfd, (const struct sockaddr *)&addr, (size_t)sizeof(addr)) < 0) + eprintf("bind %s%s%s:", + abstract ? "<abstract:" : "", + addr.sun_path + abstract, + abstract ? ">" : ""); + + if (listen(serverfd, 1) < 0) + eprintf("listen:"); + + connfd = accept(serverfd, NULL, NULL); + if (connfd < 0) + eprintf("accept:"); + + if (*addr.sun_path) + unlink(addr.sun_path); + close(serverfd); + + esend_fd(connfd, STDIN_FILENO); + close(connfd); + return 0; +} |
