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