2 Unix SMB/CIFS implementation.
4 Map SIDs to unixids and back
6 Copyright (C) Kai Blin 2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "auth/auth.h"
24 #include "librpc/gen_ndr/ndr_security.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb_wrap.h"
27 #include "param/param.h"
28 #include "winbind/idmap.h"
29 #include "libcli/security/security.h"
30 #include "libcli/ldap/ldap_ndr.h"
33 * Get uid/gid bounds from idmap database
35 * \param idmap_ctx idmap context to use
36 * \param low lower uid/gid bound is stored here
37 * \param high upper uid/gid bound is stored here
38 * \return 0 on success, nonzero on failure
40 static int idmap_get_bounds(struct idmap_context *idmap_ctx, uint32_t *low,
44 struct ldb_context *ldb = idmap_ctx->ldb_ctx;
46 struct ldb_result *res = NULL;
47 TALLOC_CTX *tmp_ctx = talloc_new(idmap_ctx);
48 uint32_t lower_bound = (uint32_t) -1;
49 uint32_t upper_bound = (uint32_t) -1;
51 dn = ldb_dn_new(tmp_ctx, ldb, "CN=CONFIG");
52 if (dn == NULL) goto failed;
54 ret = ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
55 if (ret != LDB_SUCCESS) goto failed;
57 if (res->count != 1) {
62 lower_bound = ldb_msg_find_attr_as_uint(res->msgs[0], "lowerBound", -1);
63 if (lower_bound != (uint32_t) -1) {
70 upper_bound = ldb_msg_find_attr_as_uint(res->msgs[0], "upperBound", -1);
71 if (upper_bound != (uint32_t) -1) {
85 * Add a dom_sid structure to a ldb_message
86 * \param idmap_ctx idmap context to use
87 * \param mem_ctx talloc context to use
88 * \param ldb_message ldb message to add dom_sid to
89 * \param attr_name name of the attribute to store the dom_sid in
90 * \param sid dom_sid to store
91 * \return 0 on success, an ldb error code on failure.
93 static int idmap_msg_add_dom_sid(struct idmap_context *idmap_ctx,
94 TALLOC_CTX *mem_ctx, struct ldb_message *msg,
95 const char *attr_name, const struct dom_sid *sid)
98 enum ndr_err_code ndr_err;
100 ndr_err = ndr_push_struct_blob(&val, mem_ctx,
101 lp_iconv_convenience(idmap_ctx->lp_ctx),
103 (ndr_push_flags_fn_t)ndr_push_dom_sid);
105 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
109 return ldb_msg_add_value(msg, attr_name, &val, NULL);
113 * Get a dom_sid structure from a ldb message.
115 * \param mem_ctx talloc context to allocate dom_sid memory in
116 * \param msg ldb_message to get dom_sid from
117 * \param attr_name key that has the dom_sid as data
118 * \return dom_sid structure on success, NULL on failure
120 static struct dom_sid *idmap_msg_get_dom_sid(TALLOC_CTX *mem_ctx,
121 struct ldb_message *msg, const char *attr_name)
124 const struct ldb_val *val;
125 enum ndr_err_code ndr_err;
127 val = ldb_msg_find_ldb_val(msg, attr_name);
132 sid = talloc(mem_ctx, struct dom_sid);
137 ndr_err = ndr_pull_struct_blob(val, sid, NULL, sid,
138 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
139 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
148 * Initialize idmap context
150 * talloc_free to close.
152 * \param mem_ctx talloc context to use.
153 * \return allocated idmap_context on success, NULL on error
155 struct idmap_context *idmap_init(TALLOC_CTX *mem_ctx,
156 struct tevent_context *ev_ctx,
157 struct loadparm_context *lp_ctx)
159 struct idmap_context *idmap_ctx;
161 idmap_ctx = talloc(mem_ctx, struct idmap_context);
162 if (idmap_ctx == NULL) {
166 idmap_ctx->lp_ctx = lp_ctx;
168 idmap_ctx->ldb_ctx = ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx,
169 lp_idmap_url(lp_ctx),
170 system_session(lp_ctx),
172 if (idmap_ctx->ldb_ctx == NULL) {
176 idmap_ctx->unix_groups_sid = dom_sid_parse_talloc(mem_ctx, "S-1-22-2");
177 if (idmap_ctx->unix_groups_sid == NULL) {
181 idmap_ctx->unix_users_sid = dom_sid_parse_talloc(mem_ctx, "S-1-22-1");
182 if (idmap_ctx->unix_users_sid == NULL) {
190 * Convert an unixid to the corresponding SID
192 * \param idmap_ctx idmap context to use
193 * \param mem_ctx talloc context the memory for the struct dom_sid is allocated
195 * \param unixid pointer to a unixid struct to convert
196 * \param sid pointer that will take the struct dom_sid pointer if the mapping
198 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping not
199 * possible or some other NTSTATUS that is more descriptive on failure.
202 NTSTATUS idmap_xid_to_sid(struct idmap_context *idmap_ctx, TALLOC_CTX *mem_ctx,
203 const struct unixid *unixid, struct dom_sid **sid)
206 NTSTATUS status = NT_STATUS_NONE_MAPPED;
207 struct ldb_context *ldb = idmap_ctx->ldb_ctx;
208 struct ldb_result *res = NULL;
209 struct dom_sid *unix_sid, *new_sid;
210 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
213 switch (unixid->type) {
215 id_type = "ID_TYPE_UID";
218 id_type = "ID_TYPE_GID";
221 DEBUG(1, ("unixid->type must be type gid or uid\n"));
222 status = NT_STATUS_NONE_MAPPED;
226 ret = ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
227 NULL, "(&(|(type=ID_TYPE_BOTH)(type=%s))"
228 "(xidNumber=%u))", id_type, unixid->id);
229 if (ret != LDB_SUCCESS) {
230 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb)));
231 status = NT_STATUS_NONE_MAPPED;
235 if (res->count == 1) {
236 *sid = idmap_msg_get_dom_sid(mem_ctx, res->msgs[0],
239 DEBUG(1, ("Failed to get sid from db: %u\n", ret));
240 status = NT_STATUS_NONE_MAPPED;
243 talloc_free(tmp_ctx);
247 DEBUG(6, ("xid not found in idmap db, create S-1-22- SID.\n"));
249 /* For local users/groups , we just create a rid = uid/gid */
250 if (unixid->type == ID_TYPE_UID) {
251 unix_sid = dom_sid_parse_talloc(tmp_ctx, "S-1-22-1");
253 unix_sid = dom_sid_parse_talloc(tmp_ctx, "S-1-22-2");
255 if (unix_sid == NULL) {
256 status = NT_STATUS_NO_MEMORY;
260 new_sid = dom_sid_add_rid(mem_ctx, unix_sid, unixid->id);
261 if (new_sid == NULL) {
262 status = NT_STATUS_NO_MEMORY;
267 talloc_free(tmp_ctx);
271 talloc_free(tmp_ctx);
277 * Map a SID to an unixid struct.
279 * If no mapping exists, a new mapping will be created.
281 * \todo Check if SIDs can be resolved if lp_idmap_trusted_only() == true
282 * \todo Fix backwards compatibility for Samba3
284 * \param idmap_ctx idmap context to use
285 * \param mem_ctx talloc context to use
286 * \param sid SID to map to an unixid struct
287 * \param unixid pointer to a unixid struct pointer
288 * \return NT_STATUS_OK on success, NT_STATUS_INVALID_SID if the sid is not from
289 * a trusted domain and idmap trusted only = true, NT_STATUS_NONE_MAPPED if the
292 NTSTATUS idmap_sid_to_xid(struct idmap_context *idmap_ctx, TALLOC_CTX *mem_ctx,
293 const struct dom_sid *sid, struct unixid **unixid)
296 NTSTATUS status = NT_STATUS_NONE_MAPPED;
297 struct ldb_context *ldb = idmap_ctx->ldb_ctx;
299 struct ldb_message *hwm_msg, *map_msg;
300 struct ldb_result *res = NULL;
302 uint32_t low, high, hwm, new_xid;
303 char *sid_string, *unixid_string, *hwm_string;
304 bool hwm_entry_exists;
305 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
307 if (dom_sid_in_domain(idmap_ctx->unix_users_sid, sid)) {
309 DEBUG(6, ("This is a local unix uid, just calculate that.\n"));
310 status = dom_sid_split_rid(tmp_ctx, sid, NULL, &rid);
311 if (!NT_STATUS_IS_OK(status)) goto failed;
313 *unixid = talloc(mem_ctx, struct unixid);
314 if (*unixid == NULL) {
315 status = NT_STATUS_NO_MEMORY;
319 (*unixid)->type = ID_TYPE_UID;
321 talloc_free(tmp_ctx);
325 if (dom_sid_in_domain(idmap_ctx->unix_groups_sid, sid)) {
327 DEBUG(6, ("This is a local unix gid, just calculate that.\n"));
328 status = dom_sid_split_rid(tmp_ctx, sid, NULL, &rid);
329 if (!NT_STATUS_IS_OK(status)) goto failed;
331 *unixid = talloc(mem_ctx, struct unixid);
332 if (*unixid == NULL) {
333 status = NT_STATUS_NO_MEMORY;
337 (*unixid)->type = ID_TYPE_GID;
339 talloc_free(tmp_ctx);
343 ret = ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
344 NULL, "(&(objectClass=sidMap)(objectSid=%s))",
345 ldap_encode_ndr_dom_sid(tmp_ctx, sid));
346 if (ret != LDB_SUCCESS) {
347 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb)));
348 status = NT_STATUS_NONE_MAPPED;
352 if (res->count == 1) {
353 const char *type = ldb_msg_find_attr_as_string(res->msgs[0],
355 new_xid = ldb_msg_find_attr_as_uint(res->msgs[0], "xidNumber",
357 if (new_xid == (uint32_t) -1) {
358 DEBUG(1, ("Invalid xid mapping.\n"));
359 status = NT_STATUS_NONE_MAPPED;
364 DEBUG(1, ("Invalid type for mapping entry.\n"));
365 status = NT_STATUS_NONE_MAPPED;
369 *unixid = talloc(mem_ctx, struct unixid);
370 if (*unixid == NULL) {
371 status = NT_STATUS_NO_MEMORY;
375 (*unixid)->id = new_xid;
377 if (strcmp(type, "ID_TYPE_BOTH") == 0) {
378 (*unixid)->type = ID_TYPE_BOTH;
379 } else if (strcmp(type, "ID_TYPE_UID") == 0) {
380 (*unixid)->type = ID_TYPE_UID;
382 (*unixid)->type = ID_TYPE_GID;
385 talloc_free(tmp_ctx);
389 DEBUG(6, ("No existing mapping found, attempting to create one.\n"));
391 trans = ldb_transaction_start(ldb);
392 if (trans != LDB_SUCCESS) {
393 status = NT_STATUS_NONE_MAPPED;
397 /* Redo the search to make sure noone changed the mapping while we
399 ret = ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
400 NULL, "(&(objectClass=sidMap)(objectSid=%s))",
401 ldap_encode_ndr_dom_sid(tmp_ctx, sid));
402 if (ret != LDB_SUCCESS) {
403 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb)));
404 status = NT_STATUS_NONE_MAPPED;
408 if (res->count > 0) {
409 DEBUG(1, ("Database changed while trying to add a sidmap.\n"));
410 status = NT_STATUS_RETRY;
414 /*FIXME: if lp_idmap_trusted_only() == true, check if SID can be
417 ret = idmap_get_bounds(idmap_ctx, &low, &high);
418 if (ret != LDB_SUCCESS) {
419 status = NT_STATUS_NONE_MAPPED;
423 dn = ldb_dn_new(tmp_ctx, ldb, "CN=CONFIG");
425 status = NT_STATUS_NO_MEMORY;
429 ret = ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
430 if (ret != LDB_SUCCESS) {
431 DEBUG(1, ("Search failed: %s\n", ldb_errstring(ldb)));
432 status = NT_STATUS_NONE_MAPPED;
436 if (res->count != 1) {
437 DEBUG(1, ("No CN=CONFIG record, idmap database is broken.\n"));
438 status = NT_STATUS_NONE_MAPPED;
442 hwm = ldb_msg_find_attr_as_uint(res->msgs[0], "xidNumber", -1);
443 if (hwm == (uint32_t)-1) {
445 hwm_entry_exists = false;
447 hwm_entry_exists = true;
451 DEBUG(1, ("Out of xids to allocate.\n"));
452 status = NT_STATUS_NONE_MAPPED;
456 hwm_msg = ldb_msg_new(tmp_ctx);
457 if (hwm_msg == NULL) {
458 DEBUG(1, ("Out of memory when creating ldb_message\n"));
459 status = NT_STATUS_NO_MEMORY;
468 hwm_string = talloc_asprintf(tmp_ctx, "%u", hwm);
469 if (hwm_string == NULL) {
470 status = NT_STATUS_NO_MEMORY;
474 sid_string = dom_sid_string(tmp_ctx, sid);
475 if (sid_string == NULL) {
476 status = NT_STATUS_NO_MEMORY;
480 unixid_string = talloc_asprintf(tmp_ctx, "%u", new_xid);
481 if (unixid_string == NULL) {
482 status = NT_STATUS_NO_MEMORY;
486 if (hwm_entry_exists) {
487 struct ldb_message_element *els;
488 struct ldb_val *vals;
490 /* We're modifying the entry, not just adding a new one. */
491 els = talloc_array(tmp_ctx, struct ldb_message_element, 2);
493 status = NT_STATUS_NO_MEMORY;
497 vals = talloc_array(tmp_ctx, struct ldb_val, 2);
499 status = NT_STATUS_NO_MEMORY;
503 hwm_msg->num_elements = 2;
504 hwm_msg->elements = els;
506 els[0].num_values = 1;
507 els[0].values = &vals[0];
508 els[0].flags = LDB_FLAG_MOD_DELETE;
509 els[0].name = talloc_strdup(tmp_ctx, "xidNumber");
510 if (els[0].name == NULL) {
511 status = NT_STATUS_NO_MEMORY;
515 els[1].num_values = 1;
516 els[1].values = &vals[1];
517 els[1].flags = LDB_FLAG_MOD_ADD;
518 els[1].name = els[0].name;
520 vals[0].data = (uint8_t *)unixid_string;
521 vals[0].length = strlen(unixid_string);
522 vals[1].data = (uint8_t *)hwm_string;
523 vals[1].length = strlen(hwm_string);
525 ret = ldb_msg_add_empty(hwm_msg, "xidNumber", LDB_FLAG_MOD_ADD,
527 if (ret != LDB_SUCCESS) {
528 status = NT_STATUS_NONE_MAPPED;
532 ret = ldb_msg_add_string(hwm_msg, "xidNumber", hwm_string);
533 if (ret != LDB_SUCCESS)
535 status = NT_STATUS_NONE_MAPPED;
540 ret = ldb_modify(ldb, hwm_msg);
541 if (ret != LDB_SUCCESS) {
542 DEBUG(1, ("Updating the xid high water mark failed: %s\n",
543 ldb_errstring(ldb)));
544 status = NT_STATUS_NONE_MAPPED;
548 map_msg = ldb_msg_new(tmp_ctx);
549 if (map_msg == NULL) {
550 status = NT_STATUS_NO_MEMORY;
554 map_msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s", sid_string);
555 if (map_msg->dn == NULL) {
556 status = NT_STATUS_NO_MEMORY;
560 ret = ldb_msg_add_string(map_msg, "xidNumber", unixid_string);
561 if (ret != LDB_SUCCESS) {
562 status = NT_STATUS_NONE_MAPPED;
566 ret = idmap_msg_add_dom_sid(idmap_ctx, tmp_ctx, map_msg, "objectSid",
568 if (ret != LDB_SUCCESS) {
569 status = NT_STATUS_NONE_MAPPED;
573 ret = ldb_msg_add_string(map_msg, "objectClass", "sidMap");
574 if (ret != LDB_SUCCESS) {
575 status = NT_STATUS_NONE_MAPPED;
579 ret = ldb_msg_add_string(map_msg, "type", "ID_TYPE_BOTH");
580 if (ret != LDB_SUCCESS) {
581 status = NT_STATUS_NONE_MAPPED;
585 ret = ldb_msg_add_string(map_msg, "cn", sid_string);
586 if (ret != LDB_SUCCESS) {
587 status = NT_STATUS_NONE_MAPPED;
591 ret = ldb_add(ldb, map_msg);
592 if (ret != LDB_SUCCESS) {
593 DEBUG(1, ("Adding a sidmap failed: %s\n", ldb_errstring(ldb)));
594 status = NT_STATUS_NONE_MAPPED;
598 trans = ldb_transaction_commit(ldb);
599 if (trans != LDB_SUCCESS) {
600 DEBUG(1, ("Transaction failed: %s\n", ldb_errstring(ldb)));
601 status = NT_STATUS_NONE_MAPPED;
605 *unixid = talloc(mem_ctx, struct unixid);
606 if (*unixid == NULL) {
607 status = NT_STATUS_NO_MEMORY;
611 (*unixid)->id = new_xid;
612 (*unixid)->type = ID_TYPE_BOTH;
613 talloc_free(tmp_ctx);
617 if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
618 talloc_free(tmp_ctx);
623 * Convert an array of unixids to the corresponding array of SIDs
625 * \param idmap_ctx idmap context to use
626 * \param mem_ctx talloc context the memory for the dom_sids is allocated
628 * \param count length of id_mapping array.
629 * \param id array of id_mappings.
630 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping is not
631 * possible at all, NT_STATUS_SOME_UNMAPPED if some mappings worked and some
635 NTSTATUS idmap_xids_to_sids(struct idmap_context *idmap_ctx,
636 TALLOC_CTX *mem_ctx, int count,
637 struct id_mapping *id)
642 for (i = 0; i < count; ++i) {
643 id[i].status = idmap_xid_to_sid(idmap_ctx, mem_ctx,
644 id[i].unixid, &id[i].sid);
645 if (NT_STATUS_EQUAL(id[i].status, NT_STATUS_RETRY)) {
646 id[i].status = idmap_xid_to_sid(idmap_ctx, mem_ctx,
650 if (!NT_STATUS_IS_OK(id[i].status)) {
651 DEBUG(1, ("idmapping xid_to_sid failed for id[%d]\n", i));
656 if (error_count == count) {
657 /* Mapping did not work at all. */
658 return NT_STATUS_NONE_MAPPED;
659 } else if (error_count > 0) {
660 /* Some mappings worked, some did not. */
661 return STATUS_SOME_UNMAPPED;
668 * Convert an array of SIDs to the corresponding array of unixids
670 * \param idmap_ctx idmap context to use
671 * \param mem_ctx talloc context the memory for the unixids is allocated
673 * \param count length of id_mapping array.
674 * \param id array of id_mappings.
675 * \return NT_STATUS_OK on success, NT_STATUS_NONE_MAPPED if mapping is not
676 * possible at all, NT_STATUS_SOME_UNMAPPED if some mappings worked and some
680 NTSTATUS idmap_sids_to_xids(struct idmap_context *idmap_ctx,
681 TALLOC_CTX *mem_ctx, int count,
682 struct id_mapping *id)
687 for (i = 0; i < count; ++i) {
688 id[i].status = idmap_sid_to_xid(idmap_ctx, mem_ctx,
689 id[i].sid, &id[i].unixid);
690 if (NT_STATUS_EQUAL(id[i].status, NT_STATUS_RETRY)) {
691 id[i].status = idmap_sid_to_xid(idmap_ctx, mem_ctx,
695 if (!NT_STATUS_IS_OK(id[i].status)) {
696 DEBUG(1, ("idmapping sid_to_xid failed for id[%d]\n", i));
701 if (error_count == count) {
702 /* Mapping did not work at all. */
703 return NT_STATUS_NONE_MAPPED;
704 } else if (error_count > 0) {
705 /* Some mappings worked, some did not. */
706 return STATUS_SOME_UNMAPPED;