xref: /titanic_41/usr/src/cmd/backup/lib/myrcmd.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 /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <myrcmd.h>
38*7c478bd9Sstevel@tonic-gate #include <limits.h>
39*7c478bd9Sstevel@tonic-gate #include <stdio.h>
40*7c478bd9Sstevel@tonic-gate #include <string.h>
41*7c478bd9Sstevel@tonic-gate #include <pwd.h>
42*7c478bd9Sstevel@tonic-gate #include <errno.h>
43*7c478bd9Sstevel@tonic-gate #include <unistd.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
46*7c478bd9Sstevel@tonic-gate #include <signal.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
50*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #include <netdb.h>
53*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
54*7c478bd9Sstevel@tonic-gate #include <libintl.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #include <memutils.h>
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #define	index(s, c)		strchr(s, c)
59*7c478bd9Sstevel@tonic-gate char	*strchr();
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate char	*inet_ntoa();
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate char myrcmd_stderr[1024];
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate int
myrcmd(char ** ahost,unsigned short rport,char * locuser,char * remuser,char * cmd)66*7c478bd9Sstevel@tonic-gate myrcmd(char **ahost, unsigned short rport, char *locuser, char *remuser,
67*7c478bd9Sstevel@tonic-gate     char *cmd)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	uint_t loclen, remlen, cmdlen;
70*7c478bd9Sstevel@tonic-gate 	int s, timo, retval;
71*7c478bd9Sstevel@tonic-gate 	int tries = 0;
72*7c478bd9Sstevel@tonic-gate 	pid_t pid;
73*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin;
74*7c478bd9Sstevel@tonic-gate 	char c;
75*7c478bd9Sstevel@tonic-gate 	int lport;
76*7c478bd9Sstevel@tonic-gate 	int saverr;
77*7c478bd9Sstevel@tonic-gate 	struct hostent *hp;
78*7c478bd9Sstevel@tonic-gate 	sigset_t oldmask;
79*7c478bd9Sstevel@tonic-gate 	sigset_t newmask;
80*7c478bd9Sstevel@tonic-gate 	struct sigaction oldaction;
81*7c478bd9Sstevel@tonic-gate 	struct sigaction newaction;
82*7c478bd9Sstevel@tonic-gate 	static struct hostent numhp;
83*7c478bd9Sstevel@tonic-gate 	static char numhostname[32];	/* big enough for "255.255.255.255" */
84*7c478bd9Sstevel@tonic-gate 	struct in_addr numaddr;
85*7c478bd9Sstevel@tonic-gate 	struct in_addr *numaddrlist[2];
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	myrcmd_stderr[0] = '\0';	/* empty error string */
88*7c478bd9Sstevel@tonic-gate 	pid = getpid();
89*7c478bd9Sstevel@tonic-gate 	hp = gethostbyname(*ahost);
90*7c478bd9Sstevel@tonic-gate 	if (hp == 0) {
91*7c478bd9Sstevel@tonic-gate 		char *straddr;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 		bzero((char *)numaddrlist, sizeof (numaddrlist));
94*7c478bd9Sstevel@tonic-gate 		if ((numaddr.s_addr = inet_addr(*ahost)) == (in_addr_t)-1) {
95*7c478bd9Sstevel@tonic-gate 			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
96*7c478bd9Sstevel@tonic-gate 			    gettext("%s: unknown host\n"), *ahost);
97*7c478bd9Sstevel@tonic-gate 			return (MYRCMD_NOHOST);
98*7c478bd9Sstevel@tonic-gate 		} else {
99*7c478bd9Sstevel@tonic-gate 			bzero((char *)&numhp, sizeof (numhp));
100*7c478bd9Sstevel@tonic-gate 			bzero(numhostname, sizeof (numhostname));
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 			if ((straddr = inet_ntoa(numaddr)) == (char *)0) {
103*7c478bd9Sstevel@tonic-gate 				(void) snprintf(myrcmd_stderr,
104*7c478bd9Sstevel@tonic-gate 				    sizeof (myrcmd_stderr),
105*7c478bd9Sstevel@tonic-gate 				    gettext("%s: unknown host\n"), *ahost);
106*7c478bd9Sstevel@tonic-gate 				return (MYRCMD_NOHOST);
107*7c478bd9Sstevel@tonic-gate 			}
108*7c478bd9Sstevel@tonic-gate 			(void) strncpy(numhostname, straddr,
109*7c478bd9Sstevel@tonic-gate 			    sizeof (numhostname));
110*7c478bd9Sstevel@tonic-gate 			numhostname[sizeof (numhostname) - 1] = '\0';
111*7c478bd9Sstevel@tonic-gate 			numhp.h_name = numhostname;
112*7c478bd9Sstevel@tonic-gate 			numhp.h_addrtype = AF_INET;
113*7c478bd9Sstevel@tonic-gate 			numhp.h_length = sizeof (numaddr);
114*7c478bd9Sstevel@tonic-gate 			numaddrlist[0] = &numaddr;
115*7c478bd9Sstevel@tonic-gate 			numaddrlist[1] = NULL;
116*7c478bd9Sstevel@tonic-gate 			numhp.h_addr_list = (char **)numaddrlist;
117*7c478bd9Sstevel@tonic-gate 			hp = &numhp;
118*7c478bd9Sstevel@tonic-gate 		}
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 	*ahost = hp->h_name;
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	/* This provides a bounds-test for the bcopy()s below. */
123*7c478bd9Sstevel@tonic-gate 	if ((unsigned)(hp->h_length) > sizeof (sin.sin_addr)) {
124*7c478bd9Sstevel@tonic-gate 		(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
125*7c478bd9Sstevel@tonic-gate 		    gettext("rcmd: address size: %d larger than limit %d\n"),
126*7c478bd9Sstevel@tonic-gate 		    hp->h_length, sizeof (sin.sin_addr));
127*7c478bd9Sstevel@tonic-gate 		return (MYRCMD_EBAD);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	/* ignore SIGPIPE */
131*7c478bd9Sstevel@tonic-gate 	bzero((char *)&newaction, sizeof (newaction));
132*7c478bd9Sstevel@tonic-gate 	newaction.sa_handler = SIG_IGN;
133*7c478bd9Sstevel@tonic-gate 	newaction.sa_flags = SA_ONSTACK;
134*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPIPE, &newaction, &oldaction);
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	/* block SIGURG */
137*7c478bd9Sstevel@tonic-gate 	bzero((char *)&newmask, sizeof (newmask));
138*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&newmask, SIGURG);
139*7c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &newmask, &oldmask);
140*7c478bd9Sstevel@tonic-gate again:
141*7c478bd9Sstevel@tonic-gate 	timo = 1;
142*7c478bd9Sstevel@tonic-gate 	/*
143*7c478bd9Sstevel@tonic-gate 	 * Use 0 as lport means that rresvport() will bind to a port in
144*7c478bd9Sstevel@tonic-gate 	 * the anonymous priviledged port range.
145*7c478bd9Sstevel@tonic-gate 	 */
146*7c478bd9Sstevel@tonic-gate 	lport = 0;
147*7c478bd9Sstevel@tonic-gate 	for (;;) {
148*7c478bd9Sstevel@tonic-gate 		s = rresvport(&lport);
149*7c478bd9Sstevel@tonic-gate 		if (s < 0) {
150*7c478bd9Sstevel@tonic-gate 			int err;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 			if (errno == EAGAIN) {
153*7c478bd9Sstevel@tonic-gate 				(void) snprintf(myrcmd_stderr,
154*7c478bd9Sstevel@tonic-gate 				    sizeof (myrcmd_stderr),
155*7c478bd9Sstevel@tonic-gate 				    gettext("socket: All ports in use\n"));
156*7c478bd9Sstevel@tonic-gate 				err = MYRCMD_ENOPORT;
157*7c478bd9Sstevel@tonic-gate 			} else {
158*7c478bd9Sstevel@tonic-gate 				saverr = errno;
159*7c478bd9Sstevel@tonic-gate 				(void) snprintf(myrcmd_stderr,
160*7c478bd9Sstevel@tonic-gate 				    sizeof (myrcmd_stderr),
161*7c478bd9Sstevel@tonic-gate 				    gettext("rcmd: socket: %s\n"),
162*7c478bd9Sstevel@tonic-gate 				    strerror(saverr));
163*7c478bd9Sstevel@tonic-gate 				err = MYRCMD_ENOSOCK;
164*7c478bd9Sstevel@tonic-gate 			}
165*7c478bd9Sstevel@tonic-gate 			/* restore original SIGPIPE handler */
166*7c478bd9Sstevel@tonic-gate 			(void) sigaction(SIGPIPE, &oldaction,
167*7c478bd9Sstevel@tonic-gate 			    (struct sigaction *)0);
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 			/* restore original signal mask */
170*7c478bd9Sstevel@tonic-gate 			(void) sigprocmask(SIG_SETMASK, &oldmask,
171*7c478bd9Sstevel@tonic-gate 			    (sigset_t *)0);
172*7c478bd9Sstevel@tonic-gate 			return (err);
173*7c478bd9Sstevel@tonic-gate 		}
174*7c478bd9Sstevel@tonic-gate 		/* Can't fail, according to fcntl(2) */
175*7c478bd9Sstevel@tonic-gate 		(void) fcntl(s, F_SETOWN, pid);
176*7c478bd9Sstevel@tonic-gate 		sin.sin_family = hp->h_addrtype;
177*7c478bd9Sstevel@tonic-gate 		bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr, hp->h_length);
178*7c478bd9Sstevel@tonic-gate 		sin.sin_port = rport;
179*7c478bd9Sstevel@tonic-gate 		if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0)
180*7c478bd9Sstevel@tonic-gate 			break;
181*7c478bd9Sstevel@tonic-gate 		saverr = errno;
182*7c478bd9Sstevel@tonic-gate 		(void) close(s);
183*7c478bd9Sstevel@tonic-gate 		if (saverr == EADDRINUSE) {
184*7c478bd9Sstevel@tonic-gate 			continue;
185*7c478bd9Sstevel@tonic-gate 		}
186*7c478bd9Sstevel@tonic-gate 		if (saverr == ECONNREFUSED && timo <= 16) {
187*7c478bd9Sstevel@tonic-gate 			sleep(timo);
188*7c478bd9Sstevel@tonic-gate 			timo *= 2;
189*7c478bd9Sstevel@tonic-gate 			continue;
190*7c478bd9Sstevel@tonic-gate 		}
191*7c478bd9Sstevel@tonic-gate 		if (hp->h_addr_list[1] != NULL) {
192*7c478bd9Sstevel@tonic-gate 			saverr = errno;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
195*7c478bd9Sstevel@tonic-gate 			    gettext("connect to address %s: "),
196*7c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin.sin_addr));
197*7c478bd9Sstevel@tonic-gate 			errno = saverr;
198*7c478bd9Sstevel@tonic-gate 			perror(0);
199*7c478bd9Sstevel@tonic-gate 			hp->h_addr_list++;
200*7c478bd9Sstevel@tonic-gate 			bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr,
201*7c478bd9Sstevel@tonic-gate 			    hp->h_length);
202*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Trying %s...\n"),
203*7c478bd9Sstevel@tonic-gate 				inet_ntoa(sin.sin_addr));
204*7c478bd9Sstevel@tonic-gate 			continue;
205*7c478bd9Sstevel@tonic-gate 		}
206*7c478bd9Sstevel@tonic-gate 		(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
207*7c478bd9Sstevel@tonic-gate 		    "%s: %s\n", hp->h_name, strerror(saverr));
208*7c478bd9Sstevel@tonic-gate 		/* restore original SIGPIPE handler */
209*7c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGPIPE, &oldaction,
210*7c478bd9Sstevel@tonic-gate 		    (struct sigaction *)0);
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 		/* restore original signal mask */
213*7c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
214*7c478bd9Sstevel@tonic-gate 		return (MYRCMD_ENOCONNECT);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	if (write(s, "", 1) < 0) {
217*7c478bd9Sstevel@tonic-gate 		(void) close(s);
218*7c478bd9Sstevel@tonic-gate 		return (MYRCMD_ENOCONNECT);
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	loclen = strlen(locuser) + 1;
222*7c478bd9Sstevel@tonic-gate 	remlen = strlen(remuser) + 1;
223*7c478bd9Sstevel@tonic-gate 	cmdlen = strlen(cmd) + 1;
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	if (((retval = write(s, locuser, loclen)) != loclen) ||
226*7c478bd9Sstevel@tonic-gate 	    ((retval = write(s, remuser, remlen)) != remlen) ||
227*7c478bd9Sstevel@tonic-gate 	    ((retval = write(s, cmd, cmdlen)) != cmdlen)) {
228*7c478bd9Sstevel@tonic-gate 		if (retval == -1)
229*7c478bd9Sstevel@tonic-gate 			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
230*7c478bd9Sstevel@tonic-gate 			    "write: %s\n", strerror(errno));
231*7c478bd9Sstevel@tonic-gate 		else
232*7c478bd9Sstevel@tonic-gate 			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
233*7c478bd9Sstevel@tonic-gate 			    gettext("write unexpectedly truncated\n"));
234*7c478bd9Sstevel@tonic-gate 		goto bad;
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 	retval = read(s, &c, 1);
237*7c478bd9Sstevel@tonic-gate 	if (retval != 1) {
238*7c478bd9Sstevel@tonic-gate 		if (retval == 0) {
239*7c478bd9Sstevel@tonic-gate 			/*
240*7c478bd9Sstevel@tonic-gate 			 * Solaris 2.0 bug alert.  Sometimes, if the
241*7c478bd9Sstevel@tonic-gate 			 * tapehost is a Solaris 2.0 system, the connection
242*7c478bd9Sstevel@tonic-gate 			 * will be dropped at this point.  Let's try again,
243*7c478bd9Sstevel@tonic-gate 			 * three times, before we throw in the towel.
244*7c478bd9Sstevel@tonic-gate 			 */
245*7c478bd9Sstevel@tonic-gate 			if (++tries < 3) {
246*7c478bd9Sstevel@tonic-gate 				(void) close(s);
247*7c478bd9Sstevel@tonic-gate 				goto again;
248*7c478bd9Sstevel@tonic-gate 			}
249*7c478bd9Sstevel@tonic-gate 			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
250*7c478bd9Sstevel@tonic-gate 			    gettext("Protocol error, %s closed connection\n"),
251*7c478bd9Sstevel@tonic-gate 			    *ahost);
252*7c478bd9Sstevel@tonic-gate 		} else if (retval < 0) {
253*7c478bd9Sstevel@tonic-gate 			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
254*7c478bd9Sstevel@tonic-gate 			    "%s: %s\n", *ahost, strerror(errno));
255*7c478bd9Sstevel@tonic-gate 		} else {
256*7c478bd9Sstevel@tonic-gate 			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
257*7c478bd9Sstevel@tonic-gate 			    gettext("Protocol error, %s sent %d bytes\n"),
258*7c478bd9Sstevel@tonic-gate 			    *ahost, retval);
259*7c478bd9Sstevel@tonic-gate 		}
260*7c478bd9Sstevel@tonic-gate 		goto bad;
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate 	if (c != 0) {
263*7c478bd9Sstevel@tonic-gate 		char *cp = myrcmd_stderr;
264*7c478bd9Sstevel@tonic-gate 		char *ecp = &myrcmd_stderr[sizeof (myrcmd_stderr) - 1];
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 		while (read(s, &c, 1) == 1) {
267*7c478bd9Sstevel@tonic-gate 			*cp++ = c;
268*7c478bd9Sstevel@tonic-gate 			if (c == '\n' || cp >= ecp)
269*7c478bd9Sstevel@tonic-gate 				break;
270*7c478bd9Sstevel@tonic-gate 		}
271*7c478bd9Sstevel@tonic-gate 		*cp = '\0';
272*7c478bd9Sstevel@tonic-gate 		goto bad;
273*7c478bd9Sstevel@tonic-gate 	}
274*7c478bd9Sstevel@tonic-gate 	/* restore original SIGPIPE handler */
275*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPIPE, &oldaction, (struct sigaction *)0);
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	/* restore original signal mask */
278*7c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
279*7c478bd9Sstevel@tonic-gate 	return (s);
280*7c478bd9Sstevel@tonic-gate bad:
281*7c478bd9Sstevel@tonic-gate 	(void) close(s);
282*7c478bd9Sstevel@tonic-gate 	/* restore original SIGPIPE handler */
283*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPIPE, &oldaction, (struct sigaction *)0);
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 	/* restore original signal mask */
286*7c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
287*7c478bd9Sstevel@tonic-gate 	return (MYRCMD_EBAD);
288*7c478bd9Sstevel@tonic-gate }
289