(void) sudo_setenv("HOME", runas_homedir);
sudo_setenv("HOME", runas_homedir);
main(argc, argv)    int argc;    char **argv;{    int validated;    int fd;    int cmnd_status;    int sudo_mode;#ifdef POSIX_SIGNALS    sigset_t set, oset;#else    int omask;#endif /* POSIX_SIGNALS */    extern char **environ;    extern int printmatches;    /* Must be done as the first thing... */#if defined(HAVE_GETPRPWNAM) && defined(HAVE_SET_AUTH_PARAMETERS)    (void) set_auth_parameters(argc, argv);# ifdef HAVE_INITPRIVS    initprivs();# endif#endif /* HAVE_GETPRPWNAM && HAVE_SET_AUTH_PARAMETERS */    Argv = argv;    Argc = argc;    if (geteuid() != 0) {	(void) fprintf(stderr, "Sorry, %s must be setuid root.\n", Argv[0]);	exit(1);    }    /*     * Block signals so the user cannot interrupt us at some point and     * avoid the logging.     */#ifdef POSIX_SIGNALS    (void) sigemptyset(&set);    (void) sigaddset(&set, SIGINT);    (void) sigaddset(&set, SIGQUIT);    (void) sigaddset(&set, SIGTSTP);    (void) sigprocmask(SIG_BLOCK, &set, &oset);#else    omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGTSTP));#endif /* POSIX_SIGNALS */    /*     * Setup signal handlers, turn off core dumps, and close open files.     */    initial_setup();    /*     * Set the prompt based on $SUDO_PROMPT (can be overridden by `-p')     */    user_prompt = getenv("SUDO_PROMPT");    /* Parse our arguments. */    sudo_mode = parse_args();    /* Setup defaults data structures. */    init_defaults();    if (sudo_mode & MODE_SHELL)	user_cmnd = "shell";    else	switch (sudo_mode) {	    case MODE_VERSION:		(void) printf("Sudo version %s\n", version);		if (getuid() == 0) {		    putchar('\n');		    dump_auth_methods();		    dump_defaults();		}		exit(0);		break;	    case MODE_HELP:		usage(0);		break;	    case MODE_VALIDATE:		user_cmnd = "validate";		break;	    case MODE_KILL:	    case MODE_INVALIDATE:		user_cmnd = "kill";		break;	    case MODE_LISTDEFS:		list_options();		exit(0);		break;	    case MODE_LIST:		user_cmnd = "list";		printmatches = 1;		break;	}    /* Must have a command to run... */    if (user_cmnd == NULL && NewArgc == 0)	usage(1);    clean_env(environ, badenv_table);    cmnd_status = init_vars(sudo_mode);    /* At this point, ruid == euid == 0 */    check_sudoers();	/* check mode/owner on _PATH_SUDOERS */    add_env(!(sudo_mode & MODE_SHELL));	/* add in SUDO_* envariables */    /* Validate the user but don't search for pseudo-commands. */    validated = sudoers_lookup(sudo_mode);    /* This goes after the sudoers parse since we honor sudoers options. */    if (sudo_mode == MODE_KILL || sudo_mode == MODE_INVALIDATE) {	remove_timestamp((sudo_mode == MODE_KILL));	exit(0);    }    if (validated & VALIDATE_ERROR)	log_error(0, "parse error in %s near line %d", _PATH_SUDOERS,	    errorlineno);    /* Is root even allowed to run sudo? */    if (user_uid == 0 && !def_flag(I_ROOT_SUDO)) {	(void) fputs("You are already root, you don't need to use sudo.\n",	    stderr);	exit(1);    }    /* If no command line args and "set_home" is not set, error out. */    if ((sudo_mode & MODE_IMPLIED_SHELL) && !def_flag(I_SHELL_NOARGS))	usage(1);    /* May need to set $HOME to target user. */    if ((sudo_mode & MODE_SHELL) && def_flag(I_SET_HOME))	sudo_mode |= MODE_RESET_HOME;    /* Bail if a tty is required and we don't have one.  */    if (def_flag(I_REQUIRETTY)) {	if ((fd = open(_PATH_TTY, O_RDWR|O_NOCTTY)) == -1)	    log_error(NO_MAIL, "sorry, you must have a tty to run sudo");	else	    (void) close(fd);    }    /* Fill in passwd struct based on user we are authenticating as.  */    auth_pw = get_authpw();    /* Require a password unless the NOPASS tag was set.  */    if (!(validated & FLAG_NOPASS))	check_user();    if (validated & VALIDATE_OK) {	/* Finally tell the user if the command did not exist. */	if (cmnd_status == NOT_FOUND_DOT) {	    (void) fprintf(stderr, "%s: ignoring `%s' found in '.'\nUse `sudo ./%s' if this is the `%s' you wish to run.\n", Argv[0], user_cmnd, user_cmnd, user_cmnd);	    exit(1);	} else if (cmnd_status == NOT_FOUND) {	    (void) fprintf(stderr, "%s: %s: command not found\n", Argv[0],		user_cmnd);	    exit(1);	}	log_auth(validated, 1);	if (sudo_mode == MODE_VALIDATE)	    exit(0);	else if (sudo_mode == MODE_LIST) {	    list_matches();	    exit(0);	}	/* This *must* have been set if we got a match but... */	if (safe_cmnd == NULL) {	    log_error(MSG_ONLY,		"internal error, cmnd_safe never got set for %s; %s",		user_cmnd,		"please report this error at http://courtesan.com/sudo/bugs/");	}	if (def_ival(I_LOGFACSTR))	    closelog();	/* Reset signal mask before we exec. */#ifdef POSIX_SIGNALS	(void) sigprocmask(SIG_SETMASK, &oset, NULL);#else	(void) sigsetmask(omask);#endif /* POSIX_SIGNALS */	/* Override user's umask if configured to do so. */	if (def_ival(I_UMASK) != 0777)	    (void) umask(def_mode(I_UMASK));	/* Replace the PATH envariable with a secure one. */	if (def_str(I_SECURE_PATH) && !user_is_exempt())	    if (sudo_setenv("PATH", def_str(I_SECURE_PATH))) {		(void) fprintf(stderr, "%s: cannot allocate memory!\n",		    Argv[0]);		exit(1);	    }	/* Restore coredumpsize resource limit. */#if defined(RLIMIT_CORE) && !defined(SUDO_DEVEL)	(void) setrlimit(RLIMIT_CORE, &corelimit);#endif /* RLIMIT_CORE */	/* Become specified user or root. */	set_perms(PERM_RUNAS, sudo_mode);	/* Set $HOME for `sudo -H'.  Only valid at PERM_RUNAS. */	if ((sudo_mode & MODE_RESET_HOME) && runas_homedir)	    (void) sudo_setenv("HOME", runas_homedir);#ifndef PROFILING	if ((sudo_mode & MODE_BACKGROUND) && fork() > 0)	    exit(0);	else	    EXEC(safe_cmnd, NewArgv);	/* run the command */#else	exit(0);#endif /* PROFILING */	/*	 * If we got here then the exec() failed...	 */	(void) fprintf(stderr, "%s: unable to exec %s: %s\n",	    Argv[0], safe_cmnd, strerror(errno));	exit(-1);    } else if ((validated & FLAG_NO_USER) || (validated & FLAG_NO_HOST)) {	log_auth(validated, 1);	exit(1);    } else if (validated & VALIDATE_NOT_OK) {	if (def_flag(I_PATH_INFO)) {	    /*	     * We'd like to not leak path info at all here, but that can	     * *really* confuse the users.  To really close the leak we'd	     * have to say "not allowed to run foo" even when the problem	     * is just "no foo in path" since the user can trivially set	     * their path to just contain a single dir.	     */	    log_auth(validated,		!(cmnd_status == NOT_FOUND_DOT || cmnd_status == NOT_FOUND));	    if (cmnd_status == NOT_FOUND)		(void) fprintf(stderr, "%s: %s: command not found\n", Argv[0],		    user_cmnd);	    else if (cmnd_status == NOT_FOUND_DOT)		(void) fprintf(stderr, "%s: ignoring `%s' found in '.'\nUse `sudo ./%s' if this is the `%s' you wish to run.\n", Argv[0], user_cmnd, user_cmnd, user_cmnd);	} else {	    /* Just tell the user they are not allowed to run foo. */	    log_auth(validated, 1);	}	exit(1);    } else {	/* should never get here */	log_auth(validated, 1);	exit(1);    }    exit(0);	/* not reached */}