2 Unix SMB/CIFS implementation.
4 Map SIDs to unixids and back
6 Copyright (C) Kai Blin 2008
7 Copyright (C) Andrew Bartlett 2012
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "auth/auth.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26 #include "lib/util_unixsids.h"
29 #include "param/param.h"
30 #include "winbind/idmap.h"
31 #include "libcli/security/security.h"
32 #include "libcli/ldap/ldap_ndr.h"
33 #include "dsdb/samdb/samdb.h"
34 #include "../libds/common/flags.h"
37 * Get uid/gid bounds from idmap database
39 * \param idmap_ctx idmap context to use
40 * \param low lower uid/gid bound is stored here
41 * \param high upper uid/gid bound is stored here
42 * \return 0 on success, nonzero on failure
44 static int idmap_get_bounds(struct idmap_context
*idmap_ctx
, uint32_t *low
,
48 struct ldb_context
*ldb
= idmap_ctx
->ldb_ctx
;
50 struct ldb_result
*res
= NULL
;
51 TALLOC_CTX
*tmp_ctx
= talloc_new(idmap_ctx
);
52 uint32_t lower_bound
= (uint32_t) -1;
53 uint32_t upper_bound
= (uint32_t) -1;
55 dn
= ldb_dn_new(tmp_ctx
, ldb
, "CN=CONFIG");
56 if (dn
== NULL
) goto failed
;
58 ret
= ldb_search(ldb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
);
59 if (ret
!= LDB_SUCCESS
) goto failed
;
61 if (res
->count
!= 1) {
66 lower_bound
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "lowerBound", -1);
67 if (lower_bound
!= (uint32_t) -1) {
74 upper_bound
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "upperBound", -1);
75 if (upper_bound
!= (uint32_t) -1) {
89 * Add a dom_sid structure to a ldb_message
90 * \param idmap_ctx idmap context to use
91 * \param mem_ctx talloc context to use
92 * \param ldb_message ldb message to add dom_sid to
93 * \param attr_name name of the attribute to store the dom_sid in
94 * \param sid dom_sid to store
95 * \return 0 on success, an ldb error code on failure.
97 static int idmap_msg_add_dom_sid(struct idmap_context
*idmap_ctx
,
98 TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
99 const char *attr_name
, const struct dom_sid
*sid
)
102 enum ndr_err_code ndr_err
;
104 ndr_err
= ndr_push_struct_blob(&val
, mem_ctx
, sid
,
105 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
107 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
111 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
115 * Get a dom_sid structure from a ldb message.
117 * \param mem_ctx talloc context to allocate dom_sid memory in
118 * \param msg ldb_message to get dom_sid from
119 * \param attr_name key that has the dom_sid as data
120 * \return dom_sid structure on success, NULL on failure
122 static struct dom_sid
*idmap_msg_get_dom_sid(TALLOC_CTX
*mem_ctx
,
123 struct ldb_message
*msg
, const char *attr_name
)
126 const struct ldb_val
*val
;
127 enum ndr_err_code ndr_err
;
129 val
= ldb_msg_find_ldb_val(msg
, attr_name
);
134 sid
= talloc(mem_ctx
, struct dom_sid
);
139 ndr_err
= ndr_pull_struct_blob(val
, sid
, sid
,
140 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
141 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
150 * Initialize idmap context
152 * talloc_free to close.
154 * \param mem_ctx talloc context to use.
155 * \return allocated idmap_context on success, NULL on error
157 struct idmap_context
*idmap_init(TALLOC_CTX
*mem_ctx
,
158 struct tevent_context
*ev_ctx
,
159 struct loadparm_context
*lp_ctx
)
161 struct idmap_context
*idmap_ctx
;
163 idmap_ctx
= talloc(mem_ctx
, struct idmap_context
);
164 if (idmap_ctx
== NULL
) {
168 idmap_ctx
->lp_ctx
= lp_ctx
;
170 idmap_ctx
->ldb_ctx
= ldb_wrap_connect(idmap_ctx
, ev_ctx
, lp_ctx
,
172 system_session(lp_ctx
),
174 if (idmap_ctx
->ldb_ctx
== NULL
) {
178 idmap_ctx
->samdb
= samdb_connect(idmap_ctx
,
181 system_session(lp_ctx
),
184 if (idmap_ctx
->samdb
== NULL
) {
185 DEBUG(0, ("Failed to load sam.ldb in idmap_init\n"));
191 TALLOC_FREE(idmap_ctx
);
196 * Convert an unixid to the corresponding SID
198 * \param idmap_ctx idmap context to use
199 * \param mem_ctx talloc context the memory for the struct dom_sid is allocated
201 * \param unixid pointer to a unixid struct to convert
202 * \param sid pointer that will take the struct dom_sid pointer if the mapping
204 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping not
205 * possible or some other NTSTATUS that is more descriptive on failure.
208 static NTSTATUS
idmap_xid_to_sid(struct idmap_context
*idmap_ctx
,
210 struct unixid
*unixid
,
211 struct dom_sid
**sid
)
215 struct ldb_context
*ldb
= idmap_ctx
->ldb_ctx
;
216 struct ldb_result
*res
= NULL
;
217 struct ldb_message
*msg
;
218 const struct dom_sid
*unix_sid
;
219 struct dom_sid
*new_sid
;
220 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
223 const char *sam_attrs
[] = {"objectSid", NULL
};
226 * First check against our local DB, to see if this user has a
227 * mapping there. This means that the Samba4 AD DC behaves
228 * much like a winbindd member server running idmap_ad
231 switch (unixid
->type
) {
233 if (lpcfg_parm_bool(idmap_ctx
->lp_ctx
, NULL
, "idmap_ldb", "use rfc2307", false)) {
234 ret
= dsdb_search_one(idmap_ctx
->samdb
, tmp_ctx
, &msg
,
235 ldb_get_default_basedn(idmap_ctx
->samdb
),
238 "(&(|(sAMaccountType=%u)(sAMaccountType=%u)(sAMaccountType=%u))"
239 "(uidNumber=%u)(objectSid=*))",
240 ATYPE_ACCOUNT
, ATYPE_WORKSTATION_TRUST
, ATYPE_INTERDOMAIN_TRUST
, unixid
->id
);
242 /* If we are not to use the rfc2307 attributes, we just emulate a non-match */
243 ret
= LDB_ERR_NO_SUCH_OBJECT
;
246 if (ret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
247 DEBUG(1, ("Search for uidNumber=%lu gave duplicate results, failing to map to a SID!\n",
248 (unsigned long)unixid
->id
));
249 status
= NT_STATUS_NONE_MAPPED
;
251 } else if (ret
== LDB_SUCCESS
) {
252 *sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
254 DEBUG(1, ("Search for uidNumber=%lu did not return an objectSid!\n",
255 (unsigned long)unixid
->id
));
256 status
= NT_STATUS_NONE_MAPPED
;
259 talloc_free(tmp_ctx
);
261 } else if (ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
262 DEBUG(1, ("Search for uidNumber=%lu gave '%s', failing to map to a SID!\n",
263 (unsigned long)unixid
->id
, ldb_errstring(idmap_ctx
->samdb
)));
264 status
= NT_STATUS_NONE_MAPPED
;
268 id_type
= "ID_TYPE_UID";
271 if (lpcfg_parm_bool(idmap_ctx
->lp_ctx
, NULL
, "idmap_ldb", "use rfc2307", false)) {
272 ret
= dsdb_search_one(idmap_ctx
->samdb
, tmp_ctx
, &msg
,
273 ldb_get_default_basedn(idmap_ctx
->samdb
),
276 "(&(|(sAMaccountType=%u)(sAMaccountType=%u))(gidNumber=%u))",
277 ATYPE_SECURITY_GLOBAL_GROUP
, ATYPE_SECURITY_LOCAL_GROUP
,
280 /* If we are not to use the rfc2307 attributes, we just emulate a non-match */
281 ret
= LDB_ERR_NO_SUCH_OBJECT
;
283 if (ret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
284 DEBUG(1, ("Search for gidNumber=%lu gave duplicate results, failing to map to a SID!\n",
285 (unsigned long)unixid
->id
));
286 status
= NT_STATUS_NONE_MAPPED
;
288 } else if (ret
== LDB_SUCCESS
) {
289 *sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
291 DEBUG(1, ("Search for gidNumber=%lu did not return an objectSid!\n",
292 (unsigned long)unixid
->id
));
293 status
= NT_STATUS_NONE_MAPPED
;
296 talloc_free(tmp_ctx
);
298 } else if (ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
299 DEBUG(1, ("Search for gidNumber=%lu gave '%s', failing to map to a SID!\n",
300 (unsigned long)unixid
->id
, ldb_errstring(idmap_ctx
->samdb
)));
301 status
= NT_STATUS_NONE_MAPPED
;
305 id_type
= "ID_TYPE_GID";
308 DEBUG(1, ("unixid->type must be type gid or uid (got %u) for lookup with id %lu\n",
309 (unsigned)unixid
->type
, (unsigned long)unixid
->id
));
310 status
= NT_STATUS_NONE_MAPPED
;
314 ret
= ldb_search(ldb
, tmp_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
,
315 NULL
, "(&(|(type=ID_TYPE_BOTH)(type=%s))"
316 "(xidNumber=%u))", id_type
, unixid
->id
);
317 if (ret
!= LDB_SUCCESS
) {
318 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb
)));
319 status
= NT_STATUS_NONE_MAPPED
;
323 if (res
->count
== 1) {
324 const char *type
= ldb_msg_find_attr_as_string(res
->msgs
[0],
327 *sid
= idmap_msg_get_dom_sid(mem_ctx
, res
->msgs
[0],
330 DEBUG(1, ("Failed to get sid from db: %u\n", ret
));
331 status
= NT_STATUS_NONE_MAPPED
;
336 DEBUG(1, ("Invalid type for mapping entry.\n"));
337 talloc_free(tmp_ctx
);
338 return NT_STATUS_NONE_MAPPED
;
341 if (strcmp(type
, "ID_TYPE_BOTH") == 0) {
342 unixid
->type
= ID_TYPE_BOTH
;
343 } else if (strcmp(type
, "ID_TYPE_UID") == 0) {
344 unixid
->type
= ID_TYPE_UID
;
346 unixid
->type
= ID_TYPE_GID
;
349 talloc_free(tmp_ctx
);
353 DEBUG(6, ("xid not found in idmap db, create S-1-22- SID.\n"));
355 /* For local users/groups , we just create a rid = uid/gid */
356 if (unixid
->type
== ID_TYPE_UID
) {
357 unix_sid
= &global_sid_Unix_Users
;
359 unix_sid
= &global_sid_Unix_Groups
;
362 new_sid
= dom_sid_add_rid(mem_ctx
, unix_sid
, unixid
->id
);
363 if (new_sid
== NULL
) {
364 status
= NT_STATUS_NO_MEMORY
;
369 talloc_free(tmp_ctx
);
373 talloc_free(tmp_ctx
);
379 * Map a SID to an unixid struct.
381 * If no mapping exists, a new mapping will be created.
383 * \param idmap_ctx idmap context to use
384 * \param mem_ctx talloc context to use
385 * \param sid SID to map to an unixid struct
386 * \param unixid pointer to a unixid struct
387 * \return NT_STATUS_OK on success, NT_STATUS_INVALID_SID if the sid is not from
388 * a trusted domain and idmap trusted only = true, NT_STATUS_NONE_MAPPED if the
391 static NTSTATUS
idmap_sid_to_xid(struct idmap_context
*idmap_ctx
,
393 const struct dom_sid
*sid
,
394 struct unixid
*unixid
)
398 struct ldb_context
*ldb
= idmap_ctx
->ldb_ctx
;
400 struct ldb_message
*hwm_msg
, *map_msg
, *sam_msg
;
401 struct ldb_result
*res
= NULL
;
403 uint32_t low
, high
, hwm
, new_xid
;
404 struct dom_sid_buf sid_string
;
405 char *unixid_string
, *hwm_string
;
406 bool hwm_entry_exists
;
407 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
408 const char *sam_attrs
[] = {"uidNumber", "gidNumber", "samAccountType", NULL
};
410 if (sid_check_is_in_unix_users(sid
)) {
412 DEBUG(6, ("This is a local unix uid, just calculate that.\n"));
413 status
= dom_sid_split_rid(tmp_ctx
, sid
, NULL
, &rid
);
414 if (!NT_STATUS_IS_OK(status
)) {
415 talloc_free(tmp_ctx
);
420 unixid
->type
= ID_TYPE_UID
;
422 talloc_free(tmp_ctx
);
426 if (sid_check_is_in_unix_groups(sid
)) {
428 DEBUG(6, ("This is a local unix gid, just calculate that.\n"));
429 status
= dom_sid_split_rid(tmp_ctx
, sid
, NULL
, &rid
);
430 if (!NT_STATUS_IS_OK(status
)) {
431 talloc_free(tmp_ctx
);
436 unixid
->type
= ID_TYPE_GID
;
438 talloc_free(tmp_ctx
);
443 * First check against our local DB, to see if this user has a
444 * mapping there. This means that the Samba4 AD DC behaves
445 * much like a winbindd member server running idmap_ad
448 if (lpcfg_parm_bool(idmap_ctx
->lp_ctx
, NULL
, "idmap_ldb", "use rfc2307", false)) {
449 struct dom_sid_buf buf
;
450 ret
= dsdb_search_one(idmap_ctx
->samdb
, tmp_ctx
, &sam_msg
,
451 ldb_get_default_basedn(idmap_ctx
->samdb
),
452 LDB_SCOPE_SUBTREE
, sam_attrs
, 0,
454 "(|(sAMaccountType=%u)(sAMaccountType=%u)(sAMaccountType=%u)"
455 "(sAMaccountType=%u)(sAMaccountType=%u))"
456 "(|(uidNumber=*)(gidNumber=*)))",
457 dom_sid_str_buf(sid
, &buf
),
458 ATYPE_ACCOUNT
, ATYPE_WORKSTATION_TRUST
, ATYPE_INTERDOMAIN_TRUST
,
459 ATYPE_SECURITY_GLOBAL_GROUP
, ATYPE_SECURITY_LOCAL_GROUP
);
461 /* If we are not to use the rfc2307 attributes, we just emulate a non-match */
462 ret
= LDB_ERR_NO_SUCH_OBJECT
;
465 if (ret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
466 struct dom_sid_buf buf
;
467 DEBUG(1, ("Search for objectSid=%s gave duplicate results, failing to map to a unix ID!\n",
468 dom_sid_str_buf(sid
, &buf
)));
469 status
= NT_STATUS_NONE_MAPPED
;
471 } else if (ret
== LDB_SUCCESS
) {
472 uint32_t account_type
= ldb_msg_find_attr_as_uint(sam_msg
, "sAMaccountType", 0);
473 if ((account_type
== ATYPE_ACCOUNT
) ||
474 (account_type
== ATYPE_WORKSTATION_TRUST
) ||
475 (account_type
== ATYPE_INTERDOMAIN_TRUST
))
477 const struct ldb_val
*v
= ldb_msg_find_ldb_val(sam_msg
, "uidNumber");
479 unixid
->type
= ID_TYPE_UID
;
480 unixid
->id
= ldb_msg_find_attr_as_uint(sam_msg
, "uidNumber", -1);
481 talloc_free(tmp_ctx
);
485 } else if ((account_type
== ATYPE_SECURITY_GLOBAL_GROUP
) ||
486 (account_type
== ATYPE_SECURITY_LOCAL_GROUP
))
488 const struct ldb_val
*v
= ldb_msg_find_ldb_val(sam_msg
, "gidNumber");
490 unixid
->type
= ID_TYPE_GID
;
491 unixid
->id
= ldb_msg_find_attr_as_uint(sam_msg
, "gidNumber", -1);
492 talloc_free(tmp_ctx
);
496 } else if (ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
497 struct dom_sid_buf buf
;
498 DEBUG(1, ("Search for objectSid=%s gave '%s', failing to map to a SID!\n",
499 dom_sid_str_buf(sid
, &buf
),
500 ldb_errstring(idmap_ctx
->samdb
)));
502 status
= NT_STATUS_NONE_MAPPED
;
506 ret
= ldb_search(ldb
, tmp_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
,
507 NULL
, "(&(objectClass=sidMap)(objectSid=%s))",
508 ldap_encode_ndr_dom_sid(tmp_ctx
, sid
));
509 if (ret
!= LDB_SUCCESS
) {
510 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb
)));
511 talloc_free(tmp_ctx
);
512 return NT_STATUS_NONE_MAPPED
;
515 if (res
->count
== 1) {
516 const char *type
= ldb_msg_find_attr_as_string(res
->msgs
[0],
518 new_xid
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "xidNumber",
520 if (new_xid
== (uint32_t) -1) {
521 DEBUG(1, ("Invalid xid mapping.\n"));
522 talloc_free(tmp_ctx
);
523 return NT_STATUS_NONE_MAPPED
;
527 DEBUG(1, ("Invalid type for mapping entry.\n"));
528 talloc_free(tmp_ctx
);
529 return NT_STATUS_NONE_MAPPED
;
532 unixid
->id
= new_xid
;
534 if (strcmp(type
, "ID_TYPE_BOTH") == 0) {
535 unixid
->type
= ID_TYPE_BOTH
;
536 } else if (strcmp(type
, "ID_TYPE_UID") == 0) {
537 unixid
->type
= ID_TYPE_UID
;
539 unixid
->type
= ID_TYPE_GID
;
542 talloc_free(tmp_ctx
);
546 DEBUG(6, ("No existing mapping found, attempting to create one.\n"));
548 trans
= ldb_transaction_start(ldb
);
549 if (trans
!= LDB_SUCCESS
) {
550 status
= NT_STATUS_NONE_MAPPED
;
554 /* Redo the search to make sure no one changed the mapping while we
556 ret
= ldb_search(ldb
, tmp_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
,
557 NULL
, "(&(objectClass=sidMap)(objectSid=%s))",
558 ldap_encode_ndr_dom_sid(tmp_ctx
, sid
));
559 if (ret
!= LDB_SUCCESS
) {
560 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb
)));
561 status
= NT_STATUS_NONE_MAPPED
;
565 if (res
->count
> 0) {
566 DEBUG(1, ("Database changed while trying to add a sidmap.\n"));
567 status
= NT_STATUS_RETRY
;
571 ret
= idmap_get_bounds(idmap_ctx
, &low
, &high
);
572 if (ret
!= LDB_SUCCESS
) {
573 status
= NT_STATUS_NONE_MAPPED
;
577 dn
= ldb_dn_new(tmp_ctx
, ldb
, "CN=CONFIG");
579 status
= NT_STATUS_NO_MEMORY
;
583 ret
= ldb_search(ldb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
);
584 if (ret
!= LDB_SUCCESS
) {
585 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb
)));
586 status
= NT_STATUS_NONE_MAPPED
;
590 if (res
->count
!= 1) {
591 DEBUG(1, ("No CN=CONFIG record, idmap database is broken.\n"));
592 status
= NT_STATUS_NONE_MAPPED
;
596 hwm
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "xidNumber", -1);
597 if (hwm
== (uint32_t)-1) {
599 hwm_entry_exists
= false;
601 hwm_entry_exists
= true;
605 DEBUG(1, ("Out of xids to allocate.\n"));
606 status
= NT_STATUS_NONE_MAPPED
;
610 hwm_msg
= ldb_msg_new(tmp_ctx
);
611 if (hwm_msg
== NULL
) {
612 DEBUG(1, ("Out of memory when creating ldb_message\n"));
613 status
= NT_STATUS_NO_MEMORY
;
622 hwm_string
= talloc_asprintf(tmp_ctx
, "%u", hwm
);
623 if (hwm_string
== NULL
) {
624 status
= NT_STATUS_NO_MEMORY
;
628 dom_sid_str_buf(sid
, &sid_string
);
630 unixid_string
= talloc_asprintf(tmp_ctx
, "%u", new_xid
);
631 if (unixid_string
== NULL
) {
632 status
= NT_STATUS_NO_MEMORY
;
636 if (hwm_entry_exists
) {
637 struct ldb_message_element
*els
;
638 struct ldb_val
*vals
;
640 /* We're modifying the entry, not just adding a new one. */
641 els
= talloc_array(tmp_ctx
, struct ldb_message_element
, 2);
643 status
= NT_STATUS_NO_MEMORY
;
647 vals
= talloc_array(tmp_ctx
, struct ldb_val
, 2);
649 status
= NT_STATUS_NO_MEMORY
;
653 hwm_msg
->num_elements
= 2;
654 hwm_msg
->elements
= els
;
656 els
[0].num_values
= 1;
657 els
[0].values
= &vals
[0];
658 els
[0].flags
= LDB_FLAG_MOD_DELETE
;
659 els
[0].name
= talloc_strdup(tmp_ctx
, "xidNumber");
660 if (els
[0].name
== NULL
) {
661 status
= NT_STATUS_NO_MEMORY
;
665 els
[1].num_values
= 1;
666 els
[1].values
= &vals
[1];
667 els
[1].flags
= LDB_FLAG_MOD_ADD
;
668 els
[1].name
= els
[0].name
;
670 vals
[0].data
= (uint8_t *)unixid_string
;
671 vals
[0].length
= strlen(unixid_string
);
672 vals
[1].data
= (uint8_t *)hwm_string
;
673 vals
[1].length
= strlen(hwm_string
);
675 ret
= ldb_msg_append_string(hwm_msg
, "xidNumber", hwm_string
,
677 if (ret
!= LDB_SUCCESS
)
679 status
= NT_STATUS_NONE_MAPPED
;
684 ret
= ldb_modify(ldb
, hwm_msg
);
685 if (ret
!= LDB_SUCCESS
) {
686 DEBUG(1, ("Updating the xid high water mark failed: %s\n",
687 ldb_errstring(ldb
)));
688 status
= NT_STATUS_NONE_MAPPED
;
692 map_msg
= ldb_msg_new(tmp_ctx
);
693 if (map_msg
== NULL
) {
694 status
= NT_STATUS_NO_MEMORY
;
698 map_msg
->dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s", sid_string
.buf
);
699 if (map_msg
->dn
== NULL
) {
700 status
= NT_STATUS_NO_MEMORY
;
704 ret
= ldb_msg_add_string(map_msg
, "xidNumber", unixid_string
);
705 if (ret
!= LDB_SUCCESS
) {
706 status
= NT_STATUS_NONE_MAPPED
;
710 ret
= idmap_msg_add_dom_sid(idmap_ctx
, tmp_ctx
, map_msg
, "objectSid",
712 if (ret
!= LDB_SUCCESS
) {
713 status
= NT_STATUS_NONE_MAPPED
;
717 ret
= ldb_msg_add_string(map_msg
, "objectClass", "sidMap");
718 if (ret
!= LDB_SUCCESS
) {
719 status
= NT_STATUS_NONE_MAPPED
;
723 ret
= ldb_msg_add_string(map_msg
, "type", "ID_TYPE_BOTH");
724 if (ret
!= LDB_SUCCESS
) {
725 status
= NT_STATUS_NONE_MAPPED
;
729 ret
= ldb_msg_add_string(map_msg
, "cn", sid_string
.buf
);
730 if (ret
!= LDB_SUCCESS
) {
731 status
= NT_STATUS_NONE_MAPPED
;
735 ret
= ldb_add(ldb
, map_msg
);
736 if (ret
!= LDB_SUCCESS
) {
737 DEBUG(1, ("Adding a sidmap failed: %s\n", ldb_errstring(ldb
)));
738 status
= NT_STATUS_NONE_MAPPED
;
742 trans
= ldb_transaction_commit(ldb
);
743 if (trans
!= LDB_SUCCESS
) {
744 DEBUG(1, ("Transaction failed: %s\n", ldb_errstring(ldb
)));
745 status
= NT_STATUS_NONE_MAPPED
;
749 unixid
->id
= new_xid
;
750 unixid
->type
= ID_TYPE_BOTH
;
751 talloc_free(tmp_ctx
);
755 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(ldb
);
756 talloc_free(tmp_ctx
);
761 * Convert an array of unixids to the corresponding array of SIDs
763 * \param idmap_ctx idmap context to use
764 * \param mem_ctx talloc context the memory for the dom_sids is allocated
766 * \param count length of id_mapping array.
767 * \param id array of id_mappings.
768 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping is not
769 * possible at all, NT_STATUS_SOME_UNMAPPED if some mappings worked and some
773 NTSTATUS
idmap_xids_to_sids(struct idmap_context
*idmap_ctx
,
777 unsigned int i
, error_count
= 0;
780 for (i
= 0; id
&& id
[i
]; i
++) {
781 status
= idmap_xid_to_sid(idmap_ctx
, mem_ctx
,
782 &id
[i
]->xid
, &id
[i
]->sid
);
783 if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
784 status
= idmap_xid_to_sid(idmap_ctx
, mem_ctx
,
788 if (!NT_STATUS_IS_OK(status
)) {
789 DEBUG(1, ("idmapping xid_to_sid failed for id[%d]=%lu: %s\n",
790 i
, (unsigned long)id
[i
]->xid
.id
, nt_errstr(status
)));
792 id
[i
]->status
= ID_UNMAPPED
;
794 id
[i
]->status
= ID_MAPPED
;
798 if (error_count
== i
) {
799 /* Mapping did not work at all. */
800 return NT_STATUS_NONE_MAPPED
;
801 } else if (error_count
> 0) {
802 /* Some mappings worked, some did not. */
803 return STATUS_SOME_UNMAPPED
;
810 * Convert an array of SIDs to the corresponding array of unixids
812 * \param idmap_ctx idmap context to use
813 * \param mem_ctx talloc context the memory for the unixids is allocated
815 * \param count length of id_mapping array.
816 * \param id array of id_mappings.
817 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping is not
818 * possible at all, NT_STATUS_SOME_UNMAPPED if some mappings worked and some
822 NTSTATUS
idmap_sids_to_xids(struct idmap_context
*idmap_ctx
,
826 unsigned int i
, error_count
= 0;
829 for (i
= 0; id
&& id
[i
]; i
++) {
830 status
= idmap_sid_to_xid(idmap_ctx
, mem_ctx
,
831 id
[i
]->sid
, &id
[i
]->xid
);
832 if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
833 status
= idmap_sid_to_xid(idmap_ctx
, mem_ctx
,
837 if (!NT_STATUS_IS_OK(status
)) {
838 struct dom_sid_buf buf
;
839 DEBUG(1, ("idmapping sid_to_xid failed for id[%d]=%s: %s\n",
841 dom_sid_str_buf(id
[i
]->sid
, &buf
),
844 id
[i
]->status
= ID_UNMAPPED
;
846 id
[i
]->status
= ID_MAPPED
;
850 if (error_count
== i
) {
851 /* Mapping did not work at all. */
852 return NT_STATUS_NONE_MAPPED
;
853 } else if (error_count
> 0) {
854 /* Some mappings worked, some did not. */
855 return STATUS_SOME_UNMAPPED
;