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