1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2011-2023 Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
#ifdef __TANDEM
# include <floss.h>
#endif
#include <config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <netdb.h>
#ifndef HAVE_GETADDRINFO
# include <compat/getaddrinfo.h>
#endif
#include <sudoers.h>
#include <timestamp.h>
#include <sudo_iolog.h>
#ifndef AI_FQDN
# define AI_FQDN AI_CANONNAME
#endif
static bool override_umask;
/*
* Look up the fully qualified domain name of host.
* Use AI_FQDN if available since "canonical" is not always the same as fqdn.
* Returns 0 on success, setting longp and shortp.
* Returns non-zero on failure, longp and shortp are unchanged.
* See gai_strerror() for the list of error return codes.
*/
static int
resolve_host(const char *host, char **longp, char **shortp)
{
struct addrinfo *res0, hint;
char *cp, *lname, *sname;
int ret;
debug_decl(resolve_host, SUDOERS_DEBUG_PLUGIN);
memset(&hint, 0, sizeof(hint));
hint.ai_family = PF_UNSPEC;
hint.ai_flags = AI_FQDN;
if ((ret = getaddrinfo(host, NULL, &hint, &res0)) != 0)
debug_return_int(ret);
if ((lname = strdup(res0->ai_canonname)) == NULL) {
freeaddrinfo(res0);
debug_return_int(EAI_MEMORY);
}
if ((cp = strchr(lname, '.')) != NULL) {
sname = strndup(lname, (size_t)(cp - lname));
if (sname == NULL) {
free(lname);
freeaddrinfo(res0);
debug_return_int(EAI_MEMORY);
}
} else {
sname = lname;
}
freeaddrinfo(res0);
*longp = lname;
*shortp = sname;
debug_return_int(0);
}
/*
* Look up the fully qualified domain name of user and runas hosts.
* Sets ctx->user.host, ctx->user.shost, ctx->runas.host and ctx->runas.shost.
*/
static bool
cb_fqdn(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
bool remote;
int rc;
char *lhost, *shost;
debug_decl(cb_fqdn, SUDOERS_DEBUG_PLUGIN);
/* Nothing to do if fqdn flag is disabled. */
if (sd_un != NULL && !sd_un->flag)
debug_return_bool(true);
/* If the -h flag was given we need to resolve both host names. */
remote = strcmp(ctx->runas.host, ctx->user.host) != 0;
/* First resolve ctx->user.host, setting host and shost. */
if (resolve_host(ctx->user.host, &lhost, &shost) != 0) {
if ((rc = resolve_host(ctx->runas.host, &lhost, &shost)) != 0) {
gai_log_warning(ctx, SLOG_PARSE_ERROR|SLOG_RAW_MSG, rc,
N_("unable to resolve host %s"), ctx->user.host);
debug_return_bool(false);
}
}
if (ctx->user.shost != ctx->user.host)
free(ctx->user.shost);
free(ctx->user.host);
ctx->user.host = lhost;
ctx->user.shost = shost;
/* Next resolve ctx->runas.host, setting host and shost in ctx->runas. */
lhost = shost = NULL;
if (remote) {
if ((rc = resolve_host(ctx->runas.host, &lhost, &shost)) != 0) {
gai_log_warning(ctx, SLOG_NO_LOG|SLOG_RAW_MSG, rc,
N_("unable to resolve host %s"), ctx->runas.host);
debug_return_bool(false);
}
} else {
/* Not remote, just use ctx->user.host. */
if ((lhost = strdup(ctx->user.host)) != NULL) {
if (ctx->user.shost != ctx->user.host)
shost = strdup(ctx->user.shost);
else
shost = lhost;
}
if (lhost == NULL || shost == NULL) {
free(lhost);
if (lhost != shost)
free(shost);
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
debug_return_bool(false);
}
}
if (lhost != NULL && shost != NULL) {
if (ctx->runas.shost != ctx->runas.host)
free(ctx->runas.shost);
free(ctx->runas.host);
ctx->runas.host = lhost;
ctx->runas.shost = shost;
}
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
"host %s, shost %s, runas host %s, runas shost %s",
ctx->user.host, ctx->user.shost, ctx->runas.host, ctx->runas.shost);
debug_return_bool(true);
}
static bool
cb_tty_tickets(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_tty_tickets, SUDOERS_DEBUG_PLUGIN);
/* Convert tty_tickets -> timestamp_type */
if (sd_un->flag)
def_timestamp_type = tty;
else
def_timestamp_type = global;
debug_return_bool(true);
}
static bool
cb_umask(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_umask, SUDOERS_DEBUG_PLUGIN);
/* Override umask if explicitly set in sudoers. */
override_umask = sd_un->mode != ACCESSPERMS;
debug_return_bool(true);
}
static bool
cb_runchroot(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_runchroot, SUDOERS_DEBUG_PLUGIN);
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
"def_runchroot now %s", sd_un->str);
if (ctx->user.cmnd != NULL) {
/* Update ctx->user.cmnd and cmnd_status based on the new chroot. */
set_cmnd_status(ctx, sd_un->str);
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
"ctx->user.cmnd now %s", ctx->user.cmnd);
}
debug_return_bool(true);
}
static bool
cb_logfile(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
int logtype = def_syslog ? EVLOG_SYSLOG : EVLOG_NONE;
debug_decl(cb_logfile, SUDOERS_DEBUG_PLUGIN);
if (sd_un->str != NULL)
SET(logtype, EVLOG_FILE);
eventlog_set_type(logtype);
eventlog_set_logpath(sd_un->str);
debug_return_bool(true);
}
static bool
cb_log_format(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_log_format, SUDOERS_DEBUG_PLUGIN);
eventlog_set_format(sd_un->tuple == sudo ? EVLOG_SUDO : EVLOG_JSON);
debug_return_bool(true);
}
static bool
cb_syslog(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
int logtype = def_logfile ? EVLOG_FILE : EVLOG_NONE;
debug_decl(cb_syslog, SUDOERS_DEBUG_PLUGIN);
if (sd_un->str != NULL)
SET(logtype, EVLOG_SYSLOG);
eventlog_set_type(logtype);
debug_return_bool(true);
}
static bool
cb_syslog_goodpri(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_syslog_goodpri, SUDOERS_DEBUG_PLUGIN);
eventlog_set_syslog_acceptpri(sd_un->ival);
debug_return_bool(true);
}
static bool
cb_syslog_badpri(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_syslog_badpri, SUDOERS_DEBUG_PLUGIN);
eventlog_set_syslog_rejectpri(sd_un->ival);
eventlog_set_syslog_alertpri(sd_un->ival);
debug_return_bool(true);
}
static bool
cb_syslog_maxlen(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_syslog_maxlen, SUDOERS_DEBUG_PLUGIN);
eventlog_set_syslog_maxlen((size_t)sd_un->ival);
debug_return_bool(true);
}
static bool
cb_loglinelen(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_loglinelen, SUDOERS_DEBUG_PLUGIN);
eventlog_set_file_maxlen((size_t)sd_un->ival);
debug_return_bool(true);
}
static bool
cb_log_year(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_syslog_maxlen, SUDOERS_DEBUG_PLUGIN);
eventlog_set_time_fmt(sd_un->flag ? "%h %e %T %Y" : "%h %e %T");
debug_return_bool(true);
}
static bool
cb_log_host(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_syslog_maxlen, SUDOERS_DEBUG_PLUGIN);
eventlog_set_omit_hostname(!sd_un->flag);
debug_return_bool(true);
}
static bool
cb_mailerpath(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_mailerpath, SUDOERS_DEBUG_PLUGIN);
eventlog_set_mailerpath(sd_un->str);
debug_return_bool(true);
}
static bool
cb_mailerflags(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_mailerflags, SUDOERS_DEBUG_PLUGIN);
eventlog_set_mailerflags(sd_un->str);
debug_return_bool(true);
}
static bool
cb_mailfrom(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_mailfrom, SUDOERS_DEBUG_PLUGIN);
eventlog_set_mailfrom(sd_un->str);
debug_return_bool(true);
}
static bool
cb_mailto(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_mailto, SUDOERS_DEBUG_PLUGIN);
eventlog_set_mailto(sd_un->str);
debug_return_bool(true);
}
static bool
cb_mailsub(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_mailsub, SUDOERS_DEBUG_PLUGIN);
eventlog_set_mailsub(sd_un->str);
debug_return_bool(true);
}
static bool
cb_intercept_type(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_intercept_type, SUDOERS_DEBUG_PLUGIN);
if (op != -1) {
/* Set explicitly in sudoers. */
if (sd_un->tuple == dso) {
/* Reset intercept_allow_setid default value. */
if (!ISSET(ctx->settings.flags, USER_INTERCEPT_SETID))
def_intercept_allow_setid = false;
}
}
debug_return_bool(true);
}
static bool
cb_intercept_allow_setid(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_intercept_allow_setid, SUDOERS_DEBUG_PLUGIN);
/* Operator will be -1 if set by front-end. */
if (op != -1) {
/* Set explicitly in sudoers. */
SET(ctx->settings.flags, USER_INTERCEPT_SETID);
}
debug_return_bool(true);
}
bool
cb_log_input(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_log_input, SUDOERS_DEBUG_PLUGIN);
def_log_stdin = op;
def_log_ttyin = op;
debug_return_bool(true);
}
bool
cb_log_output(struct sudoers_context *ctx, const char *file,
int line, int column, const union sudo_defs_val *sd_un, int op)
{
debug_decl(cb_log_output, SUDOERS_DEBUG_PLUGIN);
def_log_stdout = op;
def_log_stderr = op;
def_log_ttyout = op;
debug_return_bool(true);
}
/*
* Set parser Defaults callbacks.
* We do this here instead in def_data.in so we don't have to
* stub out the callbacks for visudo and testsudoers.
*/
void
set_callbacks(void)
{
debug_decl(set_callbacks, SUDOERS_DEBUG_PLUGIN);
/* Set fqdn callback. */
sudo_defs_table[I_FQDN].callback = cb_fqdn;
/* Set group_plugin callback. */
sudo_defs_table[I_GROUP_PLUGIN].callback = cb_group_plugin;
/* Set runas callback. */
sudo_defs_table[I_RUNAS_DEFAULT].callback = cb_runas_default;
/* Set locale callback. */
sudo_defs_table[I_SUDOERS_LOCALE].callback = sudoers_locale_callback;
#ifdef SESSID_MAX
/* Set maxseq callback. */
sudo_defs_table[I_MAXSEQ].callback = cb_maxseq;
/* Set iolog_user callback. */
sudo_defs_table[I_IOLOG_USER].callback = cb_iolog_user;
/* Set iolog_group callback. */
sudo_defs_table[I_IOLOG_GROUP].callback = cb_iolog_group;
/* Set iolog_mode callback. */
sudo_defs_table[I_IOLOG_MODE].callback = cb_iolog_mode;
#endif /* SESSID_MAX */
/* Set timestampowner callback. */
sudo_defs_table[I_TIMESTAMPOWNER].callback = cb_timestampowner;
/* Set tty_tickets callback. */
sudo_defs_table[I_TTY_TICKETS].callback = cb_tty_tickets;
/* Set umask callback. */
sudo_defs_table[I_UMASK].callback = cb_umask;
/* Set runchroot callback. */
sudo_defs_table[I_RUNCHROOT].callback = cb_runchroot;
/* eventlog callbacks */
sudo_defs_table[I_SYSLOG].callback = cb_syslog;
sudo_defs_table[I_SYSLOG_GOODPRI].callback = cb_syslog_goodpri;
sudo_defs_table[I_SYSLOG_BADPRI].callback = cb_syslog_badpri;
sudo_defs_table[I_SYSLOG_MAXLEN].callback = cb_syslog_maxlen;
sudo_defs_table[I_LOGLINELEN].callback = cb_loglinelen;
sudo_defs_table[I_LOG_HOST].callback = cb_log_host;
sudo_defs_table[I_LOGFILE].callback = cb_logfile;
sudo_defs_table[I_LOG_FORMAT].callback = cb_log_format;
sudo_defs_table[I_LOG_YEAR].callback = cb_log_year;
sudo_defs_table[I_MAILERPATH].callback = cb_mailerpath;
sudo_defs_table[I_MAILERFLAGS].callback = cb_mailerflags;
sudo_defs_table[I_MAILFROM].callback = cb_mailfrom;
sudo_defs_table[I_MAILTO].callback = cb_mailto;
sudo_defs_table[I_MAILSUB].callback = cb_mailsub;
sudo_defs_table[I_PASSPROMPT_REGEX].callback = cb_passprompt_regex;
sudo_defs_table[I_INTERCEPT_TYPE].callback = cb_intercept_type;
sudo_defs_table[I_INTERCEPT_ALLOW_SETID].callback = cb_intercept_allow_setid;
sudo_defs_table[I_LOG_INPUT].callback = cb_log_input;
sudo_defs_table[I_LOG_OUTPUT].callback = cb_log_output;
debug_return;
}
bool
sudoers_override_umask(void)
{
return override_umask;
}
|