Deleted Added
full compact
1/*
2 * Copyright (c) 1983, 1993, 1994
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

--- 16 unchanged lines hidden (view full) ---

25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/lib/libc/net/rcmd.c 56636 2000-01-26 14:13:41Z shin $
33 * $FreeBSD: head/lib/libc/net/rcmd.c 56698 2000-01-27 23:07:25Z jasone $
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
38#endif /* LIBC_SCCS and not lint */
39
40#include <sys/param.h>
41#include <sys/socket.h>

--- 89 unchanged lines hidden (view full) ---

131 "rcmd: socket: All ports in use\n");
132 else
133 (void)fprintf(stderr, "rcmd: socket: %s\n",
134 strerror(errno));
135 sigsetmask(oldmask);
136 freeaddrinfo(res);
137 return (-1);
138 }
139 _libc_fcntl(s, F_SETOWN, pid);
139 _fcntl(s, F_SETOWN, pid);
140 if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
141 break;
142 (void)_libc_close(s);
142 (void)_close(s);
143 if (errno == EADDRINUSE) {
144 lport--;
145 continue;
146 }
147 if (errno == ECONNREFUSED)
148 refused = 1;
149 if (ai->ai_next != NULL) {
150 int oerrno = errno;

--- 10 unchanged lines hidden (view full) ---

161 getnameinfo(ai->ai_addr, ai->ai_addrlen,
162 paddr, sizeof(paddr),
163 NULL, 0,
164 NI_NUMERICHOST|NI_WITHSCOPEID);
165 fprintf(stderr, "Trying %s...\n", paddr);
166 continue;
167 }
168 if (refused && timo <= 16) {
169 (void)_libc_sleep(timo);
169 struct timespec time_to_sleep, time_remaining;
170
171 time_to_sleep.tv_sec = timo;
172 time_to_sleep.tv_nsec = 0;
173 (void)_nanosleep(&time_to_sleep, &time_remaining);
174
175 timo *= 2;
176 ai = res;
177 refused = 0;
178 continue;
179 }
180 freeaddrinfo(res);
181 (void)fprintf(stderr, "%s: %s\n", *ahost, strerror(errno));
182 sigsetmask(oldmask);
183 return (-1);
184 }
185 lport--;
186 if (fd2p == 0) {
182 _libc_write(s, "", 1);
187 _write(s, "", 1);
188 lport = 0;
189 } else {
190 char num[8];
191 int s2 = rresvport_af(&lport, ai->ai_family), s3;
192 int len = ai->ai_addrlen;
193 int nfds;
194
195 if (s2 < 0)
196 goto bad;
197 listen(s2, 1);
198 (void)snprintf(num, sizeof(num), "%d", lport);
194 if (_libc_write(s, num, strlen(num)+1) != strlen(num)+1) {
199 if (_write(s, num, strlen(num)+1) != strlen(num)+1) {
200 (void)fprintf(stderr,
201 "rcmd: write (setting up stderr): %s\n",
202 strerror(errno));
198 (void)_libc_close(s2);
203 (void)_close(s2);
204 goto bad;
205 }
206 nfds = max(s, s2)+1;
207 if(nfds > FD_SETSIZE) {
208 fprintf(stderr, "rcmd: too many files\n");
204 (void)_libc_close(s2);
209 (void)_close(s2);
210 goto bad;
211 }
212again:
213 FD_ZERO(&reads);
214 FD_SET(s, &reads);
215 FD_SET(s2, &reads);
216 errno = 0;
217 if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
218 if (errno != 0)
219 (void)fprintf(stderr,
220 "rcmd: select (setting up stderr): %s\n",
221 strerror(errno));
222 else
223 (void)fprintf(stderr,
224 "select: protocol failure in circuit setup\n");
220 (void)_libc_close(s2);
225 (void)_close(s2);
226 goto bad;
227 }
228 s3 = accept(s2, (struct sockaddr *)&from, &len);
229 switch (from.ss_family) {
230 case AF_INET:
231 aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
232 break;
233#ifdef INET6

--- 5 unchanged lines hidden (view full) ---

239 aport = 0; /* error */
240 break;
241 }
242 /*
243 * XXX careful for ftp bounce attacks. If discovered, shut them
244 * down and check for the real auxiliary channel to connect.
245 */
246 if (aport == 20) {
242 _libc_close(s3);
247 _close(s3);
248 goto again;
249 }
245 (void)_libc_close(s2);
250 (void)_close(s2);
251 if (s3 < 0) {
252 (void)fprintf(stderr,
253 "rcmd: accept: %s\n", strerror(errno));
254 lport = 0;
255 goto bad;
256 }
257 *fd2p = s3;
258 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
259 (void)fprintf(stderr,
260 "socket: protocol failure in circuit setup.\n");
261 goto bad2;
262 }
263 }
259 (void)_libc_write(s, locuser, strlen(locuser)+1);
260 (void)_libc_write(s, remuser, strlen(remuser)+1);
261 (void)_libc_write(s, cmd, strlen(cmd)+1);
262 if (_libc_read(s, &c, 1) != 1) {
264 (void)_write(s, locuser, strlen(locuser)+1);
265 (void)_write(s, remuser, strlen(remuser)+1);
266 (void)_write(s, cmd, strlen(cmd)+1);
267 if (_read(s, &c, 1) != 1) {
268 (void)fprintf(stderr,
269 "rcmd: %s: %s\n", *ahost, strerror(errno));
270 goto bad2;
271 }
272 if (c != 0) {
268 while (_libc_read(s, &c, 1) == 1) {
269 (void)_libc_write(STDERR_FILENO, &c, 1);
273 while (_read(s, &c, 1) == 1) {
274 (void)_write(STDERR_FILENO, &c, 1);
275 if (c == '\n')
276 break;
277 }
278 goto bad2;
279 }
280 sigsetmask(oldmask);
281 freeaddrinfo(res);
282 return (s);
283bad2:
284 if (lport)
280 (void)_libc_close(*fd2p);
285 (void)_close(*fd2p);
286bad:
282 (void)_libc_close(s);
287 (void)_close(s);
288 sigsetmask(oldmask);
289 freeaddrinfo(res);
290 return (-1);
291}
292
293int
294rresvport(port)
295 int *port;

--- 32 unchanged lines hidden (view full) ---

328 s = socket(ss.ss_family, SOCK_STREAM, 0);
329 if (s < 0)
330 return (-1);
331#if 0 /* compat_exact_traditional_rresvport_semantics */
332 sin.sin_port = htons((u_short)*alport);
333 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
334 return (s);
335 if (errno != EADDRINUSE) {
331 (void)_libc_close(s);
336 (void)_close(s);
337 return (-1);
338 }
339#endif
340 *sport = 0;
341 if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) {
337 (void)_libc_close(s);
342 (void)_close(s);
343 return (-1);
344 }
345 *alport = (int)ntohs(*sport);
346 return (s);
347}
348
349int __check_rhosts_file = 1;
350char *__rcmd_errstr;

--- 314 unchanged lines hidden ---