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