Use new mLib selbuf features.
[fwd] / inet.c
1 /* -*-c-*-
2 *
3 * $Id: inet.c,v 1.2 1999/07/27 18:30:53 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.2 1999/07/27 18:30:53 mdw
33 * Various minor portability fixes.
34 *
35 * Revision 1.1 1999/07/26 23:34:11 mdw
36 * New socket address types.
37 *
38 */
39
40 /*----- Header files ------------------------------------------------------*/
41
42 #include "config.h"
43
44 #include <ctype.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include <sys/types.h>
51 #include <unistd.h>
52
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <netdb.h>
57
58 #include <mLib/alloc.h>
59 #include <mLib/dstr.h>
60 #include <mLib/report.h>
61 #include <mLib/sub.h>
62
63 #include "acl.h"
64 #include "addr.h"
65 #include "conf.h"
66 #include "identify.h"
67 #include "inet.h"
68 #include "reffd.h"
69 #include "scan.h"
70
71 /*----- Data structures ---------------------------------------------------*/
72
73 typedef struct inet_addrx {
74 addr a;
75 struct sockaddr_in sin;
76 } inet_addrx;
77
78 typedef struct inet_opts {
79 addr_opts ao;
80 acl_entry *acl;
81 acl_entry **acltail;
82 } inet_opts;
83
84 /*----- Protocol operations -----------------------------------------------*/
85
86 /* --- @read@ --- */
87
88 static addr *inet_read(scanner *sc, unsigned type)
89 {
90 inet_addrx *ia = xmalloc(sizeof(*ia));
91
92 ia->a.ops = &inet_ops;
93 ia->a.sz = sizeof(struct sockaddr_in);
94 memset(&ia->sin, 0, sizeof(ia->sin));
95 ia->sin.sin_family = AF_INET;
96
97 /* --- Read the host address part --- */
98
99 switch (type) {
100 case ADDR_SRC:
101 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "port") == 0)
102 token(sc);
103 ia->sin.sin_addr.s_addr = htonl(INADDR_ANY);
104 break;
105 case ADDR_DEST: {
106 struct hostent *h;
107 dstr d = DSTR_INIT;
108 conf_name(sc, '.', &d);
109 if ((h = gethostbyname(d.buf)) == 0)
110 error(sc, "couldn't resolve Internet address `%s'", d.buf);
111 memcpy(&ia->sin.sin_addr, h->h_addr, sizeof(struct in_addr));
112 dstr_destroy(&d);
113 if (sc->t == ':')
114 token(sc);
115 } break;
116 }
117
118 /* --- Read the port number --- */
119
120 {
121 struct servent *s;
122
123 if (sc->t != CTOK_WORD)
124 error(sc, "parse error, TCP port expected");
125 if (isdigit((unsigned char)sc->d.buf[0]))
126 ia->sin.sin_port = htons(atoi(sc->d.buf));
127 else if ((s = getservbyname(sc->d.buf, "tcp")) == 0)
128 error(sc, "unknown tcp service `%s'", sc->d.buf);
129 else
130 ia->sin.sin_port = s->s_port;
131 token(sc);
132 }
133
134 return (&ia->a);
135 }
136
137 /* --- @destroy@ --- */
138
139 static void inet_destroy(addr *a)
140 {
141 inet_addrx *ia = (inet_addrx *)a;
142 DESTROY(ia);
143 }
144
145 /* --- @print@ --- */
146
147 static void inet_print(addr *a, unsigned type, dstr *d)
148 {
149 inet_addrx *ia = (inet_addrx *)a;
150 switch (type) {
151 case ADDR_SRC:
152 dstr_putf(d, "inet:%u", (unsigned)ntohs(ia->sin.sin_port));
153 break;
154 case ADDR_DEST:
155 dstr_putf(d, "inet:%s:%u",
156 inet_ntoa(ia->sin.sin_addr),
157 (unsigned)ntohs(ia->sin.sin_port));
158 break;
159 }
160 }
161
162 /* --- @initopts@ --- */
163
164 static addr_opts *inet_initopts(void)
165 {
166 inet_opts *io = CREATE(inet_opts);
167 io->acl = 0;
168 io->acltail = &io->acl;
169 return (&io->ao);
170 }
171
172 /* --- @option@ --- */
173
174 static int inet_option(scanner *sc, addr_opts *ao)
175 {
176 inet_opts *io = (inet_opts *)ao;
177
178 CONF_BEGIN(sc, "inet", "Internet socket")
179
180 unsigned act;
181
182 /* --- Access control limitations --- */
183
184 if ((strcmp(sc->d.buf, "allow") == 0 && (act = ACL_ALLOW, 1)) ||
185 (strcmp(sc->d.buf, "deny") == 0 && (act = ACL_DENY, 1))) {
186 struct hostent *h;
187 struct netent *n;
188 struct in_addr a, m;
189 dstr d = DSTR_INIT;
190
191 /* --- Find the host or network address --- */
192
193 token(sc);
194 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "from") == 0)
195 token(sc);
196 conf_name(sc, '.', &d);
197 if ((n = getnetbyname(d.buf)) != 0)
198 a.s_addr = htonl(n->n_net);
199 else if ((h = gethostbyname(d.buf)) == 0)
200 error(sc, "couldn't resolve address `%s'", d.buf);
201 else
202 memcpy(&a, h->h_addr, sizeof(struct in_addr));
203
204 /* --- Find the netmask, if any --- */
205
206 if (sc->t != '/')
207 m.s_addr = ~0ul;
208 else {
209 token(sc);
210 DRESET(&d);
211 conf_name(sc, '.', &d);
212 if (strchr(d.buf, '.') == 0) {
213 int n = atoi(d.buf);
214 if (n == 0)
215 m.s_addr = 0;
216 else
217 m.s_addr = htonl((~0ul << (32 - n)) & 0xffffffff);
218 } else {
219 #ifdef HAVE_INET_ATON
220 if (!inet_aton(d.buf, &m))
221 error(sc, "bad netmask `%s'", d.buf);
222 #else
223 m.s_addr = inet_addr(d.buf);
224 #endif
225 }
226 }
227 dstr_destroy(&d);
228
229 /* --- Add the access control entry --- */
230
231 acl_add(io ? &io->acltail : 0, act, a, m);
232 CONF_ACCEPT;
233 }
234
235 /* --- Anything unrecognized --- */
236
237 CONF_END;
238 }
239
240 /* --- @accept@ --- */
241
242 static reffd *inet_accept(int fd, addr_opts *ao, const char *desc)
243 {
244 inet_opts *io = (inet_opts *)ao;
245 int nfd;
246 id_req q;
247 int lsinsz = sizeof(q.lsin), rsinsz = sizeof(q.rsin);
248
249 /* --- Accept the new connection --- */
250
251 if ((nfd = accept(fd, (struct sockaddr *)&q.rsin, &rsinsz)) < 0)
252 return (0);
253 if (getsockname(nfd, (struct sockaddr *)&q.lsin, &lsinsz)) {
254 close(nfd);
255 return (0);
256 }
257 q.desc = desc;
258 q.r = reffd_init(nfd);
259
260 /* --- Find out whether this connection is allowed --- */
261
262 if (!acl_check(io->acl, q.rsin.sin_addr)) {
263 q.act = "refused";
264 if (!(io->ao.f & ADDRF_NOLOG))
265 identify(&q);
266 REFFD_DEC(q.r);
267 return (0);
268 }
269
270 /* --- Everything seems to be OK --- */
271
272 q.act = "accepted";
273 if (!(io->ao.f & ADDRF_NOLOG))
274 identify(&q);
275 return (q.r);
276 }
277
278 /* --- @freeopts@ --- */
279
280 static void inet_freeopts(addr_opts *ao)
281 {
282 inet_opts *io = (inet_opts *)ao;
283 acl_free(io->acl);
284 DESTROY(ao);
285 }
286
287 /* --- Ops table --- */
288
289 addr_ops inet_ops = {
290 "inet", PF_INET,
291 inet_read, inet_destroy, inet_print,
292 inet_initopts, inet_option, inet_accept, inet_freeopts, 0, 0
293 };
294
295 /*----- That's all, folks -------------------------------------------------*/