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