9154f5382ba6eebd7ef93598247be6a4c9e68414
[kamenim/samba.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/ldb/include/ldb.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "libcli/security/security.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "libcli/ldap/ldap_ndr.h"
33 #include "system/time.h"
34 #include "system/filesys.h"
35 #include "ldb_wrap.h"
36 #include "util/util_ldb.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "dsdb/common/flags.h"
39 #include "param/param.h"
40 #include "lib/events/events.h"
41
42 char *samdb_relative_path(struct ldb_context *ldb,
43                                  TALLOC_CTX *mem_ctx, 
44                                  const char *name) 
45 {
46         const char *base_url = 
47                 (const char *)ldb_get_opaque(ldb, "ldb_url");
48         char *path, *p, *full_name;
49         if (name == NULL) {
50                 return NULL;
51         }
52         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
53                 return talloc_strdup(mem_ctx, name);
54         }
55         path = talloc_strdup(mem_ctx, base_url);
56         if (path == NULL) {
57                 return NULL;
58         }
59         if ( (p = strrchr(path, '/')) != NULL) {
60                 p[0] = '\0';
61                 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
62         } else {
63                 full_name = talloc_asprintf(mem_ctx, "./%s", name);
64         }
65         talloc_free(path);
66         return full_name;
67 }
68
69
70 /*
71   connect to the SAM database
72   return an opaque context pointer on success, or NULL on failure
73  */
74 struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx, 
75                                   struct event_context *ev_ctx,
76                                   struct loadparm_context *lp_ctx,
77                                   struct auth_session_info *session_info)
78 {
79         struct ldb_context *ldb;
80         ldb = ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx, 
81                                lp_sam_url(lp_ctx), session_info,
82                                NULL, 0, NULL);
83         if (!ldb) {
84                 return NULL;
85         }
86         dsdb_make_schema_global(ldb);
87         return ldb;
88 }
89
90 /*
91   copy from a template record to a message
92 */
93 int samdb_copy_template(struct ldb_context *ldb, 
94                         struct ldb_message *msg, const char *name,
95                         const char **errstring)
96 {
97         struct ldb_result *res;
98         struct ldb_message *t;
99         int ret, i, j;
100         struct ldb_context *templates_ldb;
101         char *templates_ldb_path; 
102         struct ldb_dn *basedn;
103         struct event_context *event_ctx;
104         struct loadparm_context *lp_ctx;
105
106         templates_ldb = talloc_get_type(ldb_get_opaque(ldb, "templates_ldb"), struct ldb_context);
107
108         if (!templates_ldb) {
109                 templates_ldb_path = samdb_relative_path(ldb, 
110                                                          msg, 
111                                                          "templates.ldb");
112                 if (!templates_ldb_path) {
113                         *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: Failed to contruct path for template db");
114                         return LDB_ERR_OPERATIONS_ERROR;
115                 }
116                 
117                 event_ctx = (struct event_context *)ldb_get_opaque(ldb, "EventContext");
118                 lp_ctx = (struct loadparm_context *)ldb_get_opaque(ldb, "loadparm");
119
120                 /* FIXME: need to remove this wehn we finally pass the event
121                  * context around in ldb */
122                 if (event_ctx == NULL) {
123                         event_ctx = event_context_init(templates_ldb);
124                 }
125
126                 templates_ldb = ldb_wrap_connect(ldb, event_ctx, lp_ctx, 
127                                                 templates_ldb_path, NULL,
128                                                 NULL, 0, NULL);
129                 talloc_free(templates_ldb_path);
130                 if (!templates_ldb) {
131                         *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: Failed to connect to templates db at: %s",
132                                              templates_ldb_path);
133                         return LDB_ERR_OPERATIONS_ERROR;
134                 }
135                 
136                 ret = ldb_set_opaque(ldb, "templates_ldb", templates_ldb);
137                 if (ret != LDB_SUCCESS) {
138                         return ret;
139                 }
140         }
141         *errstring = NULL;      
142
143         basedn = ldb_dn_new(templates_ldb, ldb, "cn=Templates");
144         if (!ldb_dn_add_child_fmt(basedn, "CN=Template%s", name)) {
145                 talloc_free(basedn);
146                 *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: Failed to contruct DN for template '%s'", 
147                                              name);
148                 return LDB_ERR_OPERATIONS_ERROR;
149         }
150         
151         /* pull the template record */
152         ret = ldb_search(templates_ldb, basedn, LDB_SCOPE_BASE, "distinguishedName=*", NULL, &res);     
153         talloc_free(basedn);
154         if (ret != LDB_SUCCESS) {
155                 *errstring = talloc_steal(msg, ldb_errstring(templates_ldb));
156                 return ret;
157         }
158         if (res->count != 1) {
159                 *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: template '%s' matched %d records, expected 1", 
160                                              name, 
161                                              res->count);
162                 talloc_free(res);
163                 return LDB_ERR_OPERATIONS_ERROR;
164         }
165         t = res->msgs[0];
166
167         for (i = 0; i < t->num_elements; i++) {
168                 struct ldb_message_element *el = &t->elements[i];
169                 /* some elements should not be copied from the template */
170                 if (ldb_attr_cmp(el->name, "cn") == 0 ||
171                     ldb_attr_cmp(el->name, "name") == 0 ||
172                     ldb_attr_cmp(el->name, "objectClass") == 0 ||
173                     ldb_attr_cmp(el->name, "sAMAccountName") == 0 ||
174                     ldb_attr_cmp(el->name, "sAMAccountName") == 0 ||
175                     ldb_attr_cmp(el->name, "distinguishedName") == 0 ||
176                     ldb_attr_cmp(el->name, "objectGUID") == 0) {
177                         continue;
178                 }
179                 for (j = 0; j < el->num_values; j++) {
180                         ret = samdb_find_or_add_attribute(ldb, msg, el->name, 
181                                                           (char *)el->values[j].data);
182                         if (ret) {
183                                 *errstring = talloc_asprintf(msg, "Adding attribute %s failed.", el->name);
184                                 talloc_free(res);
185                                 return ret;
186                         }
187                 }
188         }
189
190         talloc_free(res);
191
192         return LDB_SUCCESS;
193 }
194
195
196 /****************************************************************************
197  Create the SID list for this user.
198 ****************************************************************************/
199 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx, 
200                                struct event_context *ev_ctx, 
201                                struct loadparm_context *lp_ctx,
202                                struct dom_sid *user_sid,
203                                struct dom_sid *group_sid, 
204                                int n_groupSIDs,
205                                struct dom_sid **groupSIDs, 
206                                bool is_authenticated,
207                                struct security_token **token)
208 {
209         struct security_token *ptoken;
210         int i;
211         NTSTATUS status;
212
213         ptoken = security_token_initialise(mem_ctx);
214         NT_STATUS_HAVE_NO_MEMORY(ptoken);
215
216         ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
217         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
218
219         ptoken->user_sid = talloc_reference(ptoken, user_sid);
220         ptoken->group_sid = talloc_reference(ptoken, group_sid);
221         ptoken->privilege_mask = 0;
222
223         ptoken->sids[0] = ptoken->user_sid;
224         ptoken->sids[1] = ptoken->group_sid;
225
226         /*
227          * Finally add the "standard" SIDs.
228          * The only difference between guest and "anonymous"
229          * is the addition of Authenticated_Users.
230          */
231         ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_WORLD);
232         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
233         ptoken->sids[3] = dom_sid_parse_talloc(ptoken->sids, SID_NT_NETWORK);
234         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
235         ptoken->num_sids = 4;
236
237         if (is_authenticated) {
238                 ptoken->sids[4] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
239                 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
240                 ptoken->num_sids++;
241         }
242
243         for (i = 0; i < n_groupSIDs; i++) {
244                 size_t check_sid_idx;
245                 for (check_sid_idx = 1; 
246                      check_sid_idx < ptoken->num_sids; 
247                      check_sid_idx++) {
248                         if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
249                                 break;
250                         }
251                 }
252
253                 if (check_sid_idx == ptoken->num_sids) {
254                         ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
255                 }
256         }
257
258         /* setup the privilege mask for this token */
259         status = samdb_privilege_setup(ev_ctx, lp_ctx, ptoken);
260         if (!NT_STATUS_IS_OK(status)) {
261                 talloc_free(ptoken);
262                 return status;
263         }
264
265         security_token_debug(10, ptoken);
266
267         *token = ptoken;
268
269         return NT_STATUS_OK;
270 }