Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/util/fatal.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2004-2005, 2010-2015, 2017-2018
5
* Todd C. Miller <[email protected]>
6
*
7
* Permission to use, copy, modify, and distribute this software for any
8
* purpose with or without fee is hereby granted, provided that the above
9
* copyright notice and this permission notice appear in all copies.
10
*
11
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
*/
19
20
#include <config.h>
21
22
#include <errno.h>
23
#include <netdb.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#ifdef HAVE_STDBOOL_H
28
# include <stdbool.h>
29
#else
30
# include <compat/stdbool.h>
31
#endif /* HAVE_STDBOOL_H */
32
#include <unistd.h>
33
#ifndef HAVE_GETADDRINFO
34
# include <compat/getaddrinfo.h>
35
#endif
36
37
#include <sudo_compat.h>
38
#include <sudo_fatal.h>
39
#include <sudo_gettext.h>
40
#include <sudo_queue.h>
41
#include <sudo_util.h>
42
#include <sudo_plugin.h>
43
44
struct sudo_fatal_callback {
45
SLIST_ENTRY(sudo_fatal_callback) entries;
46
void (*func)(void);
47
};
48
SLIST_HEAD(sudo_fatal_callback_list, sudo_fatal_callback);
49
50
static struct sudo_fatal_callback_list callbacks = SLIST_HEAD_INITIALIZER(&callbacks);
51
static sudo_conv_t sudo_warn_conversation;
52
static sudo_warn_setlocale_t sudo_warn_setlocale;
53
static sudo_warn_setlocale_t sudo_warn_setlocale_prev;
54
55
static void warning(const char * restrict errstr, const char * restrict fmt, va_list ap);
56
57
static void
58
do_cleanup(void)
59
{
60
struct sudo_fatal_callback *cb;
61
62
/* Run callbacks, removing them from the list as we go. */
63
while ((cb = SLIST_FIRST(&callbacks)) != NULL) {
64
SLIST_REMOVE_HEAD(&callbacks, entries);
65
cb->func();
66
free(cb);
67
}
68
}
69
70
sudo_noreturn void
71
sudo_fatal_nodebug_v1(const char * restrict fmt, ...)
72
{
73
va_list ap;
74
75
va_start(ap, fmt);
76
warning(strerror(errno), fmt, ap);
77
va_end(ap);
78
do_cleanup();
79
exit(EXIT_FAILURE);
80
}
81
82
sudo_noreturn void
83
sudo_fatalx_nodebug_v1(const char * restrict fmt, ...)
84
{
85
va_list ap;
86
87
va_start(ap, fmt);
88
warning(NULL, fmt, ap);
89
va_end(ap);
90
do_cleanup();
91
exit(EXIT_FAILURE);
92
}
93
94
sudo_noreturn void
95
sudo_vfatal_nodebug_v1(const char * restrict fmt, va_list ap)
96
{
97
warning(strerror(errno), fmt, ap);
98
do_cleanup();
99
exit(EXIT_FAILURE);
100
}
101
102
sudo_noreturn void
103
sudo_vfatalx_nodebug_v1(const char * restrict fmt, va_list ap)
104
{
105
warning(NULL, fmt, ap);
106
do_cleanup();
107
exit(EXIT_FAILURE);
108
}
109
110
void
111
sudo_warn_nodebug_v1(const char * restrict fmt, ...)
112
{
113
va_list ap;
114
115
va_start(ap, fmt);
116
warning(strerror(errno), fmt, ap);
117
va_end(ap);
118
}
119
120
void
121
sudo_warnx_nodebug_v1(const char * restrict fmt, ...)
122
{
123
va_list ap;
124
va_start(ap, fmt);
125
warning(NULL, fmt, ap);
126
va_end(ap);
127
}
128
129
void
130
sudo_vwarn_nodebug_v1(const char * restrict fmt, va_list ap)
131
{
132
warning(strerror(errno), fmt, ap);
133
}
134
135
void
136
sudo_vwarnx_nodebug_v1(const char * restrict fmt, va_list ap)
137
{
138
warning(NULL, fmt, ap);
139
}
140
141
sudo_noreturn void
142
sudo_gai_fatal_nodebug_v1(int errnum, const char * restrict fmt, ...)
143
{
144
va_list ap;
145
146
va_start(ap, fmt);
147
warning(gai_strerror(errnum), fmt, ap);
148
va_end(ap);
149
do_cleanup();
150
exit(EXIT_FAILURE);
151
}
152
153
sudo_noreturn void
154
sudo_gai_vfatal_nodebug_v1(int errnum, const char * restrict fmt, va_list ap)
155
{
156
warning(gai_strerror(errnum), fmt, ap);
157
do_cleanup();
158
exit(EXIT_FAILURE);
159
}
160
161
void
162
sudo_gai_warn_nodebug_v1(int errnum, const char * restrict fmt, ...)
163
{
164
va_list ap;
165
166
va_start(ap, fmt);
167
warning(gai_strerror(errnum), fmt, ap);
168
va_end(ap);
169
}
170
171
void
172
sudo_gai_vwarn_nodebug_v1(int errnum, const char * restrict fmt, va_list ap)
173
{
174
warning(gai_strerror(errnum), fmt, ap);
175
}
176
177
static void
178
warning(const char * restrict errstr, const char * restrict fmt, va_list ap)
179
{
180
int cookie;
181
const int saved_errno = errno;
182
183
/* Set user locale if setter was specified. */
184
if (sudo_warn_setlocale != NULL)
185
sudo_warn_setlocale(false, &cookie);
186
187
if (sudo_warn_conversation != NULL) {
188
struct sudo_conv_message msgs[6];
189
char static_buf[1024], *buf = static_buf;
190
int nmsgs = 0;
191
192
/* Use conversation function. */
193
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
194
msgs[nmsgs++].msg = getprogname();
195
if (fmt != NULL) {
196
va_list ap2;
197
int buflen;
198
199
/* Use static buffer if possible, else dynamic. */
200
va_copy(ap2, ap);
201
buflen = vsnprintf(static_buf, sizeof(static_buf), fmt, ap2);
202
va_end(ap2);
203
if (buflen >= ssizeof(static_buf)) {
204
/* Not enough room in static buf, allocate dynamically. */
205
if (vasprintf(&buf, fmt, ap) == -1)
206
buf = static_buf;
207
}
208
if (buflen > 0) {
209
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
210
msgs[nmsgs++].msg = ": ";
211
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
212
msgs[nmsgs++].msg = buf;
213
}
214
}
215
if (errstr != NULL) {
216
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
217
msgs[nmsgs++].msg = ": ";
218
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
219
msgs[nmsgs++].msg = errstr;
220
}
221
msgs[nmsgs].msg_type = SUDO_CONV_ERROR_MSG;
222
msgs[nmsgs++].msg = "\n";
223
sudo_warn_conversation(nmsgs, msgs, NULL, NULL);
224
if (buf != static_buf)
225
free(buf);
226
} else {
227
/* Write to the standard error. */
228
fputs(getprogname(), stderr);
229
if (fmt != NULL) {
230
fputs(": ", stderr);
231
vfprintf(stderr, fmt, ap);
232
}
233
if (errstr != NULL) {
234
fputs(": ", stderr);
235
fputs(errstr, stderr);
236
}
237
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
238
if (sudo_term_is_raw(fileno(stderr)))
239
putc('\r', stderr);
240
#endif
241
putc('\n', stderr);
242
}
243
244
/* Restore old locale as needed. */
245
if (sudo_warn_setlocale != NULL)
246
sudo_warn_setlocale(true, &cookie);
247
248
/* Do not clobber errno. */
249
errno = saved_errno;
250
}
251
252
/*
253
* Register a callback to be run when sudo_fatal()/sudo_fatalx() is called.
254
*/
255
int
256
sudo_fatal_callback_register_v1(sudo_fatal_callback_t func)
257
{
258
struct sudo_fatal_callback *cb;
259
260
/* Do not register the same callback twice. */
261
SLIST_FOREACH(cb, &callbacks, entries) {
262
if (func == cb->func)
263
return -1; /* dupe! */
264
}
265
266
/* Allocate and insert new callback. */
267
cb = malloc(sizeof(*cb));
268
if (cb == NULL)
269
return -1;
270
cb->func = func;
271
SLIST_INSERT_HEAD(&callbacks, cb, entries);
272
273
return 0;
274
}
275
276
/*
277
* Deregister a sudo_fatal()/sudo_fatalx() callback.
278
*/
279
int
280
sudo_fatal_callback_deregister_v1(sudo_fatal_callback_t func)
281
{
282
struct sudo_fatal_callback *cb, *prev = NULL;
283
284
/* Search for callback and remove if found, dupes are not allowed. */
285
SLIST_FOREACH(cb, &callbacks, entries) {
286
if (cb->func == func) {
287
if (prev == NULL)
288
SLIST_REMOVE_HEAD(&callbacks, entries);
289
else
290
SLIST_REMOVE_AFTER(prev, entries);
291
free(cb);
292
return 0;
293
}
294
prev = cb;
295
}
296
297
return -1;
298
}
299
300
/*
301
* Set the conversation function to use for output instead of the
302
* standard error. If conv is NULL, switch back to standard error.
303
*/
304
void
305
sudo_warn_set_conversation_v1(sudo_conv_t conv)
306
{
307
sudo_warn_conversation = conv;
308
}
309
310
/*
311
* Set the locale function so the plugin can use a non-default
312
* locale for user warnings.
313
*/
314
void
315
sudo_warn_set_locale_func_v1(sudo_warn_setlocale_t func)
316
{
317
sudo_warn_setlocale_prev = sudo_warn_setlocale;
318
sudo_warn_setlocale = func;
319
}
320
321
#ifdef HAVE_LIBINTL_H
322
char *
323
sudo_warn_gettext_v1(const char *domainname, const char *msgid)
324
{
325
int cookie;
326
char *msg;
327
328
/* Set user locale if setter was specified. */
329
if (sudo_warn_setlocale != NULL)
330
sudo_warn_setlocale(false, &cookie);
331
332
msg = dgettext(domainname, msgid);
333
334
/* Restore old locale as needed. */
335
if (sudo_warn_setlocale != NULL)
336
sudo_warn_setlocale(true, &cookie);
337
338
return msg;
339
}
340
#endif /* HAVE_LIBINTL_H */
341
342