do not rely on libc providing TEMP_FAILURE_RETRY
[project/librpc-uclibc.git] / rexec.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #define __FORCE_GLIBC
31 #include <features.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34
35 #include <netinet/in.h>
36
37 #include <alloca.h>
38 #include <stdio.h>
39 #include <netdb.h>
40 #include <errno.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 # define __TEMP_FAILURE_RETRY(expression) \
46   (__extension__                                                              \
47     ({ long int __result;                                                     \
48        do __result = (long int) (expression);                                 \
49        while (__result == -1L && errno == EINTR);                             \
50        __result; }))
51
52 #define SA_LEN(_x)      __libc_sa_len((_x)->sa_family)
53 extern int __libc_sa_len(sa_family_t __af) __THROW attribute_hidden;
54
55 /* int rexecoptions; - google does not know it */
56 static char ahostbuf[NI_MAXHOST];
57
58 int
59 rexec_af(char **ahost, int rport, const char *name, const char *pass, const char *cmd, int *fd2p, sa_family_t af)
60 {
61         struct sockaddr_storage sa2, from;
62         struct addrinfo hints, *res0;
63         const char *orig_name = name;
64         const char *orig_pass = pass;
65         u_short port = 0;
66         int s, timo = 1, s3;
67         char c;
68         int gai;
69         char servbuff[NI_MAXSERV];
70
71         if (sizeof(servbuff) < sizeof(int)*3 + 2) {
72                 snprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport));
73                 servbuff[sizeof(servbuff) - 1] = '\0';
74         } else {
75                 sprintf(servbuff, "%d", ntohs(rport));
76         }
77
78         memset(&hints, '\0', sizeof(hints));
79         hints.ai_family = af;
80         hints.ai_socktype = SOCK_STREAM;
81         hints.ai_flags = AI_CANONNAME;
82         gai = getaddrinfo(*ahost, servbuff, &hints, &res0);
83         if (gai) {
84                 /* XXX: set errno? */
85                 return -1;
86         }
87
88         if (res0->ai_canonname) {
89                 strncpy(ahostbuf, res0->ai_canonname, sizeof(ahostbuf));
90                 ahostbuf[sizeof(ahostbuf)-1] = '\0';
91                 *ahost = ahostbuf;
92         }
93         else {
94                 *ahost = NULL;
95                 __set_errno(ENOENT);
96                 return -1;
97         }
98         ruserpass(res0->ai_canonname, &name, &pass);
99 retry:
100         s = socket(res0->ai_family, res0->ai_socktype, 0);
101         if (s < 0) {
102                 perror("rexec: socket");
103                 return -1;
104         }
105         if (connect(s, res0->ai_addr, res0->ai_addrlen) < 0) {
106                 if (errno == ECONNREFUSED && timo <= 16) {
107                         (void) close(s);
108                         sleep(timo);
109                         timo *= 2;
110                         goto retry;
111                 }
112                 perror(res0->ai_canonname);
113                 return -1;
114         }
115         if (fd2p == 0) {
116                 (void) write(s, "", 1);
117                 port = 0;
118         } else {
119                 char num[32];
120                 int s2;
121                 socklen_t sa2len;
122
123                 s2 = socket(res0->ai_family, res0->ai_socktype, 0);
124                 if (s2 < 0) {
125                         (void) close(s);
126                         return -1;
127                 }
128                 listen(s2, 1);
129                 sa2len = sizeof(sa2);
130                 if (getsockname(s2, (struct sockaddr *)&sa2, &sa2len) < 0) {
131                         perror("getsockname");
132                         (void) close(s2);
133                         goto bad;
134                 } else if (sa2len != SA_LEN((struct sockaddr *)&sa2)) {
135                         __set_errno(EINVAL);
136                         (void) close(s2);
137                         goto bad;
138                 }
139                 port = 0;
140                 if (!getnameinfo((struct sockaddr *)&sa2, sa2len,
141                                  NULL, 0, servbuff, sizeof(servbuff),
142                                  NI_NUMERICSERV))
143                         port = atoi(servbuff);
144                 (void) sprintf(num, "%u", port);
145                 (void) write(s, num, strlen(num)+1);
146                 {
147                         socklen_t len = sizeof(from);
148                         s3 = __TEMP_FAILURE_RETRY(accept(s2,
149                                         (struct sockaddr *)&from, &len));
150                         close(s2);
151                         if (s3 < 0) {
152                                 perror("accept");
153                                 port = 0;
154                                 goto bad;
155                         }
156                 }
157                 *fd2p = s3;
158         }
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);
163
164         /* We don't need the memory allocated for the name and the password
165            in ruserpass anymore.  */
166         if (name != orig_name)
167                 free((char *) name);
168         if (pass != orig_pass)
169                 free((char *) pass);
170
171         if (read(s, &c, 1) != 1) {
172                 perror(*ahost);
173                 goto bad;
174         }
175         if (c != 0) {
176                 while (read(s, &c, 1) == 1) {
177                         (void) write(2, &c, 1);
178                         if (c == '\n')
179                                 break;
180                 }
181                 goto bad;
182         }
183         freeaddrinfo(res0);
184         return s;
185 bad:
186         if (port)
187                 (void) close(*fd2p);
188         (void) close(s);
189         freeaddrinfo(res0);
190         return -1;
191 }
192 libc_hidden_def(rexec_af)
193
194 int
195 rexec(char **ahost, int rport, const char *name, const char *pass,
196           const char *cmd, int *fd2p)
197 {
198         return rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET);
199 }