c2a1e9ec8010b60c81b88c2c20b928b90095da4b
[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 "../lib/util/util_ldb.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "../libds/common/flags.h"
40 #include "param/param.h"
41 #include "lib/events/events.h"
42 #include "auth/credentials/credentials.h"
43 #include "param/secrets.h"
44 #include "auth/auth.h"
45
46 char *samdb_relative_path(struct ldb_context *ldb,
47                                  TALLOC_CTX *mem_ctx, 
48                                  const char *name) 
49 {
50         const char *base_url = 
51                 (const char *)ldb_get_opaque(ldb, "ldb_url");
52         char *path, *p, *full_name;
53         if (name == NULL) {
54                 return NULL;
55         }
56         if (strncmp("tdb://", base_url, 6) == 0) {
57                 base_url = base_url+6;
58         }
59         path = talloc_strdup(mem_ctx, base_url);
60         if (path == NULL) {
61                 return NULL;
62         }
63         if ( (p = strrchr(path, '/')) != NULL) {
64                 p[0] = '\0';
65                 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
66         } else {
67                 full_name = talloc_asprintf(mem_ctx, "./%s", name);
68         }
69         talloc_free(path);
70         return full_name;
71 }
72
73 /*
74   make sure the static credentials are not freed
75  */
76 static int samdb_credentials_destructor(struct cli_credentials *creds)
77 {
78         return -1;
79 }
80
81 /*
82   this returns a static set of system credentials. It is static so
83   that we always get the same pointer in ldb_wrap_connect()
84  */
85 struct cli_credentials *samdb_credentials(struct tevent_context *event_ctx, 
86                                           struct loadparm_context *lp_ctx)
87 {
88         static struct cli_credentials *static_credentials;
89         struct cli_credentials *cred;
90         char *error_string;
91
92         if (static_credentials) {
93                 return static_credentials;
94         }
95
96         cred = cli_credentials_init(talloc_autofree_context());
97         if (!cred) {
98                 return NULL;
99         }
100         cli_credentials_set_conf(cred, lp_ctx);
101
102         /* We don't want to use krb5 to talk to our samdb - recursion
103          * here would be bad, and this account isn't in the KDC
104          * anyway */
105         cli_credentials_set_kerberos_state(cred, CRED_DONT_USE_KERBEROS);
106
107         if (!NT_STATUS_IS_OK(cli_credentials_set_secrets(cred, event_ctx, lp_ctx, NULL, NULL,
108                                                          SECRETS_LDAP_FILTER, &error_string))) {
109                 DEBUG(5, ("(normal if no LDAP backend) %s", error_string));
110                 /* Perfectly OK - if not against an LDAP backend */
111                 talloc_free(cred);
112                 return NULL;
113         }
114         static_credentials = cred;
115         talloc_set_destructor(cred, samdb_credentials_destructor);
116         return cred;
117 }
118
119 /*
120   connect to the SAM database
121   return an opaque context pointer on success, or NULL on failure
122  */
123 struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx, 
124                                   struct tevent_context *ev_ctx,
125                                   struct loadparm_context *lp_ctx,
126                                   struct auth_session_info *session_info,
127                                   int flags)
128 {
129         struct ldb_context *ldb;
130         struct dsdb_schema *schema;
131         const char *url;
132         struct cli_credentials *credentials;
133         int ret;
134
135         url  = lpcfg_sam_url(lp_ctx);
136         credentials = samdb_credentials(ev_ctx, lp_ctx);
137
138         ldb = ldb_wrap_find(url, ev_ctx, lp_ctx, session_info, credentials, flags);
139         if (ldb != NULL)
140                 return talloc_reference(mem_ctx, ldb);
141
142         ldb = samba_ldb_init(mem_ctx, ev_ctx, lp_ctx, session_info, credentials);
143
144         if (ldb == NULL)
145                 return NULL;
146
147         dsdb_set_global_schema(ldb);
148
149         ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
150         if (ret != LDB_SUCCESS) {
151                 talloc_free(ldb);
152                 return NULL;
153         }
154
155         schema = dsdb_get_schema(ldb, NULL);
156         /* make the resulting schema global */
157         if (schema) {
158                 dsdb_make_schema_global(ldb, schema);
159         }
160
161         if (!ldb_wrap_add(url, ev_ctx, lp_ctx, session_info, credentials, flags, ldb)) {
162                 talloc_free(ldb);
163                 return NULL;
164         }
165
166         return ldb;
167 }
168
169
170 /****************************************************************************
171  Create the SID list for this user.
172 ****************************************************************************/
173 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx, 
174                                struct tevent_context *ev_ctx, 
175                                struct loadparm_context *lp_ctx,
176                                struct dom_sid *user_sid,
177                                struct dom_sid *group_sid, 
178                                unsigned int n_groupSIDs,
179                                struct dom_sid **groupSIDs, 
180                                uint32_t session_info_flags,
181                                struct security_token **token)
182 {
183         struct security_token *ptoken;
184         unsigned int i;
185         NTSTATUS status;
186
187         ptoken = security_token_initialise(mem_ctx);
188         NT_STATUS_HAVE_NO_MEMORY(ptoken);
189
190         ptoken->sids = talloc_array(ptoken, struct dom_sid, n_groupSIDs + 6 /* over-allocate */);
191         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
192
193         ptoken->num_sids = 1;
194
195         ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
196         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
197
198         ptoken->sids[PRIMARY_USER_SID_INDEX] = *user_sid;
199         if (!dom_sid_equal(user_sid, group_sid)) {
200                 ptoken->sids[PRIMARY_GROUP_SID_INDEX] = *group_sid;
201                 ptoken->num_sids++;
202         }
203
204         /*
205          * Finally add the "standard" SIDs.
206          * The only difference between guest and "anonymous"
207          * is the addition of Authenticated_Users.
208          */
209
210         if (session_info_flags & AUTH_SESSION_INFO_DEFAULT_GROUPS) {
211                 ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 2);
212                 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
213
214                 if (!dom_sid_parse(SID_WORLD, &ptoken->sids[ptoken->num_sids])) {
215                         return NT_STATUS_INTERNAL_ERROR;
216                 }
217                 ptoken->num_sids++;
218
219                 if (!dom_sid_parse(SID_NT_NETWORK, &ptoken->sids[ptoken->num_sids])) {
220                         return NT_STATUS_INTERNAL_ERROR;
221                 }
222                 ptoken->num_sids++;
223         }
224
225         if (session_info_flags & AUTH_SESSION_INFO_AUTHENTICATED) {
226                 ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
227                 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
228
229                 if (!dom_sid_parse(SID_NT_AUTHENTICATED_USERS, &ptoken->sids[ptoken->num_sids])) {
230                         return NT_STATUS_INTERNAL_ERROR;
231                 }
232                 ptoken->num_sids++;
233         }
234
235         for (i = 0; i < n_groupSIDs; i++) {
236                 size_t check_sid_idx;
237                 for (check_sid_idx = 1; 
238                      check_sid_idx < ptoken->num_sids; 
239                      check_sid_idx++) {
240                         if (dom_sid_equal(&ptoken->sids[check_sid_idx], groupSIDs[i])) {
241                                 break;
242                         }
243                 }
244
245                 if (check_sid_idx == ptoken->num_sids) {
246                         ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
247                         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
248
249                         ptoken->sids[ptoken->num_sids] = *groupSIDs[i];
250                         ptoken->num_sids++;
251                 }
252         }
253
254         /* setup the privilege mask for this token */
255         status = samdb_privilege_setup(ev_ctx, lp_ctx, ptoken);
256         if (!NT_STATUS_IS_OK(status)) {
257                 talloc_free(ptoken);
258                 return status;
259         }
260
261         security_token_debug(10, ptoken);
262
263         *token = ptoken;
264
265         return NT_STATUS_OK;
266 }