10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52863Ssp149894 * Common Development and Distribution License (the "License").
62863Ssp149894 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*6812Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/socket.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate #include <netinet/in.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate #include <stdio.h>
480Sstevel@tonic-gate #include <netdb.h>
490Sstevel@tonic-gate #include <errno.h>
500Sstevel@tonic-gate #include <unistd.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <stdlib.h>
530Sstevel@tonic-gate #include <libintl.h>
540Sstevel@tonic-gate
550Sstevel@tonic-gate #ifdef SYSV
560Sstevel@tonic-gate #define bcopy(a, b, c) (void) memcpy((b), (a), (c))
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate
590Sstevel@tonic-gate #define MAX_SHORTSTRLEN 6
600Sstevel@tonic-gate
610Sstevel@tonic-gate void _ruserpass(const char *host, char **aname, char **apass);
620Sstevel@tonic-gate
rexec(char ** ahost,unsigned short rport,const char * name,const char * pass,const char * cmd,int * fd2p)630Sstevel@tonic-gate int rexec(char **ahost, unsigned short rport, const char *name,
640Sstevel@tonic-gate const char *pass, const char *cmd, int *fd2p)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate return (rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET));
670Sstevel@tonic-gate }
680Sstevel@tonic-gate
rexec_af(char ** ahost,unsigned short rport,const char * name,const char * pass,const char * cmd,int * fd2p,int af)690Sstevel@tonic-gate int rexec_af(char **ahost, unsigned short rport, const char *name,
700Sstevel@tonic-gate const char *pass, const char *cmd, int *fd2p, int af)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate int s, timo = 1, s3;
730Sstevel@tonic-gate char c;
740Sstevel@tonic-gate ushort_t port;
750Sstevel@tonic-gate static char hostname[MAXHOSTNAMELEN];
760Sstevel@tonic-gate int rc;
770Sstevel@tonic-gate struct addrinfo *res;
780Sstevel@tonic-gate struct addrinfo hints;
790Sstevel@tonic-gate char aport[MAX_SHORTSTRLEN];
800Sstevel@tonic-gate
810Sstevel@tonic-gate if (!(af == AF_INET || af == AF_INET6 || af == AF_UNSPEC)) {
820Sstevel@tonic-gate (void) fprintf(stderr,
83*6812Sraf dgettext(TEXT_DOMAIN, "%d: Address family not "
840Sstevel@tonic-gate "supported\n"), af);
850Sstevel@tonic-gate errno = EAFNOSUPPORT;
860Sstevel@tonic-gate return (-1);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate memset(&hints, 0, sizeof (hints));
890Sstevel@tonic-gate (void) snprintf(aport, MAX_SHORTSTRLEN, "%u", ntohs(rport));
900Sstevel@tonic-gate hints.ai_flags = AI_CANONNAME|AI_ADDRCONFIG|AI_V4MAPPED;
910Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM;
920Sstevel@tonic-gate hints.ai_family = af;
930Sstevel@tonic-gate rc = getaddrinfo(*ahost, aport, &hints, &res);
940Sstevel@tonic-gate
950Sstevel@tonic-gate if (rc != 0) {
960Sstevel@tonic-gate (void) fprintf(stderr,
97*6812Sraf dgettext(TEXT_DOMAIN, "%s: unknown host\n"),
980Sstevel@tonic-gate *ahost);
990Sstevel@tonic-gate return (-1);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate (void) strlcpy(hostname, res->ai_canonname, MAXHOSTNAMELEN);
1020Sstevel@tonic-gate *ahost = hostname;
1030Sstevel@tonic-gate _ruserpass(res->ai_canonname, (char **)&name, (char **)&pass);
1040Sstevel@tonic-gate retry:
1050Sstevel@tonic-gate s = socket(res->ai_addr->sa_family, res->ai_socktype, res->ai_protocol);
1060Sstevel@tonic-gate if (s < 0) {
1070Sstevel@tonic-gate perror("rexec: socket");
1080Sstevel@tonic-gate freeaddrinfo(res);
1090Sstevel@tonic-gate return (-1);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate if (connect(s, res->ai_addr, res->ai_addrlen) != 0) {
1120Sstevel@tonic-gate if (errno == ECONNREFUSED && timo <= 16) {
1130Sstevel@tonic-gate (void) close(s);
1140Sstevel@tonic-gate (void) sleep(timo);
1150Sstevel@tonic-gate timo *= 2;
1160Sstevel@tonic-gate goto retry;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate perror(*ahost);
1190Sstevel@tonic-gate (void) close(s);
1200Sstevel@tonic-gate freeaddrinfo(res);
1210Sstevel@tonic-gate return (-1);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate if (fd2p == 0) {
1240Sstevel@tonic-gate (void) write(s, "", 1);
1250Sstevel@tonic-gate port = 0;
1260Sstevel@tonic-gate } else {
1270Sstevel@tonic-gate int s2;
1280Sstevel@tonic-gate socklen_t sin2len;
1290Sstevel@tonic-gate struct sockaddr_storage sin2, from;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate s2 = socket(res->ai_family, SOCK_STREAM, 0);
1320Sstevel@tonic-gate if (s2 < 0) {
1330Sstevel@tonic-gate (void) close(s);
1340Sstevel@tonic-gate freeaddrinfo(res);
1350Sstevel@tonic-gate return (-1);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate (void) listen(s2, 1);
1380Sstevel@tonic-gate sin2len = (socklen_t)sizeof (sin2);
1390Sstevel@tonic-gate if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0) {
1400Sstevel@tonic-gate perror("getsockname");
1410Sstevel@tonic-gate (void) close(s2);
1420Sstevel@tonic-gate goto bad;
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate if (res->ai_family == AF_INET6) {
1452863Ssp149894 port = ntohs(((struct sockaddr_in6 *)&sin2)->sin6_port);
1460Sstevel@tonic-gate } else {
1472863Ssp149894 port = ntohs(((struct sockaddr_in *)&sin2)->sin_port);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate (void) snprintf(aport, MAX_SHORTSTRLEN, "%u", port);
1500Sstevel@tonic-gate (void) write(s, aport, strlen(aport)+1);
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate socklen_t len = (socklen_t)sizeof (from);
1530Sstevel@tonic-gate s3 = accept(s2, (struct sockaddr *)&from, &len);
1540Sstevel@tonic-gate (void) close(s2);
1550Sstevel@tonic-gate if (s3 < 0) {
1560Sstevel@tonic-gate perror("accept");
1570Sstevel@tonic-gate port = 0;
1580Sstevel@tonic-gate goto bad;
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate *fd2p = s3;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate (void) write(s, name, strlen(name) + 1);
1640Sstevel@tonic-gate /* should public key encypt the password here */
1650Sstevel@tonic-gate (void) write(s, pass, strlen(pass) + 1);
1660Sstevel@tonic-gate (void) write(s, cmd, strlen(cmd) + 1);
1670Sstevel@tonic-gate if (read(s, &c, 1) != 1) {
1680Sstevel@tonic-gate perror(*ahost);
1690Sstevel@tonic-gate goto bad;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate if (c != 0) {
1720Sstevel@tonic-gate while (read(s, &c, 1) == 1) {
1730Sstevel@tonic-gate (void) write(2, &c, 1);
1740Sstevel@tonic-gate if (c == '\n')
1750Sstevel@tonic-gate break;
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate goto bad;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate freeaddrinfo(res);
1800Sstevel@tonic-gate return (s);
1810Sstevel@tonic-gate bad:
1820Sstevel@tonic-gate if (port)
1830Sstevel@tonic-gate (void) close(*fd2p);
1840Sstevel@tonic-gate (void) close(s);
1850Sstevel@tonic-gate freeaddrinfo(res);
1860Sstevel@tonic-gate return (-1);
1870Sstevel@tonic-gate }
188