Makefile.in: Drop dist target
[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
9c6a8729 7 * the Free Software Foundation; either version 3 of the License, or
c215a4bc
IJ
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
5ffc82e7 35FORMAT(printf,2,0)
779837e1 36static void vMessageFallback(uint32_t class, const char *message, va_list args)
7138d0c5
SE
37{
38 FILE *dest=stdout;
ba72e2bf
IJ
39 /* Messages go to stdout/stderr */
40 if (class & message_level) {
41 if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
42 dest=stderr;
43 }
44 vfprintf(dest,message,args);
45 }
46}
47
5ffc82e7 48FORMAT(printf,2,0)
ba72e2bf
IJ
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
5ffc82e7 69FORMAT(printf,2,3)
779837e1 70static void MessageFallback(uint32_t class, const char *message, ...)
ba72e2bf
IJ
71{
72 va_list ap;
73
74 va_start(ap,message);
75 vMessageFallback(class,message,ap);
76 va_end(ap);
77}
78
fe5e9cc4 79static NORETURN(vfatal(int status, bool_t perror, const char *message,
4f5e39ec
SE
80 va_list args));
81
5ffc82e7 82FORMAT(printf,3,0)
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
5ffc82e7 196FORMAT(printf, 3, 0)
fe5e9cc4 197static void log_vmulti(void *sst, int class, const char *message, va_list args)
7138d0c5
SE
198{
199 struct loglist *st=sst, *i;
200
201 if (secnet_is_daemon) {
202 for (i=st; i; i=i->next) {
779837e1 203 vslilog(i->l,class,message,args);
7138d0c5
SE
204 }
205 } else {
206 vMessage(class,message,args);
207 Message(class,"\n");
208 }
209}
210
5ffc82e7 211FORMAT(printf, 6, 0)
f1393100
IJ
212void lg_vperror(struct log_if *lg, const char *desc, struct cloc *loc,
213 int class, int errnoval, const char *fmt, va_list al)
214{
215 int status=current_phase;
216 int esave=errno;
217
218 if (!lg)
219 lg=system_log;
220
221 if (class & M_FATAL)
222 enter_phase(PHASE_SHUTDOWN);
223
224 slilog_part(lg,class,"%s",desc);
225 if (loc)
226 slilog_part(lg,class," (%s:%d)",loc->file,loc->line);
227 slilog_part(lg,class,": ");
228 vslilog_part(lg,class,fmt,al);
229 if (errnoval)
230 slilog_part(lg,class,": %s",strerror(errnoval));
231 slilog_part(lg,class,"\n");
232
233 if (class & M_FATAL)
234 exit(status);
235
236 errno=esave;
237}
238
239void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc,
240 int class, int errnoval, const char *fmt, ...)
241{
242 va_list al;
243 va_start(al,fmt);
244 lg_vperror(lg,desc,loc,class,errnoval,fmt,al);
245 va_end(al);
246}
247
45736f73
IJ
248void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc,
249 int class, int status, const char *progname)
250{
251 if (!status)
252 lg_perror(lg,desc,loc,class,0,"%s exited",progname);
253 else if (WIFEXITED(status))
254 lg_perror(lg,desc,loc,class,0,"%s exited with error exit status %d",
255 progname,WEXITSTATUS(status));
256 else if (WIFSIGNALED(status))
257 lg_perror(lg,desc,loc,class,0,"%s died due to fatal signal %s (%d)%s",
258 progname,strsignal(WTERMSIG(status)),WTERMSIG(status),
259 WCOREDUMP(status)?" (core dumped)":"");
260 else
261 lg_perror(lg,desc,loc,class,0,"%s died with unknown wait status %d",
262 progname,status);
263}
264
7138d0c5
SE
265struct log_if *init_log(list_t *ll)
266{
267 int i=0;
268 item_t *item;
269 closure_t *cl;
270 struct loglist *l=NULL, *n;
271 struct log_if *r;
272
273 if (list_length(ll)==1) {
274 item=list_elem(ll,0);
275 cl=item->data.closure;
276 if (cl->type!=CL_LOG) {
277 cfgfatal(item->loc,"init_log","closure is not a logger");
278 }
279 return cl->interface;
280 }
281 while ((item=list_elem(ll,i++))) {
282 if (item->type!=t_closure) {
283 cfgfatal(item->loc,"init_log","item is not a closure");
284 }
285 cl=item->data.closure;
286 if (cl->type!=CL_LOG) {
287 cfgfatal(item->loc,"init_log","closure is not a logger");
288 }
b7886fd4 289 NEW(n);
7138d0c5
SE
290 n->l=cl->interface;
291 n->next=l;
292 l=n;
293 }
294 if (!l) {
295 fatal("init_log: no log");
296 }
952f601f 297 NEW(r);
7138d0c5 298 r->st=l;
779837e1 299 r->vlogfn=log_vmulti;
ff1dcd86 300 r->buff[0]=0;
7138d0c5
SE
301 return r;
302}
303
304struct logfile {
305 closure_t cl;
306 struct log_if ops;
307 struct cloc loc;
308 string_t logfile;
309 uint32_t level;
310 FILE *f;
b85fe3f9 311 bool_t forked;
7138d0c5
SE
312};
313
fe5e9cc4 314static cstring_t months[]={
7138d0c5
SE
315 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
316
5ffc82e7 317FORMAT(printf, 3, 0)
fe5e9cc4
SE
318static void logfile_vlog(void *sst, int class, const char *message,
319 va_list args)
7138d0c5
SE
320{
321 struct logfile *st=sst;
322 time_t t;
323 struct tm *tm;
b85fe3f9
IJ
324 char pidbuf[20];
325
326 if (st->forked) {
327 pid_t us=getpid();
328 snprintf(pidbuf,sizeof(pidbuf),"[%ld] ",(long)us);
329 } else {
330 pidbuf[0]=0;
331 }
7138d0c5 332
fe5e9cc4 333 if (secnet_is_daemon && st->f) {
7138d0c5
SE
334 if (class&st->level) {
335 t=time(NULL);
336 tm=localtime(&t);
b85fe3f9 337 fprintf(st->f,"%s %2d %02d:%02d:%02d %s",
7138d0c5 338 months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,
b85fe3f9
IJ
339 tm->tm_sec,
340 pidbuf);
7138d0c5
SE
341 vfprintf(st->f,message,args);
342 fprintf(st->f,"\n");
343 fflush(st->f);
344 }
345 } else {
b85fe3f9 346 if (pidbuf[0]) MessageFallback(class,"%s",pidbuf);
ba72e2bf
IJ
347 vMessageFallback(class,message,args);
348 MessageFallback(class,"\n");
7138d0c5
SE
349 }
350}
351
5ffc82e7 352FORMAT(printf,3,4)
779837e1 353static void logfile_log(void *state, int class, const char *message, ...)
7138d0c5
SE
354{
355 va_list ap;
356
357 va_start(ap,message);
fe5e9cc4 358 logfile_vlog(state,class,message,ap);
7138d0c5
SE
359 va_end(ap);
360}
361
362static void logfile_hup_notify(void *sst, int signum)
363{
364 struct logfile *st=sst;
365 FILE *f;
366 f=fopen(st->logfile,"a");
367 if (!f) {
368 logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen "
369 "logfile: %s",strerror(errno));
370 } else {
371 fclose(st->f);
372 st->f=f;
373 logfile_log(st,M_INFO,"received SIGHUP");
374 }
375}
376
377static void logfile_phase_hook(void *sst, uint32_t new_phase)
378{
379 struct logfile *st=sst;
380 FILE *f;
381
382 if (background) {
383 f=fopen(st->logfile,"a");
384 if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
385 st->loc.file,st->loc.line,st->logfile);
386 st->f=f;
387 request_signal_notification(SIGHUP, logfile_hup_notify,st);
388 }
389}
390
b85fe3f9
IJ
391static void logfile_childpersist_hook(void *sst, uint32_t new_phase)
392{
393 struct logfile *st=sst;
394 st->forked=1;
395}
396
7138d0c5
SE
397static struct flagstr message_class_table[]={
398 { "debug-config", M_DEBUG_CONFIG },
399 { "debug-phase", M_DEBUG_PHASE },
400 { "debug", M_DEBUG },
401 { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG },
402 { "info", M_INFO },
403 { "notice", M_NOTICE },
404 { "warning", M_WARNING },
469fd1d9 405 { "error", M_ERR },
7138d0c5
SE
406 { "security", M_SECURITY },
407 { "fatal", M_FATAL },
469fd1d9
SE
408 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
409 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
7138d0c5
SE
410 { "quiet", M_FATAL },
411 { NULL, 0 }
412};
413
414static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
415 list_t *args)
416{
417 struct logfile *st;
418 item_t *item;
419 dict_t *dict;
420
421 /* We should defer opening the logfile until the getresources
422 phase. We should defer writing into the logfile until after we
423 become a daemon. */
424
b7886fd4 425 NEW(st);
7138d0c5
SE
426 st->cl.description="logfile";
427 st->cl.type=CL_LOG;
428 st->cl.apply=NULL;
429 st->cl.interface=&st->ops;
430 st->ops.st=st;
779837e1 431 st->ops.vlogfn=logfile_vlog;
ff1dcd86 432 st->ops.buff[0]=0;
7138d0c5 433 st->loc=loc;
fe5e9cc4 434 st->f=NULL;
b85fe3f9 435 st->forked=0;
7138d0c5
SE
436
437 item=list_elem(args,0);
438 if (!item || item->type!=t_dict) {
439 cfgfatal(loc,"logfile","argument must be a dictionary\n");
440 }
441 dict=item->data.dict;
442
443 st->logfile=dict_read_string(dict,"filename",True,"logfile",loc);
444 st->level=string_list_to_word(dict_lookup(dict,"class"),
445 message_class_table,"logfile");
446
447 add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st);
b85fe3f9 448 add_hook(PHASE_CHILDPERSIST,logfile_childpersist_hook,st);
7138d0c5
SE
449
450 return new_closure(&st->cl);
451}
452
453struct syslog {
454 closure_t cl;
455 struct log_if ops;
456 string_t ident;
457 int facility;
458 bool_t open;
459};
460
461static int msgclass_to_syslogpriority(uint32_t m)
462{
463 switch (m) {
464 case M_DEBUG_CONFIG: return LOG_DEBUG;
465 case M_DEBUG_PHASE: return LOG_DEBUG;
466 case M_DEBUG: return LOG_DEBUG;
467 case M_INFO: return LOG_INFO;
468 case M_NOTICE: return LOG_NOTICE;
469 case M_WARNING: return LOG_WARNING;
469fd1d9 470 case M_ERR: return LOG_ERR;
7138d0c5
SE
471 case M_SECURITY: return LOG_CRIT;
472 case M_FATAL: return LOG_EMERG;
473 default: return LOG_NOTICE;
474 }
475}
476
fe5e9cc4 477static void syslog_vlog(void *sst, int class, const char *message,
7138d0c5 478 va_list args)
779837e1
IJ
479 FORMAT(printf,3,0);
480static void syslog_vlog(void *sst, int class, const char *message,
481 va_list args)
7138d0c5
SE
482{
483 struct syslog *st=sst;
484
485 if (st->open)
486 vsyslog(msgclass_to_syslogpriority(class),message,args);
487 else {
ba72e2bf
IJ
488 vMessageFallback(class,message,args);
489 MessageFallback(class,"\n");
7138d0c5
SE
490 }
491}
492
7138d0c5 493static struct flagstr syslog_facility_table[]={
8dea8d37
SE
494#ifdef LOG_AUTH
495 { "auth", LOG_AUTH },
496#endif
497#ifdef LOG_AUTHPRIV
7138d0c5 498 { "authpriv", LOG_AUTHPRIV },
8dea8d37 499#endif
7138d0c5
SE
500 { "cron", LOG_CRON },
501 { "daemon", LOG_DAEMON },
502 { "kern", LOG_KERN },
503 { "local0", LOG_LOCAL0 },
504 { "local1", LOG_LOCAL1 },
505 { "local2", LOG_LOCAL2 },
506 { "local3", LOG_LOCAL3 },
507 { "local4", LOG_LOCAL4 },
508 { "local5", LOG_LOCAL5 },
509 { "local6", LOG_LOCAL6 },
510 { "local7", LOG_LOCAL7 },
511 { "lpr", LOG_LPR },
512 { "mail", LOG_MAIL },
513 { "news", LOG_NEWS },
514 { "syslog", LOG_SYSLOG },
515 { "user", LOG_USER },
516 { "uucp", LOG_UUCP },
517 { NULL, 0 }
518};
519
520static void syslog_phase_hook(void *sst, uint32_t newphase)
521{
522 struct syslog *st=sst;
523
524 if (background) {
b85fe3f9
IJ
525 openlog(st->ident,
526 newphase==PHASE_CHILDPERSIST ? LOG_PID : 0,
527 st->facility);
7138d0c5
SE
528 st->open=True;
529 }
530}
531
532static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context,
533 list_t *args)
534{
535 struct syslog *st;
536 dict_t *d;
537 item_t *item;
538 string_t facstr;
539
b7886fd4 540 NEW(st);
7138d0c5
SE
541 st->cl.description="syslog";
542 st->cl.type=CL_LOG;
543 st->cl.apply=NULL;
544 st->cl.interface=&st->ops;
545 st->ops.st=st;
779837e1 546 st->ops.vlogfn=syslog_vlog;
ff1dcd86 547 st->ops.buff[0]=0;
7138d0c5
SE
548
549 item=list_elem(args,0);
550 if (!item || item->type!=t_dict)
551 cfgfatal(loc,"syslog","parameter must be a dictionary\n");
552 d=item->data.dict;
553
554 st->ident=dict_read_string(d, "ident", False, "syslog", loc);
555 facstr=dict_read_string(d, "facility", True, "syslog", loc);
556 st->facility=string_to_word(facstr,loc,
557 syslog_facility_table,"syslog");
558 st->open=False;
559 add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st);
b85fe3f9 560 add_hook(PHASE_CHILDPERSIST,syslog_phase_hook,st);
7138d0c5
SE
561
562 return new_closure(&st->cl);
563}
564
fe5e9cc4
SE
565/* Read from a fd and output to a log. This is a quick hack to
566 support logging stderr, and needs code adding to tidy up before it
567 can be used for anything else. */
568#define FDLOG_BUFSIZE 1024
569struct fdlog {
570 struct log_if *log;
571 int fd;
572 cstring_t prefix;
573 string_t buffer;
574 int i;
575 bool_t finished;
576};
577
578static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
90a39563 579 int *timeout_io)
fe5e9cc4
SE
580{
581 struct fdlog *st=sst;
582 if (!st->finished) {
ee697dd9 583 BEFOREPOLL_WANT_FDS(1);
fe5e9cc4
SE
584 fds[0].fd=st->fd;
585 fds[0].events=POLLIN;
ee697dd9
IJ
586 } else {
587 BEFOREPOLL_WANT_FDS(0);
fe5e9cc4
SE
588 }
589 return 0;
590}
591
90a39563 592static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
fe5e9cc4
SE
593{
594 struct fdlog *st=sst;
595 int r,remain,i;
596
597 if (nfds==0) return;
598 if (fds[0].revents&POLLERR) {
599 st->finished=True;
600 }
601 if (fds[0].revents&POLLIN) {
602 remain=FDLOG_BUFSIZE-st->i-1;
603 if (remain<=0) {
604 st->buffer[FDLOG_BUFSIZE-1]=0;
779837e1 605 slilog(st->log,M_WARNING,"%s: overlong line: %s",
fe5e9cc4
SE
606 st->prefix,st->buffer);
607 st->i=0;
608 remain=FDLOG_BUFSIZE-1;
609 }
610 r=read(st->fd,st->buffer+st->i,remain);
611 if (r>0) {
612 st->i+=r;
613 for (i=0; i<st->i; i++) {
614 if (st->buffer[i]=='\n') {
615 st->buffer[i]=0;
779837e1 616 slilog(st->log,M_INFO,"%s: %s",
fe5e9cc4
SE
617 st->prefix,st->buffer);
618 i++;
619 memmove(st->buffer,st->buffer+i,st->i-i);
620 st->i-=i;
621 i=-1;
622 }
623 }
ba703386 624 } else if (errno==EINTR || iswouldblock(errno)) {
fe5e9cc4
SE
625 } else {
626 Message(M_WARNING,"log_from_fd: %s\n",strerror(errno));
627 st->finished=True;
628 }
629 }
630}
631
632void log_from_fd(int fd, cstring_t prefix, struct log_if *log)
633{
634 struct fdlog *st;
635
b7886fd4 636 NEW(st);
fe5e9cc4
SE
637 st->log=log;
638 st->fd=fd;
639 st->prefix=prefix;
640 st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd");
641 st->i=0;
642 st->finished=False;
643
ba703386
IJ
644 setnonblock(st->fd);
645
32fc582f 646 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,
fe5e9cc4
SE
647 prefix);
648}
649
7138d0c5
SE
650void log_module(dict_t *dict)
651{
652 add_closure(dict,"logfile",logfile_apply);
653 add_closure(dict,"syslog",syslog_apply);
654}