Coverage Report

Created: 2026-12-01 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/policy.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2010-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
10.7k
{
63
10.7k
    debug_decl(parse_bool, SUDOERS_DEBUG_PLUGIN);
64
65
10.7k
    switch (sudo_strtobool(line + varlen + 1)) {
66
6.07k
    case true:
67
6.07k
  SET(*flags, fval);
68
6.07k
  debug_return_int(true);
69
4.44k
    case false:
70
4.44k
  CLR(*flags, fval);
71
4.44k
  debug_return_int(false);
72
189
    default:
73
189
  sudo_warnx(U_("invalid %.*s set by sudo front-end"),
74
189
      varlen, line);
75
189
  debug_return_int(-1);
76
10.7k
    }
77
10.7k
}
78
79
16.6k
#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.86k
#define EDIT_VALID_FLAGS  (MODE_ASKPASS|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_EDIT)
81
14.2k
#define LIST_VALID_FLAGS  (MODE_ASKPASS|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_LIST|MODE_CHECK)
82
4.76k
#define VALIDATE_VALID_FLAGS  (MODE_ASKPASS|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_UPDATE_TICKET|MODE_VALIDATE)
83
4.76k
#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
29.5k
{
93
29.5k
    const size_t host_name_max = sudo_host_name_max();
94
29.5k
    const size_t login_name_max = sudo_login_name_max();
95
29.5k
    const char *p, *errstr, *groups = NULL;
96
29.5k
    struct sudoers_open_info *info = v;
97
29.5k
    unsigned int flags = MODE_UPDATE_TICKET;
98
29.5k
    const char *host = NULL;
99
29.5k
    const char *remhost = NULL;
100
29.5k
    unsigned char uuid[16];
101
29.5k
    char * const *cur;
102
29.5k
    debug_decl(sudoers_policy_deserialize_info, SUDOERS_DEBUG_PLUGIN);
103
104
29.5k
#define MATCHES(s, v) \
105
38.8M
    (strncmp((s), (v), sizeof(v) - 1) == 0)
106
107
29.5k
#define INVALID(v) do { \
108
5.59k
    sudo_warnx(U_("invalid %.*s set by sudo front-end"), \
109
5.59k
  (int)(sizeof(v) - 2), (v)); \
110
5.59k
} while (0)
111
112
117k
#define CHECK(s, v) do { \
113
117k
    if ((s)[sizeof(v) - 1] == '\0') { \
114
208
  INVALID(v); \
115
208
  goto bad; \
116
208
    } \
117
117k
} while (0)
118
119
    /* Parse sudo.conf plugin args. */
120
29.5k
    if (info->plugin_args != NULL) {
121
26.6k
  for (cur = info->plugin_args; *cur != NULL; cur++) {
122
25.8k
      if (MATCHES(*cur, "error_recovery=")) {
123
5.60k
    int val = sudo_strtobool(*cur + sizeof("error_recovery=") - 1);
124
5.60k
    if (val == -1) {
125
5.39k
        INVALID("error_recovery=");  /* Not a fatal error. */
126
5.39k
    } else {
127
213
        ctx->parser_conf.recovery = val;
128
213
    }
129
5.60k
    continue;
130
5.60k
      }
131
20.2k
      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.2k
      if (MATCHES(*cur, "sudoers_file=")) {
141
18.4k
    CHECK(*cur, "sudoers_file=");
142
18.4k
    path_sudoers = *cur + sizeof("sudoers_file=") - 1;
143
18.4k
    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.4k
    continue;
148
18.4k
      }
149
1.78k
      if (MATCHES(*cur, "sudoers_uid=")) {
150
231
    p = *cur + sizeof("sudoers_uid=") - 1;
151
231
    ctx->parser_conf.sudoers_uid = (uid_t)sudo_strtoid(p, &errstr);
152
231
    if (errstr != NULL) {
153
21
        sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
154
21
        goto bad;
155
21
    }
156
210
    continue;
157
231
      }
158
1.55k
      if (MATCHES(*cur, "sudoers_gid=")) {
159
329
    p = *cur + sizeof("sudoers_gid=") - 1;
160
329
    ctx->parser_conf.sudoers_gid = (gid_t)sudo_strtoid(p, &errstr);
161
329
    if (errstr != NULL) {
162
29
        sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
163
29
        goto bad;
164
29
    }
165
300
    continue;
166
329
      }
167
1.22k
      if (MATCHES(*cur, "sudoers_mode=")) {
168
244
    p = *cur + sizeof("sudoers_mode=") - 1;
169
244
    ctx->parser_conf.sudoers_mode = sudo_strtomode(p, &errstr);
170
244
    if (errstr != NULL) {
171
26
        sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
172
26
        goto bad;
173
26
    }
174
218
    continue;
175
244
      }
176
985
      if (MATCHES(*cur, "ldap_conf=")) {
177
455
    CHECK(*cur, "ldap_conf=");
178
353
    ctx->settings.ldap_conf = *cur + sizeof("ldap_conf=") - 1;
179
353
    if (strlen(ctx->settings.ldap_conf) >= PATH_MAX) {
180
3
        sudo_warnx(U_("path name for \"%s\" too long"), "ldap_conf");
181
3
        goto bad;
182
3
    }
183
350
    continue;
184
353
      }
185
530
      if (MATCHES(*cur, "ldap_secret=")) {
186
530
    CHECK(*cur, "ldap_secret=");
187
509
    ctx->settings.ldap_secret = *cur + sizeof("ldap_secret=") - 1;
188
509
    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
507
    continue;
193
509
      }
194
530
  }
195
1.05k
    }
196
29.3k
    ctx->parser_conf.sudoers_path = path_sudoers;
197
198
    /* Parse command line settings. */
199
29.3k
    ctx->settings.flags = 0;
200
29.3k
    ctx->user.closefrom = -1;
201
29.3k
    ctx->sudoedit_nfiles = 0;
202
29.3k
    ctx->mode = 0;
203
1.50M
    for (cur = info->settings; *cur != NULL; cur++) {
204
1.48M
  if (MATCHES(*cur, "closefrom=")) {
205
1.30k
      p = *cur + sizeof("closefrom=") - 1;
206
1.30k
      ctx->user.closefrom = (int)sudo_strtonum(p, 3, INT_MAX, &errstr);
207
1.30k
      if (ctx->user.closefrom == 0) {
208
2
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
209
2
    goto bad;
210
2
      }
211
1.30k
      continue;
212
1.30k
  }
213
1.47M
  if (MATCHES(*cur, "cmnd_chroot=")) {
214
291
      CHECK(*cur, "cmnd_chroot=");
215
290
      ctx->runas.chroot = *cur + sizeof("cmnd_chroot=") - 1;
216
290
      if (strlen(ctx->runas.chroot) >= PATH_MAX) {
217
1
    sudo_warnx(U_("path name for \"%s\" too long"), "cmnd_chroot");
218
1
    goto bad;
219
1
      }
220
289
      continue;
221
290
  }
222
1.47M
  if (MATCHES(*cur, "cmnd_cwd=")) {
223
443
      CHECK(*cur, "cmnd_cwd=");
224
442
      ctx->runas.cwd = *cur + sizeof("cmnd_cwd=") - 1;
225
442
      if (strlen(ctx->runas.cwd) >= PATH_MAX) {
226
9
    sudo_warnx(U_("path name for \"%s\" too long"), "cmnd_cwd");
227
9
    goto bad;
228
9
      }
229
433
      continue;
230
442
  }
231
1.47M
  if (MATCHES(*cur, "runas_user=")) {
232
1.20k
      CHECK(*cur, "runas_user=");
233
1.20k
      ctx->runas.user = *cur + sizeof("runas_user=") - 1;
234
1.20k
      if (strlen(ctx->runas.user) >= login_name_max) {
235
30
    errno = ENAMETOOLONG;
236
30
    sudo_warn("runas_user");
237
30
    goto bad;
238
30
      }
239
1.17k
      SET(ctx->settings.flags, RUNAS_USER_SPECIFIED);
240
1.17k
      continue;
241
1.20k
  }
242
1.47M
  if (MATCHES(*cur, "runas_group=")) {
243
956
      CHECK(*cur, "runas_group=");
244
955
      ctx->runas.group = *cur + sizeof("runas_group=") - 1;
245
955
      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
928
      SET(ctx->settings.flags, RUNAS_GROUP_SPECIFIED);
251
928
      continue;
252
955
  }
253
1.47M
  if (MATCHES(*cur, "prompt=")) {
254
      /* Allow empty prompt. */
255
21.2k
      ctx->user.prompt = *cur + sizeof("prompt=") - 1;
256
21.2k
      if (!append_default("passprompt_override", NULL, true, NULL, defaults))
257
0
    goto oom;
258
21.2k
      continue;
259
21.2k
  }
260
1.45M
  if (MATCHES(*cur, "set_home=")) {
261
586
      if (parse_bool(*cur, sizeof("set_home") - 1, &flags,
262
586
    MODE_RESET_HOME) == -1)
263
4
    goto bad;
264
582
      continue;
265
586
  }
266
1.45M
  if (MATCHES(*cur, "preserve_environment=")) {
267
541
      if (parse_bool(*cur, sizeof("preserve_environment") - 1, &flags,
268
541
    MODE_PRESERVE_ENV) == -1)
269
1
    goto bad;
270
540
      continue;
271
541
  }
272
1.45M
  if (MATCHES(*cur, "run_shell=")) {
273
1.74k
      if (parse_bool(*cur, sizeof("run_shell") -1, &flags,
274
1.74k
    MODE_SHELL) == -1)
275
5
    goto bad;
276
1.74k
      continue;
277
1.74k
  }
278
1.45M
  if (MATCHES(*cur, "login_shell=")) {
279
1.71k
      if (parse_bool(*cur, sizeof("login_shell") - 1, &flags,
280
1.71k
    MODE_LOGIN_SHELL) == -1)
281
3
    goto bad;
282
1.70k
      continue;
283
1.71k
  }
284
1.45M
  if (MATCHES(*cur, "implied_shell=")) {
285
440
      if (parse_bool(*cur, sizeof("implied_shell") - 1, &flags,
286
440
    MODE_IMPLIED_SHELL) == -1)
287
2
    goto bad;
288
438
      continue;
289
440
  }
290
1.45M
  if (MATCHES(*cur, "preserve_groups=")) {
291
795
      if (parse_bool(*cur, sizeof("preserve_groups") - 1, &flags,
292
795
    MODE_PRESERVE_GROUPS) == -1)
293
1
    goto bad;
294
794
      continue;
295
795
  }
296
1.44M
  if (MATCHES(*cur, "ignore_ticket=")) {
297
212
      if (parse_bool(*cur, sizeof("ignore_ticket") -1, &flags,
298
212
    MODE_IGNORE_TICKET) == -1)
299
1
    goto bad;
300
211
      continue;
301
212
  }
302
1.44M
  if (MATCHES(*cur, "update_ticket=")) {
303
251
      if (parse_bool(*cur, sizeof("update_ticket") -1, &flags,
304
251
    MODE_UPDATE_TICKET) == -1)
305
2
    goto bad;
306
249
      continue;
307
251
  }
308
1.44M
  if (MATCHES(*cur, "noninteractive=")) {
309
213
      if (parse_bool(*cur, sizeof("noninteractive") - 1, &flags,
310
213
    MODE_NONINTERACTIVE) == -1)
311
2
    goto bad;
312
211
      continue;
313
213
  }
314
1.44M
  if (MATCHES(*cur, "sudoedit=")) {
315
1.59k
      if (parse_bool(*cur, sizeof("sudoedit") - 1, &flags,
316
1.59k
    MODE_EDIT) == -1)
317
8
    goto bad;
318
1.58k
      continue;
319
1.59k
  }
320
1.44M
  if (MATCHES(*cur, "login_class=")) {
321
479
      CHECK(*cur, "login_class=");
322
478
      ctx->runas.class = *cur + sizeof("login_class=") - 1;
323
478
      if (strlen(ctx->runas.class) >= 1024) {
324
7
    errno = ENAMETOOLONG;
325
7
    sudo_warn("login_class");
326
7
    goto bad;
327
7
      }
328
471
      if (!append_default("use_loginclass", NULL, true, NULL, defaults))
329
0
    goto oom;
330
471
      continue;
331
471
  }
332
1.44M
  if (MATCHES(*cur, "intercept_ptrace=")) {
333
273
      if (parse_bool(*cur, sizeof("intercept_ptrace") - 1, &ctx->settings.flags,
334
273
        HAVE_INTERCEPT_PTRACE) == -1)
335
1
    goto bad;
336
272
      continue;
337
273
  }
338
1.44M
  if (MATCHES(*cur, "intercept_setid=")) {
339
290
      if (parse_bool(*cur, sizeof("intercept_setid") - 1, &ctx->settings.flags,
340
290
        CAN_INTERCEPT_SETID) == -1)
341
1
    goto bad;
342
289
      continue;
343
290
  }
344
1.44M
  if (MATCHES(*cur, "selinux_role=")) {
345
329
      CHECK(*cur, "selinux_role=");
346
328
      p = *cur + sizeof("selinux_role=") - 1;
347
328
      if (strlen(p) >= 1024) {
348
8
    errno = ENAMETOOLONG;
349
8
    sudo_warn("selinux_role");
350
8
    goto bad;
351
8
      }
352
320
      free(ctx->runas.role);
353
320
      if ((ctx->runas.role = strdup(p)) == NULL)
354
0
    goto oom;
355
320
      continue;
356
320
  }
357
1.44M
  if (MATCHES(*cur, "selinux_type=")) {
358
245
      CHECK(*cur, "selinux_type=");
359
244
      p = *cur + sizeof("selinux_type=") - 1;
360
244
      if (strlen(p) >= 1024) {
361
8
    errno = ENAMETOOLONG;
362
8
    sudo_warn("selinux_type");
363
8
    goto bad;
364
8
      }
365
236
      free(ctx->runas.type);
366
236
      if ((ctx->runas.type = strdup(p)) == NULL)
367
0
    goto oom;
368
236
      continue;
369
236
  }
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.44M
  if (MATCHES(*cur, "network_addrs=")) {
384
255
      interfaces_string = *cur + sizeof("network_addrs=") - 1;
385
255
      if (!set_interfaces(interfaces_string)) {
386
0
    sudo_warn("%s", U_("unable to parse network address list"));
387
0
    goto bad;
388
0
      }
389
255
      continue;
390
255
  }
391
1.44M
  if (MATCHES(*cur, "max_groups=")) {
392
261
      int max_groups;
393
261
      p = *cur + sizeof("max_groups=") - 1;
394
261
      max_groups = (int)sudo_strtonum(p, 1, 1024, &errstr);
395
261
      if (max_groups == 0) {
396
7
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
397
7
    goto bad;
398
7
      }
399
254
      sudo_pwutil_set_max_groups(max_groups);
400
254
      continue;
401
261
  }
402
1.44M
  if (MATCHES(*cur, "remote_host=")) {
403
1.81k
      CHECK(*cur, "remote_host=");
404
1.81k
      p = *cur + sizeof("remote_host=") - 1;
405
1.81k
      if (strlen(p) >= host_name_max) {
406
32
    errno = ENAMETOOLONG;
407
32
    sudo_warn("remote_host");
408
32
    goto bad;
409
32
      }
410
1.78k
      remhost = p;
411
1.78k
      continue;
412
1.81k
  }
413
1.44M
  if (MATCHES(*cur, "timeout=")) {
414
1.39k
      p = *cur + sizeof("timeout=") - 1;
415
1.39k
      ctx->user.timeout = parse_timeout(p);
416
1.39k
      if (ctx->user.timeout == -1) {
417
184
    if (errno == ERANGE)
418
163
        sudo_warnx(U_("%s: %s"), p, U_("timeout value too large"));
419
21
    else
420
21
        sudo_warnx(U_("%s: %s"), p, U_("invalid timeout value"));
421
184
    goto bad;
422
184
      }
423
1.20k
      continue;
424
1.39k
  }
425
1.44M
  if (MATCHES(*cur, "askpass=")) {
426
2.06k
      if (parse_bool(*cur, sizeof("askpass") - 1, &flags,
427
2.06k
    MODE_ASKPASS) == -1)
428
158
    goto bad;
429
1.90k
      continue;
430
2.06k
  }
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.44M
    }
444
    /* Ignore ticket trumps update. */
445
28.8k
    if (ISSET(flags, MODE_IGNORE_TICKET))
446
31
  CLR(flags, MODE_UPDATE_TICKET);
447
448
28.8k
    ctx->user.gid = (gid_t)-1;
449
28.8k
    ctx->user.uid = (gid_t)-1;
450
28.8k
    ctx->user.umask = (mode_t)-1;
451
28.8k
    ctx->user.ttydev = NODEV;
452
193k
    for (cur = info->user_info; *cur != NULL; cur++) {
453
165k
  if (MATCHES(*cur, "user=")) {
454
31.8k
      CHECK(*cur, "user=");
455
31.8k
      p = *cur + sizeof("user=") - 1;
456
31.8k
      if (strlen(p) >= login_name_max) {
457
19
    errno = ENAMETOOLONG;
458
19
    sudo_warn("user");
459
19
    goto bad;
460
19
      }
461
31.8k
      free(ctx->user.name);
462
31.8k
      if ((ctx->user.name = strdup(p)) == NULL)
463
0
    goto oom;
464
31.8k
      continue;
465
31.8k
  }
466
133k
  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
133k
  if (MATCHES(*cur, "uid=")) {
476
32.8k
      p = *cur + sizeof("uid=") - 1;
477
32.8k
      ctx->user.uid = (uid_t) sudo_strtoid(p, &errstr);
478
32.8k
      if (errstr != NULL) {
479
53
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
480
53
    goto bad;
481
53
      }
482
32.7k
      continue;
483
32.8k
  }
484
100k
  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
100k
  if (MATCHES(*cur, "gid=")) {
494
38.0k
      p = *cur + sizeof("gid=") - 1;
495
38.0k
      ctx->user.gid = (gid_t) sudo_strtoid(p, &errstr);
496
38.0k
      if (errstr != NULL) {
497
61
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
498
61
    goto bad;
499
61
      }
500
37.9k
      continue;
501
38.0k
  }
502
62.3k
  if (MATCHES(*cur, "groups=")) {
503
1.30k
      CHECK(*cur, "groups=");
504
1.29k
      groups = *cur + sizeof("groups=") - 1;
505
1.29k
      continue;
506
1.30k
  }
507
61.0k
  if (MATCHES(*cur, "cwd=")) {
508
6.60k
      CHECK(*cur, "cwd=");
509
6.59k
      p = *cur + sizeof("cwd=") - 1;
510
      /* It is possible for the actual cwd to be larger than PATH_MAX. */
511
6.59k
      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
6.59k
      free(ctx->user.cwd);
516
6.59k
      if ((ctx->user.cwd = strdup(p)) == NULL)
517
0
    goto oom;
518
6.59k
      continue;
519
6.59k
  }
520
54.4k
  if (MATCHES(*cur, "tty=")) {
521
1.29k
      CHECK(*cur, "tty=");
522
1.28k
      p = *cur + sizeof("tty=") - 1;
523
1.28k
      if (strlen(p) >= PATH_MAX) {
524
5
    sudo_warnx(U_("path name for \"%s\" too long"), "tty");
525
5
    goto bad;
526
5
      }
527
1.28k
      free(ctx->user.ttypath);
528
1.28k
      if ((ctx->user.ttypath = strdup(p)) == NULL)
529
0
    goto oom;
530
1.28k
      ctx->user.tty = ctx->user.ttypath;
531
1.28k
      if (strncmp(ctx->user.tty, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
532
285
    ctx->user.tty += sizeof(_PATH_DEV) - 1;
533
1.28k
      continue;
534
1.28k
  }
535
53.1k
  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
53.1k
  if (MATCHES(*cur, "host=")) {
554
51.1k
      CHECK(*cur, "host=");
555
51.1k
      p = *cur + sizeof("host=") - 1;
556
51.1k
      if (strlen(p) >= host_name_max) {
557
29
    errno = ENAMETOOLONG;
558
29
    sudo_warnx("host");
559
29
    goto bad;
560
29
      }
561
51.0k
      host = p;
562
51.0k
      continue;
563
51.1k
  }
564
2.03k
  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
1.77k
  if (MATCHES(*cur, "cols=")) {
574
415
      p = *cur + sizeof("cols=") - 1;
575
415
      ctx->user.cols = (int)sudo_strtonum(p, 1, INT_MAX, &errstr);
576
415
      if (ctx->user.cols == 0) {
577
31
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
578
31
    goto bad;
579
31
      }
580
384
      continue;
581
415
  }
582
1.35k
  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
1.35k
  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
1.35k
  if (MATCHES(*cur, "sid=")) {
601
225
      p = *cur + sizeof("sid=") - 1;
602
225
      ctx->user.sid = (pid_t) sudo_strtoid(p, &errstr);
603
225
      if (errstr != NULL) {
604
9
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
605
9
    goto bad;
606
9
      }
607
216
      continue;
608
225
  }
609
1.13k
  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.13k
  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
177
    sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
623
177
    goto bad;
624
177
      }
625
535
      continue;
626
712
  }
627
1.13k
    }
628
629
    /* User name, user-ID, group-ID and host name must be specified. */
630
28.3k
    if (ctx->user.name == NULL) {
631
4.18k
  sudo_warnx("%s", U_("user name not set by sudo front-end"));
632
4.18k
  goto bad;
633
4.18k
    }
634
24.1k
    if (ctx->user.uid == (uid_t)-1) {
635
12
  sudo_warnx("%s", U_("user-ID not set by sudo front-end"));
636
12
  goto bad;
637
12
    }
638
24.1k
    if (ctx->user.gid == (gid_t)-1) {
639
52
  sudo_warnx("%s", U_("group-ID not set by sudo front-end"));
640
52
  goto bad;
641
52
    }
642
24.0k
    if (host == NULL) {
643
48
  sudo_warnx("%s", U_("host name not set by sudo front-end"));
644
48
  goto bad;
645
48
    }
646
647
24.0k
    if (!sudoers_sethost(ctx, host, remhost)) {
648
  /* sudoers_sethost() will print a warning on error. */
649
0
  goto bad;
650
0
    }
651
24.0k
    if (ctx->user.tty == NULL) {
652
23.4k
  if ((ctx->user.tty = strdup("unknown")) == NULL)
653
0
      goto oom;
654
  /* ctx->user.ttypath remains NULL */
655
23.4k
    }
656
657
24.0k
    ctx->user.pw = sudo_getpwnam(ctx->user.name);
658
24.0k
    if (ctx->user.pw != NULL && groups != NULL) {
659
  /* sudo_parse_gids() will print a warning on error. */
660
981
  GETGROUPS_T *gids;
661
981
  int ngids = sudo_parse_gids(groups, &ctx->user.gid, &gids);
662
981
  if (ngids == -1)
663
47
      goto bad;
664
665
  /* sudo_set_gidlist will adopt gids[] */
666
934
  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
934
    }
671
672
    /* ttydev is only set in user_info[] for API 1.22 and above. */
673
24.0k
    if (ctx->user.ttydev == NODEV && ctx->user.ttypath != NULL) {
674
574
  struct stat sb;
675
574
  if (stat(ctx->user.ttypath, &sb) == 0)
676
71
      ctx->user.ttydev = sb.st_rdev;
677
503
  else
678
503
      sudo_warn("%s", ctx->user.ttypath);
679
574
    }
680
681
    /* umask is only set in user_info[] for API 1.10 and above. */
682
24.0k
    if (ctx->user.umask == (mode_t)-1) {
683
23.8k
  ctx->user.umask = umask(0);
684
23.8k
  umask(ctx->user.umask);
685
23.8k
    }
686
687
    /* Always reset the environment for a login shell. */
688
24.0k
    if (ISSET(flags, MODE_LOGIN_SHELL))
689
432
  def_env_reset = true;
690
691
    /* Some systems support fexecve() which we use for digest matches. */
692
24.0k
    ctx->runas.execfd = -1;
693
694
    /* Create a UUID to store in the event log. */
695
24.0k
    sudo_uuid_create(uuid);
696
24.0k
    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
24.0k
    if (ISSET(ctx->settings.flags, HAVE_INTERCEPT_PTRACE)) {
706
51
  if (!append_default("intercept_type", "trace", -1, NULL, defaults))
707
0
      goto oom;
708
51
    }
709
24.0k
    if (ISSET(ctx->settings.flags, CAN_INTERCEPT_SETID)) {
710
61
  if (!append_default("intercept_allow_setid", NULL, -1, NULL, defaults))
711
0
      goto oom;
712
61
    }
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
795k
    for (cur = info->settings; *cur != NULL; cur++)
720
771k
  sudo_debug_printf(SUDO_DEBUG_INFO, "settings: %s", *cur);
721
187k
    for (cur = info->user_info; *cur != NULL; cur++)
722
163k
  sudo_debug_printf(SUDO_DEBUG_INFO, "user_info: %s", *cur);
723
724
24.0k
#undef MATCHES
725
24.0k
#undef INVALID
726
24.0k
#undef CHECK
727
24.0k
    debug_return_uint(flags);
728
729
0
oom:
730
0
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
731
5.54k
bad:
732
5.54k
    debug_return_uint(MODE_ERROR);
733
5.54k
}
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
14.7k
{
745
14.7k
    struct sudoers_exec_args *exec_args = v;
746
14.7k
    unsigned int info_len = 0;
747
14.7k
    debug_decl(sudoers_policy_store_result, SUDOERS_DEBUG_PLUGIN);
748
749
14.7k
    if (exec_args == NULL)
750
0
  debug_return_bool(true); /* nothing to do */
751
752
    /* Free old data, if any. */
753
14.7k
    if (command_info != NULL) {
754
6.40k
  char **cur;
755
6.40k
  sudoers_gc_remove(GC_VECTOR, command_info);
756
227k
  for (cur = command_info; *cur != NULL; cur++)
757
220k
      free(*cur);
758
6.40k
  free(command_info);
759
6.40k
    }
760
761
    /* Increase the length of command_info as needed, it is *not* checked. */
762
14.7k
    command_info = calloc(74, sizeof(char *));
763
14.7k
    if (command_info == NULL)
764
0
  goto oom;
765
766
14.7k
    if (ctx->runas.cmnd != NULL) {
767
14.0k
  command_info[info_len] = sudo_new_key_val("command", ctx->runas.cmnd);
768
14.0k
  if (command_info[info_len++] == NULL)
769
0
      goto oom;
770
14.0k
    }
771
14.7k
    if (def_log_subcmds) {
772
0
  if ((command_info[info_len++] = strdup("log_subcmds=true")) == NULL)
773
0
      goto oom;
774
0
    }
775
14.7k
    if (iolog_enabled) {
776
14.7k
  if (iolog_path)
777
3.30k
      command_info[info_len++] = iolog_path; /* now owned */
778
14.7k
  if (def_log_stdin) {
779
14.7k
      if ((command_info[info_len++] = strdup("iolog_stdin=true")) == NULL)
780
0
    goto oom;
781
14.7k
  }
782
14.7k
  if (def_log_stdout) {
783
14.7k
      if ((command_info[info_len++] = strdup("iolog_stdout=true")) == NULL)
784
0
    goto oom;
785
14.7k
  }
786
14.7k
  if (def_log_stderr) {
787
14.7k
      if ((command_info[info_len++] = strdup("iolog_stderr=true")) == NULL)
788
0
    goto oom;
789
14.7k
  }
790
14.7k
  if (def_log_ttyin) {
791
14.7k
      if ((command_info[info_len++] = strdup("iolog_ttyin=true")) == NULL)
792
0
    goto oom;
793
14.7k
  }
794
14.7k
  if (def_log_ttyout) {
795
14.7k
      if ((command_info[info_len++] = strdup("iolog_ttyout=true")) == NULL)
796
0
    goto oom;
797
14.7k
  }
798
14.7k
  if (def_compress_io) {
799
14.7k
      if ((command_info[info_len++] = strdup("iolog_compress=true")) == NULL)
800
0
    goto oom;
801
14.7k
  }
802
14.7k
  if (def_iolog_flush) {
803
14.7k
      if ((command_info[info_len++] = strdup("iolog_flush=true")) == NULL)
804
0
    goto oom;
805
14.7k
  }
806
14.7k
  if ((command_info[info_len++] = sudo_new_key_val("log_passwords",
807
14.7k
    def_log_passwords ? "true" : "false")) == NULL)
808
0
      goto oom;
809
14.7k
  if (!SLIST_EMPTY(&def_passprompt_regex)) {
810
14.7k
      char *passprompt_regex =
811
14.7k
    serialize_list("passprompt_regex", &def_passprompt_regex);
812
14.7k
      if (passprompt_regex == NULL)
813
0
    goto oom;
814
14.7k
      command_info[info_len++] = passprompt_regex;
815
14.7k
  }
816
14.7k
  if (def_maxseq != NULL) {
817
14.7k
      if ((command_info[info_len++] = sudo_new_key_val("maxseq", def_maxseq)) == NULL)
818
0
    goto oom;
819
14.7k
  }
820
14.7k
    }
821
14.7k
    if (ISSET(ctx->mode, MODE_EDIT)) {
822
1.55k
  if ((command_info[info_len++] = strdup("sudoedit=true")) == NULL)
823
0
      goto oom;
824
1.55k
  if (ctx->sudoedit_nfiles > 0) {
825
1.22k
      if (asprintf(&command_info[info_len++], "sudoedit_nfiles=%d",
826
1.22k
    ctx->sudoedit_nfiles) == -1)
827
0
    goto oom;
828
1.22k
  }
829
1.55k
  if (!def_sudoedit_checkdir) {
830
1.55k
      if ((command_info[info_len++] = strdup("sudoedit_checkdir=false")) == NULL)
831
0
    goto oom;
832
1.55k
  }
833
1.55k
  if (def_sudoedit_follow) {
834
1.55k
      if ((command_info[info_len++] = strdup("sudoedit_follow=true")) == NULL)
835
0
    goto oom;
836
1.55k
  }
837
1.55k
    }
838
14.7k
    if (def_runcwd && strcmp(def_runcwd, "*") != 0) {
839
  /* Set cwd to explicit value (sudoers or user-specified). */
840
14.7k
  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
14.7k
  if ((command_info[info_len++] = sudo_new_key_val("cwd", def_runcwd)) == NULL)
845
0
      goto oom;
846
14.7k
    } 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
14.7k
    if ((command_info[info_len++] = sudo_new_key_val("runas_user", ctx->runas.pw->pw_name)) == NULL)
854
0
  goto oom;
855
14.7k
    if (ctx->runas.gr != NULL) {
856
412
  if ((command_info[info_len++] = sudo_new_key_val("runas_group", ctx->runas.gr->gr_name)) == NULL)
857
0
      goto oom;
858
412
    }
859
14.7k
    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
14.7k
    } else {
874
14.7k
  if (asprintf(&command_info[info_len++], "runas_uid=%u",
875
14.7k
      (unsigned int)ctx->runas.pw->pw_uid) == -1)
876
0
      goto oom;
877
14.7k
  if (asprintf(&command_info[info_len++], "runas_gid=%u",
878
14.7k
      ctx->runas.gr ? (unsigned int)ctx->runas.gr->gr_gid :
879
14.7k
      (unsigned int)ctx->runas.pw->pw_gid) == -1)
880
0
      goto oom;
881
14.7k
    }
882
14.7k
    if (def_preserve_groups) {
883
43
  if ((command_info[info_len++] = strdup("preserve_groups=true")) == NULL)
884
0
      goto oom;
885
14.7k
    } else {
886
14.7k
  int i, len;
887
14.7k
  gid_t egid;
888
14.7k
  size_t glsize;
889
14.7k
  char *cp, *gid_list;
890
14.7k
  struct gid_list *gidlist;
891
892
  /* Only use results from a group db query, not the front end. */
893
14.7k
  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
14.7k
  glsize = sizeof("runas_groups=") - 1 +
898
14.7k
      (((size_t)gidlist->ngids + 1) * (STRLEN_MAX_UNSIGNED(gid_t) + 1));
899
14.7k
  gid_list = malloc(glsize);
900
14.7k
  if (gid_list == NULL) {
901
0
      sudo_gidlist_delref(gidlist);
902
0
      goto oom;
903
0
  }
904
14.7k
  memcpy(gid_list, "runas_groups=", sizeof("runas_groups=") - 1);
905
14.7k
  cp = gid_list + sizeof("runas_groups=") - 1;
906
14.7k
  glsize -= (size_t)(cp - gid_list);
907
908
  /* On BSD systems the effective gid is the first group in the list. */
909
14.7k
  egid = ctx->runas.gr ? (unsigned int)ctx->runas.gr->gr_gid :
910
14.7k
      (unsigned int)ctx->runas.pw->pw_gid;
911
14.7k
  len = snprintf(cp, glsize, "%u", (unsigned int)egid);
912
14.7k
  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
14.7k
  cp += len;
919
14.7k
  glsize -= (size_t)len;
920
29.4k
  for (i = 0; i < gidlist->ngids; i++) {
921
14.7k
      if (gidlist->gids[i] != egid) {
922
401
    len = snprintf(cp, glsize, ",%u",
923
401
         (unsigned int)gidlist->gids[i]);
924
401
    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
401
    cp += len;
931
401
    glsize -= (size_t)len;
932
401
      }
933
14.7k
  }
934
14.7k
  command_info[info_len++] = gid_list;
935
14.7k
  sudo_gidlist_delref(gidlist);
936
14.7k
    }
937
14.7k
    if (def_closefrom >= 0) {
938
14.7k
  if (asprintf(&command_info[info_len++], "closefrom=%d", def_closefrom) == -1)
939
0
      goto oom;
940
14.7k
    }
941
14.7k
    if (def_ignore_iolog_errors) {
942
14.7k
  if ((command_info[info_len++] = strdup("ignore_iolog_errors=true")) == NULL)
943
0
      goto oom;
944
14.7k
    }
945
14.7k
    if (def_intercept) {
946
0
  if ((command_info[info_len++] = strdup("intercept=true")) == NULL)
947
0
      goto oom;
948
0
    }
949
14.7k
    if (def_intercept_type == trace) {
950
19
  if ((command_info[info_len++] = strdup("use_ptrace=true")) == NULL)
951
0
      goto oom;
952
19
    }
953
14.7k
    if (def_intercept_verify) {
954
14.7k
  if ((command_info[info_len++] = strdup("intercept_verify=true")) == NULL)
955
0
      goto oom;
956
14.7k
    }
957
14.7k
    if (def_noexec) {
958
14.7k
  if ((command_info[info_len++] = strdup("noexec=true")) == NULL)
959
0
      goto oom;
960
14.7k
    }
961
14.7k
    if (def_exec_background) {
962
14.7k
  if ((command_info[info_len++] = strdup("exec_background=true")) == NULL)
963
0
      goto oom;
964
14.7k
    }
965
14.7k
    if (def_set_utmp) {
966
14.7k
  if ((command_info[info_len++] = strdup("set_utmp=true")) == NULL)
967
0
      goto oom;
968
14.7k
    }
969
14.7k
    if (def_use_pty) {
970
14.7k
  if ((command_info[info_len++] = strdup("use_pty=true")) == NULL)
971
0
      goto oom;
972
14.7k
    }
973
14.7k
    if (def_utmp_runas) {
974
14.7k
  if ((command_info[info_len++] = sudo_new_key_val("utmp_user", ctx->runas.pw->pw_name)) == NULL)
975
0
      goto oom;
976
14.7k
    }
977
14.7k
    if (def_iolog_mode != (S_IRUSR|S_IWUSR)) {
978
14.7k
  if (asprintf(&command_info[info_len++], "iolog_mode=0%o", (unsigned int)def_iolog_mode) == -1)
979
0
      goto oom;
980
14.7k
    }
981
14.7k
    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
14.7k
    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
14.7k
    if (!SLIST_EMPTY(&def_log_servers)) {
990
10.2k
  char *log_servers = serialize_list("log_servers", &def_log_servers);
991
10.2k
  if (log_servers == NULL)
992
0
      goto oom;
993
10.2k
  command_info[info_len++] = log_servers;
994
995
10.2k
  if (asprintf(&command_info[info_len++], "log_server_timeout=%u", def_log_server_timeout) == -1)
996
0
      goto oom;
997
10.2k
    }
998
999
14.7k
    if ((command_info[info_len++] = sudo_new_key_val("log_server_keepalive",
1000
14.7k
      def_log_server_keepalive ? "true" : "false")) == NULL)
1001
0
        goto oom;
1002
1003
14.7k
    if ((command_info[info_len++] = sudo_new_key_val("log_server_verify",
1004
14.7k
      def_log_server_verify ? "true" : "false")) == NULL)
1005
0
        goto oom;
1006
1007
14.7k
    if (def_log_server_cabundle != NULL) {
1008
10.2k
        if ((command_info[info_len++] = sudo_new_key_val("log_server_cabundle", def_log_server_cabundle)) == NULL)
1009
0
            goto oom;
1010
10.2k
    }
1011
14.7k
    if (def_log_server_peer_cert != NULL) {
1012
10.2k
        if ((command_info[info_len++] = sudo_new_key_val("log_server_peer_cert", def_log_server_peer_cert)) == NULL)
1013
0
            goto oom;
1014
10.2k
    }
1015
14.7k
    if (def_log_server_peer_key != NULL) {
1016
10.2k
        if ((command_info[info_len++] = sudo_new_key_val("log_server_peer_key", def_log_server_peer_key)) == NULL)
1017
0
            goto oom;
1018
10.2k
    }
1019
1020
14.7k
    if (def_command_timeout > 0 || ctx->user.timeout > 0) {
1021
413
  int timeout = ctx->user.timeout;
1022
413
    if (timeout == 0 || (def_command_timeout > 0 && def_command_timeout < timeout))
1023
0
      timeout = def_command_timeout;
1024
413
  if (asprintf(&command_info[info_len++], "timeout=%u", timeout) == -1)
1025
0
      goto oom;
1026
413
    }
1027
14.7k
    if (def_runchroot != NULL && strcmp(def_runchroot, "*") != 0) {
1028
14.7k
  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
14.7k
        if ((command_info[info_len++] = sudo_new_key_val("chroot", def_runchroot)) == NULL)
1033
0
            goto oom;
1034
14.7k
    }
1035
14.7k
    if (cmnd_umask != ACCESSPERMS) {
1036
6.61k
  if (asprintf(&command_info[info_len++], "umask=0%o", (unsigned int)cmnd_umask) == -1)
1037
0
      goto oom;
1038
6.61k
    }
1039
14.7k
    if (sudoers_override_umask()) {
1040
14.7k
  if ((command_info[info_len++] = strdup("umask_override=true")) == NULL)
1041
0
      goto oom;
1042
14.7k
    }
1043
14.7k
    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
14.7k
    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
14.7k
    if (def_rlimit_core != NULL) {
1058
14.7k
        if ((command_info[info_len++] = sudo_new_key_val("rlimit_core", def_rlimit_core)) == NULL)
1059
0
            goto oom;
1060
14.7k
    }
1061
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    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
14.7k
    if (accepted && !ISSET(ctx->mode, MODE_POLICY_INTERCEPTED)) {
1131
6.64k
  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
6.64k
    }
1136
1137
    /* Fill in exec environment info. */
1138
14.7k
    *(exec_args->argv) = argv;
1139
14.7k
    *(exec_args->envp) = envp;
1140
14.7k
    *(exec_args->info) = command_info;
1141
1142
    /* Free command_info on exit. */
1143
14.7k
    sudoers_gc_add(GC_VECTOR, command_info);
1144
1145
14.7k
    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
29.5k
{
1180
29.5k
    struct sudo_conf_debug_file_list debug_files = TAILQ_HEAD_INITIALIZER(debug_files);
1181
29.5k
    struct sudoers_open_info info;
1182
29.5k
    const char *cp, *plugin_path = NULL;
1183
29.5k
    char * const *cur;
1184
29.5k
    int ret;
1185
29.5k
    debug_decl(sudoers_policy_open, SUDOERS_DEBUG_PLUGIN);
1186
1187
29.5k
    sudo_version = version;
1188
29.5k
    sudo_conv = conversation;
1189
29.5k
    sudo_printf = plugin_printf;
1190
29.5k
    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
29.5k
    if (sudo_version < SUDO_API_MKVERSION(1, 2))
1195
0
  args = NULL;
1196
1197
    /* Initialize the debug subsystem.  */
1198
1.61M
    for (cur = settings; (cp = *cur) != NULL; cur++) {
1199
1.58M
  if (strncmp(cp, "debug_flags=", sizeof("debug_flags=") - 1) == 0) {
1200
344
      cp += sizeof("debug_flags=") - 1;
1201
344
      if (!sudoers_debug_parse_flags(&debug_files, cp))
1202
0
    debug_return_int(-1);
1203
344
      continue;
1204
344
  }
1205
1.58M
  if (strncmp(cp, "plugin_path=", sizeof("plugin_path=") - 1) == 0) {
1206
222
      plugin_path = cp + sizeof("plugin_path=") - 1;
1207
222
      continue;
1208
222
  }
1209
1.58M
    }
1210
29.5k
    if (!sudoers_debug_register(plugin_path, &debug_files))
1211
0
  debug_return_int(-1);
1212
1213
    /* Call the sudoers init function. */
1214
29.5k
    info.settings = settings;
1215
29.5k
    info.user_info = user_info;
1216
29.5k
    info.plugin_args = args;
1217
29.5k
    ret = sudoers_init(&info, log_parse_error, envp);
1218
1219
    /* The audit functions set audit_msg on failure. */
1220
29.5k
    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
29.5k
    debug_return_int(ret);
1226
29.5k
}
1227
1228
static void
1229
sudoers_policy_close(int exit_status, int error_code)
1230
29.5k
{
1231
29.5k
    const struct sudoers_context *ctx = sudoers_get_context();
1232
29.5k
    debug_decl(sudoers_policy_close, SUDOERS_DEBUG_PLUGIN);
1233
1234
29.5k
    if (session_opened) {
1235
  /* Close the session we opened in sudoers_policy_init_session(). */
1236
25.0k
  (void)sudo_auth_end_session();
1237
1238
25.0k
  if (error_code) {
1239
0
      errno = error_code;
1240
0
      sudo_warn(U_("unable to execute %s"), ctx->runas.cmnd);
1241
25.0k
  } else {
1242
25.0k
      log_exit_status(ctx, exit_status);
1243
25.0k
  }
1244
25.0k
    }
1245
1246
    /* Deregister the callback for sudo_fatal()/sudo_fatalx(). */
1247
29.5k
    sudo_fatal_callback_deregister(sudoers_cleanup);
1248
1249
    /* Free sudoers sources, ctx->user.and passwd/group caches. */
1250
29.5k
    sudoers_cleanup();
1251
1252
    /* command_info was freed by the g/c code. */
1253
29.5k
    command_info = NULL;
1254
1255
    /* Free error message passed back to front-end, if any. */
1256
29.5k
    free(audit_msg);
1257
29.5k
    audit_msg = NULL;
1258
1259
    /* sudoers_debug_deregister() calls sudo_debug_exit() for us. */
1260
29.5k
    sudoers_debug_deregister();
1261
29.5k
}
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
9.52k
{
1272
9.52k
    const struct sudoers_context *ctx = sudoers_get_context();
1273
9.52k
    int ret;
1274
9.52k
    debug_decl(sudoers_policy_init_session, SUDOERS_DEBUG_PLUGIN);
1275
1276
    /* user_env is only specified for API version 1.2 and higher. */
1277
9.52k
    if (sudo_version < SUDO_API_MKVERSION(1, 2))
1278
0
  user_env = NULL;
1279
1280
9.52k
    ret = sudo_auth_begin_session(ctx, pwd, user_env);
1281
1282
9.52k
    if (ret == 1) {
1283
9.52k
  session_opened = true;
1284
9.52k
    } 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
9.52k
    debug_return_int(ret);
1290
9.52k
}
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
16.6k
{
1297
16.6k
    const struct sudoers_context *ctx = sudoers_get_context();
1298
16.6k
    unsigned int valid_flags = RUN_VALID_FLAGS;
1299
16.6k
    unsigned int flags = MODE_RUN;
1300
16.6k
    struct sudoers_exec_args exec_args;
1301
16.6k
    int ret;
1302
16.6k
    debug_decl(sudoers_policy_check, SUDOERS_DEBUG_PLUGIN);
1303
1304
16.6k
    if (ISSET(ctx->mode, MODE_EDIT)) {
1305
1.86k
  valid_flags = EDIT_VALID_FLAGS;
1306
1.86k
  flags = 0;
1307
1.86k
    }
1308
16.6k
    if (!sudoers_set_mode(flags, valid_flags)) {
1309
106
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1310
106
      __func__, ctx->mode);
1311
106
  debug_return_int(-1);
1312
106
    }
1313
1314
16.5k
    exec_args.argv = argv_out;
1315
16.5k
    exec_args.envp = user_env_out;
1316
16.5k
    exec_args.info = command_infop;
1317
1318
16.5k
    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
16.5k
    if (ret != 1 && audit_msg != NULL) {
1330
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1331
0
      *errstr = audit_msg;
1332
0
    }
1333
16.5k
    debug_return_int(ret);
1334
16.5k
}
1335
1336
static int
1337
sudoers_policy_validate(const char **errstr)
1338
4.76k
{
1339
4.76k
    const struct sudoers_context *ctx = sudoers_get_context();
1340
4.76k
    int ret;
1341
4.76k
    debug_decl(sudoers_policy_validate, SUDOERS_DEBUG_PLUGIN);
1342
1343
4.76k
    if (!sudoers_set_mode(MODE_VALIDATE, VALIDATE_VALID_FLAGS)) {
1344
626
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1345
626
      __func__, ctx->mode);
1346
626
  debug_return_int(-1);
1347
626
    }
1348
1349
4.13k
    ret = sudoers_validate_user();
1350
1351
    /* The audit functions set audit_msg on failure. */
1352
4.13k
    if (ret != 1 && audit_msg != NULL) {
1353
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1354
0
      *errstr = audit_msg;
1355
0
    }
1356
4.13k
    debug_return_int(ret);
1357
4.13k
}
1358
1359
static void
1360
sudoers_policy_invalidate(int unlinkit)
1361
4.76k
{
1362
4.76k
    const struct sudoers_context *ctx = sudoers_get_context();
1363
4.76k
    debug_decl(sudoers_policy_invalidate, SUDOERS_DEBUG_PLUGIN);
1364
1365
4.76k
    if (!sudoers_set_mode(MODE_INVALIDATE, INVALIDATE_VALID_FLAGS)) {
1366
626
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1367
626
      __func__, ctx->mode);
1368
4.13k
    } else {
1369
4.13k
  timestamp_remove(ctx, unlinkit);
1370
4.13k
    }
1371
1372
4.76k
    debug_return;
1373
4.76k
}
1374
1375
static int
1376
sudoers_policy_list(int argc, char * const argv[], int verbose,
1377
    const char *list_user, const char **errstr)
1378
14.2k
{
1379
14.2k
    const struct sudoers_context *ctx = sudoers_get_context();
1380
14.2k
    int ret;
1381
14.2k
    debug_decl(sudoers_policy_list, SUDOERS_DEBUG_PLUGIN);
1382
1383
14.2k
    if (!sudoers_set_mode(argc ? MODE_CHECK : MODE_LIST, LIST_VALID_FLAGS)) {
1384
1.87k
  sudo_warnx(U_("%s: invalid mode flags from sudo front end: 0x%x"),
1385
1.87k
      __func__, ctx->mode);
1386
1.87k
  debug_return_int(-1);
1387
1.87k
    }
1388
1389
12.4k
    ret = sudoers_list(argc, argv, list_user, verbose);
1390
1391
    /* The audit functions set audit_msg on failure. */
1392
12.4k
    if (ret != 1 && audit_msg != NULL) {
1393
0
  if (sudo_version >= SUDO_API_MKVERSION(1, 15))
1394
0
      *errstr = audit_msg;
1395
0
    }
1396
12.4k
    debug_return_int(ret);
1397
12.4k
}
1398
1399
static int
1400
sudoers_policy_version(int verbose)
1401
2.38k
{
1402
#ifdef HAVE_LDAP
1403
    const struct sudoers_context *ctx = sudoers_get_context();
1404
#endif
1405
2.38k
    debug_decl(sudoers_policy_version, SUDOERS_DEBUG_PLUGIN);
1406
1407
2.38k
    sudo_printf(SUDO_CONV_INFO_MSG, _("Sudoers policy plugin version %s\n"),
1408
2.38k
  PACKAGE_VERSION);
1409
2.38k
    sudo_printf(SUDO_CONV_INFO_MSG, _("Sudoers file grammar version %d\n"),
1410
2.38k
  SUDOERS_GRAMMAR_VERSION);
1411
1412
2.38k
    if (verbose) {
1413
2.38k
  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.38k
  dump_auth_methods();
1424
2.38k
  dump_defaults();
1425
2.38k
  sudo_printf(SUDO_CONV_INFO_MSG, "\n");
1426
2.38k
  if (interfaces_string != NULL) {
1427
2.38k
      dump_interfaces(interfaces_string);
1428
2.38k
      sudo_printf(SUDO_CONV_INFO_MSG, "\n");
1429
2.38k
  }
1430
2.38k
    }
1431
2.38k
    debug_return_int(true);
1432
2.38k
}
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
8.11k
{
1449
8.11k
    struct sudo_hook *hook;
1450
1451
40.5k
    for (hook = sudoers_hooks; hook->hook_fn != NULL; hook++) {
1452
32.4k
  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
32.4k
    }
1459
8.11k
}
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
8.11k
{
1467
8.11k
    struct sudo_hook *hook;
1468
1469
40.5k
    for (hook = sudoers_hooks; hook->hook_fn != NULL; hook++) {
1470
32.4k
  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
32.4k
    }
1477
8.11k
}
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
};