source: code/logger.c@ 96a2e31

Last change on this file since 96a2e31 was cdd2ff5, checked in by Florian Obser <florian@…>, 11 years ago

Opportunisticly chdir to "core" (should be writeable by _icbd) after
chroot to get working core dumps.

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