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