r14840: - rename some functions
[samba.git] / source4 / dsdb / samdb / ldb_modules / kludge_acl.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlett 2005
5    Copyright (C) Simo Sorce 2006
6
7     This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /*
23  *  Name: ldb
24  *
25  *  Component: ldb kludge ACL module
26  *
27  *  Description: Simple module to enforce a simple form of access
28  *               control, sufficient for securing a default Samba4 
29  *               installation.
30  *
31  *  Author: Andrew Bartlett
32  */
33
34 #include "includes.h"
35 #include "ldb/include/ldb.h"
36 #include "ldb/include/ldb_errors.h"
37 #include "ldb/include/ldb_private.h"
38 #include "auth/auth.h"
39 #include "libcli/security/proto.h"
40
41 /* Kludge ACL rules:
42  *
43  * - System can read passwords
44  * - Administrators can write anything
45  * - Users can read anything that is not a password
46  *
47  */
48
49 enum user_is {
50         ANONYMOUS,
51         USER,
52         ADMINISTRATOR,
53         SYSTEM
54 };
55
56 struct kludge_private_data {
57         const char **password_attrs;
58 };
59
60 static enum user_is what_is_user(struct ldb_module *module) 
61 {
62         struct auth_session_info *session_info
63                 = ldb_get_opaque(module->ldb, "sessionInfo");
64         if (!session_info) {
65                 return ANONYMOUS;
66         }
67         
68         if (security_token_is_system(session_info->security_token)) {
69                 return SYSTEM;
70         }
71
72         if (security_token_is_anonymous(session_info->security_token)) {
73                 return ANONYMOUS;
74         }
75
76         if (security_token_has_builtin_administrators(session_info->security_token)) {
77                 return ADMINISTRATOR;
78         }
79
80         if (security_token_has_nt_authenticated_users(session_info->security_token)) {
81                 return USER;
82         }
83
84         return ANONYMOUS;
85 }
86
87 static const char *user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module) 
88 {
89         struct auth_session_info *session_info
90                 = ldb_get_opaque(module->ldb, "sessionInfo");
91         if (!session_info) {
92                 return "UNKNOWN (NULL)";
93         }
94         
95         return talloc_asprintf(mem_ctx, "%s\\%s",
96                                session_info->server_info->domain_name,
97                                session_info->server_info->account_name);
98 }
99
100 /* search */
101 static int kludge_acl_search(struct ldb_module *module, struct ldb_request *req)
102 {
103         struct kludge_private_data *data = talloc_get_type(module->private_data, struct kludge_private_data);
104         struct ldb_message *msg;
105         enum user_is user_type;
106         int i, j, ret;
107
108         /* go down the path and wait for reply to filter out stuff if needed */
109         ret = ldb_next_request(module, req);
110
111         /* We may not be fully initialised yet, or we might have just
112          * got an error */
113         if (ret != LDB_SUCCESS || !data->password_attrs) {
114                 return ret;
115         }
116
117         user_type = what_is_user(module);
118         switch (user_type) {
119         case SYSTEM:
120         case ADMINISTRATOR:
121                 return ret;
122         default:
123                 /* For every message, remove password attributes */
124                 for (i=0; i < req->op.search.res->count; i++) {
125                         msg = req->op.search.res->msgs[i];
126                         for (j=0; data->password_attrs[j]; j++) {
127                                 ldb_msg_remove_attr(msg, data->password_attrs[j]);
128                         }
129                 }
130         }
131         return ret;
132 }
133
134 /* search */
135 struct kludge_acl_async_context {
136
137         struct ldb_module *module;
138         void *up_context;
139         int (*up_callback)(struct ldb_context *, void *, struct ldb_async_result *);
140         int timeout;
141
142         enum user_is user_type;
143 };
144
145 static int kludge_acl_async_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
146 {
147         struct kludge_acl_async_context *ac;
148         struct kludge_private_data *data;
149         int i;
150
151         if (!context || !ares) {
152                 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
153                 goto error;
154         }
155
156         ac = talloc_get_type(context, struct kludge_acl_async_context);
157         data = talloc_get_type(ac->module->private_data, struct kludge_private_data);
158
159         if (ares->type == LDB_REPLY_ENTRY
160                 && data->password_attrs) /* if we are not initialized just get through */
161         {
162                 switch (ac->user_type) {
163                 case SYSTEM:
164                 case ADMINISTRATOR:
165                         break;
166                 default:
167                         /* remove password attributes */
168                         for (i = 0; data->password_attrs[i]; i++) {
169                                 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
170                         }
171                 }
172         }
173
174         return ac->up_callback(ldb, ac->up_context, ares);
175
176 error:
177         talloc_free(ares);
178         return LDB_ERR_OPERATIONS_ERROR;
179 }
180
181 static int kludge_acl_search_async(struct ldb_module *module, struct ldb_request *req)
182 {
183         struct kludge_acl_async_context *ac;
184         struct ldb_request *down_req;
185         int ret;
186
187         req->async.handle = NULL;
188
189         ac = talloc(req, struct kludge_acl_async_context);
190         if (ac == NULL) {
191                 return LDB_ERR_OPERATIONS_ERROR;
192         }
193
194         ac->module = module;
195         ac->up_context = req->async.context;
196         ac->up_callback = req->async.callback;
197         ac->timeout = req->async.timeout;
198         ac->user_type = what_is_user(module);
199
200         down_req = talloc_zero(req, struct ldb_request);
201         if (down_req == NULL) {
202                 return LDB_ERR_OPERATIONS_ERROR;
203         }
204
205         down_req->operation = req->operation;
206         down_req->op.search.base = req->op.search.base;
207         down_req->op.search.scope = req->op.search.scope;
208         down_req->op.search.tree = req->op.search.tree;
209         down_req->op.search.attrs = req->op.search.attrs;
210         
211         down_req->controls = req->controls;
212         down_req->creds = req->creds;
213
214         down_req->async.context = ac;
215         down_req->async.callback = kludge_acl_async_callback;
216         down_req->async.timeout = req->async.timeout;
217
218         /* perform the search */
219         ret = ldb_next_request(module, down_req);
220
221         /* do not free down_req as the call results may be linked to it,
222          * it will be freed when the upper level request get freed */
223         if (ret == LDB_SUCCESS) {
224                 req->async.handle = down_req->async.handle;
225         }
226
227         return ret;
228 }
229
230 /* ANY change type */
231 static int kludge_acl_change(struct ldb_module *module, struct ldb_request *req){
232         enum user_is user_type = what_is_user(module);
233         switch (user_type) {
234         case SYSTEM:
235         case ADMINISTRATOR:
236                 return ldb_next_request(module, req);
237         default:
238                 ldb_set_errstring(module->ldb, 
239                                   talloc_asprintf(req, "kludge_acl_change: "
240                                                   "attempted database modify not permitted. User %s is not SYSTEM or an administrator",
241                                                   user_name(req, module)));
242                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
243         }
244 }
245
246 /* start a transaction */
247 static int kludge_acl_start_trans(struct ldb_module *module)
248 {
249         return ldb_next_start_trans(module);
250 }
251
252 /* end a transaction */
253 static int kludge_acl_end_trans(struct ldb_module *module)
254 {
255         return ldb_next_end_trans(module);
256 }
257
258 /* delete a transaction */
259 static int kludge_acl_del_trans(struct ldb_module *module)
260 {
261         return ldb_next_del_trans(module);
262 }
263
264 static int kludge_acl_request(struct ldb_module *module, struct ldb_request *req)
265 {
266         switch (req->operation) {
267
268         case LDB_REQ_ADD:
269         case LDB_ASYNC_ADD:
270         case LDB_REQ_MODIFY:
271         case LDB_ASYNC_MODIFY:
272         case LDB_REQ_DELETE:
273         case LDB_ASYNC_DELETE:
274         case LDB_REQ_RENAME:
275         case LDB_ASYNC_RENAME:
276                 return kludge_acl_change(module, req);
277
278         case LDB_REQ_SEARCH:
279                 return kludge_acl_search(module, req);
280
281         case LDB_ASYNC_SEARCH:
282                 return kludge_acl_search_async(module, req);
283
284         case LDB_REQ_REGISTER:
285                 return ldb_next_request(module, req);
286
287         default:
288                 /* anything else must be something new, let's throw an error */
289                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
290         }
291 }
292
293 static int kludge_acl_init(struct ldb_module *module)
294 {
295         int ret, i;
296         TALLOC_CTX *mem_ctx = talloc_new(module);
297         const char *attrs[] = { "attribute", NULL };
298         struct ldb_result *res;
299         struct ldb_message *msg;
300         struct ldb_message_element *password_attributes;
301
302         struct kludge_private_data *data;
303
304         data = talloc(module, struct kludge_private_data);
305         if (data == NULL) {
306                 return LDB_ERR_OPERATIONS_ERROR;
307         }
308
309         data->password_attrs = NULL;
310         module->private_data = data;
311
312         if (!mem_ctx) {
313                 return LDB_ERR_OPERATIONS_ERROR;
314         }
315
316         ret = ldb_search(module->ldb, ldb_dn_explode(mem_ctx, "@KLUDGEACL"),
317                          LDB_SCOPE_BASE,
318                          NULL, attrs,
319                          &res);
320         if (ret != LDB_SUCCESS) {
321                 goto done;
322         }
323         talloc_steal(mem_ctx, res);
324         if (res->count == 0) {
325                 goto done;
326         }
327
328         if (res->count > 1) {
329                 talloc_free(mem_ctx);
330                 return LDB_ERR_CONSTRAINT_VIOLATION;
331         }
332
333         msg = res->msgs[0];
334
335         password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
336         if (!password_attributes) {
337                 goto done;
338         }
339         data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
340         if (!data->password_attrs) {
341                 talloc_free(mem_ctx);
342                 return LDB_ERR_OPERATIONS_ERROR;
343         }
344         for (i=0; i < password_attributes->num_values; i++) {
345                 data->password_attrs[i] = (const char *)password_attributes->values[i].data;    
346                 talloc_steal(data->password_attrs, password_attributes->values[i].data);
347         }
348         data->password_attrs[i] = NULL;
349
350 done:
351         talloc_free(mem_ctx);
352         return ldb_next_init(module);
353 }
354
355 static const struct ldb_module_ops kludge_acl_ops = {
356         .name              = "kludge_acl",
357         .request           = kludge_acl_request,
358         .start_transaction = kludge_acl_start_trans,
359         .end_transaction   = kludge_acl_end_trans,
360         .del_transaction   = kludge_acl_del_trans,
361         .init_context      = kludge_acl_init
362 };
363
364 int ldb_kludge_acl_init(void)
365 {
366         return ldb_register_module(&kludge_acl_ops);
367 }