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"
28 #include "param/param.h"
29 #include "winbind/idmap.h"
30 #include "libcli/security/security.h"
31 #include "libcli/ldap/ldap_ndr.h"
32 #include "dsdb/samdb/samdb.h"
33 #include "../libds/common/flags.h"
36 * Get uid/gid bounds from idmap database
38 * \param idmap_ctx idmap context to use
39 * \param low lower uid/gid bound is stored here
40 * \param high upper uid/gid bound is stored here
41 * \return 0 on success, nonzero on failure
43 static int idmap_get_bounds(struct idmap_context
*idmap_ctx
, uint32_t *low
,
47 struct ldb_context
*ldb
= idmap_ctx
->ldb_ctx
;
49 struct ldb_result
*res
= NULL
;
50 TALLOC_CTX
*tmp_ctx
= talloc_new(idmap_ctx
);
51 uint32_t lower_bound
= (uint32_t) -1;
52 uint32_t upper_bound
= (uint32_t) -1;
54 dn
= ldb_dn_new(tmp_ctx
, ldb
, "CN=CONFIG");
55 if (dn
== NULL
) goto failed
;
57 ret
= ldb_search(ldb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
);
58 if (ret
!= LDB_SUCCESS
) goto failed
;
60 if (res
->count
!= 1) {
65 lower_bound
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "lowerBound", -1);
66 if (lower_bound
!= (uint32_t) -1) {
73 upper_bound
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "upperBound", -1);
74 if (upper_bound
!= (uint32_t) -1) {
88 * Add a dom_sid structure to a ldb_message
89 * \param idmap_ctx idmap context to use
90 * \param mem_ctx talloc context to use
91 * \param ldb_message ldb message to add dom_sid to
92 * \param attr_name name of the attribute to store the dom_sid in
93 * \param sid dom_sid to store
94 * \return 0 on success, an ldb error code on failure.
96 static int idmap_msg_add_dom_sid(struct idmap_context
*idmap_ctx
,
97 TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
98 const char *attr_name
, const struct dom_sid
*sid
)
101 enum ndr_err_code ndr_err
;
103 ndr_err
= ndr_push_struct_blob(&val
, mem_ctx
, sid
,
104 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
106 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
110 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
114 * Get a dom_sid structure from a ldb message.
116 * \param mem_ctx talloc context to allocate dom_sid memory in
117 * \param msg ldb_message to get dom_sid from
118 * \param attr_name key that has the dom_sid as data
119 * \return dom_sid structure on success, NULL on failure
121 static struct dom_sid
*idmap_msg_get_dom_sid(TALLOC_CTX
*mem_ctx
,
122 struct ldb_message
*msg
, const char *attr_name
)
125 const struct ldb_val
*val
;
126 enum ndr_err_code ndr_err
;
128 val
= ldb_msg_find_ldb_val(msg
, attr_name
);
133 sid
= talloc(mem_ctx
, struct dom_sid
);
138 ndr_err
= ndr_pull_struct_blob(val
, sid
, sid
,
139 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
140 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
149 * Initialize idmap context
151 * talloc_free to close.
153 * \param mem_ctx talloc context to use.
154 * \return allocated idmap_context on success, NULL on error
156 struct idmap_context
*idmap_init(TALLOC_CTX
*mem_ctx
,
157 struct tevent_context
*ev_ctx
,
158 struct loadparm_context
*lp_ctx
)
160 struct idmap_context
*idmap_ctx
;
162 idmap_ctx
= talloc(mem_ctx
, struct idmap_context
);
163 if (idmap_ctx
== NULL
) {
167 idmap_ctx
->lp_ctx
= lp_ctx
;
169 idmap_ctx
->ldb_ctx
= ldb_wrap_connect(mem_ctx
, ev_ctx
, lp_ctx
,
171 system_session(lp_ctx
),
173 if (idmap_ctx
->ldb_ctx
== NULL
) {
177 idmap_ctx
->unix_groups_sid
= dom_sid_parse_talloc(mem_ctx
, "S-1-22-2");
178 if (idmap_ctx
->unix_groups_sid
== NULL
) {
182 idmap_ctx
->unix_users_sid
= dom_sid_parse_talloc(mem_ctx
, "S-1-22-1");
183 if (idmap_ctx
->unix_users_sid
== NULL
) {
187 idmap_ctx
->samdb
= samdb_connect(idmap_ctx
, ev_ctx
, lp_ctx
, system_session(lp_ctx
), 0);
188 if (idmap_ctx
->samdb
== NULL
) {
189 DEBUG(0, ("Failed to load sam.ldb in idmap_init\n"));
197 * Convert an unixid to the corresponding SID
199 * \param idmap_ctx idmap context to use
200 * \param mem_ctx talloc context the memory for the struct dom_sid is allocated
202 * \param unixid pointer to a unixid struct to convert
203 * \param sid pointer that will take the struct dom_sid pointer if the mapping
205 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping not
206 * possible or some other NTSTATUS that is more descriptive on failure.
209 static NTSTATUS
idmap_xid_to_sid(struct idmap_context
*idmap_ctx
,
211 const struct unixid
*unixid
,
212 struct dom_sid
**sid
)
215 NTSTATUS status
= NT_STATUS_NONE_MAPPED
;
216 struct ldb_context
*ldb
= idmap_ctx
->ldb_ctx
;
217 struct ldb_result
*res
= NULL
;
218 struct ldb_message
*msg
;
219 struct dom_sid
*unix_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 "(|(objectClass=posixAccount)(objectClass=posixGroup)))",
241 ATYPE_ACCOUNT
, ATYPE_WORKSTATION_TRUST
, ATYPE_INTERDOMAIN_TRUST
, unixid
->id
);
243 /* If we are not to use the rfc2307 attributes, we just emulate a non-match */
244 ret
= LDB_ERR_NO_SUCH_OBJECT
;
247 if (ret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
248 DEBUG(1, ("Search for uidNumber=%lu gave duplicate results, failing to map to a SID!\n",
249 (unsigned long)unixid
->id
));
250 status
= NT_STATUS_NONE_MAPPED
;
252 } else if (ret
== LDB_SUCCESS
) {
253 *sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
255 DEBUG(1, ("Search for uidNumber=%lu did not return an objectSid!\n",
256 (unsigned long)unixid
->id
));
257 status
= NT_STATUS_NONE_MAPPED
;
260 talloc_free(tmp_ctx
);
262 } else if (ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
263 DEBUG(1, ("Search for uidNumber=%lu gave '%s', failing to map to a SID!\n",
264 (unsigned long)unixid
->id
, ldb_errstring(idmap_ctx
->samdb
)));
265 status
= NT_STATUS_NONE_MAPPED
;
269 id_type
= "ID_TYPE_UID";
272 if (lpcfg_parm_bool(idmap_ctx
->lp_ctx
, NULL
, "idmap_ldb", "use rfc2307", false)) {
273 ret
= dsdb_search_one(idmap_ctx
->samdb
, tmp_ctx
, &msg
,
274 ldb_get_default_basedn(idmap_ctx
->samdb
),
277 "(&(|(sAMaccountType=%u)(sAMaccountType=%u))(gidNumber=%u)"
278 "(|(objectClass=posixAccount)(objectClass=posixGroup)))",
279 ATYPE_SECURITY_GLOBAL_GROUP
, ATYPE_SECURITY_LOCAL_GROUP
,
282 /* If we are not to use the rfc2307 attributes, we just emulate a non-match */
283 ret
= LDB_ERR_NO_SUCH_OBJECT
;
285 if (ret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
286 DEBUG(1, ("Search for gidNumber=%lu gave duplicate results, failing to map to a SID!\n",
287 (unsigned long)unixid
->id
));
288 status
= NT_STATUS_NONE_MAPPED
;
290 } else if (ret
== LDB_SUCCESS
) {
291 *sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
293 DEBUG(1, ("Search for gidNumber=%lu did not return an objectSid!\n",
294 (unsigned long)unixid
->id
));
295 status
= NT_STATUS_NONE_MAPPED
;
298 talloc_free(tmp_ctx
);
300 } else if (ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
301 DEBUG(1, ("Search for gidNumber=%lu gave '%s', failing to map to a SID!\n",
302 (unsigned long)unixid
->id
, ldb_errstring(idmap_ctx
->samdb
)));
303 status
= NT_STATUS_NONE_MAPPED
;
307 id_type
= "ID_TYPE_GID";
310 DEBUG(1, ("unixid->type must be type gid or uid (got %u) for lookup with id %lu\n",
311 (unsigned)unixid
->type
, (unsigned long)unixid
->id
));
312 status
= NT_STATUS_NONE_MAPPED
;
316 ret
= ldb_search(ldb
, tmp_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
,
317 NULL
, "(&(|(type=ID_TYPE_BOTH)(type=%s))"
318 "(xidNumber=%u))", id_type
, unixid
->id
);
319 if (ret
!= LDB_SUCCESS
) {
320 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb
)));
321 status
= NT_STATUS_NONE_MAPPED
;
325 if (res
->count
== 1) {
326 *sid
= idmap_msg_get_dom_sid(mem_ctx
, res
->msgs
[0],
329 DEBUG(1, ("Failed to get sid from db: %u\n", ret
));
330 status
= NT_STATUS_NONE_MAPPED
;
333 talloc_free(tmp_ctx
);
337 DEBUG(6, ("xid not found in idmap db, create S-1-22- SID.\n"));
339 /* For local users/groups , we just create a rid = uid/gid */
340 if (unixid
->type
== ID_TYPE_UID
) {
341 unix_sid
= dom_sid_parse_talloc(tmp_ctx
, "S-1-22-1");
343 unix_sid
= dom_sid_parse_talloc(tmp_ctx
, "S-1-22-2");
345 if (unix_sid
== NULL
) {
346 status
= NT_STATUS_NO_MEMORY
;
350 new_sid
= dom_sid_add_rid(mem_ctx
, unix_sid
, unixid
->id
);
351 if (new_sid
== NULL
) {
352 status
= NT_STATUS_NO_MEMORY
;
357 talloc_free(tmp_ctx
);
361 talloc_free(tmp_ctx
);
367 * Map a SID to an unixid struct.
369 * If no mapping exists, a new mapping will be created.
371 * \param idmap_ctx idmap context to use
372 * \param mem_ctx talloc context to use
373 * \param sid SID to map to an unixid struct
374 * \param unixid pointer to a unixid struct
375 * \return NT_STATUS_OK on success, NT_STATUS_INVALID_SID if the sid is not from
376 * a trusted domain and idmap trusted only = true, NT_STATUS_NONE_MAPPED if the
379 static NTSTATUS
idmap_sid_to_xid(struct idmap_context
*idmap_ctx
,
381 const struct dom_sid
*sid
,
382 struct unixid
*unixid
)
385 NTSTATUS status
= NT_STATUS_NONE_MAPPED
;
386 struct ldb_context
*ldb
= idmap_ctx
->ldb_ctx
;
388 struct ldb_message
*hwm_msg
, *map_msg
, *sam_msg
;
389 struct ldb_result
*res
= NULL
;
391 uint32_t low
, high
, hwm
, new_xid
;
392 char *sid_string
, *unixid_string
, *hwm_string
;
393 bool hwm_entry_exists
;
394 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
395 const char *sam_attrs
[] = {"uidNumber", "gidNumber", "samAccountType", NULL
};
397 if (dom_sid_in_domain(idmap_ctx
->unix_users_sid
, sid
)) {
399 DEBUG(6, ("This is a local unix uid, just calculate that.\n"));
400 status
= dom_sid_split_rid(tmp_ctx
, sid
, NULL
, &rid
);
401 if (!NT_STATUS_IS_OK(status
)) {
402 talloc_free(tmp_ctx
);
407 unixid
->type
= ID_TYPE_UID
;
409 talloc_free(tmp_ctx
);
413 if (dom_sid_in_domain(idmap_ctx
->unix_groups_sid
, sid
)) {
415 DEBUG(6, ("This is a local unix gid, just calculate that.\n"));
416 status
= dom_sid_split_rid(tmp_ctx
, sid
, NULL
, &rid
);
417 if (!NT_STATUS_IS_OK(status
)) {
418 talloc_free(tmp_ctx
);
423 unixid
->type
= ID_TYPE_GID
;
425 talloc_free(tmp_ctx
);
430 * First check against our local DB, to see if this user has a
431 * mapping there. This means that the Samba4 AD DC behaves
432 * much like a winbindd member server running idmap_ad
435 if (lpcfg_parm_bool(idmap_ctx
->lp_ctx
, NULL
, "idmap_ldb", "use rfc2307", false)) {
436 ret
= dsdb_search_one(idmap_ctx
->samdb
, tmp_ctx
, &sam_msg
,
437 ldb_get_default_basedn(idmap_ctx
->samdb
),
438 LDB_SCOPE_SUBTREE
, sam_attrs
, 0,
440 "(|(sAMaccountType=%u)(sAMaccountType=%u)(sAMaccountType=%u)"
441 "(sAMaccountType=%u)(sAMaccountType=%u))"
442 "(|(uidNumber=*)(gidNumber=*))"
443 "(|(objectClass=posixAccount)(objectClass=posixGroup)))",
444 dom_sid_string(tmp_ctx
, sid
),
445 ATYPE_ACCOUNT
, ATYPE_WORKSTATION_TRUST
, ATYPE_INTERDOMAIN_TRUST
,
446 ATYPE_SECURITY_GLOBAL_GROUP
, ATYPE_SECURITY_LOCAL_GROUP
);
448 /* If we are not to use the rfc2307 attributes, we just emulate a non-match */
449 ret
= LDB_ERR_NO_SUCH_OBJECT
;
452 if (ret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
453 DEBUG(1, ("Search for objectSid=%s gave duplicate results, failing to map to a unix ID!\n",
454 dom_sid_string(tmp_ctx
, sid
)));
455 status
= NT_STATUS_NONE_MAPPED
;
457 } else if (ret
== LDB_SUCCESS
) {
458 uint32_t account_type
= ldb_msg_find_attr_as_uint(sam_msg
, "sAMaccountType", 0);
459 if ((account_type
== ATYPE_ACCOUNT
) || (account_type
== ATYPE_WORKSTATION_TRUST
) || (account_type
== ATYPE_INTERDOMAIN_TRUST
)) {
460 const struct ldb_val
*v
= ldb_msg_find_ldb_val(sam_msg
, "uidNumber");
462 unixid
->type
= ID_TYPE_UID
;
463 unixid
->id
= ldb_msg_find_attr_as_uint(sam_msg
, "uidNumber", -1);
464 talloc_free(tmp_ctx
);
468 } else if ((account_type
== ATYPE_SECURITY_GLOBAL_GROUP
) || (account_type
== ATYPE_SECURITY_LOCAL_GROUP
)) {
469 const struct ldb_val
*v
= ldb_msg_find_ldb_val(sam_msg
, "gidNumber");
471 unixid
->type
= ID_TYPE_GID
;
472 unixid
->id
= ldb_msg_find_attr_as_uint(sam_msg
, "gidNumber", -1);
473 talloc_free(tmp_ctx
);
477 } else if (ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
478 DEBUG(1, ("Search for objectSid=%s gave '%s', failing to map to a SID!\n",
479 dom_sid_string(tmp_ctx
, sid
), ldb_errstring(idmap_ctx
->samdb
)));
481 status
= NT_STATUS_NONE_MAPPED
;
485 ret
= ldb_search(ldb
, tmp_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
,
486 NULL
, "(&(objectClass=sidMap)(objectSid=%s))",
487 ldap_encode_ndr_dom_sid(tmp_ctx
, sid
));
488 if (ret
!= LDB_SUCCESS
) {
489 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb
)));
490 talloc_free(tmp_ctx
);
491 return NT_STATUS_NONE_MAPPED
;
494 if (res
->count
== 1) {
495 const char *type
= ldb_msg_find_attr_as_string(res
->msgs
[0],
497 new_xid
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "xidNumber",
499 if (new_xid
== (uint32_t) -1) {
500 DEBUG(1, ("Invalid xid mapping.\n"));
501 talloc_free(tmp_ctx
);
502 return NT_STATUS_NONE_MAPPED
;
506 DEBUG(1, ("Invalid type for mapping entry.\n"));
507 talloc_free(tmp_ctx
);
508 return NT_STATUS_NONE_MAPPED
;
511 unixid
->id
= new_xid
;
513 if (strcmp(type
, "ID_TYPE_BOTH") == 0) {
514 unixid
->type
= ID_TYPE_BOTH
;
515 } else if (strcmp(type
, "ID_TYPE_UID") == 0) {
516 unixid
->type
= ID_TYPE_UID
;
518 unixid
->type
= ID_TYPE_GID
;
521 talloc_free(tmp_ctx
);
525 DEBUG(6, ("No existing mapping found, attempting to create one.\n"));
527 trans
= ldb_transaction_start(ldb
);
528 if (trans
!= LDB_SUCCESS
) {
529 status
= NT_STATUS_NONE_MAPPED
;
533 /* Redo the search to make sure noone changed the mapping while we
535 ret
= ldb_search(ldb
, tmp_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
,
536 NULL
, "(&(objectClass=sidMap)(objectSid=%s))",
537 ldap_encode_ndr_dom_sid(tmp_ctx
, sid
));
538 if (ret
!= LDB_SUCCESS
) {
539 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb
)));
540 status
= NT_STATUS_NONE_MAPPED
;
544 if (res
->count
> 0) {
545 DEBUG(1, ("Database changed while trying to add a sidmap.\n"));
546 status
= NT_STATUS_RETRY
;
550 ret
= idmap_get_bounds(idmap_ctx
, &low
, &high
);
551 if (ret
!= LDB_SUCCESS
) {
552 status
= NT_STATUS_NONE_MAPPED
;
556 dn
= ldb_dn_new(tmp_ctx
, ldb
, "CN=CONFIG");
558 status
= NT_STATUS_NO_MEMORY
;
562 ret
= ldb_search(ldb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
);
563 if (ret
!= LDB_SUCCESS
) {
564 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb
)));
565 status
= NT_STATUS_NONE_MAPPED
;
569 if (res
->count
!= 1) {
570 DEBUG(1, ("No CN=CONFIG record, idmap database is broken.\n"));
571 status
= NT_STATUS_NONE_MAPPED
;
575 hwm
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "xidNumber", -1);
576 if (hwm
== (uint32_t)-1) {
578 hwm_entry_exists
= false;
580 hwm_entry_exists
= true;
584 DEBUG(1, ("Out of xids to allocate.\n"));
585 status
= NT_STATUS_NONE_MAPPED
;
589 hwm_msg
= ldb_msg_new(tmp_ctx
);
590 if (hwm_msg
== NULL
) {
591 DEBUG(1, ("Out of memory when creating ldb_message\n"));
592 status
= NT_STATUS_NO_MEMORY
;
601 hwm_string
= talloc_asprintf(tmp_ctx
, "%u", hwm
);
602 if (hwm_string
== NULL
) {
603 status
= NT_STATUS_NO_MEMORY
;
607 sid_string
= dom_sid_string(tmp_ctx
, sid
);
608 if (sid_string
== NULL
) {
609 status
= NT_STATUS_NO_MEMORY
;
613 unixid_string
= talloc_asprintf(tmp_ctx
, "%u", new_xid
);
614 if (unixid_string
== NULL
) {
615 status
= NT_STATUS_NO_MEMORY
;
619 if (hwm_entry_exists
) {
620 struct ldb_message_element
*els
;
621 struct ldb_val
*vals
;
623 /* We're modifying the entry, not just adding a new one. */
624 els
= talloc_array(tmp_ctx
, struct ldb_message_element
, 2);
626 status
= NT_STATUS_NO_MEMORY
;
630 vals
= talloc_array(tmp_ctx
, struct ldb_val
, 2);
632 status
= NT_STATUS_NO_MEMORY
;
636 hwm_msg
->num_elements
= 2;
637 hwm_msg
->elements
= els
;
639 els
[0].num_values
= 1;
640 els
[0].values
= &vals
[0];
641 els
[0].flags
= LDB_FLAG_MOD_DELETE
;
642 els
[0].name
= talloc_strdup(tmp_ctx
, "xidNumber");
643 if (els
[0].name
== NULL
) {
644 status
= NT_STATUS_NO_MEMORY
;
648 els
[1].num_values
= 1;
649 els
[1].values
= &vals
[1];
650 els
[1].flags
= LDB_FLAG_MOD_ADD
;
651 els
[1].name
= els
[0].name
;
653 vals
[0].data
= (uint8_t *)unixid_string
;
654 vals
[0].length
= strlen(unixid_string
);
655 vals
[1].data
= (uint8_t *)hwm_string
;
656 vals
[1].length
= strlen(hwm_string
);
658 ret
= ldb_msg_add_empty(hwm_msg
, "xidNumber", LDB_FLAG_MOD_ADD
,
660 if (ret
!= LDB_SUCCESS
) {
661 status
= NT_STATUS_NONE_MAPPED
;
665 ret
= ldb_msg_add_string(hwm_msg
, "xidNumber", hwm_string
);
666 if (ret
!= LDB_SUCCESS
)
668 status
= NT_STATUS_NONE_MAPPED
;
673 ret
= ldb_modify(ldb
, hwm_msg
);
674 if (ret
!= LDB_SUCCESS
) {
675 DEBUG(1, ("Updating the xid high water mark failed: %s\n",
676 ldb_errstring(ldb
)));
677 status
= NT_STATUS_NONE_MAPPED
;
681 map_msg
= ldb_msg_new(tmp_ctx
);
682 if (map_msg
== NULL
) {
683 status
= NT_STATUS_NO_MEMORY
;
687 map_msg
->dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s", sid_string
);
688 if (map_msg
->dn
== NULL
) {
689 status
= NT_STATUS_NO_MEMORY
;
693 ret
= ldb_msg_add_string(map_msg
, "xidNumber", unixid_string
);
694 if (ret
!= LDB_SUCCESS
) {
695 status
= NT_STATUS_NONE_MAPPED
;
699 ret
= idmap_msg_add_dom_sid(idmap_ctx
, tmp_ctx
, map_msg
, "objectSid",
701 if (ret
!= LDB_SUCCESS
) {
702 status
= NT_STATUS_NONE_MAPPED
;
706 ret
= ldb_msg_add_string(map_msg
, "objectClass", "sidMap");
707 if (ret
!= LDB_SUCCESS
) {
708 status
= NT_STATUS_NONE_MAPPED
;
712 ret
= ldb_msg_add_string(map_msg
, "type", "ID_TYPE_BOTH");
713 if (ret
!= LDB_SUCCESS
) {
714 status
= NT_STATUS_NONE_MAPPED
;
718 ret
= ldb_msg_add_string(map_msg
, "cn", sid_string
);
719 if (ret
!= LDB_SUCCESS
) {
720 status
= NT_STATUS_NONE_MAPPED
;
724 ret
= ldb_add(ldb
, map_msg
);
725 if (ret
!= LDB_SUCCESS
) {
726 DEBUG(1, ("Adding a sidmap failed: %s\n", ldb_errstring(ldb
)));
727 status
= NT_STATUS_NONE_MAPPED
;
731 trans
= ldb_transaction_commit(ldb
);
732 if (trans
!= LDB_SUCCESS
) {
733 DEBUG(1, ("Transaction failed: %s\n", ldb_errstring(ldb
)));
734 status
= NT_STATUS_NONE_MAPPED
;
738 unixid
->id
= new_xid
;
739 unixid
->type
= ID_TYPE_BOTH
;
740 talloc_free(tmp_ctx
);
744 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(ldb
);
745 talloc_free(tmp_ctx
);
750 * Convert an array of unixids to the corresponding array of SIDs
752 * \param idmap_ctx idmap context to use
753 * \param mem_ctx talloc context the memory for the dom_sids is allocated
755 * \param count length of id_mapping array.
756 * \param id array of id_mappings.
757 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping is not
758 * possible at all, NT_STATUS_SOME_UNMAPPED if some mappings worked and some
762 NTSTATUS
idmap_xids_to_sids(struct idmap_context
*idmap_ctx
,
766 unsigned int i
, error_count
= 0;
769 for (i
= 0; id
&& id
[i
]; i
++) {
770 status
= idmap_xid_to_sid(idmap_ctx
, mem_ctx
,
771 &id
[i
]->xid
, &id
[i
]->sid
);
772 if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
773 status
= idmap_xid_to_sid(idmap_ctx
, mem_ctx
,
777 if (!NT_STATUS_IS_OK(status
)) {
778 DEBUG(1, ("idmapping xid_to_sid failed for id[%d]=%lu: %s\n",
779 i
, (unsigned long)id
[i
]->xid
.id
, nt_errstr(status
)));
781 id
[i
]->status
= ID_UNMAPPED
;
783 id
[i
]->status
= ID_MAPPED
;
787 if (error_count
== i
) {
788 /* Mapping did not work at all. */
789 return NT_STATUS_NONE_MAPPED
;
790 } else if (error_count
> 0) {
791 /* Some mappings worked, some did not. */
792 return STATUS_SOME_UNMAPPED
;
799 * Convert an array of SIDs to the corresponding array of unixids
801 * \param idmap_ctx idmap context to use
802 * \param mem_ctx talloc context the memory for the unixids is allocated
804 * \param count length of id_mapping array.
805 * \param id array of id_mappings.
806 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping is not
807 * possible at all, NT_STATUS_SOME_UNMAPPED if some mappings worked and some
811 NTSTATUS
idmap_sids_to_xids(struct idmap_context
*idmap_ctx
,
815 unsigned int i
, error_count
= 0;
818 for (i
= 0; id
&& id
[i
]; i
++) {
819 status
= idmap_sid_to_xid(idmap_ctx
, mem_ctx
,
820 id
[i
]->sid
, &id
[i
]->xid
);
821 if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
822 status
= idmap_sid_to_xid(idmap_ctx
, mem_ctx
,
826 if (!NT_STATUS_IS_OK(status
)) {
827 char *str
= dom_sid_string(mem_ctx
, id
[i
]->sid
);
828 DEBUG(1, ("idmapping sid_to_xid failed for id[%d]=%s: %s\n",
829 i
, str
, nt_errstr(status
)));
832 id
[i
]->status
= ID_UNMAPPED
;
834 id
[i
]->status
= ID_MAPPED
;
838 if (error_count
== i
) {
839 /* Mapping did not work at all. */
840 return NT_STATUS_NONE_MAPPED
;
841 } else if (error_count
> 0) {
842 /* Some mappings worked, some did not. */
843 return STATUS_SOME_UNMAPPED
;