Sources placed under CVS control.
[become] / src / rule.c
CommitLineData
c4f2d992 1/* -*-c-*-
2 *
03f996bd 3 * $Id: rule.c,v 1.2 1997/08/04 10:24:25 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 $
03f996bd 32 * Revision 1.2 1997/08/04 10:24:25 mdw
33 * Sources placed under CVS control.
34 *
35 * Revision 1.1 1997/07/21 13:47:45 mdw
c4f2d992 36 * Initial revision
37 *
38 */
39
40/*----- Header files ------------------------------------------------------*/
41
42/* --- ANSI headers --- */
43
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47
03f996bd 48/* --- Unix headers --- */
49
50#include <sys/types.h>
51#include <sys/socket.h>
52#include <netinet/in.h>
53#include <arpa/inet.h>
54#include <netdb.h>
55#include <unistd.h>
56
c4f2d992 57/* --- Local headers --- */
58
59#include "become.h"
60#include "class.h"
61#include "rule.h"
03f996bd 62#include "userdb.h"
c4f2d992 63#include "utils.h"
64
65/*----- Type definitions --------------------------------------------------*/
66
67/* --- Rule block --- */
68
69typedef struct rule {
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 */
75} rule;
76
77/*----- Static variables --------------------------------------------------*/
78
79static rule *rule__list; /* List of rules */
80static rule *rule__tail; /* Pointer to last rule item */
81
82/*----- Main code ---------------------------------------------------------*/
83
84/* --- @rule_init@ --- *
85 *
86 * Arguments: ---
87 *
88 * Returns: ---
89 *
90 * Use: Intialises the rule database.
91 */
92
93void rule_init(void)
94{
95 rule__list = 0;
96 rule__tail = (rule *)&rule__list;
97}
98
99/* --- @rule_reinit@ --- *
100 *
101 * Arguments: ---
102 *
103 * Returns: ---
104 *
105 * Use: Reinitialises the rule database.
106 */
107
108void rule_reinit(void)
109{
110 rule *r = rule__list;
111 rule *rr;
112
113 while (r) {
114 rr = r->next;
115 free(r);
116 r = rr;
117 }
118
119 rule_init();
120}
121
122/* --- @rule_add@ --- *
123 *
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
128 *
129 * Returns: ---
130 *
131 * Use: Registers another rule.
132 */
133
134void rule_add(classdef *host, classdef *from, classdef *to, classdef *cmd)
135{
136 rule *r = xmalloc(sizeof(*r));
137
138 r->host = host;
139 r->from = from;
140 r->to = to;
141 r->cmd = cmd;
142 r->next = 0;
143 rule__tail->next = r;
144 rule__tail = r;
145}
146
147/* --- @rule_check@ --- *
148 *
149 * Arguments: @request *r@ = pointer to a request block
150 *
151 * Returns: Zero if disallowed, nonzero if allowed.
152 *
153 * Use: Checks a request to see if it's allowed.
154 */
155
156int rule_check(request *r)
157{
03f996bd 158 rule *rr;
159
160 /* --- Trace out the request we're checking --- */
161
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),
166 AF_INET);
167
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);
173 })
174
175 /* --- Search the rule list --- */
176
177 for (rr = rule__list; rr; rr = rr->next) {
178
179 /* --- Trace out the rule --- */
180
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);
187 })
188
189 /* --- Check the rule --- */
c4f2d992 190
c4f2d992 191 if (class_userMatch(rr->from, r->from) &&
192 class_userMatch(rr->to, r->to) &&
193 class_commandMatch(rr->cmd, r->cmd) &&
03f996bd 194 class_hostMatch(rr->host, r->host)) {
195 T( trace(TRACE_CHECK, "check: rule matched -- granting permission"); )
c4f2d992 196 return (1);
03f996bd 197 }
c4f2d992 198 }
03f996bd 199
200 /* --- Failed to match --- */
201
202 T( trace(TRACE_CHECK, "check: no rules matched -- permission denied"); )
c4f2d992 203 return (0);
204}
205
03f996bd 206/* --- @rule_dump@ --- *
207 *
208 * Arguments: ---
209 *
210 * Returns: ---
211 *
212 * Use: Dumps a map of the current ruleset to the trace output.
213 */
214
215void rule_dump(void)
216{
217 rule *rr = rule__list;
218
219 trace(TRACE_RULE, "rule: dumping rules");
220 while (rr) {
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);
226 rr = rr->next;
227 }
228 trace(TRACE_RULE, "rule: dump finished");
229}
230
c4f2d992 231/*----- That's all, folks -------------------------------------------------*/