3 * - output and logging support
4 * - program lifetime support
5 * - IP address and subnet munging routines
6 * - MPI convenience functions
10 * Copyright (C) 1995--2001 Stephen Early <steve@greenend.org.uk>
12 * It is part of secnet, which is
13 * Copyright (C) 1995--2001 Stephen Early <steve@greenend.org.uk>
14 * Copyright (C) 1998 Ross Anderson, Eli Biham, Lars Knudsen
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2, or (at your option)
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42 #include "unaligned.h"
44 #define MIN_BUFFER_SIZE 64
45 #define DEFAULT_BUFFER_SIZE 4096
46 #define MAX_BUFFER_SIZE 131072
48 static char *hexdigits
="0123456789abcdef";
50 bool_t secnet_is_daemon
=False
;
51 uint32_t message_level
=M_WARNING
|M_ERROR
|M_SECURITY
|M_FATAL
;
52 struct log_if
*system_log
=NULL
;
53 static uint32_t current_phase
=0;
58 struct phase_hook
*next
;
61 static struct phase_hook
*hooks
[NR_PHASES
]={NULL
,};
63 static void vMessage(uint32_t class, char *message
, va_list args
)
66 #define MESSAGE_BUFLEN 1023
67 static char buff
[MESSAGE_BUFLEN
+1]={0,};
71 if (secnet_is_daemon
) {
72 /* Messages go to the system log interface */
74 vsnprintf(buff
+bp
,MESSAGE_BUFLEN
-bp
,message
,args
);
75 /* Each line is sent separately */
76 while ((nlp
=strchr(buff
,'\n'))) {
78 log(system_log
,class,buff
);
79 memmove(buff
,nlp
+1,strlen(nlp
+1)+1);
82 /* Messages go to stdout/stderr */
83 if (class & message_level
) {
84 if (class&M_FATAL
|| class&M_ERROR
|| class&M_WARNING
) {
87 vfprintf(dest
,message
,args
);
92 void Message(uint32_t class, char *message
, ...)
97 vMessage(class,message
,ap
);
101 static void vfatal(int status
, bool_t perror
, char *message
, va_list args
)
107 enter_phase(PHASE_SHUTDOWN
);
109 Message(M_FATAL
, "secnet fatal error: ");
110 vMessage(M_FATAL
, message
, args
);
111 Message(M_FATAL
, ": %s\n",strerror(err
));
114 Message(M_FATAL
, "secnet fatal error: ");
115 vMessage(M_FATAL
,message
,args
);
120 void fatal(char *message
, ...)
123 va_start(args
,message
);
124 vfatal(current_phase
,False
,message
,args
);
128 void fatal_status(int status
, char *message
, ...)
131 va_start(args
,message
);
132 vfatal(status
,False
,message
,args
);
136 void fatal_perror(char *message
, ...)
139 va_start(args
,message
);
140 vfatal(current_phase
,True
,message
,args
);
144 void fatal_perror_status(int status
, char *message
, ...)
147 va_start(args
,message
);
148 vfatal(status
,True
,message
,args
);
152 void cfgfatal(struct cloc loc
, string_t facility
, char *message
, ...)
156 va_start(args
,message
);
158 enter_phase(PHASE_SHUTDOWN
);
160 if (loc
.file
&& loc
.line
) {
161 Message(M_FATAL
, "config error (%s, %s:%d): ",facility
,loc
.file
,
163 } else if (!loc
.file
&& loc
.line
) {
164 Message(M_FATAL
, "config error (%s, line %d): ",facility
,loc
.line
);
166 Message(M_FATAL
, "config error (%s): ",facility
);
169 vMessage(M_FATAL
,message
,args
);
174 /* Take a list of log closures and merge them */
177 struct loglist
*next
;
180 static void log_vmulti(void *sst
, int class, char *message
, va_list args
)
182 struct loglist
*st
=sst
, *i
;
184 if (secnet_is_daemon
) {
185 for (i
=st
; i
; i
=i
->next
) {
186 i
->l
->vlog(i
->l
->st
,class,message
,args
);
189 vMessage(class,message
,args
);
194 static void log_multi(void *st
, int priority
, char *message
, ...)
198 va_start(ap
,message
);
199 log_vmulti(st
,priority
,message
,ap
);
203 struct log_if
*init_log(list_t
*ll
)
208 struct loglist
*l
=NULL
, *n
;
211 if (list_length(ll
)==1) {
212 item
=list_elem(ll
,0);
213 cl
=item
->data
.closure
;
214 if (cl
->type
!=CL_LOG
) {
215 cfgfatal(item
->loc
,"init_log","closure is not a logger");
217 return cl
->interface
;
219 while ((item
=list_elem(ll
,i
++))) {
220 if (item
->type
!=t_closure
) {
221 cfgfatal(item
->loc
,"init_log","item is not a closure");
223 cl
=item
->data
.closure
;
224 if (cl
->type
!=CL_LOG
) {
225 cfgfatal(item
->loc
,"init_log","closure is not a logger");
227 n
=safe_malloc(sizeof(*n
),"init_log");
233 fatal("init_log: no log");
235 r
=safe_malloc(sizeof(*r
), "init_log");
251 static string_t months
[]={
252 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
254 static void logfile_vlog(void *sst
, int class, char *message
, va_list args
)
256 struct logfile
*st
=sst
;
260 if (secnet_is_daemon
) {
261 if (class&st
->level
) {
264 fprintf(st
->f
,"%s %2d %02d:%02d:%02d ",
265 months
[tm
->tm_mon
],tm
->tm_mday
,tm
->tm_hour
,tm
->tm_min
,
267 vfprintf(st
->f
,message
,args
);
272 vMessage(class,message
,args
);
277 static void logfile_log(void *state
, int priority
, char *message
, ...)
281 va_start(ap
,message
);
282 logfile_vlog(state
,priority
,message
,ap
);
286 static void logfile_phase_hook(void *sst
, uint32_t new_phase
)
288 struct logfile
*st
=sst
;
292 f
=fopen(st
->logfile
,"a");
293 if (!f
) fatal_perror("logfile (%s:%d): cannot open \"%s\"",
294 st
->loc
.file
,st
->loc
.line
,st
->logfile
);
299 static struct flagstr message_class_table
[]={
300 { "debug-config", M_DEBUG_CONFIG
},
301 { "debug-phase", M_DEBUG_PHASE
},
302 { "debug", M_DEBUG
},
303 { "all-debug", M_DEBUG
|M_DEBUG_PHASE
|M_DEBUG_CONFIG
},
305 { "notice", M_NOTICE
},
306 { "warning", M_WARNING
},
307 { "error", M_ERROR
},
308 { "security", M_SECURITY
},
309 { "fatal", M_FATAL
},
310 { "default", M_WARNING
|M_ERROR
|M_SECURITY
|M_FATAL
},
311 { "verbose", M_INFO
|M_NOTICE
|M_WARNING
|M_ERROR
|M_SECURITY
|M_FATAL
},
312 { "quiet", M_FATAL
},
316 static list_t
*logfile_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
323 /* We should defer opening the logfile until the getresources
324 phase. We should defer writing into the logfile until after we
327 st
=safe_malloc(sizeof(*st
),"logfile_apply");
328 st
->cl
.description
="logfile";
331 st
->cl
.interface
=&st
->ops
;
333 st
->ops
.log
=logfile_log
;
334 st
->ops
.vlog
=logfile_vlog
;
338 item
=list_elem(args
,0);
339 if (!item
|| item
->type
!=t_dict
) {
340 cfgfatal(loc
,"logfile","argument must be a dictionary\n");
342 dict
=item
->data
.dict
;
344 st
->logfile
=dict_read_string(dict
,"filename",True
,"logfile",loc
);
345 st
->level
=string_list_to_word(dict_lookup(dict
,"class"),
346 message_class_table
,"logfile");
348 add_hook(PHASE_GETRESOURCES
,logfile_phase_hook
,st
);
350 return new_closure(&st
->cl
);
361 static int msgclass_to_syslogpriority(uint32_t m
)
364 case M_DEBUG_CONFIG
: return LOG_DEBUG
;
365 case M_DEBUG_PHASE
: return LOG_DEBUG
;
366 case M_DEBUG
: return LOG_DEBUG
;
367 case M_INFO
: return LOG_INFO
;
368 case M_NOTICE
: return LOG_NOTICE
;
369 case M_WARNING
: return LOG_WARNING
;
370 case M_ERROR
: return LOG_ERR
;
371 case M_SECURITY
: return LOG_CRIT
;
372 case M_FATAL
: return LOG_EMERG
;
373 default: return LOG_NOTICE
;
377 static void syslog_vlog(void *sst
, int class, char *message
,
380 struct syslog
*st
=sst
;
383 vsyslog(msgclass_to_syslogpriority(class),message
,args
);
385 vMessage(class,message
,args
);
390 static void syslog_log(void *sst
, int priority
, char *message
, ...)
394 va_start(ap
,message
);
395 syslog_vlog(sst
,priority
,message
,ap
);
399 static struct flagstr syslog_facility_table
[]={
400 { "authpriv", LOG_AUTHPRIV
},
401 { "cron", LOG_CRON
},
402 { "daemon", LOG_DAEMON
},
403 { "kern", LOG_KERN
},
404 { "local0", LOG_LOCAL0
},
405 { "local1", LOG_LOCAL1
},
406 { "local2", LOG_LOCAL2
},
407 { "local3", LOG_LOCAL3
},
408 { "local4", LOG_LOCAL4
},
409 { "local5", LOG_LOCAL5
},
410 { "local6", LOG_LOCAL6
},
411 { "local7", LOG_LOCAL7
},
413 { "mail", LOG_MAIL
},
414 { "news", LOG_NEWS
},
415 { "syslog", LOG_SYSLOG
},
416 { "user", LOG_USER
},
417 { "uucp", LOG_UUCP
},
421 static void syslog_phase_hook(void *sst
, uint32_t newphase
)
423 struct syslog
*st
=sst
;
426 openlog(st
->ident
,0,st
->facility
);
431 static list_t
*syslog_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
439 st
=safe_malloc(sizeof(*st
),"syslog_apply");
440 st
->cl
.description
="syslog";
443 st
->cl
.interface
=&st
->ops
;
445 st
->ops
.log
=syslog_log
;
446 st
->ops
.vlog
=syslog_vlog
;
448 item
=list_elem(args
,0);
449 if (!item
|| item
->type
!=t_dict
)
450 cfgfatal(loc
,"syslog","parameter must be a dictionary\n");
453 st
->ident
=dict_read_string(d
, "ident", False
, "syslog", loc
);
454 facstr
=dict_read_string(d
, "facility", True
, "syslog", loc
);
455 st
->facility
=string_to_word(facstr
,loc
,
456 syslog_facility_table
,"syslog");
458 add_hook(PHASE_GETRESOURCES
,syslog_phase_hook
,st
);
460 return new_closure(&st
->cl
);
463 char *safe_strdup(char *s
, char *message
)
468 fatal_perror(message
);
473 void *safe_malloc(size_t size
, char *message
)
478 fatal_perror(message
);
483 /* Convert a buffer into its MP_INT representation */
484 void read_mpbin(MP_INT
*a
, uint8_t *bin
, int binsize
)
489 buff
=safe_malloc(binsize
*2 + 1,"read_mpbin");
491 for (i
=0; i
<binsize
; i
++) {
492 buff
[i
*2]=hexdigits
[(bin
[i
] & 0xf0) >> 4];
493 buff
[i
*2+1]=hexdigits
[(bin
[i
] & 0xf)];
497 mpz_set_str(a
, buff
, 16);
501 /* Convert a MP_INT into a hex string */
502 char *write_mpstring(MP_INT
*a
)
506 buff
=safe_malloc(mpz_sizeinbase(a
,16)+2,"write_mpstring");
507 mpz_get_str(buff
, 16, a
);
511 static uint8_t hexval(uint8_t c
)
540 /* Convert a MP_INT into a buffer; return length; truncate if necessary */
541 uint32_t write_mpbin(MP_INT
*a
, uint8_t *buffer
, uint32_t buflen
)
546 if (buflen
==0) return 0;
547 hb
=write_mpstring(a
);
552 /* The number starts with a half-byte */
553 buffer
[i
++]=hexval(hb
[j
++]);
555 for (; hb
[j
] && i
<buflen
; i
++) {
556 buffer
[i
]=(hexval(hb
[j
])<<4)|hexval(hb
[j
+1]);
563 bool_t
subnet_match(struct subnet
*s
, uint32_t address
)
565 return (s
->prefix
==(address
&s
->mask
));
568 bool_t
subnet_matches_list(struct subnet_list
*list
, uint32_t address
)
571 for (i
=0; i
<list
->entries
; i
++) {
572 if (list
->list
[i
].prefix
== (address
&list
->list
[i
].mask
)) return True
;
577 bool_t
subnets_intersect(struct subnet a
, struct subnet b
)
579 uint32_t mask
=a
.mask
&b
.mask
;
580 return ((a
.prefix
&mask
)==(b
.prefix
&mask
));
583 bool_t
subnet_intersects_with_list(struct subnet a
, struct subnet_list
*b
)
587 for (i
=0; i
<b
->entries
; i
++) {
588 if (subnets_intersect(a
,b
->list
[i
])) return True
;
593 bool_t
subnet_lists_intersect(struct subnet_list
*a
, struct subnet_list
*b
)
596 for (i
=0; i
<a
->entries
; i
++) {
597 if (subnet_intersects_with_list(a
->list
[i
],b
)) return True
;
602 /* The string buffer must be at least 16 bytes long */
603 string_t
ipaddr_to_string(uint32_t addr
)
608 s
=safe_malloc(16,"ipaddr_to_string");
613 snprintf(s
, 16, "%d.%d.%d.%d", a
, b
, c
, d
);
617 string_t
subnet_to_string(struct subnet
*sn
)
619 uint32_t mask
=sn
->mask
, addr
=sn
->prefix
;
624 s
=safe_malloc(19,"subnet_to_string");
629 for (i
=0; mask
; i
++) {
633 fatal("subnet_to_string: invalid subnet structure!\n");
635 snprintf(s
, 19, "%d.%d.%d.%d/%d", a
, b
, c
, d
, sn
->len
);
639 int sys_cmd(const char *path
, char *arg
, ...)
648 /* Parent -> wait for child */
653 /* Child -> exec command */
656 while ((args
[i
++]=va_arg(ap
,char *)));
661 fatal_perror("sys_cmd(%s,%s,...)");
668 static char *phases
[NR_PHASES
]={
673 "PHASE_GETRESOURCES",
679 void enter_phase(uint32_t new_phase
)
681 struct phase_hook
*i
;
683 if (hooks
[new_phase
])
684 Message(M_DEBUG_PHASE
,"Running hooks for %s...\n", phases
[new_phase
]);
685 current_phase
=new_phase
;
687 for (i
=hooks
[new_phase
]; i
; i
=i
->next
)
688 i
->fn(i
->state
, new_phase
);
689 Message(M_DEBUG_PHASE
,"Now in %s\n",phases
[new_phase
]);
692 bool_t
add_hook(uint32_t phase
, hook_fn
*fn
, void *state
)
694 struct phase_hook
*h
;
696 h
=safe_malloc(sizeof(*h
),"add_hook");
699 h
->next
=hooks
[phase
];
704 bool_t
remove_hook(uint32_t phase
, hook_fn
*fn
, void *state
)
706 fatal("remove_hook: not implemented\n");
711 void log(struct log_if
*lf
, int priority
, char *message
, ...)
715 va_start(ap
,message
);
716 lf
->vlog(lf
->st
,priority
,message
,ap
);
722 struct buffer_if ops
;
725 void buffer_assert_free(struct buffer_if
*buffer
, string_t file
, uint32_t line
)
728 fatal("BUF_ASSERT_FREE, %s line %d, owned by %s\n",
729 file
,line
,buffer
->owner
);
733 void buffer_assert_used(struct buffer_if
*buffer
, string_t file
, uint32_t line
)
736 fatal("BUF_ASSERT_USED, %s line %d, last owned by %s\n",
737 file
,line
,buffer
->owner
);
741 void buffer_init(struct buffer_if
*buffer
, uint32_t max_start_pad
)
743 buffer
->start
=buffer
->base
+max_start_pad
;
747 void *buf_append(struct buffer_if
*buf
, uint32_t amount
) {
749 p
=buf
->start
+ buf
->size
;
754 void *buf_prepend(struct buffer_if
*buf
, uint32_t amount
) {
756 return buf
->start
-=amount
;
759 void *buf_unappend(struct buffer_if
*buf
, uint32_t amount
) {
760 if (buf
->size
< amount
) return 0;
761 return buf
->start
+(buf
->size
-=amount
);
764 void *buf_unprepend(struct buffer_if
*buf
, uint32_t amount
) {
772 /* Append a two-byte length and the string to the buffer. Length is in
773 network byte order. */
774 void buf_append_string(struct buffer_if
*buf
, string_t s
)
779 buf_append_uint16(buf
,len
);
780 memcpy(buf_append(buf
,len
),s
,len
);
783 void buffer_new(struct buffer_if
*buf
, uint32_t len
)
793 buf
->base
=safe_malloc(len
,"buffer_new");
796 static list_t
*buffer_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
802 bool_t lockdown
=False
;
803 uint32_t len
=DEFAULT_BUFFER_SIZE
;
805 st
=safe_malloc(sizeof(*st
),"buffer_apply");
806 st
->cl
.description
="buffer";
807 st
->cl
.type
=CL_BUFFER
;
809 st
->cl
.interface
=&st
->ops
;
811 /* First argument, if present, is buffer length */
812 item
=list_elem(args
,0);
814 if (item
->type
!=t_number
) {
815 cfgfatal(st
->ops
.loc
,"buffer","first parameter must be a "
816 "number (buffer size)\n");
818 len
=item
->data
.number
;
819 if (len
<MIN_BUFFER_SIZE
) {
820 cfgfatal(st
->ops
.loc
,"buffer","ludicrously small buffer size\n");
822 if (len
>MAX_BUFFER_SIZE
) {
823 cfgfatal(st
->ops
.loc
,"buffer","ludicrously large buffer size\n");
826 /* Second argument, if present, is a dictionary */
827 item
=list_elem(args
,1);
829 if (item
->type
!=t_dict
) {
830 cfgfatal(st
->ops
.loc
,"buffer","second parameter must be a "
833 dict
=item
->data
.dict
;
834 lockdown
=dict_read_bool(dict
,"lockdown",False
,"buffer",st
->ops
.loc
,
838 buffer_new(&st
->ops
,len
);
840 /* XXX mlock the buffer if possible */
843 return new_closure(&st
->cl
);
846 init_module util_module
;
847 void util_module(dict_t
*dict
)
849 add_closure(dict
,"logfile",logfile_apply
);
850 add_closure(dict
,"syslog",syslog_apply
);
851 add_closure(dict
,"sysbuffer",buffer_apply
);