2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
45 # define __TEMP_FAILURE_RETRY(expression) \
47 ({ long int __result; \
48 do __result = (long int) (expression); \
49 while (__result == -1L && errno == EINTR); \
52 #define SA_LEN(_x) __libc_sa_len((_x)->sa_family)
53 extern int __libc_sa_len(sa_family_t __af
) __THROW attribute_hidden
;
55 /* int rexecoptions; - google does not know it */
56 static char ahostbuf
[NI_MAXHOST
];
59 rexec_af(char **ahost
, int rport
, const char *name
, const char *pass
, const char *cmd
, int *fd2p
, sa_family_t af
)
61 struct sockaddr_storage sa2
, from
;
62 struct addrinfo hints
, *res0
;
63 const char *orig_name
= name
;
64 const char *orig_pass
= pass
;
69 char servbuff
[NI_MAXSERV
];
71 if (sizeof(servbuff
) < sizeof(int)*3 + 2) {
72 snprintf(servbuff
, sizeof(servbuff
), "%d", ntohs(rport
));
73 servbuff
[sizeof(servbuff
) - 1] = '\0';
75 sprintf(servbuff
, "%d", ntohs(rport
));
78 memset(&hints
, '\0', sizeof(hints
));
80 hints
.ai_socktype
= SOCK_STREAM
;
81 hints
.ai_flags
= AI_CANONNAME
;
82 gai
= getaddrinfo(*ahost
, servbuff
, &hints
, &res0
);
88 if (res0
->ai_canonname
) {
89 strncpy(ahostbuf
, res0
->ai_canonname
, sizeof(ahostbuf
));
90 ahostbuf
[sizeof(ahostbuf
)-1] = '\0';
98 ruserpass(res0
->ai_canonname
, &name
, &pass
);
100 s
= socket(res0
->ai_family
, res0
->ai_socktype
, 0);
102 perror("rexec: socket");
105 if (connect(s
, res0
->ai_addr
, res0
->ai_addrlen
) < 0) {
106 if (errno
== ECONNREFUSED
&& timo
<= 16) {
112 perror(res0
->ai_canonname
);
116 (void) write(s
, "", 1);
123 s2
= socket(res0
->ai_family
, res0
->ai_socktype
, 0);
129 sa2len
= sizeof(sa2
);
130 if (getsockname(s2
, (struct sockaddr
*)&sa2
, &sa2len
) < 0) {
131 perror("getsockname");
134 } else if (sa2len
!= SA_LEN((struct sockaddr
*)&sa2
)) {
140 if (!getnameinfo((struct sockaddr
*)&sa2
, sa2len
,
141 NULL
, 0, servbuff
, sizeof(servbuff
),
143 port
= atoi(servbuff
);
144 (void) sprintf(num
, "%u", port
);
145 (void) write(s
, num
, strlen(num
)+1);
147 socklen_t len
= sizeof(from
);
148 s3
= __TEMP_FAILURE_RETRY(accept(s2
,
149 (struct sockaddr
*)&from
, &len
));
159 (void) write(s
, name
, strlen(name
) + 1);
160 /* should public key encypt the password here */
161 (void) write(s
, pass
, strlen(pass
) + 1);
162 (void) write(s
, cmd
, strlen(cmd
) + 1);
164 /* We don't need the memory allocated for the name and the password
165 in ruserpass anymore. */
166 if (name
!= orig_name
)
168 if (pass
!= orig_pass
)
171 if (read(s
, &c
, 1) != 1) {
176 while (read(s
, &c
, 1) == 1) {
177 (void) write(2, &c
, 1);
192 libc_hidden_def(rexec_af
)
195 rexec(char **ahost
, int rport
, const char *name
, const char *pass
,
196 const char *cmd
, int *fd2p
)
198 return rexec_af(ahost
, rport
, name
, pass
, cmd
, fd2p
, AF_INET
);