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