Debianization. Socket target options. Internet binding.
[fwd] / inet.c
1 /* -*-c-*-
2 *
3 * $Id: inet.c,v 1.5 2003/11/25 14:08:23 mdw Exp $
4 *
5 * Protocol specific definitions for IPv4 sockets
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: inet.c,v $
32 * Revision 1.5 2003/11/25 14:08:23 mdw
33 * Debianization. Socket target options. Internet binding.
34 *
35 * Revision 1.4 2002/01/13 14:49:56 mdw
36 * Conditional compilation for @getnetbyname@, since Cygwin doesn't have
37 * it.
38 *
39 * Revision 1.3 2000/08/01 17:59:56 mdw
40 * Switch over to using `size_t' for socket address lengths.
41 *
42 * Revision 1.2 1999/07/27 18:30:53 mdw
43 * Various minor portability fixes.
44 *
45 * Revision 1.1 1999/07/26 23:34:11 mdw
46 * New socket address types.
47 *
48 */
49
50 /*----- Header files ------------------------------------------------------*/
51
52 #include "config.h"
53
54 #include <ctype.h>
55 #include <errno.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60 #include <sys/types.h>
61 #include <unistd.h>
62
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <netdb.h>
67
68 #include <mLib/alloc.h>
69 #include <mLib/dstr.h>
70 #include <mLib/fdflags.h>
71 #include <mLib/report.h>
72 #include <mLib/sub.h>
73
74 #include "acl.h"
75 #include "addr.h"
76 #include "conf.h"
77 #include "fw.h"
78 #include "identify.h"
79 #include "inet.h"
80 #include "reffd.h"
81 #include "scan.h"
82 #include "socket.h"
83
84 /*----- Data structures ---------------------------------------------------*/
85
86 typedef struct inet_addrx {
87 addr a;
88 struct sockaddr_in sin;
89 } inet_addrx;
90
91 typedef struct inet_opts {
92 addr_opts ao;
93 struct in_addr bind;
94 } inet_opts;
95
96 typedef struct inet_srcopts {
97 inet_opts io;
98 acl_entry *acl;
99 acl_entry **acltail;
100 } inet_srcopts;
101
102 typedef struct inet_targopts {
103 inet_opts io;
104 } inet_targopts;
105
106 static inet_srcopts inet_globalsrc =
107 { { { 0 }, { INADDR_ANY } }, 0, &inet_globalsrc.acl };
108 static inet_targopts inet_globaltarg =
109 { { { 0 }, { INADDR_ANY } } };
110
111 /*----- Protocol operations -----------------------------------------------*/
112
113 /* --- @read@ --- */
114
115 static addr *inet_read(scanner *sc, unsigned type)
116 {
117 inet_addrx *ia = xmalloc(sizeof(*ia));
118
119 ia->a.ops = &inet_ops;
120 ia->a.sz = sizeof(struct sockaddr_in);
121 memset(&ia->sin, 0, sizeof(ia->sin));
122 ia->sin.sin_family = AF_INET;
123
124 /* --- Read the host address part --- */
125
126 switch (type) {
127 case ADDR_SRC:
128 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "port") == 0)
129 token(sc);
130 ia->sin.sin_addr.s_addr = htonl(INADDR_ANY);
131 break;
132 case ADDR_DEST: {
133 struct hostent *h;
134 dstr d = DSTR_INIT;
135 conf_name(sc, '.', &d);
136 if ((h = gethostbyname(d.buf)) == 0)
137 error(sc, "couldn't resolve Internet address `%s'", d.buf);
138 memcpy(&ia->sin.sin_addr, h->h_addr, sizeof(struct in_addr));
139 dstr_destroy(&d);
140 if (sc->t == ':')
141 token(sc);
142 } break;
143 }
144
145 /* --- Read the port number --- */
146
147 {
148 struct servent *s;
149
150 if (sc->t != CTOK_WORD)
151 error(sc, "parse error, TCP port expected");
152 if (isdigit((unsigned char)sc->d.buf[0]))
153 ia->sin.sin_port = htons(atoi(sc->d.buf));
154 else if ((s = getservbyname(sc->d.buf, "tcp")) == 0)
155 error(sc, "unknown tcp service `%s'", sc->d.buf);
156 else
157 ia->sin.sin_port = s->s_port;
158 token(sc);
159 }
160
161 return (&ia->a);
162 }
163
164 /* --- @destroy@ --- */
165
166 static void inet_destroy(addr *a)
167 {
168 inet_addrx *ia = (inet_addrx *)a;
169 DESTROY(ia);
170 }
171
172 /* --- @print@ --- */
173
174 static void inet_print(addr *a, unsigned type, dstr *d)
175 {
176 inet_addrx *ia = (inet_addrx *)a;
177 switch (type) {
178 case ADDR_SRC:
179 dstr_putf(d, "inet:%u", (unsigned)ntohs(ia->sin.sin_port));
180 break;
181 case ADDR_DEST:
182 dstr_putf(d, "inet:%s:%u",
183 inet_ntoa(ia->sin.sin_addr),
184 (unsigned)ntohs(ia->sin.sin_port));
185 break;
186 }
187 }
188
189 /* --- @initopts@ --- */
190
191 static addr_opts *inet_initsrcopts(void)
192 {
193 inet_srcopts *io = CREATE(inet_srcopts);
194 *io = inet_globalsrc;
195 io->acl = 0;
196 io->acltail = &io->acl;
197 return (&io->io.ao);
198 }
199
200 static addr_opts *inet_inittargopts(void)
201 {
202 inet_targopts *io = CREATE(inet_targopts);
203 *io = inet_globaltarg;
204 return (&io->io.ao);
205 }
206
207 /* --- @option@ --- */
208
209 static void addropt(scanner *sc, inet_opts *io)
210 {
211 dstr d = DSTR_INIT;
212 struct hostent *h;
213
214 token(sc);
215 if (sc->t == '=')
216 token(sc);
217 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "any") == 0)
218 io->bind.s_addr = INADDR_ANY;
219 else {
220 conf_name(sc, '.', &d);
221 if ((h = gethostbyname(d.buf)) == 0)
222 error(sc, "couldn't resolve address `%s'", d.buf);
223 memcpy(&io->bind, h->h_addr, sizeof(struct in_addr));
224 }
225 }
226
227 static int srcopt(scanner *sc, addr_opts *ao)
228 {
229 inet_srcopts *io = (inet_srcopts *)ao;
230 unsigned act;
231
232 CONF_BEGIN(sc, "source", "Internet socket source")
233
234 /* --- Initialization --- */
235
236 if (!io) {
237 if (!inet_globalsrc.acltail)
238 inet_globalsrc.acltail = &inet_globalsrc.acl;
239 io = &inet_globalsrc;
240 }
241
242 /* --- Source address configuration --- */
243
244 if (strcmp(sc->d.buf, "addr") == 0) {
245 addropt(sc, &io->io);
246 CONF_ACCEPT;
247 }
248
249 /* --- Access control limitations --- */
250
251 if ((strcmp(sc->d.buf, "allow") == 0 && (act = ACL_ALLOW, 1)) ||
252 (strcmp(sc->d.buf, "deny") == 0 && (act = ACL_DENY, 1))) {
253 struct hostent *h;
254 struct netent *n;
255 struct in_addr a, m;
256 dstr d = DSTR_INIT;
257
258 /* --- Find out what's going on --- */
259
260 token(sc);
261 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "from") == 0)
262 token(sc);
263
264 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "priv-port") == 0) {
265 acl_addpriv(&io->acltail, act);
266 token(sc);
267 } else {
268 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "host") == 0)
269 token(sc);
270
271 /* --- Find the host or network address --- */
272
273 conf_name(sc, '.', &d);
274 #ifdef HAVE_GETNETBYNAME
275 if ((n = getnetbyname(d.buf)) != 0)
276 a.s_addr = htonl(n->n_net);
277 else
278 #endif
279 if ((h = gethostbyname(d.buf)) == 0)
280 error(sc, "couldn't resolve address `%s'", d.buf);
281 else
282 memcpy(&a, h->h_addr, sizeof(struct in_addr));
283
284 /* --- Find the netmask, if any --- */
285
286 if (sc->t != '/')
287 m.s_addr = ~0ul;
288 else {
289 token(sc);
290 DRESET(&d);
291 conf_name(sc, '.', &d);
292 if (strchr(d.buf, '.') == 0) {
293 int n = atoi(d.buf);
294 if (n == 0)
295 m.s_addr = 0;
296 else
297 m.s_addr = htonl((~0ul << (32 - n)) & 0xffffffff);
298 } else {
299 #ifdef HAVE_INET_ATON
300 if (!inet_aton(d.buf, &m))
301 error(sc, "bad netmask `%s'", d.buf);
302 #else
303 m.s_addr = inet_addr(d.buf);
304 #endif
305 }
306 }
307 dstr_destroy(&d);
308
309 /* --- Add the access control entry --- */
310
311 acl_addhost(&io->acltail, act, a, m);
312 }
313 CONF_ACCEPT;
314 }
315
316 /* --- Anything unrecognized --- */
317
318 CONF_END;
319 }
320
321 static int targopt(scanner *sc, addr_opts *ao)
322 {
323 inet_targopts *io = (inet_targopts *)ao;
324
325 CONF_BEGIN(sc, "dest", "Internet socket target");
326 if (strcmp(sc->d.buf, "addr") == 0) {
327 addropt(sc, &io->io);
328 CONF_ACCEPT;
329 }
330 CONF_END;
331 }
332
333 static int inet_option(scanner *sc, addr_opts *ao, unsigned type)
334 {
335 CONF_BEGIN(sc, "inet", "Internet socket");
336 if (type != ADDR_DEST && srcopt(sc, ao))
337 CONF_ACCEPT;
338 if (type != ADDR_SRC && targopt(sc, ao))
339 CONF_ACCEPT;
340 CONF_END;
341 }
342
343 /* --- @freeopts@ --- */
344
345 static void inet_freesrcopts(addr_opts *ao)
346 {
347 inet_srcopts *io = (inet_srcopts *)ao;
348 acl_free(io->acl);
349 DESTROY(io);
350 }
351
352 static void inet_freetargopts(addr_opts *ao)
353 {
354 inet_targopts *io = (inet_targopts *)ao;
355 DESTROY(io);
356 }
357
358 /* --- @bind@ --- */
359
360 static int inet_bind(addr *a, addr_opts *ao)
361 {
362 inet_addrx *ia = (inet_addrx *)a;
363 inet_srcopts *io = (inet_srcopts *)ao;
364 struct sockaddr_in sin;
365 int opt = 1;
366 int fd;
367
368 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
369 goto fail_0;
370 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
371 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
372 sin = ia->sin;
373 sin.sin_addr = io->io.bind;
374 if (bind(fd, (struct sockaddr *)&sin, sizeof(ia->sin)))
375 goto fail_1;
376 return (fd);
377
378 fail_1:
379 close(fd);
380 fail_0:
381 return (-1);
382 }
383
384 /* --- @accept@ --- */
385
386 static reffd *inet_accept(int fd, addr_opts *ao, const char *desc)
387 {
388 inet_srcopts *io = (inet_srcopts *)ao;
389 int nfd;
390 id_req q;
391 size_t lsinsz = sizeof(q.lsin), rsinsz = sizeof(q.rsin);
392 int act = ACL_ALLOW;
393
394 /* --- Accept the new connection --- */
395
396 if ((nfd = accept(fd, (struct sockaddr *)&q.rsin, &rsinsz)) < 0)
397 return (0);
398 if (getsockname(nfd, (struct sockaddr *)&q.lsin, &lsinsz)) {
399 close(nfd);
400 return (0);
401 }
402 q.desc = desc;
403 q.r = reffd_init(nfd);
404
405 /* --- Find out whether this connection is allowed --- */
406
407 if (!acl_check(io->acl, q.rsin.sin_addr, ntohs(q.rsin.sin_port), &act))
408 acl_check(inet_globalsrc.acl, q.rsin.sin_addr,
409 ntohs(q.rsin.sin_port), &act);
410 if (act != ACL_ALLOW) {
411 q.act = "refused";
412 if (!(io->io.ao.f & ADDRF_NOLOG))
413 identify(&q);
414 REFFD_DEC(q.r);
415 return (0);
416 }
417
418 /* --- Everything seems to be OK --- */
419
420 q.act = "accepted";
421 if (!(io->io.ao.f & ADDRF_NOLOG))
422 identify(&q);
423 return (q.r);
424 }
425
426 /* --- @connect@ --- */
427
428 static int inet_connect(addr *a, addr_opts *ao, conn *c, endpt *e)
429 {
430 inet_addrx *ia = (inet_addrx *)a;
431 inet_targopts *io = (inet_targopts *)ao;
432 int fd;
433
434 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
435 goto fail_0;
436 if (io->io.bind.s_addr != INADDR_ANY) {
437 struct sockaddr_in sin;
438 memset(&sin, 0, sizeof(sin));
439 sin.sin_family = AF_INET;
440 sin.sin_addr = io->io.bind;
441 sin.sin_port = 0;
442 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
443 goto fail_1;
444 }
445 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
446 return (conn_init(c, sel, fd, (struct sockaddr *)&ia->sin, sizeof(ia->sin),
447 starget_connected, e));
448 fail_1:
449 close(fd);
450 fail_0:
451 return (-1);
452 }
453
454 /* --- Ops table --- */
455
456 addr_ops inet_ops = {
457 "inet",
458 inet_read, inet_destroy, inet_print,
459 inet_initsrcopts, inet_option, inet_freesrcopts,
460 inet_bind, 0, inet_accept,
461 inet_inittargopts, inet_freetargopts,
462 inet_connect
463 };
464
465 /*----- That's all, folks -------------------------------------------------*/