]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/mount_portalfs/pt_tcplisten.c
This commit was generated by cvs2svn to compensate for changes in r165670,
[FreeBSD/FreeBSD.git] / usr.sbin / mount_portalfs / pt_tcplisten.c
1 /*
2  * Copyright (c) 1992, 1993
3  *  The Regents of the University of California.  All rights reserved.
4  * All rights reserved.
5  *
6  * This code is derived from software donated to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Modified by Duncan Barclay.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *  @(#)pt_tcp.c  8.3 (Berkeley) 3/27/94
36  *
37  * pt_tcp.c,v 1.1.1.1 1994/05/26 06:34:34 rgrimes Exp
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <string.h>
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/syslog.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <netdb.h>
55
56 #include "portald.h"
57
58 /*
59  * Key will be tcplisten/host/port
60  *
61  * Create a TCP socket bound to the requested host and port.
62  * If the host is "ANY" the receving address will be set to INADDR_ANY.
63  * If the port is 0 the caller must find out the returned port number
64  * using a call to getsockname.
65  *
66  * XXX!  The owner of the socket will be root rather then the user.  This
67  *       may cause remote auth (identd) to return unexpected results.
68  *
69  */
70 int portal_tcplisten(pcr, key, v, kso, fdp)
71        struct portal_cred *pcr;
72        char *key;
73        char **v;
74        int kso;
75        int *fdp;
76 {
77        char host[MAXHOSTNAMELEN];
78        char port[MAXHOSTNAMELEN];
79        char *p = key + (v[1] ? strlen(v[1]) : 0);
80        char *q;
81        struct hostent *hp;
82        struct servent *sp;
83        struct in_addr **ipp;
84        struct in_addr *ip[2];
85        struct in_addr ina;
86        u_short s_port;
87        int any = 0;
88        struct sockaddr_in sain;
89
90        q = strchr(p, '/');
91        if (q == 0 || q - p >= sizeof(host))
92                return (EINVAL);
93        *q = '\0';
94        snprintf(host, sizeof(host), "%s", p);
95        p = q + 1;
96
97        q = strchr(p, '/');
98        if (q)
99                *q = '\0';
100        if (strlen(p) >= sizeof(port))
101                return (EINVAL);
102        snprintf(port, sizeof(port), "%s", p);
103
104        if (strcmp(host, "ANY") == 0) {
105                any = 1;
106        } else {
107                hp = gethostbyname(host);
108                if (hp != 0) {
109                        ipp = (struct in_addr **) hp->h_addr_list;
110                } else {
111                        ina.s_addr = inet_addr(host);
112                        if (ina.s_addr == INADDR_NONE)
113                                return (EINVAL);
114                        ip[0] = &ina;
115                        ip[1] = 0;
116                        ipp = ip;
117                }
118        }
119 #ifdef DEBUG
120        if (any)
121                printf("INADDR_ANY to be used for hostname\n");
122        else
123                printf("inet address for %s is %s\n", host, inet_ntoa(*ipp[0]));
124 #endif
125
126        sp = getservbyname(port, "tcp");
127        if (sp != NULL) {
128                s_port = (u_short) sp->s_port;
129         } else {
130                s_port = strtoul(port, &p, 0);
131                if (*p != '\0')
132                        return (EINVAL);
133                s_port = htons(s_port);
134        }
135        if ((ntohs(s_port) != 0) &&
136            (ntohs(s_port) <= IPPORT_RESERVED) &&
137            (pcr->pcr_uid != 0))
138                return (EPERM);
139 #ifdef DEBUG
140        printf("port number for %s is %d\n", port, ntohs(s_port));
141 #endif
142
143        memset(&sain, 0, sizeof(sain));
144        sain.sin_len = sizeof(sain);
145        sain.sin_family = AF_INET;
146        sain.sin_port = s_port;
147
148        if (any) {
149                int so;
150                int sock;
151
152                so = socket(AF_INET, SOCK_STREAM, 0);
153                if (so < 0) {
154                        syslog(LOG_ERR, "socket: %m");
155                        return (errno);
156                }
157
158                sain.sin_addr.s_addr = INADDR_ANY;
159                if (bind(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
160                        listen(so, 1);
161                        if ((sock = accept(so, (struct sockaddr *)0, (int *)0)) == -1) {
162                                syslog(LOG_ERR, "accept: %m");
163                                (void) close(so);
164                                return (errno);
165                        }
166                        *fdp = sock;
167                        (void) close(so);
168                        return (0);
169                }
170                syslog(LOG_ERR, "bind: %m");
171                (void) close(so);
172                return (errno);
173        }
174
175        while (ipp[0]) {
176                int so;
177                int sock;
178
179                so = socket(AF_INET, SOCK_STREAM, 0);
180                if (so < 0) {
181                        syslog(LOG_ERR, "socket: %m");
182                        return (errno);
183                }
184
185                sain.sin_addr = *ipp[0];
186                if (bind(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
187                        listen(so, 1);
188                        if ((sock = accept(so, (struct sockaddr *)0, (int *)0)) == -1) {
189                                syslog(LOG_ERR, "accept: %m");
190                                (void) close(so);
191                                return (errno);
192                        }
193                        *fdp = sock;
194                        (void) close(so);
195                        return (0);
196                }
197                (void) close(so);
198
199                ipp++;
200        }
201
202        syslog(LOG_ERR, "bind: %m");
203        return (errno);
204
205 }