/src/sudo/logsrvd/logsrvd_conf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2019-2023 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 | | |
19 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <sys/stat.h> |
27 | | #include <sys/types.h> |
28 | | #include <sys/socket.h> |
29 | | #include <netinet/in.h> |
30 | | |
31 | | #include <errno.h> |
32 | | #include <ctype.h> |
33 | | #include <fcntl.h> |
34 | | #include <limits.h> |
35 | | #include <netdb.h> |
36 | | #ifdef HAVE_STDBOOL_H |
37 | | # include <stdbool.h> |
38 | | #else |
39 | | # include "compat/stdbool.h" |
40 | | #endif |
41 | | #include <stddef.h> |
42 | | #include <stdio.h> |
43 | | #include <stdlib.h> |
44 | | #include <string.h> |
45 | | #include <syslog.h> |
46 | | #include <time.h> |
47 | | #include <unistd.h> |
48 | | #include <grp.h> |
49 | | #include <pwd.h> |
50 | | #ifndef HAVE_GETADDRINFO |
51 | | # include "compat/getaddrinfo.h" |
52 | | #endif |
53 | | |
54 | | #include "pathnames.h" |
55 | | #include "sudo_compat.h" |
56 | | #include "sudo_debug.h" |
57 | | #include "sudo_eventlog.h" |
58 | | #include "sudo_fatal.h" |
59 | | #include "sudo_gettext.h" |
60 | | #include "sudo_iolog.h" |
61 | | #include "sudo_util.h" |
62 | | |
63 | | #include "logsrvd.h" |
64 | | |
65 | | #if defined(HAVE_OPENSSL) |
66 | 3.81k | # define DEFAULT_CA_CERT_PATH "/etc/ssl/sudo/cacert.pem" |
67 | 3.81k | # define DEFAULT_SERVER_CERT_PATH "/etc/ssl/sudo/certs/logsrvd_cert.pem" |
68 | 3.81k | # define DEFAULT_SERVER_KEY_PATH "/etc/ssl/sudo/private/logsrvd_key.pem" |
69 | | |
70 | | /* Evaluates to true if at least one TLS field is set, else false. */ |
71 | | # define TLS_CONFIGURED(_s) \ |
72 | 0 | ((_s).tls_key_path != NULL || (_s).tls_cert_path != NULL || \ |
73 | 0 | (_s).tls_cacert_path != NULL || (_s).tls_dhparams_path != NULL || \ |
74 | 0 | (_s).tls_ciphers_v12 != NULL || (_s).tls_ciphers_v13 != NULL || \ |
75 | 0 | (_s).tls_verify != -1) |
76 | | |
77 | | /* Evaluates to the relay-specific TLS setting, falling back to server. */ |
78 | | # define TLS_RELAY_STR(_c, _f) \ |
79 | 0 | ((_c)->relay._f != NULL ? (_c)->relay._f : (_c)->server._f) |
80 | | |
81 | | # define TLS_RELAY_INT(_c, _f) \ |
82 | 0 | ((_c)->relay._f != -1 ? (_c)->relay._f : (_c)->server._f) |
83 | | #endif |
84 | | |
85 | | enum server_log_type { |
86 | | SERVER_LOG_NONE, |
87 | | SERVER_LOG_STDERR, |
88 | | SERVER_LOG_SYSLOG, |
89 | | SERVER_LOG_FILE |
90 | | }; |
91 | | |
92 | | struct logsrvd_config; |
93 | | typedef bool (*logsrvd_conf_cb_t)(struct logsrvd_config *, const char *, size_t); |
94 | | |
95 | | struct logsrvd_config_entry { |
96 | | const char *conf_str; |
97 | | logsrvd_conf_cb_t setter; |
98 | | size_t offset; |
99 | | }; |
100 | | |
101 | | struct logsrvd_config_section { |
102 | | const char *name; |
103 | | struct logsrvd_config_entry *entries; |
104 | | }; |
105 | | |
106 | | struct address_list_container { |
107 | | unsigned int refcnt; |
108 | | struct server_address_list addrs; |
109 | | }; |
110 | | |
111 | | static struct logsrvd_config { |
112 | | struct logsrvd_config_server { |
113 | | struct address_list_container addresses; |
114 | | struct timespec timeout; |
115 | | bool tcp_keepalive; |
116 | | enum server_log_type log_type; |
117 | | FILE *log_stream; |
118 | | char *log_file; |
119 | | char *pid_file; |
120 | | #if defined(HAVE_OPENSSL) |
121 | | char *tls_key_path; |
122 | | char *tls_cert_path; |
123 | | char *tls_cacert_path; |
124 | | char *tls_dhparams_path; |
125 | | char *tls_ciphers_v12; |
126 | | char *tls_ciphers_v13; |
127 | | int tls_check_peer; |
128 | | int tls_verify; |
129 | | SSL_CTX *ssl_ctx; |
130 | | #endif |
131 | | } server; |
132 | | struct logsrvd_config_relay { |
133 | | struct address_list_container relays; |
134 | | struct timespec connect_timeout; |
135 | | struct timespec timeout; |
136 | | time_t retry_interval; |
137 | | char *relay_dir; |
138 | | bool tcp_keepalive; |
139 | | bool store_first; |
140 | | #if defined(HAVE_OPENSSL) |
141 | | char *tls_key_path; |
142 | | char *tls_cert_path; |
143 | | char *tls_cacert_path; |
144 | | char *tls_dhparams_path; |
145 | | char *tls_ciphers_v12; |
146 | | char *tls_ciphers_v13; |
147 | | int tls_check_peer; |
148 | | int tls_verify; |
149 | | SSL_CTX *ssl_ctx; |
150 | | #endif |
151 | | } relay; |
152 | | struct logsrvd_config_iolog { |
153 | | bool compress; |
154 | | bool flush; |
155 | | bool gid_set; |
156 | | bool log_passwords; |
157 | | uid_t uid; |
158 | | gid_t gid; |
159 | | mode_t mode; |
160 | | unsigned int maxseq; |
161 | | char *iolog_dir; |
162 | | char *iolog_file; |
163 | | void *passprompt_regex; |
164 | | } iolog; |
165 | | struct logsrvd_config_eventlog { |
166 | | int log_type; |
167 | | bool log_exit; |
168 | | enum eventlog_format log_format; |
169 | | } eventlog; |
170 | | struct logsrvd_config_syslog { |
171 | | unsigned int maxlen; |
172 | | int server_facility; |
173 | | int facility; |
174 | | int acceptpri; |
175 | | int rejectpri; |
176 | | int alertpri; |
177 | | } syslog; |
178 | | struct logsrvd_config_logfile { |
179 | | char *path; |
180 | | char *time_format; |
181 | | FILE *stream; |
182 | | } logfile; |
183 | | } *logsrvd_config; |
184 | | |
185 | | static bool logsrvd_warn_enable_stderr = true; |
186 | | |
187 | | /* eventlog getters */ |
188 | | bool |
189 | | logsrvd_conf_log_exit(void) |
190 | 0 | { |
191 | 0 | return logsrvd_config->eventlog.log_exit; |
192 | 0 | } |
193 | | |
194 | | /* iolog getters */ |
195 | | uid_t |
196 | | logsrvd_conf_iolog_uid(void) |
197 | 0 | { |
198 | 0 | return logsrvd_config->iolog.uid; |
199 | 0 | } |
200 | | |
201 | | gid_t |
202 | | logsrvd_conf_iolog_gid(void) |
203 | 0 | { |
204 | 0 | return logsrvd_config->iolog.gid; |
205 | 0 | } |
206 | | |
207 | | mode_t |
208 | | logsrvd_conf_iolog_mode(void) |
209 | 0 | { |
210 | 0 | return logsrvd_config->iolog.mode; |
211 | 0 | } |
212 | | |
213 | | const char * |
214 | | logsrvd_conf_iolog_dir(void) |
215 | 0 | { |
216 | 0 | return logsrvd_config->iolog.iolog_dir; |
217 | 0 | } |
218 | | |
219 | | const char * |
220 | | logsrvd_conf_iolog_file(void) |
221 | 0 | { |
222 | 0 | return logsrvd_config->iolog.iolog_file; |
223 | 0 | } |
224 | | |
225 | | bool |
226 | | logsrvd_conf_iolog_log_passwords(void) |
227 | 0 | { |
228 | 0 | return logsrvd_config->iolog.log_passwords; |
229 | 0 | } |
230 | | |
231 | | void * |
232 | | logsrvd_conf_iolog_passprompt_regex(void) |
233 | 0 | { |
234 | 0 | return logsrvd_config->iolog.passprompt_regex; |
235 | 0 | } |
236 | | |
237 | | /* server getters */ |
238 | | struct server_address_list * |
239 | | logsrvd_conf_server_listen_address(void) |
240 | 0 | { |
241 | 0 | return &logsrvd_config->server.addresses.addrs; |
242 | 0 | } |
243 | | |
244 | | bool |
245 | | logsrvd_conf_server_tcp_keepalive(void) |
246 | 0 | { |
247 | 0 | return logsrvd_config->server.tcp_keepalive; |
248 | 0 | } |
249 | | |
250 | | const char * |
251 | | logsrvd_conf_pid_file(void) |
252 | 0 | { |
253 | 0 | return logsrvd_config->server.pid_file; |
254 | 0 | } |
255 | | |
256 | | struct timespec * |
257 | | logsrvd_conf_server_timeout(void) |
258 | 0 | { |
259 | 0 | if (sudo_timespecisset(&logsrvd_config->server.timeout)) { |
260 | 0 | return &logsrvd_config->server.timeout; |
261 | 0 | } |
262 | | |
263 | 0 | return NULL; |
264 | 0 | } |
265 | | |
266 | | #if defined(HAVE_OPENSSL) |
267 | | SSL_CTX * |
268 | | logsrvd_server_tls_ctx(void) |
269 | 0 | { |
270 | 0 | return logsrvd_config->server.ssl_ctx; |
271 | 0 | } |
272 | | |
273 | | bool |
274 | | logsrvd_conf_server_tls_check_peer(void) |
275 | 0 | { |
276 | 0 | return logsrvd_config->server.tls_check_peer; |
277 | 0 | } |
278 | | #endif |
279 | | |
280 | | /* relay getters */ |
281 | | struct server_address_list * |
282 | | logsrvd_conf_relay_address(void) |
283 | 0 | { |
284 | 0 | return &logsrvd_config->relay.relays.addrs; |
285 | 0 | } |
286 | | |
287 | | const char * |
288 | | logsrvd_conf_relay_dir(void) |
289 | 0 | { |
290 | 0 | return logsrvd_config->relay.relay_dir; |
291 | 0 | } |
292 | | |
293 | | bool |
294 | | logsrvd_conf_relay_store_first(void) |
295 | 0 | { |
296 | 0 | return logsrvd_config->relay.store_first; |
297 | 0 | } |
298 | | |
299 | | bool |
300 | | logsrvd_conf_relay_tcp_keepalive(void) |
301 | 0 | { |
302 | 0 | return logsrvd_config->relay.tcp_keepalive; |
303 | 0 | } |
304 | | |
305 | | struct timespec * |
306 | | logsrvd_conf_relay_timeout(void) |
307 | 0 | { |
308 | 0 | if (sudo_timespecisset(&logsrvd_config->relay.timeout)) { |
309 | 0 | return &logsrvd_config->relay.timeout; |
310 | 0 | } |
311 | | |
312 | 0 | return NULL; |
313 | 0 | } |
314 | | |
315 | | struct timespec * |
316 | | logsrvd_conf_relay_connect_timeout(void) |
317 | 0 | { |
318 | 0 | if (sudo_timespecisset(&logsrvd_config->relay.connect_timeout)) { |
319 | 0 | return &logsrvd_config->relay.connect_timeout; |
320 | 0 | } |
321 | | |
322 | 0 | return NULL; |
323 | 0 | } |
324 | | |
325 | | time_t |
326 | | logsrvd_conf_relay_retry_interval(void) |
327 | 0 | { |
328 | 0 | return logsrvd_config->relay.retry_interval; |
329 | 0 | } |
330 | | |
331 | | #if defined(HAVE_OPENSSL) |
332 | | SSL_CTX * |
333 | | logsrvd_relay_tls_ctx(void) |
334 | 0 | { |
335 | 0 | if (logsrvd_config->relay.ssl_ctx != NULL) |
336 | 0 | return logsrvd_config->relay.ssl_ctx; |
337 | 0 | return logsrvd_config->server.ssl_ctx; |
338 | 0 | } |
339 | | |
340 | | bool |
341 | | logsrvd_conf_relay_tls_check_peer(void) |
342 | 0 | { |
343 | 0 | if (logsrvd_config->relay.tls_check_peer != -1) |
344 | 0 | return logsrvd_config->relay.tls_check_peer; |
345 | 0 | return logsrvd_config->server.tls_check_peer; |
346 | 0 | } |
347 | | #endif |
348 | | |
349 | | /* I/O log callbacks */ |
350 | | static bool |
351 | | cb_iolog_dir(struct logsrvd_config *config, const char *path, size_t offset) |
352 | 4.00k | { |
353 | 4.00k | debug_decl(cb_iolog_dir, SUDO_DEBUG_UTIL); |
354 | | |
355 | 4.00k | free(config->iolog.iolog_dir); |
356 | 4.00k | if ((config->iolog.iolog_dir = strdup(path)) == NULL) { |
357 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
358 | 0 | debug_return_bool(false); |
359 | 0 | } |
360 | 4.00k | debug_return_bool(true); |
361 | 4.00k | } |
362 | | |
363 | | static bool |
364 | | cb_iolog_file(struct logsrvd_config *config, const char *path, size_t offset) |
365 | 4.00k | { |
366 | 4.00k | debug_decl(cb_iolog_file, SUDO_DEBUG_UTIL); |
367 | | |
368 | 4.00k | free(config->iolog.iolog_file); |
369 | 4.00k | if ((config->iolog.iolog_file = strdup(path)) == NULL) { |
370 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
371 | 0 | debug_return_bool(false); |
372 | 0 | } |
373 | 4.00k | debug_return_bool(true); |
374 | 4.00k | } |
375 | | |
376 | | static bool |
377 | | cb_iolog_compress(struct logsrvd_config *config, const char *str, size_t offset) |
378 | 213 | { |
379 | 213 | int val; |
380 | 213 | debug_decl(cb_iolog_compress, SUDO_DEBUG_UTIL); |
381 | | |
382 | 213 | if ((val = sudo_strtobool(str)) == -1) |
383 | 5 | debug_return_bool(false); |
384 | | |
385 | 208 | config->iolog.compress = val; |
386 | 208 | debug_return_bool(true); |
387 | 208 | } |
388 | | |
389 | | static bool |
390 | | cb_iolog_log_passwords(struct logsrvd_config *config, const char *str, size_t offset) |
391 | 205 | { |
392 | 205 | int val; |
393 | 205 | debug_decl(cb_iolog_log_passwords, SUDO_DEBUG_UTIL); |
394 | | |
395 | 205 | if ((val = sudo_strtobool(str)) == -1) |
396 | 4 | debug_return_bool(false); |
397 | | |
398 | 201 | config->iolog.log_passwords = val; |
399 | 201 | debug_return_bool(true); |
400 | 201 | } |
401 | | |
402 | | static bool |
403 | | cb_iolog_flush(struct logsrvd_config *config, const char *str, size_t offset) |
404 | 388 | { |
405 | 388 | int val; |
406 | 388 | debug_decl(cb_iolog_flush, SUDO_DEBUG_UTIL); |
407 | | |
408 | 388 | if ((val = sudo_strtobool(str)) == -1) |
409 | 8 | debug_return_bool(false); |
410 | | |
411 | 380 | config->iolog.flush = val; |
412 | 380 | debug_return_bool(true); |
413 | 380 | } |
414 | | |
415 | | static bool |
416 | | cb_iolog_user(struct logsrvd_config *config, const char *user, size_t offset) |
417 | 520 | { |
418 | 520 | struct passwd *pw; |
419 | 520 | debug_decl(cb_iolog_user, SUDO_DEBUG_UTIL); |
420 | | |
421 | 520 | if ((pw = getpwnam(user)) == NULL) { |
422 | 119 | sudo_warnx(U_("unknown user %s"), user); |
423 | 119 | debug_return_bool(false); |
424 | 119 | } |
425 | 401 | config->iolog.uid = pw->pw_uid; |
426 | 401 | if (!config->iolog.gid_set) |
427 | 207 | config->iolog.gid = pw->pw_gid; |
428 | | |
429 | 401 | debug_return_bool(true); |
430 | 401 | } |
431 | | |
432 | | static bool |
433 | | cb_iolog_group(struct logsrvd_config *config, const char *group, size_t offset) |
434 | 382 | { |
435 | 382 | struct group *gr; |
436 | 382 | debug_decl(cb_iolog_group, SUDO_DEBUG_UTIL); |
437 | | |
438 | 382 | if ((gr = getgrnam(group)) == NULL) { |
439 | 121 | sudo_warnx(U_("unknown group %s"), group); |
440 | 121 | debug_return_bool(false); |
441 | 121 | } |
442 | 261 | config->iolog.gid = gr->gr_gid; |
443 | 261 | config->iolog.gid_set = true; |
444 | | |
445 | 261 | debug_return_bool(true); |
446 | 261 | } |
447 | | |
448 | | static bool |
449 | | cb_iolog_mode(struct logsrvd_config *config, const char *str, size_t offset) |
450 | 389 | { |
451 | 389 | const char *errstr; |
452 | 389 | mode_t mode; |
453 | 389 | debug_decl(cb_iolog_mode, SUDO_DEBUG_UTIL); |
454 | | |
455 | 389 | mode = sudo_strtomode(str, &errstr); |
456 | 389 | if (errstr != NULL) { |
457 | 170 | sudo_warnx(U_("unable to parse iolog mode %s"), str); |
458 | 170 | debug_return_bool(false); |
459 | 170 | } |
460 | 219 | config->iolog.mode = mode; |
461 | 219 | debug_return_bool(true); |
462 | 219 | } |
463 | | |
464 | | static bool |
465 | | cb_iolog_maxseq(struct logsrvd_config *config, const char *str, size_t offset) |
466 | 1.84k | { |
467 | 1.84k | const char *errstr; |
468 | 1.84k | unsigned int value; |
469 | 1.84k | debug_decl(cb_iolog_maxseq, SUDO_DEBUG_UTIL); |
470 | | |
471 | 1.84k | value = sudo_strtonum(str, 0, SESSID_MAX, &errstr); |
472 | 1.84k | if (errstr != NULL) { |
473 | 894 | if (errno != ERANGE) { |
474 | 25 | sudo_warnx(U_("invalid value for %s: %s"), "maxseq", errstr); |
475 | 25 | debug_return_bool(false); |
476 | 25 | } |
477 | | /* Out of range, clamp to SESSID_MAX as documented. */ |
478 | 869 | value = SESSID_MAX; |
479 | 869 | } |
480 | 1.82k | config->iolog.maxseq = value; |
481 | 1.82k | debug_return_bool(true); |
482 | 1.82k | } |
483 | | |
484 | | static bool |
485 | | cb_iolog_passprompt_regex(struct logsrvd_config *config, const char *str, size_t offset) |
486 | 1.21k | { |
487 | 1.21k | debug_decl(cb_iolog_passprompt_regex, SUDO_DEBUG_UTIL); |
488 | | |
489 | 1.21k | if (config->iolog.passprompt_regex == NULL) { |
490 | | /* Lazy alloc of the passprompt regex handle. */ |
491 | 842 | config->iolog.passprompt_regex = iolog_pwfilt_alloc(); |
492 | 842 | if (config->iolog.passprompt_regex == NULL) |
493 | 0 | debug_return_bool(false); |
494 | 842 | } |
495 | 1.21k | debug_return_bool(iolog_pwfilt_add(config->iolog.passprompt_regex, str)); |
496 | 1.21k | } |
497 | | |
498 | | /* Server callbacks */ |
499 | | static bool |
500 | | append_address(struct server_address_list *addresses, const char *str, |
501 | | bool allow_wildcard) |
502 | 1.91k | { |
503 | 1.91k | struct addrinfo hints, *res, *res0 = NULL; |
504 | 1.91k | char *sa_str = NULL, *sa_host = NULL; |
505 | 1.91k | char *copy, *host, *port; |
506 | 1.91k | bool tls, ret = false; |
507 | 1.91k | int error; |
508 | 1.91k | debug_decl(append_address, SUDO_DEBUG_UTIL); |
509 | | |
510 | 1.91k | if ((copy = strdup(str)) == NULL) { |
511 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
512 | 0 | debug_return_bool(false); |
513 | 0 | } |
514 | | |
515 | | /* Parse host[:port] */ |
516 | 1.91k | if (!iolog_parse_host_port(copy, &host, &port, &tls, DEFAULT_PORT, |
517 | 1.91k | DEFAULT_PORT_TLS)) |
518 | 4 | goto done; |
519 | 1.91k | if (host[0] == '*' && host[1] == '\0') { |
520 | 836 | if (!allow_wildcard) |
521 | 1 | goto done; |
522 | 835 | host = NULL; |
523 | 835 | } |
524 | | |
525 | | #if !defined(HAVE_OPENSSL) |
526 | | if (tls) { |
527 | | sudo_warnx("%s", U_("TLS not supported")); |
528 | | goto done; |
529 | | } |
530 | | #endif |
531 | | |
532 | | /* Only make a single copy of the string + host for all addresses. */ |
533 | 1.91k | if ((sa_str = sudo_rcstr_dup(str)) == NULL) { |
534 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
535 | 0 | goto done; |
536 | 0 | } |
537 | 1.91k | if (host != NULL && (sa_host = sudo_rcstr_dup(host)) == NULL) { |
538 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
539 | 0 | goto done; |
540 | 0 | } |
541 | | |
542 | | /* Resolve host (and port if it is a service). */ |
543 | 1.91k | memset(&hints, 0, sizeof(hints)); |
544 | 1.91k | hints.ai_family = AF_UNSPEC; |
545 | 1.91k | hints.ai_socktype = SOCK_STREAM; |
546 | 1.91k | hints.ai_flags = AI_PASSIVE; |
547 | 1.91k | error = getaddrinfo(host, port, &hints, &res0); |
548 | 1.91k | if (error != 0) { |
549 | 1.91k | sudo_gai_warn(error, U_("%s:%s"), host ? host : "*", port); |
550 | 1.91k | goto done; |
551 | 1.91k | } |
552 | 0 | for (res = res0; res != NULL; res = res->ai_next) { |
553 | 0 | struct server_address *addr; |
554 | |
|
555 | 0 | if ((addr = malloc(sizeof(*addr))) == NULL) { |
556 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
557 | 0 | goto done; |
558 | 0 | } |
559 | 0 | addr->sa_str = sudo_rcstr_addref(sa_str); |
560 | 0 | addr->sa_host = sudo_rcstr_addref(sa_host); |
561 | |
|
562 | 0 | memcpy(&addr->sa_un, res->ai_addr, res->ai_addrlen); |
563 | 0 | addr->sa_size = res->ai_addrlen; |
564 | 0 | addr->tls = tls; |
565 | 0 | TAILQ_INSERT_TAIL(addresses, addr, entries); |
566 | 0 | } |
567 | | |
568 | 0 | ret = true; |
569 | 1.91k | done: |
570 | 1.91k | sudo_rcstr_delref(sa_str); |
571 | 1.91k | sudo_rcstr_delref(sa_host); |
572 | 1.91k | if (res0 != NULL) |
573 | 0 | freeaddrinfo(res0); |
574 | 1.91k | free(copy); |
575 | 1.91k | debug_return_bool(ret); |
576 | 1.91k | } |
577 | | |
578 | | static bool |
579 | | cb_server_listen_address(struct logsrvd_config *config, const char *str, size_t offset) |
580 | 1.52k | { |
581 | 1.52k | return append_address(&config->server.addresses.addrs, str, true); |
582 | 1.52k | } |
583 | | |
584 | | static bool |
585 | | cb_server_timeout(struct logsrvd_config *config, const char *str, size_t offset) |
586 | 215 | { |
587 | 215 | time_t timeout; |
588 | 215 | const char *errstr; |
589 | 215 | debug_decl(cb_server_timeout, SUDO_DEBUG_UTIL); |
590 | | |
591 | 215 | timeout = sudo_strtonum(str, 0, TIME_T_MAX, &errstr); |
592 | 215 | if (errstr != NULL) |
593 | 5 | debug_return_bool(false); |
594 | | |
595 | 210 | config->server.timeout.tv_sec = timeout; |
596 | | |
597 | 210 | debug_return_bool(true); |
598 | 210 | } |
599 | | |
600 | | static bool |
601 | | cb_server_keepalive(struct logsrvd_config *config, const char *str, size_t offset) |
602 | 213 | { |
603 | 213 | int val; |
604 | 213 | debug_decl(cb_server_keepalive, SUDO_DEBUG_UTIL); |
605 | | |
606 | 213 | if ((val = sudo_strtobool(str)) == -1) |
607 | 5 | debug_return_bool(false); |
608 | | |
609 | 208 | config->server.tcp_keepalive = val; |
610 | 208 | debug_return_bool(true); |
611 | 208 | } |
612 | | |
613 | | static bool |
614 | | cb_server_pid_file(struct logsrvd_config *config, const char *str, size_t offset) |
615 | 401 | { |
616 | 401 | char *copy = NULL; |
617 | 401 | debug_decl(cb_server_pid_file, SUDO_DEBUG_UTIL); |
618 | | |
619 | | /* An empty value means to disable the pid file. */ |
620 | 401 | if (*str != '\0') { |
621 | 207 | if (*str != '/') { |
622 | 13 | sudo_warnx(U_("%s: not a fully qualified path"), str); |
623 | 13 | debug_return_bool(false); |
624 | 13 | } |
625 | 194 | if ((copy = strdup(str)) == NULL) { |
626 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
627 | 0 | debug_return_bool(false); |
628 | 0 | } |
629 | 194 | } |
630 | | |
631 | 388 | free(config->server.pid_file); |
632 | 388 | config->server.pid_file = copy; |
633 | | |
634 | 388 | debug_return_bool(true); |
635 | 388 | } |
636 | | |
637 | | static bool |
638 | | cb_server_log(struct logsrvd_config *config, const char *str, size_t offset) |
639 | 885 | { |
640 | 885 | char *copy = NULL; |
641 | 885 | enum server_log_type log_type = SERVER_LOG_NONE; |
642 | 885 | debug_decl(cb_server_log, SUDO_DEBUG_UTIL); |
643 | | |
644 | | /* An empty value means to disable the server log. */ |
645 | 885 | if (*str != '\0') { |
646 | 690 | if (*str == '/') { |
647 | 197 | log_type = SERVER_LOG_FILE; |
648 | 197 | if ((copy = strdup(str)) == NULL) { |
649 | 0 | sudo_warnx(U_("%s: %s"), __func__, |
650 | 0 | U_("unable to allocate memory")); |
651 | 0 | debug_return_bool(false); |
652 | 0 | } |
653 | 493 | } else if (strcmp(str, "stderr") == 0) { |
654 | 195 | log_type = SERVER_LOG_STDERR; |
655 | 298 | } else if (strcmp(str, "syslog") == 0) { |
656 | 194 | log_type = SERVER_LOG_SYSLOG; |
657 | 194 | } else { |
658 | 104 | debug_return_bool(false); |
659 | 104 | } |
660 | 690 | } |
661 | | |
662 | 781 | free(config->server.log_file); |
663 | 781 | config->server.log_file = copy; |
664 | 781 | config->server.log_type = log_type; |
665 | | |
666 | 781 | debug_return_bool(true); |
667 | 781 | } |
668 | | |
669 | | #if defined(HAVE_OPENSSL) |
670 | | static bool |
671 | | cb_tls_key(struct logsrvd_config *config, const char *path, size_t offset) |
672 | 195 | { |
673 | 195 | char **p = (char **)((char *)config + offset); |
674 | 195 | debug_decl(cb_tls_key, SUDO_DEBUG_UTIL); |
675 | | |
676 | 195 | free(*p); |
677 | 195 | if ((*p = strdup(path)) == NULL) { |
678 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
679 | 0 | debug_return_bool(false); |
680 | 0 | } |
681 | 195 | debug_return_bool(true); |
682 | 195 | } |
683 | | |
684 | | static bool |
685 | | cb_tls_cacert(struct logsrvd_config *config, const char *path, size_t offset) |
686 | 194 | { |
687 | 194 | char **p = (char **)((char *)config + offset); |
688 | 194 | debug_decl(cb_tls_cacert, SUDO_DEBUG_UTIL); |
689 | | |
690 | 194 | free(*p); |
691 | 194 | if ((*p = strdup(path)) == NULL) { |
692 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
693 | 0 | debug_return_bool(false); |
694 | 0 | } |
695 | 194 | debug_return_bool(true); |
696 | 194 | } |
697 | | |
698 | | static bool |
699 | | cb_tls_cert(struct logsrvd_config *config, const char *path, size_t offset) |
700 | 69 | { |
701 | 69 | char **p = (char **)((char *)config + offset); |
702 | 69 | debug_decl(cb_tls_cert, SUDO_DEBUG_UTIL); |
703 | | |
704 | 69 | free(*p); |
705 | 69 | if ((*p = strdup(path)) == NULL) { |
706 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
707 | 0 | debug_return_bool(false); |
708 | 0 | } |
709 | 69 | debug_return_bool(true); |
710 | 69 | } |
711 | | |
712 | | static bool |
713 | | cb_tls_dhparams(struct logsrvd_config *config, const char *path, size_t offset) |
714 | 194 | { |
715 | 194 | char **p = (char **)((char *)config + offset); |
716 | 194 | debug_decl(cb_tls_dhparams, SUDO_DEBUG_UTIL); |
717 | | |
718 | 194 | free(*p); |
719 | 194 | if ((*p = strdup(path)) == NULL) { |
720 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
721 | 0 | debug_return_bool(false); |
722 | 0 | } |
723 | 194 | debug_return_bool(true); |
724 | 194 | } |
725 | | |
726 | | static bool |
727 | | cb_tls_ciphers12(struct logsrvd_config *config, const char *str, size_t offset) |
728 | 195 | { |
729 | 195 | char **p = (char **)((char *)config + offset); |
730 | 195 | debug_decl(cb_tls_ciphers12, SUDO_DEBUG_UTIL); |
731 | | |
732 | 195 | free(*p); |
733 | 195 | if ((*p = strdup(str)) == NULL) { |
734 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
735 | 0 | debug_return_bool(false); |
736 | 0 | } |
737 | 195 | debug_return_bool(true); |
738 | 195 | } |
739 | | |
740 | | static bool |
741 | | cb_tls_ciphers13(struct logsrvd_config *config, const char *str, size_t offset) |
742 | 195 | { |
743 | 195 | char **p = (char **)((char *)config + offset); |
744 | 195 | debug_decl(cb_tls_ciphers13, SUDO_DEBUG_UTIL); |
745 | | |
746 | 195 | free(*p); |
747 | 195 | if ((*p = strdup(str)) == NULL) { |
748 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
749 | 0 | debug_return_bool(false); |
750 | 0 | } |
751 | 195 | debug_return_bool(true); |
752 | 195 | } |
753 | | |
754 | | static bool |
755 | | cb_tls_verify(struct logsrvd_config *config, const char *str, size_t offset) |
756 | 398 | { |
757 | 398 | int *p = (int *)((char *)config + offset); |
758 | 398 | int val; |
759 | 398 | debug_decl(cb_tls_verify, SUDO_DEBUG_UTIL); |
760 | | |
761 | 398 | if ((val = sudo_strtobool(str)) == -1) |
762 | 175 | debug_return_bool(false); |
763 | | |
764 | 223 | *p = val; |
765 | 223 | debug_return_bool(true); |
766 | 223 | } |
767 | | |
768 | | static bool |
769 | | cb_tls_checkpeer(struct logsrvd_config *config, const char *str, size_t offset) |
770 | 261 | { |
771 | 261 | int *p = (int *)((char *)config + offset); |
772 | 261 | int val; |
773 | 261 | debug_decl(cb_tls_checkpeer, SUDO_DEBUG_UTIL); |
774 | | |
775 | 261 | if ((val = sudo_strtobool(str)) == -1) |
776 | 7 | debug_return_bool(false); |
777 | | |
778 | 254 | *p = val; |
779 | 254 | debug_return_bool(true); |
780 | 254 | } |
781 | | #endif |
782 | | |
783 | | /* relay callbacks */ |
784 | | static bool |
785 | | cb_relay_host(struct logsrvd_config *config, const char *str, size_t offset) |
786 | 391 | { |
787 | 391 | return append_address(&config->relay.relays.addrs, str, false); |
788 | 391 | } |
789 | | |
790 | | static bool |
791 | | cb_relay_timeout(struct logsrvd_config *config, const char *str, size_t offset) |
792 | 316 | { |
793 | 316 | time_t timeout; |
794 | 316 | const char *errstr; |
795 | 316 | debug_decl(cb_relay_timeout, SUDO_DEBUG_UTIL); |
796 | | |
797 | 316 | timeout = sudo_strtonum(str, 0, TIME_T_MAX, &errstr); |
798 | 316 | if (errstr != NULL) |
799 | 36 | debug_return_bool(false); |
800 | | |
801 | 280 | config->server.timeout.tv_sec = timeout; |
802 | | |
803 | 280 | debug_return_bool(true); |
804 | 280 | } |
805 | | |
806 | | static bool |
807 | | cb_relay_connect_timeout(struct logsrvd_config *config, const char *str, size_t offset) |
808 | 322 | { |
809 | 322 | time_t timeout; |
810 | 322 | const char *errstr; |
811 | 322 | debug_decl(cb_relay_connect_timeout, SUDO_DEBUG_UTIL); |
812 | | |
813 | 322 | timeout = sudo_strtonum(str, 0, TIME_T_MAX, &errstr); |
814 | 322 | if (errstr != NULL) |
815 | 7 | debug_return_bool(false); |
816 | | |
817 | 315 | config->relay.connect_timeout.tv_sec = timeout; |
818 | | |
819 | 315 | debug_return_bool(true); |
820 | 315 | } |
821 | | |
822 | | static bool |
823 | | cb_relay_dir(struct logsrvd_config *config, const char *str, size_t offset) |
824 | 4.00k | { |
825 | 4.00k | char *copy = NULL; |
826 | 4.00k | debug_decl(cb_relay_dir, SUDO_DEBUG_UTIL); |
827 | | |
828 | 4.00k | if ((copy = strdup(str)) == NULL) { |
829 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
830 | 0 | debug_return_bool(false); |
831 | 0 | } |
832 | | |
833 | 4.00k | free(config->relay.relay_dir); |
834 | 4.00k | config->relay.relay_dir = copy; |
835 | | |
836 | 4.00k | debug_return_bool(true); |
837 | 4.00k | } |
838 | | |
839 | | static bool |
840 | | cb_retry_interval(struct logsrvd_config *config, const char *str, size_t offset) |
841 | 228 | { |
842 | 228 | time_t interval; |
843 | 228 | const char *errstr; |
844 | 228 | debug_decl(cb_retry_interval, SUDO_DEBUG_UTIL); |
845 | | |
846 | 228 | interval = sudo_strtonum(str, 0, TIME_T_MAX, &errstr); |
847 | 228 | if (errstr != NULL) |
848 | 6 | debug_return_bool(false); |
849 | | |
850 | 222 | config->relay.retry_interval = interval; |
851 | | |
852 | 222 | debug_return_bool(true); |
853 | 222 | } |
854 | | |
855 | | static bool |
856 | | cb_relay_store_first(struct logsrvd_config *config, const char *str, size_t offset) |
857 | 261 | { |
858 | 261 | int val; |
859 | 261 | debug_decl(cb_relay_store_first, SUDO_DEBUG_UTIL); |
860 | | |
861 | 261 | if ((val = sudo_strtobool(str)) == -1) |
862 | 7 | debug_return_bool(false); |
863 | | |
864 | 254 | config->relay.store_first = val; |
865 | 254 | debug_return_bool(true); |
866 | 254 | } |
867 | | |
868 | | static bool |
869 | | cb_relay_keepalive(struct logsrvd_config *config, const char *str, size_t offset) |
870 | 101 | { |
871 | 101 | int val; |
872 | 101 | debug_decl(cb_relay_keepalive, SUDO_DEBUG_UTIL); |
873 | | |
874 | 101 | if ((val = sudo_strtobool(str)) == -1) |
875 | 6 | debug_return_bool(false); |
876 | | |
877 | 95 | config->relay.tcp_keepalive = val; |
878 | 95 | debug_return_bool(true); |
879 | 95 | } |
880 | | |
881 | | /* eventlog callbacks */ |
882 | | static bool |
883 | | cb_eventlog_type(struct logsrvd_config *config, const char *str, size_t offset) |
884 | 758 | { |
885 | 758 | debug_decl(cb_eventlog_type, SUDO_DEBUG_UTIL); |
886 | | |
887 | 758 | if (strcmp(str, "none") == 0) |
888 | 223 | config->eventlog.log_type = EVLOG_NONE; |
889 | 535 | else if (strcmp(str, "syslog") == 0) |
890 | 194 | config->eventlog.log_type = EVLOG_SYSLOG; |
891 | 341 | else if (strcmp(str, "logfile") == 0) |
892 | 198 | config->eventlog.log_type = EVLOG_FILE; |
893 | 143 | else |
894 | 143 | debug_return_bool(false); |
895 | | |
896 | 615 | debug_return_bool(true); |
897 | 615 | } |
898 | | |
899 | | static bool |
900 | | cb_eventlog_format(struct logsrvd_config *config, const char *str, size_t offset) |
901 | 646 | { |
902 | 646 | debug_decl(cb_eventlog_format, SUDO_DEBUG_UTIL); |
903 | | |
904 | 646 | if (strcmp(str, "json") == 0) |
905 | 197 | config->eventlog.log_format = EVLOG_JSON; |
906 | 449 | else if (strcmp(str, "sudo") == 0) |
907 | 378 | config->eventlog.log_format = EVLOG_SUDO; |
908 | 71 | else |
909 | 71 | debug_return_bool(false); |
910 | | |
911 | 575 | debug_return_bool(true); |
912 | 575 | } |
913 | | |
914 | | static bool |
915 | | cb_eventlog_exit(struct logsrvd_config *config, const char *str, size_t offset) |
916 | 2.68k | { |
917 | 2.68k | int val; |
918 | 2.68k | debug_decl(cb_eventlog_exit, SUDO_DEBUG_UTIL); |
919 | | |
920 | 2.68k | if ((val = sudo_strtobool(str)) == -1) |
921 | 63 | debug_return_bool(false); |
922 | | |
923 | 2.62k | config->eventlog.log_exit = val; |
924 | 2.62k | debug_return_bool(true); |
925 | 2.62k | } |
926 | | |
927 | | /* syslog callbacks */ |
928 | | static bool |
929 | | cb_syslog_maxlen(struct logsrvd_config *config, const char *str, size_t offset) |
930 | 247 | { |
931 | 247 | unsigned int maxlen; |
932 | 247 | const char *errstr; |
933 | 247 | debug_decl(cb_syslog_maxlen, SUDO_DEBUG_UTIL); |
934 | | |
935 | 247 | maxlen = sudo_strtonum(str, 1, UINT_MAX, &errstr); |
936 | 247 | if (errstr != NULL) |
937 | 9 | debug_return_bool(false); |
938 | | |
939 | 238 | config->syslog.maxlen = maxlen; |
940 | | |
941 | 238 | debug_return_bool(true); |
942 | 238 | } |
943 | | |
944 | | static bool |
945 | | cb_syslog_server_facility(struct logsrvd_config *config, const char *str, size_t offset) |
946 | 228 | { |
947 | 228 | int logfac; |
948 | 228 | debug_decl(cb_syslog_server_facility, SUDO_DEBUG_UTIL); |
949 | | |
950 | 228 | if (!sudo_str2logfac(str, &logfac)) { |
951 | 6 | sudo_warnx(U_("unknown syslog facility %s"), str); |
952 | 6 | debug_return_bool(false); |
953 | 6 | } |
954 | | |
955 | 222 | config->syslog.server_facility = logfac; |
956 | | |
957 | 222 | debug_return_bool(true); |
958 | 222 | } |
959 | | |
960 | | static bool |
961 | | cb_syslog_facility(struct logsrvd_config *config, const char *str, size_t offset) |
962 | 5.04k | { |
963 | 5.04k | int logfac; |
964 | 5.04k | debug_decl(cb_syslog_facility, SUDO_DEBUG_UTIL); |
965 | | |
966 | 5.04k | if (!sudo_str2logfac(str, &logfac)) { |
967 | 95 | sudo_warnx(U_("unknown syslog facility %s"), str); |
968 | 95 | debug_return_bool(false); |
969 | 95 | } |
970 | | |
971 | 4.95k | config->syslog.facility = logfac; |
972 | | |
973 | 4.95k | debug_return_bool(true); |
974 | 4.95k | } |
975 | | |
976 | | static bool |
977 | | cb_syslog_acceptpri(struct logsrvd_config *config, const char *str, size_t offset) |
978 | 4.05k | { |
979 | 4.05k | int logpri; |
980 | 4.05k | debug_decl(cb_syslog_acceptpri, SUDO_DEBUG_UTIL); |
981 | | |
982 | 4.05k | if (!sudo_str2logpri(str, &logpri)) { |
983 | 4 | sudo_warnx(U_("unknown syslog priority %s"), str); |
984 | 4 | debug_return_bool(false); |
985 | 4 | } |
986 | | |
987 | 4.04k | config->syslog.acceptpri = logpri; |
988 | | |
989 | 4.04k | debug_return_bool(true); |
990 | 4.04k | } |
991 | | |
992 | | static bool |
993 | | cb_syslog_rejectpri(struct logsrvd_config *config, const char *str, size_t offset) |
994 | 4.03k | { |
995 | 4.03k | int logpri; |
996 | 4.03k | debug_decl(cb_syslog_rejectpri, SUDO_DEBUG_UTIL); |
997 | | |
998 | 4.03k | if (!sudo_str2logpri(str, &logpri)) { |
999 | 5 | sudo_warnx(U_("unknown syslog priority %s"), str); |
1000 | 5 | debug_return_bool(false); |
1001 | 5 | } |
1002 | | |
1003 | 4.03k | config->syslog.rejectpri = logpri; |
1004 | | |
1005 | 4.03k | debug_return_bool(true); |
1006 | 4.03k | } |
1007 | | |
1008 | | static bool |
1009 | | cb_syslog_alertpri(struct logsrvd_config *config, const char *str, size_t offset) |
1010 | 5.48k | { |
1011 | 5.48k | int logpri; |
1012 | 5.48k | debug_decl(cb_syslog_alertpri, SUDO_DEBUG_UTIL); |
1013 | | |
1014 | 5.48k | if (!sudo_str2logpri(str, &logpri)) { |
1015 | 101 | sudo_warnx(U_("unknown syslog priority %s"), str); |
1016 | 101 | debug_return_bool(false); |
1017 | 101 | } |
1018 | | |
1019 | 5.38k | config->syslog.alertpri = logpri; |
1020 | | |
1021 | 5.38k | debug_return_bool(true); |
1022 | 5.38k | } |
1023 | | |
1024 | | /* logfile callbacks */ |
1025 | | static bool |
1026 | | cb_logfile_path(struct logsrvd_config *config, const char *str, size_t offset) |
1027 | 4.49k | { |
1028 | 4.49k | char *copy = NULL; |
1029 | 4.49k | debug_decl(cb_logfile_path, SUDO_DEBUG_UTIL); |
1030 | | |
1031 | 4.49k | if (*str != '/') { |
1032 | 15 | sudo_warnx(U_("%s: not a fully qualified path"), str); |
1033 | 15 | debug_return_bool(false); |
1034 | 15 | } |
1035 | 4.48k | if ((copy = strdup(str)) == NULL) { |
1036 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
1037 | 0 | debug_return_bool(false); |
1038 | 0 | } |
1039 | | |
1040 | 4.48k | free(config->logfile.path); |
1041 | 4.48k | config->logfile.path = copy; |
1042 | | |
1043 | 4.48k | debug_return_bool(true); |
1044 | 4.48k | } |
1045 | | |
1046 | | static bool |
1047 | | cb_logfile_time_format(struct logsrvd_config *config, const char *str, size_t offset) |
1048 | 4.00k | { |
1049 | 4.00k | char *copy = NULL; |
1050 | 4.00k | debug_decl(cb_logfile_time_format, SUDO_DEBUG_UTIL); |
1051 | | |
1052 | 4.00k | if ((copy = strdup(str)) == NULL) { |
1053 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
1054 | 0 | debug_return_bool(false); |
1055 | 0 | } |
1056 | | |
1057 | 4.00k | free(config->logfile.time_format); |
1058 | 4.00k | config->logfile.time_format = copy; |
1059 | | |
1060 | 4.00k | debug_return_bool(true); |
1061 | 4.00k | } |
1062 | | |
1063 | | void |
1064 | | address_list_addref(struct server_address_list *al) |
1065 | 0 | { |
1066 | 0 | struct address_list_container *container = |
1067 | 0 | __containerof(al, struct address_list_container, addrs); |
1068 | 0 | container->refcnt++; |
1069 | 0 | } |
1070 | | |
1071 | | void |
1072 | | address_list_delref(struct server_address_list *al) |
1073 | 7.63k | { |
1074 | 7.63k | struct address_list_container *container = |
1075 | 7.63k | __containerof(al, struct address_list_container, addrs); |
1076 | 7.63k | if (--container->refcnt == 0) { |
1077 | 7.63k | struct server_address *addr; |
1078 | 7.63k | while ((addr = TAILQ_FIRST(al))) { |
1079 | 0 | TAILQ_REMOVE(al, addr, entries); |
1080 | 0 | sudo_rcstr_delref(addr->sa_str); |
1081 | 0 | sudo_rcstr_delref(addr->sa_host); |
1082 | 0 | free(addr); |
1083 | 0 | } |
1084 | 7.63k | } |
1085 | 7.63k | } |
1086 | | |
1087 | | static struct logsrvd_config_entry server_conf_entries[] = { |
1088 | | { "listen_address", cb_server_listen_address }, |
1089 | | { "timeout", cb_server_timeout }, |
1090 | | { "tcp_keepalive", cb_server_keepalive }, |
1091 | | { "pid_file", cb_server_pid_file }, |
1092 | | { "server_log", cb_server_log }, |
1093 | | #if defined(HAVE_OPENSSL) |
1094 | | { "tls_key", cb_tls_key, offsetof(struct logsrvd_config, server.tls_key_path) }, |
1095 | | { "tls_cacert", cb_tls_cacert, offsetof(struct logsrvd_config, server.tls_cacert_path) }, |
1096 | | { "tls_cert", cb_tls_cert, offsetof(struct logsrvd_config, server.tls_cert_path) }, |
1097 | | { "tls_dhparams", cb_tls_dhparams, offsetof(struct logsrvd_config, server.tls_dhparams_path) }, |
1098 | | { "tls_ciphers_v12", cb_tls_ciphers12, offsetof(struct logsrvd_config, server.tls_ciphers_v12) }, |
1099 | | { "tls_ciphers_v13", cb_tls_ciphers13, offsetof(struct logsrvd_config, server.tls_ciphers_v13) }, |
1100 | | { "tls_checkpeer", cb_tls_checkpeer, offsetof(struct logsrvd_config, server.tls_check_peer) }, |
1101 | | { "tls_verify", cb_tls_verify, offsetof(struct logsrvd_config, server.tls_verify) }, |
1102 | | #endif |
1103 | | { NULL } |
1104 | | }; |
1105 | | |
1106 | | static struct logsrvd_config_entry relay_conf_entries[] = { |
1107 | | { "relay_host", cb_relay_host }, |
1108 | | { "timeout", cb_relay_timeout }, |
1109 | | { "connect_timeout", cb_relay_connect_timeout }, |
1110 | | { "relay_dir", cb_relay_dir }, |
1111 | | { "retry_interval", cb_retry_interval }, |
1112 | | { "store_first", cb_relay_store_first }, |
1113 | | { "tcp_keepalive", cb_relay_keepalive }, |
1114 | | #if defined(HAVE_OPENSSL) |
1115 | | { "tls_key", cb_tls_key, offsetof(struct logsrvd_config, relay.tls_key_path) }, |
1116 | | { "tls_cacert", cb_tls_cacert, offsetof(struct logsrvd_config, relay.tls_cacert_path) }, |
1117 | | { "tls_cert", cb_tls_cert, offsetof(struct logsrvd_config, relay.tls_cert_path) }, |
1118 | | { "tls_dhparams", cb_tls_dhparams, offsetof(struct logsrvd_config, relay.tls_dhparams_path) }, |
1119 | | { "tls_ciphers_v12", cb_tls_ciphers12, offsetof(struct logsrvd_config, relay.tls_ciphers_v12) }, |
1120 | | { "tls_ciphers_v13", cb_tls_ciphers13, offsetof(struct logsrvd_config, relay.tls_ciphers_v13) }, |
1121 | | { "tls_checkpeer", cb_tls_checkpeer, offsetof(struct logsrvd_config, relay.tls_check_peer) }, |
1122 | | { "tls_verify", cb_tls_verify, offsetof(struct logsrvd_config, relay.tls_verify) }, |
1123 | | #endif |
1124 | | { NULL } |
1125 | | }; |
1126 | | |
1127 | | static struct logsrvd_config_entry iolog_conf_entries[] = { |
1128 | | { "iolog_dir", cb_iolog_dir }, |
1129 | | { "iolog_file", cb_iolog_file }, |
1130 | | { "iolog_flush", cb_iolog_flush }, |
1131 | | { "iolog_compress", cb_iolog_compress }, |
1132 | | { "iolog_user", cb_iolog_user }, |
1133 | | { "iolog_group", cb_iolog_group }, |
1134 | | { "iolog_mode", cb_iolog_mode }, |
1135 | | { "log_passwords", cb_iolog_log_passwords }, |
1136 | | { "maxseq", cb_iolog_maxseq }, |
1137 | | { "passprompt_regex", cb_iolog_passprompt_regex }, |
1138 | | { NULL } |
1139 | | }; |
1140 | | |
1141 | | static struct logsrvd_config_entry eventlog_conf_entries[] = { |
1142 | | { "log_type", cb_eventlog_type }, |
1143 | | { "log_format", cb_eventlog_format }, |
1144 | | { "log_exit", cb_eventlog_exit }, |
1145 | | { NULL } |
1146 | | }; |
1147 | | |
1148 | | static struct logsrvd_config_entry syslog_conf_entries[] = { |
1149 | | { "maxlen", cb_syslog_maxlen }, |
1150 | | { "server_facility", cb_syslog_server_facility }, |
1151 | | { "facility", cb_syslog_facility }, |
1152 | | { "reject_priority", cb_syslog_rejectpri }, |
1153 | | { "accept_priority", cb_syslog_acceptpri }, |
1154 | | { "alert_priority", cb_syslog_alertpri }, |
1155 | | { NULL } |
1156 | | }; |
1157 | | |
1158 | | static struct logsrvd_config_entry logfile_conf_entries[] = { |
1159 | | { "path", cb_logfile_path }, |
1160 | | { "time_format", cb_logfile_time_format }, |
1161 | | { NULL } |
1162 | | }; |
1163 | | |
1164 | | static struct logsrvd_config_section logsrvd_config_sections[] = { |
1165 | | { "server", server_conf_entries }, |
1166 | | { "relay", relay_conf_entries }, |
1167 | | { "iolog", iolog_conf_entries }, |
1168 | | { "eventlog", eventlog_conf_entries }, |
1169 | | { "syslog", syslog_conf_entries }, |
1170 | | { "logfile", logfile_conf_entries }, |
1171 | | { NULL } |
1172 | | }; |
1173 | | |
1174 | | static bool |
1175 | | logsrvd_conf_parse(struct logsrvd_config *config, FILE *fp, const char *path) |
1176 | 3.81k | { |
1177 | 3.81k | struct logsrvd_config_section *conf_section = NULL; |
1178 | 3.81k | unsigned int lineno = 0; |
1179 | 3.81k | size_t linesize = 0; |
1180 | 3.81k | char *line = NULL; |
1181 | 3.81k | bool ret = false; |
1182 | 3.81k | debug_decl(logsrvd_conf_parse, SUDO_DEBUG_UTIL); |
1183 | | |
1184 | 28.9k | while (sudo_parseln(&line, &linesize, &lineno, fp, 0) != -1) { |
1185 | 28.1k | struct logsrvd_config_entry *entry; |
1186 | 28.1k | char *ep, *val; |
1187 | | |
1188 | | /* Skip blank, comment or invalid lines. */ |
1189 | 28.1k | if (*line == '\0' || *line == ';') |
1190 | 4.17k | continue; |
1191 | | |
1192 | | /* New section */ |
1193 | 23.9k | if (line[0] == '[') { |
1194 | 4.13k | char *cp, *section_name = line + 1; |
1195 | | |
1196 | 4.13k | if ((ep = strchr(section_name, ']')) == NULL) { |
1197 | 43 | sudo_warnx(U_("%s:%d unmatched '[': %s"), |
1198 | 43 | path, lineno, line); |
1199 | 43 | goto done; |
1200 | 43 | } |
1201 | 4.30k | for (cp = ep + 1; *cp != '\0'; cp++) { |
1202 | 233 | if (!isspace((unsigned char)*cp)) { |
1203 | 18 | sudo_warnx(U_("%s:%d garbage after ']': %s"), |
1204 | 18 | path, lineno, line); |
1205 | 18 | goto done; |
1206 | 18 | } |
1207 | 233 | } |
1208 | 4.07k | *ep = '\0'; |
1209 | 10.9k | for (conf_section = logsrvd_config_sections; conf_section->name != NULL; |
1210 | 10.8k | conf_section++) { |
1211 | 10.8k | if (strcasecmp(section_name, conf_section->name) == 0) |
1212 | 3.93k | break; |
1213 | 10.8k | } |
1214 | 4.07k | if (conf_section->name == NULL) { |
1215 | 139 | sudo_warnx(U_("%s:%d invalid config section: %s"), |
1216 | 139 | path, lineno, section_name); |
1217 | 139 | goto done; |
1218 | 139 | } |
1219 | 3.93k | continue; |
1220 | 4.07k | } |
1221 | | |
1222 | 19.7k | if ((ep = strchr(line, '=')) == NULL) { |
1223 | 175 | sudo_warnx(U_("%s:%d invalid configuration line: %s"), |
1224 | 175 | path, lineno, line); |
1225 | 175 | goto done; |
1226 | 175 | } |
1227 | | |
1228 | 19.6k | if (conf_section == NULL) { |
1229 | 17 | sudo_warnx(U_("%s:%d expected section name: %s"), |
1230 | 17 | path, lineno, line); |
1231 | 17 | goto done; |
1232 | 17 | } |
1233 | | |
1234 | 19.6k | val = ep + 1; |
1235 | 19.6k | while (isspace((unsigned char)*val)) |
1236 | 208 | val++; |
1237 | 19.8k | while (ep > line && isspace((unsigned char)ep[-1])) |
1238 | 199 | ep--; |
1239 | 19.6k | *ep = '\0'; |
1240 | 96.8k | for (entry = conf_section->entries; entry->conf_str != NULL; entry++) { |
1241 | 96.7k | if (strcasecmp(line, entry->conf_str) == 0) { |
1242 | 19.4k | if (!entry->setter(config, val, entry->offset)) { |
1243 | 2.45k | sudo_warnx(U_("invalid value for %s: %s"), |
1244 | 2.45k | entry->conf_str, val); |
1245 | 2.45k | goto done; |
1246 | 2.45k | } |
1247 | 16.9k | break; |
1248 | 19.4k | } |
1249 | 96.7k | } |
1250 | 17.1k | if (entry->conf_str == NULL) { |
1251 | 148 | sudo_warnx(U_("%s:%d [%s] illegal key: %s"), path, lineno, |
1252 | 148 | conf_section->name, line); |
1253 | 148 | goto done; |
1254 | 148 | } |
1255 | 17.1k | } |
1256 | 817 | ret = true; |
1257 | | |
1258 | 3.81k | done: |
1259 | 3.81k | free(line); |
1260 | 3.81k | debug_return_bool(ret); |
1261 | 3.81k | } |
1262 | | |
1263 | | static FILE * |
1264 | | logsrvd_open_log_file(const char *path, int flags) |
1265 | 0 | { |
1266 | 0 | mode_t oldmask; |
1267 | 0 | FILE *fp = NULL; |
1268 | 0 | const char *omode; |
1269 | 0 | int fd; |
1270 | 0 | debug_decl(logsrvd_open_log_file, SUDO_DEBUG_UTIL); |
1271 | |
|
1272 | 0 | if (ISSET(flags, O_APPEND)) { |
1273 | 0 | omode = "a"; |
1274 | 0 | } else { |
1275 | 0 | omode = "w"; |
1276 | 0 | } |
1277 | 0 | oldmask = umask(S_IRWXG|S_IRWXO); |
1278 | 0 | fd = open(path, flags, S_IRUSR|S_IWUSR); |
1279 | 0 | (void)umask(oldmask); |
1280 | 0 | if (fd == -1 || (fp = fdopen(fd, omode)) == NULL) { |
1281 | 0 | sudo_warn(U_("unable to open log file %s"), path); |
1282 | 0 | if (fd != -1) |
1283 | 0 | close(fd); |
1284 | 0 | } |
1285 | |
|
1286 | 0 | debug_return_ptr(fp); |
1287 | 0 | } |
1288 | | |
1289 | | static FILE * |
1290 | | logsrvd_open_eventlog(struct logsrvd_config *config) |
1291 | 0 | { |
1292 | 0 | int flags; |
1293 | 0 | debug_decl(logsrvd_open_eventlog, SUDO_DEBUG_UTIL); |
1294 | | |
1295 | | /* Cannot append to a JSON file. */ |
1296 | 0 | if (config->eventlog.log_format == EVLOG_JSON) { |
1297 | 0 | flags = O_RDWR|O_CREAT; |
1298 | 0 | } else { |
1299 | 0 | flags = O_WRONLY|O_APPEND|O_CREAT; |
1300 | 0 | } |
1301 | 0 | debug_return_ptr(logsrvd_open_log_file(config->logfile.path, flags)); |
1302 | 0 | } |
1303 | | |
1304 | | static FILE * |
1305 | | logsrvd_stub_open_log(int type, const char *logfile) |
1306 | 0 | { |
1307 | | /* Actual open already done by logsrvd_open_eventlog() */ |
1308 | 0 | return logsrvd_config->logfile.stream; |
1309 | 0 | } |
1310 | | |
1311 | | static void |
1312 | | logsrvd_stub_close_log(int type, FILE *fp) |
1313 | 0 | { |
1314 | 0 | return; |
1315 | 0 | } |
1316 | | |
1317 | | /* Set eventlog configuration settings from logsrvd config. */ |
1318 | | static void |
1319 | | logsrvd_conf_eventlog_setconf(struct logsrvd_config *config) |
1320 | 0 | { |
1321 | 0 | debug_decl(logsrvd_conf_eventlog_setconf, SUDO_DEBUG_UTIL); |
1322 | |
|
1323 | 0 | eventlog_set_type(config->eventlog.log_type); |
1324 | 0 | eventlog_set_format(config->eventlog.log_format); |
1325 | 0 | eventlog_set_syslog_acceptpri(config->syslog.acceptpri); |
1326 | 0 | eventlog_set_syslog_rejectpri(config->syslog.rejectpri); |
1327 | 0 | eventlog_set_syslog_alertpri(config->syslog.alertpri); |
1328 | 0 | eventlog_set_syslog_maxlen(config->syslog.maxlen); |
1329 | 0 | eventlog_set_logpath(config->logfile.path); |
1330 | 0 | eventlog_set_time_fmt(config->logfile.time_format); |
1331 | 0 | eventlog_set_open_log(logsrvd_stub_open_log); |
1332 | 0 | eventlog_set_close_log(logsrvd_stub_close_log); |
1333 | |
|
1334 | 0 | debug_return; |
1335 | 0 | } |
1336 | | |
1337 | | /* Set I/O log configuration settings from logsrvd config. */ |
1338 | | static void |
1339 | | logsrvd_conf_iolog_setconf(struct logsrvd_config *config) |
1340 | 0 | { |
1341 | 0 | debug_decl(logsrvd_conf_iolog_setconf, SUDO_DEBUG_UTIL); |
1342 | |
|
1343 | 0 | iolog_set_defaults(); |
1344 | 0 | iolog_set_compress(config->iolog.compress); |
1345 | 0 | iolog_set_flush(config->iolog.flush); |
1346 | 0 | iolog_set_owner(config->iolog.uid, config->iolog.gid); |
1347 | 0 | iolog_set_mode(config->iolog.mode); |
1348 | 0 | iolog_set_maxseq(config->iolog.maxseq); |
1349 | |
|
1350 | 0 | debug_return; |
1351 | 0 | } |
1352 | | |
1353 | | /* |
1354 | | * Conversation function for use by sudo_warn/sudo_fatal. |
1355 | | * Logs to stdout/stderr. |
1356 | | */ |
1357 | | static int |
1358 | | logsrvd_conv_stderr(int num_msgs, const struct sudo_conv_message msgs[], |
1359 | | struct sudo_conv_reply replies[], struct sudo_conv_callback *callback) |
1360 | 0 | { |
1361 | 0 | int i; |
1362 | 0 | debug_decl(logsrvd_conv_stderr, SUDO_DEBUG_UTIL); |
1363 | |
|
1364 | 0 | for (i = 0; i < num_msgs; i++) { |
1365 | 0 | if (fputs(msgs[i].msg, stderr) == EOF) |
1366 | 0 | debug_return_int(-1); |
1367 | 0 | } |
1368 | | |
1369 | 0 | debug_return_int(0); |
1370 | 0 | } |
1371 | | |
1372 | | /* |
1373 | | * Conversation function for use by sudo_warn/sudo_fatal. |
1374 | | * Acts as a no-op log sink. |
1375 | | */ |
1376 | | static int |
1377 | | logsrvd_conv_none(int num_msgs, const struct sudo_conv_message msgs[], |
1378 | | struct sudo_conv_reply replies[], struct sudo_conv_callback *callback) |
1379 | 0 | { |
1380 | | /* Also write to stderr if still in the foreground. */ |
1381 | 0 | if (logsrvd_warn_enable_stderr) { |
1382 | 0 | (void)logsrvd_conv_stderr(num_msgs, msgs, replies, callback); |
1383 | 0 | } |
1384 | |
|
1385 | 0 | return 0; |
1386 | 0 | } |
1387 | | |
1388 | | /* |
1389 | | * Conversation function for use by sudo_warn/sudo_fatal. |
1390 | | * Logs to syslog. |
1391 | | */ |
1392 | | static int |
1393 | | logsrvd_conv_syslog(int num_msgs, const struct sudo_conv_message msgs[], |
1394 | | struct sudo_conv_reply replies[], struct sudo_conv_callback *callback) |
1395 | 0 | { |
1396 | 0 | char *buf = NULL, *cp = NULL; |
1397 | 0 | const char *progname; |
1398 | 0 | size_t proglen, bufsize = 0; |
1399 | 0 | int i; |
1400 | 0 | debug_decl(logsrvd_conv_syslog, SUDO_DEBUG_UTIL); |
1401 | |
|
1402 | 0 | if (logsrvd_config == NULL) { |
1403 | 0 | debug_return_int(logsrvd_conv_stderr(num_msgs, msgs, replies, callback)); |
1404 | 0 | } |
1405 | | |
1406 | | /* Also write to stderr if still in the foreground. */ |
1407 | 0 | if (logsrvd_warn_enable_stderr) { |
1408 | 0 | (void)logsrvd_conv_stderr(num_msgs, msgs, replies, callback); |
1409 | 0 | } |
1410 | | |
1411 | | /* |
1412 | | * Concat messages into a flag string that we can syslog. |
1413 | | */ |
1414 | 0 | progname = getprogname(); |
1415 | 0 | proglen = strlen(progname); |
1416 | 0 | for (i = 0; i < num_msgs; i++) { |
1417 | 0 | const char *msg = msgs[i].msg; |
1418 | 0 | size_t len = strlen(msg); |
1419 | 0 | size_t used = (size_t)(cp - buf); |
1420 | | |
1421 | | /* Strip leading "sudo_logsrvd: " prefix. */ |
1422 | 0 | if (strncmp(msg, progname, proglen) == 0) { |
1423 | 0 | msg += proglen; |
1424 | 0 | len -= proglen; |
1425 | 0 | if (len == 0) { |
1426 | | /* Skip over ": " string that follows program name. */ |
1427 | 0 | if (i + 1 < num_msgs && strcmp(msgs[i + 1].msg, ": ") == 0) { |
1428 | 0 | i++; |
1429 | 0 | continue; |
1430 | 0 | } |
1431 | 0 | } else if (msg[0] == ':' && msg[1] == ' ') { |
1432 | | /* Handle "progname: " */ |
1433 | 0 | msg += 2; |
1434 | 0 | len -= 2; |
1435 | 0 | } |
1436 | 0 | } |
1437 | | |
1438 | | /* Strip off trailing newlines. */ |
1439 | 0 | while (len > 1 && msg[len - 1] == '\n') |
1440 | 0 | len--; |
1441 | 0 | if (len == 0) |
1442 | 0 | continue; |
1443 | | |
1444 | 0 | if (len >= bufsize - used) { |
1445 | 0 | bufsize += 1024; |
1446 | 0 | char *tmp = realloc(buf, bufsize); |
1447 | 0 | if (tmp == NULL) { |
1448 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
1449 | 0 | free(buf); |
1450 | 0 | debug_return_int(-1); |
1451 | 0 | } |
1452 | 0 | buf = tmp; |
1453 | 0 | cp = tmp + used; |
1454 | 0 | } |
1455 | 0 | memcpy(cp, msg, len); |
1456 | 0 | cp[len] = '\0'; |
1457 | 0 | cp += len; |
1458 | 0 | } |
1459 | 0 | if (buf != NULL) { |
1460 | 0 | openlog(progname, 0, logsrvd_config->syslog.server_facility); |
1461 | 0 | syslog(LOG_ERR, "%s", buf); |
1462 | 0 | free(buf); |
1463 | | |
1464 | | /* Restore old syslog settings. */ |
1465 | 0 | if (logsrvd_config->eventlog.log_type == EVLOG_SYSLOG) |
1466 | 0 | openlog("sudo", 0, logsrvd_config->syslog.facility); |
1467 | 0 | } |
1468 | |
|
1469 | 0 | debug_return_int(0); |
1470 | 0 | } |
1471 | | |
1472 | | /* |
1473 | | * Conversation function for use by sudo_warn/sudo_fatal. |
1474 | | * Logs to an already-open log file. |
1475 | | */ |
1476 | | static int |
1477 | | logsrvd_conv_logfile(int num_msgs, const struct sudo_conv_message msgs[], |
1478 | | struct sudo_conv_reply replies[], struct sudo_conv_callback *callback) |
1479 | 0 | { |
1480 | 0 | const char *progname; |
1481 | 0 | size_t proglen; |
1482 | 0 | int i; |
1483 | 0 | debug_decl(logsrvd_conv_logfile, SUDO_DEBUG_UTIL); |
1484 | |
|
1485 | 0 | if (logsrvd_config == NULL) { |
1486 | 0 | debug_return_int(logsrvd_conv_stderr(num_msgs, msgs, replies, callback)); |
1487 | 0 | } |
1488 | | |
1489 | | /* Also write to stderr if still in the foreground. */ |
1490 | 0 | if (logsrvd_warn_enable_stderr) { |
1491 | 0 | (void)logsrvd_conv_stderr(num_msgs, msgs, replies, callback); |
1492 | 0 | } |
1493 | |
|
1494 | 0 | if (logsrvd_config->server.log_stream == NULL) { |
1495 | 0 | errno = EBADF; |
1496 | 0 | debug_return_int(-1); |
1497 | 0 | } |
1498 | | |
1499 | 0 | progname = getprogname(); |
1500 | 0 | proglen = strlen(progname); |
1501 | 0 | for (i = 0; i < num_msgs; i++) { |
1502 | 0 | const char *msg = msgs[i].msg; |
1503 | 0 | size_t len = strlen(msg); |
1504 | | |
1505 | | /* Strip leading "sudo_logsrvd: " prefix. */ |
1506 | 0 | if (strncmp(msg, progname, proglen) == 0) { |
1507 | 0 | msg += proglen; |
1508 | 0 | len -= proglen; |
1509 | 0 | if (len == 0) { |
1510 | | /* Skip over ": " string that follows program name. */ |
1511 | 0 | if (i + 1 < num_msgs && strcmp(msgs[i + 1].msg, ": ") == 0) { |
1512 | 0 | i++; |
1513 | 0 | continue; |
1514 | 0 | } |
1515 | 0 | } else if (msg[0] == ':' && msg[1] == ' ') { |
1516 | | /* Handle "progname: " */ |
1517 | 0 | msg += 2; |
1518 | 0 | len -= 2; |
1519 | 0 | } |
1520 | 0 | } |
1521 | | |
1522 | 0 | if (fwrite(msg, len, 1, logsrvd_config->server.log_stream) != 1) |
1523 | 0 | debug_return_int(-1); |
1524 | 0 | } |
1525 | | |
1526 | 0 | debug_return_int(0); |
1527 | 0 | } |
1528 | | |
1529 | | /* Free the specified struct logsrvd_config and its contents. */ |
1530 | | static void |
1531 | | logsrvd_conf_free(struct logsrvd_config *config) |
1532 | 3.81k | { |
1533 | 3.81k | debug_decl(logsrvd_conf_free, SUDO_DEBUG_UTIL); |
1534 | | |
1535 | 3.81k | if (config == NULL) |
1536 | 0 | debug_return; |
1537 | | |
1538 | | /* struct logsrvd_config_server */ |
1539 | 3.81k | address_list_delref(&config->server.addresses.addrs); |
1540 | 3.81k | free(config->server.pid_file); |
1541 | 3.81k | free(config->server.log_file); |
1542 | 3.81k | if (config->server.log_stream != NULL) |
1543 | 0 | fclose(config->server.log_stream); |
1544 | 3.81k | #if defined(HAVE_OPENSSL) |
1545 | 3.81k | free(config->server.tls_key_path); |
1546 | 3.81k | free(config->server.tls_cert_path); |
1547 | 3.81k | free(config->server.tls_cacert_path); |
1548 | 3.81k | free(config->server.tls_dhparams_path); |
1549 | 3.81k | free(config->server.tls_ciphers_v12); |
1550 | 3.81k | free(config->server.tls_ciphers_v13); |
1551 | | |
1552 | 3.81k | if (config->server.ssl_ctx != NULL) |
1553 | 0 | SSL_CTX_free(config->server.ssl_ctx); |
1554 | 3.81k | #endif |
1555 | | |
1556 | | /* struct logsrvd_config_relay */ |
1557 | 3.81k | address_list_delref(&config->relay.relays.addrs); |
1558 | 3.81k | free(config->relay.relay_dir); |
1559 | 3.81k | #if defined(HAVE_OPENSSL) |
1560 | 3.81k | free(config->relay.tls_key_path); |
1561 | 3.81k | free(config->relay.tls_cert_path); |
1562 | 3.81k | free(config->relay.tls_cacert_path); |
1563 | 3.81k | free(config->relay.tls_dhparams_path); |
1564 | 3.81k | free(config->relay.tls_ciphers_v12); |
1565 | 3.81k | free(config->relay.tls_ciphers_v13); |
1566 | | |
1567 | 3.81k | if (config->relay.ssl_ctx != NULL) |
1568 | 0 | SSL_CTX_free(config->relay.ssl_ctx); |
1569 | 3.81k | #endif |
1570 | | |
1571 | | /* struct logsrvd_config_iolog */ |
1572 | 3.81k | free(config->iolog.iolog_dir); |
1573 | 3.81k | free(config->iolog.iolog_file); |
1574 | 3.81k | iolog_pwfilt_free(config->iolog.passprompt_regex); |
1575 | | |
1576 | | /* struct logsrvd_config_logfile */ |
1577 | 3.81k | free(config->logfile.path); |
1578 | 3.81k | free(config->logfile.time_format); |
1579 | 3.81k | if (config->logfile.stream != NULL) |
1580 | 0 | fclose(config->logfile.stream); |
1581 | | |
1582 | 3.81k | free(config); |
1583 | | |
1584 | 3.81k | debug_return; |
1585 | 3.81k | } |
1586 | | |
1587 | | /* Allocate a new struct logsrvd_config and set default values. */ |
1588 | | static struct logsrvd_config * |
1589 | | logsrvd_conf_alloc(void) |
1590 | 3.81k | { |
1591 | 3.81k | struct logsrvd_config *config; |
1592 | 3.81k | debug_decl(logsrvd_conf_alloc, SUDO_DEBUG_UTIL); |
1593 | | |
1594 | 3.81k | if ((config = calloc(1, sizeof(*config))) == NULL) { |
1595 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
1596 | 0 | debug_return_ptr(NULL); |
1597 | 0 | } |
1598 | | |
1599 | | /* Relay defaults */ |
1600 | 3.81k | TAILQ_INIT(&config->relay.relays.addrs); |
1601 | 3.81k | config->relay.relays.refcnt = 1; |
1602 | 3.81k | config->relay.timeout.tv_sec = DEFAULT_SOCKET_TIMEOUT_SEC; |
1603 | 3.81k | config->relay.connect_timeout.tv_sec = DEFAULT_SOCKET_TIMEOUT_SEC; |
1604 | 3.81k | config->relay.tcp_keepalive = true; |
1605 | 3.81k | config->relay.retry_interval = 30; |
1606 | 3.81k | if (!cb_relay_dir(config, _PATH_SUDO_RELAY_DIR, 0)) |
1607 | 0 | goto bad; |
1608 | 3.81k | #if defined(HAVE_OPENSSL) |
1609 | 3.81k | config->relay.tls_verify = -1; |
1610 | 3.81k | config->relay.tls_check_peer = -1; |
1611 | 3.81k | #endif |
1612 | | |
1613 | | /* Server defaults */ |
1614 | 3.81k | TAILQ_INIT(&config->server.addresses.addrs); |
1615 | 3.81k | config->server.addresses.refcnt = 1; |
1616 | 3.81k | config->server.timeout.tv_sec = DEFAULT_SOCKET_TIMEOUT_SEC; |
1617 | 3.81k | config->server.tcp_keepalive = true; |
1618 | 3.81k | config->server.log_type = SERVER_LOG_SYSLOG; |
1619 | 3.81k | config->server.pid_file = strdup(_PATH_SUDO_LOGSRVD_PID); |
1620 | 3.81k | if (config->server.pid_file == NULL) { |
1621 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
1622 | 0 | goto bad; |
1623 | 0 | } |
1624 | | |
1625 | 3.81k | #if defined(HAVE_OPENSSL) |
1626 | | /* |
1627 | | * Only set default CA and cert paths if the files actually exist. |
1628 | | * This ensures we don't enable TLS by default when it is not configured. |
1629 | | */ |
1630 | 3.81k | if (access(DEFAULT_CA_CERT_PATH, R_OK) == 0) { |
1631 | 0 | config->server.tls_cacert_path = strdup(DEFAULT_CA_CERT_PATH); |
1632 | 0 | if (config->server.tls_cacert_path == NULL) { |
1633 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
1634 | 0 | goto bad; |
1635 | 0 | } |
1636 | 0 | } |
1637 | 3.81k | if (access(DEFAULT_SERVER_CERT_PATH, R_OK) == 0) { |
1638 | 0 | config->server.tls_cert_path = strdup(DEFAULT_SERVER_CERT_PATH); |
1639 | 0 | if (config->server.tls_cert_path == NULL) { |
1640 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
1641 | 0 | goto bad; |
1642 | 0 | } |
1643 | 0 | } |
1644 | 3.81k | config->server.tls_key_path = strdup(DEFAULT_SERVER_KEY_PATH); |
1645 | 3.81k | if (config->server.tls_key_path == NULL) { |
1646 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
1647 | 0 | goto bad; |
1648 | 0 | } |
1649 | 3.81k | config->server.tls_verify = true; |
1650 | 3.81k | config->server.tls_check_peer = false; |
1651 | 3.81k | #endif |
1652 | | |
1653 | | /* I/O log defaults */ |
1654 | 3.81k | config->iolog.compress = false; |
1655 | 3.81k | config->iolog.flush = true; |
1656 | 3.81k | config->iolog.mode = S_IRUSR|S_IWUSR; |
1657 | 3.81k | config->iolog.maxseq = SESSID_MAX; |
1658 | 3.81k | if (!cb_iolog_dir(config, _PATH_SUDO_IO_LOGDIR, 0)) |
1659 | 0 | goto bad; |
1660 | 3.81k | if (!cb_iolog_file(config, "%{seq}", 0)) |
1661 | 0 | goto bad; |
1662 | 3.81k | config->iolog.uid = ROOT_UID; |
1663 | 3.81k | config->iolog.gid = ROOT_GID; |
1664 | 3.81k | config->iolog.gid_set = false; |
1665 | 3.81k | config->iolog.log_passwords = true; |
1666 | | |
1667 | | /* Event log defaults */ |
1668 | 3.81k | config->eventlog.log_type = EVLOG_SYSLOG; |
1669 | 3.81k | config->eventlog.log_format = EVLOG_SUDO; |
1670 | 3.81k | config->eventlog.log_exit = false; |
1671 | | |
1672 | | /* Syslog defaults */ |
1673 | 3.81k | config->syslog.maxlen = 960; |
1674 | 3.81k | config->syslog.server_facility = LOG_DAEMON; |
1675 | 3.81k | if (!cb_syslog_facility(config, LOGFAC, 0)) { |
1676 | 0 | sudo_warnx(U_("unknown syslog facility %s"), LOGFAC); |
1677 | 0 | goto bad; |
1678 | 0 | } |
1679 | 3.81k | if (!cb_syslog_acceptpri(config, PRI_SUCCESS, 0)) { |
1680 | 0 | sudo_warnx(U_("unknown syslog priority %s"), PRI_SUCCESS); |
1681 | 0 | goto bad; |
1682 | 0 | } |
1683 | 3.81k | if (!cb_syslog_rejectpri(config, PRI_FAILURE, 0)) { |
1684 | 0 | sudo_warnx(U_("unknown syslog priority %s"), PRI_FAILURE); |
1685 | 0 | goto bad; |
1686 | 0 | } |
1687 | 3.81k | if (!cb_syslog_alertpri(config, PRI_FAILURE, 0)) { |
1688 | 0 | sudo_warnx(U_("unknown syslog priority %s"), PRI_FAILURE); |
1689 | 0 | goto bad; |
1690 | 0 | } |
1691 | | |
1692 | | /* Log file defaults */ |
1693 | 3.81k | if (!cb_logfile_time_format(config, "%h %e %T", 0)) |
1694 | 0 | goto bad; |
1695 | 3.81k | if (!cb_logfile_path(config, _PATH_SUDO_LOGFILE, 0)) |
1696 | 0 | goto bad; |
1697 | | |
1698 | 3.81k | debug_return_ptr(config); |
1699 | 0 | bad: |
1700 | 0 | logsrvd_conf_free(config); |
1701 | 0 | debug_return_ptr(NULL); |
1702 | 0 | } |
1703 | | |
1704 | | static bool |
1705 | | logsrvd_conf_apply(struct logsrvd_config *config) |
1706 | 817 | { |
1707 | 817 | #if defined(HAVE_OPENSSL) |
1708 | 817 | struct server_address *addr; |
1709 | 817 | #endif |
1710 | 817 | debug_decl(logsrvd_conf_apply, SUDO_DEBUG_UTIL); |
1711 | | |
1712 | | /* There can be multiple passprompt regular expressions. */ |
1713 | 817 | if (config->iolog.passprompt_regex == NULL) { |
1714 | 796 | if (!cb_iolog_passprompt_regex(config, PASSPROMPT_REGEX, 0)) |
1715 | 0 | debug_return_bool(false); |
1716 | 796 | } |
1717 | | |
1718 | | /* There can be multiple addresses so we can't set a default earlier. */ |
1719 | 817 | if (TAILQ_EMPTY(&config->server.addresses.addrs)) { |
1720 | | /* Enable plaintext listender. */ |
1721 | 817 | if (!cb_server_listen_address(config, "*:" DEFAULT_PORT, 0)) |
1722 | 817 | debug_return_bool(false); |
1723 | 0 | #if defined(HAVE_OPENSSL) |
1724 | | /* If a certificate was specified, enable the TLS listener too. */ |
1725 | 0 | if (config->server.tls_cert_path != NULL) { |
1726 | 0 | if (!cb_server_listen_address(config, "*:" DEFAULT_PORT_TLS "(tls)", 0)) |
1727 | 0 | debug_return_bool(false); |
1728 | 0 | } |
1729 | 0 | } else { |
1730 | | /* Check that TLS configuration is valid. */ |
1731 | 0 | TAILQ_FOREACH(addr, &config->server.addresses.addrs, entries) { |
1732 | 0 | if (!addr->tls) |
1733 | 0 | continue; |
1734 | | /* |
1735 | | * If a TLS listener was explicitly enabled but the cert path |
1736 | | * was not, use the default. |
1737 | | */ |
1738 | 0 | if (config->server.tls_cert_path == NULL) { |
1739 | 0 | config->server.tls_cert_path = |
1740 | 0 | strdup(DEFAULT_SERVER_CERT_PATH); |
1741 | 0 | if (config->server.tls_cert_path == NULL) { |
1742 | 0 | sudo_warnx(U_("%s: %s"), __func__, |
1743 | 0 | U_("unable to allocate memory")); |
1744 | 0 | debug_return_bool(false); |
1745 | 0 | } |
1746 | 0 | } |
1747 | 0 | break; |
1748 | 0 | } |
1749 | 0 | #endif /* HAVE_OPENSSL */ |
1750 | 0 | } |
1751 | | |
1752 | 0 | #if defined(HAVE_OPENSSL) |
1753 | 0 | TAILQ_FOREACH(addr, &config->server.addresses.addrs, entries) { |
1754 | 0 | if (!addr->tls) |
1755 | 0 | continue; |
1756 | | /* Create a TLS context for the server. */ |
1757 | 0 | config->server.ssl_ctx = init_tls_context( |
1758 | 0 | config->server.tls_cacert_path, config->server.tls_cert_path, |
1759 | 0 | config->server.tls_key_path, config->server.tls_dhparams_path, |
1760 | 0 | config->server.tls_ciphers_v12, config->server.tls_ciphers_v13, |
1761 | 0 | config->server.tls_verify); |
1762 | 0 | if (config->server.ssl_ctx == NULL) { |
1763 | 0 | sudo_warnx("%s", U_("unable to initialize server TLS context")); |
1764 | 0 | debug_return_bool(false); |
1765 | 0 | } |
1766 | 0 | break; |
1767 | 0 | } |
1768 | | |
1769 | 0 | if (TLS_CONFIGURED(config->relay)) { |
1770 | 0 | TAILQ_FOREACH(addr, &config->relay.relays.addrs, entries) { |
1771 | 0 | if (!addr->tls) |
1772 | 0 | continue; |
1773 | | /* Create a TLS context for the relay. */ |
1774 | 0 | config->relay.ssl_ctx = init_tls_context( |
1775 | 0 | TLS_RELAY_STR(config, tls_cacert_path), |
1776 | 0 | TLS_RELAY_STR(config, tls_cert_path), |
1777 | 0 | TLS_RELAY_STR(config, tls_key_path), |
1778 | 0 | TLS_RELAY_STR(config, tls_dhparams_path), |
1779 | 0 | TLS_RELAY_STR(config, tls_ciphers_v12), |
1780 | 0 | TLS_RELAY_STR(config, tls_ciphers_v13), |
1781 | 0 | TLS_RELAY_INT(config, tls_verify)); |
1782 | 0 | if (config->relay.ssl_ctx == NULL) { |
1783 | 0 | sudo_warnx("%s", U_("unable to initialize relay TLS context")); |
1784 | 0 | debug_return_bool(false); |
1785 | 0 | } |
1786 | 0 | break; |
1787 | 0 | } |
1788 | 0 | } |
1789 | 0 | #endif /* HAVE_OPENSSL */ |
1790 | | |
1791 | | /* Clear store_first if not relaying. */ |
1792 | 0 | if (TAILQ_EMPTY(&config->relay.relays.addrs)) |
1793 | 0 | config->relay.store_first = false; |
1794 | | |
1795 | | /* Open server log if specified. */ |
1796 | 0 | switch (config->server.log_type) { |
1797 | 0 | case SERVER_LOG_SYSLOG: |
1798 | 0 | sudo_warn_set_conversation(logsrvd_conv_syslog); |
1799 | 0 | break; |
1800 | 0 | case SERVER_LOG_FILE: |
1801 | 0 | config->server.log_stream = |
1802 | 0 | logsrvd_open_log_file(config->server.log_file, O_WRONLY|O_APPEND|O_CREAT); |
1803 | 0 | if (config->server.log_stream == NULL) |
1804 | 0 | debug_return_bool(false); |
1805 | 0 | sudo_warn_set_conversation(logsrvd_conv_logfile); |
1806 | 0 | break; |
1807 | 0 | case SERVER_LOG_NONE: |
1808 | 0 | sudo_warn_set_conversation(logsrvd_conv_none); |
1809 | 0 | break; |
1810 | 0 | case SERVER_LOG_STDERR: |
1811 | | /* Default is stderr. */ |
1812 | 0 | sudo_warn_set_conversation(NULL); |
1813 | 0 | break; |
1814 | 0 | default: |
1815 | 0 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
1816 | 0 | "cannot open unknown log type %d", config->eventlog.log_type); |
1817 | 0 | break; |
1818 | 0 | } |
1819 | | |
1820 | | /* Open event log if specified. */ |
1821 | 0 | switch (config->eventlog.log_type) { |
1822 | 0 | case EVLOG_SYSLOG: |
1823 | 0 | openlog("sudo", 0, config->syslog.facility); |
1824 | 0 | break; |
1825 | 0 | case EVLOG_FILE: |
1826 | 0 | config->logfile.stream = logsrvd_open_eventlog(config); |
1827 | 0 | if (config->logfile.stream == NULL) |
1828 | 0 | debug_return_bool(false); |
1829 | 0 | break; |
1830 | 0 | case EVLOG_NONE: |
1831 | 0 | break; |
1832 | 0 | default: |
1833 | 0 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
1834 | 0 | "cannot open unknown log type %d", config->eventlog.log_type); |
1835 | 0 | break; |
1836 | 0 | } |
1837 | | |
1838 | | /* |
1839 | | * Update event and I/O log library config and install the new |
1840 | | * logsrvd config. We must not fail past this point or the event |
1841 | | * and I/O log config will be inconsistent with the logsrvd config. |
1842 | | */ |
1843 | 0 | logsrvd_conf_iolog_setconf(config); |
1844 | 0 | logsrvd_conf_eventlog_setconf(config); |
1845 | |
|
1846 | 0 | logsrvd_conf_free(logsrvd_config); |
1847 | 0 | logsrvd_config = config; |
1848 | |
|
1849 | 0 | debug_return_bool(true); |
1850 | 0 | } |
1851 | | |
1852 | | /* |
1853 | | * Read .ini style logsrvd.conf file. |
1854 | | * If path is NULL, use _PATH_SUDO_LOGSRVD_CONF. |
1855 | | * Note that we use '#' not ';' for the comment character. |
1856 | | */ |
1857 | | bool |
1858 | | logsrvd_conf_read(const char *path) |
1859 | 3.81k | { |
1860 | 3.81k | struct logsrvd_config *config; |
1861 | 3.81k | char conf_file[PATH_MAX]; |
1862 | 3.81k | bool ret = false; |
1863 | 3.81k | FILE *fp = NULL; |
1864 | 3.81k | int fd = -1; |
1865 | 3.81k | debug_decl(logsrvd_conf_read, SUDO_DEBUG_UTIL); |
1866 | | |
1867 | 3.81k | config = logsrvd_conf_alloc(); |
1868 | | |
1869 | 3.81k | if (path != NULL) { |
1870 | 3.81k | if (strlcpy(conf_file, path, sizeof(conf_file)) >= sizeof(conf_file)) |
1871 | 0 | errno = ENAMETOOLONG; |
1872 | 3.81k | else |
1873 | 3.81k | fd = open(conf_file, O_RDONLY); |
1874 | 3.81k | } else { |
1875 | 0 | fd = sudo_open_conf_path(_PATH_SUDO_LOGSRVD_CONF, conf_file, |
1876 | 0 | sizeof(conf_file), NULL); |
1877 | 0 | } |
1878 | 3.81k | if (fd != -1) |
1879 | 3.81k | fp = fdopen(fd, "r"); |
1880 | 3.81k | if (fp == NULL) { |
1881 | 0 | if (path != NULL || errno != ENOENT) { |
1882 | 0 | sudo_warn("%s", conf_file); |
1883 | 0 | goto done; |
1884 | 0 | } |
1885 | 3.81k | } else { |
1886 | 3.81k | if (!logsrvd_conf_parse(config, fp, conf_file)) |
1887 | 2.99k | goto done; |
1888 | 3.81k | } |
1889 | | |
1890 | | /* Install new config */ |
1891 | 817 | if (logsrvd_conf_apply(config)) { |
1892 | 0 | config = NULL; |
1893 | 0 | ret = true; |
1894 | 0 | } |
1895 | | |
1896 | 3.81k | done: |
1897 | 3.81k | logsrvd_conf_free(config); |
1898 | 3.81k | if (fp != NULL) |
1899 | 3.81k | fclose(fp); |
1900 | 3.81k | debug_return_bool(ret); |
1901 | 3.81k | } |
1902 | | |
1903 | | void |
1904 | | logsrvd_conf_cleanup(void) |
1905 | 0 | { |
1906 | 0 | debug_decl(logsrvd_conf_cleanup, SUDO_DEBUG_UTIL); |
1907 | |
|
1908 | 0 | logsrvd_conf_free(logsrvd_config); |
1909 | 0 | logsrvd_config = NULL; |
1910 | |
|
1911 | 0 | debug_return; |
1912 | 0 | } |
1913 | | |
1914 | | void |
1915 | | logsrvd_warn_stderr(bool enabled) |
1916 | 0 | { |
1917 | 0 | logsrvd_warn_enable_stderr = enabled; |
1918 | 0 | } |