| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: rule.c,v 1.8 2004/04/08 01:36:20 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 | /*----- Header files ------------------------------------------------------*/ |
| 30 | |
| 31 | /* --- ANSI headers --- */ |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | /* --- Unix headers --- */ |
| 38 | |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/socket.h> |
| 41 | #include <netinet/in.h> |
| 42 | #include <arpa/inet.h> |
| 43 | #include <netdb.h> |
| 44 | #include <unistd.h> |
| 45 | |
| 46 | /* --- mLib headers --- */ |
| 47 | |
| 48 | #include <mLib/alloc.h> |
| 49 | #include <mLib/report.h> |
| 50 | #include <mLib/trace.h> |
| 51 | |
| 52 | /* --- Local headers --- */ |
| 53 | |
| 54 | #include "become.h" |
| 55 | #include "class.h" |
| 56 | #include "rule.h" |
| 57 | #include "userdb.h" |
| 58 | |
| 59 | /*----- Static variables --------------------------------------------------*/ |
| 60 | |
| 61 | static rule *rule__list; /* List of rules */ |
| 62 | static rule *rule__tail; /* Pointer to last rule item */ |
| 63 | |
| 64 | /*----- Main code ---------------------------------------------------------*/ |
| 65 | |
| 66 | /* --- @rule_init@ --- * |
| 67 | * |
| 68 | * Arguments: --- |
| 69 | * |
| 70 | * Returns: --- |
| 71 | * |
| 72 | * Use: Intialises the rule database. |
| 73 | */ |
| 74 | |
| 75 | void rule_init(void) |
| 76 | { |
| 77 | rule__list = 0; |
| 78 | rule__tail = (rule *)&rule__list; |
| 79 | } |
| 80 | |
| 81 | /* --- @rule_end@ --- * |
| 82 | * |
| 83 | * Arguments: --- |
| 84 | * |
| 85 | * Returns: --- |
| 86 | * |
| 87 | * Use: Empties the rule database. |
| 88 | */ |
| 89 | |
| 90 | void rule_end(void) |
| 91 | { |
| 92 | rule *r = rule__list; |
| 93 | rule *rr; |
| 94 | |
| 95 | while (r) { |
| 96 | rr = r->next; |
| 97 | class_dec(r->host); |
| 98 | class_dec(r->from); |
| 99 | class_dec(r->to); |
| 100 | class_dec(r->cmd); |
| 101 | free(r); |
| 102 | r = rr; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* --- @rule_list@ --- * |
| 107 | * |
| 108 | * Arguments: --- |
| 109 | * |
| 110 | * Returns: The list of rules. |
| 111 | * |
| 112 | * Use: Returns the address of the first node in the rule list. |
| 113 | */ |
| 114 | |
| 115 | rule *rule_list(void) |
| 116 | { |
| 117 | return (rule__list); |
| 118 | } |
| 119 | |
| 120 | /* --- @rule_add@ --- * |
| 121 | * |
| 122 | * Arguments: @class_node *host@ = class of hosts this rule applies to |
| 123 | * @class_node *from@ = class of users allowed to change |
| 124 | * @class_node *to@ = class of users allowed to be changed to |
| 125 | * @class_node *cmd@ = class of commands allowed |
| 126 | * |
| 127 | * Returns: --- |
| 128 | * |
| 129 | * Use: Registers another rule. |
| 130 | */ |
| 131 | |
| 132 | void rule_add(class_node *host, class_node *from, |
| 133 | class_node *to, class_node *cmd) |
| 134 | { |
| 135 | rule *r = xmalloc(sizeof(*r)); |
| 136 | |
| 137 | r->host = host; |
| 138 | r->from = from; |
| 139 | r->to = to; |
| 140 | r->cmd = cmd; |
| 141 | r->next = 0; |
| 142 | rule__tail->next = r; |
| 143 | rule__tail = r; |
| 144 | } |
| 145 | |
| 146 | /* --- @rule_check@ --- * |
| 147 | * |
| 148 | * Arguments: @request *r@ = pointer to a request block |
| 149 | * |
| 150 | * Returns: Zero if disallowed, nonzero if allowed. |
| 151 | * |
| 152 | * Use: Checks a request to see if it's allowed. |
| 153 | */ |
| 154 | |
| 155 | int rule_check(request *r) |
| 156 | { |
| 157 | rule *rr; |
| 158 | |
| 159 | /* --- Trace out the request we're checking --- */ |
| 160 | |
| 161 | IF_TRACING(TRACE_CHECK, { |
| 162 | struct passwd *pw_from = userdb_userById(r->from); |
| 163 | struct passwd *pw_to = userdb_userById(r->to); |
| 164 | struct hostent *h = gethostbyaddr((char *)&r->host, sizeof(r->host), |
| 165 | AF_INET); |
| 166 | |
| 167 | trace(TRACE_CHECK, "check: request from %s (%li) to become %s (%li)", |
| 168 | pw_from ? pw_from->pw_name : "<unknown>", (long)r->from, |
| 169 | pw_to ? pw_to->pw_name : "<unknown>", (long)r->to); |
| 170 | trace(TRACE_CHECK, "check: ... at %s (%s) for `%s'", |
| 171 | h ? h->h_name : "<unknown>", inet_ntoa(r->host), r->cmd); |
| 172 | }) |
| 173 | |
| 174 | /* --- Search the rule list --- */ |
| 175 | |
| 176 | for (rr = rule__list; rr; rr = rr->next) { |
| 177 | |
| 178 | /* --- Trace out the rule --- */ |
| 179 | |
| 180 | IF_TRACING(TRACE_RULE, { |
| 181 | trace(TRACE_RULE, "rule: check against rule..."); |
| 182 | trace(TRACE_RULE, "rule: from"); class_dump(rr->from, 2); |
| 183 | trace(TRACE_RULE, "rule: to"); class_dump(rr->to, 2); |
| 184 | trace(TRACE_RULE, "rule: cmd"); class_dump(rr->cmd, 2); |
| 185 | trace(TRACE_RULE, "rule: host"); class_dump(rr->host, 2); |
| 186 | }) |
| 187 | |
| 188 | /* --- Check the rule --- */ |
| 189 | |
| 190 | if (class_matchUser(rr->from, r->from) && |
| 191 | class_matchUser(rr->to, r->to) && |
| 192 | class_matchCommand(rr->cmd, r->cmd) && |
| 193 | class_matchHost(rr->host, r->host)) { |
| 194 | T( trace(TRACE_CHECK, "check: rule matched -- granting permission"); ) |
| 195 | return (1); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* --- Failed to match --- */ |
| 200 | |
| 201 | T( trace(TRACE_CHECK, "check: no rules matched -- permission denied"); ) |
| 202 | return (0); |
| 203 | } |
| 204 | |
| 205 | /* --- @rule_dump@ --- * |
| 206 | * |
| 207 | * Arguments: --- |
| 208 | * |
| 209 | * Returns: --- |
| 210 | * |
| 211 | * Use: Dumps a map of the current ruleset to the trace output. |
| 212 | */ |
| 213 | |
| 214 | void rule_dump(void) |
| 215 | { |
| 216 | #ifdef TRACING |
| 217 | rule *rr = rule__list; |
| 218 | |
| 219 | trace(TRACE_RULE, "rule: dumping rules"); |
| 220 | while (rr) { |
| 221 | trace(TRACE_RULE, "rule: rule dump..."); |
| 222 | trace(TRACE_RULE, "rule: from"); class_dump(rr->from, 2); |
| 223 | trace(TRACE_RULE, "rule: to"); class_dump(rr->to, 2); |
| 224 | trace(TRACE_RULE, "rule: cmd"); class_dump(rr->cmd, 2); |
| 225 | trace(TRACE_RULE, "rule: host"); class_dump(rr->host, 2); |
| 226 | rr = rr->next; |
| 227 | } |
| 228 | trace(TRACE_RULE, "rule: dump finished"); |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | /*----- That's all, folks -------------------------------------------------*/ |