[55923b7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2014 Mike Belopuhov
|
---|
| 3 | * Copyright (c) 2009 Michael Shalayeff
|
---|
| 4 | *
|
---|
| 5 | * Permission to use, copy, modify, and distribute this software for any
|
---|
| 6 | * purpose with or without fee is hereby granted, provided that the above
|
---|
| 7 | * copyright notice and this permission notice appear in all copies.
|
---|
| 8 | *
|
---|
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
---|
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
---|
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
---|
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
---|
| 13 | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
|
---|
| 14 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
---|
| 15 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | #include <sys/param.h>
|
---|
| 19 | #include <sys/socket.h>
|
---|
| 20 | #include <sys/time.h>
|
---|
| 21 | #include <sys/uio.h>
|
---|
| 22 | #include <netinet/in.h>
|
---|
| 23 | #include <arpa/inet.h>
|
---|
| 24 | #include <errno.h>
|
---|
| 25 | #include <stdlib.h>
|
---|
| 26 | #include <string.h>
|
---|
| 27 | #include <stdio.h>
|
---|
| 28 | #include <unistd.h>
|
---|
| 29 | #include <syslog.h>
|
---|
| 30 | #include <sysexits.h>
|
---|
| 31 | #include <login_cap.h>
|
---|
| 32 | #include <event.h>
|
---|
| 33 | #include <pwd.h>
|
---|
| 34 | #include <netdb.h>
|
---|
| 35 |
|
---|
| 36 | #include "icb.h"
|
---|
| 37 | #include "icbd.h"
|
---|
| 38 |
|
---|
| 39 | void logger_dispatch(int, short, void *);
|
---|
| 40 |
|
---|
| 41 | int logger_pipe;
|
---|
| 42 |
|
---|
| 43 | struct icbd_logentry {
|
---|
| 44 | time_t timestamp;
|
---|
| 45 | char group[ICB_MAXGRPLEN];
|
---|
| 46 | char nick[ICB_MAXNICKLEN];
|
---|
| 47 | size_t length;
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | int
|
---|
| 51 | logger_init(void)
|
---|
| 52 | {
|
---|
| 53 | static struct event ev;
|
---|
| 54 | struct passwd *pw;
|
---|
| 55 | int pipes[2];
|
---|
| 56 |
|
---|
| 57 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
|
---|
| 58 | syslog(LOG_ERR, "socketpair: %m");
|
---|
| 59 | exit(EX_OSERR);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | switch (fork()) {
|
---|
| 63 | case -1:
|
---|
| 64 | syslog(LOG_ERR, "fork: %m");
|
---|
| 65 | exit(EX_OSERR);
|
---|
| 66 | case 0:
|
---|
| 67 | break;
|
---|
| 68 |
|
---|
| 69 | default:
|
---|
| 70 | close(pipes[1]);
|
---|
| 71 | logger_pipe = pipes[0];
|
---|
| 72 | return (0);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | setproctitle("logger");
|
---|
| 76 | close(pipes[0]);
|
---|
| 77 |
|
---|
| 78 | if ((pw = getpwnam(ICBD_USER)) == NULL) {
|
---|
| 79 | syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
|
---|
| 80 | exit(EX_NOUSER);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | if (setusercontext(NULL, pw, pw->pw_uid,
|
---|
| 84 | LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
|
---|
| 85 | exit(EX_NOPERM);
|
---|
| 86 |
|
---|
| 87 | if (setuid(pw->pw_uid) < 0) {
|
---|
| 88 | syslog(LOG_ERR, "%d: %m", pw->pw_uid);
|
---|
| 89 | exit(EX_NOPERM);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | if (chdir(pw->pw_dir) < 0) {
|
---|
| 93 | syslog(LOG_ERR, "chdir: %m");
|
---|
| 94 | exit(EX_UNAVAILABLE);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | event_init();
|
---|
| 98 |
|
---|
| 99 | /* event for message processing */
|
---|
| 100 | event_set(&ev, pipes[1], EV_READ | EV_PERSIST, logger_dispatch, NULL);
|
---|
| 101 | if (event_add(&ev, NULL) < 0) {
|
---|
| 102 | syslog(LOG_ERR, "event_add: %m");
|
---|
| 103 | exit (EX_UNAVAILABLE);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | return event_dispatch();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | void
|
---|
| 110 | logger_dispatch(int fd, short event, void *arg __attribute__((unused)))
|
---|
| 111 | {
|
---|
| 112 | char buf[512];
|
---|
| 113 | struct icbd_logentry e;
|
---|
| 114 | struct iovec iov[2];
|
---|
| 115 |
|
---|
| 116 | if (event != EV_READ)
|
---|
| 117 | return;
|
---|
| 118 |
|
---|
| 119 | bzero(&e, sizeof e);
|
---|
| 120 | iov[0].iov_base = &e;
|
---|
| 121 | iov[0].iov_len = sizeof e;
|
---|
| 122 |
|
---|
| 123 | iov[1].iov_base = buf;
|
---|
| 124 | iov[1].iov_len = sizeof buf;
|
---|
| 125 |
|
---|
| 126 | if (readv(fd, iov, 2) < (ssize_t)sizeof e) {
|
---|
| 127 | syslog(LOG_ERR, "logger read: %m");
|
---|
| 128 | exit(EX_DATAERR);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /* XXX */
|
---|
| 132 | if (iov[1].iov_len < e.length) {
|
---|
| 133 | syslog(LOG_ERR, "logger read %lu out of %lu",
|
---|
| 134 | iov[1].iov_len, e.length);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /* TODO: check time of the day and open the next file */
|
---|
| 138 |
|
---|
| 139 | fprintf(stderr, "%s@%s: %s\n", e.nick, e.group, buf);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | void
|
---|
| 143 | logger(time_t timestamp, char *group, char *nick, char *what)
|
---|
| 144 | {
|
---|
| 145 | struct icbd_logentry e;
|
---|
| 146 | struct iovec iov[2];
|
---|
| 147 |
|
---|
| 148 | e.timestamp = timestamp;
|
---|
| 149 | strlcpy(e.group, group, ICB_MAXGRPLEN);
|
---|
| 150 | strlcpy(e.nick, nick, ICB_MAXNICKLEN);
|
---|
| 151 | e.length = strlen(what) + 1;
|
---|
| 152 |
|
---|
| 153 | iov[0].iov_base = &e;
|
---|
| 154 | iov[0].iov_len = sizeof e;
|
---|
| 155 |
|
---|
| 156 | iov[1].iov_base = what;
|
---|
| 157 | iov[1].iov_len = e.length;
|
---|
| 158 |
|
---|
| 159 | if (writev(logger_pipe, iov, 2) == -1)
|
---|
| 160 | syslog(LOG_ERR, "logger write: %m");
|
---|
| 161 | }
|
---|