| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: acl.c,v 1.3 1999/07/27 18:30:53 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 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: acl.c,v $ |
| 32 | * Revision 1.3 1999/07/27 18:30:53 mdw |
| 33 | * Various minor portability fixes. |
| 34 | * |
| 35 | * Revision 1.2 1999/07/26 23:28:15 mdw |
| 36 | * Minor modifications for new design. |
| 37 | * |
| 38 | * Revision 1.1.1.1 1999/07/01 08:56:23 mdw |
| 39 | * Initial revision. |
| 40 | * |
| 41 | */ |
| 42 | |
| 43 | /*----- Header files ------------------------------------------------------*/ |
| 44 | |
| 45 | #include "config.h" |
| 46 | |
| 47 | #include <errno.h> |
| 48 | #include <stdio.h> |
| 49 | #include <stdlib.h> |
| 50 | #include <string.h> |
| 51 | |
| 52 | #include <sys/types.h> |
| 53 | #include <unistd.h> |
| 54 | |
| 55 | #include <sys/socket.h> |
| 56 | #include <netinet/in.h> |
| 57 | #include <arpa/inet.h> |
| 58 | #include <netdb.h> |
| 59 | |
| 60 | #include <mLib/sub.h> |
| 61 | |
| 62 | #include "acl.h" |
| 63 | |
| 64 | /*----- Static variables --------------------------------------------------*/ |
| 65 | |
| 66 | static acl_entry *global = 0; |
| 67 | static acl_entry **gtail = &global; |
| 68 | |
| 69 | /*----- Main code ---------------------------------------------------------*/ |
| 70 | |
| 71 | /* --- @acl_check@ --- * |
| 72 | * |
| 73 | * Arguments: @acl_entry *a@ = pointer to ACL to check against |
| 74 | * @struct in_addr addr@ = address to check |
| 75 | * |
| 76 | * Returns: Nonzero if allowed. |
| 77 | * |
| 78 | * Use: Checks an address against an ACL. |
| 79 | */ |
| 80 | |
| 81 | int acl_check(acl_entry *a, struct in_addr addr) |
| 82 | { |
| 83 | int act = ACL_ALLOW; |
| 84 | int i; |
| 85 | |
| 86 | for (i = 0; i < 2; i++) { |
| 87 | for (; a; a = a->next) { |
| 88 | if ((addr.s_addr & a->mask.s_addr) == a->addr.s_addr) |
| 89 | return (a->act & ACL_PERM); |
| 90 | act = (a->act & ACL_PERM) ^ 1; |
| 91 | } |
| 92 | a = global; |
| 93 | } |
| 94 | |
| 95 | return (act); |
| 96 | } |
| 97 | |
| 98 | /* --- @acl_dump@ --- * |
| 99 | * |
| 100 | * Arguments: @acl_entry *a@ = pointer to ACL to dump |
| 101 | * @FILE *fp@ = pointer to stream to dump on |
| 102 | * |
| 103 | * Returns: --- |
| 104 | * |
| 105 | * Use: Dumps an access control list to an output stream. |
| 106 | */ |
| 107 | |
| 108 | void acl_dump(acl_entry *a, FILE *fp) |
| 109 | { |
| 110 | if (!a) |
| 111 | a = global; |
| 112 | for (; a; a = a->next) { |
| 113 | fprintf(fp, " %s from ", |
| 114 | (a->act & ACL_PERM) == ACL_ALLOW ? "allow" : "deny"); |
| 115 | fputs(inet_ntoa(a->addr), fp); |
| 116 | fputc('/', fp); |
| 117 | fputs(inet_ntoa(a->mask), fp); |
| 118 | fputc('\n', fp); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* --- @acl_free@ --- * |
| 123 | * |
| 124 | * Arguments: @acl_entry *a@ = pointer to a list of ACLs |
| 125 | * |
| 126 | * Returns: --- |
| 127 | * |
| 128 | * Use: Frees all of the memory used by an ACL. |
| 129 | */ |
| 130 | |
| 131 | void acl_free(acl_entry *a) |
| 132 | { |
| 133 | while (a) { |
| 134 | acl_entry *aa = a; |
| 135 | a = a->next; |
| 136 | DESTROY(aa); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* --- @acl_add@ --- * |
| 141 | * |
| 142 | * Arguments: @acl_entry ***a@ = address of pointer to list tail |
| 143 | * @unsigned act@ = what to do with matching addresses |
| 144 | * @struct in_addr addr, mask@ = address and mask to match |
| 145 | * |
| 146 | * Returns: --- |
| 147 | * |
| 148 | * Use: Adds an entry to the end of an access control list. |
| 149 | */ |
| 150 | |
| 151 | void acl_add(acl_entry ***a, unsigned act, |
| 152 | struct in_addr addr, struct in_addr mask) |
| 153 | { |
| 154 | acl_entry *aa = CREATE(acl_entry); |
| 155 | aa->act = act; |
| 156 | aa->addr.s_addr = addr.s_addr & mask.s_addr; |
| 157 | aa->mask = mask; |
| 158 | aa->next = 0; |
| 159 | if (!a) |
| 160 | a = >ail; |
| 161 | **a = aa; |
| 162 | *a = &aa->next; |
| 163 | } |
| 164 | |
| 165 | /*----- That's all, folks -------------------------------------------------*/ |