1*12605Scomay static char sccsid[] = "@(#)nsh.c 4.3 (Berkeley) 05/20/83";
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 "finger", 20, "/usr/ucb/finger", "/usr/bin/finger",
33*12605Scomay "lpq", 20, "/usr/ucb/lpq", "/usr/bin/lpq",
348202Smckusick # ifdef FREELPR
35*12605Scomay "lpr", 20, "/usr/ucb/lpr", "/usr/bin/lpr",
368202Smckusick # endif
378202Smckusick "netlog", 20, "/usr/bin/netlog", "/usr/ucb/netlog",
388202Smckusick "netq", 20, "/usr/bin/netq", "/usr/ucb/netq",
398202Smckusick "ps", 20, "/bin/ps", "/usr/bin/ps",
40*12605Scomay "pstat", 20, "/etc/pstat", "/usr/bin/pstat",
418202Smckusick "vpq", 20, "/usr/ucb/vpq", "/usr/bin/vpq",
428202Smckusick "w", 20, "/usr/ucb/w", "/usr/bin/w",
438202Smckusick "wc", 20, "/usr/bin/wc", "/bin/wc",
448202Smckusick "who", 20, "/bin/who", "/usr/bin/who",
458202Smckusick "whom", 20, "/usr/ucb/whom", "/usr/bin/whom",
468202Smckusick "yank", 20, "/usr/ucb/yank", "/usr/bin/yank",
478202Smckusick # endif
488202Smckusick 0, 0, 0, 0
498202Smckusick };
508202Smckusick /* nsh -c cmd */
main(argc,argv)518202Smckusick main(argc,argv)
528202Smckusick char **argv; {
538202Smckusick char *s, buf[500];
548202Smckusick int i, flg = 0;
558202Smckusick if(argc != 3){
568202Smckusick fprintf(stderr,"Wrong number of arguments to nsh.\n");
578202Smckusick exit(EX_USAGE);
588202Smckusick }
598202Smckusick s = argv[2];
60*12605Scomay while (*s)
61*12605Scomay if (*s == ';'
62*12605Scomay || *s == '|'
63*12605Scomay || *s == '&'
64*12605Scomay || *s == '?'
65*12605Scomay || *s == '*'
66*12605Scomay || *s == '['
67*12605Scomay || *s == '~'
68*12605Scomay || *s == '{'
69*12605Scomay || *s == '<'
70*12605Scomay || *s == '>'
71*12605Scomay || *s == '$'
72*12605Scomay || *s == '`') {
73*12605Scomay fprintf(stderr, "Illegal shell metacharacter in command.\n");
74*12605Scomay exit(9);
75*12605Scomay } else
76*12605Scomay ++s;
77*12605Scomay s = argv[2];
788202Smckusick while(*s && *s != ' ')s++;
798202Smckusick if(*s == ' ')flg++;
808202Smckusick *s = 0;
818202Smckusick if((i = mlookup(argv[2])) < 0){
828202Smckusick fprintf(stderr,
838202Smckusick "Command '%s' is not allowed if logged in as 'network'.\n",
848202Smckusick argv[2]);
858202Smckusick exit(11);
868202Smckusick }
878202Smckusick if(st[i].count == 0){
888202Smckusick fprintf(stderr,
898202Smckusick "The command '%s' is not allowed to have arguments.\n",argv[2]);
908202Smckusick exit(9);
918202Smckusick }
928202Smckusick if(stat(st[i].full,buf) >= 0)
938202Smckusick strcpy(buf,st[i].full);
948202Smckusick else strcpy(buf,st[i].full1);
958202Smckusick if(flg && st[i].count > 1){ /* some cmds don't allow parms */
968202Smckusick *s = ' ';
978202Smckusick strcat(buf,s);
988202Smckusick }
998202Smckusick /*
1008202Smckusick fprintf(stderr,"%s\n",buf);
1018202Smckusick */
1028282Scomay execl(Bsh,"sh","-c",buf,0);
1038202Smckusick fprintf(stderr,"Execute of shell failed.\n");
1048202Smckusick exit(EX_UNAVAILABLE);
1058202Smckusick }
mlookup(s)1068202Smckusick mlookup(s)
1078202Smckusick char *s; {
1088202Smckusick int i;
1098202Smckusick for(i = 0; st[i].app; i++)
1108202Smckusick if(strcmp(st[i].app,s) == 0 || strcmp(st[i].full,s) == 0
1118202Smckusick || strcmp(st[i].full1,s) == 0)return(i);
1128202Smckusick return(-1);
1138202Smckusick }
114