source: code/logger.c@ 6f076e7

Last change on this file since 6f076e7 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
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
[0519a87]18#include <sys/types.h>
[55923b7]19#include <sys/socket.h>
[4e66b3a]20#include <sys/stat.h>
[55923b7]21#include <sys/time.h>
22#include <sys/uio.h>
23#include <errno.h>
[d27dc1e]24#include <limits.h>
[0519a87]25#include <netdb.h>
[55923b7]26#include <stdlib.h>
27#include <string.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <syslog.h>
31#include <sysexits.h>
[a5893e9]32#include <time.h>
[55923b7]33#include <login_cap.h>
34#include <event.h>
35#include <pwd.h>
36
37#include "icb.h"
38#include "icbd.h"
39
[be3ad87]40void logger_ioerr(struct bufferevent *, short, void *);
41void logger_dispatch(struct bufferevent *, void *);
[4e66b3a]42FILE *logger_open(char *);
[9a2a703]43void logger_tick(void);
[55923b7]44
45struct icbd_logentry {
46 char group[ICB_MAXGRPLEN];
47 char nick[ICB_MAXNICKLEN];
48 size_t length;
49};
50
[4e66b3a]51struct {
52 char group[ICB_MAXGRPLEN];
53 FILE *fp;
54} logfiles[10];
55int nlogfiles;
[a5893e9]56
[4e66b3a]57int logger_pipe;
58
59char file_ts[sizeof "0000-00"];
[9a2a703]60char line_ts[sizeof "00:00"];
[a5893e9]61struct event ev_tick;
62
[d27dc1e]63extern char logprefix[PATH_MAX/2];
[3dba97d]64extern int dologging;
65
[55923b7]66int
67logger_init(void)
68{
[be3ad87]69 struct bufferevent *bev;
[55923b7]70 struct passwd *pw;
71 int pipes[2];
72
73 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
[fe81e9a]74 syslog(LOG_ERR, "%s: socketpair: %m", __func__);
[55923b7]75 exit(EX_OSERR);
76 }
77
78 switch (fork()) {
79 case -1:
[fe81e9a]80 syslog(LOG_ERR, "%s: fork: %m", __func__);
[55923b7]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) {
[fe81e9a]95 syslog(LOG_ERR, "%s: No passwd entry for %s", __func__,
96 ICBD_USER);
[55923b7]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
[1dd3554]104 if (chroot(pw->pw_dir) < 0) {
[fe81e9a]105 syslog(LOG_ERR, "%s: %s: %m", __func__, pw->pw_dir);
[1dd3554]106 exit(EX_UNAVAILABLE);
107 }
108
109 if (chdir("/") < 0) {
[fe81e9a]110 syslog(LOG_ERR, "%s: chdir: %m", __func__);
[55923b7]111 exit(EX_UNAVAILABLE);
112 }
113
[cdd2ff5]114 chdir (ICBD_HOME);
115
[e54f151]116 if (setuid(pw->pw_uid) < 0) {
[fe81e9a]117 syslog(LOG_ERR, "%s: %d: %m", __func__, pw->pw_uid);
[e54f151]118 exit(EX_NOPERM);
119 }
120
[411aa63]121 if (pledge("stdio cpath wpath", NULL) == -1) {
122 syslog(LOG_ERR, "%s: pledge", __func__);
123 exit(EX_NOPERM);
124 }
125
[55923b7]126 event_init();
127
128 /* event for message processing */
[be3ad87]129 if ((bev = bufferevent_new(pipes[1], logger_dispatch, NULL,
130 logger_ioerr, NULL)) == NULL) {
[fe81e9a]131 syslog(LOG_ERR, "%s: bufferevent_new: %m", __func__);
[be3ad87]132 exit(EX_UNAVAILABLE);
133 }
134 if (bufferevent_enable(bev, EV_READ)) {
[fe81e9a]135 syslog(LOG_ERR, "%s: bufferevent_enable: %m", __func__);
[be3ad87]136 bufferevent_free(bev);
137 exit(EX_UNAVAILABLE);
[55923b7]138 }
139
[9a2a703]140 evtimer_set(&ev_tick, (void (*)(int, short, void *))logger_tick, NULL);
141 logger_tick();
[55923b7]142 return event_dispatch();
143}
144
145void
[be3ad87]146logger_ioerr(struct bufferevent *bev __attribute__((__unused__)), short what,
147 void *arg __attribute__((__unused__)))
[55923b7]148{
[be3ad87]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";
[fe81e9a]157 syslog(LOG_ERR, "%s: %s", __func__, cause ? cause : "unknown");
[be3ad87]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;
[4e66b3a]167 FILE *fp = NULL;
[be3ad87]168 size_t res;
169 char *m;
[4e66b3a]170 int i;
[55923b7]171
[be3ad87]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 }
[28c4fd3]193 if (e->length >= ICB_MSGSIZE) {
194 syslog(LOG_ERR, "%s: message too big: %lu", __func__,
195 e->length);
196 exit(EX_DATAERR);
197 }
[be3ad87]198 /* fetch the message */
199 res = bufferevent_read(bev, &buf[nread],
[28c4fd3]200 e->length - (nread - sizeof *e));
[be3ad87]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
[28c4fd3]210 if (nread - sizeof *e < e->length)
[be3ad87]211 return;
212 /* terminate the buffer */
[a2fadb4]213 m[MIN(nread - sizeof *e, ICB_MSGSIZE - 1)] = '\0';
[be3ad87]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;
[55923b7]226 }
[4e66b3a]227}
228
229FILE *
230logger_open(char *group)
231{
[d27dc1e]232 char path[PATH_MAX];
[4e66b3a]233 FILE *fp = NULL;
[55923b7]234
[058b664]235 /* make sure not to overflow the logfiles table */
[fe81e9a]236 if (nlogfiles == nitems(logfiles)) {
237 syslog(LOG_NOTICE, "%s: logfiles table is full", __func__);
[058b664]238 return (NULL);
[fe81e9a]239 }
[3dba97d]240 snprintf(path, sizeof path, "%s/%s", logprefix, group);
241 if (mkdir(path, 0755) < 0 && errno != EEXIST) {
[fe81e9a]242 syslog(LOG_ERR, "%s: %s: %m", __func__, group);
[4e66b3a]243 return (NULL);
244 }
[3dba97d]245 snprintf(path, sizeof path, "%s/%s/%s", logprefix, group, file_ts);
[4e66b3a]246 if ((fp = fopen(path, "a")) == NULL) {
[fe81e9a]247 syslog(LOG_ERR, "%s: %s: %m", __func__, path);
[4e66b3a]248 return (NULL);
249 }
250 setvbuf(fp, NULL, _IOLBF, 0);
251 if (verbose)
[fe81e9a]252 syslog(LOG_NOTICE, "%s: %s", __func__, path);
[4e66b3a]253 strlcpy(logfiles[nlogfiles].group, group, ICB_MAXGRPLEN);
254 logfiles[nlogfiles++].fp = fp;
255 return (fp);
[55923b7]256}
257
258void
[45bd56a]259logger(char *group, char *nick, char *what)
[55923b7]260{
261 struct icbd_logentry e;
262 struct iovec iov[2];
[d06af04]263 const char *defgrp = "1";
[55923b7]264
[3dba97d]265 if (!dologging)
266 return;
267
[d06af04]268 if (strcmp(group, defgrp) == 0)
269 return;
270
[55923b7]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)
[fe81e9a]282 syslog(LOG_ERR, "%s: %m", __func__);
[55923b7]283}
[a5893e9]284
285void
[9a2a703]286logger_tick(void)
[a5893e9]287{
[3fdebb8]288 static int last_mon = -1, last_mday = -1;
[4e66b3a]289 struct timeval tv = { 60, 0 };
[3fdebb8]290 char buf[128];
[a5893e9]291 struct tm *tm;
292 time_t t;
[c1888a5]293 int i;
[a5893e9]294
[4e66b3a]295 time(&t);
[a5893e9]296 tm = gmtime(&t);
[c1888a5]297 if (last_mon != tm->tm_mon) {
[fe81e9a]298 snprintf(file_ts, sizeof file_ts, "%04d-%02d",
299 tm->tm_year + 1900, tm->tm_mon + 1);
[c1888a5]300 last_mon = tm->tm_mon;
[9a2a703]301 /* rotate log files */
[c1888a5]302 for (i = 0; i < nlogfiles; i++) {
303 fclose(logfiles[i].fp);
304 logfiles[i].fp = NULL;
305 }
306 nlogfiles = 0;
307 }
[3fdebb8]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 }
[9a2a703]315 snprintf(line_ts, sizeof line_ts, "%02d:%02d", tm->tm_hour,
[a5893e9]316 tm->tm_min);
[9a2a703]317 if (evtimer_add(&ev_tick, &tv) < 0) {
[fe81e9a]318 syslog(LOG_ERR, "%s: evtimer_add: %m", __func__);
[28c4fd3]319 exit(EX_UNAVAILABLE);
[9a2a703]320 }
[a5893e9]321}
Note: See TracBrowser for help on using the repository browser.