1 /* $NetBSD: syslogd.h,v 1.2 2008/11/07 07:36:38 minskim Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
41 * hold common data structures and prototypes
42 * for syslogd.c and tls.c
46 #include <sys/cdefs.h>
47 #define MAXLINE 1024 /* maximum line length */
48 #define MAXSVLINE 120 /* maximum saved line length */
49 #define DEFUPRI (LOG_USER|LOG_NOTICE)
50 #define DEFSPRI (LOG_KERN|LOG_NOTICE)
51 #define TIMERINTVL 30 /* interval for checking flush, mark */
52 #define TTYMSGTIME 1 /* timeout passed to ttymsg */
54 #include <sys/param.h>
55 #include <sys/socket.h>
56 #include <sys/sysctl.h>
57 #include <sys/types.h>
60 #include <sys/queue.h>
61 #include <netinet/in.h>
62 #include <sys/event.h>
81 #ifdef __NetBSD_Version__
83 #include "utmpentry.h"
84 #endif /* __NetBSD_Version__ */
85 #ifdef __FreeBSD_version
90 #endif /* __FreeBSD_version */
93 #include <netinet/tcp.h>
94 #include <openssl/ssl.h>
95 #endif /* !DISABLE_TLS */
97 #include <sys/stdint.h>
98 #include <sys/resource.h>
100 #include "pathnames.h"
101 #include <sys/syslog.h>
103 /* some differences between the BSDs */
104 #ifdef __FreeBSD_version
106 #define _PATH_UNIX "kernel"
107 #define HAVE_STRNDUP 0
108 #endif /* __FreeBSD_version */
110 #ifdef __NetBSD_Version__
111 #define HAVE_STRNDUP 1
112 #define HAVE_DEHUMANIZE_NUMBER 1
113 #endif /* __NetBSD_Version__ */
115 #ifndef HAVE_DEHUMANIZE_NUMBER /* not in my 4.0-STABLE yet */
116 extern int dehumanize_number(const char *str
, int64_t *size
);
117 #endif /* !HAVE_DEHUMANIZE_NUMBER */
120 char *strndup(const char *str
, size_t n
);
121 #endif /* !HAVE_STRNDUP */
127 #define FDMASK(fd) (1 << (fd))
129 #define A_CNT(x) (sizeof((x)) / sizeof((x)[0]))
131 /* debug messages with categories */
133 #define D_CALL 1 /* function calls */
134 #define D_DATA 2 /* syslog message reading/formatting */
135 #define D_NET 4 /* sockets/network */
136 #define D_FILE 8 /* local files */
137 #define D_TLS 16 /* TLS */
138 #define D_PARSE 32 /* configuration/parsing */
139 #define D_EVENT 64 /* libevent */
140 #define D_BUFFER 128 /* message queues */
141 #define D_MEM 256 /* malloc/free */
142 #define D_MEM2 1024 /* every single malloc/free */
143 #define D_SIGN 2048 /* -sign */
144 #define D_MISC 4096 /* everything else */
145 #define D_ALL (D_CALL | D_DATA | D_NET | D_FILE | D_TLS | D_PARSE | \
146 D_EVENT | D_BUFFER | D_MEM | D_MEM2 | D_SIGN | D_MISC)
147 #define D_DEFAULT (D_CALL | D_NET | D_FILE | D_TLS | D_MISC)
150 /* build with -DNDEBUG to remove all assert()s and DPRINTF()s */
152 #define DPRINTF(x, ...) (void)0
154 #define DPRINTF(x, ...) /*LINTED null effect */(void)(Debug & (x) \
155 ? (printf("%s:%s:%s:%.4d\t", make_timestamp(NULL, true), \
156 __FILE__, __func__, __LINE__), printf(__VA_ARGS__)) : 0)
159 /* shortcuts for libevent */
160 #define EVENT_ADD(x) do { \
161 DPRINTF(D_EVENT, "event_add(%s@%p)\n", #x, x); \
162 if (event_add(x, NULL) == -1) { \
163 DPRINTF(D_EVENT, "Failure in event_add()\n"); \
165 } while (/*CONSTCOND*/0)
166 #define RETRYEVENT_ADD(x) do { \
167 struct timeval _tv; \
169 _tv.tv_usec = TLS_RETRY_EVENT_USEC; \
170 DPRINTF(D_EVENT, "retryevent_add(%s@%p)\n", #x, x); \
171 if (event_add(x, &_tv) == -1) { \
172 DPRINTF(D_EVENT, "Failure in event_add()\n"); \
174 } while (/*CONSTCOND*/0)
175 #define DEL_EVENT(x) do { \
176 DPRINTF(D_MEM2, "DEL_EVENT(%s@%p)\n", #x, x); \
177 if ((x) && (event_del(x) == -1)) { \
178 DPRINTF(D_EVENT, "Failure in event_del()\n"); \
180 } while (/*CONSTCOND*/0)
182 /* safe calls to free() */
183 #define FREEPTR(x) if (x) { \
184 DPRINTF(D_MEM2, "free(%s@%p)\n", #x, x); \
186 #define FREE_SSL(x) if (x) { \
187 DPRINTF(D_MEM2, "SSL_free(%s@%p)\n", #x, x); \
188 SSL_free(x); x = NULL; }
189 #define FREE_SSL_CTX(x) if (x) { \
190 DPRINTF(D_MEM2, "SSL_CTX_free(%s@%p)\n", #x, x); \
191 SSL_CTX_free(x); x = NULL; }
193 /* reference counting macros for buffers */
194 #define NEWREF(x) ((x) ? (DPRINTF(D_BUFFER, "inc refcount of " #x \
195 " @ %p: %zu --> %zu\n", (x), (x)->refcount, \
196 (x)->refcount + 1), (x)->refcount++, (x))\
197 : (DPRINTF(D_BUFFER, "inc refcount of NULL!\n"), NULL))
198 #define DELREF(x) /*LINTED null effect*/(void)((x) ? (DPRINTF(D_BUFFER, "dec refcount of " #x \
199 " @ %p: %zu --> %zu\n", (x), (x)->refcount, \
200 (x)->refcount - 1), buf_msg_free(x), NULL) \
201 : (DPRINTF(D_BUFFER, "dec refcount of NULL!\n"), NULL))
204 * - malloc()/calloc() only fails if not enough memory available
205 * - once init() has set up all global variables etc.
206 * the bulk of available memory is used for buffers
207 * and can be freed if necessary
209 #define MALLOC(ptr, size) do { \
210 while(!(ptr = malloc(size))) { \
211 DPRINTF(D_MEM, "Unable to allocate memory"); \
212 message_allqueues_purge(); \
214 DPRINTF(D_MEM2, "MALLOC(%s@%p, %zu)\n", #ptr, ptr, size); \
215 } while (/*CONSTCOND*/0)
217 #define CALLOC(ptr, size) do { \
218 while(!(ptr = calloc(1, size))) { \
219 DPRINTF(D_MEM, "Unable to allocate memory"); \
220 message_allqueues_purge(); \
222 DPRINTF(D_MEM2, "CALLOC(%s@%p, %zu)\n", #ptr, ptr, size); \
223 } while (/*CONSTCOND*/0)
225 /* define strlen(NULL) to be 0 */
226 #define SAFEstrlen(x) ((x) ? strlen(x) : 0)
228 /* shorthand to block/restore signals for the duration of one function */
229 #define BLOCK_SIGNALS(omask, newmask) do { \
230 sigemptyset(&newmask); \
231 sigaddset(&newmask, SIGHUP); \
232 sigaddset(&newmask, SIGALRM); \
233 sigprocmask(SIG_BLOCK, &newmask, &omask); \
234 } while (/*CONSTCOND*/0)
236 #define RESTORE_SIGNALS(omask) sigprocmask(SIG_SETMASK, &omask, NULL)
238 /* small optimization to call send_queue() only if queue has elements */
239 #define SEND_QUEUE(f) do { \
240 if ((f)->f_qelements) \
241 send_queue(0, 0, f); \
242 } while (/*CONSTCOND*/0)
244 #define MAXUNAMES 20 /* maximum number of user names */
245 #define BSD_TIMESTAMPLEN 14+1
246 #define MAX_TIMESTAMPLEN 31+1
248 /* maximum field lengths in syslog-protocol */
251 #define APPNAME_MAX 48
252 #define PROCID_MAX 128
254 /* longest possible header length */
255 #define HEADER_LEN_MAX (PRI_MAX + 1 + 1 + MAX_TIMESTAMPLEN + 1 + HOST_MAX \
256 + 1 + APPNAME_MAX + 1 + PROCID_MAX + 1 + MSGID_MAX)
258 /* allowed number of priorities by IETF standards */
259 #define IETF_NUM_PRIVALUES 192
261 /* check if message with fac/sev belogs to a destination f */
262 #define MATCH_PRI(f, fac, sev) \
263 ( (((f)->f_pcmp[fac] & PRI_EQ) && ((f)->f_pmask[fac] == (sev))) \
264 ||(((f)->f_pcmp[fac] & PRI_LT) && ((f)->f_pmask[fac] < (sev))) \
265 ||(((f)->f_pcmp[fac] & PRI_GT) && ((f)->f_pmask[fac] > (sev))) \
268 /* shorthand to test Byte Order Mark which indicates UTF-8 content */
269 #define IS_BOM(p) ( \
270 (p)[0] != '\0' && (unsigned char)(p)[0] == (unsigned char)0xEF && \
271 (p)[1] != '\0' && (unsigned char)(p)[1] == (unsigned char)0xBB && \
272 (p)[2] != '\0' && (unsigned char)(p)[2] == (unsigned char)0xBF)
274 /* message buffer container used for processing, formatting, and queueing */
285 char *sd
; /* structured data */
286 char *msg
; /* message content */
287 char *msgorig
; /* in case we advance *msg beyond header fields
288 we still want to free() the original ptr */
289 size_t msglen
; /* strlen(msg) */
290 size_t msgsize
; /* allocated memory size */
291 size_t tlsprefixlen
; /* bytes for the TLS length prefix */
292 size_t prilen
; /* bytes for priority and version */
295 /* queue of messages */
298 STAILQ_ENTRY(buf_queue
) entries
;
300 STAILQ_HEAD(buf_queue_head
, buf_queue
);
302 /* a pair of a socket and an associated event object */
311 #define IGN_CONS 0x001 /* don't print on console */
312 #define SYNC_FILE 0x002 /* do fsync on file after printing */
313 #define ADDDATE 0x004 /* add a date to the message */
314 #define MARK 0x008 /* this message is a mark */
315 #define ISKERNEL 0x010 /* kernel generated message */
316 #define BSDSYSLOG 0x020 /* line in traditional BSD Syslog format */
317 #define SIGN_MSG 0x040 /* syslog-sign data, not signed again */
319 /* strategies for message_queue_purge() */
320 #define PURGE_OLDEST 1
321 #define PURGE_BY_PRIORITY 2
324 * This structure represents the files that will have log
326 * We require f_file to be valid if f_type is F_FILE, F_CONSOLE, F_TTY,
327 * or if f_type is F_PIPE and f_pid > 0.
331 struct filed
*f_next
; /* next in linked list */
332 short f_type
; /* entry type, see below */
333 short f_file
; /* file descriptor */
334 time_t f_time
; /* time this was last written */
335 char *f_host
; /* host from which to record */
336 u_char f_pmask
[LOG_NFACILITIES
+1]; /* priority mask */
337 u_char f_pcmp
[LOG_NFACILITIES
+1]; /* compare priority */
341 char *f_program
; /* program this applies to */
343 char f_uname
[MAXUNAMES
][UT_NAMESIZE
+1];
345 char f_hname
[MAXHOSTNAMELEN
];
346 struct addrinfo
*f_addr
;
347 } f_forw
; /* UDP forwarding address */
350 SSL
*ssl
; /* SSL object */
351 struct tls_conn_settings
*tls_conn
; /* certificate info */
352 } f_tls
; /* TLS forwarding address */
353 #endif /* !DISABLE_TLS */
354 char f_fname
[MAXPATHLEN
];
356 char f_pname
[MAXPATHLEN
];
361 struct signature_group_t
*f_sg
; /* one signature group */
362 #endif /* !DISABLE_SIGN */
363 struct buf_queue_head f_qhead
; /* undelivered msgs queue */
364 size_t f_qelements
; /* elements in queue */
365 size_t f_qsize
; /* size of queue in bytes */
366 struct buf_msg
*f_prevmsg
; /* last message logged */
367 struct event
*f_sq_event
; /* timer for send_queue() */
368 int f_prevcount
; /* repetition cnt of prevmsg */
369 int f_repeatcount
; /* number of "repeated" msgs */
370 int f_lasterror
; /* last error on writev() */
371 int f_flags
; /* file-specific flags */
372 #define FFLAG_SYNC 0x01 /* for F_FILE: fsync after every msg */
373 #define FFLAG_FULL 0x02 /* for F_FILE | F_PIPE: write PRI header */
374 #define FFLAG_SIGN 0x04 /* for syslog-sign with SG="3":
375 * sign the messages to this destination */
380 /* linked list for allowed TLS peer credentials
381 * (one for fingerprint, one for cert-files)
383 SLIST_HEAD(peer_cred_head
, peer_cred
);
385 SLIST_ENTRY(peer_cred
) entries
;
389 /* config options for TLS server-side */
390 struct tls_global_options_t
{
391 SSL_CTX
*global_TLS_CTX
;
392 struct peer_cred_head fprint_head
; /* trusted client fingerprints */
393 struct peer_cred_head cert_head
; /* trusted client cert files */
394 char *keyfile
; /* file with private key */
395 char *certfile
; /* file with own certificate */
396 char *CAfile
; /* file with CA certificate */
397 char *CAdir
; /* alternative: path to directory with CA certs */
398 char *x509verify
; /* level of peer verification */
399 char *bindhost
; /* hostname/IP to bind to */
400 char *bindport
; /* port/service to bind to */
401 char *server
; /* if !NULL: do not listen to incoming TLS */
402 char *gen_cert
; /* if !NULL: generate self-signed certificate */
405 /* TLS needs three sets of sockets:
406 * - listening sockets: a fixed size array TLS_Listen_Set, just like finet for UDP.
407 * - outgoing connections: managed as part of struct filed.
408 * - incoming connections: variable sized, thus a linked list TLS_Incoming.
410 /* every connection has its own input buffer with status
411 * variables for message reading */
412 SLIST_HEAD(TLS_Incoming
, TLS_Incoming_Conn
);
414 struct TLS_Incoming_Conn
{
415 SLIST_ENTRY(TLS_Incoming_Conn
) entries
;
416 struct tls_conn_settings
*tls_conn
;
418 char *inbuf
; /* input buffer */
420 size_t cur_msg_len
; /* length of current msg */
421 size_t cur_msg_start
; /* beginning of current msg */
422 size_t read_pos
; /* ring buffer position to write to */
423 size_t errorcount
; /* to close faulty connections */
424 bool closenow
; /* close connection as soon as buffer processed */
425 bool dontsave
; /* for receiving oversized messages w/o saving them */
428 #endif /* !DISABLE_TLS */
430 #endif /*SYSLOGD_H_*/