r12769: Make ldb_next_request() evident, I was much confused on first sight
[kai/samba-autobuild/.git] / source4 / dsdb / samdb / ldb_modules / kludge_acl.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlett 2005
5
6     This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: ldb kludge ACL module
25  *
26  *  Description: Simple module to enforce a simple form of access
27  *               control, sufficient for securing a default Samba4 
28  *               installation.
29  *
30  *  Author: Andrew Bartlett
31  */
32
33 #include "includes.h"
34 #include "ldb/include/ldb.h"
35 #include "ldb/include/ldb_errors.h"
36 #include "ldb/include/ldb_private.h"
37 #include "auth/auth.h"
38
39 /* Kludge ACL rules:
40  *
41  * - System can read passwords
42  * - Administrators can write anything
43  * - Users can read anything that is not a password
44  *
45  */
46
47 const char *password_attribs[] = {
48 };
49
50 enum user_is {
51         ANONYMOUS,
52         USER,
53         ADMINISTRATOR,
54         SYSTEM
55 };
56
57 struct kludge_private_data {
58         const char **password_attrs;
59 };
60
61 static enum user_is what_is_user(struct ldb_module *module) 
62 {
63         struct auth_session_info *session_info
64                 = ldb_get_opaque(module->ldb, "sessionInfo");
65         if (!session_info) {
66                 return ANONYMOUS;
67         }
68         
69         if (is_system_token(session_info->security_token)) {
70                 return SYSTEM;
71         }
72
73         if (is_administrator_token(session_info->security_token)) {
74                 return ADMINISTRATOR;
75         }
76         if (is_authenticated_token(session_info->security_token)) {
77                 return USER;
78         }
79         if (is_anonymous_token(session_info->security_token)) {
80                 return ANONYMOUS;
81         }
82         return ANONYMOUS;
83 }
84
85 /* search */
86 static int kludge_acl_search(struct ldb_module *module, struct ldb_request *req)
87 {
88         struct kludge_private_data *data = talloc_get_type(module->private_data, struct kludge_private_data);
89         struct ldb_message *msg;
90         enum user_is user_type;
91         int i, j, ret;
92
93         /* go down the path and wait for reply to filter out stuff if needed */
94         ret = ldb_next_request(module, req);
95
96         /* We may not be fully initialised yet, or we might have just
97          * got an error */
98         if (ret != LDB_SUCCESS || !data->password_attrs) {
99                 return ret;
100         }
101
102         user_type = what_is_user(module);
103         switch (user_type) {
104         case SYSTEM:
105         case ADMINISTRATOR:
106                 return ret;
107         default:
108                 /* For every message, remove password attributes */
109                 for (i=0; i < req->op.search.res->count; i++) {
110                         msg = req->op.search.res->msgs[i];
111                         for (j=0; data->password_attrs[j]; j++) {
112                                 ldb_msg_remove_attr(msg, data->password_attrs[j]);
113                         }
114                 }
115         }
116         return ret;
117 }
118
119 /* ANY change type */
120 static int kludge_acl_change(struct ldb_module *module, struct ldb_request *req){
121         enum user_is user_type = what_is_user(module);
122         switch (user_type) {
123         case SYSTEM:
124         case ADMINISTRATOR:
125                 return ldb_next_request(module, req);
126         default:
127                 ldb_set_errstring(module, 
128                                   talloc_asprintf(req, "kludge_acl_change: "
129                                                   "attempted database modify not permitted. User is not SYSTEM or an administrator"));
130                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
131         }
132 }
133
134 /* start a transaction */
135 static int kludge_acl_start_trans(struct ldb_module *module)
136 {
137         return ldb_next_start_trans(module);
138 }
139
140 /* end a transaction */
141 static int kludge_acl_end_trans(struct ldb_module *module)
142 {
143         return ldb_next_end_trans(module);
144 }
145
146 /* delete a transaction */
147 static int kludge_acl_del_trans(struct ldb_module *module)
148 {
149         return ldb_next_del_trans(module);
150 }
151
152 static int kludge_acl_request(struct ldb_module *module, struct ldb_request *req)
153 {
154         switch (req->operation) {
155
156         case LDB_REQ_SEARCH:
157                 return kludge_acl_search(module, req);
158         case LDB_REQ_REGISTER:
159                 return ldb_next_request(module, req);
160         default:
161                 /* anything else must be a change of some kind */
162                 return kludge_acl_change(module, req);
163         }
164 }
165
166 static int kludge_acl_init_2(struct ldb_module *module)
167 {
168         int ret, i;
169         TALLOC_CTX *mem_ctx = talloc_new(module);
170         const char *attrs[] = { "attribute", NULL };
171         struct ldb_result *res;
172         struct ldb_message *msg;
173         struct ldb_message_element *password_attributes;
174
175         struct kludge_private_data *data = talloc_get_type(module->private_data, struct kludge_private_data);
176         data->password_attrs = NULL;
177
178         if (!mem_ctx) {
179                 return LDB_ERR_OPERATIONS_ERROR;
180         }
181
182         ret = ldb_search(module->ldb, ldb_dn_explode(mem_ctx, "@KLUDGEACL"),
183                          LDB_SCOPE_BASE,
184                          NULL, attrs,
185                          &res);
186         if (ret != LDB_SUCCESS) {
187                 talloc_free(mem_ctx);
188                 return ret;
189         }
190         if (res->count == 0) {
191                 talloc_free(mem_ctx);
192                 data->password_attrs = NULL;
193                 return LDB_SUCCESS;
194         }
195
196         if (res->count > 1) {
197                 return LDB_ERR_CONSTRAINT_VIOLAION;
198         }
199
200         msg = res->msgs[0];
201
202         password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
203         if (!password_attributes) {
204                 return LDB_SUCCESS;
205         }
206         data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
207         if (!data->password_attrs) {
208                 return LDB_ERR_OPERATIONS_ERROR;
209         }
210         for (i=0; i < password_attributes->num_values; i++) {
211                 data->password_attrs[i] = (const char *)password_attributes->values[i].data;    
212                 talloc_steal(data->password_attrs, password_attributes->values[i].data);
213         }
214         data->password_attrs[i] = NULL;
215         return LDB_SUCCESS;
216 }
217
218 static const struct ldb_module_ops kludge_acl_ops = {
219         .name              = "kludge_acl",
220         .request           = kludge_acl_request,
221         .start_transaction = kludge_acl_start_trans,
222         .end_transaction   = kludge_acl_end_trans,
223         .del_transaction   = kludge_acl_del_trans,
224         .second_stage_init = kludge_acl_init_2
225 };
226
227 struct ldb_module *kludge_acl_module_init(struct ldb_context *ldb, const char *options[])
228 {
229         struct ldb_module *ctx;
230         struct kludge_private_data *data;
231
232         ctx = talloc(ldb, struct ldb_module);
233         if (!ctx)
234                 return NULL;
235
236         data = talloc(ctx, struct kludge_private_data);
237         if (data == NULL) {
238                 talloc_free(ctx);
239                 return NULL;
240         }
241
242         data->password_attrs = NULL;
243         ctx->private_data = data;
244
245         ctx->ldb = ldb;
246         ctx->prev = ctx->next = NULL;
247         ctx->ops = &kludge_acl_ops;
248
249         return ctx;
250 }