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 2003 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) 1984, 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 * University Copyright- Copyright (c) 1982, 1986, 1988 32*7c478bd9Sstevel@tonic-gate * The Regents of the University of California 33*7c478bd9Sstevel@tonic-gate * All Rights Reserved 34*7c478bd9Sstevel@tonic-gate * 35*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*7c478bd9Sstevel@tonic-gate * contributors. 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate #include <stdio.h> 48*7c478bd9Sstevel@tonic-gate #include <netdb.h> 49*7c478bd9Sstevel@tonic-gate #include <errno.h> 50*7c478bd9Sstevel@tonic-gate #include <unistd.h> 51*7c478bd9Sstevel@tonic-gate #include <string.h> 52*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 53*7c478bd9Sstevel@tonic-gate #include <libintl.h> 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate #ifdef SYSV 56*7c478bd9Sstevel@tonic-gate #define bcopy(a, b, c) (void) memcpy((b), (a), (c)) 57*7c478bd9Sstevel@tonic-gate #endif 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate #define MAX_SHORTSTRLEN 6 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate extern char *_dgettext(); 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate void _ruserpass(const char *host, char **aname, char **apass); 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate int rexec(char **ahost, unsigned short rport, const char *name, 66*7c478bd9Sstevel@tonic-gate const char *pass, const char *cmd, int *fd2p) 67*7c478bd9Sstevel@tonic-gate { 68*7c478bd9Sstevel@tonic-gate return (rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET)); 69*7c478bd9Sstevel@tonic-gate } 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate int rexec_af(char **ahost, unsigned short rport, const char *name, 72*7c478bd9Sstevel@tonic-gate const char *pass, const char *cmd, int *fd2p, int af) 73*7c478bd9Sstevel@tonic-gate { 74*7c478bd9Sstevel@tonic-gate int s, timo = 1, s3; 75*7c478bd9Sstevel@tonic-gate char c; 76*7c478bd9Sstevel@tonic-gate ushort_t port; 77*7c478bd9Sstevel@tonic-gate static char hostname[MAXHOSTNAMELEN]; 78*7c478bd9Sstevel@tonic-gate int rc; 79*7c478bd9Sstevel@tonic-gate struct addrinfo *res; 80*7c478bd9Sstevel@tonic-gate struct addrinfo hints; 81*7c478bd9Sstevel@tonic-gate char aport[MAX_SHORTSTRLEN]; 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate if (!(af == AF_INET || af == AF_INET6 || af == AF_UNSPEC)) { 84*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 85*7c478bd9Sstevel@tonic-gate _dgettext(TEXT_DOMAIN, "%d: Address family not " 86*7c478bd9Sstevel@tonic-gate "supported\n"), af); 87*7c478bd9Sstevel@tonic-gate errno = EAFNOSUPPORT; 88*7c478bd9Sstevel@tonic-gate return (-1); 89*7c478bd9Sstevel@tonic-gate } 90*7c478bd9Sstevel@tonic-gate memset(&hints, 0, sizeof (hints)); 91*7c478bd9Sstevel@tonic-gate (void) snprintf(aport, MAX_SHORTSTRLEN, "%u", ntohs(rport)); 92*7c478bd9Sstevel@tonic-gate hints.ai_flags = AI_CANONNAME|AI_ADDRCONFIG|AI_V4MAPPED; 93*7c478bd9Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM; 94*7c478bd9Sstevel@tonic-gate hints.ai_family = af; 95*7c478bd9Sstevel@tonic-gate rc = getaddrinfo(*ahost, aport, &hints, &res); 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate if (rc != 0) { 98*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 99*7c478bd9Sstevel@tonic-gate _dgettext(TEXT_DOMAIN, "%s: unknown host\n"), 100*7c478bd9Sstevel@tonic-gate *ahost); 101*7c478bd9Sstevel@tonic-gate return (-1); 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate (void) strlcpy(hostname, res->ai_canonname, MAXHOSTNAMELEN); 104*7c478bd9Sstevel@tonic-gate *ahost = hostname; 105*7c478bd9Sstevel@tonic-gate _ruserpass(res->ai_canonname, (char **)&name, (char **)&pass); 106*7c478bd9Sstevel@tonic-gate retry: 107*7c478bd9Sstevel@tonic-gate s = socket(res->ai_addr->sa_family, res->ai_socktype, res->ai_protocol); 108*7c478bd9Sstevel@tonic-gate if (s < 0) { 109*7c478bd9Sstevel@tonic-gate perror("rexec: socket"); 110*7c478bd9Sstevel@tonic-gate freeaddrinfo(res); 111*7c478bd9Sstevel@tonic-gate return (-1); 112*7c478bd9Sstevel@tonic-gate } 113*7c478bd9Sstevel@tonic-gate if (connect(s, res->ai_addr, res->ai_addrlen) != 0) { 114*7c478bd9Sstevel@tonic-gate if (errno == ECONNREFUSED && timo <= 16) { 115*7c478bd9Sstevel@tonic-gate (void) close(s); 116*7c478bd9Sstevel@tonic-gate (void) sleep(timo); 117*7c478bd9Sstevel@tonic-gate timo *= 2; 118*7c478bd9Sstevel@tonic-gate goto retry; 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate perror(*ahost); 121*7c478bd9Sstevel@tonic-gate (void) close(s); 122*7c478bd9Sstevel@tonic-gate freeaddrinfo(res); 123*7c478bd9Sstevel@tonic-gate return (-1); 124*7c478bd9Sstevel@tonic-gate } 125*7c478bd9Sstevel@tonic-gate if (fd2p == 0) { 126*7c478bd9Sstevel@tonic-gate (void) write(s, "", 1); 127*7c478bd9Sstevel@tonic-gate port = 0; 128*7c478bd9Sstevel@tonic-gate } else { 129*7c478bd9Sstevel@tonic-gate int s2; 130*7c478bd9Sstevel@tonic-gate socklen_t sin2len; 131*7c478bd9Sstevel@tonic-gate struct sockaddr_storage sin2, from; 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate s2 = socket(res->ai_family, SOCK_STREAM, 0); 134*7c478bd9Sstevel@tonic-gate if (s2 < 0) { 135*7c478bd9Sstevel@tonic-gate (void) close(s); 136*7c478bd9Sstevel@tonic-gate freeaddrinfo(res); 137*7c478bd9Sstevel@tonic-gate return (-1); 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate (void) listen(s2, 1); 140*7c478bd9Sstevel@tonic-gate sin2len = (socklen_t)sizeof (sin2); 141*7c478bd9Sstevel@tonic-gate if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0) { 142*7c478bd9Sstevel@tonic-gate perror("getsockname"); 143*7c478bd9Sstevel@tonic-gate (void) close(s2); 144*7c478bd9Sstevel@tonic-gate goto bad; 145*7c478bd9Sstevel@tonic-gate } 146*7c478bd9Sstevel@tonic-gate if (res->ai_family == AF_INET6) { 147*7c478bd9Sstevel@tonic-gate port = ((struct sockaddr_in6 *)&sin2)->sin6_port; 148*7c478bd9Sstevel@tonic-gate } else { 149*7c478bd9Sstevel@tonic-gate port = ((struct sockaddr_in *)&sin2)->sin_port; 150*7c478bd9Sstevel@tonic-gate } 151*7c478bd9Sstevel@tonic-gate (void) snprintf(aport, MAX_SHORTSTRLEN, "%u", port); 152*7c478bd9Sstevel@tonic-gate (void) write(s, aport, strlen(aport)+1); 153*7c478bd9Sstevel@tonic-gate { 154*7c478bd9Sstevel@tonic-gate socklen_t len = (socklen_t)sizeof (from); 155*7c478bd9Sstevel@tonic-gate s3 = accept(s2, (struct sockaddr *)&from, &len); 156*7c478bd9Sstevel@tonic-gate (void) close(s2); 157*7c478bd9Sstevel@tonic-gate if (s3 < 0) { 158*7c478bd9Sstevel@tonic-gate perror("accept"); 159*7c478bd9Sstevel@tonic-gate port = 0; 160*7c478bd9Sstevel@tonic-gate goto bad; 161*7c478bd9Sstevel@tonic-gate } 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate *fd2p = s3; 164*7c478bd9Sstevel@tonic-gate } 165*7c478bd9Sstevel@tonic-gate (void) write(s, name, strlen(name) + 1); 166*7c478bd9Sstevel@tonic-gate /* should public key encypt the password here */ 167*7c478bd9Sstevel@tonic-gate (void) write(s, pass, strlen(pass) + 1); 168*7c478bd9Sstevel@tonic-gate (void) write(s, cmd, strlen(cmd) + 1); 169*7c478bd9Sstevel@tonic-gate if (read(s, &c, 1) != 1) { 170*7c478bd9Sstevel@tonic-gate perror(*ahost); 171*7c478bd9Sstevel@tonic-gate goto bad; 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate if (c != 0) { 174*7c478bd9Sstevel@tonic-gate while (read(s, &c, 1) == 1) { 175*7c478bd9Sstevel@tonic-gate (void) write(2, &c, 1); 176*7c478bd9Sstevel@tonic-gate if (c == '\n') 177*7c478bd9Sstevel@tonic-gate break; 178*7c478bd9Sstevel@tonic-gate } 179*7c478bd9Sstevel@tonic-gate goto bad; 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate freeaddrinfo(res); 182*7c478bd9Sstevel@tonic-gate return (s); 183*7c478bd9Sstevel@tonic-gate bad: 184*7c478bd9Sstevel@tonic-gate if (port) 185*7c478bd9Sstevel@tonic-gate (void) close(*fd2p); 186*7c478bd9Sstevel@tonic-gate (void) close(s); 187*7c478bd9Sstevel@tonic-gate freeaddrinfo(res); 188*7c478bd9Sstevel@tonic-gate return (-1); 189*7c478bd9Sstevel@tonic-gate } 190