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