3 * $Id: rule.c,v 1.2 1997/08/04 10:24:25 mdw Exp $
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1997/08/04 10:24:25 mdw
33 * Sources placed under CVS control.
35 * Revision 1.1 1997/07/21 13:47:45 mdw
40 /*----- Header files ------------------------------------------------------*/
42 /* --- ANSI headers --- */
48 /* --- Unix headers --- */
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
57 /* --- Local headers --- */
65 /*----- Type definitions --------------------------------------------------*/
67 /* --- Rule block --- */
70 struct rule *next; /* Next rule in the list */
71 classdef *host; /* Hosts this rule applies to */
72 classdef *from; /* From users in this class */
73 classdef *to; /* To users in this class */
74 classdef *cmd; /* To run commands in this class */
77 /*----- Static variables --------------------------------------------------*/
79 static rule *rule__list; /* List of rules */
80 static rule *rule__tail; /* Pointer to last rule item */
82 /*----- Main code ---------------------------------------------------------*/
84 /* --- @rule_init@ --- *
90 * Use: Intialises the rule database.
96 rule__tail = (rule *)&rule__list;
99 /* --- @rule_reinit@ --- *
105 * Use: Reinitialises the rule database.
108 void rule_reinit(void)
110 rule *r = rule__list;
122 /* --- @rule_add@ --- *
124 * Arguments: @classdef *host@ = class of hosts this rule applies to
125 * @classdef *from@ = class of users allowed to change
126 * @classdef *to@ = class of users allowed to be changed to
127 * @classdef *cmd@ = class of commands allowed
131 * Use: Registers another rule.
134 void rule_add(classdef *host, classdef *from, classdef *to, classdef *cmd)
136 rule *r = xmalloc(sizeof(*r));
143 rule__tail->next = r;
147 /* --- @rule_check@ --- *
149 * Arguments: @request *r@ = pointer to a request block
151 * Returns: Zero if disallowed, nonzero if allowed.
153 * Use: Checks a request to see if it's allowed.
156 int rule_check(request *r)
160 /* --- Trace out the request we're checking --- */
162 IF_TRACING(TRACE_CHECK, {
163 struct passwd *pw_from = userdb_userById(r->from);
164 struct passwd *pw_to = userdb_userById(r->to);
165 struct hostent *h = gethostbyaddr((char *)&r->host, sizeof(r->host),
168 trace(TRACE_CHECK, "check: request from %s (%li) to become %s (%li)",
169 pw_from ? pw_from->pw_name : "<unknown>", (long)r->from,
170 pw_to ? pw_to->pw_name : "<unknown>", (long)r->to);
171 trace(TRACE_CHECK, "check: ... at %s (%s) for `%s'",
172 h ? h->h_name : "<unknown>", inet_ntoa(r->host), r->cmd);
175 /* --- Search the rule list --- */
177 for (rr = rule__list; rr; rr = rr->next) {
179 /* --- Trace out the rule --- */
181 IF_TRACING(TRACE_RULE, {
182 trace(TRACE_RULE, "rule: check against rule...");
183 trace(TRACE_RULE, " from"); class_dump(rr->from);
184 trace(TRACE_RULE, " to"); class_dump(rr->to);
185 trace(TRACE_RULE, " cmd"); class_dump(rr->cmd);
186 trace(TRACE_RULE, " host"); class_dump(rr->host);
189 /* --- Check the rule --- */
191 if (class_userMatch(rr->from, r->from) &&
192 class_userMatch(rr->to, r->to) &&
193 class_commandMatch(rr->cmd, r->cmd) &&
194 class_hostMatch(rr->host, r->host)) {
195 T( trace(TRACE_CHECK, "check: rule matched -- granting permission"); )
200 /* --- Failed to match --- */
202 T( trace(TRACE_CHECK, "check: no rules matched -- permission denied"); )
206 /* --- @rule_dump@ --- *
212 * Use: Dumps a map of the current ruleset to the trace output.
217 rule *rr = rule__list;
219 trace(TRACE_RULE, "rule: dumping rules");
221 trace(TRACE_RULE, "rule dump...");
222 trace(TRACE_RULE, " from"); class_dump(rr->from);
223 trace(TRACE_RULE, " to"); class_dump(rr->to);
224 trace(TRACE_RULE, " cmd"); class_dump(rr->cmd);
225 trace(TRACE_RULE, " host"); class_dump(rr->host);
228 trace(TRACE_RULE, "rule: dump finished");
231 /*----- That's all, folks -------------------------------------------------*/