xref: /titanic_50/usr/src/lib/libsocket/inet/rexec.c (revision b760ebbf2958f42920969bccd6ccab1b129076a8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*b760ebbfSsp149894  * Common Development and Distribution License (the "License").
6*b760ebbfSsp149894  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b760ebbfSsp149894  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/socket.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <netinet/in.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <stdio.h>
477c478bd9Sstevel@tonic-gate #include <netdb.h>
487c478bd9Sstevel@tonic-gate #include <errno.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <stdlib.h>
527c478bd9Sstevel@tonic-gate #include <libintl.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifdef SYSV
557c478bd9Sstevel@tonic-gate #define	bcopy(a, b, c)	(void) memcpy((b), (a), (c))
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	MAX_SHORTSTRLEN 6
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate extern char *_dgettext();
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate void _ruserpass(const char *host, char **aname, char **apass);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate int rexec(char **ahost, unsigned short rport, const char *name,
657c478bd9Sstevel@tonic-gate     const char *pass, const char *cmd, int *fd2p)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 		return (rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET));
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int rexec_af(char **ahost, unsigned short rport, const char *name,
717c478bd9Sstevel@tonic-gate     const char *pass, const char *cmd, int *fd2p, int af)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	int s, timo = 1, s3;
747c478bd9Sstevel@tonic-gate 	char c;
757c478bd9Sstevel@tonic-gate 	ushort_t port;
767c478bd9Sstevel@tonic-gate 	static char hostname[MAXHOSTNAMELEN];
777c478bd9Sstevel@tonic-gate 	int rc;
787c478bd9Sstevel@tonic-gate 	struct addrinfo *res;
797c478bd9Sstevel@tonic-gate 	struct addrinfo hints;
807c478bd9Sstevel@tonic-gate 	char aport[MAX_SHORTSTRLEN];
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (!(af == AF_INET || af == AF_INET6 || af == AF_UNSPEC)) {
837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
847c478bd9Sstevel@tonic-gate 		    _dgettext(TEXT_DOMAIN, "%d: Address family not "
857c478bd9Sstevel@tonic-gate 		    "supported\n"), af);
867c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
877c478bd9Sstevel@tonic-gate 		return (-1);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	memset(&hints, 0, sizeof (hints));
907c478bd9Sstevel@tonic-gate 	(void) snprintf(aport, MAX_SHORTSTRLEN, "%u", ntohs(rport));
917c478bd9Sstevel@tonic-gate 	hints.ai_flags = AI_CANONNAME|AI_ADDRCONFIG|AI_V4MAPPED;
927c478bd9Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
937c478bd9Sstevel@tonic-gate 	hints.ai_family = af;
947c478bd9Sstevel@tonic-gate 	rc = getaddrinfo(*ahost, aport, &hints, &res);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (rc != 0) {
977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
987c478bd9Sstevel@tonic-gate 		    _dgettext(TEXT_DOMAIN, "%s: unknown host\n"),
997c478bd9Sstevel@tonic-gate 		    *ahost);
1007c478bd9Sstevel@tonic-gate 		return (-1);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	(void) strlcpy(hostname, res->ai_canonname, MAXHOSTNAMELEN);
1037c478bd9Sstevel@tonic-gate 	*ahost = hostname;
1047c478bd9Sstevel@tonic-gate 	_ruserpass(res->ai_canonname, (char **)&name, (char **)&pass);
1057c478bd9Sstevel@tonic-gate retry:
1067c478bd9Sstevel@tonic-gate 	s = socket(res->ai_addr->sa_family, res->ai_socktype, res->ai_protocol);
1077c478bd9Sstevel@tonic-gate 	if (s < 0) {
1087c478bd9Sstevel@tonic-gate 		perror("rexec: socket");
1097c478bd9Sstevel@tonic-gate 		freeaddrinfo(res);
1107c478bd9Sstevel@tonic-gate 		return (-1);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	if (connect(s, res->ai_addr, res->ai_addrlen) != 0) {
1137c478bd9Sstevel@tonic-gate 		if (errno == ECONNREFUSED && timo <= 16) {
1147c478bd9Sstevel@tonic-gate 			(void) close(s);
1157c478bd9Sstevel@tonic-gate 			(void) sleep(timo);
1167c478bd9Sstevel@tonic-gate 			timo *= 2;
1177c478bd9Sstevel@tonic-gate 			goto retry;
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 		perror(*ahost);
1207c478bd9Sstevel@tonic-gate 		(void) close(s);
1217c478bd9Sstevel@tonic-gate 		freeaddrinfo(res);
1227c478bd9Sstevel@tonic-gate 		return (-1);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	if (fd2p == 0) {
1257c478bd9Sstevel@tonic-gate 		(void) write(s, "", 1);
1267c478bd9Sstevel@tonic-gate 		port = 0;
1277c478bd9Sstevel@tonic-gate 	} else {
1287c478bd9Sstevel@tonic-gate 		int s2;
1297c478bd9Sstevel@tonic-gate 		socklen_t sin2len;
1307c478bd9Sstevel@tonic-gate 		struct sockaddr_storage sin2, from;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		s2 = socket(res->ai_family, SOCK_STREAM, 0);
1337c478bd9Sstevel@tonic-gate 		if (s2 < 0) {
1347c478bd9Sstevel@tonic-gate 			(void) close(s);
1357c478bd9Sstevel@tonic-gate 			freeaddrinfo(res);
1367c478bd9Sstevel@tonic-gate 			return (-1);
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 		(void) listen(s2, 1);
1397c478bd9Sstevel@tonic-gate 		sin2len = (socklen_t)sizeof (sin2);
1407c478bd9Sstevel@tonic-gate 		if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0) {
1417c478bd9Sstevel@tonic-gate 			perror("getsockname");
1427c478bd9Sstevel@tonic-gate 			(void) close(s2);
1437c478bd9Sstevel@tonic-gate 			goto bad;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		if (res->ai_family == AF_INET6) {
146*b760ebbfSsp149894 			port = ntohs(((struct sockaddr_in6 *)&sin2)->sin6_port);
1477c478bd9Sstevel@tonic-gate 		} else {
148*b760ebbfSsp149894 			port = ntohs(((struct sockaddr_in *)&sin2)->sin_port);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 		(void) snprintf(aport, MAX_SHORTSTRLEN, "%u", port);
1517c478bd9Sstevel@tonic-gate 		(void) write(s, aport, strlen(aport)+1);
1527c478bd9Sstevel@tonic-gate 		{
1537c478bd9Sstevel@tonic-gate 			socklen_t len = (socklen_t)sizeof (from);
1547c478bd9Sstevel@tonic-gate 			s3 = accept(s2, (struct sockaddr *)&from, &len);
1557c478bd9Sstevel@tonic-gate 			(void) close(s2);
1567c478bd9Sstevel@tonic-gate 			if (s3 < 0) {
1577c478bd9Sstevel@tonic-gate 				perror("accept");
1587c478bd9Sstevel@tonic-gate 				port = 0;
1597c478bd9Sstevel@tonic-gate 				goto bad;
1607c478bd9Sstevel@tonic-gate 			}
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 		*fd2p = s3;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	(void) write(s, name, strlen(name) + 1);
1657c478bd9Sstevel@tonic-gate 	/* should public key encypt the password here */
1667c478bd9Sstevel@tonic-gate 	(void) write(s, pass, strlen(pass) + 1);
1677c478bd9Sstevel@tonic-gate 	(void) write(s, cmd, strlen(cmd) + 1);
1687c478bd9Sstevel@tonic-gate 	if (read(s, &c, 1) != 1) {
1697c478bd9Sstevel@tonic-gate 		perror(*ahost);
1707c478bd9Sstevel@tonic-gate 		goto bad;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	if (c != 0) {
1737c478bd9Sstevel@tonic-gate 		while (read(s, &c, 1) == 1) {
1747c478bd9Sstevel@tonic-gate 			(void) write(2, &c, 1);
1757c478bd9Sstevel@tonic-gate 			if (c == '\n')
1767c478bd9Sstevel@tonic-gate 				break;
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 		goto bad;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 	freeaddrinfo(res);
1817c478bd9Sstevel@tonic-gate 	return (s);
1827c478bd9Sstevel@tonic-gate bad:
1837c478bd9Sstevel@tonic-gate 	if (port)
1847c478bd9Sstevel@tonic-gate 		(void) close(*fd2p);
1857c478bd9Sstevel@tonic-gate 	(void) close(s);
1867c478bd9Sstevel@tonic-gate 	freeaddrinfo(res);
1877c478bd9Sstevel@tonic-gate 	return (-1);
1887c478bd9Sstevel@tonic-gate }
189