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/ldb/include/ldb.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "libcli/security/security.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "libcli/ldap/ldap_ndr.h"
33 #include "system/time.h"
34 #include "system/filesys.h"
36 #include "util/util_ldb.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "dsdb/common/flags.h"
39 #include "param/param.h"
41 char *samdb_relative_path(struct ldb_context *ldb,
45 const char *base_url =
46 (const char *)ldb_get_opaque(ldb, "ldb_url");
47 char *path, *p, *full_name;
51 if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
52 return talloc_strdup(mem_ctx, name);
54 path = talloc_strdup(mem_ctx, base_url);
58 if ( (p = strrchr(path, '/')) != NULL) {
60 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
62 full_name = talloc_asprintf(mem_ctx, "./%s", name);
70 connect to the SAM database
71 return an opaque context pointer on success, or NULL on failure
73 struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx,
74 struct loadparm_context *lp_ctx,
75 struct auth_session_info *session_info)
77 struct ldb_context *ldb;
78 ldb = ldb_wrap_connect(mem_ctx, lp_ctx,
79 lp_sam_url(lp_ctx), session_info,
84 dsdb_make_schema_global(ldb);
89 copy from a template record to a message
91 int samdb_copy_template(struct ldb_context *ldb,
92 struct ldb_message *msg, const char *name,
93 const char **errstring)
95 struct ldb_result *res;
96 struct ldb_message *t;
98 struct ldb_context *templates_ldb;
99 char *templates_ldb_path;
100 struct ldb_dn *basedn;
102 templates_ldb = talloc_get_type(ldb_get_opaque(ldb, "templates_ldb"), struct ldb_context);
104 if (!templates_ldb) {
105 templates_ldb_path = samdb_relative_path(ldb,
108 if (!templates_ldb_path) {
109 *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: Failed to contruct path for template db");
110 return LDB_ERR_OPERATIONS_ERROR;
113 templates_ldb = ldb_wrap_connect(ldb, (struct loadparm_context *)ldb_get_opaque(ldb, "loadparm"),
114 templates_ldb_path, NULL,
116 talloc_free(templates_ldb_path);
117 if (!templates_ldb) {
118 *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: Failed to connect to templates db at: %s",
120 return LDB_ERR_OPERATIONS_ERROR;
123 ret = ldb_set_opaque(ldb, "templates_ldb", templates_ldb);
124 if (ret != LDB_SUCCESS) {
130 basedn = ldb_dn_new(templates_ldb, ldb, "cn=Templates");
131 if (!ldb_dn_add_child_fmt(basedn, "CN=Template%s", name)) {
133 *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: Failed to contruct DN for template '%s'",
135 return LDB_ERR_OPERATIONS_ERROR;
138 /* pull the template record */
139 ret = ldb_search(templates_ldb, basedn, LDB_SCOPE_BASE, "distinguishedName=*", NULL, &res);
141 if (ret != LDB_SUCCESS) {
142 *errstring = talloc_steal(msg, ldb_errstring(templates_ldb));
145 if (res->count != 1) {
146 *errstring = talloc_asprintf(msg, "samdb_copy_template: ERROR: template '%s' matched %d records, expected 1",
150 return LDB_ERR_OPERATIONS_ERROR;
154 for (i = 0; i < t->num_elements; i++) {
155 struct ldb_message_element *el = &t->elements[i];
156 /* some elements should not be copied from the template */
157 if (ldb_attr_cmp(el->name, "cn") == 0 ||
158 ldb_attr_cmp(el->name, "name") == 0 ||
159 ldb_attr_cmp(el->name, "objectClass") == 0 ||
160 ldb_attr_cmp(el->name, "sAMAccountName") == 0 ||
161 ldb_attr_cmp(el->name, "sAMAccountName") == 0 ||
162 ldb_attr_cmp(el->name, "distinguishedName") == 0 ||
163 ldb_attr_cmp(el->name, "objectGUID") == 0) {
166 for (j = 0; j < el->num_values; j++) {
167 ret = samdb_find_or_add_attribute(ldb, msg, el->name,
168 (char *)el->values[j].data);
170 *errstring = talloc_asprintf(msg, "Adding attribute %s failed.", el->name);
183 /****************************************************************************
184 Create the SID list for this user.
185 ****************************************************************************/
186 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx,
187 struct loadparm_context *lp_ctx,
188 struct dom_sid *user_sid,
189 struct dom_sid *group_sid,
191 struct dom_sid **groupSIDs,
192 bool is_authenticated,
193 struct security_token **token)
195 struct security_token *ptoken;
199 ptoken = security_token_initialise(mem_ctx);
200 NT_STATUS_HAVE_NO_MEMORY(ptoken);
202 ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
203 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
205 ptoken->user_sid = talloc_reference(ptoken, user_sid);
206 ptoken->group_sid = talloc_reference(ptoken, group_sid);
207 ptoken->privilege_mask = 0;
209 ptoken->sids[0] = ptoken->user_sid;
210 ptoken->sids[1] = ptoken->group_sid;
213 * Finally add the "standard" SIDs.
214 * The only difference between guest and "anonymous"
215 * is the addition of Authenticated_Users.
217 ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_WORLD);
218 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
219 ptoken->sids[3] = dom_sid_parse_talloc(ptoken->sids, SID_NT_NETWORK);
220 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
221 ptoken->num_sids = 4;
223 if (is_authenticated) {
224 ptoken->sids[4] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
225 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
229 for (i = 0; i < n_groupSIDs; i++) {
230 size_t check_sid_idx;
231 for (check_sid_idx = 1;
232 check_sid_idx < ptoken->num_sids;
234 if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
239 if (check_sid_idx == ptoken->num_sids) {
240 ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
244 /* setup the privilege mask for this token */
245 status = samdb_privilege_setup(lp_ctx, ptoken);
246 if (!NT_STATUS_IS_OK(status)) {
251 security_token_debug(10, ptoken);