1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1983, 1988, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 /*- 32 * SPDX-License-Identifier: BSD-2-Clause 33 * 34 * Copyright (c) 2018 Prodrive Technologies, https://prodrive-technologies.com/ 35 * Author: Ed Schouten <ed@FreeBSD.org> 36 * Copyright (c) 2023 The FreeBSD Foundation 37 * 38 * This software was developed by Jake Freeland <jfree@FreeBSD.org> 39 * under sponsorship from the FreeBSD Foundation. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63 #ifndef _SYSLOGD_H_ 64 #define _SYSLOGD_H_ 65 66 #include <sys/param.h> 67 #include <sys/nv.h> 68 #include <sys/queue.h> 69 #include <sys/time.h> 70 #include <sys/uio.h> 71 72 #define SYSLOG_NAMES 73 #include <sys/syslog.h> 74 75 #include <regex.h> 76 #include <stdbool.h> 77 #include <stdio.h> 78 79 #include "ttymsg.h" 80 81 #define MAXLINE 8192 /* maximum line length */ 82 #define MAXSVLINE MAXLINE /* maximum saved line length */ 83 #define MAXUNAMES 20 /* maximum number of user names */ 84 85 /* Timestamps of log entries. */ 86 struct logtime { 87 struct tm tm; 88 suseconds_t usec; 89 }; 90 91 enum filt_proptype { 92 FILT_PROP_NOOP, 93 FILT_PROP_MSG, 94 FILT_PROP_HOSTNAME, 95 FILT_PROP_PROGNAME, 96 }; 97 98 enum filt_cmptype { 99 FILT_CMP_CONTAINS, 100 FILT_CMP_EQUAL, 101 FILT_CMP_STARTS, 102 FILT_CMP_REGEX, 103 }; 104 105 /* 106 * This structure holds a property-based filter 107 */ 108 struct prop_filter { 109 enum filt_proptype prop_type; 110 enum filt_cmptype cmp_type; 111 uint8_t cmp_flags; 112 #define FILT_FLAG_EXCLUDE (1 << 0) 113 #define FILT_FLAG_EXTENDED (1 << 1) 114 #define FILT_FLAG_ICASE (1 << 2) 115 char *pflt_strval; 116 regex_t *pflt_re; 117 }; 118 119 enum f_type { 120 F_UNUSED, /* unused entry */ 121 F_FILE, /* regular file */ 122 F_TTY, /* terminal */ 123 F_CONSOLE, /* console terminal */ 124 F_FORW, /* remote machine */ 125 F_USERS, /* list of users */ 126 F_WALL, /* everyone logged on */ 127 F_PIPE, /* pipe to program */ 128 }; 129 130 struct forw_addr { 131 struct sockaddr_storage laddr; 132 struct sockaddr_storage raddr; 133 }; 134 135 /* 136 * This structure represents the files that will have log 137 * copies printed. 138 * We require f_file to be valid if f_type is F_FILE, F_CONSOLE, F_TTY 139 * or if f_type is F_PIPE and f_pid > 0. 140 */ 141 struct filed { 142 enum f_type f_type; 143 144 /* Used for filtering. */ 145 char f_host[MAXHOSTNAMELEN]; /* host from which to recd. */ 146 char f_program[MAXPATHLEN]; /* program this applies to */ 147 struct prop_filter *f_prop_filter; /* property-based filter */ 148 u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */ 149 u_char f_pcmp[LOG_NFACILITIES+1]; /* compare priority */ 150 #define PRI_LT 0x1 151 #define PRI_EQ 0x2 152 #define PRI_GT 0x4 153 154 /* Logging destinations. */ 155 int f_file; /* file descriptor */ 156 int f_flags; /* file-specific flags */ 157 #define FFLAG_SYNC 0x01 158 #define FFLAG_NEEDSYNC 0x02 159 union { 160 char f_uname[MAXUNAMES][MAXLOGNAME]; /* F_WALL, F_USERS */ 161 char f_fname[MAXPATHLEN]; /* F_FILE, F_CONSOLE, F_TTY */ 162 struct { 163 char f_hname[MAXHOSTNAMELEN]; 164 int *f_addr_fds; 165 size_t f_num_addr_fds; 166 struct forw_addr *f_addrs; 167 }; /* F_FORW */ 168 struct { 169 char f_pname[MAXPATHLEN]; 170 int f_procdesc; 171 struct deadq_entry *f_dq; 172 }; /* F_PIPE */ 173 }; 174 175 /* Book-keeping. */ 176 char f_prevline[MAXSVLINE]; /* last message logged */ 177 time_t f_time; /* time this was last written */ 178 struct logtime f_lasttime; /* time of last occurrence */ 179 int f_prevpri; /* pri of f_prevline */ 180 size_t f_prevlen; /* length of f_prevline */ 181 int f_prevcount; /* repetition cnt of prevline */ 182 u_int f_repeatcount; /* number of "repeated" msgs */ 183 STAILQ_ENTRY(filed) next; /* next in linked list */ 184 }; 185 186 /* 187 * List of iovecs to which entries can be appended. 188 * Used for constructing the message to be logged. 189 */ 190 struct iovlist { 191 struct iovec iov[TTYMSG_IOV_MAX]; 192 size_t iovcnt; 193 size_t totalsize; 194 }; 195 196 extern const char *ConfFile; 197 extern char LocalHostName[MAXHOSTNAMELEN]; 198 199 void closelogfiles(void); 200 void logerror(const char *); 201 int p_open(const char *, pid_t *); 202 nvlist_t *readconfigfile(const char *); 203 void wallmsg(const struct filed *, struct iovec *, const int); 204 205 #endif /* !_SYSLOGD_H_ */ 206