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