Initial revision
[fwd] / acl.c
CommitLineData
e82f7154 1/* -*-c-*-
2 *
3 * $Id: acl.c,v 1.1 1999/07/01 08:56:23 mdw Exp $
4 *
5 * Access control list handling
6 *
7 * (c) 1999 Mark Wooding
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: acl.c,v $
32 * Revision 1.1 1999/07/01 08:56:23 mdw
33 * Initial revision
34 *
35 */
36
37/*----- Header files ------------------------------------------------------*/
38
39#include "config.h"
40
41#include <errno.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45
46#include <sys/socket.h>
47#include <netinet/in.h>
48#include <arpa/inet.h>
49#include <netdb.h>
50
51#include <mLib/sub.h>
52
53#include "acl.h"
54
55/*----- Static variables --------------------------------------------------*/
56
57static acl_entry *global = 0;
58static acl_entry **gtail = &global;
59
60/*----- Main code ---------------------------------------------------------*/
61
62/* --- @acl_check@ --- *
63 *
64 * Arguments: @acl_entry *a@ = pointer to ACL to check against
65 * @struct in_addr addr@ = address to check
66 *
67 * Returns: Nonzero if allowed.
68 *
69 * Use: Checks an address against an ACL.
70 */
71
72int acl_check(acl_entry *a, struct in_addr addr)
73{
74 int act = ACL_ALLOW;
75 int i;
76
77 for (i = 0; i < 2; i++) {
78 for (; a; a = a->next) {
79 if ((addr.s_addr & a->mask.s_addr) == a->addr.s_addr)
80 return (a->act & ACL_PERM);
81 act = (a->act & ACL_PERM) ^ 1;
82 }
83 a = global;
84 }
85
86 return (act);
87}
88
89/* --- @acl_dump@ --- *
90 *
91 * Arguments: @acl_entry *a@ = pointer to ACL to dump
92 * @FILE *fp@ = pointer to stream to dump on
93 *
94 * Returns: ---
95 *
96 * Use: Dumps an access control list to an output stream.
97 */
98
99void acl_dump(acl_entry *a, FILE *fp)
100{
101 if (!a)
102 a = global;
103 for (; a; a = a->next) {
104 fprintf(fp, " %s from ",
105 (a->act & ACL_PERM) == ACL_ALLOW ? "allow" : "deny");
106 fputs(inet_ntoa(a->addr), fp);
107 fputc('/', fp);
108 fputs(inet_ntoa(a->mask), fp);
109 fputc('\n', fp);
110 }
111}
112
113/* --- @acl_add@ --- *
114 *
115 * Arguments: @acl_entry ***a@ = address of pointer to list tail
116 * @unsigned act@ = what to do with matching addresses
117 * @struct in_addr addr, mask@ = address and mask to match
118 *
119 * Returns: ---
120 *
121 * Use: Adds an entry to the end of an access control list.
122 */
123
124void acl_add(acl_entry ***a, unsigned act,
125 struct in_addr addr, struct in_addr mask)
126{
127 acl_entry *aa = CREATE(acl_entry);
128 aa->act = act;
129 aa->addr.s_addr = addr.s_addr & mask.s_addr;
130 aa->mask = mask;
131 aa->next = 0;
132 if (!a)
133 a = &gtail;
134 **a = aa;
135 *a = &aa->next;
136}
137
138/*----- That's all, folks -------------------------------------------------*/