3 * Access control list handling
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fwd' port forwarder.
12 * `fwd' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * `fwd' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with `fwd'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Main code ---------------------------------------------------------*/
31 /* --- @acl_addhost@ --- *
33 * Arguments: @acl_entry ***a@ = address of pointer to list tail
34 * @unsigned act@ = what to do with matching addresses
35 * @struct in_addr addr, mask@ = address and mask to match
39 * Use: Adds a host-authentication entry to the end of an access
43 static int acl_checkhost(void *aa
, struct in_addr addr
, unsigned port
)
46 return ((addr
.s_addr
& a
->mask
.s_addr
) == a
->addr
.s_addr
);
49 static void acl_dumphost(void *aa
, FILE *fp
)
54 fputs(inet_ntoa(a
->addr
), fp
);
56 fputs(inet_ntoa(a
->mask
), fp
);
59 static void acl_freehost(void *aa
)
65 static const acl_ops acl_hostops
= {
66 acl_checkhost
, acl_dumphost
, acl_freehost
69 void acl_addhost(acl_entry
***a
, unsigned act
,
70 struct in_addr addr
, struct in_addr mask
)
72 acl_host
*aa
= CREATE(acl_host
);
74 aa
->a
.ops
= &acl_hostops
;
76 aa
->addr
.s_addr
= addr
.s_addr
& mask
.s_addr
;
82 /* --- @acl_addpriv@ --- *
84 * Arguments: @acl_entry ***a@ = address of pointer to list tail
85 * @unsigned act@ = what to do with matching addresses
89 * Use: Adds a privileged-port check to the end of an access control
93 static int acl_checkpriv(void *aa
, struct in_addr addr
, unsigned port
)
98 static void acl_dumppriv(void *aa
, FILE *fp
)
100 fputs("from privileged ports", fp
);
103 static void acl_freepriv(void *aa
)
109 static const acl_ops acl_privops
= {
110 acl_checkpriv
, acl_dumppriv
, acl_freepriv
113 void acl_addpriv(acl_entry
***a
, unsigned act
)
115 acl_entry
*aa
= CREATE(acl_entry
);
117 aa
->ops
= &acl_privops
;
123 /* --- @acl_check@ --- *
125 * Arguments: @acl_entry *a@ = pointer to ACL to check against
126 * @struct in_addr addr@ = address to check
127 * @unsigned port@ = port number to check
128 * @int *act@ = verdict (should initially be @ACT_ALLOW@)
130 * Returns: Zero if undecided, nonzero if a rule matched.
132 * Use: Checks an address against an ACL.
135 int acl_check(acl_entry
*a
, struct in_addr addr
, unsigned port
, int *act
)
137 for (; a
; a
= a
->next
) {
138 if (a
->ops
->check(a
, addr
, port
)) {
139 *act
= a
->act
& ACL_PERM
;
142 *act
= (a
->act
& ACL_PERM
) ^ 1;
147 /* --- @acl_dump@ --- *
149 * Arguments: @acl_entry *a@ = pointer to ACL to dump
150 * @FILE *fp@ = pointer to stream to dump on
154 * Use: Dumps an access control list to an output stream.
157 void acl_dump(acl_entry
*a
, FILE *fp
)
159 for (; a
; a
= a
->next
) {
161 (a
->act
& ACL_PERM
) == ACL_ALLOW ?
"allow" : "deny");
167 /* --- @acl_free@ --- *
169 * Arguments: @acl_entry *a@ = pointer to a list of ACLs
173 * Use: Frees all of the memory used by an ACL.
176 void acl_free(acl_entry
*a
)
185 /*----- That's all, folks -------------------------------------------------*/