Coverage Report

Created: 2026-11-23 06:16

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