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