r13253: More work to ensure that we don't keep data on long-term contexts.
[samba.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 enum user_is {
48         ANONYMOUS,
49         USER,
50         ADMINISTRATOR,
51         SYSTEM
52 };
53
54 struct kludge_private_data {
55         const char **password_attrs;
56 };
57
58 static enum user_is what_is_user(struct ldb_module *module) 
59 {
60         struct auth_session_info *session_info
61                 = ldb_get_opaque(module->ldb, "sessionInfo");
62         if (!session_info) {
63                 return ANONYMOUS;
64         }
65         
66         if (is_system_token(session_info->security_token)) {
67                 return SYSTEM;
68         }
69
70         if (is_administrator_token(session_info->security_token)) {
71                 return ADMINISTRATOR;
72         }
73         if (is_authenticated_token(session_info->security_token)) {
74                 return USER;
75         }
76         if (is_anonymous_token(session_info->security_token)) {
77                 return ANONYMOUS;
78         }
79         return ANONYMOUS;
80 }
81
82 static const char *user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module) 
83 {
84         struct auth_session_info *session_info
85                 = ldb_get_opaque(module->ldb, "sessionInfo");
86         if (!session_info) {
87                 return "UNKNOWN (NULL)";
88         }
89         
90         return talloc_asprintf(mem_ctx, "%s\\%s",
91                                session_info->server_info->domain_name,
92                                session_info->server_info->account_name);
93         return ANONYMOUS;
94 }
95
96 /* search */
97 static int kludge_acl_search(struct ldb_module *module, struct ldb_request *req)
98 {
99         struct kludge_private_data *data = talloc_get_type(module->private_data, struct kludge_private_data);
100         struct ldb_message *msg;
101         enum user_is user_type;
102         int i, j, ret;
103
104         /* go down the path and wait for reply to filter out stuff if needed */
105         ret = ldb_next_request(module, req);
106
107         /* We may not be fully initialised yet, or we might have just
108          * got an error */
109         if (ret != LDB_SUCCESS || !data->password_attrs) {
110                 return ret;
111         }
112
113         user_type = what_is_user(module);
114         switch (user_type) {
115         case SYSTEM:
116         case ADMINISTRATOR:
117                 return ret;
118         default:
119                 /* For every message, remove password attributes */
120                 for (i=0; i < req->op.search.res->count; i++) {
121                         msg = req->op.search.res->msgs[i];
122                         for (j=0; data->password_attrs[j]; j++) {
123                                 ldb_msg_remove_attr(msg, data->password_attrs[j]);
124                         }
125                 }
126         }
127         return ret;
128 }
129
130 /* ANY change type */
131 static int kludge_acl_change(struct ldb_module *module, struct ldb_request *req){
132         enum user_is user_type = what_is_user(module);
133         switch (user_type) {
134         case SYSTEM:
135         case ADMINISTRATOR:
136                 return ldb_next_request(module, req);
137         default:
138                 ldb_set_errstring(module, 
139                                   talloc_asprintf(req, "kludge_acl_change: "
140                                                   "attempted database modify not permitted. User %s is not SYSTEM or an administrator",
141                                                   user_name(req, module)));
142                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
143         }
144 }
145
146 /* start a transaction */
147 static int kludge_acl_start_trans(struct ldb_module *module)
148 {
149         return ldb_next_start_trans(module);
150 }
151
152 /* end a transaction */
153 static int kludge_acl_end_trans(struct ldb_module *module)
154 {
155         return ldb_next_end_trans(module);
156 }
157
158 /* delete a transaction */
159 static int kludge_acl_del_trans(struct ldb_module *module)
160 {
161         return ldb_next_del_trans(module);
162 }
163
164 static int kludge_acl_request(struct ldb_module *module, struct ldb_request *req)
165 {
166         switch (req->operation) {
167
168         case LDB_REQ_SEARCH:
169                 return kludge_acl_search(module, req);
170         case LDB_REQ_REGISTER:
171                 return ldb_next_request(module, req);
172         default:
173                 /* anything else must be a change of some kind */
174                 return kludge_acl_change(module, req);
175         }
176 }
177
178 static int kludge_acl_init_2(struct ldb_module *module)
179 {
180         int ret, i;
181         TALLOC_CTX *mem_ctx = talloc_new(module);
182         const char *attrs[] = { "attribute", NULL };
183         struct ldb_result *res;
184         struct ldb_message *msg;
185         struct ldb_message_element *password_attributes;
186
187         struct kludge_private_data *data = talloc_get_type(module->private_data, struct kludge_private_data);
188         data->password_attrs = NULL;
189
190         if (!mem_ctx) {
191                 return LDB_ERR_OPERATIONS_ERROR;
192         }
193
194         ret = ldb_search(module->ldb, ldb_dn_explode(mem_ctx, "@KLUDGEACL"),
195                          LDB_SCOPE_BASE,
196                          NULL, attrs,
197                          &res);
198         if (ret != LDB_SUCCESS) {
199                 talloc_free(mem_ctx);
200                 return ret;
201         }
202         talloc_steal(mem_ctx, res);
203         if (res->count == 0) {
204                 talloc_free(mem_ctx);
205                 data->password_attrs = NULL;
206                 return LDB_SUCCESS;
207         }
208
209         if (res->count > 1) {
210                 talloc_free(mem_ctx);
211                 return LDB_ERR_CONSTRAINT_VIOLATION;
212         }
213
214         msg = res->msgs[0];
215
216         password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
217         if (!password_attributes) {
218                 talloc_free(mem_ctx);
219                 return LDB_SUCCESS;
220         }
221         data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
222         if (!data->password_attrs) {
223                 talloc_free(mem_ctx);
224                 return LDB_ERR_OPERATIONS_ERROR;
225         }
226         for (i=0; i < password_attributes->num_values; i++) {
227                 data->password_attrs[i] = (const char *)password_attributes->values[i].data;    
228                 talloc_steal(data->password_attrs, password_attributes->values[i].data);
229         }
230         data->password_attrs[i] = NULL;
231         talloc_free(mem_ctx);
232         return LDB_SUCCESS;
233 }
234
235 static const struct ldb_module_ops kludge_acl_ops = {
236         .name              = "kludge_acl",
237         .request           = kludge_acl_request,
238         .start_transaction = kludge_acl_start_trans,
239         .end_transaction   = kludge_acl_end_trans,
240         .del_transaction   = kludge_acl_del_trans,
241         .second_stage_init = kludge_acl_init_2
242 };
243
244 struct ldb_module *kludge_acl_module_init(struct ldb_context *ldb, const char *options[])
245 {
246         struct ldb_module *ctx;
247         struct kludge_private_data *data;
248
249         ctx = talloc(ldb, struct ldb_module);
250         if (!ctx)
251                 return NULL;
252
253         data = talloc(ctx, struct kludge_private_data);
254         if (data == NULL) {
255                 talloc_free(ctx);
256                 return NULL;
257         }
258
259         data->password_attrs = NULL;
260         ctx->private_data = data;
261
262         ctx->ldb = ldb;
263         ctx->prev = ctx->next = NULL;
264         ctx->ops = &kludge_acl_ops;
265
266         return ctx;
267 }