summaryrefslogtreecommitdiffstats
path: root/plugins/sudoers/sudoers.h
blob: c7f386ba1b1231fcfc80b8783a58f1b0862e7fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 1993-1996, 1998-2005, 2007-2023
 *	Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Sponsored in part by the Defense Advanced Research Projects
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
 */

#ifndef SUDOERS_SUDOERS_H
#define SUDOERS_SUDOERS_H

#include <sys/types.h>		/* for gid_t, mode_t, pid_t, size_t, uid_t */
#include <limits.h>
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# include <compat/stdbool.h>
#endif /* HAVE_STDBOOL_H */

#define DEFAULT_TEXT_DOMAIN	"sudoers"

#include <pathnames.h>
#include <sudo_compat.h>
#include <sudo_conf.h>
#include <sudo_eventlog.h>
#include <sudo_fatal.h>
#include <sudo_gettext.h>
#include <sudo_nss.h>
#include <sudo_plugin.h>
#include <sudo_queue.h>
#include <sudo_util.h>
#include <sudoers_debug.h>

#include <defaults.h>
#include <logging.h>
#include <parse.h>
#include <pivot.h>

/*
 * Info passed in from the sudo front-end.
 */
struct sudoers_open_info {
    char * const *settings;
    char * const *user_info;
    char * const *plugin_args;
};

/*
 * Supplementary group IDs for a user.
 */
struct gid_list {
    int ngids;
    GETGROUPS_T *gids;
};

/*
 * Supplementary group names for a user.
 */
struct group_list {
    int ngroups;
    char **groups;
};

/*
 * Parse configuration settings.
 */
struct sudoers_parser_config {
    const char *sudoers_path;
    int strict;
    int verbose;
    bool recovery;
    bool ignore_perms;
    mode_t sudoers_mode;
    uid_t sudoers_uid;
    gid_t sudoers_gid;
};
#define SUDOERS_PARSER_CONFIG_INITIALIZER {				\
    .sudoers_path = NULL,						\
    .strict = false,							\
    .verbose = 1,							\
    .recovery = true,							\
    .ignore_perms = false,						\
    .sudoers_mode = SUDOERS_MODE,					\
    .sudoers_uid = SUDOERS_UID,						\
    .sudoers_gid = SUDOERS_GID						\
}

/*
 * Settings passed in from the sudo front-end.
 */
struct sudoers_plugin_settings {
    const char *plugin_dir;
    const char *ldap_conf;
    const char *ldap_secret;
    unsigned int flags;
};
#define SUDOERS_PLUGIN_SETTINGS_INITIALIZER {				\
    .plugin_dir = _PATH_SUDO_PLUGIN_DIR,				\
    .ldap_conf = _PATH_LDAP_CONF,					\
    .ldap_secret = _PATH_LDAP_SECRET					\
}

/*
 * Info pertaining to the invoking user.
 */
struct sudoers_user_context {
    struct passwd *pw;
    struct stat *cmnd_stat;
    char *cwd;
    char *name;
    char *path;
    char *tty;
    char *ttypath;
    char *host;
    char *shost;
    char *prompt;
    char *cmnd;
    char *cmnd_args;
    char *cmnd_base;
    char *cmnd_dir;
    char *cmnd_list;
    char *ccname;
    struct gid_list *gid_list;
    char * const * envp;
    char * const * env_add;
    int   closefrom;
    int   lines;
    int   cols;
    int   timeout;
    mode_t umask;
    uid_t euid;
    uid_t uid;
    uid_t egid;
    uid_t gid;
    pid_t pid;
    pid_t ppid;
    pid_t sid;
    pid_t tcpgid;
};

/*
 * Info pertaining to the runas user.
 */
struct sudoers_runas_context {
    int execfd;
    int argc;
    char **argv;
    char **argv_saved;
    struct passwd *pw;
    struct group *gr;
    struct passwd *list_pw;
    char *chroot;
    char *class;
    char *cmnd;
    char *cmnd_saved;
    char *cwd;
    char *group;
    char *host;
    char *shost;
    char *user;
#ifdef HAVE_SELINUX
    char *role;
    char *type;
#endif
#ifdef HAVE_APPARMOR
    char *apparmor_profile;
#endif
#ifdef HAVE_PRIV_SET
    char *privs;
    char *limitprivs;
#endif
};

/*
 * Global configuration for the sudoers module.
 */
struct sudoers_context {
    struct sudoers_parser_config parser_conf;
    struct sudoers_plugin_settings settings;
    struct sudoers_user_context user;
    struct sudoers_runas_context runas;
    struct timespec submit_time;
    char *source;
    char *iolog_file;
    char *iolog_dir;
    char *iolog_path;
    int sudoedit_nfiles;
    unsigned int mode;
    char uuid_str[37];
};
#define SUDOERS_CONTEXT_INITIALIZER {					\
    SUDOERS_PARSER_CONFIG_INITIALIZER,					\
    SUDOERS_PLUGIN_SETTINGS_INITIALIZER,				\
}

/*
 * sudo_get_gidlist() type values
 */
#define ENTRY_TYPE_ANY		0x00
#define ENTRY_TYPE_QUERIED	0x01
#define ENTRY_TYPE_FRONTEND	0x02

/*
 * sudoers_plugin_settings.flag values
 */
#define RUNAS_USER_SPECIFIED	0x01U
#define RUNAS_GROUP_SPECIFIED	0x02U
#define CAN_INTERCEPT_SETID	0x04U
#define HAVE_INTERCEPT_PTRACE	0x08U
#define USER_INTERCEPT_SETID	0x10U

/*
 * Return values for sudoers_lookup(), also used as arguments for log_auth()
 * Note: cannot use '0' as a value here.
 */
#define VALIDATE_ERROR		0x001U
#define VALIDATE_SUCCESS	0x002U
#define VALIDATE_FAILURE	0x004U
#define FLAG_CHECK_USER		0x010U
#define FLAG_NO_USER		0x020U
#define FLAG_NO_HOST		0x040U
#define FLAG_NO_CHECK		0x080U
#define FLAG_NO_USER_INPUT	0x100U
#define FLAG_BAD_PASSWORD	0x200U
#define FLAG_INTERCEPT_SETID	0x400U

/*
 * Return values for check_user() (rowhammer resistant).
 */
#undef AUTH_SUCCESS
#define AUTH_SUCCESS		0x52a2925	/* 0101001010100010100100100101 */
#undef AUTH_FAILURE
#define AUTH_FAILURE		0xad5d6da	/* 1010110101011101011011011010 */
#undef AUTH_ERROR
#define AUTH_ERROR		0x1fc8d3ac	/* 11111110010001101001110101100 */

/*
 * find_path()/set_cmnd() return values
 */
#define FOUND			0
#define NOT_FOUND		1
#define NOT_FOUND_DOT		2
#define NOT_FOUND_ERROR		3
#define NOT_FOUND_PATH		4

/*
 * Various modes sudo can be in (based on arguments) in hex
 */
#define MODE_RUN		0x00000001U
#define MODE_EDIT		0x00000002U
#define MODE_VALIDATE		0x00000004U
#define MODE_INVALIDATE		0x00000008U
#define MODE_VERSION		0x00000010U
#define MODE_HELP		0x00000020U
#define MODE_LIST		0x00000040U
#define MODE_CHECK		0x00000080U
#define MODE_ERROR		0x00000100U
#define MODE_MASK		0x0000ffffU

/* Mode flags */
#define MODE_ASKPASS		0x00010000U
#define MODE_SHELL		0x00020000U
#define MODE_LOGIN_SHELL	0x00040000U
#define MODE_IMPLIED_SHELL	0x00080000U
#define MODE_RESET_HOME		0x00100000U
#define MODE_PRESERVE_GROUPS	0x00200000U
#define MODE_PRESERVE_ENV	0x00400000U
#define MODE_NONINTERACTIVE	0x00800000U
#define MODE_IGNORE_TICKET	0x01000000U
#define MODE_UPDATE_TICKET	0x02000000U
#define MODE_POLICY_INTERCEPTED	0x04000000U

/* Mode bits allowed for intercepted commands. */
#define MODE_INTERCEPT_MASK	(MODE_RUN|MODE_NONINTERACTIVE|MODE_IGNORE_TICKET|MODE_POLICY_INTERCEPTED)

/*
 * Used with set_perms()
 */
#define PERM_INITIAL		0x00
#define PERM_ROOT		0x01
#define PERM_USER		0x02
#define PERM_FULL_USER		0x03
#define PERM_SUDOERS		0x04
#define PERM_RUNAS		0x05
#define PERM_TIMESTAMP		0x06
#define PERM_IOLOG		0x07

/* Default sudoers uid/gid/mode if not set by the Makefile. */
#ifndef SUDOERS_UID
# define SUDOERS_UID	0
#endif
#ifndef SUDOERS_GID
# define SUDOERS_GID	0
#endif
#ifndef SUDOERS_MODE
# define SUDOERS_MODE	0600
#endif

struct sudo_lbuf;
struct passwd;
struct stat;
struct timespec;

/*
 * Function prototypes
 */
/* goodpath.c */
bool sudo_goodpath(const char *path, struct stat *sbp);

/* findpath.c */
int find_path(const char *infile, char **outfile, struct stat *sbp,
    const char *path, bool ignore_dot, char * const *allowlist);

/* resolve_cmnd.c */
int resolve_cmnd(struct sudoers_context *ctx, const char *infile,
    char **outfile, const char *path);

/* check.c */
int check_user(struct sudoers_context *ctx, unsigned int validated, unsigned int mode);
bool user_is_exempt(const struct sudoers_context *ctx);

/* check_util.c */
int check_user_runchroot(const char *runchroot);
int check_user_runcwd(const char *runcwd);

/* prompt.c */
char *expand_prompt(const struct sudoers_context *ctx, const char *restrict old_prompt, const char *restrict auth_user);

/* sudo_auth.c */
bool sudo_auth_needs_end_session(void);
int verify_user(const struct sudoers_context *ctx, struct passwd *pw, char *prompt, unsigned int validated, struct sudo_conv_callback *callback);
int sudo_auth_begin_session(const struct sudoers_context *ctx, struct passwd *pw, char **user_env[]);
int sudo_auth_end_session(void);
int sudo_auth_init(const struct sudoers_context *ctx, struct passwd *pw, unsigned int mode);
int sudo_auth_approval(const struct sudoers_context *ctx, struct passwd *pw, unsigned int validated, bool exempt);
int sudo_auth_cleanup(const struct sudoers_context *ctx, struct passwd *pw, bool force);

/* set_perms.c */
bool rewind_perms(void);
bool set_perms(const struct sudoers_context *, int);
bool restore_perms(void);
int pam_prep_user(struct passwd *);

/* defaults.c */
void dump_defaults(void);
void dump_auth_methods(void);

/* getspwuid.c */
char *sudo_getepw(const struct passwd *);

/* pwutil.c */
typedef struct cache_item * (*sudo_make_pwitem_t)(uid_t uid, const char *user);
typedef struct cache_item * (*sudo_make_gritem_t)(gid_t gid, const char *group);
typedef struct cache_item * (*sudo_make_gidlist_item_t)(const struct passwd *pw, int ngids, GETGROUPS_T *gids, char * const *gidstrs, unsigned int type);
typedef struct cache_item * (*sudo_make_grlist_item_t)(const struct passwd *pw, char * const *groups);
typedef bool (*sudo_valid_shell_t)(const char *shell);
sudo_dso_public struct group *sudo_getgrgid(gid_t);
sudo_dso_public struct group *sudo_getgrnam(const char *);
sudo_dso_public void sudo_gr_addref(struct group *);
sudo_dso_public void sudo_gr_delref(struct group *);
bool user_in_group(const struct passwd *, const char *);
struct group *sudo_fakegrnam(const char *);
struct group *sudo_mkgrent(const char *group, gid_t gid, ...);
struct gid_list *sudo_get_gidlist(const struct passwd *pw, unsigned int type);
struct group_list *sudo_get_grlist(const struct passwd *pw);
struct passwd *sudo_fakepwnam(const char *, gid_t);
struct passwd *sudo_mkpwent(const char *user, uid_t uid, gid_t gid, const char *home, const char *shell);
struct passwd *sudo_getpwnam(const char *);
struct passwd *sudo_getpwuid(uid_t);
void sudo_endspent(void);
void sudo_freegrcache(void);
void sudo_freepwcache(void);
void sudo_gidlist_addref(struct gid_list *);
void sudo_gidlist_delref(struct gid_list *);
void sudo_grlist_addref(struct group_list *);
void sudo_grlist_delref(struct group_list *);
void sudo_pw_addref(struct passwd *);
void sudo_pw_delref(struct passwd *);
int  sudo_set_gidlist(struct passwd *pw, int ngids, GETGROUPS_T *gids, char * const *gidstrs, unsigned int type);
int  sudo_set_grlist(struct passwd *pw, char * const *groups);
int  sudo_pwutil_get_max_groups(void);
void sudo_pwutil_set_max_groups(int);
void sudo_pwutil_set_backend(sudo_make_pwitem_t, sudo_make_gritem_t, sudo_make_gidlist_item_t, sudo_make_grlist_item_t, sudo_valid_shell_t);
void sudo_setspent(void);
bool user_shell_valid(const struct passwd *pw);

/* timestr.c */
char *get_timestr(time_t, int);

/* boottime.c */
bool get_boottime(struct timespec *);

/* iolog.c */
bool cb_maxseq(struct sudoers_context *ctx, const char *file, int line, int column, const union sudo_defs_val *sd_un, int op);
bool cb_iolog_user(struct sudoers_context *ctx, const char *file, int line, int column, const union sudo_defs_val *sd_un, int op);
bool cb_iolog_group(struct sudoers_context *ctx, const char *file, int line, int column, const union sudo_defs_val *sd_un, int op);
bool cb_iolog_mode(struct sudoers_context *ctx, const char *file, int line, int column, const union sudo_defs_val *sd_un, int op);

/* iolog_path_escapes.c */
struct iolog_path_escape;
extern const struct iolog_path_escape *sudoers_iolog_path_escapes;

/* env.c */
char **env_get(void);
bool env_merge(const struct sudoers_context *ctx, char * const envp[]);
bool env_swap_old(void);
void env_free(void);
bool env_init(char * const envp[]);
bool init_envtables(void);
bool insert_env_vars(char * const envp[]);
bool read_env_file(const struct sudoers_context *ctx, const char *path, bool overwrite, bool restricted);
bool rebuild_env(const struct sudoers_context *ctx);
bool validate_env_vars(const struct sudoers_context *ctx, char * const envp[]);
int sudo_setenv(const char *var, const char *val, int overwrite);
int sudo_unsetenv(const char *var);
char *sudo_getenv(const char *name);
char *sudo_getenv_nodebug(const char *name);
int sudo_putenv_nodebug(char *str, bool dupcheck, bool overwrite);
int sudo_unsetenv_nodebug(const char *var);
int sudoers_hook_getenv(const char *name, char **value, void *closure);
int sudoers_hook_putenv(char *string, void *closure);
int sudoers_hook_setenv(const char *name, const char *value, int overwrite, void *closure);
int sudoers_hook_unsetenv(const char *name, void *closure);
void register_env_file(void * (*ef_open)(const char *), void (*ef_close)(void *), char * (*ef_next)(void *, int *), bool sys);

/* env_pattern.c */
bool matches_env_pattern(const char *pattern, const char *var, bool *full_match);

/* sudoers_cb.c */
void set_callbacks(void);
bool cb_log_input(struct sudoers_context *ctx, const char *file, int line, int column, const union sudo_defs_val *sd_un, int op);
bool cb_log_output(struct sudoers_context *ctx, const char *file, int line, int column, const union sudo_defs_val *sd_un, int op);

/* sudoers.c */
FILE *open_sudoers(const char *, char **, bool, bool *);
bool cb_runas_default(struct sudoers_context *ctx, const char *file, int line, int column, const union sudo_defs_val *sd_un, int op);
int set_cmnd_path(struct sudoers_context *ctx, const char *runchroot);
void set_cmnd_status(struct sudoers_context *ctx, const char *runchroot);
int sudoers_init(void *info, sudoers_logger_t logger, char * const envp[]);
int sudoers_check_cmnd(int argc, char *const argv[], char *env_add[], void *closure);
int sudoers_list(int argc, char *const argv[], const char *list_user, int verbose);
int sudoers_validate_user(void);
void sudoers_cleanup(void);
bool sudoers_override_umask(void);
const struct sudoers_context *sudoers_get_context(void);
bool sudoers_set_mode(unsigned int flags, unsigned int mask);

/* sudoers_ctx_free.c */
void sudoers_ctx_free(struct sudoers_context *ctx);

/* policy.c */
unsigned int sudoers_policy_deserialize_info(struct sudoers_context *ctx, void *v, struct defaults_list *defaults);
bool sudoers_policy_store_result(struct sudoers_context *ctx, bool accepted, char *argv[], char *envp[], mode_t cmnd_umask, char *iolog_path, void *v);
bool sudoers_tty_present(struct sudoers_context *ctx);
extern sudo_conv_t sudo_conv;
extern sudo_printf_t sudo_printf;
extern struct sudo_plugin_event * (*plugin_event_alloc)(void);

/* group_plugin.c */
void group_plugin_unload(void);
int group_plugin_query(const char *user, const char *group,
    const struct passwd *pwd);
bool cb_group_plugin(struct sudoers_context *ctx, const char *file, int line, int column, const union sudo_defs_val *sd_un, int op);

/* editor.c */
char *find_editor(int nfiles, char * const *files, int *argc_out,
    char ***argv_out, char * const *allowlist, const char **env_editor);

/* exptilde.c */
bool expand_tilde(char **path, const char *user);

/* gc.c */
enum sudoers_gc_types {
    GC_UNKNOWN,
    GC_VECTOR,
    GC_PTR
};
bool sudoers_gc_add(enum sudoers_gc_types type, void *ptr);
bool sudoers_gc_remove(enum sudoers_gc_types type, void *ptr);
void sudoers_gc_init(void);
void sudoers_gc_run(void);

/* canon_path.c */
char *canon_path(const char *inpath);
void canon_path_free(char *resolved);
void canon_path_free_cache(void);

/* strlcpy_unesc.c */
size_t strlcpy_unescape(char *restrict dst, const char *restrict src, size_t size);

/* strvec_join.c */
char *strvec_join(char *const argv[], char sep, size_t (*cpy)(char *, const char *, size_t));

/* unesc_str.c */
void unescape_string(char *str);

/* serialize_list.c */
char *serialize_list(const char *varname, struct list_members *members);

/* sethost.c */
bool sudoers_sethost(struct sudoers_context *ctx, const char *host, const char *remhost);

#endif /* SUDOERS_SUDOERS_H */