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