comm: Provide udp_socks_deregister
[secnet] / log.c
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>
8 #include <assert.h>
9 #include <unistd.h>
10 #include "process.h"
11 #include "util.h"
12
13 bool_t secnet_is_daemon=False;
14 uint32_t message_level=M_WARNING|M_ERR|M_SECURITY|M_FATAL;
15 struct log_if *system_log=NULL;
16
17 static void vMessageFallback(uint32_t class, const char *message, va_list args)
18 FORMAT(printf,2,0);
19 static void vMessageFallback(uint32_t class, const char *message, va_list args)
20 {
21 FILE *dest=stdout;
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
31 static void vMessage(uint32_t class, const char *message, va_list args)
32 {
33
34 if (system_log) {
35 /* Messages go to the system log interface */
36 vslilog_part(system_log, class, message, args);
37 } else {
38 vMessageFallback(class,message,args);
39 }
40 }
41
42 void Message(uint32_t class, const char *message, ...)
43 {
44 va_list ap;
45
46 va_start(ap,message);
47 vMessage(class,message,ap);
48 va_end(ap);
49 }
50
51 static void MessageFallback(uint32_t class, const char *message, ...)
52 FORMAT(printf,2,3);
53 static void MessageFallback(uint32_t class, const char *message, ...)
54 {
55 va_list ap;
56
57 va_start(ap,message);
58 vMessageFallback(class,message,ap);
59 va_end(ap);
60 }
61
62 static NORETURN(vfatal(int status, bool_t perror, const char *message,
63 va_list args));
64
65 static void vfatal(int status, bool_t perror, const char *message,
66 va_list args)
67 {
68 int err;
69
70 err=errno;
71
72 enter_phase(PHASE_SHUTDOWN);
73 Message(M_FATAL, "secnet fatal error: ");
74 vMessage(M_FATAL, message, args);
75 if (perror)
76 Message(M_FATAL, ": %s\n",strerror(err));
77 else
78 Message(M_FATAL, "\n");
79 exit(status);
80 }
81
82 void fatal(const char *message, ...)
83 {
84 va_list args;
85 va_start(args,message);
86 vfatal(current_phase,False,message,args);
87 va_end(args);
88 }
89
90 void fatal_status(int status, const char *message, ...)
91 {
92 va_list args;
93 va_start(args,message);
94 vfatal(status,False,message,args);
95 va_end(args);
96 }
97
98 void fatal_perror(const char *message, ...)
99 {
100 va_list args;
101 va_start(args,message);
102 vfatal(current_phase,True,message,args);
103 va_end(args);
104 }
105
106 void fatal_perror_status(int status, const char *message, ...)
107 {
108 va_list args;
109 va_start(args,message);
110 vfatal(status,True,message,args);
111 va_end(args);
112 }
113
114 void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
115 cstring_t facility, const char *message, va_list args)
116 {
117 enter_phase(PHASE_SHUTDOWN);
118
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) {
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);
137 exit(current_phase);
138 }
139
140 void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, cstring_t facility,
141 const char *message, ...)
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
150 void cfgfatal(struct cloc loc, cstring_t facility, const char *message, ...)
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
159 void cfgfile_postreadcheck(struct cloc loc, FILE *f)
160 {
161 assert(loc.file);
162 if (ferror(f)) {
163 Message(M_FATAL, "error reading config file (%s): %s\n",
164 loc.file, strerror(errno));
165 exit(current_phase);
166 } else if (feof(f)) {
167 Message(M_FATAL, "unexpected end of config file (%s)\n", loc.file);
168 exit(current_phase);
169 }
170 }
171
172 /* Take a list of log closures and merge them */
173 struct loglist {
174 struct log_if *l;
175 struct loglist *next;
176 };
177
178 static void log_vmulti(void *sst, int class, const char *message, va_list args)
179 {
180 struct loglist *st=sst, *i;
181
182 if (secnet_is_daemon) {
183 for (i=st; i; i=i->next) {
184 vslilog(i->l,class,message,args);
185 }
186 } else {
187 vMessage(class,message,args);
188 Message(class,"\n");
189 }
190 }
191
192 void 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
219 void 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
228 struct 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;
262 r->vlogfn=log_vmulti;
263 r->buff[0]=0;
264 return r;
265 }
266
267 struct 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
276 static cstring_t months[]={
277 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
278
279 static void logfile_vlog(void *sst, int class, const char *message,
280 va_list args)
281 {
282 struct logfile *st=sst;
283 time_t t;
284 struct tm *tm;
285
286 if (secnet_is_daemon && st->f) {
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 {
298 vMessageFallback(class,message,args);
299 MessageFallback(class,"\n");
300 }
301 }
302
303 static void logfile_log(void *state, int class, const char *message, ...)
304 FORMAT(printf,3,4);
305 static void logfile_log(void *state, int class, const char *message, ...)
306 {
307 va_list ap;
308
309 va_start(ap,message);
310 logfile_vlog(state,class,message,ap);
311 va_end(ap);
312 }
313
314 static 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
329 static 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
343 static 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 },
351 { "error", M_ERR },
352 { "security", M_SECURITY },
353 { "fatal", M_FATAL },
354 { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
355 { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
356 { "quiet", M_FATAL },
357 { NULL, 0 }
358 };
359
360 static 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;
377 st->ops.vlogfn=logfile_vlog;
378 st->ops.buff[0]=0;
379 st->loc=loc;
380 st->f=NULL;
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
397 struct syslog {
398 closure_t cl;
399 struct log_if ops;
400 string_t ident;
401 int facility;
402 bool_t open;
403 };
404
405 static 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;
414 case M_ERR: return LOG_ERR;
415 case M_SECURITY: return LOG_CRIT;
416 case M_FATAL: return LOG_EMERG;
417 default: return LOG_NOTICE;
418 }
419 }
420
421 static void syslog_vlog(void *sst, int class, const char *message,
422 va_list args)
423 FORMAT(printf,3,0);
424 static void syslog_vlog(void *sst, int class, const char *message,
425 va_list args)
426 {
427 struct syslog *st=sst;
428
429 if (st->open)
430 vsyslog(msgclass_to_syslogpriority(class),message,args);
431 else {
432 vMessageFallback(class,message,args);
433 MessageFallback(class,"\n");
434 }
435 }
436
437 static struct flagstr syslog_facility_table[]={
438 #ifdef LOG_AUTH
439 { "auth", LOG_AUTH },
440 #endif
441 #ifdef LOG_AUTHPRIV
442 { "authpriv", LOG_AUTHPRIV },
443 #endif
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
464 static 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
474 static 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;
488 st->ops.vlogfn=syslog_vlog;
489 st->ops.buff[0]=0;
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
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
510 struct 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
519 static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
520 int *timeout_io)
521 {
522 struct fdlog *st=sst;
523 if (!st->finished) {
524 BEFOREPOLL_WANT_FDS(1);
525 fds[0].fd=st->fd;
526 fds[0].events=POLLIN;
527 } else {
528 BEFOREPOLL_WANT_FDS(0);
529 }
530 return 0;
531 }
532
533 static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds)
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;
546 slilog(st->log,M_WARNING,"%s: overlong line: %s",
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;
557 slilog(st->log,M_INFO,"%s: %s",
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
572 void 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
584 register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,
585 prefix);
586 }
587
588 void log_module(dict_t *dict)
589 {
590 add_closure(dict,"logfile",logfile_apply);
591 add_closure(dict,"syslog",syslog_apply);
592 }