2 Unix SMB/CIFS implementation.
4 Helpers to add users and groups to the DB
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Volker Lendecke 2004
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2010
9 Copyright (C) Matthias Dieter Wallnöfer 2009
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "dsdb/samdb/samdb.h"
27 #include "dsdb/common/util.h"
28 #include "../libds/common/flags.h"
29 #include "libcli/security/security.h"
31 #include "libds/common/flag_mapping.h"
33 /* Add a user, SAMR style, including the correct transaction
34 * semantics. Used by the SAMR server and by pdb_samba4 */
35 NTSTATUS
dsdb_add_user(struct ldb_context
*ldb
,
37 const char *account_name
,
39 const struct dom_sid
*forced_sid
,
44 struct ldb_message
*msg
;
46 const char *container
, *obj_class
=NULL
;
50 const char *attrs
[] = {
56 uint32_t user_account_control
;
57 struct ldb_dn
*account_dn
;
58 struct dom_sid
*account_sid
;
60 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
61 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
64 * Start a transaction, so we can query and do a subsequent atomic
68 ret
= ldb_transaction_start(ldb
);
69 if (ret
!= LDB_SUCCESS
) {
70 DEBUG(0,("Failed to start a transaction for user creation: %s\n",
73 return NT_STATUS_LOCK_NOT_GRANTED
;
76 /* check if the user already exists */
77 name
= samdb_search_string(ldb
, tmp_ctx
, NULL
,
79 "(&(sAMAccountName=%s)(objectclass=user))",
80 ldb_binary_encode_string(tmp_ctx
, account_name
));
82 ldb_transaction_cancel(ldb
);
84 return NT_STATUS_USER_EXISTS
;
87 cn_name
= talloc_strdup(tmp_ctx
, account_name
);
89 ldb_transaction_cancel(ldb
);
91 return NT_STATUS_NO_MEMORY
;
94 cn_name_len
= strlen(cn_name
);
95 if (cn_name_len
< 1) {
96 ldb_transaction_cancel(ldb
);
98 return NT_STATUS_INVALID_PARAMETER
;
101 msg
= ldb_msg_new(tmp_ctx
);
103 ldb_transaction_cancel(ldb
);
104 talloc_free(tmp_ctx
);
105 return NT_STATUS_NO_MEMORY
;
108 /* This must be one of these values *only* */
109 if (acct_flags
== ACB_NORMAL
) {
110 container
= "CN=Users";
112 user_account_control
= UF_NORMAL_ACCOUNT
;
113 } else if (acct_flags
== ACB_WSTRUST
) {
114 if (cn_name
[cn_name_len
- 1] != '$') {
115 ldb_transaction_cancel(ldb
);
116 return NT_STATUS_FOOBAR
;
118 cn_name
[cn_name_len
- 1] = '\0';
119 container
= "CN=Computers";
120 obj_class
= "computer";
121 user_account_control
= UF_WORKSTATION_TRUST_ACCOUNT
;
123 } else if (acct_flags
== ACB_SVRTRUST
) {
124 if (cn_name
[cn_name_len
- 1] != '$') {
125 ldb_transaction_cancel(ldb
);
126 return NT_STATUS_FOOBAR
;
128 cn_name
[cn_name_len
- 1] = '\0';
129 container
= "OU=Domain Controllers";
130 obj_class
= "computer";
131 user_account_control
= UF_SERVER_TRUST_ACCOUNT
;
132 } else if (acct_flags
== ACB_DOMTRUST
) {
133 DEBUG(3, ("Invalid account flags specified: cannot create domain trusts via this interface (must use LSA CreateTrustedDomain calls\n"));
134 ldb_transaction_cancel(ldb
);
135 talloc_free(tmp_ctx
);
136 return NT_STATUS_INVALID_PARAMETER
;
138 DEBUG(3, ("Invalid account flags specified 0x%08X, must be exactly one of \n"
139 "ACB_NORMAL (0x%08X) ACB_WSTRUST (0x%08X) or ACB_SVRTRUST (0x%08X)\n",
141 ACB_NORMAL
, ACB_WSTRUST
, ACB_SVRTRUST
));
142 ldb_transaction_cancel(ldb
);
143 talloc_free(tmp_ctx
);
144 return NT_STATUS_INVALID_PARAMETER
;
147 user_account_control
|= UF_ACCOUNTDISABLE
| UF_PASSWD_NOTREQD
;
149 /* add core elements to the ldb_message for the user */
150 msg
->dn
= ldb_dn_copy(msg
, ldb_get_default_basedn(ldb
));
151 if ( ! ldb_dn_add_child_fmt(msg
->dn
, "CN=%s,%s", cn_name
, container
)) {
152 ldb_transaction_cancel(ldb
);
153 talloc_free(tmp_ctx
);
154 return NT_STATUS_FOOBAR
;
157 ldb_msg_add_string(msg
, "sAMAccountName", account_name
);
158 ldb_msg_add_string(msg
, "objectClass", obj_class
);
159 samdb_msg_add_uint(ldb
, tmp_ctx
, msg
,
160 "userAccountControl",
161 user_account_control
);
163 /* This is only here for migrations using pdb_samba4, the
164 * caller and the samldb are responsible for ensuring it makes
167 ret
= samdb_msg_add_dom_sid(ldb
, msg
, msg
, "objectSID", forced_sid
);
168 if (ret
!= LDB_SUCCESS
) {
169 ldb_transaction_cancel(ldb
);
170 talloc_free(tmp_ctx
);
171 return NT_STATUS_INTERNAL_ERROR
;
175 /* create the user */
176 ret
= ldb_add(ldb
, msg
);
180 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
181 ldb_transaction_cancel(ldb
);
182 DEBUG(0,("Failed to create user record %s: %s\n",
183 ldb_dn_get_linearized(msg
->dn
),
184 ldb_errstring(ldb
)));
185 talloc_free(tmp_ctx
);
186 return NT_STATUS_USER_EXISTS
;
187 case LDB_ERR_UNWILLING_TO_PERFORM
:
188 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
189 ldb_transaction_cancel(ldb
);
190 DEBUG(0,("Failed to create user record %s: %s\n",
191 ldb_dn_get_linearized(msg
->dn
),
192 ldb_errstring(ldb
)));
193 talloc_free(tmp_ctx
);
194 return NT_STATUS_ACCESS_DENIED
;
196 ldb_transaction_cancel(ldb
);
197 DEBUG(0,("Failed to create user record %s: %s\n",
198 ldb_dn_get_linearized(msg
->dn
),
199 ldb_errstring(ldb
)));
200 talloc_free(tmp_ctx
);
201 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
204 account_dn
= msg
->dn
;
206 /* retrieve the sid and account control bits for the user just created */
207 ret
= dsdb_search_one(ldb
, tmp_ctx
, &msg
,
208 account_dn
, LDB_SCOPE_BASE
, attrs
, 0, NULL
);
210 if (ret
!= LDB_SUCCESS
) {
211 ldb_transaction_cancel(ldb
);
212 DEBUG(0,("Can't locate the account we just created %s: %s\n",
213 ldb_dn_get_linearized(account_dn
), ldb_errstring(ldb
)));
214 talloc_free(tmp_ctx
);
215 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
217 account_sid
= samdb_result_dom_sid(tmp_ctx
, msg
, "objectSid");
218 if (account_sid
== NULL
) {
219 ldb_transaction_cancel(ldb
);
220 DEBUG(0,("Apparently we failed to get the objectSid of the just created account record %s\n",
221 ldb_dn_get_linearized(msg
->dn
)));
222 talloc_free(tmp_ctx
);
223 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
226 ret
= ldb_transaction_commit(ldb
);
227 if (ret
!= LDB_SUCCESS
) {
228 DEBUG(0,("Failed to commit transaction to add and modify account record %s: %s\n",
229 ldb_dn_get_linearized(msg
->dn
),
230 ldb_errstring(ldb
)));
231 talloc_free(tmp_ctx
);
232 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
234 *dn
= talloc_steal(mem_ctx
, account_dn
);
236 *sid
= talloc_steal(mem_ctx
, account_sid
);
238 talloc_free(tmp_ctx
);
243 called by samr_CreateDomainGroup and pdb_samba4
245 NTSTATUS
dsdb_add_domain_group(struct ldb_context
*ldb
,
247 const char *groupname
,
248 struct dom_sid
**sid
,
252 struct ldb_message
*msg
;
253 struct dom_sid
*group_sid
;
256 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
257 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
259 /* check if the group already exists */
260 name
= samdb_search_string(ldb
, tmp_ctx
, NULL
,
262 "(&(sAMAccountName=%s)(objectclass=group))",
263 ldb_binary_encode_string(tmp_ctx
, groupname
));
265 return NT_STATUS_GROUP_EXISTS
;
268 msg
= ldb_msg_new(tmp_ctx
);
270 return NT_STATUS_NO_MEMORY
;
273 /* add core elements to the ldb_message for the user */
274 msg
->dn
= ldb_dn_copy(tmp_ctx
, ldb_get_default_basedn(ldb
));
275 ldb_dn_add_child_fmt(msg
->dn
, "CN=%s,CN=Users", groupname
);
277 talloc_free(tmp_ctx
);
278 return NT_STATUS_NO_MEMORY
;
280 ldb_msg_add_string(msg
, "sAMAccountName", groupname
);
281 ldb_msg_add_string(msg
, "objectClass", "group");
283 /* create the group */
284 ret
= ldb_add(ldb
, msg
);
288 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
289 DEBUG(0,("Failed to create group record %s: %s\n",
290 ldb_dn_get_linearized(msg
->dn
),
291 ldb_errstring(ldb
)));
292 talloc_free(tmp_ctx
);
293 return NT_STATUS_GROUP_EXISTS
;
294 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
295 DEBUG(0,("Failed to create group record %s: %s\n",
296 ldb_dn_get_linearized(msg
->dn
),
297 ldb_errstring(ldb
)));
298 talloc_free(tmp_ctx
);
299 return NT_STATUS_ACCESS_DENIED
;
301 DEBUG(0,("Failed to create group record %s: %s\n",
302 ldb_dn_get_linearized(msg
->dn
),
303 ldb_errstring(ldb
)));
304 talloc_free(tmp_ctx
);
305 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
308 /* retrieve the sid for the group just created */
309 group_sid
= samdb_search_dom_sid(ldb
, tmp_ctx
,
310 msg
->dn
, "objectSid", NULL
);
311 if (group_sid
== NULL
) {
312 return NT_STATUS_UNSUCCESSFUL
;
315 *dn
= talloc_steal(mem_ctx
, msg
->dn
);
316 *sid
= talloc_steal(mem_ctx
, group_sid
);
317 talloc_free(tmp_ctx
);
321 NTSTATUS
dsdb_add_domain_alias(struct ldb_context
*ldb
,
323 const char *alias_name
,
324 struct dom_sid
**sid
,
328 struct ldb_message
*msg
;
329 struct dom_sid
*alias_sid
;
332 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
333 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
335 if (ldb_transaction_start(ldb
) != LDB_SUCCESS
) {
336 DEBUG(0, ("Failed to start transaction in dsdb_add_domain_alias(): %s\n", ldb_errstring(ldb
)));
337 return NT_STATUS_INTERNAL_ERROR
;
340 /* Check if alias already exists */
341 name
= samdb_search_string(ldb
, tmp_ctx
, NULL
,
343 "(sAMAccountName=%s)(objectclass=group))",
344 ldb_binary_encode_string(mem_ctx
, alias_name
));
347 talloc_free(tmp_ctx
);
348 ldb_transaction_cancel(ldb
);
349 return NT_STATUS_ALIAS_EXISTS
;
352 msg
= ldb_msg_new(tmp_ctx
);
354 talloc_free(tmp_ctx
);
355 ldb_transaction_cancel(ldb
);
356 return NT_STATUS_NO_MEMORY
;
359 /* add core elements to the ldb_message for the alias */
360 msg
->dn
= ldb_dn_copy(mem_ctx
, ldb_get_default_basedn(ldb
));
361 ldb_dn_add_child_fmt(msg
->dn
, "CN=%s,CN=Users", alias_name
);
363 talloc_free(tmp_ctx
);
364 ldb_transaction_cancel(ldb
);
365 return NT_STATUS_NO_MEMORY
;
368 ldb_msg_add_string(msg
, "sAMAccountName", alias_name
);
369 ldb_msg_add_string(msg
, "objectClass", "group");
370 samdb_msg_add_int(ldb
, mem_ctx
, msg
, "groupType", GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
);
372 /* create the alias */
373 ret
= ldb_add(ldb
, msg
);
377 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
378 talloc_free(tmp_ctx
);
379 ldb_transaction_cancel(ldb
);
380 return NT_STATUS_ALIAS_EXISTS
;
381 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
382 talloc_free(tmp_ctx
);
383 ldb_transaction_cancel(ldb
);
384 return NT_STATUS_ACCESS_DENIED
;
386 DEBUG(0,("Failed to create alias record %s: %s\n",
387 ldb_dn_get_linearized(msg
->dn
),
388 ldb_errstring(ldb
)));
389 talloc_free(tmp_ctx
);
390 ldb_transaction_cancel(ldb
);
391 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
394 /* retrieve the sid for the alias just created */
395 alias_sid
= samdb_search_dom_sid(ldb
, tmp_ctx
,
396 msg
->dn
, "objectSid", NULL
);
398 if (ldb_transaction_commit(ldb
) != LDB_SUCCESS
) {
399 DEBUG(0, ("Failed to commit transaction in dsdb_add_domain_alias(): %s\n",
400 ldb_errstring(ldb
)));
401 return NT_STATUS_INTERNAL_ERROR
;
404 *dn
= talloc_steal(mem_ctx
, msg
->dn
);
405 *sid
= talloc_steal(mem_ctx
, alias_sid
);
406 talloc_free(tmp_ctx
);
412 /* Return the members of this group (which may be a domain group or an alias) */
413 NTSTATUS
dsdb_enum_group_mem(struct ldb_context
*ldb
,
416 struct dom_sid
**members_out
,
417 unsigned int *pnum_members
)
419 struct ldb_message
*msg
;
422 struct dom_sid
*members
;
423 struct ldb_message_element
*member_el
;
424 const char *attrs
[] = { "member", NULL
};
426 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
427 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
429 ret
= dsdb_search_one(ldb
, tmp_ctx
, &msg
, dn
, LDB_SCOPE_BASE
, attrs
,
430 DSDB_SEARCH_SHOW_EXTENDED_DN
, NULL
);
431 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
432 talloc_free(tmp_ctx
);
433 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
435 if (ret
!= LDB_SUCCESS
) {
436 DEBUG(1, ("dsdb_enum_group_mem: dsdb_search for %s failed: %s\n",
437 ldb_dn_get_linearized(dn
), ldb_errstring(ldb
)));
438 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
441 member_el
= ldb_msg_find_element(msg
, "member");
445 talloc_free(tmp_ctx
);
449 members
= talloc_array(mem_ctx
, struct dom_sid
, member_el
->num_values
);
450 if (members
== NULL
) {
451 return NT_STATUS_NO_MEMORY
;
455 for (i
=0; i
<member_el
->num_values
; i
++) {
456 struct ldb_dn
*member_dn
= ldb_dn_from_ldb_val(tmp_ctx
, ldb
,
457 &member_el
->values
[i
]);
458 if (!member_dn
|| !ldb_dn_validate(member_dn
)) {
459 DEBUG(1, ("Could not parse %*.*s as a DN\n",
460 (int)member_el
->values
[i
].length
,
461 (int)member_el
->values
[i
].length
,
462 (const char *)member_el
->values
[i
].data
));
463 talloc_free(tmp_ctx
);
464 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
467 status
= dsdb_get_extended_dn_sid(member_dn
, &members
[j
],
469 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
470 /* If we fail finding a SID then this is no error since
471 * it could be a non SAM object - e.g. a contact */
473 } else if (!NT_STATUS_IS_OK(status
)) {
474 DEBUG(1, ("When parsing DN '%s' we failed to parse it's SID component, so we cannot fetch the membership: %s\n",
475 ldb_dn_get_extended_linearized(tmp_ctx
, member_dn
, 1),
477 talloc_free(tmp_ctx
);
484 *members_out
= talloc_steal(mem_ctx
, members
);
486 talloc_free(tmp_ctx
);
490 NTSTATUS
dsdb_lookup_rids(struct ldb_context
*ldb
,
492 const struct dom_sid
*domain_sid
,
493 unsigned int num_rids
,
496 enum lsa_SidType
*lsa_attrs
)
498 const char *attrs
[] = { "sAMAccountType", "sAMAccountName", NULL
};
499 unsigned int i
, num_mapped
;
501 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
502 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
506 for (i
=0; i
<num_rids
; i
++) {
507 struct ldb_message
*msg
;
512 lsa_attrs
[i
] = SID_NAME_UNKNOWN
;
514 dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "<SID=%s>",
515 dom_sid_string(tmp_ctx
,
516 dom_sid_add_rid(tmp_ctx
, domain_sid
,
519 talloc_free(tmp_ctx
);
520 return NT_STATUS_NO_MEMORY
;
522 rc
= dsdb_search_one(ldb
, tmp_ctx
, &msg
, dn
, LDB_SCOPE_BASE
, attrs
, 0, "samAccountName=*");
523 if (rc
== LDB_ERR_NO_SUCH_OBJECT
) {
525 } else if (rc
!= LDB_SUCCESS
) {
526 talloc_free(tmp_ctx
);
527 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
530 names
[i
] = ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
531 if (names
[i
] == NULL
) {
532 DEBUG(10, ("no samAccountName\n"));
535 talloc_steal(names
, names
[i
]);
536 attr
= ldb_msg_find_attr_as_uint(msg
, "samAccountType", 0);
537 lsa_attrs
[i
] = ds_atype_map(attr
);
538 if (lsa_attrs
[i
] == SID_NAME_UNKNOWN
) {
543 talloc_free(tmp_ctx
);
545 if (num_mapped
== 0) {
546 return NT_STATUS_NONE_MAPPED
;
548 if (num_mapped
< num_rids
) {
549 return STATUS_SOME_UNMAPPED
;