644217b99ce19648ff6e71183ef5763b91bbb093
[ira/wip.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/security.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 struct kludge_acl_context {
102
103         struct ldb_module *module;
104         void *up_context;
105         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
106
107         enum user_is user_type;
108 };
109
110 static int kludge_acl_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
111 {
112         struct kludge_acl_context *ac;
113         struct kludge_private_data *data;
114         int i;
115
116         if (!context || !ares) {
117                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
118                 goto error;
119         }
120
121         ac = talloc_get_type(context, struct kludge_acl_context);
122         data = talloc_get_type(ac->module->private_data, struct kludge_private_data);
123
124         if (ares->type == LDB_REPLY_ENTRY
125             && data && data->password_attrs) /* if we are not initialized just get through */
126         {
127                 switch (ac->user_type) {
128                 case SYSTEM:
129                         break;
130                 default:
131                         /* remove password attributes */
132                         for (i = 0; data->password_attrs[i]; i++) {
133                                 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
134                         }
135                 }
136         }
137
138         return ac->up_callback(ldb, ac->up_context, ares);
139
140 error:
141         talloc_free(ares);
142         return LDB_ERR_OPERATIONS_ERROR;
143 }
144
145 static int kludge_acl_search(struct ldb_module *module, struct ldb_request *req)
146 {
147         struct kludge_acl_context *ac;
148         struct ldb_request *down_req;
149         struct kludge_private_data *data;
150         int ret, i;
151
152         req->handle = NULL;
153
154         ac = talloc(req, struct kludge_acl_context);
155         if (ac == NULL) {
156                 return LDB_ERR_OPERATIONS_ERROR;
157         }
158
159         data = talloc_get_type(module->private_data, struct kludge_private_data);
160
161         ac->module = module;
162         ac->up_context = req->context;
163         ac->up_callback = req->callback;
164         ac->user_type = what_is_user(module);
165
166         down_req = talloc_zero(req, struct ldb_request);
167         if (down_req == NULL) {
168                 return LDB_ERR_OPERATIONS_ERROR;
169         }
170
171         down_req->operation = req->operation;
172         down_req->op.search.base = req->op.search.base;
173         down_req->op.search.scope = req->op.search.scope;
174         down_req->op.search.tree = req->op.search.tree;
175         down_req->op.search.attrs = req->op.search.attrs;
176         
177
178         /*  FIXME: I hink we should copy the tree and keep the original
179          *  unmodified. SSS */
180         /* replace any attributes in the parse tree that are private,
181            so we don't allow a search for 'sambaPassword=penguin',
182            just as we would not allow that attribute to be returned */
183         switch (ac->user_type) {
184         case SYSTEM:
185                 break;
186         default:
187                 /* remove password attributes */
188                 for (i = 0; data && data->password_attrs && data->password_attrs[i]; i++) {
189                         ldb_parse_tree_attr_replace(down_req->op.search.tree, 
190                                                     data->password_attrs[i],
191                                                     "kludgeACLredactedattribute");
192                 }
193         }
194
195         down_req->controls = req->controls;
196
197         down_req->context = ac;
198         down_req->callback = kludge_acl_callback;
199         ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
200
201         /* perform the search */
202         ret = ldb_next_request(module, down_req);
203
204         /* do not free down_req as the call results may be linked to it,
205          * it will be freed when the upper level request get freed */
206         if (ret == LDB_SUCCESS) {
207                 req->handle = down_req->handle;
208         }
209
210         return ret;
211 }
212
213 /* ANY change type */
214 static int kludge_acl_change(struct ldb_module *module, struct ldb_request *req)
215 {
216         enum user_is user_type = what_is_user(module);
217         switch (user_type) {
218         case SYSTEM:
219         case ADMINISTRATOR:
220                 return ldb_next_request(module, req);
221         default:
222                 ldb_asprintf_errstring(module->ldb,
223                                        "kludge_acl_change: "
224                                        "attempted database modify not permitted. "
225                                        "User %s is not SYSTEM or an administrator",
226                                        user_name(req, module));
227                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
228         }
229 }
230
231 static int kludge_acl_init(struct ldb_module *module)
232 {
233         int ret, i;
234         TALLOC_CTX *mem_ctx = talloc_new(module);
235         static const char *attrs[] = { "passwordAttribute", NULL };
236         struct ldb_result *res;
237         struct ldb_message *msg;
238         struct ldb_message_element *password_attributes;
239
240         struct kludge_private_data *data;
241
242         data = talloc(module, struct kludge_private_data);
243         if (data == NULL) {
244                 return LDB_ERR_OPERATIONS_ERROR;
245         }
246
247         data->password_attrs = NULL;
248         module->private_data = data;
249
250         if (!mem_ctx) {
251                 return LDB_ERR_OPERATIONS_ERROR;
252         }
253
254         ret = ldb_search(module->ldb, ldb_dn_new(mem_ctx, module->ldb, "@KLUDGEACL"),
255                          LDB_SCOPE_BASE,
256                          NULL, attrs,
257                          &res);
258         if (ret != LDB_SUCCESS) {
259                 goto done;
260         }
261         talloc_steal(mem_ctx, res);
262         if (res->count == 0) {
263                 goto done;
264         }
265
266         if (res->count > 1) {
267                 talloc_free(mem_ctx);
268                 return LDB_ERR_CONSTRAINT_VIOLATION;
269         }
270
271         msg = res->msgs[0];
272
273         password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
274         if (!password_attributes) {
275                 goto done;
276         }
277         data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
278         if (!data->password_attrs) {
279                 talloc_free(mem_ctx);
280                 return LDB_ERR_OPERATIONS_ERROR;
281         }
282         for (i=0; i < password_attributes->num_values; i++) {
283                 data->password_attrs[i] = (const char *)password_attributes->values[i].data;    
284                 talloc_steal(data->password_attrs, password_attributes->values[i].data);
285         }
286         data->password_attrs[i] = NULL;
287
288 done:
289         talloc_free(mem_ctx);
290         return ldb_next_init(module);
291 }
292
293 static const struct ldb_module_ops kludge_acl_ops = {
294         .name              = "kludge_acl",
295         .search            = kludge_acl_search,
296         .add               = kludge_acl_change,
297         .modify            = kludge_acl_change,
298         .del               = kludge_acl_change,
299         .rename            = kludge_acl_change,
300         .extended          = kludge_acl_change,
301         .init_context      = kludge_acl_init
302 };
303
304 int ldb_kludge_acl_init(void)
305 {
306         return ldb_register_module(&kludge_acl_ops);
307 }