mount_portal(8): Raise WARNS to 6.
[dragonfly.git] / sbin / mount_portal / pt_tcplisten.c
CommitLineData
984263bc
MD
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 *
e929265d 41 * $DragonFly: src/sbin/mount_portal/pt_tcplisten.c,v 1.5 2005/11/06 12:36:40 swildner Exp $
984263bc
MD
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>
d5c90ae7 49#include <string.h>
984263bc
MD
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 */
e929265d 72int
92f258c4
SW
73portal_tcplisten(struct portal_cred *pcr, char *key, char **v,
74 int kso __unused, int *fdp)
984263bc
MD
75{
76 char host[MAXHOSTNAMELEN];
77 char port[MAXHOSTNAMELEN];
78 char *p = key + (v[1] ? strlen(v[1]) : 0);
79 char *q;
80 struct hostent *hp;
81 struct servent *sp;
8af0d623 82 struct in_addr **ipp = NULL;
984263bc
MD
83 struct in_addr *ip[2];
84 struct in_addr ina;
85 u_short s_port;
86 int any = 0;
87 struct sockaddr_in sain;
88
89 q = strchr(p, '/');
92f258c4 90 if (q == 0 || (size_t)(q - p) >= sizeof(host))
984263bc
MD
91 return (EINVAL);
92 *q = '\0';
93 snprintf(host, sizeof(host), "%s", p);
94 p = q + 1;
95
96 q = strchr(p, '/');
97 if (q)
98 *q = '\0';
99 if (strlen(p) >= sizeof(port))
100 return (EINVAL);
101 snprintf(port, sizeof(port), "%s", p);
102
103 if (strcmp(host, "ANY") == 0) {
104 any = 1;
105 } else {
106 hp = gethostbyname(host);
107 if (hp != 0) {
108 ipp = (struct in_addr **) hp->h_addr_list;
109 } else {
110 ina.s_addr = inet_addr(host);
111 if (ina.s_addr == INADDR_NONE)
112 return (EINVAL);
113 ip[0] = &ina;
114 ip[1] = 0;
115 ipp = ip;
116 }
117 }
118#ifdef DEBUG
119 if (any)
120 printf("INADDR_ANY to be used for hostname\n");
121 else
122 printf("inet address for %s is %s\n", host, inet_ntoa(*ipp[0]));
123#endif
124
125 sp = getservbyname(port, "tcp");
126 if (sp != NULL) {
127 s_port = (u_short) sp->s_port;
128 } else {
129 s_port = strtoul(port, &p, 0);
130 if (*p != '\0')
131 return (EINVAL);
132 s_port = htons(s_port);
133 }
134 if ((ntohs(s_port) != 0) &&
135 (ntohs(s_port) <= IPPORT_RESERVED) &&
136 (pcr->pcr_uid != 0))
137 return (EPERM);
138#ifdef DEBUG
139 printf("port number for %s is %d\n", port, ntohs(s_port));
140#endif
141
142 memset(&sain, 0, sizeof(sain));
143 sain.sin_len = sizeof(sain);
144 sain.sin_family = AF_INET;
145 sain.sin_port = s_port;
146
147 if (any) {
148 int so;
149 int sock;
150
151 so = socket(AF_INET, SOCK_STREAM, 0);
152 if (so < 0) {
153 syslog(LOG_ERR, "socket: %m");
154 return (errno);
155 }
156
157 sain.sin_addr.s_addr = INADDR_ANY;
158 if (bind(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
159 listen(so, 1);
60233e58 160 if ((sock = accept(so, NULL, NULL)) == -1) {
984263bc 161 syslog(LOG_ERR, "accept: %m");
7cee7052 162 close(so);
984263bc
MD
163 return (errno);
164 }
165 *fdp = sock;
7cee7052 166 close(so);
984263bc
MD
167 return (0);
168 }
169 syslog(LOG_ERR, "bind: %m");
7cee7052 170 close(so);
984263bc
MD
171 return (errno);
172 }
173
174 while (ipp[0]) {
175 int so;
176 int sock;
177
178 so = socket(AF_INET, SOCK_STREAM, 0);
179 if (so < 0) {
180 syslog(LOG_ERR, "socket: %m");
181 return (errno);
182 }
183
184 sain.sin_addr = *ipp[0];
185 if (bind(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
186 listen(so, 1);
60233e58 187 if ((sock = accept(so, NULL, NULL)) == -1) {
984263bc 188 syslog(LOG_ERR, "accept: %m");
7cee7052 189 close(so);
984263bc
MD
190 return (errno);
191 }
192 *fdp = sock;
7cee7052 193 close(so);
984263bc
MD
194 return (0);
195 }
7cee7052 196 close(so);
984263bc
MD
197
198 ipp++;
199 }
200
201 syslog(LOG_ERR, "bind: %m");
202 return (errno);
203
204}