e82f7154 |
1 | /* -*-c-*- |
2 | * |
7481fc9c |
3 | * $Id: acl.c,v 1.5 2004/04/08 01:36:25 mdw Exp $ |
e82f7154 |
4 | * |
5 | * Access control list handling |
6 | * |
e5398e09 |
7 | * (c) 1999 Straylight/Edgeware |
e82f7154 |
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 | |
e82f7154 |
29 | /*----- Header files ------------------------------------------------------*/ |
30 | |
31 | #include "config.h" |
32 | |
33 | #include <errno.h> |
34 | #include <stdio.h> |
35 | #include <stdlib.h> |
36 | #include <string.h> |
37 | |
e0ce9d38 |
38 | #include <sys/types.h> |
39 | #include <unistd.h> |
40 | |
e82f7154 |
41 | #include <sys/socket.h> |
42 | #include <netinet/in.h> |
43 | #include <arpa/inet.h> |
44 | #include <netdb.h> |
45 | |
46 | #include <mLib/sub.h> |
47 | |
48 | #include "acl.h" |
49 | |
0ac54f22 |
50 | /*----- Main code ---------------------------------------------------------*/ |
51 | |
52 | /* --- @acl_addhost@ --- * |
53 | * |
54 | * Arguments: @acl_entry ***a@ = address of pointer to list tail |
55 | * @unsigned act@ = what to do with matching addresses |
56 | * @struct in_addr addr, mask@ = address and mask to match |
57 | * |
58 | * Returns: --- |
59 | * |
60 | * Use: Adds a host-authentication entry to the end of an access |
61 | * control list. |
62 | */ |
63 | |
64 | static int acl_checkhost(void *aa, struct in_addr addr, unsigned port) |
65 | { |
66 | acl_host *a = aa; |
67 | return ((addr.s_addr & a->mask.s_addr) == a->addr.s_addr); |
68 | } |
e82f7154 |
69 | |
0ac54f22 |
70 | static void acl_dumphost(void *aa, FILE *fp) |
71 | { |
72 | acl_host *a = aa; |
73 | |
74 | fputs("from ", fp); |
75 | fputs(inet_ntoa(a->addr), fp); |
76 | fputc('/', fp); |
77 | fputs(inet_ntoa(a->mask), fp); |
78 | } |
e82f7154 |
79 | |
0ac54f22 |
80 | static void acl_freehost(void *aa) |
81 | { |
82 | acl_host *a = aa; |
83 | DESTROY(a); |
84 | } |
85 | |
86 | static const acl_ops acl_hostops = { |
87 | acl_checkhost, acl_dumphost, acl_freehost |
88 | }; |
89 | |
90 | void acl_addhost(acl_entry ***a, unsigned act, |
91 | struct in_addr addr, struct in_addr mask) |
92 | { |
93 | acl_host *aa = CREATE(acl_host); |
94 | aa->a.next = 0; |
95 | aa->a.ops = &acl_hostops; |
96 | aa->a.act = act; |
97 | aa->addr.s_addr = addr.s_addr & mask.s_addr; |
98 | aa->mask = mask; |
99 | **a = &aa->a; |
100 | *a = &aa->a.next; |
101 | } |
102 | |
103 | /* --- @acl_addpriv@ --- * |
104 | * |
105 | * Arguments: @acl_entry ***a@ = address of pointer to list tail |
106 | * @unsigned act@ = what to do with matching addresses |
107 | * |
108 | * Returns: --- |
109 | * |
110 | * Use: Adds a privileged-port check to the end of an access control |
111 | * list. |
112 | */ |
113 | |
114 | static int acl_checkpriv(void *aa, struct in_addr addr, unsigned port) |
115 | { |
116 | return (port < 1024); |
117 | } |
118 | |
119 | static void acl_dumppriv(void *aa, FILE *fp) |
120 | { |
121 | fputs("from privileged ports", fp); |
122 | } |
123 | |
124 | static void acl_freepriv(void *aa) |
125 | { |
126 | acl_entry *a = aa; |
127 | DESTROY(a); |
128 | } |
129 | |
130 | static const acl_ops acl_privops = { |
131 | acl_checkpriv, acl_dumppriv, acl_freepriv |
132 | }; |
133 | |
134 | void acl_addpriv(acl_entry ***a, unsigned act) |
135 | { |
136 | acl_entry *aa = CREATE(acl_entry); |
137 | aa->next = 0; |
138 | aa->ops = &acl_privops; |
139 | aa->act = act; |
140 | **a = aa; |
141 | *a = &aa->next; |
142 | } |
e82f7154 |
143 | |
144 | /* --- @acl_check@ --- * |
145 | * |
146 | * Arguments: @acl_entry *a@ = pointer to ACL to check against |
147 | * @struct in_addr addr@ = address to check |
0ac54f22 |
148 | * @unsigned port@ = port number to check |
149 | * @int *act@ = verdict (should initially be @ACT_ALLOW@) |
e82f7154 |
150 | * |
0ac54f22 |
151 | * Returns: Zero if undecided, nonzero if a rule matched. |
e82f7154 |
152 | * |
153 | * Use: Checks an address against an ACL. |
154 | */ |
155 | |
0ac54f22 |
156 | int acl_check(acl_entry *a, struct in_addr addr, unsigned port, int *act) |
e82f7154 |
157 | { |
0ac54f22 |
158 | for (; a; a = a->next) { |
159 | if (a->ops->check(a, addr, port)) { |
160 | *act = a->act & ACL_PERM; |
161 | return (1); |
e82f7154 |
162 | } |
0ac54f22 |
163 | *act = (a->act & ACL_PERM) ^ 1; |
e82f7154 |
164 | } |
0ac54f22 |
165 | return (0); |
e82f7154 |
166 | } |
167 | |
168 | /* --- @acl_dump@ --- * |
169 | * |
170 | * Arguments: @acl_entry *a@ = pointer to ACL to dump |
171 | * @FILE *fp@ = pointer to stream to dump on |
172 | * |
173 | * Returns: --- |
174 | * |
175 | * Use: Dumps an access control list to an output stream. |
176 | */ |
177 | |
178 | void acl_dump(acl_entry *a, FILE *fp) |
179 | { |
e82f7154 |
180 | for (; a; a = a->next) { |
0ac54f22 |
181 | fprintf(fp, " %s ", |
e82f7154 |
182 | (a->act & ACL_PERM) == ACL_ALLOW ? "allow" : "deny"); |
0ac54f22 |
183 | a->ops->dump(a, fp); |
e82f7154 |
184 | fputc('\n', fp); |
185 | } |
186 | } |
187 | |
e5398e09 |
188 | /* --- @acl_free@ --- * |
189 | * |
190 | * Arguments: @acl_entry *a@ = pointer to a list of ACLs |
191 | * |
192 | * Returns: --- |
193 | * |
194 | * Use: Frees all of the memory used by an ACL. |
195 | */ |
196 | |
197 | void acl_free(acl_entry *a) |
198 | { |
199 | while (a) { |
200 | acl_entry *aa = a; |
201 | a = a->next; |
0ac54f22 |
202 | aa->ops->free(aa); |
e5398e09 |
203 | } |
204 | } |
205 | |
e82f7154 |
206 | /*----- That's all, folks -------------------------------------------------*/ |