Coverage Report

Created: 2026-12-01 07:06

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