Make sure standard file descriptors are open before starting properly.
[become] / src / rule.c
CommitLineData
c4f2d992 1/* -*-c-*-
2 *
f60a3434 3 * $Id: rule.c,v 1.7 2003/10/12 00:14:55 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 $
f60a3434 32 * Revision 1.7 2003/10/12 00:14:55 mdw
33 * Major overhaul. Now uses DSA signatures rather than the bogus symmetric
34 * encrypt-and-hope thing. Integrated with mLib and Catacomb.
35 *
b40be44e 36 * Revision 1.6 1998/04/23 13:27:31 mdw
37 * Export structure of the rule list, for `bcquery's benefit.
38 *
c758e654 39 * Revision 1.5 1998/01/12 16:46:25 mdw
40 * Fix copyright date.
41 *
42 * Revision 1.4 1997/09/17 10:27:17 mdw
30868bd5 43 * Use rewritten class handler.
44 *
45 * Revision 1.3 1997/08/20 16:22:36 mdw
ce0f6e91 46 * Rename `rule_reinit' to `rule_end' for more sensible restart. Don't try
47 * to trace when tracing's turned off.
48 *
03f996bd 49 * Revision 1.2 1997/08/04 10:24:25 mdw
50 * Sources placed under CVS control.
51 *
52 * Revision 1.1 1997/07/21 13:47:45 mdw
c4f2d992 53 * Initial revision
54 *
55 */
56
57/*----- Header files ------------------------------------------------------*/
58
59/* --- ANSI headers --- */
60
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64
03f996bd 65/* --- Unix headers --- */
66
67#include <sys/types.h>
68#include <sys/socket.h>
69#include <netinet/in.h>
70#include <arpa/inet.h>
71#include <netdb.h>
72#include <unistd.h>
73
f60a3434 74/* --- mLib headers --- */
75
76#include <mLib/alloc.h>
77#include <mLib/report.h>
78#include <mLib/trace.h>
79
c4f2d992 80/* --- Local headers --- */
81
82#include "become.h"
83#include "class.h"
84#include "rule.h"
03f996bd 85#include "userdb.h"
c4f2d992 86
c4f2d992 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
b40be44e 134/* --- @rule_list@ --- *
135 *
136 * Arguments: ---
137 *
138 * Returns: The list of rules.
139 *
140 * Use: Returns the address of the first node in the rule list.
141 */
142
143rule *rule_list(void)
144{
145 return (rule__list);
146}
147
c4f2d992 148/* --- @rule_add@ --- *
149 *
30868bd5 150 * Arguments: @class_node *host@ = class of hosts this rule applies to
151 * @class_node *from@ = class of users allowed to change
152 * @class_node *to@ = class of users allowed to be changed to
153 * @class_node *cmd@ = class of commands allowed
c4f2d992 154 *
155 * Returns: ---
156 *
157 * Use: Registers another rule.
158 */
159
b40be44e 160void rule_add(class_node *host, class_node *from,
161 class_node *to, class_node *cmd)
c4f2d992 162{
163 rule *r = xmalloc(sizeof(*r));
164
165 r->host = host;
166 r->from = from;
167 r->to = to;
168 r->cmd = cmd;
169 r->next = 0;
170 rule__tail->next = r;
171 rule__tail = r;
172}
173
174/* --- @rule_check@ --- *
175 *
176 * Arguments: @request *r@ = pointer to a request block
177 *
178 * Returns: Zero if disallowed, nonzero if allowed.
179 *
180 * Use: Checks a request to see if it's allowed.
181 */
182
183int rule_check(request *r)
184{
03f996bd 185 rule *rr;
186
187 /* --- Trace out the request we're checking --- */
188
189 IF_TRACING(TRACE_CHECK, {
190 struct passwd *pw_from = userdb_userById(r->from);
191 struct passwd *pw_to = userdb_userById(r->to);
192 struct hostent *h = gethostbyaddr((char *)&r->host, sizeof(r->host),
193 AF_INET);
194
195 trace(TRACE_CHECK, "check: request from %s (%li) to become %s (%li)",
196 pw_from ? pw_from->pw_name : "<unknown>", (long)r->from,
197 pw_to ? pw_to->pw_name : "<unknown>", (long)r->to);
198 trace(TRACE_CHECK, "check: ... at %s (%s) for `%s'",
199 h ? h->h_name : "<unknown>", inet_ntoa(r->host), r->cmd);
200 })
201
202 /* --- Search the rule list --- */
203
204 for (rr = rule__list; rr; rr = rr->next) {
205
206 /* --- Trace out the rule --- */
207
208 IF_TRACING(TRACE_RULE, {
209 trace(TRACE_RULE, "rule: check against rule...");
30868bd5 210 trace(TRACE_RULE, "rule: from"); class_dump(rr->from, 2);
211 trace(TRACE_RULE, "rule: to"); class_dump(rr->to, 2);
212 trace(TRACE_RULE, "rule: cmd"); class_dump(rr->cmd, 2);
213 trace(TRACE_RULE, "rule: host"); class_dump(rr->host, 2);
03f996bd 214 })
215
216 /* --- Check the rule --- */
c4f2d992 217
30868bd5 218 if (class_matchUser(rr->from, r->from) &&
219 class_matchUser(rr->to, r->to) &&
220 class_matchCommand(rr->cmd, r->cmd) &&
221 class_matchHost(rr->host, r->host)) {
03f996bd 222 T( trace(TRACE_CHECK, "check: rule matched -- granting permission"); )
c4f2d992 223 return (1);
03f996bd 224 }
c4f2d992 225 }
03f996bd 226
227 /* --- Failed to match --- */
228
229 T( trace(TRACE_CHECK, "check: no rules matched -- permission denied"); )
c4f2d992 230 return (0);
231}
232
03f996bd 233/* --- @rule_dump@ --- *
234 *
235 * Arguments: ---
236 *
237 * Returns: ---
238 *
239 * Use: Dumps a map of the current ruleset to the trace output.
240 */
241
242void rule_dump(void)
243{
ce0f6e91 244#ifdef TRACING
03f996bd 245 rule *rr = rule__list;
246
247 trace(TRACE_RULE, "rule: dumping rules");
248 while (rr) {
30868bd5 249 trace(TRACE_RULE, "rule: rule dump...");
250 trace(TRACE_RULE, "rule: from"); class_dump(rr->from, 2);
251 trace(TRACE_RULE, "rule: to"); class_dump(rr->to, 2);
252 trace(TRACE_RULE, "rule: cmd"); class_dump(rr->cmd, 2);
253 trace(TRACE_RULE, "rule: host"); class_dump(rr->host, 2);
03f996bd 254 rr = rr->next;
255 }
256 trace(TRACE_RULE, "rule: dump finished");
ce0f6e91 257#endif
03f996bd 258}
259
c4f2d992 260/*----- That's all, folks -------------------------------------------------*/