xref: /titanic_50/usr/src/lib/libsocket/inet/rexec.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #pragma ident	"%Z%%M%	%I%	%E% SMI"
40 
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 
44 #include <netinet/in.h>
45 
46 #include <stdio.h>
47 #include <netdb.h>
48 #include <errno.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <libintl.h>
53 
54 #ifdef SYSV
55 #define	bcopy(a, b, c)	(void) memcpy((b), (a), (c))
56 #endif
57 
58 #define	MAX_SHORTSTRLEN 6
59 
60 extern char *_dgettext();
61 
62 void _ruserpass(const char *host, char **aname, char **apass);
63 
64 int rexec(char **ahost, unsigned short rport, const char *name,
65     const char *pass, const char *cmd, int *fd2p)
66 {
67 		return (rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET));
68 }
69 
70 int rexec_af(char **ahost, unsigned short rport, const char *name,
71     const char *pass, const char *cmd, int *fd2p, int af)
72 {
73 	int s, timo = 1, s3;
74 	char c;
75 	ushort_t port;
76 	static char hostname[MAXHOSTNAMELEN];
77 	int rc;
78 	struct addrinfo *res;
79 	struct addrinfo hints;
80 	char aport[MAX_SHORTSTRLEN];
81 
82 	if (!(af == AF_INET || af == AF_INET6 || af == AF_UNSPEC)) {
83 		(void) fprintf(stderr,
84 		    _dgettext(TEXT_DOMAIN, "%d: Address family not "
85 		    "supported\n"), af);
86 		errno = EAFNOSUPPORT;
87 		return (-1);
88 	}
89 	memset(&hints, 0, sizeof (hints));
90 	(void) snprintf(aport, MAX_SHORTSTRLEN, "%u", ntohs(rport));
91 	hints.ai_flags = AI_CANONNAME|AI_ADDRCONFIG|AI_V4MAPPED;
92 	hints.ai_socktype = SOCK_STREAM;
93 	hints.ai_family = af;
94 	rc = getaddrinfo(*ahost, aport, &hints, &res);
95 
96 	if (rc != 0) {
97 		(void) fprintf(stderr,
98 		    _dgettext(TEXT_DOMAIN, "%s: unknown host\n"),
99 		    *ahost);
100 		return (-1);
101 	}
102 	(void) strlcpy(hostname, res->ai_canonname, MAXHOSTNAMELEN);
103 	*ahost = hostname;
104 	_ruserpass(res->ai_canonname, (char **)&name, (char **)&pass);
105 retry:
106 	s = socket(res->ai_addr->sa_family, res->ai_socktype, res->ai_protocol);
107 	if (s < 0) {
108 		perror("rexec: socket");
109 		freeaddrinfo(res);
110 		return (-1);
111 	}
112 	if (connect(s, res->ai_addr, res->ai_addrlen) != 0) {
113 		if (errno == ECONNREFUSED && timo <= 16) {
114 			(void) close(s);
115 			(void) sleep(timo);
116 			timo *= 2;
117 			goto retry;
118 		}
119 		perror(*ahost);
120 		(void) close(s);
121 		freeaddrinfo(res);
122 		return (-1);
123 	}
124 	if (fd2p == 0) {
125 		(void) write(s, "", 1);
126 		port = 0;
127 	} else {
128 		int s2;
129 		socklen_t sin2len;
130 		struct sockaddr_storage sin2, from;
131 
132 		s2 = socket(res->ai_family, SOCK_STREAM, 0);
133 		if (s2 < 0) {
134 			(void) close(s);
135 			freeaddrinfo(res);
136 			return (-1);
137 		}
138 		(void) listen(s2, 1);
139 		sin2len = (socklen_t)sizeof (sin2);
140 		if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0) {
141 			perror("getsockname");
142 			(void) close(s2);
143 			goto bad;
144 		}
145 		if (res->ai_family == AF_INET6) {
146 			port = ntohs(((struct sockaddr_in6 *)&sin2)->sin6_port);
147 		} else {
148 			port = ntohs(((struct sockaddr_in *)&sin2)->sin_port);
149 		}
150 		(void) snprintf(aport, MAX_SHORTSTRLEN, "%u", port);
151 		(void) write(s, aport, strlen(aport)+1);
152 		{
153 			socklen_t len = (socklen_t)sizeof (from);
154 			s3 = accept(s2, (struct sockaddr *)&from, &len);
155 			(void) close(s2);
156 			if (s3 < 0) {
157 				perror("accept");
158 				port = 0;
159 				goto bad;
160 			}
161 		}
162 		*fd2p = s3;
163 	}
164 	(void) write(s, name, strlen(name) + 1);
165 	/* should public key encypt the password here */
166 	(void) write(s, pass, strlen(pass) + 1);
167 	(void) write(s, cmd, strlen(cmd) + 1);
168 	if (read(s, &c, 1) != 1) {
169 		perror(*ahost);
170 		goto bad;
171 	}
172 	if (c != 0) {
173 		while (read(s, &c, 1) == 1) {
174 			(void) write(2, &c, 1);
175 			if (c == '\n')
176 				break;
177 		}
178 		goto bad;
179 	}
180 	freeaddrinfo(res);
181 	return (s);
182 bad:
183 	if (port)
184 		(void) close(*fd2p);
185 	(void) close(s);
186 	freeaddrinfo(res);
187 	return (-1);
188 }
189