/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Martin Sch�tte.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SYSLOGD_H_
#define SYSLOGD_H_
/*
* hold common data structures and prototypes
* for syslogd.c and tls.c
*
*/
#include <sys/cdefs.h>
#define MAXLINE 1024 /* maximum line length */
#define MAXSVLINE 120 /* maximum saved line length */
#define DEFUPRI (LOG_USER|LOG_NOTICE)
#define DEFSPRI (LOG_KERN|LOG_NOTICE)
#define TIMERINTVL 30 /* interval for checking flush, mark */
#define TTYMSGTIME 1 /* timeout passed to ttymsg */
#ifndef HAVE_DEHUMANIZE_NUMBER /* not in my 4.0-STABLE yet */
extern int dehumanize_number(const char *str, int64_t *size);
#endif /* !HAVE_DEHUMANIZE_NUMBER */
/* assumption:
* - malloc()/calloc() only fails if not enough memory available
* - once init() has set up all global variables etc.
* the bulk of available memory is used for buffers
* and can be freed if necessary
*/
#define MALLOC(ptr, size) do { \
while(!(ptr = malloc(size))) { \
DPRINTF(D_MEM, "Unable to allocate memory"); \
message_allqueues_purge(); \
} \
DPRINTF(D_MEM2, "MALLOC(%s@%p, %zu)\n", #ptr, ptr, size); \
} while (0)
/* define strlen(NULL) to be 0 */
#define SAFEstrlen(x) ((x) ? strlen(x) : 0)
/* shorthand to block/restore signals for the duration of one function */
#define BLOCK_SIGNALS(omask, newmask) do { \
sigemptyset(&newmask); \
sigaddset(&newmask, SIGHUP); \
sigaddset(&newmask, SIGALRM); \
sigprocmask(SIG_BLOCK, &newmask, &omask); \
} while (0)
/* small optimization to call send_queue() only if queue has elements */
#define SEND_QUEUE(f) do { \
if ((f)->f_qelements) \
send_queue(0, 0, f); \
} while (0)
#define MAXUNAMES 20 /* maximum number of user names */
#define BSD_TIMESTAMPLEN (14+1)
#define MAX_TIMESTAMPLEN (31+1)
/* a pair of a socket and an associated event object */
struct socketEvent {
int fd;
int af;
struct event *ev;
};
/*
* Flags to logmsg().
*/
#define IGN_CONS 0x001 /* don't print on console */
#define SYNC_FILE 0x002 /* do fsync on file after printing */
#define ADDDATE 0x004 /* add a date to the message */
#define MARK 0x008 /* this message is a mark */
#define ISKERNEL 0x010 /* kernel generated message */
#define BSDSYSLOG 0x020 /* line in traditional BSD Syslog format */
#define SIGN_MSG 0x040 /* syslog-sign data, not signed again */
/*
* This structure represents the files that will have log
* copies printed.
* We require f_file to be valid if f_type is F_FILE, F_CONSOLE, F_TTY,
* or if f_type is F_PIPE and f_pid > 0.
*/
struct filed {
struct filed *f_next; /* next in linked list */
short f_type; /* entry type, see below */
short f_file; /* file descriptor */
time_t f_time; /* time this was last written */
char *f_host; /* host from which to record */
u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */
u_char f_pcmp[LOG_NFACILITIES+1]; /* compare priority */
#define PRI_LT 0x1
#define PRI_EQ 0x2
#define PRI_GT 0x4
char *f_program; /* program this applies to */
union {
char f_uname[MAXUNAMES][UT_NAMESIZE+1];
struct {
char f_hname[MAXHOSTNAMELEN];
struct addrinfo *f_addr;
} f_forw; /* UDP forwarding address */
#ifndef DISABLE_TLS
struct {
SSL *ssl; /* SSL object */
struct tls_conn_settings *tls_conn; /* certificate info */
} f_tls; /* TLS forwarding address */
#endif /* !DISABLE_TLS */
char f_fname[MAXPATHLEN];
struct {
char f_pname[MAXPATHLEN];
pid_t f_pid;
} f_pipe;
} f_un;
#ifndef DISABLE_SIGN
struct signature_group_t *f_sg; /* one signature group */
#endif /* !DISABLE_SIGN */
struct buf_queue_head f_qhead; /* undelivered msgs queue */
size_t f_qelements; /* elements in queue */
size_t f_qsize; /* size of queue in bytes */
struct buf_msg *f_prevmsg; /* last message logged */
struct event *f_sq_event; /* timer for send_queue() */
int f_prevcount; /* repetition cnt of prevmsg */
int f_repeatcount; /* number of "repeated" msgs */
int f_lasterror; /* last error on writev() */
int f_flags; /* file-specific flags */
#define FFLAG_SYNC 0x01 /* for F_FILE: fsync after every msg */
#define FFLAG_FULL 0x02 /* for F_FILE | F_PIPE: write PRI header */
#define FFLAG_SIGN 0x04 /* for syslog-sign with SG="3":
* sign the messages to this destination */
};
#ifndef DISABLE_TLS
/* linked list for allowed TLS peer credentials
* (one for fingerprint, one for cert-files)
*/
SLIST_HEAD(peer_cred_head, peer_cred);
struct peer_cred {
SLIST_ENTRY(peer_cred) entries;
char *data;
};
/* config options for TLS server-side */
struct tls_global_options_t {
SSL_CTX *global_TLS_CTX;
struct peer_cred_head fprint_head; /* trusted client fingerprints */
struct peer_cred_head cert_head; /* trusted client cert files */
char *keyfile; /* file with private key */
char *certfile; /* file with own certificate */
char *CAfile; /* file with CA certificate */
char *CAdir; /* alternative: path to directory with CA certs */
char *x509verify; /* level of peer verification */
char *bindhost; /* hostname/IP to bind to */
char *bindport; /* port/service to bind to */
char *server; /* if !NULL: do not listen to incoming TLS */
char *gen_cert; /* if !NULL: generate self-signed certificate */
};
/* TLS needs three sets of sockets:
* - listening sockets: a fixed size array TLS_Listen_Set, just like finet for UDP.
* - outgoing connections: managed as part of struct filed.
* - incoming connections: variable sized, thus a linked list TLS_Incoming.
*/
/* every connection has its own input buffer with status
* variables for message reading */
SLIST_HEAD(TLS_Incoming, TLS_Incoming_Conn);
struct TLS_Incoming_Conn {
SLIST_ENTRY(TLS_Incoming_Conn) entries;
struct tls_conn_settings *tls_conn;
int socket;
char *inbuf; /* input buffer */
size_t inbuflen;
size_t cur_msg_len; /* length of current msg */
size_t cur_msg_start; /* beginning of current msg */
size_t read_pos; /* ring buffer position to write to */
size_t errorcount; /* to close faulty connections */
bool closenow; /* close connection as soon as buffer processed */
bool dontsave; /* for receiving oversized messages w/o saving them */
};