3 * $Id: acl.c,v 1.4 2003/11/25 14:08:23 mdw Exp $
5 * Access control list handling
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.4 2003/11/25 14:08:23 mdw
33 * Debianization. Socket target options. Internet binding.
35 * Revision 1.3 1999/07/27 18:30:53 mdw
36 * Various minor portability fixes.
38 * Revision 1.2 1999/07/26 23:28:15 mdw
39 * Minor modifications for new design.
41 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
46 /*----- Header files ------------------------------------------------------*/
55 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
67 /*----- Main code ---------------------------------------------------------*/
69 /* --- @acl_addhost@ --- *
71 * Arguments: @acl_entry ***a@ = address of pointer to list tail
72 * @unsigned act@ = what to do with matching addresses
73 * @struct in_addr addr, mask@ = address and mask to match
77 * Use: Adds a host-authentication entry to the end of an access
81 static int acl_checkhost(void *aa
, struct in_addr addr
, unsigned port
)
84 return ((addr
.s_addr
& a
->mask
.s_addr
) == a
->addr
.s_addr
);
87 static void acl_dumphost(void *aa
, FILE *fp
)
92 fputs(inet_ntoa(a
->addr
), fp
);
94 fputs(inet_ntoa(a
->mask
), fp
);
97 static void acl_freehost(void *aa
)
103 static const acl_ops acl_hostops
= {
104 acl_checkhost
, acl_dumphost
, acl_freehost
107 void acl_addhost(acl_entry
***a
, unsigned act
,
108 struct in_addr addr
, struct in_addr mask
)
110 acl_host
*aa
= CREATE(acl_host
);
112 aa
->a
.ops
= &acl_hostops
;
114 aa
->addr
.s_addr
= addr
.s_addr
& mask
.s_addr
;
120 /* --- @acl_addpriv@ --- *
122 * Arguments: @acl_entry ***a@ = address of pointer to list tail
123 * @unsigned act@ = what to do with matching addresses
127 * Use: Adds a privileged-port check to the end of an access control
131 static int acl_checkpriv(void *aa
, struct in_addr addr
, unsigned port
)
133 return (port
< 1024);
136 static void acl_dumppriv(void *aa
, FILE *fp
)
138 fputs("from privileged ports", fp
);
141 static void acl_freepriv(void *aa
)
147 static const acl_ops acl_privops
= {
148 acl_checkpriv
, acl_dumppriv
, acl_freepriv
151 void acl_addpriv(acl_entry
***a
, unsigned act
)
153 acl_entry
*aa
= CREATE(acl_entry
);
155 aa
->ops
= &acl_privops
;
161 /* --- @acl_check@ --- *
163 * Arguments: @acl_entry *a@ = pointer to ACL to check against
164 * @struct in_addr addr@ = address to check
165 * @unsigned port@ = port number to check
166 * @int *act@ = verdict (should initially be @ACT_ALLOW@)
168 * Returns: Zero if undecided, nonzero if a rule matched.
170 * Use: Checks an address against an ACL.
173 int acl_check(acl_entry
*a
, struct in_addr addr
, unsigned port
, int *act
)
175 for (; a
; a
= a
->next
) {
176 if (a
->ops
->check(a
, addr
, port
)) {
177 *act
= a
->act
& ACL_PERM
;
180 *act
= (a
->act
& ACL_PERM
) ^ 1;
185 /* --- @acl_dump@ --- *
187 * Arguments: @acl_entry *a@ = pointer to ACL to dump
188 * @FILE *fp@ = pointer to stream to dump on
192 * Use: Dumps an access control list to an output stream.
195 void acl_dump(acl_entry
*a
, FILE *fp
)
197 for (; a
; a
= a
->next
) {
199 (a
->act
& ACL_PERM
) == ACL_ALLOW ?
"allow" : "deny");
205 /* --- @acl_free@ --- *
207 * Arguments: @acl_entry *a@ = pointer to a list of ACLs
211 * Use: Frees all of the memory used by an ACL.
214 void acl_free(acl_entry
*a
)
223 /*----- That's all, folks -------------------------------------------------*/