Coverage Report

Created: 2025-10-10 07:09

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
10.7k
{
63
10.7k
    debug_decl(parse_bool, SUDOERS_DEBUG_PLUGIN);
64
65
10.7k
    switch (sudo_strtobool(line + varlen + 1)) {
66
6.84k
    case true:
67
6.84k
  SET(*flags, fval);
68
6.84k
  debug_return_int(true);
69
3.69k
    case false:
70
3.69k
  CLR(*flags, fval);
71
3.69k
  debug_return_int(false);
72
233
    default:
73
233
  sudo_warnx(U_("invalid %.*s set by sudo front-end"),
74
233
      varlen, line);
75
233
  debug_return_int(-1);
76
10.7k
    }
77
10.7k
}
78
79
14.0k
#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
1.16k
#define EDIT_VALID_FLAGS  (MODE_ASKPASS|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_EDIT)
81
12.0k
#define LIST_VALID_FLAGS  (MODE_ASKPASS|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_LIST|MODE_CHECK)
82
4.02k
#define VALIDATE_VALID_FLAGS  (MODE_ASKPASS|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_VALIDATE)
83
4.02k
#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
25.7k
{
93
25.7k
    const char *p, *errstr, *groups = NULL;
94
25.7k
    struct sudoers_open_info *info = v;
95
25.7k
    unsigned int flags = MODE_UPDATE_TICKET;
96
25.7k
    const char *host = NULL;
97
25.7k
    const char *remhost = NULL;
98
25.7k
    unsigned char uuid[16];
99
25.7k
    char * const *cur;
100
25.7k
    debug_decl(sudoers_policy_deserialize_info, SUDOERS_DEBUG_PLUGIN);
101
102
25.7k
#define MATCHES(s, v) \
103
65.0M
    (strncmp((s), (v), sizeof(v) - 1) == 0)
104
105
25.7k
#define INVALID(v) do { \
106
3.82k
    sudo_warnx(U_("invalid %.*s set by sudo front-end"), \
107
3.82k
  (int)(sizeof(v) - 2), (v)); \
108
3.82k
} while (0)
109
110
100k
#define CHECK(s, v) do { \
111
100k
    if ((s)[sizeof(v) - 1] == '\0') { \
112
201
  INVALID(v); \
113
201
  goto bad; \
114
201
    } \
115
100k
} while (0)
116
117
    /* Parse sudo.conf plugin args. */
118
25.7k
    if (info->plugin_args != NULL) {
119
24.6k
  for (cur = info->plugin_args; *cur != NULL; cur++) {
120
23.8k
      if (MATCHES(*cur, "error_recovery=")) {
121
3.83k
    int val = sudo_strtobool(*cur + sizeof("error_recovery=") - 1);
122
3.83k
    if (val == -1) {
123
3.61k
        INVALID("error_recovery=");  /* Not a fatal error. */
124
3.61k
    } else {
125
211
        ctx->parser_conf.recovery = val;
126
211
    }
127
3.83k
    continue;
128
3.83k
      }
129
19.9k
      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
19.9k
      if (MATCHES(*cur, "sudoers_file=")) {
139
18.4k
    CHECK(*cur, "sudoers_file=");
140
18.4k
    path_sudoers = *cur + sizeof("sudoers_file=") - 1;
141
18.4k
    continue;
142
18.4k
      }
143
1.52k
      if (MATCHES(*cur, "sudoers_uid=")) {
144
248
    p = *cur + sizeof("sudoers_uid=") - 1;
145
248
    ctx->parser_conf.sudoers_uid = (uid_t)sudo_strtoid(p, &errstr);
146
248
    if (errstr != NULL) {
147
28
        sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
148
28
        goto bad;
149
28
    }
150
220
    continue;
151
248
      }
152
1.27k
      if (MATCHES(*cur, "sudoers_gid=")) {
153
245
    p = *cur + sizeof("sudoers_gid=") - 1;
154
245
    ctx->parser_conf.sudoers_gid = (gid_t)sudo_strtoid(p, &errstr);
155
245
    if (errstr != NULL) {
156
35
        sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
157
35
        goto bad;
158
35
    }
159
210
    continue;
160
245
      }
161
1.02k
      if (MATCHES(*cur, "sudoers_mode=")) {
162
228
    p = *cur + sizeof("sudoers_mode=") - 1;
163
228
    ctx->parser_conf.sudoers_mode = sudo_strtomode(p, &errstr);
164
228
    if (errstr != NULL) {
165
18
        sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
166
18
        goto bad;
167
18
    }
168
210
    continue;
169
228
      }
170
801
      if (MATCHES(*cur, "ldap_conf=")) {
171
564
    CHECK(*cur, "ldap_conf=");
172
466
    ctx->settings.ldap_conf = *cur + sizeof("ldap_conf=") - 1;
173
466
    continue;
174
564
      }
175
237
      if (MATCHES(*cur, "ldap_secret=")) {
176
237
    CHECK(*cur, "ldap_secret=");
177
218
    ctx->settings.ldap_secret = *cur + sizeof("ldap_secret=") - 1;
178
218
    continue;
179
237
      }
180
237
  }
181
1.01k
    }
182
25.5k
    ctx->parser_conf.sudoers_path = path_sudoers;
183
184
    /* Parse command line settings. */
185
25.5k
    ctx->settings.flags = 0;
186
25.5k
    ctx->user.closefrom = -1;
187
25.5k
    ctx->sudoedit_nfiles = 0;
188
25.5k
    ctx->mode = 0;
189
2.51M
    for (cur = info->settings; *cur != NULL; cur++) {
190
2.49M
  if (MATCHES(*cur, "closefrom=")) {
191
988
      p = *cur + sizeof("closefrom=") - 1;
192
988
      ctx->user.closefrom = (int)sudo_strtonum(p, 3, INT_MAX, &errstr);
193
988
      if (ctx->user.closefrom == 0) {
194
2
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
195
2
    goto bad;
196
2
      }
197
986
      continue;
198
988
  }
199
2.48M
  if (MATCHES(*cur, "cmnd_chroot=")) {
200
259
      CHECK(*cur, "cmnd_chroot=");
201
258
      ctx->runas.chroot = *cur + sizeof("cmnd_chroot=") - 1;
202
258
      if (strlen(ctx->runas.chroot) >= PATH_MAX) {
203
5
    sudo_warnx(U_("path name for \"%s\" too long"), "cmnd_chroot");
204
5
    goto bad;
205
5
      }
206
253
      continue;
207
258
  }
208
2.48M
  if (MATCHES(*cur, "cmnd_cwd=")) {
209
294
      CHECK(*cur, "cmnd_cwd=");
210
293
      ctx->runas.cwd = *cur + sizeof("cmnd_cwd=") - 1;
211
293
      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
289
      continue;
216
293
  }
217
2.48M
  if (MATCHES(*cur, "runas_user=")) {
218
595
      CHECK(*cur, "runas_user=");
219
594
      ctx->runas.user = *cur + sizeof("runas_user=") - 1;
220
594
      SET(ctx->settings.flags, RUNAS_USER_SPECIFIED);
221
594
      continue;
222
595
  }
223
2.48M
  if (MATCHES(*cur, "runas_group=")) {
224
922
      CHECK(*cur, "runas_group=");
225
921
      ctx->runas.group = *cur + sizeof("runas_group=") - 1;
226
921
      SET(ctx->settings.flags, RUNAS_GROUP_SPECIFIED);
227
921
      continue;
228
922
  }
229
2.48M
  if (MATCHES(*cur, "prompt=")) {
230
      /* Allow empty prompt. */
231
17.9k
      ctx->user.prompt = *cur + sizeof("prompt=") - 1;
232
17.9k
      if (!append_default("passprompt_override", NULL, true, NULL, defaults))
233
0
    goto oom;
234
17.9k
      continue;
235
17.9k
  }
236
2.46M
  if (MATCHES(*cur, "set_home=")) {
237
694
      if (parse_bool(*cur, sizeof("set_home") - 1, &flags,
238
694
    MODE_RESET_HOME) == -1)
239
4
    goto bad;
240
690
      continue;
241
694
  }
242
2.46M
  if (MATCHES(*cur, "preserve_environment=")) {
243
513
      if (parse_bool(*cur, sizeof("preserve_environment") - 1, &flags,
244
513
    MODE_PRESERVE_ENV) == -1)
245
2
    goto bad;
246
511
      continue;
247
513
  }
248
2.46M
  if (MATCHES(*cur, "run_shell=")) {
249
2.32k
      if (parse_bool(*cur, sizeof("run_shell") -1, &flags,
250
2.32k
    MODE_SHELL) == -1)
251
4
    goto bad;
252
2.31k
      continue;
253
2.32k
  }
254
2.46M
  if (MATCHES(*cur, "login_shell=")) {
255
953
      if (parse_bool(*cur, sizeof("login_shell") - 1, &flags,
256
953
    MODE_LOGIN_SHELL) == -1)
257
1
    goto bad;
258
952
      continue;
259
953
  }
260
2.46M
  if (MATCHES(*cur, "implied_shell=")) {
261
659
      if (parse_bool(*cur, sizeof("implied_shell") - 1, &flags,
262
659
    MODE_IMPLIED_SHELL) == -1)
263
1
    goto bad;
264
658
      continue;
265
659
  }
266
2.46M
  if (MATCHES(*cur, "preserve_groups=")) {
267
501
      if (parse_bool(*cur, sizeof("preserve_groups") - 1, &flags,
268
501
    MODE_PRESERVE_GROUPS) == -1)
269
1
    goto bad;
270
500
      continue;
271
501
  }
272
2.46M
  if (MATCHES(*cur, "ignore_ticket=")) {
273
532
      if (parse_bool(*cur, sizeof("ignore_ticket") -1, &flags,
274
532
    MODE_IGNORE_TICKET) == -1)
275
1
    goto bad;
276
531
      continue;
277
532
  }
278
2.46M
  if (MATCHES(*cur, "update_ticket=")) {
279
245
      if (parse_bool(*cur, sizeof("update_ticket") -1, &flags,
280
245
    MODE_UPDATE_TICKET) == -1)
281
1
    goto bad;
282
244
      continue;
283
245
  }
284
2.46M
  if (MATCHES(*cur, "noninteractive=")) {
285
212
      if (parse_bool(*cur, sizeof("noninteractive") - 1, &flags,
286
212
    MODE_NONINTERACTIVE) == -1)
287
1
    goto bad;
288
211
      continue;
289
212
  }
290
2.46M
  if (MATCHES(*cur, "sudoedit=")) {
291
1.68k
      if (parse_bool(*cur, sizeof("sudoedit") - 1, &flags,
292
1.68k
    MODE_EDIT) == -1)
293
2
    goto bad;
294
1.68k
      continue;
295
1.68k
  }
296
2.46M
  if (MATCHES(*cur, "login_class=")) {
297
278
      CHECK(*cur, "login_class=");
298
277
      ctx->runas.class = *cur + sizeof("login_class=") - 1;
299
277
      if (!append_default("use_loginclass", NULL, true, NULL, defaults))
300
0
    goto oom;
301
277
      continue;
302
277
  }
303
2.46M
  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
1
    goto bad;
307
242
      continue;
308
243
  }
309
2.46M
  if (MATCHES(*cur, "intercept_setid=")) {
310
243
      if (parse_bool(*cur, sizeof("intercept_setid") - 1, &ctx->settings.flags,
311
243
        CAN_INTERCEPT_SETID) == -1)
312
1
    goto bad;
313
242
      continue;
314
243
  }
315
2.46M
  if (MATCHES(*cur, "selinux_role=")) {
316
219
      CHECK(*cur, "selinux_role=");
317
218
      free(ctx->runas.role);
318
218
      ctx->runas.role = strdup(*cur + sizeof("selinux_role=") - 1);
319
218
      if (ctx->runas.role == NULL)
320
0
    goto oom;
321
218
      continue;
322
218
  }
323
2.45M
  if (MATCHES(*cur, "selinux_type=")) {
324
298
      CHECK(*cur, "selinux_type=");
325
297
      free(ctx->runas.type);
326
297
      ctx->runas.type = strdup(*cur + sizeof("selinux_type=") - 1);
327
297
      if (ctx->runas.type == NULL)
328
0
    goto oom;
329
297
      continue;
330
297
  }
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
2.45M
  if (MATCHES(*cur, "network_addrs=")) {
340
401
      interfaces_string = *cur + sizeof("network_addrs=") - 1;
341
401
      if (!set_interfaces(interfaces_string)) {
342
0
    sudo_warn("%s", U_("unable to parse network address list"));
343
0
    goto bad;
344
0
      }
345
401
      continue;
346
401
  }
347
2.45M
  if (MATCHES(*cur, "max_groups=")) {
348
544
      int max_groups;
349
544
      p = *cur + sizeof("max_groups=") - 1;
350
544
      max_groups = (int)sudo_strtonum(p, 1, 1024, &errstr);
351
544
      if (max_groups == 0) {
352
5
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
353
5
    goto bad;
354
5
      }
355
539
      sudo_pwutil_set_max_groups(max_groups);
356
539
      continue;
357
544
  }
358
2.45M
  if (MATCHES(*cur, "remote_host=")) {
359
1.48k
      CHECK(*cur, "remote_host=");
360
1.48k
      remhost = *cur + sizeof("remote_host=") - 1;
361
1.48k
      continue;
362
1.48k
  }
363
2.45M
  if (MATCHES(*cur, "timeout=")) {
364
1.49k
      p = *cur + sizeof("timeout=") - 1;
365
1.49k
      ctx->user.timeout = parse_timeout(p);
366
1.49k
      if (ctx->user.timeout == -1) {
367
181
    if (errno == ERANGE)
368
158
        sudo_warnx(U_("%s: %s"), p, U_("timeout value too large"));
369
23
    else
370
23
        sudo_warnx(U_("%s: %s"), p, U_("invalid timeout value"));
371
181
    goto bad;
372
181
      }
373
1.31k
      continue;
374
1.49k
  }
375
2.45M
  if (MATCHES(*cur, "askpass=")) {
376
1.97k
      if (parse_bool(*cur, sizeof("askpass") - 1, &flags,
377
1.97k
    MODE_ASKPASS) == -1)
378
213
    goto bad;
379
1.76k
      continue;
380
1.97k
  }
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
2.45M
    }
389
    /* Ignore ticket trumps update. */
390
25.0k
    if (ISSET(flags, MODE_IGNORE_TICKET))
391
22
  CLR(flags, MODE_UPDATE_TICKET);
392
393
25.0k
    ctx->user.gid = (gid_t)-1;
394
25.0k
    ctx->user.uid = (gid_t)-1;
395
25.0k
    ctx->user.umask = (mode_t)-1;
396
25.0k
    ctx->user.ttydev = NODEV;
397
170k
    for (cur = info->user_info; *cur != NULL; cur++) {
398
145k
  if (MATCHES(*cur, "user=")) {
399
30.2k
      CHECK(*cur, "user=");
400
30.2k
      free(ctx->user.name);
401
30.2k
      if ((ctx->user.name = strdup(*cur + sizeof("user=") - 1)) == NULL)
402
0
    goto oom;
403
30.2k
      continue;
404
30.2k
  }
405
115k
  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
115k
  if (MATCHES(*cur, "uid=")) {
415
30.9k
      p = *cur + sizeof("uid=") - 1;
416
30.9k
      ctx->user.uid = (uid_t) sudo_strtoid(p, &errstr);
417
30.9k
      if (errstr != NULL) {
418
71
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
419
71
    goto bad;
420
71
      }
421
30.8k
      continue;
422
30.9k
  }
423
84.3k
  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
84.3k
  if (MATCHES(*cur, "gid=")) {
433
35.3k
      p = *cur + sizeof("gid=") - 1;
434
35.3k
      ctx->user.gid = (gid_t) sudo_strtoid(p, &errstr);
435
35.3k
      if (errstr != NULL) {
436
65
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
437
65
    goto bad;
438
65
      }
439
35.2k
      continue;
440
35.3k
  }
441
49.0k
  if (MATCHES(*cur, "groups=")) {
442
1.29k
      CHECK(*cur, "groups=");
443
1.28k
      groups = *cur + sizeof("groups=") - 1;
444
1.28k
      continue;
445
1.29k
  }
446
47.7k
  if (MATCHES(*cur, "cwd=")) {
447
1.01k
      CHECK(*cur, "cwd=");
448
1.00k
      free(ctx->user.cwd);
449
1.00k
      if ((ctx->user.cwd = strdup(*cur + sizeof("cwd=") - 1)) == NULL)
450
0
    goto oom;
451
1.00k
      continue;
452
1.00k
  }
453
46.7k
  if (MATCHES(*cur, "tty=")) {
454
894
      CHECK(*cur, "tty=");
455
881
      free(ctx->user.ttypath);
456
881
      if ((ctx->user.ttypath = strdup(*cur + sizeof("tty=") - 1)) == NULL)
457
0
    goto oom;
458
881
      ctx->user.tty = ctx->user.ttypath;
459
881
      if (strncmp(ctx->user.tty, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
460
210
    ctx->user.tty += sizeof(_PATH_DEV) - 1;
461
881
      continue;
462
881
  }
463
45.8k
  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
45.8k
  if (MATCHES(*cur, "host=")) {
482
42.8k
      CHECK(*cur, "host=");
483
42.8k
      host = *cur + sizeof("host=") - 1;
484
42.8k
      continue;
485
42.8k
  }
486
2.95k
  if (MATCHES(*cur, "lines=")) {
487
1.06k
      p = *cur + sizeof("lines=") - 1;
488
1.06k
      ctx->user.lines = (int)sudo_strtonum(p, 1, INT_MAX, &errstr);
489
1.06k
      if (ctx->user.lines == 0) {
490
9
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
491
9
    goto bad;
492
9
      }
493
1.06k
      continue;
494
1.06k
  }
495
1.89k
  if (MATCHES(*cur, "cols=")) {
496
430
      p = *cur + sizeof("cols=") - 1;
497
430
      ctx->user.cols = (int)sudo_strtonum(p, 1, INT_MAX, &errstr);
498
430
      if (ctx->user.cols == 0) {
499
35
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
500
35
    goto bad;
501
35
      }
502
395
      continue;
503
430
  }
504
1.46k
  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.46k
  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.46k
  if (MATCHES(*cur, "sid=")) {
523
232
      p = *cur + sizeof("sid=") - 1;
524
232
      ctx->user.sid = (pid_t) sudo_strtoid(p, &errstr);
525
232
      if (errstr != NULL) {
526
12
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
527
12
    goto bad;
528
12
      }
529
220
      continue;
530
232
  }
531
1.22k
  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.22k
  if (MATCHES(*cur, "umask=")) {
541
814
      p = *cur + sizeof("umask=") - 1;
542
814
      ctx->user.umask = sudo_strtomode(p, &errstr);
543
814
      if (errstr != NULL) {
544
177
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
545
177
    goto bad;
546
177
      }
547
637
      continue;
548
814
  }
549
1.22k
    }
550
551
    /* User name, user-ID, group-ID and host name must be specified. */
552
24.6k
    if (ctx->user.name == NULL) {
553
4.00k
  sudo_warnx("%s", U_("user name not set by sudo front-end"));
554
4.00k
  goto bad;
555
4.00k
    }
556
20.6k
    if (ctx->user.uid == (uid_t)-1) {
557
11
  sudo_warnx("%s", U_("user-ID not set by sudo front-end"));
558
11
  goto bad;
559
11
    }
560
20.6k
    if (ctx->user.gid == (gid_t)-1) {
561
43
  sudo_warnx("%s", U_("group-ID not set by sudo front-end"));
562
43
  goto bad;
563
43
    }
564
20.5k
    if (host == NULL) {
565
49
  sudo_warnx("%s", U_("host name not set by sudo front-end"));
566
49
  goto bad;
567
49
    }
568
569
20.5k
    if (!sudoers_sethost(ctx, host, remhost)) {
570
  /* sudoers_sethost() will print a warning on error. */
571
0
  goto bad;
572
0
    }
573
20.5k
    if (ctx->user.tty == NULL) {
574
20.1k
  if ((ctx->user.tty = strdup("unknown")) == NULL)
575
0
      goto oom;
576
  /* ctx->user.ttypath remains NULL */
577
20.1k
    }
578
579
20.5k
    ctx->user.pw = sudo_getpwnam(ctx->user.name);
580
20.5k
    if (ctx->user.pw != NULL && groups != NULL) {
581
  /* sudo_parse_gids() will print a warning on error. */
582
1.10k
  GETGROUPS_T *gids;
583
1.10k
  int ngids = sudo_parse_gids(groups, &ctx->user.gid, &gids);
584
1.10k
  if (ngids == -1)
585
53
      goto bad;
586
587
  /* sudo_set_gidlist will adopt gids[] */
588
1.05k
  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
1.05k
    }
593
594
    /* ttydev is only set in user_info[] for API 1.22 and above. */
595
20.4k
    if (ctx->user.ttydev == NODEV && ctx->user.ttypath != NULL) {
596
413
  struct stat sb;
597
413
  if (stat(ctx->user.ttypath, &sb) == 0)
598
81
      ctx->user.ttydev = sb.st_rdev;
599
332
  else
600
332
      sudo_warn("%s", ctx->user.ttypath);
601
413
    }
602
603
    /* umask is only set in user_info[] for API 1.10 and above. */
604
20.4k
    if (ctx->user.umask == (mode_t)-1) {
605
20.3k
  ctx->user.umask = umask(0);
606
20.3k
  umask(ctx->user.umask);
607
20.3k
    }
608
609
    /* Always reset the environment for a login shell. */
610
20.4k
    if (ISSET(flags, MODE_LOGIN_SHELL))
611
512
  def_env_reset = true;
612
613
    /* Some systems support fexecve() which we use for digest matches. */
614
20.4k
    ctx->runas.execfd = -1;
615
616
    /* Create a UUID to store in the event log. */
617
20.4k
    sudo_uuid_create(uuid);
618
20.4k
    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
20.4k
    if (ISSET(ctx->settings.flags, HAVE_INTERCEPT_PTRACE)) {
628
51
  if (!append_default("intercept_type", "trace", -1, NULL, defaults))
629
0
      goto oom;
630
51
    }
631
20.4k
    if (ISSET(ctx->settings.flags, CAN_INTERCEPT_SETID)) {
632
51
  if (!append_default("intercept_allow_setid", NULL, -1, NULL, defaults))
633
0
      goto oom;
634
51
    }
635
636
#ifdef NO_ROOT_MAILER
637
    eventlog_set_mailuid(ctx->user.uid);
638
#endif
639
640
    /* Dump settings and user info (XXX - plugin args) */
641
1.74M
    for (cur = info->settings; *cur != NULL; cur++)
642
1.72M
  sudo_debug_printf(SUDO_DEBUG_INFO, "settings: %s", *cur);
643
164k
    for (cur = info->user_info; *cur != NULL; cur++)
644
143k
  sudo_debug_printf(SUDO_DEBUG_INFO, "user_info: %s", *cur);
645
646
20.4k
#undef MATCHES
647
20.4k
#undef INVALID
648
20.4k
#undef CHECK
649
20.4k
    debug_return_uint(flags);
650
651
0
oom:
652
0
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
653
5.24k
bad:
654
5.24k
    debug_return_uint(MODE_ERROR);
655
5.24k
}
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
13.3k
{
667
13.3k
    struct sudoers_exec_args *exec_args = v;
668
13.3k
    unsigned int info_len = 0;
669
13.3k
    debug_decl(sudoers_policy_store_result, SUDOERS_DEBUG_PLUGIN);
670
671
13.3k
    if (exec_args == NULL)
672
0
  debug_return_bool(true); /* nothing to do */
673
674
    /* Free old data, if any. */
675
13.3k
    if (command_info != NULL) {
676
5.68k
  char **cur;
677
5.68k
  sudoers_gc_remove(GC_VECTOR, command_info);
678
200k
  for (cur = command_info; *cur != NULL; cur++)
679
194k
      free(*cur);
680
5.68k
  free(command_info);
681
5.68k
    }
682
683
    /* Increase the length of command_info as needed, it is *not* checked. */
684
13.3k
    command_info = calloc(74, sizeof(char *));
685
13.3k
    if (command_info == NULL)
686
0
  goto oom;
687
688
13.3k
    if (ctx->runas.cmnd != NULL) {
689
12.7k
  command_info[info_len] = sudo_new_key_val("command", ctx->runas.cmnd);
690
12.7k
  if (command_info[info_len++] == NULL)
691
0
      goto oom;
692
12.7k
    }
693
13.3k
    if (def_log_subcmds) {
694
0
  if ((command_info[info_len++] = strdup("log_subcmds=true")) == NULL)
695
0
      goto oom;
696
0
    }
697
13.3k
    if (iolog_enabled) {
698
13.3k
  if (iolog_path)
699
2.72k
      command_info[info_len++] = iolog_path; /* now owned */
700
13.3k
  if (def_log_stdin) {
701
13.3k
      if ((command_info[info_len++] = strdup("iolog_stdin=true")) == NULL)
702
0
    goto oom;
703
13.3k
  }
704
13.3k
  if (def_log_stdout) {
705
13.3k
      if ((command_info[info_len++] = strdup("iolog_stdout=true")) == NULL)
706
0
    goto oom;
707
13.3k
  }
708
13.3k
  if (def_log_stderr) {
709
13.3k
      if ((command_info[info_len++] = strdup("iolog_stderr=true")) == NULL)
710
0
    goto oom;
711
13.3k
  }
712
13.3k
  if (def_log_ttyin) {
713
13.3k
      if ((command_info[info_len++] = strdup("iolog_ttyin=true")) == NULL)
714
0
    goto oom;
715
13.3k
  }
716
13.3k
  if (def_log_ttyout) {
717
13.3k
      if ((command_info[info_len++] = strdup("iolog_ttyout=true")) == NULL)
718
0
    goto oom;
719
13.3k
  }
720
13.3k
  if (def_compress_io) {
721
13.3k
      if ((command_info[info_len++] = strdup("iolog_compress=true")) == NULL)
722
0
    goto oom;
723
13.3k
  }
724
13.3k
  if (def_iolog_flush) {
725
13.3k
      if ((command_info[info_len++] = strdup("iolog_flush=true")) == NULL)
726
0
    goto oom;
727
13.3k
  }
728
13.3k
  if ((command_info[info_len++] = sudo_new_key_val("log_passwords",
729
13.3k
    def_log_passwords ? "true" : "false")) == NULL)
730
0
      goto oom;
731
13.3k
  if (!SLIST_EMPTY(&def_passprompt_regex)) {
732
13.3k
      char *passprompt_regex =
733
13.3k
    serialize_list("passprompt_regex", &def_passprompt_regex);
734
13.3k
      if (passprompt_regex == NULL)
735
0
    goto oom;
736
13.3k
      command_info[info_len++] = passprompt_regex;
737
13.3k
  }
738
13.3k
  if (def_maxseq != NULL) {
739
13.3k
      if ((command_info[info_len++] = sudo_new_key_val("maxseq", def_maxseq)) == NULL)
740
0
    goto oom;
741
13.3k
  }
742
13.3k
    }
743
13.3k
    if (ISSET(ctx->mode, MODE_EDIT)) {
744
818
  if ((command_info[info_len++] = strdup("sudoedit=true")) == NULL)
745
0
      goto oom;
746
818
  if (ctx->sudoedit_nfiles > 0) {
747
128
      if (asprintf(&command_info[info_len++], "sudoedit_nfiles=%d",
748
128
    ctx->sudoedit_nfiles) == -1)
749
0
    goto oom;
750
128
  }
751
818
  if (!def_sudoedit_checkdir) {
752
818
      if ((command_info[info_len++] = strdup("sudoedit_checkdir=false")) == NULL)
753
0
    goto oom;
754
818
  }
755
818
  if (def_sudoedit_follow) {
756
818
      if ((command_info[info_len++] = strdup("sudoedit_follow=true")) == NULL)
757
0
    goto oom;
758
818
  }
759
818
    }
760
13.3k
    if (def_runcwd && strcmp(def_runcwd, "*") != 0) {
761
  /* Set cwd to explicit value (sudoers or user-specified). */
762
13.3k
  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
13.3k
  if ((command_info[info_len++] = sudo_new_key_val("cwd", def_runcwd)) == NULL)
767
0
      goto oom;
768
13.3k
    } 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
13.3k
    if ((command_info[info_len++] = sudo_new_key_val("runas_user", ctx->runas.pw->pw_name)) == NULL)
776
0
  goto oom;
777
13.3k
    if (ctx->runas.gr != NULL) {
778
380
  if ((command_info[info_len++] = sudo_new_key_val("runas_group", ctx->runas.gr->gr_name)) == NULL)
779
0
      goto oom;
780
380
    }
781
13.3k
    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
13.3k
    } else {
796
13.3k
  if (asprintf(&command_info[info_len++], "runas_uid=%u",
797
13.3k
      (unsigned int)ctx->runas.pw->pw_uid) == -1)
798
0
      goto oom;
799
13.3k
  if (asprintf(&command_info[info_len++], "runas_gid=%u",
800
13.3k
      ctx->runas.gr ? (unsigned int)ctx->runas.gr->gr_gid :
801
13.3k
      (unsigned int)ctx->runas.pw->pw_gid) == -1)
802
0
      goto oom;
803
13.3k
    }
804
13.3k
    if (def_preserve_groups) {
805
43
  if ((command_info[info_len++] = strdup("preserve_groups=true")) == NULL)
806
0
      goto oom;
807
13.2k
    } else {
808
13.2k
  int i, len;
809
13.2k
  gid_t egid;
810
13.2k
  size_t glsize;
811
13.2k
  char *cp, *gid_list;
812
13.2k
  struct gid_list *gidlist;
813
814
  /* Only use results from a group db query, not the front end. */
815
13.2k
  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
13.2k
  glsize = sizeof("runas_groups=") - 1 +
819
13.2k
      (((size_t)gidlist->ngids + 1) * (STRLEN_MAX_UNSIGNED(gid_t) + 1));
820
13.2k
  gid_list = malloc(glsize);
821
13.2k
  if (gid_list == NULL) {
822
0
      sudo_gidlist_delref(gidlist);
823
0
      goto oom;
824
0
  }
825
13.2k
  memcpy(gid_list, "runas_groups=", sizeof("runas_groups=") - 1);
826
13.2k
  cp = gid_list + sizeof("runas_groups=") - 1;
827
13.2k
  glsize -= (size_t)(cp - gid_list);
828
829
  /* On BSD systems the effective gid is the first group in the list. */
830
13.2k
  egid = ctx->runas.gr ? (unsigned int)ctx->runas.gr->gr_gid :
831
13.2k
      (unsigned int)ctx->runas.pw->pw_gid;
832
13.2k
  len = snprintf(cp, glsize, "%u", (unsigned int)egid);
833
13.2k
  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
13.2k
  cp += len;
840
13.2k
  glsize -= (size_t)len;
841
26.5k
  for (i = 0; i < gidlist->ngids; i++) {
842
13.2k
      if (gidlist->gids[i] != egid) {
843
373
    len = snprintf(cp, glsize, ",%u",
844
373
         (unsigned int)gidlist->gids[i]);
845
373
    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
373
    cp += len;
852
373
    glsize -= (size_t)len;
853
373
      }
854
13.2k
  }
855
13.2k
  command_info[info_len++] = gid_list;
856
13.2k
  sudo_gidlist_delref(gidlist);
857
13.2k
    }
858
13.3k
    if (def_closefrom >= 0) {
859
13.3k
  if (asprintf(&command_info[info_len++], "closefrom=%d", def_closefrom) == -1)
860
0
      goto oom;
861
13.3k
    }
862
13.3k
    if (def_ignore_iolog_errors) {
863
13.3k
  if ((command_info[info_len++] = strdup("ignore_iolog_errors=true")) == NULL)
864
0
      goto oom;
865
13.3k
    }
866
13.3k
    if (def_intercept) {
867
0
  if ((command_info[info_len++] = strdup("intercept=true")) == NULL)
868
0
      goto oom;
869
0
    }
870
13.3k
    if (def_intercept_type == trace) {
871
19
  if ((command_info[info_len++] = strdup("use_ptrace=true")) == NULL)
872
0
      goto oom;
873
19
    }
874
13.3k
    if (def_intercept_verify) {
875
13.3k
  if ((command_info[info_len++] = strdup("intercept_verify=true")) == NULL)
876
0
      goto oom;
877
13.3k
    }
878
13.3k
    if (def_noexec) {
879
13.3k
  if ((command_info[info_len++] = strdup("noexec=true")) == NULL)
880
0
      goto oom;
881
13.3k
    }
882
13.3k
    if (def_exec_background) {
883
13.3k
  if ((command_info[info_len++] = strdup("exec_background=true")) == NULL)
884
0
      goto oom;
885
13.3k
    }
886
13.3k
    if (def_set_utmp) {
887
13.3k
  if ((command_info[info_len++] = strdup("set_utmp=true")) == NULL)
888
0
      goto oom;
889
13.3k
    }
890
13.3k
    if (def_use_pty) {
891
13.3k
  if ((command_info[info_len++] = strdup("use_pty=true")) == NULL)
892
0
      goto oom;
893
13.3k
    }
894
13.3k
    if (def_utmp_runas) {
895
13.3k
  if ((command_info[info_len++] = sudo_new_key_val("utmp_user", ctx->runas.pw->pw_name)) == NULL)
896
0
      goto oom;
897
13.3k
    }
898
13.3k
    if (def_iolog_mode != (S_IRUSR|S_IWUSR)) {
899
13.3k
  if (asprintf(&command_info[info_len++], "iolog_mode=0%o", (unsigned int)def_iolog_mode) == -1)
900
0
      goto oom;
901
13.3k
    }
902
13.3k
    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
13.3k
    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
13.3k
    if (!SLIST_EMPTY(&def_log_servers)) {
911
9.42k
  char *log_servers = serialize_list("log_servers", &def_log_servers);
912
9.42k
  if (log_servers == NULL)
913
0
      goto oom;
914
9.42k
  command_info[info_len++] = log_servers;
915
916
9.42k
  if (asprintf(&command_info[info_len++], "log_server_timeout=%u", def_log_server_timeout) == -1)
917
0
      goto oom;
918
9.42k
    }
919
920
13.3k
    if ((command_info[info_len++] = sudo_new_key_val("log_server_keepalive",
921
13.3k
      def_log_server_keepalive ? "true" : "false")) == NULL)
922
0
        goto oom;
923
924
13.3k
    if ((command_info[info_len++] = sudo_new_key_val("log_server_verify",
925
13.3k
      def_log_server_verify ? "true" : "false")) == NULL)
926
0
        goto oom;
927
928
13.3k
    if (def_log_server_cabundle != NULL) {
929
9.42k
        if ((command_info[info_len++] = sudo_new_key_val("log_server_cabundle", def_log_server_cabundle)) == NULL)
930
0
            goto oom;
931
9.42k
    }
932
13.3k
    if (def_log_server_peer_cert != NULL) {
933
9.42k
        if ((command_info[info_len++] = sudo_new_key_val("log_server_peer_cert", def_log_server_peer_cert)) == NULL)
934
0
            goto oom;
935
9.42k
    }
936
13.3k
    if (def_log_server_peer_key != NULL) {
937
9.42k
        if ((command_info[info_len++] = sudo_new_key_val("log_server_peer_key", def_log_server_peer_key)) == NULL)
938
0
            goto oom;
939
9.42k
    }
940
941
13.3k
    if (def_command_timeout > 0 || ctx->user.timeout > 0) {
942
375
  int timeout = ctx->user.timeout;
943
375
    if (timeout == 0 || (def_command_timeout > 0 && def_command_timeout < timeout))
944
0
      timeout = def_command_timeout;
945
375
  if (asprintf(&command_info[info_len++], "timeout=%u", timeout) == -1)
946
0
      goto oom;
947
375
    }
948
13.3k
    if (def_runchroot != NULL && strcmp(def_runchroot, "*") != 0) {
949
13.3k
  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
13.3k
        if ((command_info[info_len++] = sudo_new_key_val("chroot", def_runchroot)) == NULL)
954
0
            goto oom;
955
13.3k
    }
956
13.3k
    if (cmnd_umask != ACCESSPERMS) {
957
5.45k
  if (asprintf(&command_info[info_len++], "umask=0%o", (unsigned int)cmnd_umask) == -1)
958
0
      goto oom;
959
5.45k
    }
960
13.3k
    if (sudoers_override_umask()) {
961
13.3k
  if ((command_info[info_len++] = strdup("umask_override=true")) == NULL)
962
0
      goto oom;
963
13.3k
    }
964
13.3k
    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
13.3k
    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
13.3k
    if (def_rlimit_core != NULL) {
979
13.3k
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_core", def_rlimit_core)) == NULL)
980
0
            goto oom;
981
13.3k
    }
982
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    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
13.3k
    if (accepted && !ISSET(ctx->mode, MODE_POLICY_INTERCEPTED)) {
1052
5.51k
  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
5.51k
    }
1057
1058
    /* Fill in exec environment info. */
1059
13.3k
    *(exec_args->argv) = argv;
1060
13.3k
    *(exec_args->envp) = envp;
1061
13.3k
    *(exec_args->info) = command_info;
1062
1063
    /* Free command_info on exit. */
1064
13.3k
    sudoers_gc_add(GC_VECTOR, command_info);
1065
1066
13.3k
    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
25.7k
{
1101
25.7k
    struct sudo_conf_debug_file_list debug_files = TAILQ_HEAD_INITIALIZER(debug_files);
1102
25.7k
    struct sudoers_open_info info;
1103
25.7k
    const char *cp, *plugin_path = NULL;
1104
25.7k
    char * const *cur;
1105
25.7k
    int ret;
1106
25.7k
    debug_decl(sudoers_policy_open, SUDOERS_DEBUG_PLUGIN);
1107
1108
25.7k
    sudo_version = version;
1109
25.7k
    sudo_conv = conversation;
1110
25.7k
    sudo_printf = plugin_printf;
1111
25.7k
    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
25.7k
    if (sudo_version < SUDO_API_MKVERSION(1, 2))
1116
0
  args = NULL;
1117
1118
    /* Initialize the debug subsystem.  */
1119
2.57M
    for (cur = settings; (cp = *cur) != NULL; cur++) {
1120
2.54M
  if (strncmp(cp, "debug_flags=", sizeof("debug_flags=") - 1) == 0) {
1121
214
      cp += sizeof("debug_flags=") - 1;
1122
214
      if (!sudoers_debug_parse_flags(&debug_files, cp))
1123
0
    debug_return_int(-1);
1124
214
      continue;
1125
214
  }
1126
2.54M
  if (strncmp(cp, "plugin_path=", sizeof("plugin_path=") - 1) == 0) {
1127
217
      plugin_path = cp + sizeof("plugin_path=") - 1;
1128
217
      continue;
1129
217
  }
1130
2.54M
    }
1131
25.7k
    if (!sudoers_debug_register(plugin_path, &debug_files))
1132
0
  debug_return_int(-1);
1133
1134
    /* Call the sudoers init function. */
1135
25.7k
    info.settings = settings;
1136
25.7k
    info.user_info = user_info;
1137
25.7k
    info.plugin_args = args;
1138
25.7k
    ret = sudoers_init(&info, log_parse_error, envp);
1139
1140
    /* The audit functions set audit_msg on failure. */
1141
25.7k
    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
25.7k
    debug_return_int(ret);
1147
25.7k
}
1148
1149
static void
1150
sudoers_policy_close(int exit_status, int error_code)
1151
25.7k
{
1152
25.7k
    const struct sudoers_context *ctx = sudoers_get_context();
1153
25.7k
    debug_decl(sudoers_policy_close, SUDOERS_DEBUG_PLUGIN);
1154
1155
25.7k
    if (session_opened) {
1156
  /* Close the session we opened in sudoers_policy_init_session(). */
1157
21.2k
  (void)sudo_auth_end_session();
1158
1159
21.2k
  if (error_code) {
1160
0
      errno = error_code;
1161
0
      sudo_warn(U_("unable to execute %s"), ctx->runas.cmnd);
1162
21.2k
  } else {
1163
21.2k
      log_exit_status(ctx, exit_status);
1164
21.2k
  }
1165
21.2k
    }
1166
1167
    /* Deregister the callback for sudo_fatal()/sudo_fatalx(). */
1168
25.7k
    sudo_fatal_callback_deregister(sudoers_cleanup);
1169
1170
    /* Free sudoers sources, ctx->user.and passwd/group caches. */
1171
25.7k
    sudoers_cleanup();
1172
1173
    /* command_info was freed by the g/c code. */
1174
25.7k
    command_info = NULL;
1175
1176
    /* Free error message passed back to front-end, if any. */
1177
25.7k
    free(audit_msg);
1178
25.7k
    audit_msg = NULL;
1179
1180
    /* sudoers_debug_deregister() calls sudo_debug_exit() for us. */
1181
25.7k
    sudoers_debug_deregister();
1182
25.7k
}
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
8.04k
{
1193
8.04k
    const struct sudoers_context *ctx = sudoers_get_context();
1194
8.04k
    int ret;
1195
8.04k
    debug_decl(sudoers_policy_init_session, SUDOERS_DEBUG_PLUGIN);
1196
1197
    /* user_env is only specified for API version 1.2 and higher. */
1198
8.04k
    if (sudo_version < SUDO_API_MKVERSION(1, 2))
1199
0
  user_env = NULL;
1200
1201
8.04k
    ret = sudo_auth_begin_session(ctx, pwd, user_env);
1202
1203
8.04k
    if (ret == 1) {
1204
8.04k
  session_opened = true;
1205
8.04k
    } 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
8.04k
    debug_return_int(ret);
1211
8.04k
}
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
14.0k
{
1218
14.0k
    const struct sudoers_context *ctx = sudoers_get_context();
1219
14.0k
    unsigned int valid_flags = RUN_VALID_FLAGS;
1220
14.0k
    unsigned int flags = MODE_RUN;
1221
14.0k
    struct sudoers_exec_args exec_args;
1222
14.0k
    int ret;
1223
14.0k
    debug_decl(sudoers_policy_check, SUDOERS_DEBUG_PLUGIN);
1224
1225
14.0k
    if (ISSET(ctx->mode, MODE_EDIT)) {
1226
1.16k
  valid_flags = EDIT_VALID_FLAGS;
1227
1.16k
  flags = 0;
1228
1.16k
    }
1229
14.0k
    if (!sudoers_set_mode(flags, valid_flags)) {
1230
118
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1231
118
      __func__, ctx->mode);
1232
118
  debug_return_int(-1);
1233
118
    }
1234
1235
13.9k
    exec_args.argv = argv_out;
1236
13.9k
    exec_args.envp = user_env_out;
1237
13.9k
    exec_args.info = command_infop;
1238
1239
13.9k
    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
13.9k
    if (ret != 1 && audit_msg != NULL) {
1251
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1252
0
      *errstr = audit_msg;
1253
0
    }
1254
13.9k
    debug_return_int(ret);
1255
13.9k
}
1256
1257
static int
1258
sudoers_policy_validate(const char **errstr)
1259
4.02k
{
1260
4.02k
    const struct sudoers_context *ctx = sudoers_get_context();
1261
4.02k
    int ret;
1262
4.02k
    debug_decl(sudoers_policy_validate, SUDOERS_DEBUG_PLUGIN);
1263
1264
4.02k
    if (!sudoers_set_mode(MODE_VALIDATE, VALIDATE_VALID_FLAGS)) {
1265
782
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1266
782
      __func__, ctx->mode);
1267
782
  debug_return_int(-1);
1268
782
    }
1269
1270
3.23k
    ret = sudoers_validate_user();
1271
1272
    /* The audit functions set audit_msg on failure. */
1273
3.23k
    if (ret != 1 && audit_msg != NULL) {
1274
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1275
0
      *errstr = audit_msg;
1276
0
    }
1277
3.23k
    debug_return_int(ret);
1278
3.23k
}
1279
1280
static void
1281
sudoers_policy_invalidate(int unlinkit)
1282
4.02k
{
1283
4.02k
    const struct sudoers_context *ctx = sudoers_get_context();
1284
4.02k
    debug_decl(sudoers_policy_invalidate, SUDOERS_DEBUG_PLUGIN);
1285
1286
4.02k
    if (!sudoers_set_mode(MODE_INVALIDATE, INVALIDATE_VALID_FLAGS)) {
1287
782
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1288
782
      __func__, ctx->mode);
1289
3.23k
    } else {
1290
3.23k
  timestamp_remove(ctx, unlinkit);
1291
3.23k
    }
1292
1293
4.02k
    debug_return;
1294
4.02k
}
1295
1296
static int
1297
sudoers_policy_list(int argc, char * const argv[], int verbose,
1298
    const char *list_user, const char **errstr)
1299
12.0k
{
1300
12.0k
    const struct sudoers_context *ctx = sudoers_get_context();
1301
12.0k
    int ret;
1302
12.0k
    debug_decl(sudoers_policy_list, SUDOERS_DEBUG_PLUGIN);
1303
1304
12.0k
    if (!sudoers_set_mode(argc ? MODE_CHECK : MODE_LIST, LIST_VALID_FLAGS)) {
1305
2.34k
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1306
2.34k
      __func__, ctx->mode);
1307
2.34k
  debug_return_int(-1);
1308
2.34k
    }
1309
1310
9.71k
    ret = sudoers_list(argc, argv, list_user, verbose);
1311
1312
    /* The audit functions set audit_msg on failure. */
1313
9.71k
    if (ret != 1 && audit_msg != NULL) {
1314
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1315
0
      *errstr = audit_msg;
1316
0
    }
1317
9.71k
    debug_return_int(ret);
1318
9.71k
}
1319
1320
static int
1321
sudoers_policy_version(int verbose)
1322
2.01k
{
1323
#ifdef HAVE_LDAP
1324
    const struct sudoers_context *ctx = sudoers_get_context();
1325
#endif
1326
2.01k
    debug_decl(sudoers_policy_version, SUDOERS_DEBUG_PLUGIN);
1327
1328
2.01k
    sudo_printf(SUDO_CONV_INFO_MSG, _("Sudoers policy plugin version %s\n"),
1329
2.01k
  PACKAGE_VERSION);
1330
2.01k
    sudo_printf(SUDO_CONV_INFO_MSG, _("Sudoers file grammar version %d\n"),
1331
2.01k
  SUDOERS_GRAMMAR_VERSION);
1332
1333
2.01k
    if (verbose) {
1334
2.01k
  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
2.01k
  dump_auth_methods();
1345
2.01k
  dump_defaults();
1346
2.01k
  sudo_printf(SUDO_CONV_INFO_MSG, "\n");
1347
2.01k
  if (interfaces_string != NULL) {
1348
2.01k
      dump_interfaces(interfaces_string);
1349
2.01k
      sudo_printf(SUDO_CONV_INFO_MSG, "\n");
1350
2.01k
  }
1351
2.01k
    }
1352
2.01k
    debug_return_int(true);
1353
2.01k
}
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
7.63k
{
1370
7.63k
    struct sudo_hook *hook;
1371
1372
38.1k
    for (hook = sudoers_hooks; hook->hook_fn != NULL; hook++) {
1373
30.5k
  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
30.5k
    }
1380
7.63k
}
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
7.63k
{
1388
7.63k
    struct sudo_hook *hook;
1389
1390
38.1k
    for (hook = sudoers_hooks; hook->hook_fn != NULL; hook++) {
1391
30.5k
  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
30.5k
    }
1398
7.63k
}
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
};