void SwitchProgram(const char *CommandLine, int Flags, const char *Config) { char **argv, *ptr; char *Token=NULL, *SafeStr=NULL; int i; if (Flags & SPAWN_TRUST_COMMAND) SafeStr=CopyStr(SafeStr,CommandLine); else SafeStr=MakeShellSafeString(SafeStr, CommandLine, 0); SafeStr=MakeShellSafeString(SafeStr,CommandLine,0); argv=(char **) calloc(101,sizeof(char *)); ptr=SafeStr; for (i=0; i < 100; i++) { ptr=GetToken(ptr,"\\S",&Token,GETTOKEN_QUOTES); if (! ptr) break; argv[i]=CopyStr(argv[i],Token); } SpawnApplyConfig(Config, Flags); DestroyString(Token); DestroyString(SafeStr); /* we are the child so we continue */ execv(argv[0],argv); //no point trying to free stuff here, we will no longer //be the main program }
void SwitchProgram(const char *CommandLine, const char *User,const char *Group, const char *Dir) { char **argv, *ptr; char *Token=NULL, *SafeStr=NULL; int i; SafeStr=MakeShellSafeString(SafeStr,CommandLine,0); argv=(char **) calloc(101,sizeof(char *)); ptr=SafeStr; for (i=0; i < 100; i++) { ptr=GetToken(ptr,"\\S",&Token,GETTOKEN_QUOTES); if (! ptr) break; argv[i]=CopyStr(argv[i],Token); } if (StrLen(Dir)) chdir(Dir); if (StrLen(Group)) SwitchGroup(Group); if (StrLen(User)) SwitchUser(User); DestroyString(Token); DestroyString(SafeStr); /* we are the child so we continue */ execv(argv[0],argv); //no point trying to free stuff here, we will no longer //be the main program }
int BASIC_FUNC_EXEC_COMMAND(void *Command) { char *SafeStr=NULL; int result; SafeStr=MakeShellSafeString(SafeStr,Command,0); result=execl("/bin/sh","/bin/sh","-c",(char *) SafeStr,NULL); DestroyString(SafeStr); return(result); }
STREAM *STREAMSpawnCommand(const char *Command, int Flags, const char *Config) { STREAM *S=NULL; char *Tempstr=NULL; if (Flags & SPAWN_TRUST_COMMAND) Tempstr=CopyStr(Tempstr,Command); else Tempstr=MakeShellSafeString(Tempstr, Command, 0); S=STREAMSpawnFunction(BASIC_FUNC_EXEC_COMMAND, Tempstr, Flags, Config); DestroyString(Tempstr); return(S); }
//This is the function we call in the child process for 'SpawnCommand' int BASIC_FUNC_EXEC_COMMAND(void *Command, int Flags) { int result; char *Token=NULL, *FinalCommand=NULL, *ExecPath=NULL; char **argv; const char *ptr; int i; if (Flags & SPAWN_TRUST_COMMAND) FinalCommand=CopyStr(FinalCommand, (char *) Command); else FinalCommand=MakeShellSafeString(FinalCommand, (char *) Command, 0); StripTrailingWhitespace(FinalCommand); if (Flags & SPAWN_NOSHELL) { argv=(char **) calloc(101,sizeof(char *)); ptr=FinalCommand; ptr=GetToken(FinalCommand,"\\S",&Token,GETTOKEN_QUOTES); ExecPath=FindFileInPath(ExecPath,Token,getenv("PATH")); i=0; if (! (Flags & SPAWN_ARG0)) { argv[0]=CopyStr(argv[0],ExecPath); i=1; } for (; i < 100; i++) { ptr=GetToken(ptr,"\\S",&Token,GETTOKEN_QUOTES); if (! ptr) break; argv[i]=CopyStr(argv[i],Token); } execv(ExecPath,argv); } else result=execl("/bin/sh","/bin/sh","-c",(char *) Command,NULL); RaiseError(ERRFLAG_ERRNO, "Spawn", "Failed to execute '%s'",Command); //We'll never get to here unless something fails! DestroyString(FinalCommand); DestroyString(ExecPath); DestroyString(Token); return(result); }
STREAM *STREAMSpawnCommand(const char *Command, const char *User, const char *Group, int Flags) { int to_fd, from_fd; pid_t pid; STREAM *S=NULL; char *Tempstr=NULL; if (Flags & SPAWN_TRUST_COMMAND) Tempstr=CopyStr(Tempstr,Command); else Tempstr=MakeShellSafeString(Tempstr, Command, 0); if (Flags & COMMS_BY_PTY) { pid=PseudoTTYSpawn(&to_fd,Tempstr,User,Group,Flags); if (pid > 0) S=STREAMFromFD(to_fd); } else { if (Flags & COMMS_COMBINE_STDERR) { pid=PipeSpawn(&to_fd, &from_fd, COMMS_COMBINE_STDERR, Tempstr,User,Group); } else pid=PipeSpawn(&to_fd, &from_fd, NULL, Tempstr,User,Group); if (pid > 0) S=STREAMFromDualFD(from_fd, to_fd); } if (S) { STREAMSetFlushType(S,FLUSH_LINE,0,0); Tempstr=FormatStr(Tempstr,"%d",pid); STREAMSetValue(S,"PeerPID",Tempstr); } DestroyString(Tempstr); return(S); }