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