/src/sudo/lib/eventlog/eventlog_conf.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * SPDX-License-Identifier: ISC  | 
3  |  |  *  | 
4  |  |  * Copyright (c) 1994-1996, 1998-2020 Todd C. Miller <Todd.Miller@sudo.ws>  | 
5  |  |  *  | 
6  |  |  * Permission to use, copy, modify, and distribute this software for any  | 
7  |  |  * purpose with or without fee is hereby granted, provided that the above  | 
8  |  |  * copyright notice and this permission notice appear in all copies.  | 
9  |  |  *  | 
10  |  |  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
11  |  |  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
12  |  |  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
13  |  |  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
14  |  |  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN  | 
15  |  |  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  | 
16  |  |  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
17  |  |  *  | 
18  |  |  * Sponsored in part by the Defense Advanced Research Projects  | 
19  |  |  * Agency (DARPA) and Air Force Research Laboratory, Air Force  | 
20  |  |  * Materiel Command, USAF, under agreement number F39502-99-1-0512.  | 
21  |  |  */  | 
22  |  |  | 
23  |  | #include <config.h>  | 
24  |  |  | 
25  |  | #include <sys/types.h>  | 
26  |  | #include <sys/socket.h>  | 
27  |  | #include <sys/stat.h>  | 
28  |  | #include <sys/wait.h>  | 
29  |  | #include <netinet/in.h>  | 
30  |  |  | 
31  |  | #include <ctype.h>  | 
32  |  | #include <errno.h>  | 
33  |  | #include <fcntl.h>  | 
34  |  | #include <grp.h>  | 
35  |  | #include <locale.h>  | 
36  |  | #include <pwd.h>  | 
37  |  | #include <signal.h>  | 
38  |  | #include <stdio.h>  | 
39  |  | #include <stdlib.h>  | 
40  |  | #include <string.h>  | 
41  |  | #include <syslog.h>  | 
42  |  | #include <time.h>  | 
43  |  | #include <unistd.h>  | 
44  |  |  | 
45  |  | #include <pathnames.h>  | 
46  |  | #include <sudo_compat.h>  | 
47  |  | #include <sudo_debug.h>  | 
48  |  | #include <sudo_eventlog.h>  | 
49  |  | #include <sudo_fatal.h>  | 
50  |  | #include <sudo_gettext.h>  | 
51  |  | #include <sudo_json.h>  | 
52  |  | #include <sudo_queue.h>  | 
53  |  | #include <sudo_util.h>  | 
54  |  |  | 
55  |  | static FILE *eventlog_stub_open_log(int type, const char *logfile);  | 
56  |  | static void eventlog_stub_close_log(int type, FILE *fp);  | 
57  |  |  | 
58  |  | /* Eventlog config settings (default values). */  | 
59  |  | static struct eventlog_config evl_conf = { | 
60  |  |     EVLOG_NONE,     /* type */  | 
61  |  |     EVLOG_SUDO,     /* format */  | 
62  |  |     0,        /* file_maxlen */  | 
63  |  |     MAXSYSLOGLEN,   /* syslog_maxlen */  | 
64  |  |     LOG_NOTICE,     /* syslog_acceptpri */  | 
65  |  |     LOG_ALERT,      /* syslog_rejectpri */  | 
66  |  |     LOG_ALERT,      /* syslog_alertpri */  | 
67  |  |     ROOT_UID,     /* mailuid */  | 
68  |  |     false,      /* omit_hostname */  | 
69  |  |     _PATH_SUDO_LOGFILE,   /* logpath */  | 
70  |  |     "%h %e %T",     /* time_fmt */  | 
71  |  | #ifdef _PATH_SUDO_SENDMAIL  | 
72  |  |     _PATH_SUDO_SENDMAIL,  /* mailerpath */  | 
73  |  | #else  | 
74  |  |     NULL,     /* mailerpath (disabled) */  | 
75  |  | #endif  | 
76  |  |     "-t",     /* mailerflags */  | 
77  |  |     NULL,     /* mailfrom */  | 
78  |  |     MAILTO,     /* mailto */  | 
79  |  |     N_(MAILSUBJECT),    /* mailsub */  | 
80  |  |     eventlog_stub_open_log, /* open_log */  | 
81  |  |     eventlog_stub_close_log /* close_log */  | 
82  |  | };  | 
83  |  |  | 
84  |  | static FILE *  | 
85  |  | eventlog_stub_open_log(int type, const char *logfile)  | 
86  | 0  | { | 
87  | 0  |     debug_decl(eventlog_stub_open_log, SUDO_DEBUG_UTIL);  | 
88  | 0  |     sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,  | 
89  | 0  |   "open_log not set, using stub");  | 
90  | 0  |     debug_return_ptr(NULL);  | 
91  | 0  | }  | 
92  |  |  | 
93  |  | static void  | 
94  |  | eventlog_stub_close_log(int type, FILE *fp)  | 
95  | 0  | { | 
96  | 0  |     debug_decl(eventlog_stub_close_log, SUDO_DEBUG_UTIL);  | 
97  | 0  |     sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,  | 
98  | 0  |   "close_log not set, using stub");  | 
99  | 0  |     debug_return;  | 
100  | 0  | }  | 
101  |  |  | 
102  |  | /*  | 
103  |  |  * eventlog config setters.  | 
104  |  |  */  | 
105  |  |  | 
106  |  | void  | 
107  |  | eventlog_set_type(int type)  | 
108  | 50.6k  | { | 
109  | 50.6k  |     evl_conf.type = type;  | 
110  | 50.6k  | }  | 
111  |  |  | 
112  |  | void  | 
113  |  | eventlog_set_format(enum eventlog_format format)  | 
114  | 0  | { | 
115  | 0  |     evl_conf.format = format;  | 
116  | 0  | }  | 
117  |  |  | 
118  |  | void  | 
119  |  | eventlog_set_syslog_acceptpri(int pri)  | 
120  | 25.3k  | { | 
121  | 25.3k  |     evl_conf.syslog_acceptpri = pri;  | 
122  | 25.3k  | }  | 
123  |  |  | 
124  |  | void  | 
125  |  | eventlog_set_syslog_rejectpri(int pri)  | 
126  | 25.3k  | { | 
127  | 25.3k  |     evl_conf.syslog_rejectpri = pri;  | 
128  | 25.3k  | }  | 
129  |  |  | 
130  |  | void  | 
131  |  | eventlog_set_syslog_alertpri(int pri)  | 
132  | 25.3k  | { | 
133  | 25.3k  |     evl_conf.syslog_alertpri = pri;  | 
134  | 25.3k  | }  | 
135  |  |  | 
136  |  | void  | 
137  |  | eventlog_set_syslog_maxlen(size_t len)  | 
138  | 25.3k  | { | 
139  | 25.3k  |     evl_conf.syslog_maxlen = len;  | 
140  | 25.3k  | }  | 
141  |  |  | 
142  |  | void  | 
143  |  | eventlog_set_file_maxlen(size_t len)  | 
144  | 25.3k  | { | 
145  | 25.3k  |     evl_conf.file_maxlen = len;  | 
146  | 25.3k  | }  | 
147  |  |  | 
148  |  | void  | 
149  |  | eventlog_set_mailuid(uid_t uid)  | 
150  | 0  | { | 
151  | 0  |     evl_conf.mailuid = uid;  | 
152  | 0  | }  | 
153  |  |  | 
154  |  | void  | 
155  |  | eventlog_set_omit_hostname(bool omit_hostname)  | 
156  | 25.3k  | { | 
157  | 25.3k  |     evl_conf.omit_hostname = omit_hostname;  | 
158  | 25.3k  | }  | 
159  |  |  | 
160  |  | void  | 
161  |  | eventlog_set_logpath(const char *path)  | 
162  | 25.3k  | { | 
163  | 25.3k  |     evl_conf.logpath = path;  | 
164  | 25.3k  | }  | 
165  |  |  | 
166  |  | void  | 
167  |  | eventlog_set_time_fmt(const char *fmt)  | 
168  | 25.3k  | { | 
169  | 25.3k  |     evl_conf.time_fmt = fmt;  | 
170  | 25.3k  | }  | 
171  |  |  | 
172  |  | void  | 
173  |  | eventlog_set_mailerpath(const char *path)  | 
174  | 25.3k  | { | 
175  | 25.3k  |     evl_conf.mailerpath = path;  | 
176  | 25.3k  | }  | 
177  |  |  | 
178  |  | void  | 
179  |  | eventlog_set_mailerflags(const char *mflags)  | 
180  | 25.3k  | { | 
181  | 25.3k  |     evl_conf.mailerflags = mflags;  | 
182  | 25.3k  | }  | 
183  |  |  | 
184  |  | void  | 
185  |  | eventlog_set_mailfrom(const char *from_addr)  | 
186  | 25.3k  | { | 
187  | 25.3k  |     evl_conf.mailfrom = from_addr;  | 
188  | 25.3k  | }  | 
189  |  |  | 
190  |  | void  | 
191  |  | eventlog_set_mailto(const char *to_addr)  | 
192  | 25.3k  | { | 
193  | 25.3k  |     evl_conf.mailto = to_addr;  | 
194  | 25.3k  | }  | 
195  |  |  | 
196  |  | void  | 
197  |  | eventlog_set_mailsub(const char *subject)  | 
198  | 25.3k  | { | 
199  | 25.3k  |     evl_conf.mailsub = subject;  | 
200  | 25.3k  | }  | 
201  |  |  | 
202  |  | void  | 
203  |  | eventlog_set_open_log(FILE *(*fn)(int type, const char *))  | 
204  | 0  | { | 
205  | 0  |     evl_conf.open_log = fn;  | 
206  | 0  | }  | 
207  |  |  | 
208  |  | void  | 
209  |  | eventlog_set_close_log(void (*fn)(int type, FILE *))  | 
210  | 0  | { | 
211  | 0  |     evl_conf.close_log = fn;  | 
212  | 0  | }  | 
213  |  |  | 
214  |  | /*  | 
215  |  |  * get eventlog config.  | 
216  |  |  */  | 
217  |  | const struct eventlog_config *  | 
218  |  | eventlog_getconf(void)  | 
219  | 0  | { | 
220  | 0  |     return &evl_conf;  | 
221  | 0  | }  |