cam - Fix bus registration race
[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. 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 <stdio.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <errno.h>
44 #include <string.h>
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/syslog.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <netdb.h>
52
53 #include "portald.h"
54
55 /*
56  * Key will be tcplisten/host/port
57  *
58  * Create a TCP socket bound to the requested host and port.
59  * If the host is "ANY" the receving address will be set to INADDR_ANY.
60  * If the port is 0 the caller must find out the returned port number
61  * using a call to getsockname.
62  *
63  * XXX!  The owner of the socket will be root rather then the user.  This
64  *       may cause remote auth (identd) to return unexpected results.
65  *
66  */
67 int
68 portal_tcplisten(struct portal_cred *pcr, char *key, char **v,
69     int kso __unused, int *fdp)
70 {
71        char host[MAXHOSTNAMELEN];
72        char port[MAXHOSTNAMELEN];
73        char *p = key + (v[1] ? strlen(v[1]) : 0);
74        char *q;
75        struct hostent *hp;
76        struct servent *sp;
77        struct in_addr **ipp = NULL;
78        struct in_addr *ip[2];
79        struct in_addr ina;
80        u_short s_port;
81        int any = 0;
82        struct sockaddr_in sain;
83
84        q = strchr(p, '/');
85        if (q == NULL || (size_t)(q - p) >= sizeof(host))
86                return (EINVAL);
87        *q = '\0';
88        snprintf(host, sizeof(host), "%s", p);
89        p = q + 1;
90
91        q = strchr(p, '/');
92        if (q)
93                *q = '\0';
94        if (strlen(p) >= sizeof(port))
95                return (EINVAL);
96        snprintf(port, sizeof(port), "%s", p);
97
98        if (strcmp(host, "ANY") == 0) {
99                any = 1;
100        } else {
101                hp = gethostbyname(host);
102                if (hp != NULL) {
103                        ipp = (struct in_addr **) hp->h_addr_list;
104                } else {
105                        ina.s_addr = inet_addr(host);
106                        if (ina.s_addr == INADDR_NONE)
107                                return (EINVAL);
108                        ip[0] = &ina;
109                        ip[1] = NULL;
110                        ipp = ip;
111                }
112        }
113 #ifdef DEBUG
114        if (any)
115                printf("INADDR_ANY to be used for hostname\n");
116        else
117                printf("inet address for %s is %s\n", host, inet_ntoa(*ipp[0]));
118 #endif
119
120        sp = getservbyname(port, "tcp");
121        if (sp != NULL) {
122                s_port = (u_short) sp->s_port;
123         } else {
124                s_port = strtoul(port, &p, 0);
125                if (*p != '\0')
126                        return (EINVAL);
127                s_port = htons(s_port);
128        }
129        if ((ntohs(s_port) != 0) &&
130            (ntohs(s_port) <= IPPORT_RESERVED) &&
131            (pcr->pcr_uid != 0))
132                return (EPERM);
133 #ifdef DEBUG
134        printf("port number for %s is %d\n", port, ntohs(s_port));
135 #endif
136
137        memset(&sain, 0, sizeof(sain));
138        sain.sin_len = sizeof(sain);
139        sain.sin_family = AF_INET;
140        sain.sin_port = s_port;
141
142        if (any) {
143                int so;
144                int sock;
145
146                so = socket(AF_INET, SOCK_STREAM, 0);
147                if (so < 0) {
148                        syslog(LOG_ERR, "socket: %m");
149                        return (errno);
150                }
151
152                sain.sin_addr.s_addr = INADDR_ANY;
153                if (bind(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
154                        listen(so, 1);
155                        if ((sock = accept(so, NULL, NULL)) == -1) {
156                                syslog(LOG_ERR, "accept: %m");
157                                close(so);
158                                return (errno);
159                        }
160                        *fdp = sock;
161                        close(so);
162                        return (0);
163                }
164                syslog(LOG_ERR, "bind: %m");
165                close(so);
166                return (errno);
167        }
168
169        while (ipp[0]) {
170                int so;
171                int sock;
172
173                so = socket(AF_INET, SOCK_STREAM, 0);
174                if (so < 0) {
175                        syslog(LOG_ERR, "socket: %m");
176                        return (errno);
177                }
178
179                sain.sin_addr = *ipp[0];
180                if (bind(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
181                        listen(so, 1);
182                        if ((sock = accept(so, NULL, NULL)) == -1) {
183                                syslog(LOG_ERR, "accept: %m");
184                                close(so);
185                                return (errno);
186                        }
187                        *fdp = sock;
188                        close(so);
189                        return (0);
190                }
191                close(so);
192
193                ipp++;
194        }
195
196        syslog(LOG_ERR, "bind: %m");
197        return (errno);
198
199 }