/* * Function: main() * * Description: * Entry point for this program. * * Inputs: * argc - The number of argument with which this program was executed. * argv - Array of pointers to strings containing the command-line arguments. * * Output:: * 0 - This program terminated normally. */ int main(int argc, char *argv[]) { char command[60]; char *args[argc]; int arg_num = argc - 2; char runas_uname[20]; int i; user cur_user = get_user("",getuid()); user runas_user; //user runas_user = get_user(argv[1], -1); if (argc >= 2) { //strcpy(runas_uname, argv[1]); runas_user = get_user(argv[1], -1); strcpy(command, argv[2]); for (i = 2; i < argc; i++) { args[i - 2] = (char *)malloc(strlen (argv [i]) * sizeof(char) + 1); strcpy(args[i - 2], argv[i]); } args[argc - 2] = NULL; } else { setuid(getuid()); fprintf(stderr, "%s: No username or command specified\n", argv[0]); exit(127); } char *pwd = getpass("Password: "******"%s: user not found, or password incorrect\n", runas_uname); exit(1); } /* Spawn a child to run the program. */ int status; pid_t pid=fork(); if (pid==0) { /* child process */ //setuid(cur_user.pw_uid); //seteuid(runas_user.pw_uid); setreuid(cur_user.pw_uid,runas_user.pw_uid); setregid(cur_user.pw_gid,runas_user.pw_gid); //setresuid(cur_user.pw_uid,runas_user.pw_uid,cur_user.pw_uid,getsuid()); execvp(command,args); exit(127); /* only if execvp fails */ } else { /* pid!=0; parent process */ waitpid(pid,&status,0); /* wait for child to exit */ if(WIFEXITED(status) != 0) { log_exec(WEXITSTATUS(status), command, arg_num, args); } else { /* process exited abnormally, do not log */ fprintf(stderr, "%s: process exited abnormally with status %d\n", command, status); exit(1); } } setuid(getuid()); return 0; }
/// Spawns a new binary and multiplexes and captures its stdout and stderr. /// /// If the subprocess cannot be completely set up for any reason, it attempts to /// dump an error message to its stderr channel and it then calls std::abort(). /// /// \param program The binary to execute. /// \param args The arguments to pass to the binary, without the program name. /// /// \return A new child object, returned as a dynamically-allocated object /// because children classes are unique and thus noncopyable. /// /// \throw process::system_error If the process cannot be spawned due to a /// system call error. std::auto_ptr< process::child > process::child::spawn_capture(const fs::path& program, const args_vector& args) { std::auto_ptr< child > child = fork_capture_aux(); if (child.get() == NULL) cxx_exec(program, args); log_exec(program, args); return child; }
/// Spawns a new binary and redirects its stdout and stderr to files. /// /// If the subprocess cannot be completely set up for any reason, it attempts to /// dump an error message to its stderr channel and it then calls std::abort(). /// /// \param program The binary to execute. /// \param args The arguments to pass to the binary, without the program name. /// \param stdout_file The name of the file in which to store the stdout. /// \param stderr_file The name of the file in which to store the stderr. /// /// \return A new child object, returned as a dynamically-allocated object /// because children classes are unique and thus noncopyable. /// /// \throw process::system_error If the process cannot be spawned due to a /// system call error. std::auto_ptr< process::child > process::child::spawn_files(const fs::path& program, const args_vector& args, const fs::path& stdout_file, const fs::path& stderr_file) { std::auto_ptr< child > child = fork_files_aux(stdout_file, stderr_file); if (child.get() == NULL) cxx_exec(program, args); log_exec(program, args); return child; }