source: code/logger.c@ 36f8e7c

Last change on this file since 36f8e7c was 411aa63, checked in by Tim Kuijsten <tim@…>, 9 years ago

pledge icbd and it's logger process

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