1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string>
using namespace std;
int main()
{
while (true)
{
string cmd;
cout << "rsh: " << flush;
cin >> cmd;
if (cmd == "exit")
{
cout << "terminating the shell\n";
return 0;
}
if (cmd != "date" && cmd != "id")
{
cout << "Command " << cmd << " disallowed\n";
continue;
}
pid_t pid = fork();
if (pid == 0)
{
execlp(cmd.c_str(), cmd.c_str(), 0);
cerr << "Execution of " << cmd << " failed\n";
return 1;
}
if (pid < 0)
{
cerr << "Fork() failed\n";
return 1;
}
int status;
wait(&status);
if ((status = WEXITSTATUS(status)))
cerr << cmd << " returned exit status " << status << '\n';
}
}
/*
Example of generated conversation:
rsh: opa
Command opa disallowed
rsh: date
Tue Jul 17 23:19:08 CEST 2001
rsh: id
uid=405(frank) gid=100(users) groups=100(users),4(adm)
rsh: quit
Command quit disallowed
rsh: exit
terminating the shell
*/
|