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