Sources placed under CVS control.
[become] / src / rule.c
CommitLineData
c4f2d992 1/* -*-c-*-
2 *
3 * $Id: rule.c,v 1.1 1997/07/21 13:47:45 mdw Exp $
4 *
5 * Managing rule sets
6 *
7 * (c) 1997 EBI
8 */
9
10/*----- Licencing notice --------------------------------------------------*
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
25 * along with `become'; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: rule.c,v $
32 * Revision 1.1 1997/07/21 13:47:45 mdw
33 * Initial revision
34 *
35 */
36
37/*----- Header files ------------------------------------------------------*/
38
39/* --- ANSI headers --- */
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45/* --- Local headers --- */
46
47#include "become.h"
48#include "class.h"
49#include "rule.h"
50#include "utils.h"
51
52/*----- Type definitions --------------------------------------------------*/
53
54/* --- Rule block --- */
55
56typedef struct rule {
57 struct rule *next; /* Next rule in the list */
58 classdef *host; /* Hosts this rule applies to */
59 classdef *from; /* From users in this class */
60 classdef *to; /* To users in this class */
61 classdef *cmd; /* To run commands in this class */
62} rule;
63
64/*----- Static variables --------------------------------------------------*/
65
66static rule *rule__list; /* List of rules */
67static rule *rule__tail; /* Pointer to last rule item */
68
69/*----- Main code ---------------------------------------------------------*/
70
71/* --- @rule_init@ --- *
72 *
73 * Arguments: ---
74 *
75 * Returns: ---
76 *
77 * Use: Intialises the rule database.
78 */
79
80void rule_init(void)
81{
82 rule__list = 0;
83 rule__tail = (rule *)&rule__list;
84}
85
86/* --- @rule_reinit@ --- *
87 *
88 * Arguments: ---
89 *
90 * Returns: ---
91 *
92 * Use: Reinitialises the rule database.
93 */
94
95void rule_reinit(void)
96{
97 rule *r = rule__list;
98 rule *rr;
99
100 while (r) {
101 rr = r->next;
102 free(r);
103 r = rr;
104 }
105
106 rule_init();
107}
108
109/* --- @rule_add@ --- *
110 *
111 * Arguments: @classdef *host@ = class of hosts this rule applies to
112 * @classdef *from@ = class of users allowed to change
113 * @classdef *to@ = class of users allowed to be changed to
114 * @classdef *cmd@ = class of commands allowed
115 *
116 * Returns: ---
117 *
118 * Use: Registers another rule.
119 */
120
121void rule_add(classdef *host, classdef *from, classdef *to, classdef *cmd)
122{
123 rule *r = xmalloc(sizeof(*r));
124
125 r->host = host;
126 r->from = from;
127 r->to = to;
128 r->cmd = cmd;
129 r->next = 0;
130 rule__tail->next = r;
131 rule__tail = r;
132}
133
134/* --- @rule_check@ --- *
135 *
136 * Arguments: @request *r@ = pointer to a request block
137 *
138 * Returns: Zero if disallowed, nonzero if allowed.
139 *
140 * Use: Checks a request to see if it's allowed.
141 */
142
143int rule_check(request *r)
144{
145 rule *rr = rule__list;
146
147 while (rr) {
148 if (class_userMatch(rr->from, r->from) &&
149 class_userMatch(rr->to, r->to) &&
150 class_commandMatch(rr->cmd, r->cmd) &&
151 class_hostMatch(rr->host, r->host))
152 return (1);
153 rr = rr->next;
154 }
155 return (0);
156}
157
158/*----- That's all, folks -------------------------------------------------*/