e69a9779d014daf51f3bf2afe7ab3f8fead0de5e
[debian/sudo] / plugins / sudoers / sudoers.h
1 /*
2  * Copyright (c) 1993-1996, 1998-2005, 2007-2011
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * Sponsored in part by the Defense Advanced Research Projects
18  * Agency (DARPA) and Air Force Research Laboratory, Air Force
19  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
20  */
21
22 #ifndef _SUDO_SUDOERS_H
23 #define _SUDO_SUDOERS_H
24
25 #include <limits.h>
26 #ifdef HAVE_STDBOOL_H
27 # include <stdbool.h>
28 #else
29 # include "compat/stdbool.h"
30 #endif /* HAVE_STDBOOL_H */
31
32 #include <pathnames.h>
33 #include "missing.h"
34 #include "error.h"
35 #include "alloc.h"
36 #include "list.h"
37 #include "fileops.h"
38 #include "defaults.h"
39 #include "logging.h"
40 #include "sudo_nss.h"
41 #include "sudo_plugin.h"
42 #include "sudo_debug.h"
43
44 #define DEFAULT_TEXT_DOMAIN     "sudoers"
45 #include "gettext.h"
46
47 /*
48  * Password db and supplementary group IDs with associated group names.
49  */
50 struct group_list {
51     char **groups;
52     GETGROUPS_T *gids;
53     int ngroups;
54     int ngids;
55 };
56
57 /*
58  * Info pertaining to the invoking user.
59  */
60 struct sudo_user {
61     struct passwd *pw;
62     struct passwd *_runas_pw;
63     struct group *_runas_gr;
64     struct stat *cmnd_stat;
65     char *name;
66     char *path;
67     char *tty;
68     char *ttypath;
69     char *host;
70     char *shost;
71     char *prompt;
72     char *cmnd;
73     char *cmnd_args;
74     char *cmnd_base;
75     char *cmnd_safe;
76     char *class_name;
77     char *krb5_ccname;
78     struct group_list *group_list;
79     char * const * env_vars;
80 #ifdef HAVE_SELINUX
81     char *role;
82     char *type;
83 #endif
84 #ifdef HAVE_PRIV_SET
85     char *privs;
86     char *limitprivs;
87 #endif
88     char *cwd;
89     char *iolog_file;
90     GETGROUPS_T *gids;
91     int   ngids;
92     int   closefrom;
93     int   lines;
94     int   cols;
95     int   flags;
96     uid_t uid;
97     uid_t gid;
98     pid_t sid;
99 };
100
101 /*
102  * sudo_user flag values
103  */
104 #define RUNAS_USER_SPECIFIED    0x01
105 #define RUNAS_GROUP_SPECIFIED   0x02
106
107 /*
108  * Return values for sudoers_lookup(), also used as arguments for log_auth()
109  * Note: cannot use '0' as a value here.
110  */
111 /* XXX - VALIDATE_SUCCESS and VALIDATE_FAILURE instead? */
112 #define VALIDATE_ERROR          0x001
113 #define VALIDATE_OK             0x002
114 #define VALIDATE_NOT_OK         0x004
115 #define FLAG_CHECK_USER         0x010
116 #define FLAG_NO_USER            0x020
117 #define FLAG_NO_HOST            0x040
118 #define FLAG_NO_CHECK           0x080
119 #define FLAG_NON_INTERACTIVE    0x100
120 #define FLAG_BAD_PASSWORD       0x200
121 #define FLAG_AUTH_ERROR         0x400
122
123 /*
124  * find_path()/load_cmnd() return values
125  */
126 #define FOUND                   0
127 #define NOT_FOUND               1
128 #define NOT_FOUND_DOT           2
129
130 /*
131  * Various modes sudo can be in (based on arguments) in hex
132  */
133 #define MODE_RUN                0x00000001
134 #define MODE_EDIT               0x00000002
135 #define MODE_VALIDATE           0x00000004
136 #define MODE_INVALIDATE         0x00000008
137 #define MODE_KILL               0x00000010
138 #define MODE_VERSION            0x00000020
139 #define MODE_HELP               0x00000040
140 #define MODE_LIST               0x00000080
141 #define MODE_CHECK              0x00000100
142 #define MODE_LISTDEFS           0x00000200
143 #define MODE_MASK               0x0000ffff
144
145 /* Mode flags */
146 #define MODE_BACKGROUND         0x00010000 /* XXX - unused */
147 #define MODE_SHELL              0x00020000
148 #define MODE_LOGIN_SHELL        0x00040000
149 #define MODE_IMPLIED_SHELL      0x00080000
150 #define MODE_RESET_HOME         0x00100000
151 #define MODE_PRESERVE_GROUPS    0x00200000
152 #define MODE_PRESERVE_ENV       0x00400000
153 #define MODE_NONINTERACTIVE     0x00800000
154 #define MODE_IGNORE_TICKET      0x01000000
155
156 /*
157  * Used with set_perms()
158  */
159 #define PERM_INITIAL             0x00
160 #define PERM_ROOT                0x01
161 #define PERM_USER                0x02
162 #define PERM_FULL_USER           0x03
163 #define PERM_SUDOERS             0x04
164 #define PERM_RUNAS               0x05
165 #define PERM_TIMESTAMP           0x06
166 #define PERM_NOEXIT              0x10 /* flag */
167 #define PERM_MASK                0xf0
168
169 /*
170  * Shortcuts for sudo_user contents.
171  */
172 #define user_name               (sudo_user.name)
173 #define user_uid                (sudo_user.uid)
174 #define user_gid                (sudo_user.gid)
175 #define user_sid                (sudo_user.sid)
176 #define user_passwd             (sudo_user.pw->pw_passwd)
177 #define user_dir                (sudo_user.pw->pw_dir)
178 #define user_gids               (sudo_user.gids)
179 #define user_ngids              (sudo_user.ngids)
180 #define user_group_list         (sudo_user.group_list)
181 #define user_tty                (sudo_user.tty)
182 #define user_ttypath            (sudo_user.ttypath)
183 #define user_cwd                (sudo_user.cwd)
184 #define user_cmnd               (sudo_user.cmnd)
185 #define user_args               (sudo_user.cmnd_args)
186 #define user_base               (sudo_user.cmnd_base)
187 #define user_stat               (sudo_user.cmnd_stat)
188 #define user_path               (sudo_user.path)
189 #define user_prompt             (sudo_user.prompt)
190 #define user_host               (sudo_user.host)
191 #define user_shost              (sudo_user.shost)
192 #define user_ccname             (sudo_user.krb5_ccname)
193 #define safe_cmnd               (sudo_user.cmnd_safe)
194 #define login_class             (sudo_user.class_name)
195 #define runas_pw                (sudo_user._runas_pw)
196 #define runas_gr                (sudo_user._runas_gr)
197 #define user_role               (sudo_user.role)
198 #define user_type               (sudo_user.type)
199 #define user_closefrom          (sudo_user.closefrom)
200 #define runas_privs             (sudo_user.privs)
201 #define runas_limitprivs        (sudo_user.limitprivs)
202
203 #ifdef __TANDEM
204 # define ROOT_UID       65535
205 #else
206 # define ROOT_UID       0
207 #endif
208
209 /*
210  * We used to use the system definition of PASS_MAX or _PASSWD_LEN,
211  * but that caused problems with various alternate authentication
212  * methods.  So, we just define our own and assume that it is >= the
213  * system max.
214  */
215 #define SUDO_PASS_MAX   256
216
217 struct lbuf;
218 struct passwd;
219 struct stat;
220 struct timeval;
221
222 /*
223  * Function prototypes
224  */
225 #define YY_DECL int yylex(void)
226
227 /* goodpath.c */
228 bool sudo_goodpath(const char *, struct stat *);
229
230 /* findpath.c */
231 int find_path(char *, char **, struct stat *, char *, int);
232
233 /* check.c */
234 int check_user(int, int);
235 void remove_timestamp(bool);
236 bool user_is_exempt(void);
237
238 /* sudo_auth.c */
239 int verify_user(struct passwd *pw, char *prompt, int validated);
240 int sudo_auth_begin_session(struct passwd *pw, char **user_env[]);
241 int sudo_auth_end_session(struct passwd *pw);
242 int sudo_auth_init(struct passwd *pw);
243 int sudo_auth_cleanup(struct passwd *pw);
244
245 /* parse.c */
246 int sudo_file_open(struct sudo_nss *);
247 int sudo_file_close(struct sudo_nss *);
248 int sudo_file_setdefs(struct sudo_nss *);
249 int sudo_file_lookup(struct sudo_nss *, int, int);
250 int sudo_file_parse(struct sudo_nss *);
251 int sudo_file_display_cmnd(struct sudo_nss *, struct passwd *);
252 int sudo_file_display_defaults(struct sudo_nss *, struct passwd *, struct lbuf *);
253 int sudo_file_display_bound_defaults(struct sudo_nss *, struct passwd *, struct lbuf *);
254 int sudo_file_display_privs(struct sudo_nss *, struct passwd *, struct lbuf *);
255
256 /* set_perms.c */
257 void rewind_perms(void);
258 int set_perms(int);
259 void restore_perms(void);
260 int pam_prep_user(struct passwd *);
261
262 /* gram.y */
263 int yyparse(void);
264
265 /* toke.l */
266 YY_DECL;
267 extern const char *sudoers_file;
268 extern mode_t sudoers_mode;
269 extern uid_t sudoers_uid;
270 extern gid_t sudoers_gid;
271
272 /* defaults.c */
273 void dump_defaults(void);
274 void dump_auth_methods(void);
275
276 /* getspwuid.c */
277 char *sudo_getepw(const struct passwd *);
278
279 /* zero_bytes.c */
280 void zero_bytes(volatile void *, size_t);
281
282 /* sudo_nss.c */
283 void display_privs(struct sudo_nss_list *, struct passwd *);
284 bool display_cmnd(struct sudo_nss_list *, struct passwd *);
285
286 /* pwutil.c */
287 __dso_public struct group *sudo_getgrgid(gid_t);
288 __dso_public struct group *sudo_getgrnam(const char *);
289 __dso_public void sudo_gr_addref(struct group *);
290 __dso_public void sudo_gr_delref(struct group *);
291 bool user_in_group(struct passwd *, const char *);
292 struct group *sudo_fakegrnam(const char *);
293 struct group_list *sudo_get_grlist(struct passwd *pw);
294 struct passwd *sudo_fakepwnam(const char *, gid_t);
295 struct passwd *sudo_fakepwnamid(const char *user, uid_t uid, gid_t gid);
296 struct passwd *sudo_getpwnam(const char *);
297 struct passwd *sudo_getpwuid(uid_t);
298 void sudo_endgrent(void);
299 void sudo_endpwent(void);
300 void sudo_endspent(void);
301 void sudo_grlist_addref(struct group_list *);
302 void sudo_grlist_delref(struct group_list *);
303 void sudo_pw_addref(struct passwd *);
304 void sudo_pw_delref(struct passwd *);
305 void sudo_setgrent(void);
306 void sudo_setpwent(void);
307 void sudo_setspent(void);
308
309 /* timestr.c */
310 char *get_timestr(time_t, int);
311
312 /* atobool.c */
313 int atobool(const char *str);
314
315 /* boottime.c */
316 int get_boottime(struct timeval *);
317
318 /* iolog.c */
319 void io_nextid(char *iolog_dir, char *iolog_dir_fallback, char sessid[7]);
320
321 /* iolog_path.c */
322 char *expand_iolog_path(const char *prefix, const char *dir, const char *file,
323     char **slashp);
324
325 /* env.c */
326 char **env_get(void);
327 void env_merge(char * const envp[], bool overwrite);
328 void env_init(char * const envp[]);
329 void init_envtables(void);
330 void insert_env_vars(char * const envp[]);
331 void read_env_file(const char *, int);
332 void rebuild_env(void);
333 void validate_env_vars(char * const envp[]);
334 int sudo_setenv(const char *var, const char *val, int overwrite);
335 int sudo_unsetenv(const char *var);
336 char *sudo_getenv(const char *name);
337 int sudoers_hook_getenv(const char *name, char **value, void *closure);
338 int sudoers_hook_putenv(char *string, void *closure);
339 int sudoers_hook_setenv(const char *name, const char *value, int overwrite, void *closure);
340 int sudoers_hook_unsetenv(const char *name, void *closure);
341
342 /* fmt_string.c */
343 char *fmt_string(const char *, const char *);
344
345 /* sudoers.c */
346 void plugin_cleanup(int);
347 void set_fqdn(void);
348 FILE *open_sudoers(const char *, bool, bool *);
349
350 /* aix.c */
351 void aix_restoreauthdb(void);
352 void aix_setauthdb(char *user);
353
354 /* group_plugin.c */
355 int group_plugin_load(char *plugin_info);
356 void group_plugin_unload(void);
357 int group_plugin_query(const char *user, const char *group,
358     const struct passwd *pwd);
359
360 /* setgroups.c */
361 int sudo_setgroups(int ngids, const GETGROUPS_T *gids);
362
363 #ifndef _SUDO_MAIN
364 extern struct sudo_user sudo_user;
365 extern struct passwd *list_pw;
366 extern int long_list;
367 extern int sudo_mode;
368 extern uid_t timestamp_uid;
369 extern sudo_conv_t sudo_conv;
370 extern sudo_printf_t sudo_printf;
371 #endif
372
373 #endif /* _SUDO_SUDOERS_H */