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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 #pragma ident "%Z%%M% %I% %E% SMI"
42 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
56 #define bcopy(a, b, c) (void) memcpy((b), (a), (c))
59 #define MAX_SHORTSTRLEN 6
61 void _ruserpass(const char *host
, char **aname
, char **apass
);
63 int rexec(char **ahost
, unsigned short rport
, const char *name
,
64 const char *pass
, const char *cmd
, int *fd2p
)
66 return (rexec_af(ahost
, rport
, name
, pass
, cmd
, fd2p
, AF_INET
));
69 int rexec_af(char **ahost
, unsigned short rport
, const char *name
,
70 const char *pass
, const char *cmd
, int *fd2p
, int af
)
75 static char hostname
[MAXHOSTNAMELEN
];
78 struct addrinfo hints
;
79 char aport
[MAX_SHORTSTRLEN
];
81 if (!(af
== AF_INET
|| af
== AF_INET6
|| af
== AF_UNSPEC
)) {
82 (void) fprintf(stderr
,
83 dgettext(TEXT_DOMAIN
, "%d: Address family not "
88 memset(&hints
, 0, sizeof (hints
));
89 (void) snprintf(aport
, MAX_SHORTSTRLEN
, "%u", ntohs(rport
));
90 hints
.ai_flags
= AI_CANONNAME
|AI_ADDRCONFIG
|AI_V4MAPPED
;
91 hints
.ai_socktype
= SOCK_STREAM
;
93 rc
= getaddrinfo(*ahost
, aport
, &hints
, &res
);
96 (void) fprintf(stderr
,
97 dgettext(TEXT_DOMAIN
, "%s: unknown host\n"),
101 (void) strlcpy(hostname
, res
->ai_canonname
, MAXHOSTNAMELEN
);
103 _ruserpass(res
->ai_canonname
, (char **)&name
, (char **)&pass
);
105 s
= socket(res
->ai_addr
->sa_family
, res
->ai_socktype
, res
->ai_protocol
);
107 perror("rexec: socket");
111 if (connect(s
, res
->ai_addr
, res
->ai_addrlen
) != 0) {
112 if (errno
== ECONNREFUSED
&& timo
<= 16) {
124 (void) write(s
, "", 1);
129 struct sockaddr_storage sin2
, from
;
131 s2
= socket(res
->ai_family
, SOCK_STREAM
, 0);
137 (void) listen(s2
, 1);
138 sin2len
= (socklen_t
)sizeof (sin2
);
139 if (getsockname(s2
, (struct sockaddr
*)&sin2
, &sin2len
) < 0) {
140 perror("getsockname");
144 if (res
->ai_family
== AF_INET6
) {
145 port
= ntohs(((struct sockaddr_in6
*)&sin2
)->sin6_port
);
147 port
= ntohs(((struct sockaddr_in
*)&sin2
)->sin_port
);
149 (void) snprintf(aport
, MAX_SHORTSTRLEN
, "%u", port
);
150 (void) write(s
, aport
, strlen(aport
)+1);
152 socklen_t len
= (socklen_t
)sizeof (from
);
153 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
163 (void) write(s
, name
, strlen(name
) + 1);
164 /* should public key encypt the password here */
165 (void) write(s
, pass
, strlen(pass
) + 1);
166 (void) write(s
, cmd
, strlen(cmd
) + 1);
167 if (read(s
, &c
, 1) != 1) {
172 while (read(s
, &c
, 1) == 1) {
173 (void) write(2, &c
, 1);