lib/replace: Add ucontext configure autoconf checks.
[Samba/gbeck.git] / source4 / winbind / idmap.c
blob3773c1de591bf79ffc1ac53e0ea7eaccc7118def
1 /*
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/>.
23 #include "includes.h"
24 #include "auth/auth.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26 #include <ldb.h>
27 #include "ldb_wrap.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"
35 /**
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,
44 uint32_t *high)
46 int ret = -1;
47 struct ldb_context *ldb = idmap_ctx->ldb_ctx;
48 struct ldb_dn *dn;
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) {
61 ret = -1;
62 goto failed;
65 lower_bound = ldb_msg_find_attr_as_uint(res->msgs[0], "lowerBound", -1);
66 if (lower_bound != (uint32_t) -1) {
67 ret = LDB_SUCCESS;
68 } else {
69 ret = -1;
70 goto failed;
73 upper_bound = ldb_msg_find_attr_as_uint(res->msgs[0], "upperBound", -1);
74 if (upper_bound != (uint32_t) -1) {
75 ret = LDB_SUCCESS;
76 } else {
77 ret = -1;
80 failed:
81 talloc_free(tmp_ctx);
82 *low = lower_bound;
83 *high = upper_bound;
84 return ret;
87 /**
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)
100 struct ldb_val val;
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)) {
107 return -1;
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)
124 struct dom_sid *sid;
125 const struct ldb_val *val;
126 enum ndr_err_code ndr_err;
128 val = ldb_msg_find_ldb_val(msg, attr_name);
129 if (val == NULL) {
130 return NULL;
133 sid = talloc(mem_ctx, struct dom_sid);
134 if (sid == NULL) {
135 return NULL;
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)) {
141 talloc_free(sid);
142 return NULL;
145 return sid;
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) {
164 return NULL;
167 idmap_ctx->lp_ctx = lp_ctx;
169 idmap_ctx->ldb_ctx = ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx,
170 "idmap.ldb",
171 system_session(lp_ctx),
172 NULL, 0);
173 if (idmap_ctx->ldb_ctx == NULL) {
174 return 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) {
179 return 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) {
184 return 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"));
190 return NULL;
193 return idmap_ctx;
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
201 * from.
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
204 * succeeds.
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,
210 TALLOC_CTX *mem_ctx,
211 const struct unixid *unixid,
212 struct dom_sid **sid)
214 int ret;
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);
221 const char *id_type;
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) {
232 case ID_TYPE_UID:
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),
236 LDB_SCOPE_SUBTREE,
237 sam_attrs, 0,
238 "(&(|(sAMaccountType=%u)(sAMaccountType=%u)(sAMaccountType=%u))"
239 "(uidNumber=%u)(objectSid=*))",
240 ATYPE_ACCOUNT, ATYPE_WORKSTATION_TRUST, ATYPE_INTERDOMAIN_TRUST, unixid->id);
241 } else {
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;
250 goto failed;
251 } else if (ret == LDB_SUCCESS) {
252 *sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
253 if (*sid == NULL) {
254 DEBUG(1, ("Search for uidNumber=%lu did not return an objectSid!\n",
255 (unsigned long)unixid->id));
256 status = NT_STATUS_NONE_MAPPED;
257 goto failed;
259 talloc_free(tmp_ctx);
260 return NT_STATUS_OK;
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;
265 goto failed;
268 id_type = "ID_TYPE_UID";
269 break;
270 case ID_TYPE_GID:
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),
274 LDB_SCOPE_SUBTREE,
275 sam_attrs, 0,
276 "(&(|(sAMaccountType=%u)(sAMaccountType=%u))(gidNumber=%u))",
277 ATYPE_SECURITY_GLOBAL_GROUP, ATYPE_SECURITY_LOCAL_GROUP,
278 unixid->id);
279 } else {
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;
287 goto failed;
288 } else if (ret == LDB_SUCCESS) {
289 *sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
290 if (*sid == NULL) {
291 DEBUG(1, ("Search for gidNumber=%lu did not return an objectSid!\n",
292 (unsigned long)unixid->id));
293 status = NT_STATUS_NONE_MAPPED;
294 goto failed;
296 talloc_free(tmp_ctx);
297 return NT_STATUS_OK;
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;
302 goto failed;
305 id_type = "ID_TYPE_GID";
306 break;
307 default:
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;
311 goto failed;
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;
320 goto failed;
323 if (res->count == 1) {
324 *sid = idmap_msg_get_dom_sid(mem_ctx, res->msgs[0],
325 "objectSid");
326 if (*sid == NULL) {
327 DEBUG(1, ("Failed to get sid from db: %u\n", ret));
328 status = NT_STATUS_NONE_MAPPED;
329 goto failed;
331 talloc_free(tmp_ctx);
332 return NT_STATUS_OK;
335 DEBUG(6, ("xid not found in idmap db, create S-1-22- SID.\n"));
337 /* For local users/groups , we just create a rid = uid/gid */
338 if (unixid->type == ID_TYPE_UID) {
339 unix_sid = dom_sid_parse_talloc(tmp_ctx, "S-1-22-1");
340 } else {
341 unix_sid = dom_sid_parse_talloc(tmp_ctx, "S-1-22-2");
343 if (unix_sid == NULL) {
344 status = NT_STATUS_NO_MEMORY;
345 goto failed;
348 new_sid = dom_sid_add_rid(mem_ctx, unix_sid, unixid->id);
349 if (new_sid == NULL) {
350 status = NT_STATUS_NO_MEMORY;
351 goto failed;
354 *sid = new_sid;
355 talloc_free(tmp_ctx);
356 return NT_STATUS_OK;
358 failed:
359 talloc_free(tmp_ctx);
360 return status;
365 * Map a SID to an unixid struct.
367 * If no mapping exists, a new mapping will be created.
369 * \param idmap_ctx idmap context to use
370 * \param mem_ctx talloc context to use
371 * \param sid SID to map to an unixid struct
372 * \param unixid pointer to a unixid struct
373 * \return NT_STATUS_OK on success, NT_STATUS_INVALID_SID if the sid is not from
374 * a trusted domain and idmap trusted only = true, NT_STATUS_NONE_MAPPED if the
375 * mapping failed.
377 static NTSTATUS idmap_sid_to_xid(struct idmap_context *idmap_ctx,
378 TALLOC_CTX *mem_ctx,
379 const struct dom_sid *sid,
380 struct unixid *unixid)
382 int ret;
383 NTSTATUS status = NT_STATUS_NONE_MAPPED;
384 struct ldb_context *ldb = idmap_ctx->ldb_ctx;
385 struct ldb_dn *dn;
386 struct ldb_message *hwm_msg, *map_msg, *sam_msg;
387 struct ldb_result *res = NULL;
388 int trans = -1;
389 uint32_t low, high, hwm, new_xid;
390 char *sid_string, *unixid_string, *hwm_string;
391 bool hwm_entry_exists;
392 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
393 const char *sam_attrs[] = {"uidNumber", "gidNumber", "samAccountType", NULL};
395 if (dom_sid_in_domain(idmap_ctx->unix_users_sid, sid)) {
396 uint32_t rid;
397 DEBUG(6, ("This is a local unix uid, just calculate that.\n"));
398 status = dom_sid_split_rid(tmp_ctx, sid, NULL, &rid);
399 if (!NT_STATUS_IS_OK(status)) {
400 talloc_free(tmp_ctx);
401 return status;
404 unixid->id = rid;
405 unixid->type = ID_TYPE_UID;
407 talloc_free(tmp_ctx);
408 return NT_STATUS_OK;
411 if (dom_sid_in_domain(idmap_ctx->unix_groups_sid, sid)) {
412 uint32_t rid;
413 DEBUG(6, ("This is a local unix gid, just calculate that.\n"));
414 status = dom_sid_split_rid(tmp_ctx, sid, NULL, &rid);
415 if (!NT_STATUS_IS_OK(status)) {
416 talloc_free(tmp_ctx);
417 return status;
420 unixid->id = rid;
421 unixid->type = ID_TYPE_GID;
423 talloc_free(tmp_ctx);
424 return NT_STATUS_OK;
428 * First check against our local DB, to see if this user has a
429 * mapping there. This means that the Samba4 AD DC behaves
430 * much like a winbindd member server running idmap_ad
433 if (lpcfg_parm_bool(idmap_ctx->lp_ctx, NULL, "idmap_ldb", "use rfc2307", false)) {
434 ret = dsdb_search_one(idmap_ctx->samdb, tmp_ctx, &sam_msg,
435 ldb_get_default_basedn(idmap_ctx->samdb),
436 LDB_SCOPE_SUBTREE, sam_attrs, 0,
437 "(&(objectSid=%s)"
438 "(|(sAMaccountType=%u)(sAMaccountType=%u)(sAMaccountType=%u)"
439 "(sAMaccountType=%u)(sAMaccountType=%u))"
440 "(|(uidNumber=*)(gidNumber=*)))",
441 dom_sid_string(tmp_ctx, sid),
442 ATYPE_ACCOUNT, ATYPE_WORKSTATION_TRUST, ATYPE_INTERDOMAIN_TRUST,
443 ATYPE_SECURITY_GLOBAL_GROUP, ATYPE_SECURITY_LOCAL_GROUP);
444 } else {
445 /* If we are not to use the rfc2307 attributes, we just emulate a non-match */
446 ret = LDB_ERR_NO_SUCH_OBJECT;
449 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
450 DEBUG(1, ("Search for objectSid=%s gave duplicate results, failing to map to a unix ID!\n",
451 dom_sid_string(tmp_ctx, sid)));
452 status = NT_STATUS_NONE_MAPPED;
453 goto failed;
454 } else if (ret == LDB_SUCCESS) {
455 uint32_t account_type = ldb_msg_find_attr_as_uint(sam_msg, "sAMaccountType", 0);
456 if ((account_type == ATYPE_ACCOUNT) || (account_type == ATYPE_WORKSTATION_TRUST ) || (account_type == ATYPE_INTERDOMAIN_TRUST )) {
457 const struct ldb_val *v = ldb_msg_find_ldb_val(sam_msg, "uidNumber");
458 if (v) {
459 unixid->type = ID_TYPE_UID;
460 unixid->id = ldb_msg_find_attr_as_uint(sam_msg, "uidNumber", -1);
461 talloc_free(tmp_ctx);
462 return NT_STATUS_OK;
465 } else if ((account_type == ATYPE_SECURITY_GLOBAL_GROUP) || (account_type == ATYPE_SECURITY_LOCAL_GROUP)) {
466 const struct ldb_val *v = ldb_msg_find_ldb_val(sam_msg, "gidNumber");
467 if (v) {
468 unixid->type = ID_TYPE_GID;
469 unixid->id = ldb_msg_find_attr_as_uint(sam_msg, "gidNumber", -1);
470 talloc_free(tmp_ctx);
471 return NT_STATUS_OK;
474 } else if (ret != LDB_ERR_NO_SUCH_OBJECT) {
475 DEBUG(1, ("Search for objectSid=%s gave '%s', failing to map to a SID!\n",
476 dom_sid_string(tmp_ctx, sid), ldb_errstring(idmap_ctx->samdb)));
478 status = NT_STATUS_NONE_MAPPED;
479 goto failed;
482 ret = ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
483 NULL, "(&(objectClass=sidMap)(objectSid=%s))",
484 ldap_encode_ndr_dom_sid(tmp_ctx, sid));
485 if (ret != LDB_SUCCESS) {
486 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb)));
487 talloc_free(tmp_ctx);
488 return NT_STATUS_NONE_MAPPED;
491 if (res->count == 1) {
492 const char *type = ldb_msg_find_attr_as_string(res->msgs[0],
493 "type", NULL);
494 new_xid = ldb_msg_find_attr_as_uint(res->msgs[0], "xidNumber",
495 -1);
496 if (new_xid == (uint32_t) -1) {
497 DEBUG(1, ("Invalid xid mapping.\n"));
498 talloc_free(tmp_ctx);
499 return NT_STATUS_NONE_MAPPED;
502 if (type == NULL) {
503 DEBUG(1, ("Invalid type for mapping entry.\n"));
504 talloc_free(tmp_ctx);
505 return NT_STATUS_NONE_MAPPED;
508 unixid->id = new_xid;
510 if (strcmp(type, "ID_TYPE_BOTH") == 0) {
511 unixid->type = ID_TYPE_BOTH;
512 } else if (strcmp(type, "ID_TYPE_UID") == 0) {
513 unixid->type = ID_TYPE_UID;
514 } else {
515 unixid->type = ID_TYPE_GID;
518 talloc_free(tmp_ctx);
519 return NT_STATUS_OK;
522 DEBUG(6, ("No existing mapping found, attempting to create one.\n"));
524 trans = ldb_transaction_start(ldb);
525 if (trans != LDB_SUCCESS) {
526 status = NT_STATUS_NONE_MAPPED;
527 goto failed;
530 /* Redo the search to make sure noone changed the mapping while we
531 * weren't looking */
532 ret = ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
533 NULL, "(&(objectClass=sidMap)(objectSid=%s))",
534 ldap_encode_ndr_dom_sid(tmp_ctx, sid));
535 if (ret != LDB_SUCCESS) {
536 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb)));
537 status = NT_STATUS_NONE_MAPPED;
538 goto failed;
541 if (res->count > 0) {
542 DEBUG(1, ("Database changed while trying to add a sidmap.\n"));
543 status = NT_STATUS_RETRY;
544 goto failed;
547 ret = idmap_get_bounds(idmap_ctx, &low, &high);
548 if (ret != LDB_SUCCESS) {
549 status = NT_STATUS_NONE_MAPPED;
550 goto failed;
553 dn = ldb_dn_new(tmp_ctx, ldb, "CN=CONFIG");
554 if (dn == NULL) {
555 status = NT_STATUS_NO_MEMORY;
556 goto failed;
559 ret = ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
560 if (ret != LDB_SUCCESS) {
561 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb)));
562 status = NT_STATUS_NONE_MAPPED;
563 goto failed;
566 if (res->count != 1) {
567 DEBUG(1, ("No CN=CONFIG record, idmap database is broken.\n"));
568 status = NT_STATUS_NONE_MAPPED;
569 goto failed;
572 hwm = ldb_msg_find_attr_as_uint(res->msgs[0], "xidNumber", -1);
573 if (hwm == (uint32_t)-1) {
574 hwm = low;
575 hwm_entry_exists = false;
576 } else {
577 hwm_entry_exists = true;
580 if (hwm > high) {
581 DEBUG(1, ("Out of xids to allocate.\n"));
582 status = NT_STATUS_NONE_MAPPED;
583 goto failed;
586 hwm_msg = ldb_msg_new(tmp_ctx);
587 if (hwm_msg == NULL) {
588 DEBUG(1, ("Out of memory when creating ldb_message\n"));
589 status = NT_STATUS_NO_MEMORY;
590 goto failed;
593 hwm_msg->dn = dn;
595 new_xid = hwm;
596 hwm++;
598 hwm_string = talloc_asprintf(tmp_ctx, "%u", hwm);
599 if (hwm_string == NULL) {
600 status = NT_STATUS_NO_MEMORY;
601 goto failed;
604 sid_string = dom_sid_string(tmp_ctx, sid);
605 if (sid_string == NULL) {
606 status = NT_STATUS_NO_MEMORY;
607 goto failed;
610 unixid_string = talloc_asprintf(tmp_ctx, "%u", new_xid);
611 if (unixid_string == NULL) {
612 status = NT_STATUS_NO_MEMORY;
613 goto failed;
616 if (hwm_entry_exists) {
617 struct ldb_message_element *els;
618 struct ldb_val *vals;
620 /* We're modifying the entry, not just adding a new one. */
621 els = talloc_array(tmp_ctx, struct ldb_message_element, 2);
622 if (els == NULL) {
623 status = NT_STATUS_NO_MEMORY;
624 goto failed;
627 vals = talloc_array(tmp_ctx, struct ldb_val, 2);
628 if (els == NULL) {
629 status = NT_STATUS_NO_MEMORY;
630 goto failed;
633 hwm_msg->num_elements = 2;
634 hwm_msg->elements = els;
636 els[0].num_values = 1;
637 els[0].values = &vals[0];
638 els[0].flags = LDB_FLAG_MOD_DELETE;
639 els[0].name = talloc_strdup(tmp_ctx, "xidNumber");
640 if (els[0].name == NULL) {
641 status = NT_STATUS_NO_MEMORY;
642 goto failed;
645 els[1].num_values = 1;
646 els[1].values = &vals[1];
647 els[1].flags = LDB_FLAG_MOD_ADD;
648 els[1].name = els[0].name;
650 vals[0].data = (uint8_t *)unixid_string;
651 vals[0].length = strlen(unixid_string);
652 vals[1].data = (uint8_t *)hwm_string;
653 vals[1].length = strlen(hwm_string);
654 } else {
655 ret = ldb_msg_add_empty(hwm_msg, "xidNumber", LDB_FLAG_MOD_ADD,
656 NULL);
657 if (ret != LDB_SUCCESS) {
658 status = NT_STATUS_NONE_MAPPED;
659 goto failed;
662 ret = ldb_msg_add_string(hwm_msg, "xidNumber", hwm_string);
663 if (ret != LDB_SUCCESS)
665 status = NT_STATUS_NONE_MAPPED;
666 goto failed;
670 ret = ldb_modify(ldb, hwm_msg);
671 if (ret != LDB_SUCCESS) {
672 DEBUG(1, ("Updating the xid high water mark failed: %s\n",
673 ldb_errstring(ldb)));
674 status = NT_STATUS_NONE_MAPPED;
675 goto failed;
678 map_msg = ldb_msg_new(tmp_ctx);
679 if (map_msg == NULL) {
680 status = NT_STATUS_NO_MEMORY;
681 goto failed;
684 map_msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s", sid_string);
685 if (map_msg->dn == NULL) {
686 status = NT_STATUS_NO_MEMORY;
687 goto failed;
690 ret = ldb_msg_add_string(map_msg, "xidNumber", unixid_string);
691 if (ret != LDB_SUCCESS) {
692 status = NT_STATUS_NONE_MAPPED;
693 goto failed;
696 ret = idmap_msg_add_dom_sid(idmap_ctx, tmp_ctx, map_msg, "objectSid",
697 sid);
698 if (ret != LDB_SUCCESS) {
699 status = NT_STATUS_NONE_MAPPED;
700 goto failed;
703 ret = ldb_msg_add_string(map_msg, "objectClass", "sidMap");
704 if (ret != LDB_SUCCESS) {
705 status = NT_STATUS_NONE_MAPPED;
706 goto failed;
709 ret = ldb_msg_add_string(map_msg, "type", "ID_TYPE_BOTH");
710 if (ret != LDB_SUCCESS) {
711 status = NT_STATUS_NONE_MAPPED;
712 goto failed;
715 ret = ldb_msg_add_string(map_msg, "cn", sid_string);
716 if (ret != LDB_SUCCESS) {
717 status = NT_STATUS_NONE_MAPPED;
718 goto failed;
721 ret = ldb_add(ldb, map_msg);
722 if (ret != LDB_SUCCESS) {
723 DEBUG(1, ("Adding a sidmap failed: %s\n", ldb_errstring(ldb)));
724 status = NT_STATUS_NONE_MAPPED;
725 goto failed;
728 trans = ldb_transaction_commit(ldb);
729 if (trans != LDB_SUCCESS) {
730 DEBUG(1, ("Transaction failed: %s\n", ldb_errstring(ldb)));
731 status = NT_STATUS_NONE_MAPPED;
732 goto failed;
735 unixid->id = new_xid;
736 unixid->type = ID_TYPE_BOTH;
737 talloc_free(tmp_ctx);
738 return NT_STATUS_OK;
740 failed:
741 if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
742 talloc_free(tmp_ctx);
743 return status;
747 * Convert an array of unixids to the corresponding array of SIDs
749 * \param idmap_ctx idmap context to use
750 * \param mem_ctx talloc context the memory for the dom_sids is allocated
751 * from.
752 * \param count length of id_mapping array.
753 * \param id array of id_mappings.
754 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping is not
755 * possible at all, NT_STATUS_SOME_UNMAPPED if some mappings worked and some
756 * did not.
759 NTSTATUS idmap_xids_to_sids(struct idmap_context *idmap_ctx,
760 TALLOC_CTX *mem_ctx,
761 struct id_map **id)
763 unsigned int i, error_count = 0;
764 NTSTATUS status;
766 for (i = 0; id && id[i]; i++) {
767 status = idmap_xid_to_sid(idmap_ctx, mem_ctx,
768 &id[i]->xid, &id[i]->sid);
769 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
770 status = idmap_xid_to_sid(idmap_ctx, mem_ctx,
771 &id[i]->xid,
772 &id[i]->sid);
774 if (!NT_STATUS_IS_OK(status)) {
775 DEBUG(1, ("idmapping xid_to_sid failed for id[%d]=%lu: %s\n",
776 i, (unsigned long)id[i]->xid.id, nt_errstr(status)));
777 error_count++;
778 id[i]->status = ID_UNMAPPED;
779 } else {
780 id[i]->status = ID_MAPPED;
784 if (error_count == i) {
785 /* Mapping did not work at all. */
786 return NT_STATUS_NONE_MAPPED;
787 } else if (error_count > 0) {
788 /* Some mappings worked, some did not. */
789 return STATUS_SOME_UNMAPPED;
790 } else {
791 return NT_STATUS_OK;
796 * Convert an array of SIDs to the corresponding array of unixids
798 * \param idmap_ctx idmap context to use
799 * \param mem_ctx talloc context the memory for the unixids is allocated
800 * from.
801 * \param count length of id_mapping array.
802 * \param id array of id_mappings.
803 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping is not
804 * possible at all, NT_STATUS_SOME_UNMAPPED if some mappings worked and some
805 * did not.
808 NTSTATUS idmap_sids_to_xids(struct idmap_context *idmap_ctx,
809 TALLOC_CTX *mem_ctx,
810 struct id_map **id)
812 unsigned int i, error_count = 0;
813 NTSTATUS status;
815 for (i = 0; id && id[i]; i++) {
816 status = idmap_sid_to_xid(idmap_ctx, mem_ctx,
817 id[i]->sid, &id[i]->xid);
818 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
819 status = idmap_sid_to_xid(idmap_ctx, mem_ctx,
820 id[i]->sid,
821 &id[i]->xid);
823 if (!NT_STATUS_IS_OK(status)) {
824 char *str = dom_sid_string(mem_ctx, id[i]->sid);
825 DEBUG(1, ("idmapping sid_to_xid failed for id[%d]=%s: %s\n",
826 i, str, nt_errstr(status)));
827 talloc_free(str);
828 error_count++;
829 id[i]->status = ID_UNMAPPED;
830 } else {
831 id[i]->status = ID_MAPPED;
835 if (error_count == i) {
836 /* Mapping did not work at all. */
837 return NT_STATUS_NONE_MAPPED;
838 } else if (error_count > 0) {
839 /* Some mappings worked, some did not. */
840 return STATUS_SOME_UNMAPPED;
841 } else {
842 return NT_STATUS_OK;