[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 |
|
---|
[0519a87] | 18 | #include <sys/types.h>
|
---|
[55923b7] | 19 | #include <sys/socket.h>
|
---|
[4e66b3a] | 20 | #include <sys/stat.h>
|
---|
[55923b7] | 21 | #include <sys/time.h>
|
---|
| 22 | #include <sys/uio.h>
|
---|
| 23 | #include <errno.h>
|
---|
[d27dc1e] | 24 | #include <limits.h>
|
---|
[0519a87] | 25 | #include <netdb.h>
|
---|
[55923b7] | 26 | #include <stdlib.h>
|
---|
| 27 | #include <string.h>
|
---|
| 28 | #include <stdio.h>
|
---|
| 29 | #include <unistd.h>
|
---|
| 30 | #include <syslog.h>
|
---|
| 31 | #include <sysexits.h>
|
---|
[a5893e9] | 32 | #include <time.h>
|
---|
[55923b7] | 33 | #include <event.h>
|
---|
| 34 | #include <pwd.h>
|
---|
| 35 |
|
---|
| 36 | #include "icb.h"
|
---|
| 37 | #include "icbd.h"
|
---|
| 38 |
|
---|
[be3ad87] | 39 | void logger_ioerr(struct bufferevent *, short, void *);
|
---|
| 40 | void logger_dispatch(struct bufferevent *, void *);
|
---|
[4e66b3a] | 41 | FILE *logger_open(char *);
|
---|
[9a2a703] | 42 | void logger_tick(void);
|
---|
[55923b7] | 43 |
|
---|
| 44 | struct icbd_logentry {
|
---|
| 45 | char group[ICB_MAXGRPLEN];
|
---|
| 46 | char nick[ICB_MAXNICKLEN];
|
---|
| 47 | size_t length;
|
---|
| 48 | };
|
---|
| 49 |
|
---|
[4e66b3a] | 50 | struct {
|
---|
| 51 | char group[ICB_MAXGRPLEN];
|
---|
| 52 | FILE *fp;
|
---|
| 53 | } logfiles[10];
|
---|
| 54 | int nlogfiles;
|
---|
[a5893e9] | 55 |
|
---|
[4e66b3a] | 56 | int logger_pipe;
|
---|
| 57 |
|
---|
| 58 | char file_ts[sizeof "0000-00"];
|
---|
[9a2a703] | 59 | char line_ts[sizeof "00:00"];
|
---|
[a5893e9] | 60 | struct event ev_tick;
|
---|
| 61 |
|
---|
[d27dc1e] | 62 | extern char logprefix[PATH_MAX/2];
|
---|
[3dba97d] | 63 | extern int dologging;
|
---|
| 64 |
|
---|
[55923b7] | 65 | int
|
---|
| 66 | logger_init(void)
|
---|
| 67 | {
|
---|
[be3ad87] | 68 | struct bufferevent *bev;
|
---|
[55923b7] | 69 | struct passwd *pw;
|
---|
| 70 | int pipes[2];
|
---|
| 71 |
|
---|
| 72 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
|
---|
[fe81e9a] | 73 | syslog(LOG_ERR, "%s: socketpair: %m", __func__);
|
---|
[55923b7] | 74 | exit(EX_OSERR);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | switch (fork()) {
|
---|
| 78 | case -1:
|
---|
[fe81e9a] | 79 | syslog(LOG_ERR, "%s: fork: %m", __func__);
|
---|
[55923b7] | 80 | exit(EX_OSERR);
|
---|
| 81 | case 0:
|
---|
| 82 | break;
|
---|
| 83 |
|
---|
| 84 | default:
|
---|
| 85 | close(pipes[1]);
|
---|
| 86 | logger_pipe = pipes[0];
|
---|
| 87 | return (0);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | setproctitle("logger");
|
---|
| 91 | close(pipes[0]);
|
---|
| 92 |
|
---|
| 93 | if ((pw = getpwnam(ICBD_USER)) == NULL) {
|
---|
[fe81e9a] | 94 | syslog(LOG_ERR, "%s: No passwd entry for %s", __func__,
|
---|
| 95 | ICBD_USER);
|
---|
[55923b7] | 96 | exit(EX_NOUSER);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[1dd3554] | 99 | if (chroot(pw->pw_dir) < 0) {
|
---|
[fe81e9a] | 100 | syslog(LOG_ERR, "%s: %s: %m", __func__, pw->pw_dir);
|
---|
[1dd3554] | 101 | exit(EX_UNAVAILABLE);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | if (chdir("/") < 0) {
|
---|
[fe81e9a] | 105 | syslog(LOG_ERR, "%s: chdir: %m", __func__);
|
---|
[55923b7] | 106 | exit(EX_UNAVAILABLE);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[cdd2ff5] | 109 | chdir (ICBD_HOME);
|
---|
| 110 |
|
---|
[e54f151] | 111 | if (setuid(pw->pw_uid) < 0) {
|
---|
[fe81e9a] | 112 | syslog(LOG_ERR, "%s: %d: %m", __func__, pw->pw_uid);
|
---|
[e54f151] | 113 | exit(EX_NOPERM);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[411aa63] | 116 | if (pledge("stdio cpath wpath", NULL) == -1) {
|
---|
| 117 | syslog(LOG_ERR, "%s: pledge", __func__);
|
---|
| 118 | exit(EX_NOPERM);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[55923b7] | 121 | event_init();
|
---|
| 122 |
|
---|
| 123 | /* event for message processing */
|
---|
[be3ad87] | 124 | if ((bev = bufferevent_new(pipes[1], logger_dispatch, NULL,
|
---|
| 125 | logger_ioerr, NULL)) == NULL) {
|
---|
[fe81e9a] | 126 | syslog(LOG_ERR, "%s: bufferevent_new: %m", __func__);
|
---|
[be3ad87] | 127 | exit(EX_UNAVAILABLE);
|
---|
| 128 | }
|
---|
| 129 | if (bufferevent_enable(bev, EV_READ)) {
|
---|
[fe81e9a] | 130 | syslog(LOG_ERR, "%s: bufferevent_enable: %m", __func__);
|
---|
[be3ad87] | 131 | bufferevent_free(bev);
|
---|
| 132 | exit(EX_UNAVAILABLE);
|
---|
[55923b7] | 133 | }
|
---|
| 134 |
|
---|
[9a2a703] | 135 | evtimer_set(&ev_tick, (void (*)(int, short, void *))logger_tick, NULL);
|
---|
| 136 | logger_tick();
|
---|
[55923b7] | 137 | return event_dispatch();
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void
|
---|
[be3ad87] | 141 | logger_ioerr(struct bufferevent *bev __attribute__((__unused__)), short what,
|
---|
| 142 | void *arg __attribute__((__unused__)))
|
---|
[55923b7] | 143 | {
|
---|
[be3ad87] | 144 | const char *cause = NULL;
|
---|
| 145 |
|
---|
| 146 | if (what & EVBUFFER_TIMEOUT)
|
---|
| 147 | cause = "timeout";
|
---|
| 148 | else if (what & EVBUFFER_EOF)
|
---|
| 149 | cause = "eof";
|
---|
| 150 | else if (what & EVBUFFER_ERROR)
|
---|
| 151 | cause = what & EVBUFFER_READ ? "read" : "write";
|
---|
[fe81e9a] | 152 | syslog(LOG_ERR, "%s: %s", __func__, cause ? cause : "unknown");
|
---|
[be3ad87] | 153 | exit(EX_IOERR);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | void
|
---|
| 157 | logger_dispatch(struct bufferevent *bev, void *arg __attribute__((unused)))
|
---|
| 158 | {
|
---|
| 159 | struct icbd_logentry *e;
|
---|
| 160 | static char buf[sizeof *e + ICB_MSGSIZE];
|
---|
| 161 | static size_t nread = 0;
|
---|
[4e66b3a] | 162 | FILE *fp = NULL;
|
---|
[be3ad87] | 163 | size_t res;
|
---|
| 164 | char *m;
|
---|
[4e66b3a] | 165 | int i;
|
---|
[55923b7] | 166 |
|
---|
[be3ad87] | 167 | e = (struct icbd_logentry *)buf;
|
---|
| 168 | m = buf + sizeof *e;
|
---|
| 169 |
|
---|
| 170 | while (EVBUFFER_LENGTH(EVBUFFER_INPUT(bev)) > 0) {
|
---|
| 171 | if (nread == 0) {
|
---|
| 172 | bzero(e, sizeof *e);
|
---|
| 173 | /* read the log entry header */
|
---|
| 174 | res = bufferevent_read(bev, &buf[0], sizeof *e);
|
---|
| 175 | nread += res;
|
---|
| 176 | if (nread < sizeof *e)
|
---|
| 177 | return;
|
---|
| 178 | }
|
---|
| 179 | /* see if we got the whole header */
|
---|
| 180 | if (nread < sizeof *e) {
|
---|
| 181 | /* finish reading */
|
---|
| 182 | res = bufferevent_read(bev, &buf[nread],
|
---|
| 183 | sizeof *e - nread);
|
---|
| 184 | nread += res;
|
---|
| 185 | if (nread < sizeof *e)
|
---|
| 186 | return;
|
---|
| 187 | }
|
---|
[28c4fd3] | 188 | if (e->length >= ICB_MSGSIZE) {
|
---|
| 189 | syslog(LOG_ERR, "%s: message too big: %lu", __func__,
|
---|
| 190 | e->length);
|
---|
| 191 | exit(EX_DATAERR);
|
---|
| 192 | }
|
---|
[be3ad87] | 193 | /* fetch the message */
|
---|
| 194 | res = bufferevent_read(bev, &buf[nread],
|
---|
[28c4fd3] | 195 | e->length - (nread - sizeof *e));
|
---|
[be3ad87] | 196 | nread += res;
|
---|
[614260f] | 197 | if (nread - sizeof *e < e->length)
|
---|
| 198 | return;
|
---|
[be3ad87] | 199 | #ifdef DEBUG
|
---|
| 200 | {
|
---|
[614260f] | 201 | printf("logger read %lu\n", nread - sizeof *e);
|
---|
| 202 | for (i = 0; i < (int)(nread - sizeof *e); i++)
|
---|
[be3ad87] | 203 | printf(" %02x", (unsigned char)m[i]);
|
---|
| 204 | printf("\n");
|
---|
| 205 | }
|
---|
| 206 | #endif
|
---|
| 207 | /* terminate the buffer */
|
---|
[a2fadb4] | 208 | m[MIN(nread - sizeof *e, ICB_MSGSIZE - 1)] = '\0';
|
---|
[be3ad87] | 209 | /* find the appropriate log file */
|
---|
| 210 | for (i = 0; i < nlogfiles; i++)
|
---|
| 211 | if (strcmp(logfiles[i].group, e->group) == 0)
|
---|
| 212 | fp = logfiles[i].fp;
|
---|
| 213 | if (!fp && (fp = logger_open(e->group)) == NULL)
|
---|
| 214 | return;
|
---|
| 215 | if (strlen(e->nick) == 0)
|
---|
| 216 | fprintf(fp, "[%s] %s\n", line_ts, m);
|
---|
| 217 | else
|
---|
| 218 | fprintf(fp, "[%s] <%s> %s\n", line_ts, e->nick, m);
|
---|
| 219 | /* get ready for the next message */
|
---|
| 220 | nread = 0;
|
---|
[55923b7] | 221 | }
|
---|
[4e66b3a] | 222 | }
|
---|
| 223 |
|
---|
| 224 | FILE *
|
---|
| 225 | logger_open(char *group)
|
---|
| 226 | {
|
---|
[d27dc1e] | 227 | char path[PATH_MAX];
|
---|
[4e66b3a] | 228 | FILE *fp = NULL;
|
---|
[55923b7] | 229 |
|
---|
[058b664] | 230 | /* make sure not to overflow the logfiles table */
|
---|
[fe81e9a] | 231 | if (nlogfiles == nitems(logfiles)) {
|
---|
| 232 | syslog(LOG_NOTICE, "%s: logfiles table is full", __func__);
|
---|
[058b664] | 233 | return (NULL);
|
---|
[fe81e9a] | 234 | }
|
---|
[3dba97d] | 235 | snprintf(path, sizeof path, "%s/%s", logprefix, group);
|
---|
| 236 | if (mkdir(path, 0755) < 0 && errno != EEXIST) {
|
---|
[fe81e9a] | 237 | syslog(LOG_ERR, "%s: %s: %m", __func__, group);
|
---|
[4e66b3a] | 238 | return (NULL);
|
---|
| 239 | }
|
---|
[3dba97d] | 240 | snprintf(path, sizeof path, "%s/%s/%s", logprefix, group, file_ts);
|
---|
[4e66b3a] | 241 | if ((fp = fopen(path, "a")) == NULL) {
|
---|
[fe81e9a] | 242 | syslog(LOG_ERR, "%s: %s: %m", __func__, path);
|
---|
[4e66b3a] | 243 | return (NULL);
|
---|
| 244 | }
|
---|
| 245 | setvbuf(fp, NULL, _IOLBF, 0);
|
---|
| 246 | if (verbose)
|
---|
[fe81e9a] | 247 | syslog(LOG_NOTICE, "%s: %s", __func__, path);
|
---|
[4e66b3a] | 248 | strlcpy(logfiles[nlogfiles].group, group, ICB_MAXGRPLEN);
|
---|
| 249 | logfiles[nlogfiles++].fp = fp;
|
---|
| 250 | return (fp);
|
---|
[55923b7] | 251 | }
|
---|
| 252 |
|
---|
| 253 | void
|
---|
[45bd56a] | 254 | logger(char *group, char *nick, char *what)
|
---|
[55923b7] | 255 | {
|
---|
| 256 | struct icbd_logentry e;
|
---|
| 257 | struct iovec iov[2];
|
---|
[d06af04] | 258 | const char *defgrp = "1";
|
---|
[55923b7] | 259 |
|
---|
[3dba97d] | 260 | if (!dologging)
|
---|
| 261 | return;
|
---|
| 262 |
|
---|
[d06af04] | 263 | if (strcmp(group, defgrp) == 0)
|
---|
| 264 | return;
|
---|
| 265 |
|
---|
[55923b7] | 266 | strlcpy(e.group, group, ICB_MAXGRPLEN);
|
---|
| 267 | strlcpy(e.nick, nick, ICB_MAXNICKLEN);
|
---|
| 268 | e.length = strlen(what) + 1;
|
---|
| 269 |
|
---|
| 270 | iov[0].iov_base = &e;
|
---|
| 271 | iov[0].iov_len = sizeof e;
|
---|
| 272 |
|
---|
| 273 | iov[1].iov_base = what;
|
---|
| 274 | iov[1].iov_len = e.length;
|
---|
| 275 |
|
---|
| 276 | if (writev(logger_pipe, iov, 2) == -1)
|
---|
[fe81e9a] | 277 | syslog(LOG_ERR, "%s: %m", __func__);
|
---|
[55923b7] | 278 | }
|
---|
[a5893e9] | 279 |
|
---|
| 280 | void
|
---|
[9a2a703] | 281 | logger_tick(void)
|
---|
[a5893e9] | 282 | {
|
---|
[3fdebb8] | 283 | static int last_mon = -1, last_mday = -1;
|
---|
[4e66b3a] | 284 | struct timeval tv = { 60, 0 };
|
---|
[3fdebb8] | 285 | char buf[128];
|
---|
[a5893e9] | 286 | struct tm *tm;
|
---|
| 287 | time_t t;
|
---|
[c1888a5] | 288 | int i;
|
---|
[a5893e9] | 289 |
|
---|
[4e66b3a] | 290 | time(&t);
|
---|
[a5893e9] | 291 | tm = gmtime(&t);
|
---|
[c1888a5] | 292 | if (last_mon != tm->tm_mon) {
|
---|
[fe81e9a] | 293 | snprintf(file_ts, sizeof file_ts, "%04d-%02d",
|
---|
| 294 | tm->tm_year + 1900, tm->tm_mon + 1);
|
---|
[c1888a5] | 295 | last_mon = tm->tm_mon;
|
---|
[9a2a703] | 296 | /* rotate log files */
|
---|
[c1888a5] | 297 | for (i = 0; i < nlogfiles; i++) {
|
---|
| 298 | fclose(logfiles[i].fp);
|
---|
| 299 | logfiles[i].fp = NULL;
|
---|
| 300 | }
|
---|
| 301 | nlogfiles = 0;
|
---|
| 302 | }
|
---|
[3fdebb8] | 303 | if (tm->tm_mday != last_mday) {
|
---|
| 304 | strftime(buf, sizeof(buf),
|
---|
| 305 | "Today is %a %b %e %Y %H:%M %Z (%z)", tm);
|
---|
| 306 | for (i = 0; i < nlogfiles; i++)
|
---|
| 307 | fprintf(logfiles[i].fp, "%s\n", buf);
|
---|
| 308 | last_mday = tm->tm_mday;
|
---|
| 309 | }
|
---|
[9a2a703] | 310 | snprintf(line_ts, sizeof line_ts, "%02d:%02d", tm->tm_hour,
|
---|
[a5893e9] | 311 | tm->tm_min);
|
---|
[9a2a703] | 312 | if (evtimer_add(&ev_tick, &tv) < 0) {
|
---|
[fe81e9a] | 313 | syslog(LOG_ERR, "%s: evtimer_add: %m", __func__);
|
---|
[28c4fd3] | 314 | exit(EX_UNAVAILABLE);
|
---|
[9a2a703] | 315 | }
|
---|
[a5893e9] | 316 | }
|
---|