Make up the right dependencies now that ldb depends on libevents
[ira/wip.git] / source4 / dsdb / samdb / samdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    interface functions for the sam database
5
6    Copyright (C) Andrew Tridgell 2004
7    Copyright (C) Volker Lendecke 2004
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_netlogon.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "lib/events/events.h"
29 #include "lib/ldb/include/ldb.h"
30 #include "lib/ldb/include/ldb_errors.h"
31 #include "libcli/security/security.h"
32 #include "libcli/auth/libcli_auth.h"
33 #include "libcli/ldap/ldap_ndr.h"
34 #include "system/time.h"
35 #include "system/filesys.h"
36 #include "ldb_wrap.h"
37 #include "util/util_ldb.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "dsdb/common/flags.h"
40 #include "param/param.h"
41 #include "lib/events/events.h"
42
43 char *samdb_relative_path(struct ldb_context *ldb,
44                                  TALLOC_CTX *mem_ctx, 
45                                  const char *name) 
46 {
47         const char *base_url = 
48                 (const char *)ldb_get_opaque(ldb, "ldb_url");
49         char *path, *p, *full_name;
50         if (name == NULL) {
51                 return NULL;
52         }
53         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
54                 return talloc_strdup(mem_ctx, name);
55         }
56         path = talloc_strdup(mem_ctx, base_url);
57         if (path == NULL) {
58                 return NULL;
59         }
60         if ( (p = strrchr(path, '/')) != NULL) {
61                 p[0] = '\0';
62                 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
63         } else {
64                 full_name = talloc_asprintf(mem_ctx, "./%s", name);
65         }
66         talloc_free(path);
67         return full_name;
68 }
69
70
71 /*
72   connect to the SAM database
73   return an opaque context pointer on success, or NULL on failure
74  */
75 struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx, 
76                                   struct event_context *ev_ctx,
77                                   struct loadparm_context *lp_ctx,
78                                   struct auth_session_info *session_info)
79 {
80         struct ldb_context *ldb;
81         ldb = ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx, 
82                                lp_sam_url(lp_ctx), session_info,
83                                NULL, 0, NULL);
84         if (!ldb) {
85                 return NULL;
86         }
87         dsdb_make_schema_global(ldb);
88         return ldb;
89 }
90
91 /*
92   copy from a template record to a message
93 */
94 int samdb_copy_template(struct ldb_context *ldb, 
95                         struct ldb_message *msg, const char *name,
96                         const char **errstring)
97 {
98         struct ldb_result *res;
99         struct ldb_message *t;
100         int ret, i, j;
101         struct ldb_context *templates_ldb;
102         char *templates_ldb_path; 
103         struct ldb_dn *basedn;
104         struct event_context *event_ctx;
105         struct loadparm_context *lp_ctx;
106
107         templates_ldb = talloc_get_type(ldb_get_opaque(ldb, "templates_ldb"), struct ldb_context);
108
109         if (!templates_ldb) {
110                 templates_ldb_path = samdb_relative_path(ldb, 
111                                                          msg, 
112                                                          "templates.ldb");
113                 if (!templates_ldb_path) {
114                         *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: Failed to contruct path for template db");
115                         return LDB_ERR_OPERATIONS_ERROR;
116                 }
117                 
118                 event_ctx = ldb_get_event_context(ldb);
119                 lp_ctx = (struct loadparm_context *)ldb_get_opaque(ldb, "loadparm");
120
121                 /* FIXME: need to remove this wehn we finally pass the event
122                  * context around in ldb */
123                 if (event_ctx == NULL) {
124                         event_ctx = event_context_init(templates_ldb);
125                 }
126
127                 templates_ldb = ldb_wrap_connect(ldb, event_ctx, lp_ctx, 
128                                                 templates_ldb_path, NULL,
129                                                 NULL, 0, NULL);
130                 talloc_free(templates_ldb_path);
131                 if (!templates_ldb) {
132                         *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: Failed to connect to templates db at: %s",
133                                              templates_ldb_path);
134                         return LDB_ERR_OPERATIONS_ERROR;
135                 }
136                 
137                 ret = ldb_set_opaque(ldb, "templates_ldb", templates_ldb);
138                 if (ret != LDB_SUCCESS) {
139                         return ret;
140                 }
141         }
142         *errstring = NULL;      
143
144         basedn = ldb_dn_new(templates_ldb, ldb, "cn=Templates");
145         if (!ldb_dn_add_child_fmt(basedn, "CN=Template%s", name)) {
146                 talloc_free(basedn);
147                 *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: Failed to contruct DN for template '%s'", 
148                                              name);
149                 return LDB_ERR_OPERATIONS_ERROR;
150         }
151         
152         /* pull the template record */
153         ret = ldb_search(templates_ldb, basedn, LDB_SCOPE_BASE, "distinguishedName=*", NULL, &res);     
154         talloc_free(basedn);
155         if (ret != LDB_SUCCESS) {
156                 *errstring = talloc_steal(msg, ldb_errstring(templates_ldb));
157                 return ret;
158         }
159         if (res->count != 1) {
160                 *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: template '%s' matched %d records, expected 1", 
161                                              name, 
162                                              res->count);
163                 talloc_free(res);
164                 return LDB_ERR_OPERATIONS_ERROR;
165         }
166         t = res->msgs[0];
167
168         for (i = 0; i < t->num_elements; i++) {
169                 struct ldb_message_element *el = &t->elements[i];
170                 /* some elements should not be copied from the template */
171                 if (ldb_attr_cmp(el->name, "cn") == 0 ||
172                     ldb_attr_cmp(el->name, "name") == 0 ||
173                     ldb_attr_cmp(el->name, "objectClass") == 0 ||
174                     ldb_attr_cmp(el->name, "sAMAccountName") == 0 ||
175                     ldb_attr_cmp(el->name, "sAMAccountName") == 0 ||
176                     ldb_attr_cmp(el->name, "distinguishedName") == 0 ||
177                     ldb_attr_cmp(el->name, "objectGUID") == 0) {
178                         continue;
179                 }
180                 for (j = 0; j < el->num_values; j++) {
181                         ret = samdb_find_or_add_attribute(ldb, msg, el->name, 
182                                                           (char *)el->values[j].data);
183                         if (ret) {
184                                 *errstring = talloc_asprintf(msg, "Adding attribute %s failed.", el->name);
185                                 talloc_free(res);
186                                 return ret;
187                         }
188                 }
189         }
190
191         talloc_free(res);
192
193         return LDB_SUCCESS;
194 }
195
196
197 /****************************************************************************
198  Create the SID list for this user.
199 ****************************************************************************/
200 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx, 
201                                struct event_context *ev_ctx, 
202                                struct loadparm_context *lp_ctx,
203                                struct dom_sid *user_sid,
204                                struct dom_sid *group_sid, 
205                                int n_groupSIDs,
206                                struct dom_sid **groupSIDs, 
207                                bool is_authenticated,
208                                struct security_token **token)
209 {
210         struct security_token *ptoken;
211         int i;
212         NTSTATUS status;
213
214         ptoken = security_token_initialise(mem_ctx);
215         NT_STATUS_HAVE_NO_MEMORY(ptoken);
216
217         ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
218         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
219
220         ptoken->user_sid = talloc_reference(ptoken, user_sid);
221         ptoken->group_sid = talloc_reference(ptoken, group_sid);
222         ptoken->privilege_mask = 0;
223
224         ptoken->sids[0] = ptoken->user_sid;
225         ptoken->sids[1] = ptoken->group_sid;
226
227         /*
228          * Finally add the "standard" SIDs.
229          * The only difference between guest and "anonymous"
230          * is the addition of Authenticated_Users.
231          */
232         ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_WORLD);
233         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
234         ptoken->sids[3] = dom_sid_parse_talloc(ptoken->sids, SID_NT_NETWORK);
235         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
236         ptoken->num_sids = 4;
237
238         if (is_authenticated) {
239                 ptoken->sids[4] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
240                 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
241                 ptoken->num_sids++;
242         }
243
244         for (i = 0; i < n_groupSIDs; i++) {
245                 size_t check_sid_idx;
246                 for (check_sid_idx = 1; 
247                      check_sid_idx < ptoken->num_sids; 
248                      check_sid_idx++) {
249                         if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
250                                 break;
251                         }
252                 }
253
254                 if (check_sid_idx == ptoken->num_sids) {
255                         ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
256                 }
257         }
258
259         /* setup the privilege mask for this token */
260         status = samdb_privilege_setup(ev_ctx, lp_ctx, ptoken);
261         if (!NT_STATUS_IS_OK(status)) {
262                 talloc_free(ptoken);
263                 return status;
264         }
265
266         security_token_debug(10, ptoken);
267
268         *token = ptoken;
269
270         return NT_STATUS_OK;
271 }