libs: s3 and s4: make our dns lookup code signal-safe.
[Samba.git] / source4 / dsdb / samdb / samdb.c
blob361ece79f0a224fcb0066f2b40f1a5093dd1f80a
1 /*
2 Unix SMB/CIFS implementation.
4 interface functions for the sam database
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Volker Lendecke 2004
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
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.
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.
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/>.
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"
47 connect to the SAM database specified by URL
48 return an opaque context pointer on success, or NULL on failure
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)
56 struct ldb_context *ldb;
57 struct dsdb_schema *schema;
58 int ret;
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);
64 ldb = samba_ldb_init(mem_ctx, ev_ctx, lp_ctx, session_info, NULL);
66 if (ldb == NULL)
67 return NULL;
69 dsdb_set_global_schema(ldb);
71 ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
72 if (ret != LDB_SUCCESS) {
73 talloc_free(ldb);
74 return NULL;
77 schema = dsdb_get_schema(ldb, NULL);
78 /* make the resulting schema global */
79 if (schema) {
80 dsdb_make_schema_global(ldb, schema);
83 if (!ldb_wrap_add(url, ev_ctx, lp_ctx, session_info, NULL, flags, ldb)) {
84 talloc_free(ldb);
85 return NULL;
88 return ldb;
93 connect to the SAM database
94 return an opaque context pointer on success, or NULL on failure
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)
102 return samdb_connect_url(mem_ctx, ev_ctx, lp_ctx, session_info, flags, "sam.ldb");
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)
115 struct security_token *ptoken;
116 unsigned int i;
117 NTSTATUS status;
119 ptoken = security_token_initialise(mem_ctx);
120 NT_STATUS_HAVE_NO_MEMORY(ptoken);
122 ptoken->sids = talloc_array(ptoken, struct dom_sid, num_sids + 6 /* over-allocate */);
123 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
125 ptoken->num_sids = 0;
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;
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);
141 ptoken->sids[ptoken->num_sids] = sids[i];
142 ptoken->num_sids++;
146 /* The caller may have requested simple privilages, for example if there isn't a local DB */
147 if (session_info_flags & AUTH_SESSION_INFO_SIMPLE_PRIVILEGES) {
148 /* Shortcuts to prevent recursion and avoid lookups */
149 if (ptoken->sids == NULL) {
150 ptoken->privilege_mask = 0;
151 } else if (security_token_is_system(ptoken)) {
152 ptoken->privilege_mask = ~0;
153 } else if (security_token_is_anonymous(ptoken)) {
154 ptoken->privilege_mask = 0;
155 } else if (security_token_has_builtin_administrators(ptoken)) {
156 ptoken->privilege_mask = ~0;
157 } else {
158 /* All other 'users' get a empty priv set so far */
159 ptoken->privilege_mask = 0;
161 } else {
162 /* setup the privilege mask for this token */
163 status = samdb_privilege_setup(lp_ctx, ptoken);
164 if (!NT_STATUS_IS_OK(status)) {
165 talloc_free(ptoken);
166 DEBUG(1,("Unable to access privileges database\n"));
167 return status;
171 security_token_debug(0, 10, ptoken);
173 *token = ptoken;
175 return NT_STATUS_OK;