xref: /titanic_53/usr/src/cmd/rexd/under.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 1985 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  *  under.c - program to execute a command under a given directory
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <signal.h>
36*7c478bd9Sstevel@tonic-gate #include <stdio.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
41*7c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
42*7c478bd9Sstevel@tonic-gate #include <rpcsvc/mount.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static char **Argv;		/* saved argument vector (for ps) */
46*7c478bd9Sstevel@tonic-gate static char *LastArgv;		/* saved end-of-argument vector */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate int	Debug = 0;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate int child = 0;			/* pid of the executed process */
51*7c478bd9Sstevel@tonic-gate int ChildDied = 0;		/* true when above is valid */
52*7c478bd9Sstevel@tonic-gate int HasHelper = 0;		/* must kill helpers (interactive mode) */
53*7c478bd9Sstevel@tonic-gate time_t time_now;
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate  *  SETPROCTITLE -- set the title of this process for "ps"
56*7c478bd9Sstevel@tonic-gate  *
57*7c478bd9Sstevel@tonic-gate  *	Does nothing if there were not enough arguments on the command
58*7c478bd9Sstevel@tonic-gate  * 	line for the information.
59*7c478bd9Sstevel@tonic-gate  *
60*7c478bd9Sstevel@tonic-gate  *	Side Effects:
61*7c478bd9Sstevel@tonic-gate  *		Clobbers argv[] of our main procedure.
62*7c478bd9Sstevel@tonic-gate  */
63*7c478bd9Sstevel@tonic-gate void
setproctitle(user,host)64*7c478bd9Sstevel@tonic-gate setproctitle(user, host)
65*7c478bd9Sstevel@tonic-gate 	char *user, *host;
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	register char *tohere;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	tohere = Argv[0];
70*7c478bd9Sstevel@tonic-gate 	if ((int)(LastArgv == (char *)NULL) ||
71*7c478bd9Sstevel@tonic-gate 			(int)(strlen(user)+strlen(host)+3) > (int)(LastArgv - tohere))
72*7c478bd9Sstevel@tonic-gate 		return;
73*7c478bd9Sstevel@tonic-gate 	*tohere++ = '-';		/* So ps prints (rpc.rexd)	*/
74*7c478bd9Sstevel@tonic-gate 	sprintf(tohere, "%s@%s", user, host);
75*7c478bd9Sstevel@tonic-gate 	while (*tohere++)		/* Skip to end of printf output	*/
76*7c478bd9Sstevel@tonic-gate 		;
77*7c478bd9Sstevel@tonic-gate 	while (tohere < LastArgv)	/* Avoid confusing ps		*/
78*7c478bd9Sstevel@tonic-gate 		*tohere++ = ' ';
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate void
main(argc,argv)83*7c478bd9Sstevel@tonic-gate main(argc, argv)
84*7c478bd9Sstevel@tonic-gate 	int argc;
85*7c478bd9Sstevel@tonic-gate 	char **argv;
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	static char usage[] = "Usage: under [-d] dir command...\n";
88*7c478bd9Sstevel@tonic-gate 	char *dir, *p;
89*7c478bd9Sstevel@tonic-gate 	char hostname[255];
90*7c478bd9Sstevel@tonic-gate 	char *tmpdir, *subdir, *parsefs();
91*7c478bd9Sstevel@tonic-gate 	char dirbuf[1024];
92*7c478bd9Sstevel@tonic-gate 	char error[1024];
93*7c478bd9Sstevel@tonic-gate 	int status;
94*7c478bd9Sstevel@tonic-gate 	int len;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	if (argc < 3)
97*7c478bd9Sstevel@tonic-gate 	{
98*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, usage);
99*7c478bd9Sstevel@tonic-gate 		exit(1);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	/*
103*7c478bd9Sstevel@tonic-gate 	 * argv start and extent for setproctitle()
104*7c478bd9Sstevel@tonic-gate 	 */
105*7c478bd9Sstevel@tonic-gate 	Argv = argv;
106*7c478bd9Sstevel@tonic-gate 	if (argc > 0)
107*7c478bd9Sstevel@tonic-gate 		LastArgv = argv[argc-1] + strlen(argv[argc-1]);
108*7c478bd9Sstevel@tonic-gate 	else
109*7c478bd9Sstevel@tonic-gate 		LastArgv = NULL;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	gethostname(hostname, 255);
112*7c478bd9Sstevel@tonic-gate 	strcat(hostname, ":/");
113*7c478bd9Sstevel@tonic-gate 	len = strlen(hostname);
114*7c478bd9Sstevel@tonic-gate 	if ( strcmp( argv[1], "-d" ) == 0 )
115*7c478bd9Sstevel@tonic-gate 	{
116*7c478bd9Sstevel@tonic-gate 		Debug = 1;
117*7c478bd9Sstevel@tonic-gate 		argv++;
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 	dir = argv[1];
120*7c478bd9Sstevel@tonic-gate 	if ( (int)strlen(dir) > len &&  (int)strncmp(dir, hostname, len) == 0)
121*7c478bd9Sstevel@tonic-gate 		dir = strchr(dir, ':') + 1;
122*7c478bd9Sstevel@tonic-gate 	else if (p = strchr(dir, ':'))
123*7c478bd9Sstevel@tonic-gate 	{
124*7c478bd9Sstevel@tonic-gate 		if (p[1] != '/')
125*7c478bd9Sstevel@tonic-gate 		{
126*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "under: %s invalid name\n", dir);
127*7c478bd9Sstevel@tonic-gate 			exit(1);
128*7c478bd9Sstevel@tonic-gate 		}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 		tmpdir = mktemp("/tmp/underXXXXXX");
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate                 if ( Debug && errno )
133*7c478bd9Sstevel@tonic-gate 		{
134*7c478bd9Sstevel@tonic-gate 			if ( errno != ENOENT )
135*7c478bd9Sstevel@tonic-gate                         printf("mktemp of %s returned %d %s\n",
136*7c478bd9Sstevel@tonic-gate                                         tmpdir, errno, strerror(errno));
137*7c478bd9Sstevel@tonic-gate 		}
138*7c478bd9Sstevel@tonic-gate 		errno = 0;	/* XXX access() call in mktemp sets errno = ENOENT */
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 		if (mkdir(tmpdir, 0777))
141*7c478bd9Sstevel@tonic-gate 		{
142*7c478bd9Sstevel@tonic-gate 			perror(tmpdir);
143*7c478bd9Sstevel@tonic-gate 			exit(1);
144*7c478bd9Sstevel@tonic-gate 		}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate                 if ( Debug && errno )
147*7c478bd9Sstevel@tonic-gate                         printf("mkdir of %s returned %d %s\n",
148*7c478bd9Sstevel@tonic-gate                                         tmpdir, errno, strerror(errno));
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 		subdir = parsefs(dir, error);
151*7c478bd9Sstevel@tonic-gate 		if (subdir == NULL)
152*7c478bd9Sstevel@tonic-gate 		{
153*7c478bd9Sstevel@tonic-gate 			exit(1);
154*7c478bd9Sstevel@tonic-gate 		}
155*7c478bd9Sstevel@tonic-gate 		time_now = time((long *) 0);
156*7c478bd9Sstevel@tonic-gate 		if (mount_nfs(dir, tmpdir, error))
157*7c478bd9Sstevel@tonic-gate 		{
158*7c478bd9Sstevel@tonic-gate 			exit(1);
159*7c478bd9Sstevel@tonic-gate 		}
160*7c478bd9Sstevel@tonic-gate 		strcpy(dirbuf, tmpdir);
161*7c478bd9Sstevel@tonic-gate 		strcat(dirbuf, "/");
162*7c478bd9Sstevel@tonic-gate 		strcat(dirbuf, subdir);
163*7c478bd9Sstevel@tonic-gate 		status = runcmd(dirbuf, argv[2], &argv[2]);
164*7c478bd9Sstevel@tonic-gate 		if (umount_nfs(dir, tmpdir))
165*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "under: couldn't umount %s\n", dir);
166*7c478bd9Sstevel@tonic-gate 		rmdir(tmpdir);
167*7c478bd9Sstevel@tonic-gate 		exit(status);
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	setgid(getgid());
171*7c478bd9Sstevel@tonic-gate 	setuid(getuid());
172*7c478bd9Sstevel@tonic-gate 	if (chdir(dir))
173*7c478bd9Sstevel@tonic-gate 	{
174*7c478bd9Sstevel@tonic-gate 		perror(dir);
175*7c478bd9Sstevel@tonic-gate 		exit(1);
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 	execvp(argv[2], &argv[2]);
178*7c478bd9Sstevel@tonic-gate 	perror(argv[2]);
179*7c478bd9Sstevel@tonic-gate 	exit(1);
180*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate typedef void (*sig_t)();
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate int
runcmd(dir,cmd,args)186*7c478bd9Sstevel@tonic-gate runcmd(dir, cmd, args)
187*7c478bd9Sstevel@tonic-gate 	char	*dir;
188*7c478bd9Sstevel@tonic-gate 	char	*cmd;
189*7c478bd9Sstevel@tonic-gate 	char	**args;
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	int pid, child, status;
192*7c478bd9Sstevel@tonic-gate 	sig_t sigint, sigquit;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	sigint = sigset(SIGINT, SIG_IGN);
195*7c478bd9Sstevel@tonic-gate 	sigquit = sigset(SIGQUIT, SIG_IGN);
196*7c478bd9Sstevel@tonic-gate 	pid = fork();
197*7c478bd9Sstevel@tonic-gate 	if (pid == -1)
198*7c478bd9Sstevel@tonic-gate 		return (0177);
199*7c478bd9Sstevel@tonic-gate 	if (pid == 0)
200*7c478bd9Sstevel@tonic-gate 	{
201*7c478bd9Sstevel@tonic-gate 		setgid(getgid());
202*7c478bd9Sstevel@tonic-gate 		setuid(getuid());
203*7c478bd9Sstevel@tonic-gate 		if (chdir(dir))
204*7c478bd9Sstevel@tonic-gate 		{
205*7c478bd9Sstevel@tonic-gate 			perror(dir);
206*7c478bd9Sstevel@tonic-gate 			exit(1);
207*7c478bd9Sstevel@tonic-gate 		}
208*7c478bd9Sstevel@tonic-gate 		(void) sigset(SIGINT, sigint);
209*7c478bd9Sstevel@tonic-gate 		(void) sigset(SIGQUIT, sigquit);
210*7c478bd9Sstevel@tonic-gate 		execvp(cmd, args);
211*7c478bd9Sstevel@tonic-gate 		perror(cmd);
212*7c478bd9Sstevel@tonic-gate 		exit(1);
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate 	while ((child = wait(&status)) != pid && child != -1)
215*7c478bd9Sstevel@tonic-gate 		;
216*7c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, sigint);
217*7c478bd9Sstevel@tonic-gate 	(void) sigset(SIGQUIT, sigquit);
218*7c478bd9Sstevel@tonic-gate 	if (child == -1)
219*7c478bd9Sstevel@tonic-gate 		return (0177);
220*7c478bd9Sstevel@tonic-gate 	if (status & 0377)
221*7c478bd9Sstevel@tonic-gate 		return (status & 0377);
222*7c478bd9Sstevel@tonic-gate 	return ((status >> 8) & 0377);
223*7c478bd9Sstevel@tonic-gate }
224