int PipeCommandProcessorInit(TProcessingModule *ProcMod, const char *Args) { int result=FALSE; char *Tempstr=NULL; char *Name=NULL, *Value=NULL, *ptr; STREAM *S; ptr=GetNameValuePair(Args,"\\S","=",&Name,&Value); while (ptr) { if (strcasecmp(Name,"Command")==0) Tempstr=CopyStr(Tempstr,Value); ptr=GetNameValuePair(ptr,"\\S","=",&Name,&Value); } if (! StrLen(Tempstr) ) { DestroyString(Name); DestroyString(Value); DestroyString(Tempstr); return(FALSE); } GetToken(Tempstr,"\\S",&Name,0); Value=FindFileInPath(Value,Name,getenv("PATH")); if (! StrLen(Value) ) { DestroyString(Name); DestroyString(Value); DestroyString(Tempstr); return(FALSE); } S=STREAMSpawnCommand(Value, COMMS_BY_PIPE); ProcMod->Data=(void *) S; result=TRUE; DestroyString(Name); DestroyString(Value); DestroyString(Tempstr); return(result); }
int ConnectHopSSH(STREAM *S,int Type, char *Host, int Port, char *User, char *Pass, char *NextHop) { char *Tempstr=NULL, *KeyFile=NULL, *Token=NULL, *Token2=NULL; STREAM *AuthS; int result=FALSE, val; unsigned int TunnelPort=0; if (Type==CONNECT_HOP_SSHTUNNEL) { TunnelPort=(rand() % (0xFFFF - 9000)) +9000; //Host will be Token, and port Token2 ParseConnectDetails(NextHop, NULL, &Token, &Token2, NULL, NULL, NULL); Tempstr=FormatStr(Tempstr,"ssh -2 -N %s@%s -L %d:%s:%s ",User,Host,TunnelPort,Token,Token2); } else Tempstr=MCopyStr(Tempstr,"ssh -2 -T ",User,"@",Host, " ", NULL ); if (strncmp(Pass,"keyfile:",8)==0) { if (S->in_fd != -1) { Token=FormatStr(Token,".%d-%d",getpid(),time(NULL)); SendPublicKeyToRemote(S,Token,Pass+8); KeyFile=CopyStr(KeyFile,Token); } Tempstr=MCatStr(Tempstr,"-i ",KeyFile," ",NULL); } if (Port > 0) { Token=FormatStr(Token," -p %d ",Port); Tempstr=CatStr(Tempstr,Token); } if (Type==CONNECT_HOP_SSHTUNNEL) { Tempstr=CatStr(Tempstr, " 2> /dev/null"); AuthS=STREAMSpawnCommand(Tempstr,COMMS_BY_PTY); STREAMSetValue(S,"HelperPID:SSH",STREAMGetValue(AuthS,"PeerPID")); } else if (S->in_fd==-1) { Tempstr=CatStr(Tempstr, " 2> /dev/null"); PseudoTTYSpawn(&S->in_fd,Tempstr,0); S->out_fd=S->in_fd; if (S->in_fd > -1) { result=TRUE; STREAMSetFlushType(S,FLUSH_LINE,0,0); } AuthS=S; } else { if (StrLen(KeyFile)) Tempstr=MCatStr(Tempstr," ; rm -f ",KeyFile,NULL); Tempstr=CatStr(Tempstr,"; exit\n"); STREAMWriteLine(Tempstr,S); result=TRUE; AuthS=S; } if ((StrLen(KeyFile)==0) && (StrLen(Pass) > 0)) { Token=MCopyStr(Token,Pass,"\n",NULL); for (val=0; val < 3; val++) { if (STREAMExpectAndReply(AuthS,"assword:",Token)) break; } } STREAMSetTimeout(AuthS,100); //STREAMExpectSilence(AuthS); sleep(3); if (Type==CONNECT_HOP_SSHTUNNEL) { S->in_fd=ConnectToHost("127.0.0.1",TunnelPort,0); S->out_fd=S->in_fd; if (S->in_fd > -1) result=TRUE; } DestroyString(Tempstr); DestroyString(KeyFile); DestroyString(Token2); DestroyString(Token); return(result); }
int HTTPServerSendPackedDir(STREAM *S, HTTPSession *Session, const char *Dir) { char *Tempstr=NULL, *DirName=NULL, *FileName=NULL, *ptr; char *Extn=NULL, *PackType=NULL, *Name=NULL, *Value=NULL; char *PackList=NULL; TFileMagic *FM; HTTPSession *Response; STREAM *Pipe; chdir(Dir); //unset session reuse, because we will close session to indicate end of package Session->Flags &= ~SESSION_REUSE; //do this so we can strcmp it PackList=CopyStr(PackList,""); Response=HTTPSessionResponse(Session); Response->ResponseCode=CopyStr(Response->ResponseCode,"200 OK"); ptr=GetNameValuePair(Session->Arguments, "&","=",&Name,&Value); while (ptr) { if ( StrLen(Name) ) { if (strcasecmp(Name,"packtype")==0) { PackType=CopyStr(PackType,Value); Extn=MCopyStr(Extn, ".", Value, NULL); FM=GetFileMagicForFile(Extn, NULL); Response->ContentType=CopyStr(Response->ContentType, FM->ContentType); } else if (strcasecmp(Name,"selected")==0) { if (strcmp(PackList," *") !=0) PackList=MCatStr(PackList, " ", Value, NULL); } } ptr=GetNameValuePair(ptr, "&","=",&Name,&Value); } if (StrLen(PackList)==0) PackList=CopyStr(PackList, " *"); DirName=CopyStr(DirName,Dir); StripDirectorySlash(DirName); ptr=GetBasename(DirName); if (! StrLen(ptr)) ptr="rootdir"; FileName=MCopyStr(FileName,Session->Host,"-",Session->UserName,"-",ptr,Extn,NULL); strrep(FileName,' ','_'); Tempstr=MCopyStr(Tempstr,"attachment; filename=",FileName,NULL); SetVar(Response->Headers,"Content-disposition",Tempstr); ptr=GetNameValuePair(Settings.PackFormats, ",",":", &Name, &Value); while (ptr) { if (strcasecmp(Name, PackType)==0) { if (strcasecmp(Value,"internal")==0) { if (strcasecmp(Name,"tar")==0) { HTTPServerSendHeaders(S, Response, 0); TarFiles(S, PackList); } } else { HTTPServerSendHeaders(S, Response, 0); Tempstr=MCopyStr(Tempstr,Value,PackList,NULL); Pipe=STREAMSpawnCommand(Tempstr, COMMS_BY_PIPE); STREAMSendFile(Pipe, S, 0, SENDFILE_KERNEL| SENDFILE_LOOP); STREAMClose(Pipe); } } ptr=GetNameValuePair(ptr, ",",":", &Name, &Value); } STREAMFlush(S); DestroyString(FileName); DestroyString(Tempstr); DestroyString(DirName); DestroyString(PackList); DestroyString(PackType); DestroyString(Name); DestroyString(Value); DestroyString(Extn); //This true means 'please close the connection' as our tarballs/zips are transferred using //connection: close to indicate end of transfer return(STREAM_CLOSED); }