source: code/logger.c@ 1c6061c

Last change on this file since 1c6061c 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
Line 
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/stat.h>
21#include <sys/time.h>
22#include <sys/types.h>
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>
31#include <time.h>
32#include <login_cap.h>
33#include <event.h>
34#include <pwd.h>
35
36#include "icb.h"
37#include "icbd.h"
38
39void logger_ioerr(struct bufferevent *, short, void *);
40void logger_dispatch(struct bufferevent *, void *);
41FILE *logger_open(char *);
42void logger_tick(void);
43
44struct icbd_logentry {
45 char group[ICB_MAXGRPLEN];
46 char nick[ICB_MAXNICKLEN];
47 size_t length;
48};
49
50struct {
51 char group[ICB_MAXGRPLEN];
52 FILE *fp;
53} logfiles[10];
54int nlogfiles;
55
56int logger_pipe;
57
58char file_ts[sizeof "0000-00"];
59char line_ts[sizeof "00:00"];
60struct event ev_tick;
61
62extern char logprefix[MAXPATHLEN/2];
63extern int dologging;
64
65int
66logger_init(void)
67{
68 struct bufferevent *bev;
69 struct passwd *pw;
70 int pipes[2];
71
72 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
73 syslog(LOG_ERR, "%s: socketpair: %m", __func__);
74 exit(EX_OSERR);
75 }
76
77 switch (fork()) {
78 case -1:
79 syslog(LOG_ERR, "%s: fork: %m", __func__);
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) {
94 syslog(LOG_ERR, "%s: No passwd entry for %s", __func__,
95 ICBD_USER);
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
103 if (chroot(pw->pw_dir) < 0) {
104 syslog(LOG_ERR, "%s: %s: %m", __func__, pw->pw_dir);
105 exit(EX_UNAVAILABLE);
106 }
107
108 if (chdir("/") < 0) {
109 syslog(LOG_ERR, "%s: chdir: %m", __func__);
110 exit(EX_UNAVAILABLE);
111 }
112
113 chdir (ICBD_HOME);
114
115 if (setuid(pw->pw_uid) < 0) {
116 syslog(LOG_ERR, "%s: %d: %m", __func__, pw->pw_uid);
117 exit(EX_NOPERM);
118 }
119
120 event_init();
121
122 /* event for message processing */
123 if ((bev = bufferevent_new(pipes[1], logger_dispatch, NULL,
124 logger_ioerr, NULL)) == NULL) {
125 syslog(LOG_ERR, "%s: bufferevent_new: %m", __func__);
126 exit(EX_UNAVAILABLE);
127 }
128 if (bufferevent_enable(bev, EV_READ)) {
129 syslog(LOG_ERR, "%s: bufferevent_enable: %m", __func__);
130 bufferevent_free(bev);
131 exit(EX_UNAVAILABLE);
132 }
133
134 evtimer_set(&ev_tick, (void (*)(int, short, void *))logger_tick, NULL);
135 logger_tick();
136 return event_dispatch();
137}
138
139void
140logger_ioerr(struct bufferevent *bev __attribute__((__unused__)), short what,
141 void *arg __attribute__((__unused__)))
142{
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";
151 syslog(LOG_ERR, "%s: %s", __func__, cause ? cause : "unknown");
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;
161 FILE *fp = NULL;
162 size_t res;
163 char *m;
164 int i;
165
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 }
187 if (e->length >= ICB_MSGSIZE) {
188 syslog(LOG_ERR, "%s: message too big: %lu", __func__,
189 e->length);
190 exit(EX_DATAERR);
191 }
192 /* fetch the message */
193 res = bufferevent_read(bev, &buf[nread],
194 e->length - (nread - sizeof *e));
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
204 if (nread - sizeof *e < e->length)
205 return;
206 /* terminate the buffer */
207 m[MIN(nread - sizeof *e, ICB_MSGSIZE - 1)] = '\0';
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;
220 }
221}
222
223FILE *
224logger_open(char *group)
225{
226 char path[MAXPATHLEN];
227 FILE *fp = NULL;
228
229 /* make sure not to overflow the logfiles table */
230 if (nlogfiles == nitems(logfiles)) {
231 syslog(LOG_NOTICE, "%s: logfiles table is full", __func__);
232 return (NULL);
233 }
234 snprintf(path, sizeof path, "%s/%s", logprefix, group);
235 if (mkdir(path, 0755) < 0 && errno != EEXIST) {
236 syslog(LOG_ERR, "%s: %s: %m", __func__, group);
237 return (NULL);
238 }
239 snprintf(path, sizeof path, "%s/%s/%s", logprefix, group, file_ts);
240 if ((fp = fopen(path, "a")) == NULL) {
241 syslog(LOG_ERR, "%s: %s: %m", __func__, path);
242 return (NULL);
243 }
244 setvbuf(fp, NULL, _IOLBF, 0);
245 if (verbose)
246 syslog(LOG_NOTICE, "%s: %s", __func__, path);
247 strlcpy(logfiles[nlogfiles].group, group, ICB_MAXGRPLEN);
248 logfiles[nlogfiles++].fp = fp;
249 return (fp);
250}
251
252void
253logger(char *group, char *nick, char *what)
254{
255 struct icbd_logentry e;
256 struct iovec iov[2];
257 const char *defgrp = "1";
258
259 if (!dologging)
260 return;
261
262 if (strcmp(group, defgrp) == 0)
263 return;
264
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)
276 syslog(LOG_ERR, "%s: %m", __func__);
277}
278
279void
280logger_tick(void)
281{
282 static int last_mon = -1, last_mday = -1;
283 struct timeval tv = { 60, 0 };
284 char buf[128];
285 struct tm *tm;
286 time_t t;
287 int i;
288
289 time(&t);
290 tm = gmtime(&t);
291 if (last_mon != tm->tm_mon) {
292 snprintf(file_ts, sizeof file_ts, "%04d-%02d",
293 tm->tm_year + 1900, tm->tm_mon + 1);
294 last_mon = tm->tm_mon;
295 /* rotate log files */
296 for (i = 0; i < nlogfiles; i++) {
297 fclose(logfiles[i].fp);
298 logfiles[i].fp = NULL;
299 }
300 nlogfiles = 0;
301 }
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 }
309 snprintf(line_ts, sizeof line_ts, "%02d:%02d", tm->tm_hour,
310 tm->tm_min);
311 if (evtimer_add(&ev_tick, &tv) < 0) {
312 syslog(LOG_ERR, "%s: evtimer_add: %m", __func__);
313 exit(EX_UNAVAILABLE);
314 }
315}
Note: See TracBrowser for help on using the repository browser.