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_context {
60 struct smbldap_state *smbldap_state;
67 struct idmap_ldap_alloc_context {
68 struct smbldap_state *smbldap_state;
72 uid_t low_uid, high_uid; /* Range of uids */
73 gid_t low_gid, high_gid; /* Range of gids */
77 #define CHECK_ALLOC_DONE(mem) do { \
79 DEBUG(0, ("Out of memory!\n")); \
80 ret = NT_STATUS_NO_MEMORY; \
84 /**********************************************************************
85 IDMAP ALLOC TDB BACKEND
86 **********************************************************************/
88 static struct idmap_ldap_alloc_context *idmap_alloc_ldap;
90 /*********************************************************************
91 ********************************************************************/
93 static NTSTATUS get_credentials( TALLOC_CTX *mem_ctx,
94 struct smbldap_state *ldap_state,
95 const char *config_option,
96 struct idmap_domain *dom,
99 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
101 const char *tmp = NULL;
102 char *user_dn = NULL;
105 /* assume anonymous if we don't have a specified user */
107 tmp = lp_parm_const_string(-1, config_option, "ldap_user_dn", NULL);
111 /* only the alloc backend can pass in a NULL dom */
112 secret = idmap_fetch_secret("ldap", True,
115 secret = idmap_fetch_secret("ldap", False,
120 DEBUG(0, ("get_credentials: Unable to fetch "
121 "auth credentials for %s in %s\n",
122 tmp, (dom==NULL)?"ALLOC":dom->name));
123 ret = NT_STATUS_ACCESS_DENIED;
126 *dn = talloc_strdup(mem_ctx, tmp);
127 CHECK_ALLOC_DONE(*dn);
129 if (!fetch_ldap_pw(&user_dn, &secret)) {
130 DEBUG(2, ("get_credentials: Failed to lookup ldap "
131 "bind creds. Using anonymous connection.\n"));
134 *dn = talloc_strdup(mem_ctx, user_dn);
135 SAFE_FREE( user_dn );
136 CHECK_ALLOC_DONE(*dn);
140 smbldap_set_creds(ldap_state, anon, *dn, secret);
150 /**********************************************************************
151 Verify the sambaUnixIdPool entry in the directory.
152 **********************************************************************/
154 static NTSTATUS verify_idpool(struct idmap_domain *dom)
158 LDAPMessage *result = NULL;
159 LDAPMod **mods = NULL;
160 const char **attr_list;
165 if ( ! idmap_alloc_ldap) {
166 return NT_STATUS_UNSUCCESSFUL;
169 ctx = talloc_new(idmap_alloc_ldap);
171 DEBUG(0, ("Out of memory!\n"));
172 return NT_STATUS_NO_MEMORY;
175 filter = talloc_asprintf(ctx, "(objectclass=%s)", LDAP_OBJ_IDPOOL);
176 CHECK_ALLOC_DONE(filter);
178 attr_list = get_attr_list(ctx, idpool_attr_list);
179 CHECK_ALLOC_DONE(attr_list);
181 rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
182 idmap_alloc_ldap->suffix,
189 if (rc != LDAP_SUCCESS) {
190 DEBUG(1, ("Unable to verify the idpool, "
191 "cannot continue initialization!\n"));
192 return NT_STATUS_UNSUCCESSFUL;
195 count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
198 ldap_msgfree(result);
201 DEBUG(0,("Multiple entries returned from %s (base == %s)\n",
202 filter, idmap_alloc_ldap->suffix));
203 ret = NT_STATUS_UNSUCCESSFUL;
206 else if (count == 0) {
207 char *uid_str, *gid_str;
209 uid_str = talloc_asprintf(ctx, "%lu",
210 (unsigned long)dom->low_id);
211 gid_str = talloc_asprintf(ctx, "%lu",
212 (unsigned long)dom->low_id);
214 smbldap_set_mod(&mods, LDAP_MOD_ADD,
215 "objectClass", LDAP_OBJ_IDPOOL);
216 smbldap_set_mod(&mods, LDAP_MOD_ADD,
217 get_attr_key2string(idpool_attr_list,
218 LDAP_ATTR_UIDNUMBER),
220 smbldap_set_mod(&mods, LDAP_MOD_ADD,
221 get_attr_key2string(idpool_attr_list,
222 LDAP_ATTR_GIDNUMBER),
225 rc = smbldap_modify(idmap_alloc_ldap->smbldap_state,
226 idmap_alloc_ldap->suffix,
228 ldap_mods_free(mods, True);
230 ret = NT_STATUS_UNSUCCESSFUL;
235 ret = (rc == LDAP_SUCCESS)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
241 /*****************************************************************************
242 Initialise idmap database.
243 *****************************************************************************/
245 static NTSTATUS idmap_ldap_alloc_init(struct idmap_domain *dom,
248 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
251 /* Only do init if we are online */
252 if (idmap_is_offline()) {
253 return NT_STATUS_FILE_IS_OFFLINE;
256 idmap_alloc_ldap = TALLOC_ZERO_P(NULL, struct idmap_ldap_alloc_context);
257 CHECK_ALLOC_DONE( idmap_alloc_ldap );
259 if (params && *params) {
260 /* assume location is the only parameter */
261 idmap_alloc_ldap->url = talloc_strdup(idmap_alloc_ldap, params);
263 tmp = lp_parm_const_string(-1, "idmap alloc config",
267 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
268 ret = NT_STATUS_UNSUCCESSFUL;
272 idmap_alloc_ldap->url = talloc_strdup(idmap_alloc_ldap, tmp);
274 CHECK_ALLOC_DONE( idmap_alloc_ldap->url );
276 trim_char(idmap_alloc_ldap->url, '\"', '\"');
278 tmp = lp_parm_const_string(-1, "idmap alloc config",
279 "ldap_base_dn", NULL);
280 if ( ! tmp || ! *tmp) {
281 tmp = lp_ldap_idmap_suffix();
283 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
284 ret = NT_STATUS_UNSUCCESSFUL;
289 idmap_alloc_ldap->suffix = talloc_strdup(idmap_alloc_ldap, tmp);
290 CHECK_ALLOC_DONE( idmap_alloc_ldap->suffix );
292 ret = smbldap_init(idmap_alloc_ldap, winbind_event_context(),
293 idmap_alloc_ldap->url,
294 &idmap_alloc_ldap->smbldap_state);
295 if (!NT_STATUS_IS_OK(ret)) {
296 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n",
297 idmap_alloc_ldap->url));
301 ret = get_credentials( idmap_alloc_ldap,
302 idmap_alloc_ldap->smbldap_state,
303 "idmap alloc config", NULL,
304 &idmap_alloc_ldap->user_dn );
305 if ( !NT_STATUS_IS_OK(ret) ) {
306 DEBUG(1,("idmap_ldap_alloc_init: Failed to get connection "
307 "credentials (%s)\n", nt_errstr(ret)));
311 /* see if the idmap suffix and sub entries exists */
313 ret = verify_idpool(dom);
316 if ( !NT_STATUS_IS_OK( ret ) )
317 TALLOC_FREE( idmap_alloc_ldap );
322 /********************************
323 Allocate a new uid or gid
324 ********************************/
326 static NTSTATUS idmap_ldap_allocate_id(struct unixid *xid)
329 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
330 int rc = LDAP_SERVER_DOWN;
332 LDAPMessage *result = NULL;
333 LDAPMessage *entry = NULL;
334 LDAPMod **mods = NULL;
338 const char *dn = NULL;
339 const char **attr_list;
342 /* Only do query if we are online */
343 if (idmap_is_offline()) {
344 return NT_STATUS_FILE_IS_OFFLINE;
347 if ( ! idmap_alloc_ldap) {
348 return NT_STATUS_UNSUCCESSFUL;
351 ctx = talloc_new(idmap_alloc_ldap);
353 DEBUG(0, ("Out of memory!\n"));
354 return NT_STATUS_NO_MEMORY;
361 type = get_attr_key2string(idpool_attr_list,
362 LDAP_ATTR_UIDNUMBER);
366 type = get_attr_key2string(idpool_attr_list,
367 LDAP_ATTR_GIDNUMBER);
371 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
372 return NT_STATUS_INVALID_PARAMETER;
375 filter = talloc_asprintf(ctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
376 CHECK_ALLOC_DONE(filter);
378 attr_list = get_attr_list(ctx, idpool_attr_list);
379 CHECK_ALLOC_DONE(attr_list);
381 DEBUG(10, ("Search of the id pool (filter: %s)\n", filter));
383 rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
384 idmap_alloc_ldap->suffix,
385 LDAP_SCOPE_SUBTREE, filter,
386 attr_list, 0, &result);
388 if (rc != LDAP_SUCCESS) {
389 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
393 talloc_autofree_ldapmsg(ctx, result);
395 count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
398 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
402 entry = ldap_first_entry(idmap_alloc_ldap->smbldap_state->ldap_struct,
405 dn = smbldap_talloc_dn(ctx,
406 idmap_alloc_ldap->smbldap_state->ldap_struct,
412 if ( ! (id_str = smbldap_talloc_single_attribute(idmap_alloc_ldap->smbldap_state->ldap_struct,
413 entry, type, ctx))) {
414 DEBUG(0,("%s attribute not found\n", type));
418 DEBUG(0,("Out of memory\n"));
419 ret = NT_STATUS_NO_MEMORY;
423 xid->id = strtoul(id_str, NULL, 10);
425 /* make sure we still have room to grow */
429 if (xid->id > idmap_alloc_ldap->high_uid) {
430 DEBUG(0,("Cannot allocate uid above %lu!\n",
431 (unsigned long)idmap_alloc_ldap->high_uid));
437 if (xid->id > idmap_alloc_ldap->high_gid) {
438 DEBUG(0,("Cannot allocate gid above %lu!\n",
439 (unsigned long)idmap_alloc_ldap->high_uid));
449 new_id_str = talloc_asprintf(ctx, "%lu", (unsigned long)xid->id + 1);
451 DEBUG(0,("Out of memory\n"));
452 ret = NT_STATUS_NO_MEMORY;
456 smbldap_set_mod(&mods, LDAP_MOD_DELETE, type, id_str);
457 smbldap_set_mod(&mods, LDAP_MOD_ADD, type, new_id_str);
460 DEBUG(0,("smbldap_set_mod() failed.\n"));
464 DEBUG(10, ("Try to atomically increment the id (%s -> %s)\n",
465 id_str, new_id_str));
467 rc = smbldap_modify(idmap_alloc_ldap->smbldap_state, dn, mods);
469 ldap_mods_free(mods, True);
471 if (rc != LDAP_SUCCESS) {
472 DEBUG(1,("Failed to allocate new %s. "
473 "smbldap_modify() failed.\n", type));
484 /**********************************
485 Close idmap ldap alloc
486 **********************************/
488 static NTSTATUS idmap_ldap_alloc_close(void)
490 if (idmap_alloc_ldap) {
491 smbldap_free_struct(&idmap_alloc_ldap->smbldap_state);
492 DEBUG(5,("The connection to the LDAP server was closed\n"));
493 /* maybe free the results here --metze */
494 TALLOC_FREE(idmap_alloc_ldap);
500 /**********************************************************************
501 IDMAP MAPPING LDAP BACKEND
502 **********************************************************************/
504 static int idmap_ldap_close_destructor(struct idmap_ldap_context *ctx)
506 smbldap_free_struct(&ctx->smbldap_state);
507 DEBUG(5,("The connection to the LDAP server was closed\n"));
508 /* maybe free the results here --metze */
513 /********************************
514 Initialise idmap database.
515 ********************************/
517 static NTSTATUS idmap_ldap_db_init(struct idmap_domain *dom,
521 struct idmap_ldap_context *ctx = NULL;
522 char *config_option = NULL;
523 const char *tmp = NULL;
525 /* Only do init if we are online */
526 if (idmap_is_offline()) {
527 return NT_STATUS_FILE_IS_OFFLINE;
530 ctx = TALLOC_ZERO_P(dom, struct idmap_ldap_context);
532 DEBUG(0, ("Out of memory!\n"));
533 return NT_STATUS_NO_MEMORY;
536 if (strequal(dom->name, "*")) {
537 /* more specific configuration can go here */
539 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
540 if ( ! config_option) {
541 DEBUG(0, ("Out of memory!\n"));
542 ret = NT_STATUS_NO_MEMORY;
547 if (params != NULL) {
548 /* assume location is the only parameter */
549 ctx->url = talloc_strdup(ctx, params);
551 tmp = lp_parm_const_string(-1, config_option, "ldap_url", NULL);
554 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
555 ret = NT_STATUS_UNSUCCESSFUL;
559 ctx->url = talloc_strdup(ctx, tmp);
561 CHECK_ALLOC_DONE(ctx->url);
563 trim_char(ctx->url, '\"', '\"');
565 tmp = lp_parm_const_string(-1, config_option, "ldap_base_dn", NULL);
566 if ( ! tmp || ! *tmp) {
567 tmp = lp_ldap_idmap_suffix();
569 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
570 ret = NT_STATUS_UNSUCCESSFUL;
575 ctx->suffix = talloc_strdup(ctx, tmp);
576 CHECK_ALLOC_DONE(ctx->suffix);
578 ret = smbldap_init(ctx, winbind_event_context(), ctx->url,
579 &ctx->smbldap_state);
580 if (!NT_STATUS_IS_OK(ret)) {
581 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", ctx->url));
585 ret = get_credentials( ctx, ctx->smbldap_state, config_option,
586 dom, &ctx->user_dn );
587 if ( !NT_STATUS_IS_OK(ret) ) {
588 DEBUG(1,("idmap_ldap_db_init: Failed to get connection "
589 "credentials (%s)\n", nt_errstr(ret)));
593 /* set the destructor on the context, so that resource are properly
594 freed if the contexts is released */
596 talloc_set_destructor(ctx, idmap_ldap_close_destructor);
598 dom->private_data = ctx;
600 talloc_free(config_option);
609 /* max number of ids requested per batch query */
610 #define IDMAP_LDAP_MAX_IDS 30
612 /**********************************
613 lookup a set of unix ids.
614 **********************************/
616 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
617 * in maps for a match */
618 static struct id_map *find_map_by_id(struct id_map **maps,
624 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
625 if (maps[i] == NULL) { /* end of the run */
628 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
636 static NTSTATUS idmap_ldap_unixids_to_sids(struct idmap_domain *dom,
641 struct idmap_ldap_context *ctx;
642 LDAPMessage *result = NULL;
643 LDAPMessage *entry = NULL;
644 const char *uidNumber;
645 const char *gidNumber;
646 const char **attr_list;
655 /* Only do query if we are online */
656 if (idmap_is_offline()) {
657 return NT_STATUS_FILE_IS_OFFLINE;
660 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
662 memctx = talloc_new(ctx);
664 DEBUG(0, ("Out of memory!\n"));
665 return NT_STATUS_NO_MEMORY;
668 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
669 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
671 attr_list = get_attr_list(memctx, sidmap_attr_list);
674 /* if we are requested just one mapping use the simple filter */
676 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%lu))",
677 LDAP_OBJ_IDMAP_ENTRY,
678 (ids[0]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
679 (unsigned long)ids[0]->xid.id);
680 CHECK_ALLOC_DONE(filter);
681 DEBUG(10, ("Filter: [%s]\n", filter));
683 /* multiple mappings */
687 for (i = 0; ids[i]; i++) {
688 ids[i]->status = ID_UNKNOWN;
695 filter = talloc_asprintf(memctx,
696 "(&(objectClass=%s)(|",
697 LDAP_OBJ_IDMAP_ENTRY);
698 CHECK_ALLOC_DONE(filter);
701 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
702 filter = talloc_asprintf_append_buffer(filter, "(%s=%lu)",
703 (ids[idx]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
704 (unsigned long)ids[idx]->xid.id);
705 CHECK_ALLOC_DONE(filter);
707 filter = talloc_asprintf_append_buffer(filter, "))");
708 CHECK_ALLOC_DONE(filter);
709 DEBUG(10, ("Filter: [%s]\n", filter));
715 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
716 filter, attr_list, 0, &result);
718 if (rc != LDAP_SUCCESS) {
719 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc)));
720 ret = NT_STATUS_UNSUCCESSFUL;
724 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
727 DEBUG(10, ("NO SIDs found\n"));
730 for (i = 0; i < count; i++) {
737 if (i == 0) { /* first entry */
738 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
740 } else { /* following ones */
741 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
745 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
750 /* first check if the SID is present */
751 sidstr = smbldap_talloc_single_attribute(
752 ctx->smbldap_state->ldap_struct,
753 entry, LDAP_ATTRIBUTE_SID, memctx);
754 if ( ! sidstr) { /* no sid, skip entry */
755 DEBUG(2, ("WARNING SID not found on entry\n"));
759 /* now try to see if it is a uid, if not try with a gid
760 * (gid is more common, but in case both uidNumber and
761 * gidNumber are returned the SID is mapped to the uid
764 tmp = smbldap_talloc_single_attribute(
765 ctx->smbldap_state->ldap_struct,
766 entry, uidNumber, memctx);
769 tmp = smbldap_talloc_single_attribute(
770 ctx->smbldap_state->ldap_struct,
771 entry, gidNumber, memctx);
773 if ( ! tmp) { /* wow very strange entry, how did it match ? */
774 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
775 "nor gidNumber returned\n", sidstr));
780 id = strtoul(tmp, NULL, 10);
781 if (!idmap_unix_id_is_in_range(id, dom)) {
782 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
784 dom->low_id, dom->high_id));
791 map = find_map_by_id(&ids[bidx], type, id);
793 DEBUG(2, ("WARNING: couldn't match sid (%s) "
794 "with requested ids\n", sidstr));
799 if ( ! string_to_sid(map->sid, sidstr)) {
800 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
805 if (map->status == ID_MAPPED) {
806 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
807 "overwriting mapping %u -> %s with %u -> %s\n",
808 (type == ID_TYPE_UID) ? "UID" : "GID",
809 id, sid_string_dbg(map->sid), id, sidstr));
815 map->status = ID_MAPPED;
817 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
818 (unsigned long)map->xid.id, map->xid.type));
821 /* free the ldap results */
823 ldap_msgfree(result);
827 if (multi && ids[idx]) { /* still some values to map */
833 /* mark all unknwon/expired ones as unmapped */
834 for (i = 0; ids[i]; i++) {
835 if (ids[i]->status != ID_MAPPED)
836 ids[i]->status = ID_UNMAPPED;
844 /**********************************
845 lookup a set of sids.
846 **********************************/
848 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
849 * in maps for a match */
850 static struct id_map *find_map_by_sid(struct id_map **maps, DOM_SID *sid)
854 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
855 if (maps[i] == NULL) { /* end of the run */
858 if (sid_equal(maps[i]->sid, sid)) {
866 static NTSTATUS idmap_ldap_sids_to_unixids(struct idmap_domain *dom,
869 LDAPMessage *entry = NULL;
872 struct idmap_ldap_context *ctx;
873 LDAPMessage *result = NULL;
874 const char *uidNumber;
875 const char *gidNumber;
876 const char **attr_list;
885 /* Only do query if we are online */
886 if (idmap_is_offline()) {
887 return NT_STATUS_FILE_IS_OFFLINE;
890 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
892 memctx = talloc_new(ctx);
894 DEBUG(0, ("Out of memory!\n"));
895 return NT_STATUS_NO_MEMORY;
898 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
899 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
901 attr_list = get_attr_list(memctx, sidmap_attr_list);
904 /* if we are requested just one mapping use the simple filter */
906 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%s))",
907 LDAP_OBJ_IDMAP_ENTRY,
909 sid_string_talloc(memctx, ids[0]->sid));
910 CHECK_ALLOC_DONE(filter);
911 DEBUG(10, ("Filter: [%s]\n", filter));
913 /* multiple mappings */
917 for (i = 0; ids[i]; i++) {
918 ids[i]->status = ID_UNKNOWN;
925 filter = talloc_asprintf(memctx,
926 "(&(objectClass=%s)(|",
927 LDAP_OBJ_IDMAP_ENTRY);
928 CHECK_ALLOC_DONE(filter);
931 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
932 filter = talloc_asprintf_append_buffer(filter, "(%s=%s)",
934 sid_string_talloc(memctx,
936 CHECK_ALLOC_DONE(filter);
938 filter = talloc_asprintf_append_buffer(filter, "))");
939 CHECK_ALLOC_DONE(filter);
940 DEBUG(10, ("Filter: [%s]", filter));
946 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
947 filter, attr_list, 0, &result);
949 if (rc != LDAP_SUCCESS) {
950 DEBUG(3,("Failure looking up sids (%s)\n",
951 ldap_err2string(rc)));
952 ret = NT_STATUS_UNSUCCESSFUL;
956 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
959 DEBUG(10, ("NO SIDs found\n"));
962 for (i = 0; i < count; i++) {
970 if (i == 0) { /* first entry */
971 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
973 } else { /* following ones */
974 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
978 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
983 /* first check if the SID is present */
984 sidstr = smbldap_talloc_single_attribute(
985 ctx->smbldap_state->ldap_struct,
986 entry, LDAP_ATTRIBUTE_SID, memctx);
987 if ( ! sidstr) { /* no sid ??, skip entry */
988 DEBUG(2, ("WARNING SID not found on entry\n"));
992 if ( ! string_to_sid(&sid, sidstr)) {
993 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
998 map = find_map_by_sid(&ids[bidx], &sid);
1000 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
1002 TALLOC_FREE(sidstr);
1006 /* now try to see if it is a uid, if not try with a gid
1007 * (gid is more common, but in case both uidNumber and
1008 * gidNumber are returned the SID is mapped to the uid
1011 tmp = smbldap_talloc_single_attribute(
1012 ctx->smbldap_state->ldap_struct,
1013 entry, uidNumber, memctx);
1016 tmp = smbldap_talloc_single_attribute(
1017 ctx->smbldap_state->ldap_struct,
1018 entry, gidNumber, memctx);
1020 if ( ! tmp) { /* no ids ?? */
1021 DEBUG(5, ("no uidNumber, "
1022 "nor gidNumber attributes found\n"));
1023 TALLOC_FREE(sidstr);
1027 id = strtoul(tmp, NULL, 10);
1028 if (!idmap_unix_id_is_in_range(id, dom)) {
1029 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1031 dom->low_id, dom->high_id));
1032 TALLOC_FREE(sidstr);
1038 if (map->status == ID_MAPPED) {
1039 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1040 "overwriting mapping %s -> %u with %s -> %u\n",
1041 (type == ID_TYPE_UID) ? "UID" : "GID",
1042 sidstr, map->xid.id, sidstr, id));
1045 TALLOC_FREE(sidstr);
1048 map->xid.type = type;
1050 map->status = ID_MAPPED;
1052 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1053 (unsigned long)map->xid.id, map->xid.type));
1056 /* free the ldap results */
1058 ldap_msgfree(result);
1062 if (multi && ids[idx]) { /* still some values to map */
1068 /* mark all unknwon/expired ones as unmapped */
1069 for (i = 0; ids[i]; i++) {
1070 if (ids[i]->status != ID_MAPPED)
1071 ids[i]->status = ID_UNMAPPED;
1075 talloc_free(memctx);
1079 /**********************************
1081 **********************************/
1083 /* TODO: change this: This function cannot be called to modify a mapping,
1084 * only set a new one */
1086 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
1087 const struct id_map *map)
1091 struct idmap_ldap_context *ctx;
1092 LDAPMessage *entry = NULL;
1093 LDAPMod **mods = NULL;
1100 /* Only do query if we are online */
1101 if (idmap_is_offline()) {
1102 return NT_STATUS_FILE_IS_OFFLINE;
1105 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
1107 switch(map->xid.type) {
1109 type = get_attr_key2string(sidmap_attr_list,
1110 LDAP_ATTR_UIDNUMBER);
1114 type = get_attr_key2string(sidmap_attr_list,
1115 LDAP_ATTR_GIDNUMBER);
1119 return NT_STATUS_INVALID_PARAMETER;
1122 memctx = talloc_new(ctx);
1124 DEBUG(0, ("Out of memory!\n"));
1125 return NT_STATUS_NO_MEMORY;
1128 id_str = talloc_asprintf(memctx, "%lu", (unsigned long)map->xid.id);
1129 CHECK_ALLOC_DONE(id_str);
1131 sid = talloc_strdup(memctx, sid_string_talloc(memctx, map->sid));
1132 CHECK_ALLOC_DONE(sid);
1134 dn = talloc_asprintf(memctx, "%s=%s,%s",
1135 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1138 CHECK_ALLOC_DONE(dn);
1140 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1141 "objectClass", LDAP_OBJ_IDMAP_ENTRY);
1143 smbldap_make_mod(ctx->smbldap_state->ldap_struct,
1144 entry, &mods, type, id_str);
1146 smbldap_make_mod(ctx->smbldap_state->ldap_struct, entry, &mods,
1147 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1151 DEBUG(2, ("ERROR: No mods?\n"));
1152 ret = NT_STATUS_UNSUCCESSFUL;
1156 /* TODO: remove conflicting mappings! */
1158 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SID_ENTRY);
1160 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn, sid, id_str));
1162 rc = smbldap_add(ctx->smbldap_state, dn, mods);
1163 ldap_mods_free(mods, True);
1165 if (rc != LDAP_SUCCESS) {
1166 char *ld_error = NULL;
1167 ldap_get_option(ctx->smbldap_state->ldap_struct,
1168 LDAP_OPT_ERROR_STRING, &ld_error);
1169 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
1170 "mapping [%s]\n", sid,
1171 (unsigned long)map->xid.id, type));
1172 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
1173 ld_error ? ld_error : "(NULL)", ldap_err2string (rc)));
1175 ldap_memfree(ld_error);
1177 ret = NT_STATUS_UNSUCCESSFUL;
1181 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
1182 "%lu [%s]\n", sid, (unsigned long)map->xid.id, type));
1187 talloc_free(memctx);
1191 /**********************************
1192 Close the idmap ldap instance
1193 **********************************/
1195 static NTSTATUS idmap_ldap_close(struct idmap_domain *dom)
1197 struct idmap_ldap_context *ctx;
1199 if (dom->private_data) {
1200 ctx = talloc_get_type(dom->private_data,
1201 struct idmap_ldap_context);
1204 dom->private_data = NULL;
1207 return NT_STATUS_OK;
1210 static struct idmap_methods idmap_ldap_methods = {
1212 .init = idmap_ldap_db_init,
1213 .unixids_to_sids = idmap_ldap_unixids_to_sids,
1214 .sids_to_unixids = idmap_ldap_sids_to_unixids,
1215 .allocate_id = idmap_ldap_get_new_id,
1216 .close_fn = idmap_ldap_close
1219 NTSTATUS idmap_ldap_init(void);
1220 NTSTATUS idmap_ldap_init(void)
1222 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1223 &idmap_ldap_methods);