2 * Copyright (c) 1993-1996, 1998-2005, 2007-2010
3 * Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
25 #include <pathnames.h>
36 #ifdef HAVE_MBR_CHECK_MEMBERSHIP
37 # include <membership.h>
41 * Info pertaining to the invoking user.
45 struct passwd *_runas_pw;
46 struct group *_runas_gr;
47 struct stat *cmnd_stat;
65 struct list_member *env_vars;
72 #ifdef HAVE_MBR_CHECK_MEMBERSHIP
77 /* Status passed between parent and child via socketpair */
78 struct command_status {
88 * Return values for sudoers_lookup(), also used as arguments for log_auth()
89 * Note: cannot use '0' as a value here.
91 /* XXX - VALIDATE_SUCCESS and VALIDATE_FAILURE instead? */
92 #define VALIDATE_ERROR 0x001
93 #define VALIDATE_OK 0x002
94 #define VALIDATE_NOT_OK 0x004
95 #define FLAG_CHECK_USER 0x010
96 #define FLAG_NO_USER 0x020
97 #define FLAG_NO_HOST 0x040
98 #define FLAG_NO_CHECK 0x080
101 * Pseudo-boolean values
109 * find_path()/load_cmnd() return values
113 #define NOT_FOUND_DOT -1
116 * Various modes sudo can be in (based on arguments) in hex
118 #define MODE_RUN 0x00000001
119 #define MODE_EDIT 0x00000002
120 #define MODE_VALIDATE 0x00000004
121 #define MODE_INVALIDATE 0x00000008
122 #define MODE_KILL 0x00000010
123 #define MODE_VERSION 0x00000020
124 #define MODE_HELP 0x00000040
125 #define MODE_LIST 0x00000080
126 #define MODE_CHECK 0x00000100
127 #define MODE_LISTDEFS 0x00000200
128 #define MODE_MASK 0x0000ffff
131 #define MODE_BACKGROUND 0x00010000
132 #define MODE_SHELL 0x00020000
133 #define MODE_LOGIN_SHELL 0x00040000
134 #define MODE_IMPLIED_SHELL 0x00080000
135 #define MODE_RESET_HOME 0x00100000
136 #define MODE_PRESERVE_GROUPS 0x00200000
137 #define MODE_PRESERVE_ENV 0x00400000
138 #define MODE_NONINTERACTIVE 0x00800000
141 * Used with set_perms()
143 #define PERM_ROOT 0x00
144 #define PERM_USER 0x01
145 #define PERM_FULL_USER 0x02
146 #define PERM_SUDOERS 0x03
147 #define PERM_RUNAS 0x04
148 #define PERM_FULL_RUNAS 0x05
149 #define PERM_TIMESTAMP 0x06
150 #define PERM_NOEXIT 0x10 /* flag */
151 #define PERM_MASK 0xf0
154 * Shortcuts for sudo_user contents.
156 #define user_name (sudo_user.pw->pw_name)
157 #define user_passwd (sudo_user.pw->pw_passwd)
158 #define user_uid (sudo_user.pw->pw_uid)
159 #define user_uuid (sudo_user.uuid)
160 #define user_gid (sudo_user.pw->pw_gid)
161 #define user_dir (sudo_user.pw->pw_dir)
162 #define user_shell (sudo_user.shell)
163 #define user_ngroups (sudo_user.ngroups)
164 #define user_groups (sudo_user.groups)
165 #define user_tty (sudo_user.tty)
166 #define user_ttypath (sudo_user.ttypath)
167 #define user_cwd (sudo_user.cwd)
168 #define user_cmnd (sudo_user.cmnd)
169 #define user_args (sudo_user.cmnd_args)
170 #define user_base (sudo_user.cmnd_base)
171 #define user_stat (sudo_user.cmnd_stat)
172 #define user_path (sudo_user.path)
173 #define user_prompt (sudo_user.prompt)
174 #define user_host (sudo_user.host)
175 #define user_shost (sudo_user.shost)
176 #define user_ccname (sudo_user.krb5_ccname)
177 #define user_display (sudo_user.display)
178 #define user_askpass (sudo_user.askpass)
179 #define safe_cmnd (sudo_user.cmnd_safe)
180 #define login_class (sudo_user.class_name)
181 #define runas_pw (sudo_user._runas_pw)
182 #define runas_gr (sudo_user._runas_gr)
183 #define user_role (sudo_user.role)
184 #define user_type (sudo_user.type)
187 * We used to use the system definition of PASS_MAX or _PASSWD_LEN,
188 * but that caused problems with various alternate authentication
189 * methods. So, we just define our own and assume that it is >= the
192 #define SUDO_PASS_MAX 256
195 * Flags for lock_file()
197 #define SUDO_LOCK 1 /* lock a file */
198 #define SUDO_TLOCK 2 /* test & lock a file (non-blocking) */
199 #define SUDO_UNLOCK 4 /* unlock a file */
202 * Flags for tgetpass()
204 #define TGP_ECHO 0x01 /* leave echo on when reading passwd */
205 #define TGP_STDIN 0x02 /* read from stdin, not /dev/tty */
206 #define TGP_ASKPASS 0x04 /* read from askpass helper program */
214 void aix_prep_user __P((char *, char *));
215 void aix_setauthdb __P((char *user));
216 void aix_restoreauthdb __P((void));
219 int get_boottime __P((struct timeval *));
222 int user_is_exempt __P((void));
223 void check_user __P((int, int));
224 void remove_timestamp __P((int));
227 char **env_get __P((void));
228 void env_init __P((int lazy));
229 void init_envtables __P((void));
230 void insert_env_vars __P((struct list_member *));
231 void read_env_file __P((const char *, int));
232 void rebuild_env __P((int));
233 void validate_env_vars __P((struct list_member *));
236 int sudo_execve __P((const char *path, char *argv[], char *envp[], uid_t uid,
237 struct command_status *cstat, int dowait, int bgmode));
238 void save_signals __P((void));
239 void restore_signals __P((void));
242 char *sudo_parseln __P((FILE *));
243 int lock_file __P((int, int));
244 int touch __P((int, char *, struct timeval *));
247 int find_path __P((char *, char **, struct stat *, char *, int));
250 char *sudo_getepw __P((const struct passwd *));
253 int gettime __P((struct timeval *));
256 char *sudo_goodpath __P((const char *, struct stat *));
259 int yyparse __P((void));
262 int io_log_open __P((void));
263 int log_stderr __P((const char *buf, unsigned int len));
264 int log_stdin __P((const char *buf, unsigned int len));
265 int log_stdout __P((const char *buf, unsigned int len));
266 int log_ttyin __P((const char *buf, unsigned int len));
267 int log_ttyout __P((const char *buf, unsigned int len));
268 void io_log_close __P((void));
269 void io_nextid __P((void));
272 int pam_begin_session __P((struct passwd *));
273 int pam_end_session __P((void));
276 int sudo_file_open __P((struct sudo_nss *));
277 int sudo_file_close __P((struct sudo_nss *));
278 int sudo_file_setdefs __P((struct sudo_nss *));
279 int sudo_file_lookup __P((struct sudo_nss *, int, int));
280 int sudo_file_parse __P((struct sudo_nss *));
281 int sudo_file_display_cmnd __P((struct sudo_nss *, struct passwd *));
282 int sudo_file_display_defaults __P((struct sudo_nss *, struct passwd *, struct lbuf *));
283 int sudo_file_display_bound_defaults __P((struct sudo_nss *, struct passwd *, struct lbuf *));
284 int sudo_file_display_privs __P((struct sudo_nss *, struct passwd *, struct lbuf *));
287 int parse_args __P((int, char **));
290 int get_pty __P((int *master, int *slave, char *name, size_t namesz, uid_t uid));
293 int user_in_group __P((struct passwd *, const char *));
294 struct group *sudo_fakegrnam __P((const char *));
295 struct group *sudo_getgrgid __P((gid_t));
296 struct group *sudo_getgrnam __P((const char *));
297 struct passwd *sudo_fakepwnam __P((const char *, gid_t));
298 struct passwd *sudo_getpwnam __P((const char *));
299 struct passwd *sudo_getpwuid __P((uid_t));
300 void sudo_endgrent __P((void));
301 void sudo_endpwent __P((void));
302 void sudo_endspent __P((void));
303 void sudo_setgrent __P((void));
304 void sudo_setpwent __P((void));
305 void sudo_setspent __P((void));
306 void gr_addref __P((struct group *));
307 void gr_delref __P((struct group *));
308 void pw_addref __P((struct passwd *));
309 void pw_delref __P((struct passwd *));
312 int selinux_restore_tty __P((void));
313 int selinux_setup __P((const char *role, const char *type, const char *ttyn,
315 void selinux_execve __P((const char *path, char *argv[], char *envp[]));
318 int set_perms __P((int));
321 FILE *open_sudoers __P((const char *, int, int *));
322 int exec_setup __P((int, const char *, int));
323 RETSIGTYPE cleanup __P((int));
324 void set_fqdn __P((void));
327 void verify_user __P((struct passwd *, char *));
328 void pass_warn __P((FILE *));
329 void dump_auth_methods __P((void));
332 void display_privs __P((struct sudo_nss_list *, struct passwd *));
333 int display_cmnd __P((struct sudo_nss_list *, struct passwd *));
336 int term_cbreak __P((int));
337 int term_copy __P((int, int));
338 int term_noecho __P((int));
339 int term_raw __P((int, int));
340 int term_restore __P((int, int));
343 char *tgetpass __P((const char *, int, int));
344 int tty_present __P((void));
347 char *get_timestr __P((time_t, int));
350 #define YY_DECL int yylex __P((void))
354 void zero_bytes __P((volatile void *, size_t));
356 /* Only provide extern declarations outside of sudo.c. */
358 extern struct sudo_user sudo_user;
359 extern struct passwd *list_pw;
361 extern int tgetpass_flags;
362 extern int long_list;
363 extern int sudo_mode;
364 extern uid_t timestamp_uid;
365 /* XXX - conflicts with the one in visudo */
366 int run_command __P((const char *path, char *argv[], char *envp[], uid_t uid, int dowait));
372 #endif /* _SUDO_SUDO_H */