dynamically allocate ahostbuf buffer
[uclibc-ng.git] / libc / inet / rpc / rexec.c
blobe17be50b2cbb50131d351620a5eb751ac9698685
1 /*
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
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.
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.
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
35 #include <alloca.h>
36 #include <stdio.h>
37 #include <netdb.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <malloc.h>
44 #define SA_LEN(_x) __libc_sa_len((_x)->sa_family)
45 extern int __libc_sa_len(sa_family_t __af) __THROW attribute_hidden;
47 /* int rexecoptions; - google does not know it */
48 static char *ahostbuf = NULL;
50 int
51 rexec_af(char **ahost, int rport, const char *name, const char *pass, const char *cmd, int *fd2p, sa_family_t af)
53 struct sockaddr_storage sa2, from;
54 struct addrinfo hints, *res0;
55 const char *orig_name = name;
56 const char *orig_pass = pass;
57 u_short port = 0;
58 int s, timo = 1, s3;
59 char c;
60 int gai;
61 char servbuff[NI_MAXSERV];
63 if (sizeof(servbuff) < sizeof(int)*3 + 2) {
64 snprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport));
65 servbuff[sizeof(servbuff) - 1] = '\0';
66 } else {
67 sprintf(servbuff, "%d", ntohs(rport));
70 memset(&hints, '\0', sizeof(hints));
71 hints.ai_family = af;
72 hints.ai_socktype = SOCK_STREAM;
73 hints.ai_flags = AI_CANONNAME;
74 gai = getaddrinfo(*ahost, servbuff, &hints, &res0);
75 if (gai) {
76 /* XXX: set errno? */
77 return -1;
80 if (res0->ai_canonname) {
81 if (!ahostbuf)
82 ahostbuf = __uc_malloc(NI_MAXHOST);
83 strncpy(ahostbuf, res0->ai_canonname, NI_MAXHOST);
84 ahostbuf[NI_MAXHOST-1] = '\0';
85 *ahost = ahostbuf;
87 else {
88 *ahost = NULL;
89 __set_errno(ENOENT);
90 return -1;
92 ruserpass(res0->ai_canonname, &name, &pass);
93 retry:
94 s = socket(res0->ai_family, res0->ai_socktype, 0);
95 if (s < 0) {
96 perror("rexec: socket");
97 return -1;
99 if (connect(s, res0->ai_addr, res0->ai_addrlen) < 0) {
100 if (errno == ECONNREFUSED && timo <= 16) {
101 (void) close(s);
102 sleep(timo);
103 timo *= 2;
104 goto retry;
106 perror(res0->ai_canonname);
107 return -1;
109 if (fd2p == 0) {
110 (void) write(s, "", 1);
111 port = 0;
112 } else {
113 char num[32];
114 int s2;
115 socklen_t sa2len;
117 s2 = socket(res0->ai_family, res0->ai_socktype, 0);
118 if (s2 < 0) {
119 (void) close(s);
120 return -1;
122 listen(s2, 1);
123 sa2len = sizeof(sa2);
124 if (getsockname(s2, (struct sockaddr *)&sa2, &sa2len) < 0) {
125 perror("getsockname");
126 (void) close(s2);
127 goto bad;
128 } else if (sa2len != SA_LEN((struct sockaddr *)&sa2)) {
129 __set_errno(EINVAL);
130 (void) close(s2);
131 goto bad;
133 port = 0;
134 if (!getnameinfo((struct sockaddr *)&sa2, sa2len,
135 NULL, 0, servbuff, sizeof(servbuff),
136 NI_NUMERICSERV))
137 port = atoi(servbuff);
138 (void) sprintf(num, "%u", port);
139 (void) write(s, num, strlen(num)+1);
141 socklen_t len = sizeof(from);
142 s3 = TEMP_FAILURE_RETRY(accept(s2,
143 (struct sockaddr *)&from, &len));
144 close(s2);
145 if (s3 < 0) {
146 perror("accept");
147 port = 0;
148 goto bad;
151 *fd2p = s3;
153 (void) write(s, name, strlen(name) + 1);
154 /* should public key encypt the password here */
155 (void) write(s, pass, strlen(pass) + 1);
156 (void) write(s, cmd, strlen(cmd) + 1);
158 /* We don't need the memory allocated for the name and the password
159 in ruserpass anymore. */
160 if (name != orig_name)
161 free((char *) name);
162 if (pass != orig_pass)
163 free((char *) pass);
165 if (read(s, &c, 1) != 1) {
166 perror(*ahost);
167 goto bad;
169 if (c != 0) {
170 while (read(s, &c, 1) == 1) {
171 (void) write(2, &c, 1);
172 if (c == '\n')
173 break;
175 goto bad;
177 freeaddrinfo(res0);
178 return s;
179 bad:
180 if (port)
181 (void) close(*fd2p);
182 (void) close(s);
183 freeaddrinfo(res0);
184 return -1;
186 libc_hidden_def(rexec_af)
189 rexec(char **ahost, int rport, const char *name, const char *pass,
190 const char *cmd, int *fd2p)
192 return rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET);