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