chiark / gitweb /
docs: Generate grammar and option summaries from manpage.
[fwd] / acl.c
1 /* -*-c-*-
2  *
3  * $Id: acl.c,v 1.5 2004/04/08 01:36:25 mdw Exp $
4  *
5  * Access control list handling
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 /*----- 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
38 #include <sys/types.h>
39 #include <unistd.h>
40
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
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 }
69
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 }
79
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 }
143
144 /* --- @acl_check@ --- *
145  *
146  * Arguments:   @acl_entry *a@ = pointer to ACL to check against
147  *              @struct in_addr addr@ = address to check
148  *              @unsigned port@ = port number to check
149  *              @int *act@ = verdict (should initially be @ACT_ALLOW@)
150  *
151  * Returns:     Zero if undecided, nonzero if a rule matched.
152  *
153  * Use:         Checks an address against an ACL.
154  */
155
156 int acl_check(acl_entry *a, struct in_addr addr, unsigned port, int *act)
157 {
158   for (; a; a = a->next) {
159     if (a->ops->check(a, addr, port)) {
160       *act = a->act & ACL_PERM;
161       return (1);
162     }
163     *act = (a->act & ACL_PERM) ^ 1;
164   }
165   return (0);
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 {
180   for (; a; a = a->next) {
181     fprintf(fp, "  %s ",
182             (a->act & ACL_PERM) == ACL_ALLOW ? "allow" : "deny");
183     a->ops->dump(a, fp);
184     fputc('\n', fp);
185   }
186 }
187
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;
202     aa->ops->free(aa);
203   }
204 }
205
206 /*----- That's all, folks -------------------------------------------------*/