1*8282Scomay static char sccsid[] = "@(#)nsh.c 4.2 (Berkeley) 10/02/82"; 28202Smckusick 38202Smckusick # include "defs.h" 48202Smckusick /* 58202Smckusick nsh -c "comand to be executed" 68202Smckusick 78202Smckusick This pseudo-shell is executed over the network 88202Smckusick as the login shell of an acount "network", no passwd. 98202Smckusick It will only execute certain allowed commands. 108202Smckusick 118202Smckusick has these exit codes: 128202Smckusick EX_USAGE = wrong # arguments to nsh 138202Smckusick 9 = command you execute may not take arguments 148202Smckusick 10= the execl failed 158202Smckusick EX_UNAVAILABLE= could not find full path name for the command 168202Smckusick 178202Smckusick count is the # of arguments (= argc) allowed. 188202Smckusick a count of 0 turns off the command 198202Smckusick */ 208202Smckusick 218202Smckusick struct { 228202Smckusick char *app; 238202Smckusick char count; 248202Smckusick char *full; 258202Smckusick char *full1; 268202Smckusick } st[] = { 278202Smckusick /* I assume these are the same for RAND */ 288202Smckusick "mmail", 20, "/usr/net/bin/mmail", "/usr/net/bin/mmail", 298202Smckusick "mwrite", 20, "/usr/net/bin/mwrite", "/usr/net/bin/mwrite", 308202Smckusick "prmail", 20, "/usr/net/bin/prmail", "/usr/net/bin/prmail", 318202Smckusick # ifndef NFREECMD 328202Smckusick "bpq", 20, "/usr/bin/bpq", "/bin/bpq", 338202Smckusick "epq", 20, "/usr/bin/epq", "/bin/epq", 348202Smckusick "finger", 20, "/usr/ucb/finger", "/usr/bin/finger", 358202Smckusick "help", 20, "/bin/help", "/usr/bin/help", 368202Smckusick "lpq", 20, "/usr/bin/lpq", "/bin/lpq", 378202Smckusick # ifdef FREELPR 388202Smckusick "lpr", 20, "/usr/bin/lpr", "/bin/lpr", 398202Smckusick # endif 408202Smckusick "netlog", 20, "/usr/bin/netlog", "/usr/ucb/netlog", 418202Smckusick "netq", 20, "/usr/bin/netq", "/usr/ucb/netq", 428202Smckusick "news", 20, "/usr/bin/news", "/usr/ucb/news", 438202Smckusick "ps", 20, "/bin/ps", "/usr/bin/ps", 448202Smckusick "pstat", 20, "/usr/bin/pstat", "/bin/pstat", 458202Smckusick "rcs", 20, "/usr/bin/rcs", "/bin/rcs", 468202Smckusick "rcslog", 1, "/usr/bin/rcslog", "/bin/rcslog", 478202Smckusick "rcsq", 20, "/usr/bin/rcsq", "/bin/rcsq", 488202Smckusick "trq", 20, "/usr/bin/trq", "/bin/trq", 498202Smckusick "vpq", 20, "/usr/ucb/vpq", "/usr/bin/vpq", 508202Smckusick "w", 20, "/usr/ucb/w", "/usr/bin/w", 518202Smckusick "wc", 20, "/usr/bin/wc", "/bin/wc", 528202Smckusick "where", 20, "/usr/bin/where", "/bin/where", 538202Smckusick "who", 20, "/bin/who", "/usr/bin/who", 548202Smckusick "whom", 20, "/usr/ucb/whom", "/usr/bin/whom", 558202Smckusick "write", 20, "/usr/bin/write", "/bin/write", 568202Smckusick "yank", 20, "/usr/ucb/yank", "/usr/bin/yank", 578202Smckusick # endif 588202Smckusick 0, 0, 0, 0 598202Smckusick }; 608202Smckusick /* nsh -c cmd */ 618202Smckusick main(argc,argv) 628202Smckusick char **argv; { 638202Smckusick char *s, buf[500]; 648202Smckusick int i, flg = 0; 658202Smckusick if(argc != 3){ 668202Smckusick fprintf(stderr,"Wrong number of arguments to nsh.\n"); 678202Smckusick exit(EX_USAGE); 688202Smckusick } 698202Smckusick s = argv[2]; 708202Smckusick while(*s && *s != ' ')s++; 718202Smckusick if(*s == ' ')flg++; 728202Smckusick *s = 0; 738202Smckusick if((i = mlookup(argv[2])) < 0){ 748202Smckusick fprintf(stderr, 758202Smckusick "Command '%s' is not allowed if logged in as 'network'.\n", 768202Smckusick argv[2]); 778202Smckusick exit(11); 788202Smckusick } 798202Smckusick if(st[i].count == 0){ 808202Smckusick fprintf(stderr, 818202Smckusick "The command '%s' is not allowed to have arguments.\n",argv[2]); 828202Smckusick exit(9); 838202Smckusick } 848202Smckusick if(stat(st[i].full,buf) >= 0) 858202Smckusick strcpy(buf,st[i].full); 868202Smckusick else strcpy(buf,st[i].full1); 878202Smckusick if(flg && st[i].count > 1){ /* some cmds don't allow parms */ 888202Smckusick *s = ' '; 898202Smckusick strcat(buf,s); 908202Smckusick } 918202Smckusick /* 928202Smckusick fprintf(stderr,"%s\n",buf); 938202Smckusick */ 94*8282Scomay execl(Bsh,"sh","-c",buf,0); 958202Smckusick fprintf(stderr,"Execute of shell failed.\n"); 968202Smckusick exit(EX_UNAVAILABLE); 978202Smckusick } 988202Smckusick mlookup(s) 998202Smckusick char *s; { 1008202Smckusick int i; 1018202Smckusick for(i = 0; st[i].app; i++) 1028202Smckusick if(strcmp(st[i].app,s) == 0 || strcmp(st[i].full,s) == 0 1038202Smckusick || strcmp(st[i].full1,s) == 0)return(i); 1048202Smckusick return(-1); 1058202Smckusick } 106