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