s3: piddir creation fix part 2.
[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_security.h"
27 #include "lib/events/events.h"
28 #include "lib/ldb-samba/ldb_wrap.h"
29 #include <ldb.h>
30 #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   connect to the SAM database specified by URL
48   return an opaque context pointer on success, or NULL on failure
49  */
50 struct ldb_context *samdb_connect_url(TALLOC_CTX *mem_ctx,
51                                   struct tevent_context *ev_ctx,
52                                   struct loadparm_context *lp_ctx,
53                                   struct auth_session_info *session_info,
54                                   unsigned int flags, const char *url)
55 {
56         struct ldb_context *ldb;
57         struct dsdb_schema *schema;
58         int ret;
59
60         ldb = ldb_wrap_find(url, ev_ctx, lp_ctx, session_info, NULL, flags);
61         if (ldb != NULL)
62                 return talloc_reference(mem_ctx, ldb);
63
64         ldb = samba_ldb_init(mem_ctx, ev_ctx, lp_ctx, session_info, NULL);
65
66         if (ldb == NULL)
67                 return NULL;
68
69         dsdb_set_global_schema(ldb);
70
71         ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
72         if (ret != LDB_SUCCESS) {
73                 talloc_free(ldb);
74                 return NULL;
75         }
76
77         schema = dsdb_get_schema(ldb, NULL);
78         /* make the resulting schema global */
79         if (schema) {
80                 dsdb_make_schema_global(ldb, schema);
81         }
82
83         if (!ldb_wrap_add(url, ev_ctx, lp_ctx, session_info, NULL, flags, ldb)) {
84                 talloc_free(ldb);
85                 return NULL;
86         }
87
88         return ldb;
89 }
90
91
92 /*
93   connect to the SAM database
94   return an opaque context pointer on success, or NULL on failure
95  */
96 struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx,
97                                   struct tevent_context *ev_ctx,
98                                   struct loadparm_context *lp_ctx,
99                                   struct auth_session_info *session_info,
100                                   unsigned int flags)
101 {
102         return samdb_connect_url(mem_ctx, ev_ctx, lp_ctx, session_info, flags, "sam.ldb");
103 }
104
105 /****************************************************************************
106  Create the SID list for this user.
107 ****************************************************************************/
108 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx, 
109                                struct loadparm_context *lp_ctx,
110                                unsigned int num_sids,
111                                struct dom_sid *sids,
112                                uint32_t session_info_flags,
113                                struct security_token **token)
114 {
115         struct security_token *ptoken;
116         unsigned int i;
117         NTSTATUS status;
118
119         ptoken = security_token_initialise(mem_ctx);
120         NT_STATUS_HAVE_NO_MEMORY(ptoken);
121
122         ptoken->sids = talloc_array(ptoken, struct dom_sid, num_sids + 6 /* over-allocate */);
123         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
124
125         ptoken->num_sids = 0;
126
127         for (i = 0; i < num_sids; i++) {
128                 size_t check_sid_idx;
129                 for (check_sid_idx = 0;
130                      check_sid_idx < ptoken->num_sids;
131                      check_sid_idx++) {
132                         if (dom_sid_equal(&ptoken->sids[check_sid_idx], &sids[i])) {
133                                 break;
134                         }
135                 }
136
137                 if (check_sid_idx == ptoken->num_sids) {
138                         ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
139                         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
140
141                         ptoken->sids[ptoken->num_sids] = sids[i];
142                         ptoken->num_sids++;
143                 }
144         }
145
146         /*
147          * Finally add the "standard" sids.
148          * The only difference between guest and "anonymous"
149          * is the addition of Authenticated_Users.
150          */
151
152         if (session_info_flags & AUTH_SESSION_INFO_DEFAULT_GROUPS) {
153                 ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 2);
154                 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
155
156                 if (!dom_sid_parse(SID_WORLD, &ptoken->sids[ptoken->num_sids])) {
157                         return NT_STATUS_INTERNAL_ERROR;
158                 }
159                 ptoken->num_sids++;
160
161                 if (!dom_sid_parse(SID_NT_NETWORK, &ptoken->sids[ptoken->num_sids])) {
162                         return NT_STATUS_INTERNAL_ERROR;
163                 }
164                 ptoken->num_sids++;
165         }
166
167         if (session_info_flags & AUTH_SESSION_INFO_AUTHENTICATED) {
168                 ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
169                 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
170
171                 if (!dom_sid_parse(SID_NT_AUTHENTICATED_USERS, &ptoken->sids[ptoken->num_sids])) {
172                         return NT_STATUS_INTERNAL_ERROR;
173                 }
174                 ptoken->num_sids++;
175         }
176
177         /* The caller may have requested simple privilages, for example if there isn't a local DB */
178         if (session_info_flags & AUTH_SESSION_INFO_SIMPLE_PRIVILEGES) {
179                 /* Shortcuts to prevent recursion and avoid lookups */
180                 if (ptoken->sids == NULL) {
181                         ptoken->privilege_mask = 0;
182                 } else if (security_token_is_system(ptoken)) {
183                         ptoken->privilege_mask = ~0;
184                 } else if (security_token_is_anonymous(ptoken)) {
185                         ptoken->privilege_mask = 0;
186                 } else if (security_token_has_builtin_administrators(ptoken)) {
187                         ptoken->privilege_mask = ~0;
188                 } else {
189                         /* All other 'users' get a empty priv set so far */
190                         ptoken->privilege_mask = 0;
191                 }
192         } else {
193                 /* setup the privilege mask for this token */
194                 status = samdb_privilege_setup(lp_ctx, ptoken);
195                 if (!NT_STATUS_IS_OK(status)) {
196                         talloc_free(ptoken);
197                         DEBUG(1,("Unable to access privileges database\n"));
198                         return status;
199                 }
200         }
201
202         security_token_debug(0, 10, ptoken);
203
204         *token = ptoken;
205
206         return NT_STATUS_OK;
207 }