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