source: code/logger.c@ 65b48ee

Last change on this file since 65b48ee was d27dc1e, checked in by Mike Belopuhov <mike@…>, 10 years ago

fixup signal and MAXPATHLEN include fallout

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