Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Fake identd server. |
| 4 | * |
Denis Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 Denys Vlasenko |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 6 | * |
| 7 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 11 | #include <syslog.h> |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 12 | #include "isrv.h" |
| 13 | |
| 14 | enum { TIMEOUT = 20 }; |
| 15 | |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 16 | typedef struct identd_buf_t { |
| 17 | int pos; |
| 18 | int fd_flag; |
| 19 | char buf[64 - 2*sizeof(int)]; |
| 20 | } identd_buf_t; |
| 21 | |
Denis Vlasenko | 04c99eb | 2007-04-07 00:44:31 +0000 | [diff] [blame] | 22 | #define bogouser bb_common_bufsiz1 |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 23 | |
| 24 | static int new_peer(isrv_state_t *state, int fd) |
| 25 | { |
| 26 | int peer; |
| 27 | identd_buf_t *buf = xzalloc(sizeof(*buf)); |
| 28 | |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 29 | peer = isrv_register_peer(state, buf); |
| 30 | if (peer < 0) |
| 31 | return 0; /* failure */ |
| 32 | if (isrv_register_fd(state, peer, fd) < 0) |
| 33 | return peer; /* failure, unregister peer */ |
| 34 | |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 35 | buf->fd_flag = fcntl(fd, F_GETFL) | O_NONBLOCK; |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 36 | isrv_want_rd(state, fd); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static int do_rd(int fd, void **paramp) |
| 41 | { |
| 42 | identd_buf_t *buf = *paramp; |
| 43 | char *cur, *p; |
Denis Vlasenko | c14c95e | 2007-01-14 12:31:26 +0000 | [diff] [blame] | 44 | int retval = 0; /* session is ok (so far) */ |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 45 | int sz; |
| 46 | |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 47 | cur = buf->buf + buf->pos; |
| 48 | |
Denis Vlasenko | c14c95e | 2007-01-14 12:31:26 +0000 | [diff] [blame] | 49 | if (buf->fd_flag & O_NONBLOCK) |
| 50 | fcntl(fd, F_SETFL, buf->fd_flag); |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 51 | sz = safe_read(fd, cur, sizeof(buf->buf) - buf->pos); |
| 52 | |
| 53 | if (sz < 0) { |
| 54 | if (errno != EAGAIN) |
| 55 | goto term; /* terminate this session if !EAGAIN */ |
| 56 | goto ok; |
| 57 | } |
| 58 | |
| 59 | buf->pos += sz; |
| 60 | buf->buf[buf->pos] = '\0'; |
| 61 | p = strpbrk(cur, "\r\n"); |
| 62 | if (p) |
| 63 | *p = '\0'; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 64 | if (!p && sz && buf->pos <= (int)sizeof(buf->buf)) |
Denis Vlasenko | c14c95e | 2007-01-14 12:31:26 +0000 | [diff] [blame] | 65 | goto ok; |
| 66 | /* Terminate session. If we are in server mode, then |
| 67 | * fd is still in nonblocking mode - we never block here */ |
Denis Vlasenko | d97c924 | 2007-01-14 13:12:06 +0000 | [diff] [blame] | 68 | if (fd == 0) fd++; /* inetd mode? then write to fd 1 */ |
Denis Vlasenko | c14c95e | 2007-01-14 12:31:26 +0000 | [diff] [blame] | 69 | fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser); |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 70 | term: |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 71 | free(buf); |
Denis Vlasenko | c14c95e | 2007-01-14 12:31:26 +0000 | [diff] [blame] | 72 | retval = 1; /* terminate */ |
| 73 | ok: |
| 74 | if (buf->fd_flag & O_NONBLOCK) |
| 75 | fcntl(fd, F_SETFL, buf->fd_flag & ~O_NONBLOCK); |
| 76 | return retval; |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 79 | static int do_timeout(void **paramp ATTRIBUTE_UNUSED) |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 80 | { |
| 81 | return 1; /* terminate session */ |
| 82 | } |
| 83 | |
| 84 | static void inetd_mode(void) |
| 85 | { |
| 86 | identd_buf_t *buf = xzalloc(sizeof(*buf)); |
Denis Vlasenko | b8b81a6 | 2007-01-14 12:47:27 +0000 | [diff] [blame] | 87 | /* buf->pos = 0; - xzalloc did it */ |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 88 | /* We do NOT want nonblocking I/O here! */ |
Denis Vlasenko | b8b81a6 | 2007-01-14 12:47:27 +0000 | [diff] [blame] | 89 | /* buf->fd_flag = 0; - xzalloc did it */ |
Denis Vlasenko | 1925081 | 2007-01-14 12:07:25 +0000 | [diff] [blame] | 90 | do |
| 91 | alarm(TIMEOUT); |
Denis Vlasenko | b8b81a6 | 2007-01-14 12:47:27 +0000 | [diff] [blame] | 92 | while (do_rd(0, (void*)&buf) == 0); |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 95 | int fakeidentd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 96 | int fakeidentd_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 97 | { |
| 98 | enum { |
| 99 | OPT_foreground = 0x1, |
| 100 | OPT_inetd = 0x2, |
| 101 | OPT_inetdwait = 0x4, |
Denis Vlasenko | d97c924 | 2007-01-14 13:12:06 +0000 | [diff] [blame] | 102 | OPT_fiw = 0x7, |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 103 | OPT_bindaddr = 0x8, |
| 104 | }; |
| 105 | |
| 106 | const char *bind_address = NULL; |
| 107 | unsigned opt; |
| 108 | int fd; |
| 109 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 110 | opt = getopt32(argv, "fiwb:", &bind_address); |
Denis Vlasenko | 04c99eb | 2007-04-07 00:44:31 +0000 | [diff] [blame] | 111 | strcpy(bogouser, "nobody"); |
| 112 | if (argv[optind]) |
| 113 | strncpy(bogouser, argv[optind], sizeof(bogouser)); |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 114 | |
Denis Vlasenko | d97c924 | 2007-01-14 13:12:06 +0000 | [diff] [blame] | 115 | /* Daemonize if no -f and no -i and no -w */ |
Denis Vlasenko | 43d5d42 | 2008-05-15 19:44:46 +0000 | [diff] [blame] | 116 | if (!(opt & OPT_fiw)) |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 117 | bb_daemonize_or_rexec(0, argv); |
| 118 | |
Denis Vlasenko | d97c924 | 2007-01-14 13:12:06 +0000 | [diff] [blame] | 119 | /* Where to log in inetd modes? "Classic" inetd |
| 120 | * probably has its stderr /dev/null'ed (we need log to syslog?), |
| 121 | * but daemontools-like utilities usually expect that children |
| 122 | * log to stderr. I like daemontools more. Go their way. |
| 123 | * (Or maybe we need yet another option "log to syslog") */ |
| 124 | if (!(opt & OPT_fiw) /* || (opt & OPT_syslog) */) { |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 125 | openlog(applet_name, 0, LOG_DAEMON); |
| 126 | logmode = LOGMODE_SYSLOG; |
| 127 | } |
| 128 | |
| 129 | if (opt & OPT_inetd) { |
| 130 | inetd_mode(); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* Ignore closed connections when writing */ |
| 135 | signal(SIGPIPE, SIG_IGN); |
| 136 | |
Denis Vlasenko | d97c924 | 2007-01-14 13:12:06 +0000 | [diff] [blame] | 137 | fd = 0; |
| 138 | if (!(opt & OPT_inetdwait)) { |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 139 | fd = create_and_bind_stream_or_die(bind_address, |
| 140 | bb_lookup_port("identd", "tcp", 113)); |
| 141 | xlisten(fd, 5); |
| 142 | } |
| 143 | |
Denis Vlasenko | 1925081 | 2007-01-14 12:07:25 +0000 | [diff] [blame] | 144 | isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout, |
| 145 | TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0); |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 146 | return 0; |
| 147 | } |