/src/sudo/plugins/sudoers/parser_warnx.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2022-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 | | #include <config.h> |
20 | | |
21 | | #include <stdarg.h> |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | |
25 | | #include <sudoers.h> |
26 | | |
27 | | bool |
28 | | parser_vwarnx(const struct sudoers_context *ctx, const char *file, int line, |
29 | | int column, bool strict, bool quiet, const char * restrict fmt, va_list ap) |
30 | 3.35k | { |
31 | 3.35k | bool ret = true; |
32 | 3.35k | debug_decl(parser_warnx, SUDOERS_DEBUG_DEFAULTS); |
33 | | |
34 | 3.35k | if (strict && sudoers_error_hook != NULL) { |
35 | 0 | va_list ap2; |
36 | |
|
37 | 0 | va_copy(ap2, ap); |
38 | 0 | ret = sudoers_error_hook(ctx, file, line, column, fmt, ap2); |
39 | 0 | va_end(ap2); |
40 | 0 | } |
41 | | |
42 | 3.35k | if (!quiet) { |
43 | 3.35k | int oldlocale; |
44 | 3.35k | char *errstr; |
45 | | |
46 | 3.35k | sudoers_setlocale(SUDOERS_LOCALE_USER, &oldlocale); |
47 | 3.35k | if (vasprintf(&errstr, _(fmt), ap) == -1) { |
48 | 0 | errstr = NULL; |
49 | 0 | ret = false; |
50 | 3.35k | } else if (line > 0) { |
51 | 3.35k | sudo_printf(SUDO_CONV_ERROR_MSG, _("%s:%d:%d: %s\n"), file, |
52 | 3.35k | line, column, errstr); |
53 | 3.35k | } else { |
54 | 0 | sudo_printf(SUDO_CONV_ERROR_MSG, _("%s: %s\n"), file, errstr); |
55 | 0 | } |
56 | 3.35k | sudoers_setlocale(oldlocale, NULL); |
57 | | |
58 | 3.35k | free(errstr); |
59 | 3.35k | } |
60 | | |
61 | 3.35k | debug_return_bool(ret); |
62 | 3.35k | } |
63 | | |
64 | | bool |
65 | | parser_warnx(const struct sudoers_context *ctx, const char *file, int line, |
66 | | int column, bool strict, bool quiet, const char * restrict fmt, ...) |
67 | 12 | { |
68 | 12 | va_list ap; |
69 | 12 | bool ret; |
70 | 12 | debug_decl(parser_warnx, SUDOERS_DEBUG_DEFAULTS); |
71 | | |
72 | 12 | va_start(ap, fmt); |
73 | 12 | ret = parser_vwarnx(ctx, file, line, column, strict, quiet, fmt, ap); |
74 | 12 | va_end(ap); |
75 | | |
76 | | debug_return_bool(ret); |
77 | 12 | } |