| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: rule.c,v 1.7 2003/10/12 00:14:55 mdw Exp $ |
| 4 | * |
| 5 | * Managing rule sets |
| 6 | * |
| 7 | * (c) 1998 EBI |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of `become' |
| 13 | * |
| 14 | * `Become' 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 | * `Become' 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 `become'; 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: rule.c,v $ |
| 32 | * Revision 1.7 2003/10/12 00:14:55 mdw |
| 33 | * Major overhaul. Now uses DSA signatures rather than the bogus symmetric |
| 34 | * encrypt-and-hope thing. Integrated with mLib and Catacomb. |
| 35 | * |
| 36 | * Revision 1.6 1998/04/23 13:27:31 mdw |
| 37 | * Export structure of the rule list, for `bcquery's benefit. |
| 38 | * |
| 39 | * Revision 1.5 1998/01/12 16:46:25 mdw |
| 40 | * Fix copyright date. |
| 41 | * |
| 42 | * Revision 1.4 1997/09/17 10:27:17 mdw |
| 43 | * Use rewritten class handler. |
| 44 | * |
| 45 | * Revision 1.3 1997/08/20 16:22:36 mdw |
| 46 | * Rename `rule_reinit' to `rule_end' for more sensible restart. Don't try |
| 47 | * to trace when tracing's turned off. |
| 48 | * |
| 49 | * Revision 1.2 1997/08/04 10:24:25 mdw |
| 50 | * Sources placed under CVS control. |
| 51 | * |
| 52 | * Revision 1.1 1997/07/21 13:47:45 mdw |
| 53 | * Initial revision |
| 54 | * |
| 55 | */ |
| 56 | |
| 57 | /*----- Header files ------------------------------------------------------*/ |
| 58 | |
| 59 | /* --- ANSI headers --- */ |
| 60 | |
| 61 | #include <stdio.h> |
| 62 | #include <stdlib.h> |
| 63 | #include <string.h> |
| 64 | |
| 65 | /* --- Unix headers --- */ |
| 66 | |
| 67 | #include <sys/types.h> |
| 68 | #include <sys/socket.h> |
| 69 | #include <netinet/in.h> |
| 70 | #include <arpa/inet.h> |
| 71 | #include <netdb.h> |
| 72 | #include <unistd.h> |
| 73 | |
| 74 | /* --- mLib headers --- */ |
| 75 | |
| 76 | #include <mLib/alloc.h> |
| 77 | #include <mLib/report.h> |
| 78 | #include <mLib/trace.h> |
| 79 | |
| 80 | /* --- Local headers --- */ |
| 81 | |
| 82 | #include "become.h" |
| 83 | #include "class.h" |
| 84 | #include "rule.h" |
| 85 | #include "userdb.h" |
| 86 | |
| 87 | /*----- Static variables --------------------------------------------------*/ |
| 88 | |
| 89 | static rule *rule__list; /* List of rules */ |
| 90 | static rule *rule__tail; /* Pointer to last rule item */ |
| 91 | |
| 92 | /*----- Main code ---------------------------------------------------------*/ |
| 93 | |
| 94 | /* --- @rule_init@ --- * |
| 95 | * |
| 96 | * Arguments: --- |
| 97 | * |
| 98 | * Returns: --- |
| 99 | * |
| 100 | * Use: Intialises the rule database. |
| 101 | */ |
| 102 | |
| 103 | void rule_init(void) |
| 104 | { |
| 105 | rule__list = 0; |
| 106 | rule__tail = (rule *)&rule__list; |
| 107 | } |
| 108 | |
| 109 | /* --- @rule_end@ --- * |
| 110 | * |
| 111 | * Arguments: --- |
| 112 | * |
| 113 | * Returns: --- |
| 114 | * |
| 115 | * Use: Empties the rule database. |
| 116 | */ |
| 117 | |
| 118 | void rule_end(void) |
| 119 | { |
| 120 | rule *r = rule__list; |
| 121 | rule *rr; |
| 122 | |
| 123 | while (r) { |
| 124 | rr = r->next; |
| 125 | class_dec(r->host); |
| 126 | class_dec(r->from); |
| 127 | class_dec(r->to); |
| 128 | class_dec(r->cmd); |
| 129 | free(r); |
| 130 | r = rr; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* --- @rule_list@ --- * |
| 135 | * |
| 136 | * Arguments: --- |
| 137 | * |
| 138 | * Returns: The list of rules. |
| 139 | * |
| 140 | * Use: Returns the address of the first node in the rule list. |
| 141 | */ |
| 142 | |
| 143 | rule *rule_list(void) |
| 144 | { |
| 145 | return (rule__list); |
| 146 | } |
| 147 | |
| 148 | /* --- @rule_add@ --- * |
| 149 | * |
| 150 | * Arguments: @class_node *host@ = class of hosts this rule applies to |
| 151 | * @class_node *from@ = class of users allowed to change |
| 152 | * @class_node *to@ = class of users allowed to be changed to |
| 153 | * @class_node *cmd@ = class of commands allowed |
| 154 | * |
| 155 | * Returns: --- |
| 156 | * |
| 157 | * Use: Registers another rule. |
| 158 | */ |
| 159 | |
| 160 | void rule_add(class_node *host, class_node *from, |
| 161 | class_node *to, class_node *cmd) |
| 162 | { |
| 163 | rule *r = xmalloc(sizeof(*r)); |
| 164 | |
| 165 | r->host = host; |
| 166 | r->from = from; |
| 167 | r->to = to; |
| 168 | r->cmd = cmd; |
| 169 | r->next = 0; |
| 170 | rule__tail->next = r; |
| 171 | rule__tail = r; |
| 172 | } |
| 173 | |
| 174 | /* --- @rule_check@ --- * |
| 175 | * |
| 176 | * Arguments: @request *r@ = pointer to a request block |
| 177 | * |
| 178 | * Returns: Zero if disallowed, nonzero if allowed. |
| 179 | * |
| 180 | * Use: Checks a request to see if it's allowed. |
| 181 | */ |
| 182 | |
| 183 | int rule_check(request *r) |
| 184 | { |
| 185 | rule *rr; |
| 186 | |
| 187 | /* --- Trace out the request we're checking --- */ |
| 188 | |
| 189 | IF_TRACING(TRACE_CHECK, { |
| 190 | struct passwd *pw_from = userdb_userById(r->from); |
| 191 | struct passwd *pw_to = userdb_userById(r->to); |
| 192 | struct hostent *h = gethostbyaddr((char *)&r->host, sizeof(r->host), |
| 193 | AF_INET); |
| 194 | |
| 195 | trace(TRACE_CHECK, "check: request from %s (%li) to become %s (%li)", |
| 196 | pw_from ? pw_from->pw_name : "<unknown>", (long)r->from, |
| 197 | pw_to ? pw_to->pw_name : "<unknown>", (long)r->to); |
| 198 | trace(TRACE_CHECK, "check: ... at %s (%s) for `%s'", |
| 199 | h ? h->h_name : "<unknown>", inet_ntoa(r->host), r->cmd); |
| 200 | }) |
| 201 | |
| 202 | /* --- Search the rule list --- */ |
| 203 | |
| 204 | for (rr = rule__list; rr; rr = rr->next) { |
| 205 | |
| 206 | /* --- Trace out the rule --- */ |
| 207 | |
| 208 | IF_TRACING(TRACE_RULE, { |
| 209 | trace(TRACE_RULE, "rule: check against rule..."); |
| 210 | trace(TRACE_RULE, "rule: from"); class_dump(rr->from, 2); |
| 211 | trace(TRACE_RULE, "rule: to"); class_dump(rr->to, 2); |
| 212 | trace(TRACE_RULE, "rule: cmd"); class_dump(rr->cmd, 2); |
| 213 | trace(TRACE_RULE, "rule: host"); class_dump(rr->host, 2); |
| 214 | }) |
| 215 | |
| 216 | /* --- Check the rule --- */ |
| 217 | |
| 218 | if (class_matchUser(rr->from, r->from) && |
| 219 | class_matchUser(rr->to, r->to) && |
| 220 | class_matchCommand(rr->cmd, r->cmd) && |
| 221 | class_matchHost(rr->host, r->host)) { |
| 222 | T( trace(TRACE_CHECK, "check: rule matched -- granting permission"); ) |
| 223 | return (1); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* --- Failed to match --- */ |
| 228 | |
| 229 | T( trace(TRACE_CHECK, "check: no rules matched -- permission denied"); ) |
| 230 | return (0); |
| 231 | } |
| 232 | |
| 233 | /* --- @rule_dump@ --- * |
| 234 | * |
| 235 | * Arguments: --- |
| 236 | * |
| 237 | * Returns: --- |
| 238 | * |
| 239 | * Use: Dumps a map of the current ruleset to the trace output. |
| 240 | */ |
| 241 | |
| 242 | void rule_dump(void) |
| 243 | { |
| 244 | #ifdef TRACING |
| 245 | rule *rr = rule__list; |
| 246 | |
| 247 | trace(TRACE_RULE, "rule: dumping rules"); |
| 248 | while (rr) { |
| 249 | trace(TRACE_RULE, "rule: rule dump..."); |
| 250 | trace(TRACE_RULE, "rule: from"); class_dump(rr->from, 2); |
| 251 | trace(TRACE_RULE, "rule: to"); class_dump(rr->to, 2); |
| 252 | trace(TRACE_RULE, "rule: cmd"); class_dump(rr->cmd, 2); |
| 253 | trace(TRACE_RULE, "rule: host"); class_dump(rr->host, 2); |
| 254 | rr = rr->next; |
| 255 | } |
| 256 | trace(TRACE_RULE, "rule: dump finished"); |
| 257 | #endif |
| 258 | } |
| 259 | |
| 260 | /*----- That's all, folks -------------------------------------------------*/ |