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/>.
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"
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"
45 char *samdb_relative_path(struct ldb_context *ldb,
49 const char *base_url =
50 (const char *)ldb_get_opaque(ldb, "ldb_url");
51 char *path, *p, *full_name;
55 if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
56 return talloc_strdup(mem_ctx, name);
58 if (strncmp("tdb://", base_url, 6) == 0) {
59 base_url = base_url+6;
61 path = talloc_strdup(mem_ctx, base_url);
65 if ( (p = strrchr(path, '/')) != NULL) {
67 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
69 full_name = talloc_asprintf(mem_ctx, "./%s", name);
76 this returns a static set of system credentials. It is static so
77 that we always get the same pointer in ldb_wrap_connect()
79 struct cli_credentials *samdb_credentials(struct tevent_context *event_ctx,
80 struct loadparm_context *lp_ctx)
82 static struct cli_credentials *static_credentials;
83 struct cli_credentials *cred;
85 if (static_credentials) {
86 return static_credentials;
89 cred = cli_credentials_init(talloc_autofree_context());
93 cli_credentials_set_conf(cred, lp_ctx);
95 /* We don't want to use krb5 to talk to our samdb - recursion
96 * here would be bad, and this account isn't in the KDC
98 cli_credentials_set_kerberos_state(cred, CRED_DONT_USE_KERBEROS);
100 if (!NT_STATUS_IS_OK(cli_credentials_set_secrets(cred, event_ctx, lp_ctx, NULL, NULL,
101 SECRETS_LDAP_FILTER))) {
102 /* Perfectly OK - if not against an LDAP backend */
106 static_credentials = cred;
111 connect to the SAM database
112 return an opaque context pointer on success, or NULL on failure
114 struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx,
115 struct tevent_context *ev_ctx,
116 struct loadparm_context *lp_ctx,
117 struct auth_session_info *session_info)
119 struct ldb_context *ldb;
120 ldb = ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx,
121 lp_sam_url(lp_ctx), session_info,
122 samdb_credentials(ev_ctx, lp_ctx),
127 dsdb_make_schema_global(ldb);
132 /****************************************************************************
133 Create the SID list for this user.
134 ****************************************************************************/
135 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx,
136 struct tevent_context *ev_ctx,
137 struct loadparm_context *lp_ctx,
138 struct dom_sid *user_sid,
139 struct dom_sid *group_sid,
141 struct dom_sid **groupSIDs,
142 bool is_authenticated,
143 struct security_token **token)
145 struct security_token *ptoken;
149 ptoken = security_token_initialise(mem_ctx);
150 NT_STATUS_HAVE_NO_MEMORY(ptoken);
152 ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
153 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
155 ptoken->user_sid = talloc_reference(ptoken, user_sid);
156 ptoken->group_sid = talloc_reference(ptoken, group_sid);
157 ptoken->privilege_mask = 0;
159 ptoken->sids[0] = ptoken->user_sid;
160 ptoken->sids[1] = ptoken->group_sid;
163 * Finally add the "standard" SIDs.
164 * The only difference between guest and "anonymous"
165 * is the addition of Authenticated_Users.
167 ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_WORLD);
168 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
169 ptoken->sids[3] = dom_sid_parse_talloc(ptoken->sids, SID_NT_NETWORK);
170 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
171 ptoken->num_sids = 4;
173 if (is_authenticated) {
174 ptoken->sids[4] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
175 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
179 for (i = 0; i < n_groupSIDs; i++) {
180 size_t check_sid_idx;
181 for (check_sid_idx = 1;
182 check_sid_idx < ptoken->num_sids;
184 if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
189 if (check_sid_idx == ptoken->num_sids) {
190 ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
194 /* setup the privilege mask for this token */
195 status = samdb_privilege_setup(ev_ctx, lp_ctx, ptoken);
196 if (!NT_STATUS_IS_OK(status)) {
201 security_token_debug(10, ptoken);