source: trunk/libs/newlib/src/newlib/libc/sys/linux/net/rexec.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 5.3 KB
Line 
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#if defined(LIBC_SCCS) && !defined(lint)
31static char sccsid[] = "@(#)rexec.c     8.1 (Berkeley) 6/4/93";
32#endif /* LIBC_SCCS and not lint */
33
34#include <sys/types.h>
35#include <sys/socket.h>
36
37#include <netinet/in.h>
38
39#include <alloca.h>
40#include <stdio.h>
41#include <netdb.h>
42#include <errno.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46#include <sys/uio.h>
47#include "libc-symbols.h"
48
49int     rexecoptions;
50libc_freeres_ptr (static char *ahostbuf);
51
52int
53rexec_af(ahost, rport, name, pass, cmd, fd2p, af)
54        char **ahost;
55        int rport;
56        const char *name, *pass, *cmd;
57        int *fd2p;
58        sa_family_t af;
59{
60        struct sockaddr_storage sa2, from;
61        struct addrinfo hints, *res0;
62        const char *orig_name = name;
63        const char *orig_pass = pass;
64        u_short port = 0;
65        int s, timo = 1, s3;
66        char c;
67        int gai;
68        char servbuff[NI_MAXSERV];
69
70        snprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport));
71        servbuff[sizeof(servbuff) - 1] = '\0';
72
73        memset(&hints, '\0', sizeof(hints));
74        hints.ai_family = af;
75        hints.ai_socktype = SOCK_STREAM;
76        hints.ai_flags = AI_CANONNAME;
77        gai = getaddrinfo(*ahost, servbuff, &hints, &res0);
78        if (gai){
79                /* XXX: set errno? */
80                return -1;
81        }
82
83        if (res0->ai_canonname){
84                free (ahostbuf);
85                ahostbuf = strdup (res0->ai_canonname);
86                if (ahostbuf == NULL) {
87                        perror ("rexec: strdup");
88                        return (-1);
89                }
90                *ahost = ahostbuf;
91        } else
92                *ahost = NULL;
93        ruserpass(res0->ai_canonname, &name, &pass);
94retry:
95        s = socket(res0->ai_family, res0->ai_socktype, 0);
96        if (s < 0) {
97                perror("rexec: socket");
98                return (-1);
99        }
100        if (connect(s, res0->ai_addr, res0->ai_addrlen) < 0) {
101                if (errno == ECONNREFUSED && timo <= 16) {
102                        (void) __close(s);
103                        sleep(timo);
104                        timo *= 2;
105                        goto retry;
106                }
107                perror(res0->ai_canonname);
108                return (-1);
109        }
110        if (fd2p == 0) {
111                (void) write(s, "", 1);
112                port = 0;
113        } else {
114                char num[32];
115                int s2, sa2len;
116
117                s2 = socket(res0->ai_family, res0->ai_socktype, 0);
118                if (s2 < 0) {
119                        (void) __close(s);
120                        return (-1);
121                }
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#ifdef SA_LEN
129                } else if (sa2len != SA_LEN((struct sockaddr *)&sa2)) {
130                        __set_errno(EINVAL);
131                        (void) __close(s2);
132                        goto bad;
133#endif
134                }
135                port = 0;
136                if (!getnameinfo((struct sockaddr *)&sa2, sa2len,
137                                 NULL, 0, servbuff, sizeof(servbuff),
138                                 NI_NUMERICSERV))
139                        port = atoi(servbuff);
140                (void) sprintf(num, "%u", port);
141                (void) __write(s, num, strlen(num)+1);
142                { int len = sizeof (from);
143                  s3 = TEMP_FAILURE_RETRY (accept(s2, (struct sockaddr *)&from,
144                                                  &len));
145                  __close(s2);
146                  if (s3 < 0) {
147                        perror("accept");
148                        port = 0;
149                        goto bad;
150                  }
151                }
152                *fd2p = s3;
153        }
154
155        struct iovec iov[3] =
156          {
157            [0] = { .iov_base = (void *) name, .iov_len = strlen (name) + 1 },
158            /* should public key encypt the password here */
159            [1] = { .iov_base = (void *) pass, .iov_len = strlen (pass) + 1 },
160            [2] = { .iov_base = (void *) cmd, .iov_len = strlen (cmd) + 1 }
161          };
162        (void) TEMP_FAILURE_RETRY (writev (s, iov, 3));
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);
185bad:
186        if (port)
187                (void) __close(*fd2p);
188        (void) __close(s);
189        freeaddrinfo(res0);
190        return (-1);
191}
192libc_hidden_def (rexec_af)
193
194int
195rexec(ahost, rport, name, pass, cmd, fd2p)
196        char **ahost;
197        int rport;
198        const char *name, *pass, *cmd;
199        int *fd2p;
200{
201        return rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET);
202}
Note: See TracBrowser for help on using the repository browser.