[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 <errno.h>
|
---|
| 23 | #include <stdlib.h>
|
---|
| 24 | #include <string.h>
|
---|
| 25 | #include <stdio.h>
|
---|
| 26 | #include <unistd.h>
|
---|
| 27 | #include <syslog.h>
|
---|
| 28 | #include <sysexits.h>
|
---|
| 29 | #include <login_cap.h>
|
---|
| 30 | #include <event.h>
|
---|
| 31 | #include <pwd.h>
|
---|
| 32 |
|
---|
| 33 | #include "icb.h"
|
---|
| 34 | #include "icbd.h"
|
---|
| 35 |
|
---|
| 36 | void logger_dispatch(int, short, void *);
|
---|
| 37 |
|
---|
| 38 | int logger_pipe;
|
---|
| 39 |
|
---|
| 40 | struct icbd_logentry {
|
---|
| 41 | char group[ICB_MAXGRPLEN];
|
---|
| 42 | char nick[ICB_MAXNICKLEN];
|
---|
| 43 | size_t length;
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | int
|
---|
| 47 | logger_init(void)
|
---|
| 48 | {
|
---|
| 49 | static struct event ev;
|
---|
| 50 | struct passwd *pw;
|
---|
| 51 | int pipes[2];
|
---|
| 52 |
|
---|
| 53 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
|
---|
| 54 | syslog(LOG_ERR, "socketpair: %m");
|
---|
| 55 | exit(EX_OSERR);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | switch (fork()) {
|
---|
| 59 | case -1:
|
---|
| 60 | syslog(LOG_ERR, "fork: %m");
|
---|
| 61 | exit(EX_OSERR);
|
---|
| 62 | case 0:
|
---|
| 63 | break;
|
---|
| 64 |
|
---|
| 65 | default:
|
---|
| 66 | close(pipes[1]);
|
---|
| 67 | logger_pipe = pipes[0];
|
---|
| 68 | return (0);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | setproctitle("logger");
|
---|
| 72 | close(pipes[0]);
|
---|
| 73 |
|
---|
| 74 | if ((pw = getpwnam(ICBD_USER)) == NULL) {
|
---|
| 75 | syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
|
---|
| 76 | exit(EX_NOUSER);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | if (setusercontext(NULL, pw, pw->pw_uid,
|
---|
| 80 | LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
|
---|
| 81 | exit(EX_NOPERM);
|
---|
| 82 |
|
---|
| 83 | if (setuid(pw->pw_uid) < 0) {
|
---|
| 84 | syslog(LOG_ERR, "%d: %m", pw->pw_uid);
|
---|
| 85 | exit(EX_NOPERM);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | if (chdir(pw->pw_dir) < 0) {
|
---|
| 89 | syslog(LOG_ERR, "chdir: %m");
|
---|
| 90 | exit(EX_UNAVAILABLE);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | event_init();
|
---|
| 94 |
|
---|
| 95 | /* event for message processing */
|
---|
| 96 | event_set(&ev, pipes[1], EV_READ | EV_PERSIST, logger_dispatch, NULL);
|
---|
| 97 | if (event_add(&ev, NULL) < 0) {
|
---|
| 98 | syslog(LOG_ERR, "event_add: %m");
|
---|
| 99 | exit (EX_UNAVAILABLE);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | return event_dispatch();
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | void
|
---|
| 106 | logger_dispatch(int fd, short event, void *arg __attribute__((unused)))
|
---|
| 107 | {
|
---|
| 108 | char buf[512];
|
---|
| 109 | struct icbd_logentry e;
|
---|
| 110 | struct iovec iov[2];
|
---|
| 111 |
|
---|
| 112 | if (event != EV_READ)
|
---|
| 113 | return;
|
---|
| 114 |
|
---|
| 115 | bzero(&e, sizeof e);
|
---|
| 116 | iov[0].iov_base = &e;
|
---|
| 117 | iov[0].iov_len = sizeof e;
|
---|
| 118 |
|
---|
| 119 | iov[1].iov_base = buf;
|
---|
| 120 | iov[1].iov_len = sizeof buf;
|
---|
| 121 |
|
---|
| 122 | if (readv(fd, iov, 2) < (ssize_t)sizeof e) {
|
---|
| 123 | syslog(LOG_ERR, "logger read: %m");
|
---|
| 124 | exit(EX_DATAERR);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /* XXX */
|
---|
| 128 | if (iov[1].iov_len < e.length) {
|
---|
| 129 | syslog(LOG_ERR, "logger read %lu out of %lu",
|
---|
| 130 | iov[1].iov_len, e.length);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /* TODO: check time of the day and open the next file */
|
---|
| 134 |
|
---|
| 135 | fprintf(stderr, "%s@%s: %s\n", e.nick, e.group, buf);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | void
|
---|
[45bd56a] | 139 | logger(char *group, char *nick, char *what)
|
---|
[55923b7] | 140 | {
|
---|
| 141 | struct icbd_logentry e;
|
---|
| 142 | struct iovec iov[2];
|
---|
| 143 |
|
---|
| 144 | strlcpy(e.group, group, ICB_MAXGRPLEN);
|
---|
| 145 | strlcpy(e.nick, nick, ICB_MAXNICKLEN);
|
---|
| 146 | e.length = strlen(what) + 1;
|
---|
| 147 |
|
---|
| 148 | iov[0].iov_base = &e;
|
---|
| 149 | iov[0].iov_len = sizeof e;
|
---|
| 150 |
|
---|
| 151 | iov[1].iov_base = what;
|
---|
| 152 | iov[1].iov_len = e.length;
|
---|
| 153 |
|
---|
| 154 | if (writev(logger_pipe, iov, 2) == -1)
|
---|
| 155 | syslog(LOG_ERR, "logger write: %m");
|
---|
| 156 | }
|
---|