irced

ed, but like irc
git clone git://bvnf.space/irced.git
Log | Files | Refs | README

irced.c (7765B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define NUM_CONNS 3
#define UNAME_LEN 10

char *program[] = {"/bin/ed", "-p", "* ", NULL};

void
fail(char *s, ...) {
    char e[0x400] = {'\0'};
    va_list ap;
    fflush(stdout);

    va_start(ap, s);
    vsnprintf(e, sizeof(e), s, ap);
    va_end(ap);

    syslog(LOG_DAEMON | LOG_ERR, "%s", e);
    fprintf(stderr, "irced: %s\n", e);
    exit(1);
}

void
drop_privileges(char *path) {
#ifdef __OpenBSD__
    if (unveil(path, "rwxc") != 0)
        fail("unveil failed: %s", strerror(errno));
#endif
    if (chdir(path) != 0)
        fail("chdir(%s) failed: %s", path, strerror(errno));
}

void sigchld_handler(int s) {
    int e = errno;
    while (waitpid(-1, NULL, WNOHANG) > 0);
    errno = e;
}

void
usage(char *argv0) {
    fprintf(stderr, "usage: %s [-p port] dir\n", argv0);
}

int
start_listening(char *port) {
    int sfd = -1;
    struct addrinfo hints = {0}, *result, *rp;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    hints.ai_protocol = 0;

    int s = getaddrinfo(NULL, port, &hints, &result);
    if (s != 0)
        fail("getaddrinfo: %s", gai_strerror(s));

    for (rp = result; rp != NULL; rp = rp->ai_next) {
        sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (sfd == -1)
            continue;

        if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
            break;

        if (close(sfd) == -1) {
            freeaddrinfo(result);
            fail("close: %s", strerror(errno));
        }
    }

    freeaddrinfo(result);

    if (rp == NULL)
        fail("localhost:%s: connection refused", port);

    /* can have up to SOMAXCONN backlogged. */
    if (listen(sfd, 10) == -1) {
        close(sfd);
        fail("listen: %s", strerror(errno));
    }

    return sfd;
}

int
accept_one(int sfd) {
    int newfd;
    struct sockaddr their_addr;
    socklen_t addr_size = sizeof their_addr;
    newfd = accept(sfd, &their_addr, &addr_size);
    fprintf(stderr, "accepted %d\n", newfd);
    if (newfd == -1)
        fail("accept: %s", strerror(errno));
    return newfd;
}

void
fork_rw(char *cmd[], int *wr, int *rd) {
    int to[2], from[2];
    pipe(to);
    pipe(from);
    *wr = to[1];
    *rd = from[0];

    switch (fork()) {
        case -1:
            fail("fork failed: %s", strerror(errno));
            break;
        case 0:
            /* child */
            /* exec is promised, but only $PWD and /tmp are unveiled. */
            drop_privileges(".");
#ifdef __OpenBSD__
            if (unveil("/tmp/", "crw") != 0)
                fail("unveil failed: %s", strerror(errno));
#endif
            close(to[1]);
            close(from[0]);
            dup2(to[0], 0);
            dup2(from[1], 1);
            dup2(from[1], 2); /* also get stderr */
            execv(cmd[0], cmd);
            fail("exec %s failed: %s", strerror(errno));
            break;
        default:
            close(to[0]);
            close(from[1]);
            break;
    }
}

int
loop(int sfd) {
    int pollret, to_ed, from_ed, n;
    struct pollfd fds[NUM_CONNS + 2]; /* NUM_CONNS clients + server listening + program. */
    char unames[NUM_CONNS][UNAME_LEN + 1] = {'\0'};
    char buf[0x100] = {'\0'};
    ssize_t buf_used;
    n = 0;
    fds[0].fd = sfd; n++;
    fds[0].events = POLLIN;
    fork_rw(program, &to_ed, &from_ed);
    fds[1].fd = from_ed; n++;
    fds[1].events = POLLIN;

    while (1) {
        for (int i = 0; i < n; i++)
            fds[i].revents = 0;

        pollret = poll(fds, n, -1);
        if (pollret < 0) {
            if (errno == EAGAIN)
                continue;
            fail("poll: %s", strerror(errno));
            return 1;
        }
        if (fds[0].revents & POLLIN) {
            int newfd = accept_one(sfd);
            if (newfd == -1)
                continue;
            if (n + 1 > NUM_CONNS + 2) {
                dprintf(newfd, "sorry, my hands are full!\n(no more connections allowed.)\n");
                shutdown(newfd, SHUT_RDWR);
                close(newfd);
                continue;
            }

            dprintf(newfd, "welcome to the ed server!\nplease provide a username (max %d chars): ", UNAME_LEN);
            fds[n].fd = newfd;
            fds[n].events = POLLIN;
            n++;
        }
        for (int i = 1; i < n; i++) {
            if (fds[i].revents & POLLIN) {
                buf_used = read(fds[i].fd, buf, sizeof buf);
                if (buf_used <= 0) {
                    /* shutdown */
                    close(fds[i].fd);
                    fds[i].fd = -1;
                    /* TODO: actually handle this */
                } else {
                    fprintf(stderr, "got '");
                    write(2, buf, buf_used);
                    fprintf(stderr, "'\n");

                    if (i >=2 && unames[i-2][0] == '\0') {
                        /* first message from client; use as username */
                        fprintf(stderr, "using as username\n");
                        char *p;
                        memcpy(unames[i-2], buf, UNAME_LEN);
                        if ((p = memchr(unames[i-2], '\n', UNAME_LEN)))
                            *p = '\0';
                        if ((p = memchr(unames[i-2], '\r', UNAME_LEN)))
                            *p = '\0';
                        unames[i-2][UNAME_LEN] = '\0';
                        /* announce arrival */
                        buf_used = snprintf(buf, sizeof buf, "%s joined\n", unames[i-2]);
                        goto send_to_others;
                    }

                    /* send to ed */
                    if (i != 1 && write(to_ed, buf, buf_used) == -1)
                        fail("couldn't write to ed: %s", strerror(errno));
send_to_others:
                    /* send to others */
                    for (int j = 2; j < n; j++) {
                        if (i == j) /* don't send to self */
                            continue;
                        /* write username: */
                        if (i >= 2)
                            dprintf(fds[j].fd, "%s: ", unames[i - 2]);
                        if (send(fds[j].fd, buf, buf_used, 0) == -1) {
                            fprintf(stderr, "couldn't write to fd %d: %s\n", fds[j].fd, strerror(errno));
                            continue;
                        }
                    }
                }
            }
        }
    }
    return 0;
}

int
main(int argc, char **argv) {
    int c, ret, sfd;
    char *path, *port;
    ret = 0;
    port = "1314";

    openlog("irced", LOG_NDELAY, LOG_DAEMON);

    while ((c = getopt(argc, argv, "p:")) != -1) {
        switch(c) {
            case 'p':
                port = optarg;
                break;
            case '?':
                usage(argv[0]);
                return 1;
        }
    }
    if (argc - optind != 1) {
        usage(argv[0]);
        return 1;
    }

#ifdef __OpenBSD__
    if (unveil("/bin/", "x") != 0)
        fail("unveil failed: %s", strerror(errno));
#endif
    path = argv[optind];
    drop_privileges(path);
#ifdef __OpenBSD__
    if (pledge("exec stdio dns proc unveil rpath wpath cpath inet", "rpath wpath stdio proc tty cpath exec error") == -1)
        fail("pledge failed: %s", strerror(errno));
#endif

    sfd = start_listening(port);
    if (sfd == -1) {
        return 1;
    }
    fprintf(stderr, "bound to port %s\n", port);

    if (loop(sfd) != 0)
        ret = 1;

    close(sfd);
    return ret;
}