Commit | Line | Data |
---|---|---|
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 | |
13 | bool_t secnet_is_daemon=False; | |
469fd1d9 | 14 | uint32_t message_level=M_WARNING|M_ERR|M_SECURITY|M_FATAL; |
7138d0c5 SE |
15 | struct log_if *system_log=NULL; |
16 | ||
ba72e2bf | 17 | static void vMessageFallback(uint32_t class, const char *message, va_list args) |
779837e1 IJ |
18 | FORMAT(printf,2,0); |
19 | static 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 | ||
31 | static 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 | 42 | void 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 | 51 | static void MessageFallback(uint32_t class, const char *message, ...) |
779837e1 IJ |
52 | FORMAT(printf,2,3); |
53 | static 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 | 62 | static NORETURN(vfatal(int status, bool_t perror, const char *message, |
4f5e39ec SE |
63 | va_list args)); |
64 | ||
fe5e9cc4 SE |
65 | static 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 | 82 | void 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 | 90 | void 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 | 98 | void 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 | 106 | void 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 | 114 | void 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 |
140 | void 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 | 150 | void 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 | ||
159 | void 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 */ |
173 | struct loglist { | |
174 | struct log_if *l; | |
175 | struct loglist *next; | |
176 | }; | |
177 | ||
fe5e9cc4 | 178 | static 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 | ||
7138d0c5 SE |
192 | struct log_if *init_log(list_t *ll) |
193 | { | |
194 | int i=0; | |
195 | item_t *item; | |
196 | closure_t *cl; | |
197 | struct loglist *l=NULL, *n; | |
198 | struct log_if *r; | |
199 | ||
200 | if (list_length(ll)==1) { | |
201 | item=list_elem(ll,0); | |
202 | cl=item->data.closure; | |
203 | if (cl->type!=CL_LOG) { | |
204 | cfgfatal(item->loc,"init_log","closure is not a logger"); | |
205 | } | |
206 | return cl->interface; | |
207 | } | |
208 | while ((item=list_elem(ll,i++))) { | |
209 | if (item->type!=t_closure) { | |
210 | cfgfatal(item->loc,"init_log","item is not a closure"); | |
211 | } | |
212 | cl=item->data.closure; | |
213 | if (cl->type!=CL_LOG) { | |
214 | cfgfatal(item->loc,"init_log","closure is not a logger"); | |
215 | } | |
216 | n=safe_malloc(sizeof(*n),"init_log"); | |
217 | n->l=cl->interface; | |
218 | n->next=l; | |
219 | l=n; | |
220 | } | |
221 | if (!l) { | |
222 | fatal("init_log: no log"); | |
223 | } | |
224 | r=safe_malloc(sizeof(*r), "init_log"); | |
225 | r->st=l; | |
779837e1 | 226 | r->vlogfn=log_vmulti; |
ff1dcd86 | 227 | r->buff[0]=0; |
7138d0c5 SE |
228 | return r; |
229 | } | |
230 | ||
231 | struct logfile { | |
232 | closure_t cl; | |
233 | struct log_if ops; | |
234 | struct cloc loc; | |
235 | string_t logfile; | |
236 | uint32_t level; | |
237 | FILE *f; | |
238 | }; | |
239 | ||
fe5e9cc4 | 240 | static cstring_t months[]={ |
7138d0c5 SE |
241 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; |
242 | ||
fe5e9cc4 SE |
243 | static void logfile_vlog(void *sst, int class, const char *message, |
244 | va_list args) | |
7138d0c5 SE |
245 | { |
246 | struct logfile *st=sst; | |
247 | time_t t; | |
248 | struct tm *tm; | |
249 | ||
fe5e9cc4 | 250 | if (secnet_is_daemon && st->f) { |
7138d0c5 SE |
251 | if (class&st->level) { |
252 | t=time(NULL); | |
253 | tm=localtime(&t); | |
254 | fprintf(st->f,"%s %2d %02d:%02d:%02d ", | |
255 | months[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min, | |
256 | tm->tm_sec); | |
257 | vfprintf(st->f,message,args); | |
258 | fprintf(st->f,"\n"); | |
259 | fflush(st->f); | |
260 | } | |
261 | } else { | |
ba72e2bf IJ |
262 | vMessageFallback(class,message,args); |
263 | MessageFallback(class,"\n"); | |
7138d0c5 SE |
264 | } |
265 | } | |
266 | ||
fe5e9cc4 | 267 | static void logfile_log(void *state, int class, const char *message, ...) |
779837e1 IJ |
268 | FORMAT(printf,3,4); |
269 | static void logfile_log(void *state, int class, const char *message, ...) | |
7138d0c5 SE |
270 | { |
271 | va_list ap; | |
272 | ||
273 | va_start(ap,message); | |
fe5e9cc4 | 274 | logfile_vlog(state,class,message,ap); |
7138d0c5 SE |
275 | va_end(ap); |
276 | } | |
277 | ||
278 | static void logfile_hup_notify(void *sst, int signum) | |
279 | { | |
280 | struct logfile *st=sst; | |
281 | FILE *f; | |
282 | f=fopen(st->logfile,"a"); | |
283 | if (!f) { | |
284 | logfile_log(st,M_FATAL,"received SIGHUP, but could not reopen " | |
285 | "logfile: %s",strerror(errno)); | |
286 | } else { | |
287 | fclose(st->f); | |
288 | st->f=f; | |
289 | logfile_log(st,M_INFO,"received SIGHUP"); | |
290 | } | |
291 | } | |
292 | ||
293 | static void logfile_phase_hook(void *sst, uint32_t new_phase) | |
294 | { | |
295 | struct logfile *st=sst; | |
296 | FILE *f; | |
297 | ||
298 | if (background) { | |
299 | f=fopen(st->logfile,"a"); | |
300 | if (!f) fatal_perror("logfile (%s:%d): cannot open \"%s\"", | |
301 | st->loc.file,st->loc.line,st->logfile); | |
302 | st->f=f; | |
303 | request_signal_notification(SIGHUP, logfile_hup_notify,st); | |
304 | } | |
305 | } | |
306 | ||
307 | static struct flagstr message_class_table[]={ | |
308 | { "debug-config", M_DEBUG_CONFIG }, | |
309 | { "debug-phase", M_DEBUG_PHASE }, | |
310 | { "debug", M_DEBUG }, | |
311 | { "all-debug", M_DEBUG|M_DEBUG_PHASE|M_DEBUG_CONFIG }, | |
312 | { "info", M_INFO }, | |
313 | { "notice", M_NOTICE }, | |
314 | { "warning", M_WARNING }, | |
469fd1d9 | 315 | { "error", M_ERR }, |
7138d0c5 SE |
316 | { "security", M_SECURITY }, |
317 | { "fatal", M_FATAL }, | |
469fd1d9 SE |
318 | { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL }, |
319 | { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL }, | |
7138d0c5 SE |
320 | { "quiet", M_FATAL }, |
321 | { NULL, 0 } | |
322 | }; | |
323 | ||
324 | static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context, | |
325 | list_t *args) | |
326 | { | |
327 | struct logfile *st; | |
328 | item_t *item; | |
329 | dict_t *dict; | |
330 | ||
331 | /* We should defer opening the logfile until the getresources | |
332 | phase. We should defer writing into the logfile until after we | |
333 | become a daemon. */ | |
334 | ||
335 | st=safe_malloc(sizeof(*st),"logfile_apply"); | |
336 | st->cl.description="logfile"; | |
337 | st->cl.type=CL_LOG; | |
338 | st->cl.apply=NULL; | |
339 | st->cl.interface=&st->ops; | |
340 | st->ops.st=st; | |
779837e1 | 341 | st->ops.vlogfn=logfile_vlog; |
ff1dcd86 | 342 | st->ops.buff[0]=0; |
7138d0c5 | 343 | st->loc=loc; |
fe5e9cc4 | 344 | st->f=NULL; |
7138d0c5 SE |
345 | |
346 | item=list_elem(args,0); | |
347 | if (!item || item->type!=t_dict) { | |
348 | cfgfatal(loc,"logfile","argument must be a dictionary\n"); | |
349 | } | |
350 | dict=item->data.dict; | |
351 | ||
352 | st->logfile=dict_read_string(dict,"filename",True,"logfile",loc); | |
353 | st->level=string_list_to_word(dict_lookup(dict,"class"), | |
354 | message_class_table,"logfile"); | |
355 | ||
356 | add_hook(PHASE_GETRESOURCES,logfile_phase_hook,st); | |
357 | ||
358 | return new_closure(&st->cl); | |
359 | } | |
360 | ||
361 | struct syslog { | |
362 | closure_t cl; | |
363 | struct log_if ops; | |
364 | string_t ident; | |
365 | int facility; | |
366 | bool_t open; | |
367 | }; | |
368 | ||
369 | static int msgclass_to_syslogpriority(uint32_t m) | |
370 | { | |
371 | switch (m) { | |
372 | case M_DEBUG_CONFIG: return LOG_DEBUG; | |
373 | case M_DEBUG_PHASE: return LOG_DEBUG; | |
374 | case M_DEBUG: return LOG_DEBUG; | |
375 | case M_INFO: return LOG_INFO; | |
376 | case M_NOTICE: return LOG_NOTICE; | |
377 | case M_WARNING: return LOG_WARNING; | |
469fd1d9 | 378 | case M_ERR: return LOG_ERR; |
7138d0c5 SE |
379 | case M_SECURITY: return LOG_CRIT; |
380 | case M_FATAL: return LOG_EMERG; | |
381 | default: return LOG_NOTICE; | |
382 | } | |
383 | } | |
384 | ||
fe5e9cc4 | 385 | static void syslog_vlog(void *sst, int class, const char *message, |
7138d0c5 | 386 | va_list args) |
779837e1 IJ |
387 | FORMAT(printf,3,0); |
388 | static void syslog_vlog(void *sst, int class, const char *message, | |
389 | va_list args) | |
7138d0c5 SE |
390 | { |
391 | struct syslog *st=sst; | |
392 | ||
393 | if (st->open) | |
394 | vsyslog(msgclass_to_syslogpriority(class),message,args); | |
395 | else { | |
ba72e2bf IJ |
396 | vMessageFallback(class,message,args); |
397 | MessageFallback(class,"\n"); | |
7138d0c5 SE |
398 | } |
399 | } | |
400 | ||
7138d0c5 | 401 | static struct flagstr syslog_facility_table[]={ |
8dea8d37 SE |
402 | #ifdef LOG_AUTH |
403 | { "auth", LOG_AUTH }, | |
404 | #endif | |
405 | #ifdef LOG_AUTHPRIV | |
7138d0c5 | 406 | { "authpriv", LOG_AUTHPRIV }, |
8dea8d37 | 407 | #endif |
7138d0c5 SE |
408 | { "cron", LOG_CRON }, |
409 | { "daemon", LOG_DAEMON }, | |
410 | { "kern", LOG_KERN }, | |
411 | { "local0", LOG_LOCAL0 }, | |
412 | { "local1", LOG_LOCAL1 }, | |
413 | { "local2", LOG_LOCAL2 }, | |
414 | { "local3", LOG_LOCAL3 }, | |
415 | { "local4", LOG_LOCAL4 }, | |
416 | { "local5", LOG_LOCAL5 }, | |
417 | { "local6", LOG_LOCAL6 }, | |
418 | { "local7", LOG_LOCAL7 }, | |
419 | { "lpr", LOG_LPR }, | |
420 | { "mail", LOG_MAIL }, | |
421 | { "news", LOG_NEWS }, | |
422 | { "syslog", LOG_SYSLOG }, | |
423 | { "user", LOG_USER }, | |
424 | { "uucp", LOG_UUCP }, | |
425 | { NULL, 0 } | |
426 | }; | |
427 | ||
428 | static void syslog_phase_hook(void *sst, uint32_t newphase) | |
429 | { | |
430 | struct syslog *st=sst; | |
431 | ||
432 | if (background) { | |
433 | openlog(st->ident,0,st->facility); | |
434 | st->open=True; | |
435 | } | |
436 | } | |
437 | ||
438 | static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context, | |
439 | list_t *args) | |
440 | { | |
441 | struct syslog *st; | |
442 | dict_t *d; | |
443 | item_t *item; | |
444 | string_t facstr; | |
445 | ||
446 | st=safe_malloc(sizeof(*st),"syslog_apply"); | |
447 | st->cl.description="syslog"; | |
448 | st->cl.type=CL_LOG; | |
449 | st->cl.apply=NULL; | |
450 | st->cl.interface=&st->ops; | |
451 | st->ops.st=st; | |
779837e1 | 452 | st->ops.vlogfn=syslog_vlog; |
ff1dcd86 | 453 | st->ops.buff[0]=0; |
7138d0c5 SE |
454 | |
455 | item=list_elem(args,0); | |
456 | if (!item || item->type!=t_dict) | |
457 | cfgfatal(loc,"syslog","parameter must be a dictionary\n"); | |
458 | d=item->data.dict; | |
459 | ||
460 | st->ident=dict_read_string(d, "ident", False, "syslog", loc); | |
461 | facstr=dict_read_string(d, "facility", True, "syslog", loc); | |
462 | st->facility=string_to_word(facstr,loc, | |
463 | syslog_facility_table,"syslog"); | |
464 | st->open=False; | |
465 | add_hook(PHASE_GETRESOURCES,syslog_phase_hook,st); | |
466 | ||
467 | return new_closure(&st->cl); | |
468 | } | |
469 | ||
fe5e9cc4 SE |
470 | /* Read from a fd and output to a log. This is a quick hack to |
471 | support logging stderr, and needs code adding to tidy up before it | |
472 | can be used for anything else. */ | |
473 | #define FDLOG_BUFSIZE 1024 | |
474 | struct fdlog { | |
475 | struct log_if *log; | |
476 | int fd; | |
477 | cstring_t prefix; | |
478 | string_t buffer; | |
479 | int i; | |
480 | bool_t finished; | |
481 | }; | |
482 | ||
483 | static int log_from_fd_beforepoll(void *sst, struct pollfd *fds, int *nfds_io, | |
90a39563 | 484 | int *timeout_io) |
fe5e9cc4 SE |
485 | { |
486 | struct fdlog *st=sst; | |
487 | if (!st->finished) { | |
488 | *nfds_io=1; | |
489 | fds[0].fd=st->fd; | |
490 | fds[0].events=POLLIN; | |
491 | } | |
492 | return 0; | |
493 | } | |
494 | ||
90a39563 | 495 | static void log_from_fd_afterpoll(void *sst, struct pollfd *fds, int nfds) |
fe5e9cc4 SE |
496 | { |
497 | struct fdlog *st=sst; | |
498 | int r,remain,i; | |
499 | ||
500 | if (nfds==0) return; | |
501 | if (fds[0].revents&POLLERR) { | |
502 | st->finished=True; | |
503 | } | |
504 | if (fds[0].revents&POLLIN) { | |
505 | remain=FDLOG_BUFSIZE-st->i-1; | |
506 | if (remain<=0) { | |
507 | st->buffer[FDLOG_BUFSIZE-1]=0; | |
779837e1 | 508 | slilog(st->log,M_WARNING,"%s: overlong line: %s", |
fe5e9cc4 SE |
509 | st->prefix,st->buffer); |
510 | st->i=0; | |
511 | remain=FDLOG_BUFSIZE-1; | |
512 | } | |
513 | r=read(st->fd,st->buffer+st->i,remain); | |
514 | if (r>0) { | |
515 | st->i+=r; | |
516 | for (i=0; i<st->i; i++) { | |
517 | if (st->buffer[i]=='\n') { | |
518 | st->buffer[i]=0; | |
779837e1 | 519 | slilog(st->log,M_INFO,"%s: %s", |
fe5e9cc4 SE |
520 | st->prefix,st->buffer); |
521 | i++; | |
522 | memmove(st->buffer,st->buffer+i,st->i-i); | |
523 | st->i-=i; | |
524 | i=-1; | |
525 | } | |
526 | } | |
527 | } else { | |
528 | Message(M_WARNING,"log_from_fd: %s\n",strerror(errno)); | |
529 | st->finished=True; | |
530 | } | |
531 | } | |
532 | } | |
533 | ||
534 | void log_from_fd(int fd, cstring_t prefix, struct log_if *log) | |
535 | { | |
536 | struct fdlog *st; | |
537 | ||
538 | st=safe_malloc(sizeof(*st),"log_from_fd"); | |
539 | st->log=log; | |
540 | st->fd=fd; | |
541 | st->prefix=prefix; | |
542 | st->buffer=safe_malloc(FDLOG_BUFSIZE,"log_from_fd"); | |
543 | st->i=0; | |
544 | st->finished=False; | |
545 | ||
546 | register_for_poll(st,log_from_fd_beforepoll,log_from_fd_afterpoll,1, | |
547 | prefix); | |
548 | } | |
549 | ||
7138d0c5 SE |
550 | void log_module(dict_t *dict) |
551 | { | |
552 | add_closure(dict,"logfile",logfile_apply); | |
553 | add_closure(dict,"syslog",syslog_apply); | |
554 | } |