2 Unix SMB/CIFS implementation.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) Gerald Carter 2003
9 Copyright (C) Simo Sorce 2003-2007
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/>.
29 #define DBGC_CLASS DBGC_IDMAP
36 static char *idmap_fetch_secret(const char *backend, bool alloc,
37 const char *domain, const char *identity)
43 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
45 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
51 strupper_m(tmp); /* make sure the key is case insensitive */
52 ret = secrets_fetch_generic(tmp, identity);
59 struct idmap_ldap_alloc_context {
60 struct smbldap_state *smbldap_state;
66 struct idmap_ldap_context {
67 struct smbldap_state *smbldap_state;
72 struct idmap_ldap_alloc_context *alloc;
75 #define CHECK_ALLOC_DONE(mem) do { \
77 DEBUG(0, ("Out of memory!\n")); \
78 ret = NT_STATUS_NO_MEMORY; \
82 /**********************************************************************
83 IDMAP ALLOC TDB BACKEND
84 **********************************************************************/
86 /*********************************************************************
87 ********************************************************************/
89 static NTSTATUS get_credentials( TALLOC_CTX *mem_ctx,
90 struct smbldap_state *ldap_state,
91 const char *config_option,
92 struct idmap_domain *dom,
95 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
97 const char *tmp = NULL;
101 /* assume anonymous if we don't have a specified user */
103 tmp = lp_parm_const_string(-1, config_option, "ldap_user_dn", NULL);
107 /* only the alloc backend can pass in a NULL dom */
108 secret = idmap_fetch_secret("ldap", True,
111 secret = idmap_fetch_secret("ldap", False,
116 DEBUG(0, ("get_credentials: Unable to fetch "
117 "auth credentials for %s in %s\n",
118 tmp, (dom==NULL)?"ALLOC":dom->name));
119 ret = NT_STATUS_ACCESS_DENIED;
122 *dn = talloc_strdup(mem_ctx, tmp);
123 CHECK_ALLOC_DONE(*dn);
125 if (!fetch_ldap_pw(&user_dn, &secret)) {
126 DEBUG(2, ("get_credentials: Failed to lookup ldap "
127 "bind creds. Using anonymous connection.\n"));
130 *dn = talloc_strdup(mem_ctx, user_dn);
131 SAFE_FREE( user_dn );
132 CHECK_ALLOC_DONE(*dn);
136 smbldap_set_creds(ldap_state, anon, *dn, secret);
146 /**********************************************************************
147 Verify the sambaUnixIdPool entry in the directory.
148 **********************************************************************/
150 static NTSTATUS verify_idpool(struct idmap_domain *dom)
154 LDAPMessage *result = NULL;
155 LDAPMod **mods = NULL;
156 const char **attr_list;
160 struct idmap_ldap_context *ctx;
162 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
165 return NT_STATUS_UNSUCCESSFUL;
168 mem_ctx = talloc_new(ctx->alloc);
169 if (mem_ctx == NULL) {
170 DEBUG(0, ("Out of memory!\n"));
171 return NT_STATUS_NO_MEMORY;
174 filter = talloc_asprintf(mem_ctx, "(objectclass=%s)", LDAP_OBJ_IDPOOL);
175 CHECK_ALLOC_DONE(filter);
177 attr_list = get_attr_list(mem_ctx, idpool_attr_list);
178 CHECK_ALLOC_DONE(attr_list);
180 rc = smbldap_search(ctx->alloc->smbldap_state,
188 if (rc != LDAP_SUCCESS) {
189 DEBUG(1, ("Unable to verify the idpool, "
190 "cannot continue initialization!\n"));
191 return NT_STATUS_UNSUCCESSFUL;
194 count = ldap_count_entries(ctx->alloc->smbldap_state->ldap_struct,
197 ldap_msgfree(result);
200 DEBUG(0,("Multiple entries returned from %s (base == %s)\n",
201 filter, ctx->alloc->suffix));
202 ret = NT_STATUS_UNSUCCESSFUL;
205 else if (count == 0) {
206 char *uid_str, *gid_str;
208 uid_str = talloc_asprintf(mem_ctx, "%lu",
209 (unsigned long)dom->low_id);
210 gid_str = talloc_asprintf(mem_ctx, "%lu",
211 (unsigned long)dom->low_id);
213 smbldap_set_mod(&mods, LDAP_MOD_ADD,
214 "objectClass", LDAP_OBJ_IDPOOL);
215 smbldap_set_mod(&mods, LDAP_MOD_ADD,
216 get_attr_key2string(idpool_attr_list,
217 LDAP_ATTR_UIDNUMBER),
219 smbldap_set_mod(&mods, LDAP_MOD_ADD,
220 get_attr_key2string(idpool_attr_list,
221 LDAP_ATTR_GIDNUMBER),
224 rc = smbldap_modify(ctx->alloc->smbldap_state,
227 ldap_mods_free(mods, True);
229 ret = NT_STATUS_UNSUCCESSFUL;
234 ret = (rc == LDAP_SUCCESS)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
236 talloc_free(mem_ctx);
240 /*****************************************************************************
241 Initialise idmap database.
242 *****************************************************************************/
244 static int idmap_ldap_alloc_close_destructor(struct idmap_ldap_alloc_context *ctx)
246 smbldap_free_struct(&ctx->smbldap_state);
247 DEBUG(5,("The connection to the LDAP server was closed\n"));
248 /* maybe free the results here --metze */
252 static NTSTATUS idmap_ldap_alloc_init(struct idmap_domain *dom,
255 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
257 struct idmap_ldap_context *ctx;
259 /* Only do init if we are online */
260 if (idmap_is_offline()) {
261 return NT_STATUS_FILE_IS_OFFLINE;
264 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
266 ctx->alloc = talloc_zero(ctx, struct idmap_ldap_alloc_context);
267 CHECK_ALLOC_DONE(ctx->alloc);
269 if (params && *params) {
270 /* assume location is the only parameter */
271 ctx->alloc->url = talloc_strdup(ctx->alloc, params);
273 tmp = lp_parm_const_string(-1, "idmap alloc config",
277 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
278 ret = NT_STATUS_UNSUCCESSFUL;
282 ctx->alloc->url = talloc_strdup(ctx->alloc, tmp);
284 CHECK_ALLOC_DONE(ctx->alloc->url);
286 trim_char(ctx->alloc->url, '\"', '\"');
288 tmp = lp_parm_const_string(-1, "idmap alloc config",
289 "ldap_base_dn", NULL);
290 if ( ! tmp || ! *tmp) {
291 tmp = lp_ldap_idmap_suffix();
293 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
294 ret = NT_STATUS_UNSUCCESSFUL;
299 ctx->alloc->suffix = talloc_strdup(ctx->alloc, tmp);
300 CHECK_ALLOC_DONE(ctx->alloc->suffix);
302 ret = smbldap_init(ctx->alloc, winbind_event_context(),
304 &ctx->alloc->smbldap_state);
305 if (!NT_STATUS_IS_OK(ret)) {
306 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n",
311 talloc_set_destructor(ctx->alloc, idmap_ldap_alloc_close_destructor);
313 ret = get_credentials(ctx->alloc,
314 ctx->alloc->smbldap_state,
315 "idmap alloc config", NULL,
316 &ctx->alloc->user_dn);
317 if ( !NT_STATUS_IS_OK(ret) ) {
318 DEBUG(1,("idmap_ldap_alloc_init: Failed to get connection "
319 "credentials (%s)\n", nt_errstr(ret)));
323 /* see if the idmap suffix and sub entries exists */
325 ret = verify_idpool(dom);
328 if ( !NT_STATUS_IS_OK( ret ) )
329 TALLOC_FREE(ctx->alloc);
334 /********************************
335 Allocate a new uid or gid
336 ********************************/
338 static NTSTATUS idmap_ldap_allocate_id(struct idmap_domain *dom,
342 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
343 int rc = LDAP_SERVER_DOWN;
345 LDAPMessage *result = NULL;
346 LDAPMessage *entry = NULL;
347 LDAPMod **mods = NULL;
351 const char *dn = NULL;
352 const char **attr_list;
354 struct idmap_ldap_context *ctx;
356 /* Only do query if we are online */
357 if (idmap_is_offline()) {
358 return NT_STATUS_FILE_IS_OFFLINE;
361 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
364 return NT_STATUS_UNSUCCESSFUL;
367 mem_ctx = talloc_new(ctx->alloc);
369 DEBUG(0, ("Out of memory!\n"));
370 return NT_STATUS_NO_MEMORY;
377 type = get_attr_key2string(idpool_attr_list,
378 LDAP_ATTR_UIDNUMBER);
382 type = get_attr_key2string(idpool_attr_list,
383 LDAP_ATTR_GIDNUMBER);
387 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
388 return NT_STATUS_INVALID_PARAMETER;
391 filter = talloc_asprintf(mem_ctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
392 CHECK_ALLOC_DONE(filter);
394 attr_list = get_attr_list(mem_ctx, idpool_attr_list);
395 CHECK_ALLOC_DONE(attr_list);
397 DEBUG(10, ("Search of the id pool (filter: %s)\n", filter));
399 rc = smbldap_search(ctx->alloc->smbldap_state,
401 LDAP_SCOPE_SUBTREE, filter,
402 attr_list, 0, &result);
404 if (rc != LDAP_SUCCESS) {
405 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
409 talloc_autofree_ldapmsg(mem_ctx, result);
411 count = ldap_count_entries(ctx->alloc->smbldap_state->ldap_struct,
414 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
418 entry = ldap_first_entry(ctx->alloc->smbldap_state->ldap_struct,
421 dn = smbldap_talloc_dn(mem_ctx,
422 ctx->alloc->smbldap_state->ldap_struct,
428 id_str = smbldap_talloc_single_attribute(
429 ctx->alloc->smbldap_state->ldap_struct,
430 entry, type, mem_ctx);
431 if (id_str == NULL) {
432 DEBUG(0,("%s attribute not found\n", type));
436 DEBUG(0,("Out of memory\n"));
437 ret = NT_STATUS_NO_MEMORY;
441 xid->id = strtoul(id_str, NULL, 10);
443 /* make sure we still have room to grow */
447 if (xid->id > dom->high_id) {
448 DEBUG(0,("Cannot allocate uid above %lu!\n",
449 (unsigned long)dom->high_id));
455 if (xid->id > dom->high_id) {
456 DEBUG(0,("Cannot allocate gid above %lu!\n",
457 (unsigned long)dom->high_id));
467 new_id_str = talloc_asprintf(mem_ctx, "%lu", (unsigned long)xid->id + 1);
469 DEBUG(0,("Out of memory\n"));
470 ret = NT_STATUS_NO_MEMORY;
474 smbldap_set_mod(&mods, LDAP_MOD_DELETE, type, id_str);
475 smbldap_set_mod(&mods, LDAP_MOD_ADD, type, new_id_str);
478 DEBUG(0,("smbldap_set_mod() failed.\n"));
482 DEBUG(10, ("Try to atomically increment the id (%s -> %s)\n",
483 id_str, new_id_str));
485 rc = smbldap_modify(ctx->alloc->smbldap_state, dn, mods);
487 ldap_mods_free(mods, True);
489 if (rc != LDAP_SUCCESS) {
490 DEBUG(1,("Failed to allocate new %s. "
491 "smbldap_modify() failed.\n", type));
498 talloc_free(mem_ctx);
503 * Allocate a new unix-ID.
504 * For now this is for the default idmap domain only.
505 * Should be extended later on.
507 static NTSTATUS idmap_ldap_get_new_id(struct idmap_domain *dom,
512 if (!strequal(dom->name, "*")) {
513 DEBUG(3, ("idmap_ldap_get_new_id: "
514 "Refusing allocation of a new unixid for domain'%s'. "
515 "Currently only supported for the default "
518 return NT_STATUS_NOT_IMPLEMENTED;
521 ret = idmap_ldap_allocate_id(dom, id);
527 /**********************************************************************
528 IDMAP MAPPING LDAP BACKEND
529 **********************************************************************/
531 static int idmap_ldap_close_destructor(struct idmap_ldap_context *ctx)
533 smbldap_free_struct(&ctx->smbldap_state);
534 DEBUG(5,("The connection to the LDAP server was closed\n"));
535 /* maybe free the results here --metze */
540 /********************************
541 Initialise idmap database.
542 ********************************/
544 static NTSTATUS idmap_ldap_db_init(struct idmap_domain *dom,
548 struct idmap_ldap_context *ctx = NULL;
549 char *config_option = NULL;
550 const char *tmp = NULL;
552 /* Only do init if we are online */
553 if (idmap_is_offline()) {
554 return NT_STATUS_FILE_IS_OFFLINE;
557 ctx = TALLOC_ZERO_P(dom, struct idmap_ldap_context);
559 DEBUG(0, ("Out of memory!\n"));
560 return NT_STATUS_NO_MEMORY;
563 if (strequal(dom->name, "*")) {
564 /* more specific configuration can go here */
566 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
567 if ( ! config_option) {
568 DEBUG(0, ("Out of memory!\n"));
569 ret = NT_STATUS_NO_MEMORY;
574 if (params != NULL) {
575 /* assume location is the only parameter */
576 ctx->url = talloc_strdup(ctx, params);
578 tmp = lp_parm_const_string(-1, config_option, "ldap_url", NULL);
581 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
582 ret = NT_STATUS_UNSUCCESSFUL;
586 ctx->url = talloc_strdup(ctx, tmp);
588 CHECK_ALLOC_DONE(ctx->url);
590 trim_char(ctx->url, '\"', '\"');
592 tmp = lp_parm_const_string(-1, config_option, "ldap_base_dn", NULL);
593 if ( ! tmp || ! *tmp) {
594 tmp = lp_ldap_idmap_suffix();
596 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
597 ret = NT_STATUS_UNSUCCESSFUL;
602 ctx->suffix = talloc_strdup(ctx, tmp);
603 CHECK_ALLOC_DONE(ctx->suffix);
605 ret = smbldap_init(ctx, winbind_event_context(), ctx->url,
606 &ctx->smbldap_state);
607 if (!NT_STATUS_IS_OK(ret)) {
608 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", ctx->url));
612 ret = get_credentials( ctx, ctx->smbldap_state, config_option,
613 dom, &ctx->user_dn );
614 if ( !NT_STATUS_IS_OK(ret) ) {
615 DEBUG(1,("idmap_ldap_db_init: Failed to get connection "
616 "credentials (%s)\n", nt_errstr(ret)));
620 /* set the destructor on the context, so that resource are properly
621 freed if the contexts is released */
623 talloc_set_destructor(ctx, idmap_ldap_close_destructor);
625 dom->private_data = ctx;
627 ret = idmap_ldap_alloc_init(dom, params);
628 if (!NT_STATUS_IS_OK(ret)) {
629 DEBUG(1, ("idmap_ldap_db_init: Failed to initialize alloc "
630 "subsystem: %s\n", nt_errstr(ret)));
634 talloc_free(config_option);
647 /* TODO: change this: This function cannot be called to modify a mapping,
648 * only set a new one */
650 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
651 const struct id_map *map)
655 struct idmap_ldap_context *ctx;
656 LDAPMessage *entry = NULL;
657 LDAPMod **mods = NULL;
664 /* Only do query if we are online */
665 if (idmap_is_offline()) {
666 return NT_STATUS_FILE_IS_OFFLINE;
669 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
671 switch(map->xid.type) {
673 type = get_attr_key2string(sidmap_attr_list,
674 LDAP_ATTR_UIDNUMBER);
678 type = get_attr_key2string(sidmap_attr_list,
679 LDAP_ATTR_GIDNUMBER);
683 return NT_STATUS_INVALID_PARAMETER;
686 memctx = talloc_new(ctx);
688 DEBUG(0, ("Out of memory!\n"));
689 return NT_STATUS_NO_MEMORY;
692 id_str = talloc_asprintf(memctx, "%lu", (unsigned long)map->xid.id);
693 CHECK_ALLOC_DONE(id_str);
695 sid = talloc_strdup(memctx, sid_string_talloc(memctx, map->sid));
696 CHECK_ALLOC_DONE(sid);
698 dn = talloc_asprintf(memctx, "%s=%s,%s",
699 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
702 CHECK_ALLOC_DONE(dn);
704 smbldap_set_mod(&mods, LDAP_MOD_ADD,
705 "objectClass", LDAP_OBJ_IDMAP_ENTRY);
707 smbldap_make_mod(ctx->smbldap_state->ldap_struct,
708 entry, &mods, type, id_str);
710 smbldap_make_mod(ctx->smbldap_state->ldap_struct, entry, &mods,
711 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
715 DEBUG(2, ("ERROR: No mods?\n"));
716 ret = NT_STATUS_UNSUCCESSFUL;
720 /* TODO: remove conflicting mappings! */
722 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SID_ENTRY);
724 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn, sid, id_str));
726 rc = smbldap_add(ctx->smbldap_state, dn, mods);
727 ldap_mods_free(mods, True);
729 if (rc != LDAP_SUCCESS) {
730 char *ld_error = NULL;
731 ldap_get_option(ctx->smbldap_state->ldap_struct,
732 LDAP_OPT_ERROR_STRING, &ld_error);
733 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
734 "mapping [%s]\n", sid,
735 (unsigned long)map->xid.id, type));
736 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
737 ld_error ? ld_error : "(NULL)", ldap_err2string (rc)));
739 ldap_memfree(ld_error);
741 ret = NT_STATUS_UNSUCCESSFUL;
745 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
746 "%lu [%s]\n", sid, (unsigned long)map->xid.id, type));
756 /* max number of ids requested per batch query */
757 #define IDMAP_LDAP_MAX_IDS 30
759 /**********************************
760 lookup a set of unix ids.
761 **********************************/
763 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
764 * in maps for a match */
765 static struct id_map *find_map_by_id(struct id_map **maps,
771 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
772 if (maps[i] == NULL) { /* end of the run */
775 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
783 static NTSTATUS idmap_ldap_unixids_to_sids(struct idmap_domain *dom,
788 struct idmap_ldap_context *ctx;
789 LDAPMessage *result = NULL;
790 LDAPMessage *entry = NULL;
791 const char *uidNumber;
792 const char *gidNumber;
793 const char **attr_list;
802 /* Only do query if we are online */
803 if (idmap_is_offline()) {
804 return NT_STATUS_FILE_IS_OFFLINE;
807 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
809 memctx = talloc_new(ctx);
811 DEBUG(0, ("Out of memory!\n"));
812 return NT_STATUS_NO_MEMORY;
815 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
816 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
818 attr_list = get_attr_list(memctx, sidmap_attr_list);
821 /* if we are requested just one mapping use the simple filter */
823 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%lu))",
824 LDAP_OBJ_IDMAP_ENTRY,
825 (ids[0]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
826 (unsigned long)ids[0]->xid.id);
827 CHECK_ALLOC_DONE(filter);
828 DEBUG(10, ("Filter: [%s]\n", filter));
830 /* multiple mappings */
834 for (i = 0; ids[i]; i++) {
835 ids[i]->status = ID_UNKNOWN;
842 filter = talloc_asprintf(memctx,
843 "(&(objectClass=%s)(|",
844 LDAP_OBJ_IDMAP_ENTRY);
845 CHECK_ALLOC_DONE(filter);
848 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
849 filter = talloc_asprintf_append_buffer(filter, "(%s=%lu)",
850 (ids[idx]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
851 (unsigned long)ids[idx]->xid.id);
852 CHECK_ALLOC_DONE(filter);
854 filter = talloc_asprintf_append_buffer(filter, "))");
855 CHECK_ALLOC_DONE(filter);
856 DEBUG(10, ("Filter: [%s]\n", filter));
862 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
863 filter, attr_list, 0, &result);
865 if (rc != LDAP_SUCCESS) {
866 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc)));
867 ret = NT_STATUS_UNSUCCESSFUL;
871 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
874 DEBUG(10, ("NO SIDs found\n"));
877 for (i = 0; i < count; i++) {
884 if (i == 0) { /* first entry */
885 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
887 } else { /* following ones */
888 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
892 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
897 /* first check if the SID is present */
898 sidstr = smbldap_talloc_single_attribute(
899 ctx->smbldap_state->ldap_struct,
900 entry, LDAP_ATTRIBUTE_SID, memctx);
901 if ( ! sidstr) { /* no sid, skip entry */
902 DEBUG(2, ("WARNING SID not found on entry\n"));
906 /* now try to see if it is a uid, if not try with a gid
907 * (gid is more common, but in case both uidNumber and
908 * gidNumber are returned the SID is mapped to the uid
911 tmp = smbldap_talloc_single_attribute(
912 ctx->smbldap_state->ldap_struct,
913 entry, uidNumber, memctx);
916 tmp = smbldap_talloc_single_attribute(
917 ctx->smbldap_state->ldap_struct,
918 entry, gidNumber, memctx);
920 if ( ! tmp) { /* wow very strange entry, how did it match ? */
921 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
922 "nor gidNumber returned\n", sidstr));
927 id = strtoul(tmp, NULL, 10);
928 if (!idmap_unix_id_is_in_range(id, dom)) {
929 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
931 dom->low_id, dom->high_id));
938 map = find_map_by_id(&ids[bidx], type, id);
940 DEBUG(2, ("WARNING: couldn't match sid (%s) "
941 "with requested ids\n", sidstr));
946 if ( ! string_to_sid(map->sid, sidstr)) {
947 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
952 if (map->status == ID_MAPPED) {
953 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
954 "overwriting mapping %u -> %s with %u -> %s\n",
955 (type == ID_TYPE_UID) ? "UID" : "GID",
956 id, sid_string_dbg(map->sid), id, sidstr));
962 map->status = ID_MAPPED;
964 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
965 (unsigned long)map->xid.id, map->xid.type));
968 /* free the ldap results */
970 ldap_msgfree(result);
974 if (multi && ids[idx]) { /* still some values to map */
980 /* mark all unknwon/expired ones as unmapped */
981 for (i = 0; ids[i]; i++) {
982 if (ids[i]->status != ID_MAPPED)
983 ids[i]->status = ID_UNMAPPED;
991 /**********************************
992 lookup a set of sids.
993 **********************************/
995 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
996 * in maps for a match */
997 static struct id_map *find_map_by_sid(struct id_map **maps, DOM_SID *sid)
1001 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
1002 if (maps[i] == NULL) { /* end of the run */
1005 if (sid_equal(maps[i]->sid, sid)) {
1013 static NTSTATUS idmap_ldap_sids_to_unixids(struct idmap_domain *dom,
1014 struct id_map **ids)
1016 LDAPMessage *entry = NULL;
1019 struct idmap_ldap_context *ctx;
1020 LDAPMessage *result = NULL;
1021 const char *uidNumber;
1022 const char *gidNumber;
1023 const char **attr_list;
1024 char *filter = NULL;
1032 /* Only do query if we are online */
1033 if (idmap_is_offline()) {
1034 return NT_STATUS_FILE_IS_OFFLINE;
1037 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
1039 memctx = talloc_new(ctx);
1041 DEBUG(0, ("Out of memory!\n"));
1042 return NT_STATUS_NO_MEMORY;
1045 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
1046 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
1048 attr_list = get_attr_list(memctx, sidmap_attr_list);
1051 /* if we are requested just one mapping use the simple filter */
1053 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%s))",
1054 LDAP_OBJ_IDMAP_ENTRY,
1056 sid_string_talloc(memctx, ids[0]->sid));
1057 CHECK_ALLOC_DONE(filter);
1058 DEBUG(10, ("Filter: [%s]\n", filter));
1060 /* multiple mappings */
1064 for (i = 0; ids[i]; i++) {
1065 ids[i]->status = ID_UNKNOWN;
1071 TALLOC_FREE(filter);
1072 filter = talloc_asprintf(memctx,
1073 "(&(objectClass=%s)(|",
1074 LDAP_OBJ_IDMAP_ENTRY);
1075 CHECK_ALLOC_DONE(filter);
1078 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
1079 filter = talloc_asprintf_append_buffer(filter, "(%s=%s)",
1081 sid_string_talloc(memctx,
1083 CHECK_ALLOC_DONE(filter);
1085 filter = talloc_asprintf_append_buffer(filter, "))");
1086 CHECK_ALLOC_DONE(filter);
1087 DEBUG(10, ("Filter: [%s]", filter));
1093 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
1094 filter, attr_list, 0, &result);
1096 if (rc != LDAP_SUCCESS) {
1097 DEBUG(3,("Failure looking up sids (%s)\n",
1098 ldap_err2string(rc)));
1099 ret = NT_STATUS_UNSUCCESSFUL;
1103 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
1106 DEBUG(10, ("NO SIDs found\n"));
1109 for (i = 0; i < count; i++) {
1110 char *sidstr = NULL;
1117 if (i == 0) { /* first entry */
1118 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
1120 } else { /* following ones */
1121 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
1125 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
1130 /* first check if the SID is present */
1131 sidstr = smbldap_talloc_single_attribute(
1132 ctx->smbldap_state->ldap_struct,
1133 entry, LDAP_ATTRIBUTE_SID, memctx);
1134 if ( ! sidstr) { /* no sid ??, skip entry */
1135 DEBUG(2, ("WARNING SID not found on entry\n"));
1139 if ( ! string_to_sid(&sid, sidstr)) {
1140 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
1141 TALLOC_FREE(sidstr);
1145 map = find_map_by_sid(&ids[bidx], &sid);
1147 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
1149 TALLOC_FREE(sidstr);
1153 /* now try to see if it is a uid, if not try with a gid
1154 * (gid is more common, but in case both uidNumber and
1155 * gidNumber are returned the SID is mapped to the uid
1158 tmp = smbldap_talloc_single_attribute(
1159 ctx->smbldap_state->ldap_struct,
1160 entry, uidNumber, memctx);
1163 tmp = smbldap_talloc_single_attribute(
1164 ctx->smbldap_state->ldap_struct,
1165 entry, gidNumber, memctx);
1167 if ( ! tmp) { /* no ids ?? */
1168 DEBUG(5, ("no uidNumber, "
1169 "nor gidNumber attributes found\n"));
1170 TALLOC_FREE(sidstr);
1174 id = strtoul(tmp, NULL, 10);
1175 if (!idmap_unix_id_is_in_range(id, dom)) {
1176 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1178 dom->low_id, dom->high_id));
1179 TALLOC_FREE(sidstr);
1185 if (map->status == ID_MAPPED) {
1186 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1187 "overwriting mapping %s -> %u with %s -> %u\n",
1188 (type == ID_TYPE_UID) ? "UID" : "GID",
1189 sidstr, map->xid.id, sidstr, id));
1192 TALLOC_FREE(sidstr);
1195 map->xid.type = type;
1197 map->status = ID_MAPPED;
1199 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1200 (unsigned long)map->xid.id, map->xid.type));
1203 /* free the ldap results */
1205 ldap_msgfree(result);
1209 if (multi && ids[idx]) { /* still some values to map */
1215 /* mark all unknwon/expired ones as unmapped */
1216 for (i = 0; ids[i]; i++) {
1217 if (ids[i]->status != ID_MAPPED)
1218 ids[i]->status = ID_UNMAPPED;
1222 talloc_free(memctx);
1226 /**********************************
1227 Close the idmap ldap instance
1228 **********************************/
1230 static NTSTATUS idmap_ldap_close(struct idmap_domain *dom)
1232 struct idmap_ldap_context *ctx;
1234 if (dom->private_data) {
1235 ctx = talloc_get_type(dom->private_data,
1236 struct idmap_ldap_context);
1239 dom->private_data = NULL;
1242 return NT_STATUS_OK;
1245 static struct idmap_methods idmap_ldap_methods = {
1247 .init = idmap_ldap_db_init,
1248 .unixids_to_sids = idmap_ldap_unixids_to_sids,
1249 .sids_to_unixids = idmap_ldap_sids_to_unixids,
1250 .allocate_id = idmap_ldap_get_new_id,
1251 .close_fn = idmap_ldap_close
1254 NTSTATUS idmap_ldap_init(void);
1255 NTSTATUS idmap_ldap_init(void)
1257 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1258 &idmap_ldap_methods);