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