samba-tool: Fix enum values in dns.py
[Samba.git] / source4 / dsdb / samdb / samdb.c
blob4584a61d475d1df394e9925aae74a451c1d07d20
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 int ret;
59 ldb = ldb_wrap_find(url, ev_ctx, lp_ctx, session_info, NULL, flags);
60 if (ldb != NULL)
61 return talloc_reference(mem_ctx, ldb);
63 ldb = samba_ldb_init(mem_ctx, ev_ctx, lp_ctx, session_info, NULL);
65 if (ldb == NULL)
66 return NULL;
68 dsdb_set_global_schema(ldb);
70 ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
71 if (ret != LDB_SUCCESS) {
72 talloc_free(ldb);
73 return NULL;
76 if (!ldb_wrap_add(url, ev_ctx, lp_ctx, session_info, NULL, flags, ldb)) {
77 talloc_free(ldb);
78 return NULL;
81 return ldb;
86 connect to the SAM database
87 return an opaque context pointer on success, or NULL on failure
89 struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx,
90 struct tevent_context *ev_ctx,
91 struct loadparm_context *lp_ctx,
92 struct auth_session_info *session_info,
93 unsigned int flags)
95 return samdb_connect_url(mem_ctx, ev_ctx, lp_ctx, session_info, flags, "sam.ldb");
98 /****************************************************************************
99 Create the SID list for this user.
100 ****************************************************************************/
101 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx,
102 struct loadparm_context *lp_ctx,
103 unsigned int num_sids,
104 struct dom_sid *sids,
105 uint32_t session_info_flags,
106 struct security_token **token)
108 struct security_token *ptoken;
109 unsigned int i;
110 NTSTATUS status;
112 ptoken = security_token_initialise(mem_ctx);
113 NT_STATUS_HAVE_NO_MEMORY(ptoken);
115 ptoken->sids = talloc_array(ptoken, struct dom_sid, num_sids + 6 /* over-allocate */);
116 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
118 ptoken->num_sids = 0;
120 for (i = 0; i < num_sids; i++) {
121 size_t check_sid_idx;
122 for (check_sid_idx = 0;
123 check_sid_idx < ptoken->num_sids;
124 check_sid_idx++) {
125 if (dom_sid_equal(&ptoken->sids[check_sid_idx], &sids[i])) {
126 break;
130 if (check_sid_idx == ptoken->num_sids) {
131 ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
132 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
134 ptoken->sids[ptoken->num_sids] = sids[i];
135 ptoken->num_sids++;
139 /* The caller may have requested simple privilages, for example if there isn't a local DB */
140 if (session_info_flags & AUTH_SESSION_INFO_SIMPLE_PRIVILEGES) {
141 /* Shortcuts to prevent recursion and avoid lookups */
142 if (ptoken->sids == NULL) {
143 ptoken->privilege_mask = 0;
144 } else if (security_token_is_system(ptoken)) {
145 ptoken->privilege_mask = ~0;
146 } else if (security_token_is_anonymous(ptoken)) {
147 ptoken->privilege_mask = 0;
148 } else if (security_token_has_builtin_administrators(ptoken)) {
149 ptoken->privilege_mask = ~0;
150 } else {
151 /* All other 'users' get a empty priv set so far */
152 ptoken->privilege_mask = 0;
154 } else {
155 /* setup the privilege mask for this token */
156 status = samdb_privilege_setup(lp_ctx, ptoken);
157 if (!NT_STATUS_IS_OK(status)) {
158 talloc_free(ptoken);
159 DEBUG(1,("Unable to access privileges database\n"));
160 return status;
164 security_token_debug(0, 10, ptoken);
166 *token = ptoken;
168 return NT_STATUS_OK;