changelog: mention hippotat
[secnet] / log.c
CommitLineData
c215a4bc
IJ
1/*
2 * This file is part of secnet.
3 * See README for full list of copyright holders.
4 *
5 * secnet is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version d of the License, or
8 * (at your option) any later version.
9 *
10 * secnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * version 3 along with secnet; if not, see
17 * https://www.gnu.org/licenses/gpl.html.
18 */
7138d0c5
SE
19#include "secnet.h"
20#include <stdio.h>
21#include <string.h>
22#include <stdlib.h>
23#include <time.h>
24#include <errno.h>
25#include <syslog.h>
4f5e39ec 26#include <assert.h>
fe5e9cc4 27#include <unistd.h>
7138d0c5 28#include "process.h"
08ee90a2 29#include "util.h"
7138d0c5
SE
30
31bool_t secnet_is_daemon=False;
469fd1d9 32uint32_t message_level=M_WARNING|M_ERR|M_SECURITY|M_FATAL;
7138d0c5
SE
33struct log_if *system_log=NULL;
34
ba72e2bf 35static void vMessageFallback(uint32_t class, const char *message, va_list args)
779837e1
IJ
36 FORMAT(printf,2,0);
37static void vMessageFallback(uint32_t class, const char *message, va_list args)
7138d0c5
SE
38{
39 FILE *dest=stdout;
ba72e2bf
IJ
40 /* Messages go to stdout/stderr */
41 if (class & message_level) {
42 if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
43 dest=stderr;
44 }
45 vfprintf(dest,message,args);
46 }
47}
48
49static void vMessage(uint32_t class, const char *message, va_list args)
50{
7138d0c5 51
7b1a9fb7 52 if (system_log) {
7138d0c5 53 /* Messages go to the system log interface */
ff1dcd86 54 vslilog_part(system_log, class, message, args);
7138d0c5 55 } else {
ba72e2bf 56 vMessageFallback(class,message,args);
7138d0c5
SE
57 }
58}
59
fe5e9cc4 60void Message(uint32_t class, const char *message, ...)
7138d0c5
SE
61{
62 va_list ap;
63
64 va_start(ap,message);
65 vMessage(class,message,ap);
66 va_end(ap);
67}
68
ba72e2bf 69static void MessageFallback(uint32_t class, const char *message, ...)
779837e1
IJ
70 FORMAT(printf,2,3);
71static void MessageFallback(uint32_t class, const char *message, ...)
ba72e2bf
IJ
72{
73 va_list ap;
74
75 va_start(ap,message);
76 vMessageFallback(class,message,ap);
77 va_end(ap);
78}
79
fe5e9cc4 80static NORETURN(vfatal(int status, bool_t perror, const char *message,
4f5e39ec
SE
81 va_list args));
82
fe5e9cc4
SE
83static void vfatal(int status, bool_t perror, const char *message,
84 va_list args)
7138d0c5
SE
85{
86 int err;
87
88 err=errno;
89
90 enter_phase(PHASE_SHUTDOWN);
4f5e39ec
SE
91 Message(M_FATAL, "secnet fatal error: ");
92 vMessage(M_FATAL, message, args);
93 if (perror)
7138d0c5 94 Message(M_FATAL, ": %s\n",strerror(err));
4f5e39ec
SE
95 else
96 Message(M_FATAL, "\n");
7138d0c5
SE
97 exit(status);
98}
99
fe5e9cc4 100void fatal(const char *message, ...)
7138d0c5
SE
101{
102 va_list args;
103 va_start(args,message);
104 vfatal(current_phase,False,message,args);
105 va_end(args);
106}
107
fe5e9cc4 108void fatal_status(int status, const char *message, ...)
7138d0c5
SE
109{
110 va_list args;
111 va_start(args,message);
112 vfatal(status,False,message,args);
113 va_end(args);
114}
115
fe5e9cc4 116void fatal_perror(const char *message, ...)
7138d0c5
SE
117{
118 va_list args;
119 va_start(args,message);
120 vfatal(current_phase,True,message,args);
121 va_end(args);
122}
123
fe5e9cc4 124void fatal_perror_status(int status, const char *message, ...)
7138d0c5
SE
125{
126 va_list args;
127 va_start(args,message);
128 vfatal(status,True,message,args);
129 va_end(args);
130}
131
4f5e39ec 132void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
fe5e9cc4 133 cstring_t facility, const char *message, va_list args)
7138d0c5 134{
7138d0c5
SE
135 enter_phase(PHASE_SHUTDOWN);
136
4f5e39ec
SE
137 if (maybe_f && ferror(maybe_f)) {
138 assert(loc.file);
139 Message(M_FATAL, "error reading config file (%s, %s): %s",
140 facility, loc.file, strerror(errno));
141 } else if (maybe_f && feof(maybe_f)) {
142 assert(loc.file);
143 Message(M_FATAL, "unexpected end of config file (%s, %s)",
144 facility, loc.file);
145 } else if (loc.file && loc.line) {
7138d0c5
SE
146 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
147 loc.line);
148 } else if (!loc.file && loc.line) {
149 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
150 } else {
151 Message(M_FATAL, "config error (%s): ",facility);
152 }
153
154 vMessage(M_FATAL,message,args);
7138d0c5
SE
155 exit(current_phase);
156}
157
fe5e9cc4
SE
158void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
159 const char *message, ...)
4f5e39ec
SE
160{
161 va_list args;
162
163 va_start(args,message);
164 vcfgfatal_maybefile(maybe_f,loc,facility,message,args);
165 va_end(args);
166}
167
fe5e9cc4 168void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
4f5e39ec
SE
169{
170 va_list args;
171
172 va_start(args,message);
173 vcfgfatal_maybefile(0,loc,facility,message,args);
174 va_end(args);
175}
176
177void cfgfile_postreadcheck(struct cloc loc, FILE *f)
178{
179 assert(loc.file);
180 if (ferror(f)) {
39a6b1e2 181 Message(M_FATAL, "error reading config file (%s): %s\n",
4f5e39ec
SE
182 loc.file, strerror(errno));
183 exit(current_phase);
184 } else if (feof(f)) {
39a6b1e2 185 Message(M_FATAL, "unexpected end of config file (%s)\n", loc.file);
4f5e39ec
SE
186 exit(current_phase);
187 }
188}
189
7138d0c5
SE
190/* Take a list of log closures and merge them */
191struct loglist {
192 struct log_if *l;
193 struct loglist *next;
194};
195
fe5e9cc4 196static void log_vmulti(void *sst, int class, const char *message, va_list args)
7138d0c5
SE
197{
198 struct loglist *st=sst, *i;
199
200 if (secnet_is_daemon) {
201 for (i=st; i; i=i->next) {
779837e1 202 vslilog(i->l,class,message,args);
7138d0c5
SE
203 }
204 } else {
205 vMessage(class,message,args);
206 Message(class,"\n");
207 }
208}
209
f1393100
IJ
210void lg_vperror(struct log_if *lg, const char *desc, struct cloc *loc,
211 int class, int errnoval, const char *fmt, va_list al)
212{
213 int status=current_phase;
214 int esave=errno;
215
216 if (!lg)
217 lg=system_log;
218
219 if (class & M_FATAL)
220 enter_phase(PHASE_SHUTDOWN);
221
222 slilog_part(lg,class,"%s",desc);
223 if (loc)
224 slilog_part(lg,class," (%s:%d)",loc->file,loc->line);
225 slilog_part(lg,class,": ");
226 vslilog_part(lg,class,fmt,al);
227 if (errnoval)
228 slilog_part(lg,class,": %s",strerror(errnoval));
229 slilog_part(lg,class,"\n");
230
231 if (class & M_FATAL)
232 exit(status);
233
234 errno=esave;
235}
236
237void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc,
238 int class, int errnoval, const char *fmt, ...)
239{
240 va_list al;
241 va_start(al,fmt);
242 lg_vperror(lg,desc,loc,class,errnoval,fmt,al);
243 va_end(al);
244}
245
45736f73
IJ
246void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc,
247 int class, int status, const char *progname)
248{
249 if (!status)
250 lg_perror(lg,desc,loc,class,0,"%s exited",progname);
251 else if (WIFEXITED(status))
252 lg_perror(lg,desc,loc,class,0,"%s exited with error exit status %d",
253 progname,WEXITSTATUS(status));
254 else if (WIFSIGNALED(status))
255 lg_perror(lg,desc,loc,class,0,"%s died due to fatal signal %s (%d)%s",
256 progname,strsignal(WTERMSIG(status)),WTERMSIG(status),
257 WCOREDUMP(status)?" (core dumped)":"");
258 else
259 lg_perror(lg,desc,loc,class,0,"%s died with unknown wait status %d",
260 progname,status);
261}
262
7138d0c5
SE
263struct log_if *init_log(list_t *ll)
264{
265 int i=0;
266 item_t *item;
267 closure_t *cl;
268 struct loglist *l=NULL, *n;
269 struct log_if *r;
270
271 if (list_length(ll)==1) {
272 item=list_elem(ll,0);
273 cl=item->data.closure;
274 if (cl->type!=CL_LOG) {
275 cfgfatal(item->loc,"init_log","closure is not a logger");
276 }
277 return cl->interface;
278 }
279 while ((item=list_elem(ll,i++))) {
280 if (item->type!=t_closure) {
281 cfgfatal(item->loc,"init_log","item is not a closure");
282 }
283 cl=item->data.closure;
284 if (cl->type!=CL_LOG) {
285 cfgfatal(item->loc,"init_log","closure is not a logger");
286 }
b7886fd4 287 NEW(n);
7138d0c5
SE
288 n->l=cl->interface;
289 n->next=l;
290 l=n;
291 }
292 if (!l) {
293 fatal("init_log: no log");
294 }
952f601f 295 NEW(r);
7138d0c5 296 r->st=l;
779837e1 297 r->vlogfn=log_vmulti;
ff1dcd86 298 r->buff[0]=0;
7138d0c5
SE
299 return r;
300}
301
302struct logfile {
303 closure_t cl;
304 struct log_if ops;
305 struct cloc loc;
306 string_t logfile;
307 uint32_t level;
308 FILE *f;
b85fe3f9 309 bool_t forked;
7138d0c5
SE
310};
311
fe5e9cc4 312static cstring_t months[]={
7138d0c5
SE
313 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
314
fe5e9cc4
SE
315static void logfile_vlog(void *sst, int class, const char *message,
316 va_list args)
7138d0c5
SE
317{
318 struct logfile *st=sst;
319 time_t t;
320 struct tm *tm;
b85fe3f9
IJ
321 char pidbuf[20];
322
323 if (st->forked) {
324 pid_t us=getpid();
325 snprintf(pidbuf,sizeof(pidbuf),"[%ld] ",(long)us);
326 } else {
327 pidbuf[0]=0;
328 }
7138d0c5 329
fe5e9cc4 330 if (secnet_is_daemon && st->f) {
7138d0c5
SE
331 if (class&st->level) {
332 t=time(NULL);
333 tm=localtime(&t);
b85fe3f9 334 fprintf(st->f,"%s %2d %02d:%02d:%02d %s",
7138d0c5 335 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
b85fe3f9
IJ
336 tm->tm_sec,
337 pidbuf);
7138d0c5
SE
338 vfprintf(st->f,message,args);
339 fprintf(st->f,"\n");
340 fflush(st->f);
341 }
342 } else {
b85fe3f9 343 if (pidbuf[0]) MessageFallback(class,"%s",pidbuf);
ba72e2bf
IJ
344 vMessageFallback(class,message,args);
345 MessageFallback(class,"\n");
7138d0c5
SE
346 }
347}
348
fe5e9cc4 349static void logfile_log(void *state, int class, const char *message, ...)
779837e1
IJ
350 FORMAT(printf,3,4);
351static void logfile_log(void *state, int class, const char *message, ...)
7138d0c5
SE
352{
353 va_list ap;
354
355 va_start(ap,message);
fe5e9cc4 356 logfile_vlog(state,class,message,ap);
7138d0c5
SE
357 va_end(ap);
358}
359
360static void logfile_hup_notify(void *sst, int signum)
361{
362 struct logfile *st=sst;
363 FILE *f;
364 f=fopen(st->logfile,"a");
365 if (!f) {
366 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
367 "logfile: %s",strerror(errno));
368 } else {
369 fclose(st->f);
370 st->f=f;
371 logfile_log(st,M_INFO,"received SIGHUP");
372 }
373}
374
375static void logfile_phase_hook(void *sst, uint32_t new_phase)
376{
377 struct logfile *st=sst;
378 FILE *f;
379
380 if (background) {
381 f=fopen(st->logfile,"a");
382 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
383 st->loc.file,st->loc.line,st->logfile);
384 st->f=f;
385 request_signal_notification(SIGHUP, logfile_hup_notify,st);
386 }
387}
388
b85fe3f9
IJ
389static void logfile_childpersist_hook(void *sst, uint32_t new_phase)
390{
391 struct logfile *st=sst;
392 st->forked=1;
393}
394
7138d0c5
SE
395static struct flagstr message_class_table[]={
396 { "debug-config", M_DEBUG_CONFIG },
397 { "debug-phase", M_DEBUG_PHASE },
398 { "debug", M_DEBUG },
399 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
400 { "info", M_INFO },
401 { "notice", M_NOTICE },
402 { "warning", M_WARNING },
469fd1d9 403 { "error", M_ERR },
7138d0c5
SE
404 { "security", M_SECURITY },
405 { "fatal", M_FATAL },
469fd1d9
SE
406 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
407 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
7138d0c5
SE
408 { "quiet", M_FATAL },
409 { NULL, 0 }
410};
411
412static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
413 list_t *args)
414{
415 struct logfile *st;
416 item_t *item;
417 dict_t *dict;
418
419 /* We should defer opening the logfile until the getresources
420 phase. We should defer writing into the logfile until after we
421 become a daemon. */
422
b7886fd4 423 NEW(st);
7138d0c5
SE
424 st->cl.description="logfile";
425 st->cl.type=CL_LOG;
426 st->cl.apply=NULL;
427 st->cl.interface=&st->ops;
428 st->ops.st=st;
779837e1 429 st->ops.vlogfn=logfile_vlog;
ff1dcd86 430 st->ops.buff[0]=0;
7138d0c5 431 st->loc=loc;
fe5e9cc4 432 st->f=NULL;
b85fe3f9 433 st->forked=0;
7138d0c5
SE
434
435 item=list_elem(args,0);
436 if (!item || item->type!=t_dict) {
437 cfgfatal(loc,"logfile","argument must be a dictionary\n");
438 }
439 dict=item->data.dict;
440
441 st->logfile=dict_read_string(dict,"filename",True,"logfile",loc);
442 st->level=string_list_to_word(dict_lookup(dict,"class"),
443 message_class_table,"logfile");
444
445 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
b85fe3f9 446 add_hook(PHASE_CHILDPERSIST,logfile_childpersist_hook,st);
7138d0c5
SE
447
448 return new_closure(&st->cl);
449}
450
451struct syslog {
452 closure_t cl;
453 struct log_if ops;
454 string_t ident;
455 int facility;
456 bool_t open;
457};
458
459static int msgclass_to_syslogpriority(uint32_t m)
460{
461 switch (m) {
462 case M_DEBUG_CONFIG: return LOG_DEBUG;
463 case M_DEBUG_PHASE: return LOG_DEBUG;
464 case M_DEBUG: return LOG_DEBUG;
465 case M_INFO: return LOG_INFO;
466 case M_NOTICE: return LOG_NOTICE;
467 case M_WARNING: return LOG_WARNING;
469fd1d9 468 case M_ERR: return LOG_ERR;
7138d0c5
SE
469 case M_SECURITY: return LOG_CRIT;
470 case M_FATAL: return LOG_EMERG;
471 default: return LOG_NOTICE;
472 }
473}
474
fe5e9cc4 475static void syslog_vlog(void *sst, int class, const char *message,
7138d0c5 476 va_list args)
779837e1
IJ
477 FORMAT(printf,3,0);
478static void syslog_vlog(void *sst, int class, const char *message,
479 va_list args)
7138d0c5
SE
480{
481 struct syslog *st=sst;
482
483 if (st->open)
484 vsyslog(msgclass_to_syslogpriority(class),message,args);
485 else {
ba72e2bf
IJ
486 vMessageFallback(class,message,args);
487 MessageFallback(class,"\n");
7138d0c5
SE
488 }
489}
490
7138d0c5 491static struct flagstr syslog_facility_table[]={
8dea8d37
SE
492#ifdef LOG_AUTH
493 { "auth", LOG_AUTH },
494#endif
495#ifdef LOG_AUTHPRIV
7138d0c5 496 { "authpriv", LOG_AUTHPRIV },
8dea8d37 497#endif
7138d0c5
SE
498 { "cron", LOG_CRON },
499 { "daemon", LOG_DAEMON },
500 { "kern", LOG_KERN },
501 { "local0", LOG_LOCAL0 },
502 { "local1", LOG_LOCAL1 },
503 { "local2", LOG_LOCAL2 },
504 { "local3", LOG_LOCAL3 },
505 { "local4", LOG_LOCAL4 },
506 { "local5", LOG_LOCAL5 },
507 { "local6", LOG_LOCAL6 },
508 { "local7", LOG_LOCAL7 },
509 { "lpr", LOG_LPR },
510 { "mail", LOG_MAIL },
511 { "news", LOG_NEWS },
512 { "syslog", LOG_SYSLOG },
513 { "user", LOG_USER },
514 { "uucp", LOG_UUCP },
515 { NULL, 0 }
516};
517
518static void syslog_phase_hook(void *sst, uint32_t newphase)
519{
520 struct syslog *st=sst;
521
522 if (background) {
b85fe3f9
IJ
523 openlog(st->ident,
524 newphase==PHASE_CHILDPERSIST ? LOG_PID : 0,
525 st->facility);
7138d0c5
SE
526 st->open=True;
527 }
528}
529
530static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
531 list_t *args)
532{
533 struct syslog *st;
534 dict_t *d;
535 item_t *item;
536 string_t facstr;
537
b7886fd4 538 NEW(st);
7138d0c5
SE
539 st->cl.description="syslog";
540 st->cl.type=CL_LOG;
541 st->cl.apply=NULL;
542 st->cl.interface=&st->ops;
543 st->ops.st=st;
779837e1 544 st->ops.vlogfn=syslog_vlog;
ff1dcd86 545 st->ops.buff[0]=0;
7138d0c5
SE
546
547 item=list_elem(args,0);
548 if (!item || item->type!=t_dict)
549 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
550 d=item->data.dict;
551
552 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
553 facstr=dict_read_string(d, "facility", True, "syslog", loc);
554 st->facility=string_to_word(facstr,loc,
555 syslog_facility_table,"syslog");
556 st->open=False;
557 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
b85fe3f9 558 add_hook(PHASE_CHILDPERSIST,syslog_phase_hook,st);
7138d0c5
SE
559
560 return new_closure(&st->cl);
561}
562
fe5e9cc4
SE
563/* Read from a fd and output to a log. This is a quick hack to
564 support logging stderr, and needs code adding to tidy up before it
565 can be used for anything else. */
566#define FDLOG_BUFSIZE 1024
567struct fdlog {
568 struct log_if *log;
569 int fd;
570 cstring_t prefix;
571 string_t buffer;
572 int i;
573 bool_t finished;
574};
575
576static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
90a39563 577 int *timeout_io)
fe5e9cc4
SE
578{
579 struct fdlog *st=sst;
580 if (!st->finished) {
ee697dd9 581 BEFOREPOLL_WANT_FDS(1);
fe5e9cc4
SE
582 fds[0].fd=st->fd;
583 fds[0].events=POLLIN;
ee697dd9
IJ
584 } else {
585 BEFOREPOLL_WANT_FDS(0);
fe5e9cc4
SE
586 }
587 return 0;
588}
589
90a39563 590static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
fe5e9cc4
SE
591{
592 struct fdlog *st=sst;
593 int r,remain,i;
594
595 if (nfds==0) return;
596 if (fds[0].revents&POLLERR) {
597 st->finished=True;
598 }
599 if (fds[0].revents&POLLIN) {
600 remain=FDLOG_BUFSIZE-st->i-1;
601 if (remain<=0) {
602 st->buffer[FDLOG_BUFSIZE-1]=0;
779837e1 603 slilog(st->log,M_WARNING,"%s: overlong line: %s",
fe5e9cc4
SE
604 st->prefix,st->buffer);
605 st->i=0;
606 remain=FDLOG_BUFSIZE-1;
607 }
608 r=read(st->fd,st->buffer+st->i,remain);
609 if (r>0) {
610 st->i+=r;
611 for (i=0; i<st->i; i++) {
612 if (st->buffer[i]=='\n') {
613 st->buffer[i]=0;
779837e1 614 slilog(st->log,M_INFO,"%s: %s",
fe5e9cc4
SE
615 st->prefix,st->buffer);
616 i++;
617 memmove(st->buffer,st->buffer+i,st->i-i);
618 st->i-=i;
619 i=-1;
620 }
621 }
ba703386 622 } else if (errno==EINTR || iswouldblock(errno)) {
fe5e9cc4
SE
623 } else {
624 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
625 st->finished=True;
626 }
627 }
628}
629
630void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
631{
632 struct fdlog *st;
633
b7886fd4 634 NEW(st);
fe5e9cc4
SE
635 st->log=log;
636 st->fd=fd;
637 st->prefix=prefix;
638 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
639 st->i=0;
640 st->finished=False;
641
ba703386
IJ
642 setnonblock(st->fd);
643
32fc582f 644 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,
fe5e9cc4
SE
645 prefix);
646}
647
7138d0c5
SE
648void log_module(dict_t *dict)
649{
650 add_closure(dict,"logfile",logfile_apply);
651 add_closure(dict,"syslog",syslog_apply);
652}