1 static char sccsid[] = "@(#)nsh.c 4.2 (Berkeley) 10/02/82"; 2 3 # include "defs.h" 4 /* 5 nsh -c "comand to be executed" 6 7 This pseudo-shell is executed over the network 8 as the login shell of an acount "network", no passwd. 9 It will only execute certain allowed commands. 10 11 has these exit codes: 12 EX_USAGE = wrong # arguments to nsh 13 9 = command you execute may not take arguments 14 10= the execl failed 15 EX_UNAVAILABLE= could not find full path name for the command 16 17 count is the # of arguments (= argc) allowed. 18 a count of 0 turns off the command 19 */ 20 21 struct { 22 char *app; 23 char count; 24 char *full; 25 char *full1; 26 } st[] = { 27 /* I assume these are the same for RAND */ 28 "mmail", 20, "/usr/net/bin/mmail", "/usr/net/bin/mmail", 29 "mwrite", 20, "/usr/net/bin/mwrite", "/usr/net/bin/mwrite", 30 "prmail", 20, "/usr/net/bin/prmail", "/usr/net/bin/prmail", 31 # ifndef NFREECMD 32 "bpq", 20, "/usr/bin/bpq", "/bin/bpq", 33 "epq", 20, "/usr/bin/epq", "/bin/epq", 34 "finger", 20, "/usr/ucb/finger", "/usr/bin/finger", 35 "help", 20, "/bin/help", "/usr/bin/help", 36 "lpq", 20, "/usr/bin/lpq", "/bin/lpq", 37 # ifdef FREELPR 38 "lpr", 20, "/usr/bin/lpr", "/bin/lpr", 39 # endif 40 "netlog", 20, "/usr/bin/netlog", "/usr/ucb/netlog", 41 "netq", 20, "/usr/bin/netq", "/usr/ucb/netq", 42 "news", 20, "/usr/bin/news", "/usr/ucb/news", 43 "ps", 20, "/bin/ps", "/usr/bin/ps", 44 "pstat", 20, "/usr/bin/pstat", "/bin/pstat", 45 "rcs", 20, "/usr/bin/rcs", "/bin/rcs", 46 "rcslog", 1, "/usr/bin/rcslog", "/bin/rcslog", 47 "rcsq", 20, "/usr/bin/rcsq", "/bin/rcsq", 48 "trq", 20, "/usr/bin/trq", "/bin/trq", 49 "vpq", 20, "/usr/ucb/vpq", "/usr/bin/vpq", 50 "w", 20, "/usr/ucb/w", "/usr/bin/w", 51 "wc", 20, "/usr/bin/wc", "/bin/wc", 52 "where", 20, "/usr/bin/where", "/bin/where", 53 "who", 20, "/bin/who", "/usr/bin/who", 54 "whom", 20, "/usr/ucb/whom", "/usr/bin/whom", 55 "write", 20, "/usr/bin/write", "/bin/write", 56 "yank", 20, "/usr/ucb/yank", "/usr/bin/yank", 57 # endif 58 0, 0, 0, 0 59 }; 60 /* nsh -c cmd */ 61 main(argc,argv) 62 char **argv; { 63 char *s, buf[500]; 64 int i, flg = 0; 65 if(argc != 3){ 66 fprintf(stderr,"Wrong number of arguments to nsh.\n"); 67 exit(EX_USAGE); 68 } 69 s = argv[2]; 70 while(*s && *s != ' ')s++; 71 if(*s == ' ')flg++; 72 *s = 0; 73 if((i = mlookup(argv[2])) < 0){ 74 fprintf(stderr, 75 "Command '%s' is not allowed if logged in as 'network'.\n", 76 argv[2]); 77 exit(11); 78 } 79 if(st[i].count == 0){ 80 fprintf(stderr, 81 "The command '%s' is not allowed to have arguments.\n",argv[2]); 82 exit(9); 83 } 84 if(stat(st[i].full,buf) >= 0) 85 strcpy(buf,st[i].full); 86 else strcpy(buf,st[i].full1); 87 if(flg && st[i].count > 1){ /* some cmds don't allow parms */ 88 *s = ' '; 89 strcat(buf,s); 90 } 91 /* 92 fprintf(stderr,"%s\n",buf); 93 */ 94 execl(Bsh,"sh","-c",buf,0); 95 fprintf(stderr,"Execute of shell failed.\n"); 96 exit(EX_UNAVAILABLE); 97 } 98 mlookup(s) 99 char *s; { 100 int i; 101 for(i = 0; st[i].app; i++) 102 if(strcmp(st[i].app,s) == 0 || strcmp(st[i].full,s) == 0 103 || strcmp(st[i].full1,s) == 0)return(i); 104 return(-1); 105 } 106