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