STREAM *STREAMSpawnFunction(BASIC_FUNC Func, void *Data, const char *Config) { int to_fd, from_fd, *iptr; pid_t pid=0; STREAM *S=NULL; char *Tempstr=NULL; int Flags=0; Flags=TTYParseConfig(Config, NULL); if (Flags & TTYFLAG_PTY) { pid=PseudoTTYSpawnFunction(&to_fd, Func, Data, Flags, Config); from_fd=to_fd; } else { iptr=NULL; //if (Flags & COMMS_COMBINE_STDERR) iptr=(int *) COMMS_COMBINE_STDERR; pid=PipeSpawnFunction(&to_fd, &from_fd, iptr, Func, Data, Config); } 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); S->Type=STREAM_TYPE_PIPE; } DestroyString(Tempstr); return(S); }
pid_t PseudoTTYSpawn(int *pty, const char *Command, const char *Config) { return(PseudoTTYSpawnFunction(pty, BASIC_FUNC_EXEC_COMMAND, (void *) Command, TTYParseConfig(Config, NULL), Config)); }
pid_t PseudoTTYSpawn(int *pty, const char *Command, int Flags, const char *Config) { return(PseudoTTYSpawnFunction(pty, BASIC_FUNC_EXEC_COMMAND, (void *) Command, Flags, Config)); }
pid_t PseudoTTYSpawn(int *pty, const char *Command, const char *User, const char *Group, int TTYFlags) { return(PseudoTTYSpawnFunction(pty, BASIC_FUNC_EXEC_COMMAND, (void *) Command, User, Group, TTYFlags)); }
void RunTelnetSession(TSession *Session) { STREAM *Local, *S; char *Tempstr=NULL; int result, fd; ListNode *Streams; struct passwd *pwent; struct group *grent; struct timeval tv; time_t Duration, Start, Now, LastActivity; time(&Start); LastActivity=Start; Streams=ListCreate(); ListAddItem(Streams,Session->S); //if '-real-user' was specified on the command-line, then this overrides //anything read from password files if (Settings.Flags & FLAG_FORCE_REALUSER) { Session->RealUser=CopyStr(Session->RealUser,Settings.RealUser); } //Get User Details before we chroot! if (StrLen(Session->RealUser)) { pwent=getpwnam(Session->RealUser); if (! pwent) { syslog(Settings.InfoLogLevel,"Failed to lookup RealUser '%s' for user '%s'",Session->RealUser,Session->User); exit(1); } Session->RealUserUID=pwent->pw_uid; Session->GroupID=pwent->pw_gid; } //if '-shell' was specified on the command-line, then this overrides //anything read from password files if (Settings.Flags & FLAG_FORCE_SHELL) { Session->Shell=CopyStr(Session->Shell,Settings.RealUser); } if (Settings.Flags & FLAG_DYNHOME) { Session->HomeDir=SessionSubstituteVars(Session->HomeDir,Settings.DynamicHomeDir,Session); Session->HomeDir=SlashTerminateDirectoryPath(Session->HomeDir); MakeDirPath(Session->HomeDir,0777); } //CD to the user's home directory if (StrLen(Session->HomeDir)) { chdir(Session->HomeDir); } DoBindMounts(Settings.BindMounts,0); //This login script allows setting up any aspects of the environment before we launch the shell. For instance it //might be used to copy files into the chroot environment before chrooting if (StrLen(Settings.LoginScript)) system(Settings.LoginScript); //LAUNCH THE SHELL FUNCTION!!! This launches the program that the telnet user is 'speaking' to. //If chhome is active, then it will be chrooted into the user's home directory PseudoTTYSpawnFunction(&fd, LaunchPtyFunc, Session, TTYFLAG_CANON | TTYFLAG_ECHO | TTYFLAG_CRLF | TTYFLAG_LFCR | TTYFLAG_IGNSIG); Local=STREAMFromFD(fd); STREAMSetTimeout(Local,0); //Might as well chroot on this side of the pipe too, unless we have a 'LogoutScript' //Logout scripts exist to allow copying stuff back out of the chroot when the session is //finished. We can't do this if we chroot this side as well as the 'shell' side if ( (! StrLen(Settings.LogoutScript)) && (Settings.Flags & FLAG_CHHOME) ) chroot("."); //DON'T SWITCH USER. NEED root TO UNBIND MOUNTS //if (setreuid(Session->RealUserUID,Session->RealUserUID) !=0) exit(1); ListAddItem(Streams,Local); Tempstr=SetStrLen(Tempstr,4096); while (1) { if (Settings.IdleTimeout) tv.tv_sec=Settings.IdleTimeout; else tv.tv_sec=3600 * 24; S=STREAMSelect(Streams,&tv); time(&Now); if (S) { if (S==Session->S) { result=TelnetReadBytes(Session->S, Tempstr, 4096, TNRB_NONBLOCK); if (result ==-1) break; STREAMWriteBytes(Local,Tempstr,result); } else { result=STREAMReadBytes(Local,Tempstr,4096); if (result < 0) break; STREAMWriteBytes(Session->S,Tempstr,result); if (result < 0) break; } if (Settings.Flags & FLAG_WINSIZE) SetWindowSize(Session->S->out_fd); LastActivity=Now; } if ((Settings.IdleTimeout > 0) && ((Now - LastActivity) > Settings.IdleTimeout)) break; } if (StrLen(Settings.LogoutScript)) system(Settings.LogoutScript); if (Settings.Flags & FLAG_UNMOUNT) UndoBindMounts(Settings.BindMounts, 0); if (Settings.Flags & FLAG_DYNHOME) rmdir(Session->HomeDir); Duration=time(NULL) - Start; syslog(Settings.InfoLogLevel,"%s@%s logged out after %d secs",Session->User,Session->ClientIP, Duration); STREAMClose(Session->S); STREAMClose(Local); DestroyString(Tempstr); }