Coverage Report

Created: 2025-12-14 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/policy.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2010-2024 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 <sys/types.h>
22
#include <sys/stat.h>
23
#include <netinet/in.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <unistd.h>
28
#include <errno.h>
29
#include <fcntl.h>
30
#include <grp.h>
31
#include <pwd.h>
32
33
#include <sudoers.h>
34
#include <sudoers_version.h>
35
#include <timestamp.h>
36
#include <interfaces.h>
37
#include "auth/sudo_auth.h"
38
39
static char **command_info;
40
41
/*
42
 * Command execution args to be filled in: argv, envp and command info.
43
 */
44
struct sudoers_exec_args {
45
    char ***argv;
46
    char ***envp;
47
    char ***info;
48
};
49
50
static unsigned int sudo_version;
51
static const char *interfaces_string;
52
sudo_conv_t sudo_conv;
53
sudo_printf_t sudo_printf;
54
struct sudo_plugin_event * (*plugin_event_alloc)(void);
55
static const char *path_sudoers = _PATH_SUDOERS;
56
static bool session_opened;
57
58
extern sudo_dso_public struct policy_plugin sudoers_policy;
59
60
static int
61
parse_bool(const char *line, int varlen, unsigned int *flags, unsigned int fval)
62
14.3k
{
63
14.3k
    debug_decl(parse_bool, SUDOERS_DEBUG_PLUGIN);
64
65
14.3k
    switch (sudo_strtobool(line + varlen + 1)) {
66
7.53k
    case true:
67
7.53k
  SET(*flags, fval);
68
7.53k
  debug_return_int(true);
69
6.60k
    case false:
70
6.60k
  CLR(*flags, fval);
71
6.60k
  debug_return_int(false);
72
208
    default:
73
208
  sudo_warnx(U_("invalid %.*s set by sudo front-end"),
74
208
      varlen, line);
75
208
  debug_return_int(-1);
76
14.3k
    }
77
14.3k
}
78
79
12.2k
#define RUN_VALID_FLAGS (MODE_ASKPASS|MODE_PRESERVE_ENV|MODE_RESET_HOME|MODE_IMPLIED_SHELL|MODE_LOGIN_SHELL|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_PRESERVE_GROUPS|MODE_SHELL|MODE_RUN|MODE_POLICY_INTERCEPTED)
80
793
#define EDIT_VALID_FLAGS  (MODE_ASKPASS|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_EDIT)
81
10.4k
#define LIST_VALID_FLAGS  (MODE_ASKPASS|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_LIST|MODE_CHECK)
82
3.49k
#define VALIDATE_VALID_FLAGS  (MODE_ASKPASS|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_VALIDATE)
83
3.49k
#define INVALIDATE_VALID_FLAGS  (MODE_ASKPASS|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_INVALIDATE)
84
85
/*
86
 * Deserialize args, settings and user_info arrays.
87
 * Fills in struct sudoers_user_context and other common sudoers state.
88
 */
89
unsigned int
90
sudoers_policy_deserialize_info(struct sudoers_context *ctx, void *v,
91
    struct defaults_list *defaults)
92
22.6k
{
93
22.6k
    const char *p, *errstr, *groups = NULL;
94
22.6k
    struct sudoers_open_info *info = v;
95
22.6k
    unsigned int flags = MODE_UPDATE_TICKET;
96
22.6k
    const char *host = NULL;
97
22.6k
    const char *remhost = NULL;
98
22.6k
    unsigned char uuid[16];
99
22.6k
    char * const *cur;
100
22.6k
    debug_decl(sudoers_policy_deserialize_info, SUDOERS_DEBUG_PLUGIN);
101
102
22.6k
#define MATCHES(s, v) \
103
33.5M
    (strncmp((s), (v), sizeof(v) - 1) == 0)
104
105
22.6k
#define INVALID(v) do { \
106
4.61k
    sudo_warnx(U_("invalid %.*s set by sudo front-end"), \
107
4.61k
  (int)(sizeof(v) - 2), (v)); \
108
4.61k
} while (0)
109
110
95.4k
#define CHECK(s, v) do { \
111
95.4k
    if ((s)[sizeof(v) - 1] == '\0') { \
112
199
  INVALID(v); \
113
199
  goto bad; \
114
199
    } \
115
95.4k
} while (0)
116
117
    /* Parse sudo.conf plugin args. */
118
22.6k
    if (info->plugin_args != NULL) {
119
25.6k
  for (cur = info->plugin_args; *cur != NULL; cur++) {
120
24.7k
      if (MATCHES(*cur, "error_recovery=")) {
121
4.62k
    int val = sudo_strtobool(*cur + sizeof("error_recovery=") - 1);
122
4.62k
    if (val == -1) {
123
4.41k
        INVALID("error_recovery=");  /* Not a fatal error. */
124
4.41k
    } else {
125
211
        ctx->parser_conf.recovery = val;
126
211
    }
127
4.62k
    continue;
128
4.62k
      }
129
20.1k
      if (MATCHES(*cur, "ignore_perms=")) {
130
0
    int val = sudo_strtobool(*cur + sizeof("ignore_perms=") - 1);
131
0
    if (val == -1) {
132
0
        INVALID("ignore_perms=");  /* Not a fatal error. */
133
0
    } else {
134
0
        ctx->parser_conf.ignore_perms = val;
135
0
    }
136
0
    continue;
137
0
      }
138
20.1k
      if (MATCHES(*cur, "sudoers_file=")) {
139
18.6k
    CHECK(*cur, "sudoers_file=");
140
18.5k
    path_sudoers = *cur + sizeof("sudoers_file=") - 1;
141
18.5k
    continue;
142
18.6k
      }
143
1.52k
      if (MATCHES(*cur, "sudoers_uid=")) {
144
334
    p = *cur + sizeof("sudoers_uid=") - 1;
145
334
    ctx->parser_conf.sudoers_uid = (uid_t)sudo_strtoid(p, &errstr);
146
334
    if (errstr != NULL) {
147
24
        sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
148
24
        goto bad;
149
24
    }
150
310
    continue;
151
334
      }
152
1.18k
      if (MATCHES(*cur, "sudoers_gid=")) {
153
377
    p = *cur + sizeof("sudoers_gid=") - 1;
154
377
    ctx->parser_conf.sudoers_gid = (gid_t)sudo_strtoid(p, &errstr);
155
377
    if (errstr != NULL) {
156
27
        sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
157
27
        goto bad;
158
27
    }
159
350
    continue;
160
377
      }
161
809
      if (MATCHES(*cur, "sudoers_mode=")) {
162
234
    p = *cur + sizeof("sudoers_mode=") - 1;
163
234
    ctx->parser_conf.sudoers_mode = sudo_strtomode(p, &errstr);
164
234
    if (errstr != NULL) {
165
24
        sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
166
24
        goto bad;
167
24
    }
168
210
    continue;
169
234
      }
170
575
      if (MATCHES(*cur, "ldap_conf=")) {
171
332
    CHECK(*cur, "ldap_conf=");
172
247
    ctx->settings.ldap_conf = *cur + sizeof("ldap_conf=") - 1;
173
247
    continue;
174
332
      }
175
243
      if (MATCHES(*cur, "ldap_secret=")) {
176
243
    CHECK(*cur, "ldap_secret=");
177
226
    ctx->settings.ldap_secret = *cur + sizeof("ldap_secret=") - 1;
178
226
    continue;
179
243
      }
180
243
  }
181
1.04k
    }
182
22.4k
    ctx->parser_conf.sudoers_path = path_sudoers;
183
184
    /* Parse command line settings. */
185
22.4k
    ctx->settings.flags = 0;
186
22.4k
    ctx->user.closefrom = -1;
187
22.4k
    ctx->sudoedit_nfiles = 0;
188
22.4k
    ctx->mode = 0;
189
1.31M
    for (cur = info->settings; *cur != NULL; cur++) {
190
1.29M
  if (MATCHES(*cur, "closefrom=")) {
191
758
      p = *cur + sizeof("closefrom=") - 1;
192
758
      ctx->user.closefrom = (int)sudo_strtonum(p, 3, INT_MAX, &errstr);
193
758
      if (ctx->user.closefrom == 0) {
194
2
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
195
2
    goto bad;
196
2
      }
197
756
      continue;
198
758
  }
199
1.29M
  if (MATCHES(*cur, "cmnd_chroot=")) {
200
253
      CHECK(*cur, "cmnd_chroot=");
201
252
      ctx->runas.chroot = *cur + sizeof("cmnd_chroot=") - 1;
202
252
      if (strlen(ctx->runas.chroot) >= PATH_MAX) {
203
2
    sudo_warnx(U_("path name for \"%s\" too long"), "cmnd_chroot");
204
2
    goto bad;
205
2
      }
206
250
      continue;
207
252
  }
208
1.29M
  if (MATCHES(*cur, "cmnd_cwd=")) {
209
324
      CHECK(*cur, "cmnd_cwd=");
210
323
      ctx->runas.cwd = *cur + sizeof("cmnd_cwd=") - 1;
211
323
      if (strlen(ctx->runas.cwd) >= PATH_MAX) {
212
4
    sudo_warnx(U_("path name for \"%s\" too long"), "cmnd_cwd");
213
4
    goto bad;
214
4
      }
215
319
      continue;
216
323
  }
217
1.29M
  if (MATCHES(*cur, "runas_user=")) {
218
440
      CHECK(*cur, "runas_user=");
219
439
      ctx->runas.user = *cur + sizeof("runas_user=") - 1;
220
439
      SET(ctx->settings.flags, RUNAS_USER_SPECIFIED);
221
439
      continue;
222
440
  }
223
1.28M
  if (MATCHES(*cur, "runas_group=")) {
224
761
      CHECK(*cur, "runas_group=");
225
760
      ctx->runas.group = *cur + sizeof("runas_group=") - 1;
226
760
      SET(ctx->settings.flags, RUNAS_GROUP_SPECIFIED);
227
760
      continue;
228
761
  }
229
1.28M
  if (MATCHES(*cur, "prompt=")) {
230
      /* Allow empty prompt. */
231
27.2k
      ctx->user.prompt = *cur + sizeof("prompt=") - 1;
232
27.2k
      if (!append_default("passprompt_override", NULL, true, NULL, defaults))
233
0
    goto oom;
234
27.2k
      continue;
235
27.2k
  }
236
1.26M
  if (MATCHES(*cur, "set_home=")) {
237
655
      if (parse_bool(*cur, sizeof("set_home") - 1, &flags,
238
655
    MODE_RESET_HOME) == -1)
239
11
    goto bad;
240
644
      continue;
241
655
  }
242
1.26M
  if (MATCHES(*cur, "preserve_environment=")) {
243
508
      if (parse_bool(*cur, sizeof("preserve_environment") - 1, &flags,
244
508
    MODE_PRESERVE_ENV) == -1)
245
2
    goto bad;
246
506
      continue;
247
508
  }
248
1.26M
  if (MATCHES(*cur, "run_shell=")) {
249
2.36k
      if (parse_bool(*cur, sizeof("run_shell") -1, &flags,
250
2.36k
    MODE_SHELL) == -1)
251
11
    goto bad;
252
2.35k
      continue;
253
2.36k
  }
254
1.25M
  if (MATCHES(*cur, "login_shell=")) {
255
839
      if (parse_bool(*cur, sizeof("login_shell") - 1, &flags,
256
839
    MODE_LOGIN_SHELL) == -1)
257
6
    goto bad;
258
833
      continue;
259
839
  }
260
1.25M
  if (MATCHES(*cur, "implied_shell=")) {
261
471
      if (parse_bool(*cur, sizeof("implied_shell") - 1, &flags,
262
471
    MODE_IMPLIED_SHELL) == -1)
263
4
    goto bad;
264
467
      continue;
265
471
  }
266
1.25M
  if (MATCHES(*cur, "preserve_groups=")) {
267
563
      if (parse_bool(*cur, sizeof("preserve_groups") - 1, &flags,
268
563
    MODE_PRESERVE_GROUPS) == -1)
269
2
    goto bad;
270
561
      continue;
271
563
  }
272
1.25M
  if (MATCHES(*cur, "ignore_ticket=")) {
273
217
      if (parse_bool(*cur, sizeof("ignore_ticket") -1, &flags,
274
217
    MODE_IGNORE_TICKET) == -1)
275
6
    goto bad;
276
211
      continue;
277
217
  }
278
1.25M
  if (MATCHES(*cur, "update_ticket=")) {
279
298
      if (parse_bool(*cur, sizeof("update_ticket") -1, &flags,
280
298
    MODE_UPDATE_TICKET) == -1)
281
8
    goto bad;
282
290
      continue;
283
298
  }
284
1.25M
  if (MATCHES(*cur, "noninteractive=")) {
285
242
      if (parse_bool(*cur, sizeof("noninteractive") - 1, &flags,
286
242
    MODE_NONINTERACTIVE) == -1)
287
10
    goto bad;
288
232
      continue;
289
242
  }
290
1.25M
  if (MATCHES(*cur, "sudoedit=")) {
291
5.87k
      if (parse_bool(*cur, sizeof("sudoedit") - 1, &flags,
292
5.87k
    MODE_EDIT) == -1)
293
8
    goto bad;
294
5.86k
      continue;
295
5.87k
  }
296
1.24M
  if (MATCHES(*cur, "login_class=")) {
297
968
      CHECK(*cur, "login_class=");
298
967
      ctx->runas.class = *cur + sizeof("login_class=") - 1;
299
967
      if (!append_default("use_loginclass", NULL, true, NULL, defaults))
300
0
    goto oom;
301
967
      continue;
302
967
  }
303
1.24M
  if (MATCHES(*cur, "intercept_ptrace=")) {
304
243
      if (parse_bool(*cur, sizeof("intercept_ptrace") - 1, &ctx->settings.flags,
305
243
        HAVE_INTERCEPT_PTRACE) == -1)
306
2
    goto bad;
307
241
      continue;
308
243
  }
309
1.24M
  if (MATCHES(*cur, "intercept_setid=")) {
310
237
      if (parse_bool(*cur, sizeof("intercept_setid") - 1, &ctx->settings.flags,
311
237
        CAN_INTERCEPT_SETID) == -1)
312
2
    goto bad;
313
235
      continue;
314
237
  }
315
1.24M
  if (MATCHES(*cur, "selinux_role=")) {
316
262
      CHECK(*cur, "selinux_role=");
317
261
      free(ctx->runas.role);
318
261
      ctx->runas.role = strdup(*cur + sizeof("selinux_role=") - 1);
319
261
      if (ctx->runas.role == NULL)
320
0
    goto oom;
321
261
      continue;
322
261
  }
323
1.24M
  if (MATCHES(*cur, "selinux_type=")) {
324
348
      CHECK(*cur, "selinux_type=");
325
347
      free(ctx->runas.type);
326
347
      ctx->runas.type = strdup(*cur + sizeof("selinux_type=") - 1);
327
347
      if (ctx->runas.type == NULL)
328
0
    goto oom;
329
347
      continue;
330
347
  }
331
#ifdef HAVE_BSD_AUTH_H
332
  if (MATCHES(*cur, "bsdauth_type=")) {
333
      CHECK(*cur, "bsdauth_type=");
334
      p = *cur + sizeof("bsdauth_type=") - 1;
335
      bsdauth_set_style(p);
336
      continue;
337
  }
338
#endif /* HAVE_BSD_AUTH_H */
339
1.24M
  if (MATCHES(*cur, "network_addrs=")) {
340
302
      interfaces_string = *cur + sizeof("network_addrs=") - 1;
341
302
      if (!set_interfaces(interfaces_string)) {
342
0
    sudo_warn("%s", U_("unable to parse network address list"));
343
0
    goto bad;
344
0
      }
345
302
      continue;
346
302
  }
347
1.24M
  if (MATCHES(*cur, "max_groups=")) {
348
261
      int max_groups;
349
261
      p = *cur + sizeof("max_groups=") - 1;
350
261
      max_groups = (int)sudo_strtonum(p, 1, 1024, &errstr);
351
261
      if (max_groups == 0) {
352
10
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
353
10
    goto bad;
354
10
      }
355
251
      sudo_pwutil_set_max_groups(max_groups);
356
251
      continue;
357
261
  }
358
1.24M
  if (MATCHES(*cur, "remote_host=")) {
359
1.78k
      CHECK(*cur, "remote_host=");
360
1.78k
      remhost = *cur + sizeof("remote_host=") - 1;
361
1.78k
      continue;
362
1.78k
  }
363
1.24M
  if (MATCHES(*cur, "timeout=")) {
364
1.38k
      p = *cur + sizeof("timeout=") - 1;
365
1.38k
      ctx->user.timeout = parse_timeout(p);
366
1.38k
      if (ctx->user.timeout == -1) {
367
190
    if (errno == ERANGE)
368
162
        sudo_warnx(U_("%s: %s"), p, U_("timeout value too large"));
369
28
    else
370
28
        sudo_warnx(U_("%s: %s"), p, U_("invalid timeout value"));
371
190
    goto bad;
372
190
      }
373
1.19k
      continue;
374
1.38k
  }
375
1.24M
  if (MATCHES(*cur, "askpass=")) {
376
1.83k
      if (parse_bool(*cur, sizeof("askpass") - 1, &flags,
377
1.83k
    MODE_ASKPASS) == -1)
378
136
    goto bad;
379
1.69k
      continue;
380
1.83k
  }
381
#ifdef ENABLE_SUDO_PLUGIN_API
382
  if (MATCHES(*cur, "plugin_dir=")) {
383
      CHECK(*cur, "plugin_dir=");
384
      ctx->settings.plugin_dir = *cur + sizeof("plugin_dir=") - 1;
385
      continue;
386
  }
387
#endif
388
1.24M
    }
389
    /* Ignore ticket trumps update. */
390
21.9k
    if (ISSET(flags, MODE_IGNORE_TICKET))
391
22
  CLR(flags, MODE_UPDATE_TICKET);
392
393
21.9k
    ctx->user.gid = (gid_t)-1;
394
21.9k
    ctx->user.uid = (gid_t)-1;
395
21.9k
    ctx->user.umask = (mode_t)-1;
396
21.9k
    ctx->user.ttydev = NODEV;
397
158k
    for (cur = info->user_info; *cur != NULL; cur++) {
398
137k
  if (MATCHES(*cur, "user=")) {
399
28.9k
      CHECK(*cur, "user=");
400
28.9k
      free(ctx->user.name);
401
28.9k
      if ((ctx->user.name = strdup(*cur + sizeof("user=") - 1)) == NULL)
402
0
    goto oom;
403
28.9k
      continue;
404
28.9k
  }
405
108k
  if (MATCHES(*cur, "euid=")) {
406
0
      p = *cur + sizeof("euid=") - 1;
407
0
      ctx->user.euid = (uid_t) sudo_strtoid(p, &errstr);
408
0
      if (errstr != NULL) {
409
0
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
410
0
    goto bad;
411
0
      }
412
0
      continue;
413
0
  }
414
108k
  if (MATCHES(*cur, "uid=")) {
415
32.1k
      p = *cur + sizeof("uid=") - 1;
416
32.1k
      ctx->user.uid = (uid_t) sudo_strtoid(p, &errstr);
417
32.1k
      if (errstr != NULL) {
418
59
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
419
59
    goto bad;
420
59
      }
421
32.0k
      continue;
422
32.1k
  }
423
76.1k
  if (MATCHES(*cur, "egid=")) {
424
0
      p = *cur + sizeof("egid=") - 1;
425
0
      ctx->user.egid = (gid_t) sudo_strtoid(p, &errstr);
426
0
      if (errstr != NULL) {
427
0
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
428
0
    goto bad;
429
0
      }
430
0
      continue;
431
0
  }
432
76.1k
  if (MATCHES(*cur, "gid=")) {
433
31.4k
      p = *cur + sizeof("gid=") - 1;
434
31.4k
      ctx->user.gid = (gid_t) sudo_strtoid(p, &errstr);
435
31.4k
      if (errstr != NULL) {
436
100
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
437
100
    goto bad;
438
100
      }
439
31.3k
      continue;
440
31.4k
  }
441
44.7k
  if (MATCHES(*cur, "groups=")) {
442
1.14k
      CHECK(*cur, "groups=");
443
1.13k
      groups = *cur + sizeof("groups=") - 1;
444
1.13k
      continue;
445
1.14k
  }
446
43.5k
  if (MATCHES(*cur, "cwd=")) {
447
948
      CHECK(*cur, "cwd=");
448
938
      free(ctx->user.cwd);
449
938
      if ((ctx->user.cwd = strdup(*cur + sizeof("cwd=") - 1)) == NULL)
450
0
    goto oom;
451
938
      continue;
452
938
  }
453
42.6k
  if (MATCHES(*cur, "tty=")) {
454
1.36k
      CHECK(*cur, "tty=");
455
1.35k
      free(ctx->user.ttypath);
456
1.35k
      if ((ctx->user.ttypath = strdup(*cur + sizeof("tty=") - 1)) == NULL)
457
0
    goto oom;
458
1.35k
      ctx->user.tty = ctx->user.ttypath;
459
1.35k
      if (strncmp(ctx->user.tty, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
460
311
    ctx->user.tty += sizeof(_PATH_DEV) - 1;
461
1.35k
      continue;
462
1.35k
  }
463
41.2k
  if (MATCHES(*cur, "ttydev=")) {
464
0
      long long llval;
465
466
      /*
467
       * dev_t can be signed or unsigned.  The front-end formats it
468
       * as long long (signed).  We allow the full range of values
469
       * which should work with either signed or unsigned dev_t.
470
       */
471
0
      p = *cur + sizeof("ttydev=") - 1;
472
0
      llval = sudo_strtonum(p, LLONG_MIN, LLONG_MAX, &errstr);
473
0
      if (errstr != NULL) {
474
    /* Front end bug?  Not a fatal error. */
475
0
    INVALID("ttydev=");
476
0
    continue;
477
0
      }
478
0
      ctx->user.ttydev = (dev_t)llval;
479
0
      continue;
480
0
  }
481
41.2k
  if (MATCHES(*cur, "host=")) {
482
38.7k
      CHECK(*cur, "host=");
483
38.7k
      host = *cur + sizeof("host=") - 1;
484
38.7k
      continue;
485
38.7k
  }
486
2.52k
  if (MATCHES(*cur, "lines=")) {
487
496
      p = *cur + sizeof("lines=") - 1;
488
496
      ctx->user.lines = (int)sudo_strtonum(p, 1, INT_MAX, &errstr);
489
496
      if (ctx->user.lines == 0) {
490
15
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
491
15
    goto bad;
492
15
      }
493
481
      continue;
494
496
  }
495
2.03k
  if (MATCHES(*cur, "cols=")) {
496
426
      p = *cur + sizeof("cols=") - 1;
497
426
      ctx->user.cols = (int)sudo_strtonum(p, 1, INT_MAX, &errstr);
498
426
      if (ctx->user.cols == 0) {
499
30
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
500
30
    goto bad;
501
30
      }
502
396
      continue;
503
426
  }
504
1.60k
  if (MATCHES(*cur, "pid=")) {
505
0
      p = *cur + sizeof("pid=") - 1;
506
0
      ctx->user.pid = (pid_t) sudo_strtoid(p, &errstr);
507
0
      if (errstr != NULL) {
508
0
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
509
0
    goto bad;
510
0
      }
511
0
      continue;
512
0
  }
513
1.60k
  if (MATCHES(*cur, "ppid=")) {
514
0
      p = *cur + sizeof("ppid=") - 1;
515
0
      ctx->user.ppid = (pid_t) sudo_strtoid(p, &errstr);
516
0
      if (errstr != NULL) {
517
0
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
518
0
    goto bad;
519
0
      }
520
0
      continue;
521
0
  }
522
1.60k
  if (MATCHES(*cur, "sid=")) {
523
223
      p = *cur + sizeof("sid=") - 1;
524
223
      ctx->user.sid = (pid_t) sudo_strtoid(p, &errstr);
525
223
      if (errstr != NULL) {
526
9
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
527
9
    goto bad;
528
9
      }
529
214
      continue;
530
223
  }
531
1.38k
  if (MATCHES(*cur, "tcpgid=")) {
532
0
      p = *cur + sizeof("tcpgid=") - 1;
533
0
      ctx->user.tcpgid = (pid_t) sudo_strtoid(p, &errstr);
534
0
      if (errstr != NULL) {
535
0
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
536
0
    goto bad;
537
0
      }
538
0
      continue;
539
0
  }
540
1.38k
  if (MATCHES(*cur, "umask=")) {
541
927
      p = *cur + sizeof("umask=") - 1;
542
927
      ctx->user.umask = sudo_strtomode(p, &errstr);
543
927
      if (errstr != NULL) {
544
158
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
545
158
    goto bad;
546
158
      }
547
769
      continue;
548
927
  }
549
1.38k
    }
550
551
    /* User name, user-ID, group-ID and host name must be specified. */
552
21.5k
    if (ctx->user.name == NULL) {
553
3.70k
  sudo_warnx("%s", U_("user name not set by sudo front-end"));
554
3.70k
  goto bad;
555
3.70k
    }
556
17.8k
    if (ctx->user.uid == (uid_t)-1) {
557
23
  sudo_warnx("%s", U_("user-ID not set by sudo front-end"));
558
23
  goto bad;
559
23
    }
560
17.8k
    if (ctx->user.gid == (gid_t)-1) {
561
34
  sudo_warnx("%s", U_("group-ID not set by sudo front-end"));
562
34
  goto bad;
563
34
    }
564
17.8k
    if (host == NULL) {
565
41
  sudo_warnx("%s", U_("host name not set by sudo front-end"));
566
41
  goto bad;
567
41
    }
568
569
17.7k
    if (!sudoers_sethost(ctx, host, remhost)) {
570
  /* sudoers_sethost() will print a warning on error. */
571
0
  goto bad;
572
0
    }
573
17.7k
    if (ctx->user.tty == NULL) {
574
17.2k
  if ((ctx->user.tty = strdup("unknown")) == NULL)
575
0
      goto oom;
576
  /* ctx->user.ttypath remains NULL */
577
17.2k
    }
578
579
17.7k
    ctx->user.pw = sudo_getpwnam(ctx->user.name);
580
17.7k
    if (ctx->user.pw != NULL && groups != NULL) {
581
  /* sudo_parse_gids() will print a warning on error. */
582
906
  GETGROUPS_T *gids;
583
906
  int ngids = sudo_parse_gids(groups, &ctx->user.gid, &gids);
584
906
  if (ngids == -1)
585
44
      goto bad;
586
587
  /* sudo_set_gidlist will adopt gids[] */
588
862
  if (sudo_set_gidlist(ctx->user.pw, ngids, gids, NULL, ENTRY_TYPE_FRONTEND) == -1) {
589
0
      free(gids);
590
0
      goto bad;
591
0
  }
592
862
    }
593
594
    /* ttydev is only set in user_info[] for API 1.22 and above. */
595
17.7k
    if (ctx->user.ttydev == NODEV && ctx->user.ttypath != NULL) {
596
477
  struct stat sb;
597
477
  if (stat(ctx->user.ttypath, &sb) == 0)
598
91
      ctx->user.ttydev = sb.st_rdev;
599
386
  else
600
386
      sudo_warn("%s", ctx->user.ttypath);
601
477
    }
602
603
    /* umask is only set in user_info[] for API 1.10 and above. */
604
17.7k
    if (ctx->user.umask == (mode_t)-1) {
605
17.5k
  ctx->user.umask = umask(0);
606
17.5k
  umask(ctx->user.umask);
607
17.5k
    }
608
609
    /* Always reset the environment for a login shell. */
610
17.7k
    if (ISSET(flags, MODE_LOGIN_SHELL))
611
342
  def_env_reset = true;
612
613
    /* Some systems support fexecve() which we use for digest matches. */
614
17.7k
    ctx->runas.execfd = -1;
615
616
    /* Create a UUID to store in the event log. */
617
17.7k
    sudo_uuid_create(uuid);
618
17.7k
    if (sudo_uuid_to_string(uuid, ctx->uuid_str, sizeof(ctx->uuid_str)) == NULL) {
619
0
  sudo_warnx("%s", U_("unable to generate UUID"));
620
0
  goto bad;
621
0
    }
622
623
    /*
624
     * Set intercept defaults based on flags set above.
625
     * We pass -1 as the operator to indicate it is set by the front end.
626
     */
627
17.7k
    if (ISSET(ctx->settings.flags, HAVE_INTERCEPT_PTRACE)) {
628
41
  if (!append_default("intercept_type", "trace", -1, NULL, defaults))
629
0
      goto oom;
630
41
    }
631
17.7k
    if (ISSET(ctx->settings.flags, CAN_INTERCEPT_SETID)) {
632
62
  if (!append_default("intercept_allow_setid", NULL, -1, NULL, defaults))
633
0
      goto oom;
634
62
    }
635
636
#ifdef NO_ROOT_MAILER
637
    eventlog_set_mailuser(ctx->user.uid, ctx->user.gid);
638
#endif
639
640
    /* Dump settings and user info (XXX - plugin args) */
641
993k
    for (cur = info->settings; *cur != NULL; cur++)
642
975k
  sudo_debug_printf(SUDO_DEBUG_INFO, "settings: %s", *cur);
643
153k
    for (cur = info->user_info; *cur != NULL; cur++)
644
135k
  sudo_debug_printf(SUDO_DEBUG_INFO, "user_info: %s", *cur);
645
646
17.7k
#undef MATCHES
647
17.7k
#undef INVALID
648
17.7k
#undef CHECK
649
17.7k
    debug_return_uint(flags);
650
651
0
oom:
652
0
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
653
4.90k
bad:
654
4.90k
    debug_return_uint(MODE_ERROR);
655
4.90k
}
656
657
/*
658
 * Store the execution environment and other front-end settings.
659
 * Builds up the command_info list and sets argv and envp.
660
 * Consumes iolog_path if not NULL.
661
 * Returns true on success, else false.
662
 */
663
bool
664
sudoers_policy_store_result(struct sudoers_context *ctx, bool accepted,
665
    char *argv[], char *envp[], mode_t cmnd_umask, char *iolog_path, void *v)
666
11.7k
{
667
11.7k
    struct sudoers_exec_args *exec_args = v;
668
11.7k
    unsigned int info_len = 0;
669
11.7k
    debug_decl(sudoers_policy_store_result, SUDOERS_DEBUG_PLUGIN);
670
671
11.7k
    if (exec_args == NULL)
672
0
  debug_return_bool(true); /* nothing to do */
673
674
    /* Free old data, if any. */
675
11.7k
    if (command_info != NULL) {
676
4.97k
  char **cur;
677
4.97k
  sudoers_gc_remove(GC_VECTOR, command_info);
678
174k
  for (cur = command_info; *cur != NULL; cur++)
679
169k
      free(*cur);
680
4.97k
  free(command_info);
681
4.97k
    }
682
683
    /* Increase the length of command_info as needed, it is *not* checked. */
684
11.7k
    command_info = calloc(74, sizeof(char *));
685
11.7k
    if (command_info == NULL)
686
0
  goto oom;
687
688
11.7k
    if (ctx->runas.cmnd != NULL) {
689
11.3k
  command_info[info_len] = sudo_new_key_val("command", ctx->runas.cmnd);
690
11.3k
  if (command_info[info_len++] == NULL)
691
0
      goto oom;
692
11.3k
    }
693
11.7k
    if (def_log_subcmds) {
694
0
  if ((command_info[info_len++] = strdup("log_subcmds=true")) == NULL)
695
0
      goto oom;
696
0
    }
697
11.7k
    if (iolog_enabled) {
698
11.7k
  if (iolog_path)
699
2.24k
      command_info[info_len++] = iolog_path; /* now owned */
700
11.7k
  if (def_log_stdin) {
701
11.7k
      if ((command_info[info_len++] = strdup("iolog_stdin=true")) == NULL)
702
0
    goto oom;
703
11.7k
  }
704
11.7k
  if (def_log_stdout) {
705
11.7k
      if ((command_info[info_len++] = strdup("iolog_stdout=true")) == NULL)
706
0
    goto oom;
707
11.7k
  }
708
11.7k
  if (def_log_stderr) {
709
11.7k
      if ((command_info[info_len++] = strdup("iolog_stderr=true")) == NULL)
710
0
    goto oom;
711
11.7k
  }
712
11.7k
  if (def_log_ttyin) {
713
11.7k
      if ((command_info[info_len++] = strdup("iolog_ttyin=true")) == NULL)
714
0
    goto oom;
715
11.7k
  }
716
11.7k
  if (def_log_ttyout) {
717
11.7k
      if ((command_info[info_len++] = strdup("iolog_ttyout=true")) == NULL)
718
0
    goto oom;
719
11.7k
  }
720
11.7k
  if (def_compress_io) {
721
11.7k
      if ((command_info[info_len++] = strdup("iolog_compress=true")) == NULL)
722
0
    goto oom;
723
11.7k
  }
724
11.7k
  if (def_iolog_flush) {
725
11.7k
      if ((command_info[info_len++] = strdup("iolog_flush=true")) == NULL)
726
0
    goto oom;
727
11.7k
  }
728
11.7k
  if ((command_info[info_len++] = sudo_new_key_val("log_passwords",
729
11.7k
    def_log_passwords ? "true" : "false")) == NULL)
730
0
      goto oom;
731
11.7k
  if (!SLIST_EMPTY(&def_passprompt_regex)) {
732
11.7k
      char *passprompt_regex =
733
11.7k
    serialize_list("passprompt_regex", &def_passprompt_regex);
734
11.7k
      if (passprompt_regex == NULL)
735
0
    goto oom;
736
11.7k
      command_info[info_len++] = passprompt_regex;
737
11.7k
  }
738
11.7k
  if (def_maxseq != NULL) {
739
11.7k
      if ((command_info[info_len++] = sudo_new_key_val("maxseq", def_maxseq)) == NULL)
740
0
    goto oom;
741
11.7k
  }
742
11.7k
    }
743
11.7k
    if (ISSET(ctx->mode, MODE_EDIT)) {
744
652
  if ((command_info[info_len++] = strdup("sudoedit=true")) == NULL)
745
0
      goto oom;
746
652
  if (ctx->sudoedit_nfiles > 0) {
747
166
      if (asprintf(&command_info[info_len++], "sudoedit_nfiles=%d",
748
166
    ctx->sudoedit_nfiles) == -1)
749
0
    goto oom;
750
166
  }
751
652
  if (!def_sudoedit_checkdir) {
752
652
      if ((command_info[info_len++] = strdup("sudoedit_checkdir=false")) == NULL)
753
0
    goto oom;
754
652
  }
755
652
  if (def_sudoedit_follow) {
756
652
      if ((command_info[info_len++] = strdup("sudoedit_follow=true")) == NULL)
757
0
    goto oom;
758
652
  }
759
652
    }
760
11.7k
    if (def_runcwd && strcmp(def_runcwd, "*") != 0) {
761
  /* Set cwd to explicit value (sudoers or user-specified). */
762
11.7k
  if (!expand_tilde(&def_runcwd, ctx->runas.pw->pw_name)) {
763
0
      sudo_warnx(U_("invalid working directory: %s"), def_runcwd);
764
0
      goto bad;
765
0
  }
766
11.7k
  if ((command_info[info_len++] = sudo_new_key_val("cwd", def_runcwd)) == NULL)
767
0
      goto oom;
768
11.7k
    } else if (ISSET(ctx->mode, MODE_LOGIN_SHELL)) {
769
  /* Set cwd to run user's homedir. */
770
0
  if ((command_info[info_len++] = sudo_new_key_val("cwd", ctx->runas.pw->pw_dir)) == NULL)
771
0
      goto oom;
772
0
  if ((command_info[info_len++] = strdup("cwd_optional=true")) == NULL)
773
0
      goto oom;
774
0
    }
775
11.7k
    if ((command_info[info_len++] = sudo_new_key_val("runas_user", ctx->runas.pw->pw_name)) == NULL)
776
0
  goto oom;
777
11.7k
    if (ctx->runas.gr != NULL) {
778
268
  if ((command_info[info_len++] = sudo_new_key_val("runas_group", ctx->runas.gr->gr_name)) == NULL)
779
0
      goto oom;
780
268
    }
781
11.7k
    if (def_stay_setuid) {
782
0
  if (asprintf(&command_info[info_len++], "runas_uid=%u",
783
0
      (unsigned int)ctx->user.uid) == -1)
784
0
      goto oom;
785
0
  if (asprintf(&command_info[info_len++], "runas_gid=%u",
786
0
      (unsigned int)ctx->user.gid) == -1)
787
0
      goto oom;
788
0
  if (asprintf(&command_info[info_len++], "runas_euid=%u",
789
0
      (unsigned int)ctx->runas.pw->pw_uid) == -1)
790
0
      goto oom;
791
0
  if (asprintf(&command_info[info_len++], "runas_egid=%u",
792
0
      ctx->runas.gr ? (unsigned int)ctx->runas.gr->gr_gid :
793
0
      (unsigned int)ctx->runas.pw->pw_gid) == -1)
794
0
      goto oom;
795
11.7k
    } else {
796
11.7k
  if (asprintf(&command_info[info_len++], "runas_uid=%u",
797
11.7k
      (unsigned int)ctx->runas.pw->pw_uid) == -1)
798
0
      goto oom;
799
11.7k
  if (asprintf(&command_info[info_len++], "runas_gid=%u",
800
11.7k
      ctx->runas.gr ? (unsigned int)ctx->runas.gr->gr_gid :
801
11.7k
      (unsigned int)ctx->runas.pw->pw_gid) == -1)
802
0
      goto oom;
803
11.7k
    }
804
11.7k
    if (def_preserve_groups) {
805
51
  if ((command_info[info_len++] = strdup("preserve_groups=true")) == NULL)
806
0
      goto oom;
807
11.6k
    } else {
808
11.6k
  int i, len;
809
11.6k
  gid_t egid;
810
11.6k
  size_t glsize;
811
11.6k
  char *cp, *gid_list;
812
11.6k
  struct gid_list *gidlist;
813
814
  /* Only use results from a group db query, not the front end. */
815
11.6k
  gidlist = sudo_get_gidlist(ctx->runas.pw, ENTRY_TYPE_QUERIED);
816
817
  /* We reserve an extra spot in the list for the effective gid. */
818
11.6k
  glsize = sizeof("runas_groups=") - 1 +
819
11.6k
      (((size_t)gidlist->ngids + 1) * (STRLEN_MAX_UNSIGNED(gid_t) + 1));
820
11.6k
  gid_list = malloc(glsize);
821
11.6k
  if (gid_list == NULL) {
822
0
      sudo_gidlist_delref(gidlist);
823
0
      goto oom;
824
0
  }
825
11.6k
  memcpy(gid_list, "runas_groups=", sizeof("runas_groups=") - 1);
826
11.6k
  cp = gid_list + sizeof("runas_groups=") - 1;
827
11.6k
  glsize -= (size_t)(cp - gid_list);
828
829
  /* On BSD systems the effective gid is the first group in the list. */
830
11.6k
  egid = ctx->runas.gr ? (unsigned int)ctx->runas.gr->gr_gid :
831
11.6k
      (unsigned int)ctx->runas.pw->pw_gid;
832
11.6k
  len = snprintf(cp, glsize, "%u", (unsigned int)egid);
833
11.6k
  if (len < 0 || (size_t)len >= glsize) {
834
0
      sudo_warnx(U_("internal error, %s overflow"), __func__);
835
0
      free(gid_list);
836
0
      sudo_gidlist_delref(gidlist);
837
0
      goto bad;
838
0
  }
839
11.6k
  cp += len;
840
11.6k
  glsize -= (size_t)len;
841
23.3k
  for (i = 0; i < gidlist->ngids; i++) {
842
11.6k
      if (gidlist->gids[i] != egid) {
843
268
    len = snprintf(cp, glsize, ",%u",
844
268
         (unsigned int)gidlist->gids[i]);
845
268
    if (len < 0 || (size_t)len >= glsize) {
846
0
        sudo_warnx(U_("internal error, %s overflow"), __func__);
847
0
        free(gid_list);
848
0
        sudo_gidlist_delref(gidlist);
849
0
        goto bad;
850
0
    }
851
268
    cp += len;
852
268
    glsize -= (size_t)len;
853
268
      }
854
11.6k
  }
855
11.6k
  command_info[info_len++] = gid_list;
856
11.6k
  sudo_gidlist_delref(gidlist);
857
11.6k
    }
858
11.7k
    if (def_closefrom >= 0) {
859
11.7k
  if (asprintf(&command_info[info_len++], "closefrom=%d", def_closefrom) == -1)
860
0
      goto oom;
861
11.7k
    }
862
11.7k
    if (def_ignore_iolog_errors) {
863
11.7k
  if ((command_info[info_len++] = strdup("ignore_iolog_errors=true")) == NULL)
864
0
      goto oom;
865
11.7k
    }
866
11.7k
    if (def_intercept) {
867
0
  if ((command_info[info_len++] = strdup("intercept=true")) == NULL)
868
0
      goto oom;
869
0
    }
870
11.7k
    if (def_intercept_type == trace) {
871
13
  if ((command_info[info_len++] = strdup("use_ptrace=true")) == NULL)
872
0
      goto oom;
873
13
    }
874
11.7k
    if (def_intercept_verify) {
875
11.7k
  if ((command_info[info_len++] = strdup("intercept_verify=true")) == NULL)
876
0
      goto oom;
877
11.7k
    }
878
11.7k
    if (def_noexec) {
879
11.7k
  if ((command_info[info_len++] = strdup("noexec=true")) == NULL)
880
0
      goto oom;
881
11.7k
    }
882
11.7k
    if (def_exec_background) {
883
11.7k
  if ((command_info[info_len++] = strdup("exec_background=true")) == NULL)
884
0
      goto oom;
885
11.7k
    }
886
11.7k
    if (def_set_utmp) {
887
11.7k
  if ((command_info[info_len++] = strdup("set_utmp=true")) == NULL)
888
0
      goto oom;
889
11.7k
    }
890
11.7k
    if (def_use_pty) {
891
11.7k
  if ((command_info[info_len++] = strdup("use_pty=true")) == NULL)
892
0
      goto oom;
893
11.7k
    }
894
11.7k
    if (def_utmp_runas) {
895
11.7k
  if ((command_info[info_len++] = sudo_new_key_val("utmp_user", ctx->runas.pw->pw_name)) == NULL)
896
0
      goto oom;
897
11.7k
    }
898
11.7k
    if (def_iolog_mode != (S_IRUSR|S_IWUSR)) {
899
11.7k
  if (asprintf(&command_info[info_len++], "iolog_mode=0%o", (unsigned int)def_iolog_mode) == -1)
900
0
      goto oom;
901
11.7k
    }
902
11.7k
    if (def_iolog_user != NULL) {
903
0
  if ((command_info[info_len++] = sudo_new_key_val("iolog_user", def_iolog_user)) == NULL)
904
0
      goto oom;
905
0
    }
906
11.7k
    if (def_iolog_group != NULL) {
907
0
  if ((command_info[info_len++] = sudo_new_key_val("iolog_group", def_iolog_group)) == NULL)
908
0
      goto oom;
909
0
    }
910
11.7k
    if (!SLIST_EMPTY(&def_log_servers)) {
911
8.29k
  char *log_servers = serialize_list("log_servers", &def_log_servers);
912
8.29k
  if (log_servers == NULL)
913
0
      goto oom;
914
8.29k
  command_info[info_len++] = log_servers;
915
916
8.29k
  if (asprintf(&command_info[info_len++], "log_server_timeout=%u", def_log_server_timeout) == -1)
917
0
      goto oom;
918
8.29k
    }
919
920
11.7k
    if ((command_info[info_len++] = sudo_new_key_val("log_server_keepalive",
921
11.7k
      def_log_server_keepalive ? "true" : "false")) == NULL)
922
0
        goto oom;
923
924
11.7k
    if ((command_info[info_len++] = sudo_new_key_val("log_server_verify",
925
11.7k
      def_log_server_verify ? "true" : "false")) == NULL)
926
0
        goto oom;
927
928
11.7k
    if (def_log_server_cabundle != NULL) {
929
8.29k
        if ((command_info[info_len++] = sudo_new_key_val("log_server_cabundle", def_log_server_cabundle)) == NULL)
930
0
            goto oom;
931
8.29k
    }
932
11.7k
    if (def_log_server_peer_cert != NULL) {
933
8.29k
        if ((command_info[info_len++] = sudo_new_key_val("log_server_peer_cert", def_log_server_peer_cert)) == NULL)
934
0
            goto oom;
935
8.29k
    }
936
11.7k
    if (def_log_server_peer_key != NULL) {
937
8.29k
        if ((command_info[info_len++] = sudo_new_key_val("log_server_peer_key", def_log_server_peer_key)) == NULL)
938
0
            goto oom;
939
8.29k
    }
940
941
11.7k
    if (def_command_timeout > 0 || ctx->user.timeout > 0) {
942
368
  int timeout = ctx->user.timeout;
943
368
    if (timeout == 0 || (def_command_timeout > 0 && def_command_timeout < timeout))
944
0
      timeout = def_command_timeout;
945
368
  if (asprintf(&command_info[info_len++], "timeout=%u", timeout) == -1)
946
0
      goto oom;
947
368
    }
948
11.7k
    if (def_runchroot != NULL && strcmp(def_runchroot, "*") != 0) {
949
11.7k
  if (!expand_tilde(&def_runchroot, ctx->runas.pw->pw_name)) {
950
0
      sudo_warnx(U_("invalid chroot directory: %s"), def_runchroot);
951
0
      goto bad;
952
0
  }
953
11.7k
        if ((command_info[info_len++] = sudo_new_key_val("chroot", def_runchroot)) == NULL)
954
0
            goto oom;
955
11.7k
    }
956
11.7k
    if (cmnd_umask != ACCESSPERMS) {
957
4.49k
  if (asprintf(&command_info[info_len++], "umask=0%o", (unsigned int)cmnd_umask) == -1)
958
0
      goto oom;
959
4.49k
    }
960
11.7k
    if (sudoers_override_umask()) {
961
11.7k
  if ((command_info[info_len++] = strdup("umask_override=true")) == NULL)
962
0
      goto oom;
963
11.7k
    }
964
11.7k
    if (ctx->runas.execfd != -1) {
965
0
  if (sudo_version < SUDO_API_MKVERSION(1, 9)) {
966
      /* execfd only supported by plugin API 1.9 and higher */
967
0
      close(ctx->runas.execfd);
968
0
      ctx->runas.execfd = -1;
969
0
  } else {
970
0
      if (asprintf(&command_info[info_len++], "execfd=%d", ctx->runas.execfd) == -1)
971
0
    goto oom;
972
0
  }
973
0
    }
974
11.7k
    if (def_rlimit_as != NULL) {
975
0
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_as", def_rlimit_as)) == NULL)
976
0
            goto oom;
977
0
    }
978
11.7k
    if (def_rlimit_core != NULL) {
979
11.7k
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_core", def_rlimit_core)) == NULL)
980
0
            goto oom;
981
11.7k
    }
982
11.7k
    if (def_rlimit_cpu != NULL) {
983
0
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_cpu", def_rlimit_cpu)) == NULL)
984
0
            goto oom;
985
0
    }
986
11.7k
    if (def_rlimit_data != NULL) {
987
0
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_data", def_rlimit_data)) == NULL)
988
0
            goto oom;
989
0
    }
990
11.7k
    if (def_rlimit_fsize != NULL) {
991
0
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_fsize", def_rlimit_fsize)) == NULL)
992
0
            goto oom;
993
0
    }
994
11.7k
    if (def_rlimit_locks != NULL) {
995
0
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_locks", def_rlimit_locks)) == NULL)
996
0
            goto oom;
997
0
    }
998
11.7k
    if (def_rlimit_memlock != NULL) {
999
0
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_memlock", def_rlimit_memlock)) == NULL)
1000
0
            goto oom;
1001
0
    }
1002
11.7k
    if (def_rlimit_nofile != NULL) {
1003
0
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_nofile", def_rlimit_nofile)) == NULL)
1004
0
            goto oom;
1005
0
    }
1006
11.7k
    if (def_rlimit_nproc != NULL) {
1007
0
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_nproc", def_rlimit_nproc)) == NULL)
1008
0
            goto oom;
1009
0
    }
1010
11.7k
    if (def_rlimit_rss != NULL) {
1011
0
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_rss", def_rlimit_rss)) == NULL)
1012
0
            goto oom;
1013
0
    }
1014
11.7k
    if (def_rlimit_stack != NULL) {
1015
0
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_stack", def_rlimit_stack)) == NULL)
1016
0
            goto oom;
1017
0
    }
1018
11.7k
    if (ctx->source != NULL) {
1019
0
  command_info[info_len] = sudo_new_key_val("source", ctx->source);
1020
0
  if (command_info[info_len++] == NULL)
1021
0
      goto oom;
1022
0
    }
1023
#ifdef HAVE_LOGIN_CAP_H
1024
    if (def_use_loginclass) {
1025
  if ((command_info[info_len++] = sudo_new_key_val("login_class", ctx->runas.class)) == NULL)
1026
      goto oom;
1027
    }
1028
#endif /* HAVE_LOGIN_CAP_H */
1029
11.7k
    if (def_selinux && ctx->runas.role != NULL) {
1030
0
  if ((command_info[info_len++] = sudo_new_key_val("selinux_role", ctx->runas.role)) == NULL)
1031
0
      goto oom;
1032
0
    }
1033
11.7k
    if (def_selinux && ctx->runas.type != NULL) {
1034
0
  if ((command_info[info_len++] = sudo_new_key_val("selinux_type", ctx->runas.type)) == NULL)
1035
0
      goto oom;
1036
0
    }
1037
11.7k
    if (ctx->runas.apparmor_profile != NULL) {
1038
0
  if ((command_info[info_len++] = sudo_new_key_val("apparmor_profile", ctx->runas.apparmor_profile)) == NULL)
1039
0
      goto oom;
1040
0
    }
1041
11.7k
    if (ctx->runas.privs != NULL) {
1042
0
  if ((command_info[info_len++] = sudo_new_key_val("runas_privs", ctx->runas.privs)) == NULL)
1043
0
      goto oom;
1044
0
    }
1045
11.7k
    if (ctx->runas.limitprivs != NULL) {
1046
0
  if ((command_info[info_len++] = sudo_new_key_val("runas_limitprivs", ctx->runas.limitprivs)) == NULL)
1047
0
      goto oom;
1048
0
    }
1049
1050
    /* Set command start time (monotonic) for the first accepted command. */
1051
11.7k
    if (accepted && !ISSET(ctx->mode, MODE_POLICY_INTERCEPTED)) {
1052
4.52k
  if (sudo_gettime_awake(&ctx->start_time) == -1) {
1053
0
      sudo_warn("%s", U_("unable to get time of day"));
1054
0
      goto bad;
1055
0
  }
1056
4.52k
    }
1057
1058
    /* Fill in exec environment info. */
1059
11.7k
    *(exec_args->argv) = argv;
1060
11.7k
    *(exec_args->envp) = envp;
1061
11.7k
    *(exec_args->info) = command_info;
1062
1063
    /* Free command_info on exit. */
1064
11.7k
    sudoers_gc_add(GC_VECTOR, command_info);
1065
1066
11.7k
    debug_return_bool(true);
1067
1068
0
oom:
1069
0
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1070
0
bad:
1071
0
    free(audit_msg);
1072
0
    audit_msg = NULL;
1073
0
    while (info_len)
1074
0
  free(command_info[--info_len]);
1075
0
    free(command_info);
1076
0
    command_info = NULL;
1077
0
    debug_return_bool(false);
1078
0
}
1079
1080
bool
1081
sudoers_tty_present(struct sudoers_context *ctx)
1082
0
{
1083
0
    debug_decl(sudoers_tty_present, SUDOERS_DEBUG_PLUGIN);
1084
    
1085
0
    if (ctx->user.tcpgid == 0 && ctx->user.ttypath == NULL) {
1086
  /* No job control or terminal, check /dev/tty. */
1087
0
  int fd = open(_PATH_TTY, O_RDWR);
1088
0
  if (fd == -1)
1089
0
      debug_return_bool(false);
1090
0
  close(fd);
1091
0
    }
1092
0
    debug_return_bool(true);
1093
0
}
1094
1095
static int
1096
sudoers_policy_open(unsigned int version, sudo_conv_t conversation,
1097
    sudo_printf_t plugin_printf, char * const settings[],
1098
    char * const user_info[], char * const envp[], char * const args[],
1099
    const char **errstr)
1100
22.6k
{
1101
22.6k
    struct sudo_conf_debug_file_list debug_files = TAILQ_HEAD_INITIALIZER(debug_files);
1102
22.6k
    struct sudoers_open_info info;
1103
22.6k
    const char *cp, *plugin_path = NULL;
1104
22.6k
    char * const *cur;
1105
22.6k
    int ret;
1106
22.6k
    debug_decl(sudoers_policy_open, SUDOERS_DEBUG_PLUGIN);
1107
1108
22.6k
    sudo_version = version;
1109
22.6k
    sudo_conv = conversation;
1110
22.6k
    sudo_printf = plugin_printf;
1111
22.6k
    if (sudoers_policy.event_alloc != NULL)
1112
0
  plugin_event_alloc = sudoers_policy.event_alloc;
1113
1114
    /* Plugin args are only specified for API version 1.2 and higher. */
1115
22.6k
    if (sudo_version < SUDO_API_MKVERSION(1, 2))
1116
0
  args = NULL;
1117
1118
    /* Initialize the debug subsystem.  */
1119
1.42M
    for (cur = settings; (cp = *cur) != NULL; cur++) {
1120
1.39M
  if (strncmp(cp, "debug_flags=", sizeof("debug_flags=") - 1) == 0) {
1121
369
      cp += sizeof("debug_flags=") - 1;
1122
369
      if (!sudoers_debug_parse_flags(&debug_files, cp))
1123
0
    debug_return_int(-1);
1124
369
      continue;
1125
369
  }
1126
1.39M
  if (strncmp(cp, "plugin_path=", sizeof("plugin_path=") - 1) == 0) {
1127
345
      plugin_path = cp + sizeof("plugin_path=") - 1;
1128
345
      continue;
1129
345
  }
1130
1.39M
    }
1131
22.6k
    if (!sudoers_debug_register(plugin_path, &debug_files))
1132
0
  debug_return_int(-1);
1133
1134
    /* Call the sudoers init function. */
1135
22.6k
    info.settings = settings;
1136
22.6k
    info.user_info = user_info;
1137
22.6k
    info.plugin_args = args;
1138
22.6k
    ret = sudoers_init(&info, log_parse_error, envp);
1139
1140
    /* The audit functions set audit_msg on failure. */
1141
22.6k
    if (ret != 1 && audit_msg != NULL) {
1142
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1143
0
      *errstr = audit_msg;
1144
0
    }
1145
1146
22.6k
    debug_return_int(ret);
1147
22.6k
}
1148
1149
static void
1150
sudoers_policy_close(int exit_status, int error_code)
1151
22.6k
{
1152
22.6k
    const struct sudoers_context *ctx = sudoers_get_context();
1153
22.6k
    debug_decl(sudoers_policy_close, SUDOERS_DEBUG_PLUGIN);
1154
1155
22.6k
    if (session_opened) {
1156
  /* Close the session we opened in sudoers_policy_init_session(). */
1157
18.8k
  (void)sudo_auth_end_session();
1158
1159
18.8k
  if (error_code) {
1160
0
      errno = error_code;
1161
0
      sudo_warn(U_("unable to execute %s"), ctx->runas.cmnd);
1162
18.8k
  } else {
1163
18.8k
      log_exit_status(ctx, exit_status);
1164
18.8k
  }
1165
18.8k
    }
1166
1167
    /* Deregister the callback for sudo_fatal()/sudo_fatalx(). */
1168
22.6k
    sudo_fatal_callback_deregister(sudoers_cleanup);
1169
1170
    /* Free sudoers sources, ctx->user.and passwd/group caches. */
1171
22.6k
    sudoers_cleanup();
1172
1173
    /* command_info was freed by the g/c code. */
1174
22.6k
    command_info = NULL;
1175
1176
    /* Free error message passed back to front-end, if any. */
1177
22.6k
    free(audit_msg);
1178
22.6k
    audit_msg = NULL;
1179
1180
    /* sudoers_debug_deregister() calls sudo_debug_exit() for us. */
1181
22.6k
    sudoers_debug_deregister();
1182
22.6k
}
1183
1184
/*
1185
 * The init_session function is called before executing the command
1186
 * and before uid/gid changes occur.
1187
 * Returns 1 on success, 0 on failure and -1 on error.
1188
 */
1189
static int
1190
sudoers_policy_init_session(struct passwd *pwd, char **user_env[],
1191
    const char **errstr)
1192
6.98k
{
1193
6.98k
    const struct sudoers_context *ctx = sudoers_get_context();
1194
6.98k
    int ret;
1195
6.98k
    debug_decl(sudoers_policy_init_session, SUDOERS_DEBUG_PLUGIN);
1196
1197
    /* user_env is only specified for API version 1.2 and higher. */
1198
6.98k
    if (sudo_version < SUDO_API_MKVERSION(1, 2))
1199
0
  user_env = NULL;
1200
1201
6.98k
    ret = sudo_auth_begin_session(ctx, pwd, user_env);
1202
1203
6.98k
    if (ret == 1) {
1204
6.98k
  session_opened = true;
1205
6.98k
    } else if (audit_msg != NULL) {
1206
  /* The audit functions set audit_msg on failure. */
1207
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1208
0
      *errstr = audit_msg;
1209
0
    }
1210
6.98k
    debug_return_int(ret);
1211
6.98k
}
1212
1213
static int
1214
sudoers_policy_check(int argc, char * const argv[], char *env_add[],
1215
    char **command_infop[], char **argv_out[], char **user_env_out[],
1216
    const char **errstr)
1217
12.2k
{
1218
12.2k
    const struct sudoers_context *ctx = sudoers_get_context();
1219
12.2k
    unsigned int valid_flags = RUN_VALID_FLAGS;
1220
12.2k
    unsigned int flags = MODE_RUN;
1221
12.2k
    struct sudoers_exec_args exec_args;
1222
12.2k
    int ret;
1223
12.2k
    debug_decl(sudoers_policy_check, SUDOERS_DEBUG_PLUGIN);
1224
1225
12.2k
    if (ISSET(ctx->mode, MODE_EDIT)) {
1226
793
  valid_flags = EDIT_VALID_FLAGS;
1227
793
  flags = 0;
1228
793
    }
1229
12.2k
    if (!sudoers_set_mode(flags, valid_flags)) {
1230
104
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1231
104
      __func__, ctx->mode);
1232
104
  debug_return_int(-1);
1233
104
    }
1234
1235
12.1k
    exec_args.argv = argv_out;
1236
12.1k
    exec_args.envp = user_env_out;
1237
12.1k
    exec_args.info = command_infop;
1238
1239
12.1k
    ret = sudoers_check_cmnd(argc, argv, env_add, &exec_args);
1240
#ifndef NO_LEAKS
1241
    if (ret == true && sudo_version >= SUDO_API_MKVERSION(1, 3)) {
1242
  /* Unset close function if we don't need it to avoid extra process. */
1243
  if (!iolog_enabled && !def_use_pty && !def_log_exit_status &&
1244
    SLIST_EMPTY(&def_log_servers) && !sudo_auth_needs_end_session())
1245
      sudoers_policy.close = NULL;
1246
    }
1247
#endif
1248
1249
    /* The audit functions set audit_msg on failure. */
1250
12.1k
    if (ret != 1 && audit_msg != NULL) {
1251
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1252
0
      *errstr = audit_msg;
1253
0
    }
1254
12.1k
    debug_return_int(ret);
1255
12.1k
}
1256
1257
static int
1258
sudoers_policy_validate(const char **errstr)
1259
3.49k
{
1260
3.49k
    const struct sudoers_context *ctx = sudoers_get_context();
1261
3.49k
    int ret;
1262
3.49k
    debug_decl(sudoers_policy_validate, SUDOERS_DEBUG_PLUGIN);
1263
1264
3.49k
    if (!sudoers_set_mode(MODE_VALIDATE, VALIDATE_VALID_FLAGS)) {
1265
580
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1266
580
      __func__, ctx->mode);
1267
580
  debug_return_int(-1);
1268
580
    }
1269
1270
2.91k
    ret = sudoers_validate_user();
1271
1272
    /* The audit functions set audit_msg on failure. */
1273
2.91k
    if (ret != 1 && audit_msg != NULL) {
1274
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1275
0
      *errstr = audit_msg;
1276
0
    }
1277
2.91k
    debug_return_int(ret);
1278
2.91k
}
1279
1280
static void
1281
sudoers_policy_invalidate(int unlinkit)
1282
3.49k
{
1283
3.49k
    const struct sudoers_context *ctx = sudoers_get_context();
1284
3.49k
    debug_decl(sudoers_policy_invalidate, SUDOERS_DEBUG_PLUGIN);
1285
1286
3.49k
    if (!sudoers_set_mode(MODE_INVALIDATE, INVALIDATE_VALID_FLAGS)) {
1287
580
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1288
580
      __func__, ctx->mode);
1289
2.91k
    } else {
1290
2.91k
  timestamp_remove(ctx, unlinkit);
1291
2.91k
    }
1292
1293
3.49k
    debug_return;
1294
3.49k
}
1295
1296
static int
1297
sudoers_policy_list(int argc, char * const argv[], int verbose,
1298
    const char *list_user, const char **errstr)
1299
10.4k
{
1300
10.4k
    const struct sudoers_context *ctx = sudoers_get_context();
1301
10.4k
    int ret;
1302
10.4k
    debug_decl(sudoers_policy_list, SUDOERS_DEBUG_PLUGIN);
1303
1304
10.4k
    if (!sudoers_set_mode(argc ? MODE_CHECK : MODE_LIST, LIST_VALID_FLAGS)) {
1305
1.74k
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1306
1.74k
      __func__, ctx->mode);
1307
1.74k
  debug_return_int(-1);
1308
1.74k
    }
1309
1310
8.73k
    ret = sudoers_list(argc, argv, list_user, verbose);
1311
1312
    /* The audit functions set audit_msg on failure. */
1313
8.73k
    if (ret != 1 && audit_msg != NULL) {
1314
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1315
0
      *errstr = audit_msg;
1316
0
    }
1317
8.73k
    debug_return_int(ret);
1318
8.73k
}
1319
1320
static int
1321
sudoers_policy_version(int verbose)
1322
1.74k
{
1323
#ifdef HAVE_LDAP
1324
    const struct sudoers_context *ctx = sudoers_get_context();
1325
#endif
1326
1.74k
    debug_decl(sudoers_policy_version, SUDOERS_DEBUG_PLUGIN);
1327
1328
1.74k
    sudo_printf(SUDO_CONV_INFO_MSG, _("Sudoers policy plugin version %s\n"),
1329
1.74k
  PACKAGE_VERSION);
1330
1.74k
    sudo_printf(SUDO_CONV_INFO_MSG, _("Sudoers file grammar version %d\n"),
1331
1.74k
  SUDOERS_GRAMMAR_VERSION);
1332
1333
1.74k
    if (verbose) {
1334
1.74k
  sudo_printf(SUDO_CONV_INFO_MSG, _("\nSudoers path: %s\n"), path_sudoers);
1335
#ifdef HAVE_LDAP
1336
# ifdef _PATH_NSSWITCH_CONF
1337
  sudo_printf(SUDO_CONV_INFO_MSG, _("nsswitch path: %s\n"), _PATH_NSSWITCH_CONF);
1338
# endif
1339
  if (ctx->settings.ldap_conf != NULL)
1340
      sudo_printf(SUDO_CONV_INFO_MSG, _("ldap.conf path: %s\n"), ctx->settings.ldap_conf);
1341
  if (ctx->settings.ldap_secret != NULL)
1342
      sudo_printf(SUDO_CONV_INFO_MSG, _("ldap.secret path: %s\n"), ctx->settings.ldap_secret);
1343
#endif
1344
1.74k
  dump_auth_methods();
1345
1.74k
  dump_defaults();
1346
1.74k
  sudo_printf(SUDO_CONV_INFO_MSG, "\n");
1347
1.74k
  if (interfaces_string != NULL) {
1348
1.74k
      dump_interfaces(interfaces_string);
1349
1.74k
      sudo_printf(SUDO_CONV_INFO_MSG, "\n");
1350
1.74k
  }
1351
1.74k
    }
1352
1.74k
    debug_return_int(true);
1353
1.74k
}
1354
1355
static struct sudo_hook sudoers_hooks[] = {
1356
    { SUDO_HOOK_VERSION, SUDO_HOOK_SETENV, (sudo_hook_fn_t)sudoers_hook_setenv, NULL },
1357
    { SUDO_HOOK_VERSION, SUDO_HOOK_UNSETENV, (sudo_hook_fn_t)sudoers_hook_unsetenv, NULL },
1358
    { SUDO_HOOK_VERSION, SUDO_HOOK_GETENV, (sudo_hook_fn_t)sudoers_hook_getenv, NULL },
1359
    { SUDO_HOOK_VERSION, SUDO_HOOK_PUTENV, (sudo_hook_fn_t)sudoers_hook_putenv, NULL },
1360
    { 0, 0, NULL, NULL }
1361
};
1362
1363
/*
1364
 * Register environment function hooks.
1365
 * Note that we have not registered sudoers with the debug subsystem yet.
1366
 */
1367
static void
1368
sudoers_policy_register_hooks(int version, int (*register_hook)(struct sudo_hook *hook))
1369
6.92k
{
1370
6.92k
    struct sudo_hook *hook;
1371
1372
34.6k
    for (hook = sudoers_hooks; hook->hook_fn != NULL; hook++) {
1373
27.6k
  if (register_hook(hook) != 0) {
1374
0
      sudo_warn_nodebug(
1375
0
    U_("unable to register hook of type %d (version %d.%d)"),
1376
0
    hook->hook_type, SUDO_API_VERSION_GET_MAJOR(hook->hook_version),
1377
0
    SUDO_API_VERSION_GET_MINOR(hook->hook_version));
1378
0
  }
1379
27.6k
    }
1380
6.92k
}
1381
1382
/*
1383
 * De-register environment function hooks.
1384
 */
1385
static void
1386
sudoers_policy_deregister_hooks(int version, int (*deregister_hook)(struct sudo_hook *hook))
1387
6.92k
{
1388
6.92k
    struct sudo_hook *hook;
1389
1390
34.6k
    for (hook = sudoers_hooks; hook->hook_fn != NULL; hook++) {
1391
27.6k
  if (deregister_hook(hook) != 0) {
1392
0
      sudo_warn_nodebug(
1393
0
    U_("unable to deregister hook of type %d (version %d.%d)"),
1394
0
    hook->hook_type, SUDO_API_VERSION_GET_MAJOR(hook->hook_version),
1395
0
    SUDO_API_VERSION_GET_MINOR(hook->hook_version));
1396
0
  }
1397
27.6k
    }
1398
6.92k
}
1399
1400
sudo_dso_public struct policy_plugin sudoers_policy = {
1401
    SUDO_POLICY_PLUGIN,
1402
    SUDO_API_VERSION,
1403
    sudoers_policy_open,
1404
    sudoers_policy_close,
1405
    sudoers_policy_version,
1406
    sudoers_policy_check,
1407
    sudoers_policy_list,
1408
    sudoers_policy_validate,
1409
    sudoers_policy_invalidate,
1410
    sudoers_policy_init_session,
1411
    sudoers_policy_register_hooks,
1412
    sudoers_policy_deregister_hooks,
1413
    NULL /* event_alloc() filled in by sudo */
1414
};