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