3 * $Id: rule.c,v 1.3 1997/08/20 16:22:36 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.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.
36 * Revision 1.2 1997/08/04 10:24:25 mdw
37 * Sources placed under CVS control.
39 * Revision 1.1 1997/07/21 13:47:45 mdw
44 /*----- Header files ------------------------------------------------------*/
46 /* --- ANSI headers --- */
52 /* --- Unix headers --- */
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
61 /* --- Local headers --- */
69 /*----- Type definitions --------------------------------------------------*/
71 /* --- Rule block --- */
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 */
81 /*----- Static variables --------------------------------------------------*/
83 static rule
*rule__list
; /* List of rules */
84 static rule
*rule__tail
; /* Pointer to last rule item */
86 /*----- Main code ---------------------------------------------------------*/
88 /* --- @rule_init@ --- *
94 * Use: Intialises the rule database.
100 rule__tail
= (rule
*)&rule__list
;
103 /* --- @rule_end@ --- *
109 * Use: Empties the rule database.
114 rule
*r
= rule__list
;
128 /* --- @rule_add@ --- *
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
137 * Use: Registers another rule.
140 void rule_add(classdef
*host
, classdef
*from
, classdef
*to
, classdef
*cmd
)
142 rule
*r
= xmalloc(sizeof(*r
));
149 rule__tail
->next
= r
;
153 /* --- @rule_check@ --- *
155 * Arguments: @request *r@ = pointer to a request block
157 * Returns: Zero if disallowed, nonzero if allowed.
159 * Use: Checks a request to see if it's allowed.
162 int rule_check(request
*r
)
166 /* --- Trace out the request we're checking --- */
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
),
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
);
181 /* --- Search the rule list --- */
183 for (rr
= rule__list
; rr
; rr
= rr
->next
) {
185 /* --- Trace out the rule --- */
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
);
195 /* --- Check the rule --- */
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"); )
206 /* --- Failed to match --- */
208 T( trace(TRACE_CHECK
, "check: no rules matched -- permission denied"); )
212 /* --- @rule_dump@ --- *
218 * Use: Dumps a map of the current ruleset to the trace output.
224 rule
*rr
= rule__list
;
226 trace(TRACE_RULE
, "rule: dumping rules");
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
);
235 trace(TRACE_RULE
, "rule: dump finished");
239 /*----- That's all, folks -------------------------------------------------*/