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/>.
30 #define DBGC_CLASS DBGC_IDMAP
37 static char *idmap_fetch_secret(const char *backend, bool alloc,
38 const char *domain, const char *identity)
44 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
46 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
52 strupper_m(tmp); /* make sure the key is case insensitive */
53 ret = secrets_fetch_generic(tmp, identity);
60 struct idmap_ldap_context {
61 struct smbldap_state *smbldap_state;
68 struct idmap_ldap_alloc_context {
69 struct smbldap_state *smbldap_state;
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 static struct idmap_ldap_alloc_context *idmap_alloc_ldap;
88 /*********************************************************************
89 ********************************************************************/
91 static NTSTATUS get_credentials( TALLOC_CTX *mem_ctx,
92 struct smbldap_state *ldap_state,
93 const char *config_option,
94 struct idmap_domain *dom,
97 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
99 const char *tmp = NULL;
100 char *user_dn = NULL;
103 /* assume anonymous if we don't have a specified user */
105 tmp = lp_parm_const_string(-1, config_option, "ldap_user_dn", NULL);
109 /* only the alloc backend can pass in a NULL dom */
110 secret = idmap_fetch_secret("ldap", True,
113 secret = idmap_fetch_secret("ldap", False,
118 DEBUG(0, ("get_credentials: Unable to fetch "
119 "auth credentials for %s in %s\n",
120 tmp, (dom==NULL)?"ALLOC":dom->name));
121 ret = NT_STATUS_ACCESS_DENIED;
124 *dn = talloc_strdup(mem_ctx, tmp);
125 CHECK_ALLOC_DONE(*dn);
127 if (!fetch_ldap_pw(&user_dn, &secret)) {
128 DEBUG(2, ("get_credentials: Failed to lookup ldap "
129 "bind creds. Using anonymous connection.\n"));
133 *dn = talloc_strdup(mem_ctx, user_dn);
134 SAFE_FREE( user_dn );
135 CHECK_ALLOC_DONE(*dn);
139 smbldap_set_creds(ldap_state, anon, *dn, secret);
149 /**********************************************************************
150 Verify the sambaUnixIdPool entry in the directory.
151 **********************************************************************/
153 static NTSTATUS verify_idpool(struct idmap_domain *dom)
157 LDAPMessage *result = NULL;
158 LDAPMod **mods = NULL;
159 const char **attr_list;
164 if ( ! idmap_alloc_ldap) {
165 return NT_STATUS_UNSUCCESSFUL;
168 ctx = talloc_new(idmap_alloc_ldap);
170 DEBUG(0, ("Out of memory!\n"));
171 return NT_STATUS_NO_MEMORY;
174 filter = talloc_asprintf(ctx, "(objectclass=%s)", LDAP_OBJ_IDPOOL);
175 CHECK_ALLOC_DONE(filter);
177 attr_list = get_attr_list(ctx, idpool_attr_list);
178 CHECK_ALLOC_DONE(attr_list);
180 rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
181 idmap_alloc_ldap->suffix,
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(idmap_alloc_ldap->smbldap_state->ldap_struct,
197 ldap_msgfree(result);
200 DEBUG(0,("Multiple entries returned from %s (base == %s)\n",
201 filter, idmap_alloc_ldap->suffix));
202 ret = NT_STATUS_UNSUCCESSFUL;
205 else if (count == 0) {
206 char *uid_str, *gid_str;
208 uid_str = talloc_asprintf(ctx, "%lu",
209 (unsigned long)dom->low_id);
210 gid_str = talloc_asprintf(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(idmap_alloc_ldap->smbldap_state,
225 idmap_alloc_ldap->suffix,
227 ldap_mods_free(mods, True);
229 ret = NT_STATUS_UNSUCCESSFUL;
234 ret = (rc == LDAP_SUCCESS)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
240 /*****************************************************************************
241 Initialise idmap database.
242 *****************************************************************************/
244 static NTSTATUS idmap_ldap_alloc_init(struct idmap_domain *dom,
247 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
250 /* Only do init if we are online */
251 if (idmap_is_offline()) {
252 return NT_STATUS_FILE_IS_OFFLINE;
255 idmap_alloc_ldap = TALLOC_ZERO_P(NULL, struct idmap_ldap_alloc_context);
256 CHECK_ALLOC_DONE( idmap_alloc_ldap );
258 if (params && *params) {
259 /* assume location is the only parameter */
260 idmap_alloc_ldap->url = talloc_strdup(idmap_alloc_ldap, params);
262 tmp = lp_parm_const_string(-1, "idmap alloc config",
266 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
267 ret = NT_STATUS_UNSUCCESSFUL;
271 idmap_alloc_ldap->url = talloc_strdup(idmap_alloc_ldap, tmp);
273 CHECK_ALLOC_DONE( idmap_alloc_ldap->url );
275 trim_char(idmap_alloc_ldap->url, '\"', '\"');
277 tmp = lp_parm_const_string(-1, "idmap alloc config",
278 "ldap_base_dn", NULL);
279 if ( ! tmp || ! *tmp) {
280 tmp = lp_ldap_idmap_suffix();
282 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
283 ret = NT_STATUS_UNSUCCESSFUL;
288 idmap_alloc_ldap->suffix = talloc_strdup(idmap_alloc_ldap, tmp);
289 CHECK_ALLOC_DONE( idmap_alloc_ldap->suffix );
291 ret = smbldap_init(idmap_alloc_ldap, winbind_event_context(),
292 idmap_alloc_ldap->url,
293 &idmap_alloc_ldap->smbldap_state);
294 if (!NT_STATUS_IS_OK(ret)) {
295 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n",
296 idmap_alloc_ldap->url));
300 ret = get_credentials( idmap_alloc_ldap,
301 idmap_alloc_ldap->smbldap_state,
302 "idmap alloc config", NULL,
303 &idmap_alloc_ldap->user_dn );
304 if ( !NT_STATUS_IS_OK(ret) ) {
305 DEBUG(1,("idmap_ldap_alloc_init: Failed to get connection "
306 "credentials (%s)\n", nt_errstr(ret)));
310 /* see if the idmap suffix and sub entries exists */
312 ret = verify_idpool(dom);
315 if ( !NT_STATUS_IS_OK( ret ) )
316 TALLOC_FREE( idmap_alloc_ldap );
321 /********************************
322 Allocate a new uid or gid
323 ********************************/
325 static NTSTATUS idmap_ldap_allocate_id(struct idmap_domain *dom,
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));
415 ret = NT_STATUS_UNSUCCESSFUL;
419 xid->id = strtoul(id_str, NULL, 10);
421 /* make sure we still have room to grow */
425 if (xid->id > dom->high_id) {
426 DEBUG(0,("Cannot allocate uid above %lu!\n",
427 (unsigned long)dom->high_id));
433 if (xid->id > dom->high_id) {
434 DEBUG(0,("Cannot allocate gid above %lu!\n",
435 (unsigned long)dom->high_id));
445 new_id_str = talloc_asprintf(ctx, "%lu", (unsigned long)xid->id + 1);
447 DEBUG(0,("Out of memory\n"));
448 ret = NT_STATUS_NO_MEMORY;
452 smbldap_set_mod(&mods, LDAP_MOD_DELETE, type, id_str);
453 smbldap_set_mod(&mods, LDAP_MOD_ADD, type, new_id_str);
456 DEBUG(0,("smbldap_set_mod() failed.\n"));
460 DEBUG(10, ("Try to atomically increment the id (%s -> %s)\n",
461 id_str, new_id_str));
463 rc = smbldap_modify(idmap_alloc_ldap->smbldap_state, dn, mods);
465 ldap_mods_free(mods, True);
467 if (rc != LDAP_SUCCESS) {
468 DEBUG(1,("Failed to allocate new %s. "
469 "smbldap_modify() failed.\n", type));
480 /**********************************
481 Close idmap ldap alloc
482 **********************************/
484 static NTSTATUS idmap_ldap_alloc_close(void)
486 if (idmap_alloc_ldap) {
487 smbldap_free_struct(&idmap_alloc_ldap->smbldap_state);
488 DEBUG(5,("The connection to the LDAP server was closed\n"));
489 /* maybe free the results here --metze */
490 TALLOC_FREE(idmap_alloc_ldap);
496 /**********************************************************************
497 IDMAP MAPPING LDAP BACKEND
498 **********************************************************************/
500 static int idmap_ldap_close_destructor(struct idmap_ldap_context *ctx)
502 smbldap_free_struct(&ctx->smbldap_state);
503 DEBUG(5,("The connection to the LDAP server was closed\n"));
504 /* maybe free the results here --metze */
509 /********************************
510 Initialise idmap database.
511 ********************************/
513 static NTSTATUS idmap_ldap_db_init(struct idmap_domain *dom,
517 struct idmap_ldap_context *ctx = NULL;
518 char *config_option = NULL;
519 const char *tmp = NULL;
521 /* Only do init if we are online */
522 if (idmap_is_offline()) {
523 return NT_STATUS_FILE_IS_OFFLINE;
526 ctx = TALLOC_ZERO_P(dom, struct idmap_ldap_context);
528 DEBUG(0, ("Out of memory!\n"));
529 return NT_STATUS_NO_MEMORY;
532 if (strequal(dom->name, "*")) {
533 /* more specific configuration can go here */
535 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
536 if ( ! config_option) {
537 DEBUG(0, ("Out of memory!\n"));
538 ret = NT_STATUS_NO_MEMORY;
543 if (params != NULL) {
544 /* assume location is the only parameter */
545 ctx->url = talloc_strdup(ctx, params);
547 tmp = lp_parm_const_string(-1, config_option, "ldap_url", NULL);
550 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
551 ret = NT_STATUS_UNSUCCESSFUL;
555 ctx->url = talloc_strdup(ctx, tmp);
557 CHECK_ALLOC_DONE(ctx->url);
559 trim_char(ctx->url, '\"', '\"');
561 tmp = lp_parm_const_string(-1, config_option, "ldap_base_dn", NULL);
562 if ( ! tmp || ! *tmp) {
563 tmp = lp_ldap_idmap_suffix();
565 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
566 ret = NT_STATUS_UNSUCCESSFUL;
571 ctx->suffix = talloc_strdup(ctx, tmp);
572 CHECK_ALLOC_DONE(ctx->suffix);
574 ret = smbldap_init(ctx, winbind_event_context(), ctx->url,
575 &ctx->smbldap_state);
576 if (!NT_STATUS_IS_OK(ret)) {
577 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", ctx->url));
581 ret = get_credentials( ctx, ctx->smbldap_state, config_option,
582 dom, &ctx->user_dn );
583 if ( !NT_STATUS_IS_OK(ret) ) {
584 DEBUG(1,("idmap_ldap_db_init: Failed to get connection "
585 "credentials (%s)\n", nt_errstr(ret)));
589 /* set the destructor on the context, so that resource are properly
590 freed if the contexts is released */
592 talloc_set_destructor(ctx, idmap_ldap_close_destructor);
594 dom->private_data = ctx;
596 ret = idmap_ldap_alloc_init(dom, params);
597 if (!NT_STATUS_IS_OK(ret)) {
598 DEBUG(1, ("idmap_ldap_db_init: Failed to initialize alloc "
599 "subsystem: %s\n", nt_errstr(ret)));
603 talloc_free(config_option);
612 /* max number of ids requested per batch query */
613 #define IDMAP_LDAP_MAX_IDS 30
615 /**********************************
616 lookup a set of unix ids.
617 **********************************/
619 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
620 * in maps for a match */
621 static struct id_map *find_map_by_id(struct id_map **maps,
627 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
628 if (maps[i] == NULL) { /* end of the run */
631 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
639 static NTSTATUS idmap_ldap_unixids_to_sids(struct idmap_domain *dom,
644 struct idmap_ldap_context *ctx;
645 LDAPMessage *result = NULL;
646 LDAPMessage *entry = NULL;
647 const char *uidNumber;
648 const char *gidNumber;
649 const char **attr_list;
658 /* Only do query if we are online */
659 if (idmap_is_offline()) {
660 return NT_STATUS_FILE_IS_OFFLINE;
663 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
665 memctx = talloc_new(ctx);
667 DEBUG(0, ("Out of memory!\n"));
668 return NT_STATUS_NO_MEMORY;
671 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
672 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
674 attr_list = get_attr_list(memctx, sidmap_attr_list);
677 /* if we are requested just one mapping use the simple filter */
679 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%lu))",
680 LDAP_OBJ_IDMAP_ENTRY,
681 (ids[0]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
682 (unsigned long)ids[0]->xid.id);
683 CHECK_ALLOC_DONE(filter);
684 DEBUG(10, ("Filter: [%s]\n", filter));
686 /* multiple mappings */
690 for (i = 0; ids[i]; i++) {
691 ids[i]->status = ID_UNKNOWN;
698 filter = talloc_asprintf(memctx,
699 "(&(objectClass=%s)(|",
700 LDAP_OBJ_IDMAP_ENTRY);
701 CHECK_ALLOC_DONE(filter);
704 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
705 filter = talloc_asprintf_append_buffer(filter, "(%s=%lu)",
706 (ids[idx]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
707 (unsigned long)ids[idx]->xid.id);
708 CHECK_ALLOC_DONE(filter);
710 filter = talloc_asprintf_append_buffer(filter, "))");
711 CHECK_ALLOC_DONE(filter);
712 DEBUG(10, ("Filter: [%s]\n", filter));
718 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
719 filter, attr_list, 0, &result);
721 if (rc != LDAP_SUCCESS) {
722 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc)));
723 ret = NT_STATUS_UNSUCCESSFUL;
727 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
730 DEBUG(10, ("NO SIDs found\n"));
733 for (i = 0; i < count; i++) {
740 if (i == 0) { /* first entry */
741 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
743 } else { /* following ones */
744 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
748 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
753 /* first check if the SID is present */
754 sidstr = smbldap_talloc_single_attribute(
755 ctx->smbldap_state->ldap_struct,
756 entry, LDAP_ATTRIBUTE_SID, memctx);
757 if ( ! sidstr) { /* no sid, skip entry */
758 DEBUG(2, ("WARNING SID not found on entry\n"));
762 /* now try to see if it is a uid, if not try with a gid
763 * (gid is more common, but in case both uidNumber and
764 * gidNumber are returned the SID is mapped to the uid
767 tmp = smbldap_talloc_single_attribute(
768 ctx->smbldap_state->ldap_struct,
769 entry, uidNumber, memctx);
772 tmp = smbldap_talloc_single_attribute(
773 ctx->smbldap_state->ldap_struct,
774 entry, gidNumber, memctx);
776 if ( ! tmp) { /* wow very strange entry, how did it match ? */
777 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
778 "nor gidNumber returned\n", sidstr));
783 id = strtoul(tmp, NULL, 10);
784 if (!idmap_unix_id_is_in_range(id, dom)) {
785 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
787 dom->low_id, dom->high_id));
794 map = find_map_by_id(&ids[bidx], type, id);
796 DEBUG(2, ("WARNING: couldn't match sid (%s) "
797 "with requested ids\n", sidstr));
802 if ( ! string_to_sid(map->sid, sidstr)) {
803 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
808 if (map->status == ID_MAPPED) {
809 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
810 "overwriting mapping %u -> %s with %u -> %s\n",
811 (type == ID_TYPE_UID) ? "UID" : "GID",
812 id, sid_string_dbg(map->sid), id, sidstr));
818 map->status = ID_MAPPED;
820 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
821 (unsigned long)map->xid.id, map->xid.type));
824 /* free the ldap results */
826 ldap_msgfree(result);
830 if (multi && ids[idx]) { /* still some values to map */
836 /* mark all unknwon/expired ones as unmapped */
837 for (i = 0; ids[i]; i++) {
838 if (ids[i]->status != ID_MAPPED)
839 ids[i]->status = ID_UNMAPPED;
847 /**********************************
848 lookup a set of sids.
849 **********************************/
851 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
852 * in maps for a match */
853 static struct id_map *find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
857 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
858 if (maps[i] == NULL) { /* end of the run */
861 if (sid_equal(maps[i]->sid, sid)) {
869 static NTSTATUS idmap_ldap_sids_to_unixids(struct idmap_domain *dom,
872 LDAPMessage *entry = NULL;
875 struct idmap_ldap_context *ctx;
876 LDAPMessage *result = NULL;
877 const char *uidNumber;
878 const char *gidNumber;
879 const char **attr_list;
888 /* Only do query if we are online */
889 if (idmap_is_offline()) {
890 return NT_STATUS_FILE_IS_OFFLINE;
893 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
895 memctx = talloc_new(ctx);
897 DEBUG(0, ("Out of memory!\n"));
898 return NT_STATUS_NO_MEMORY;
901 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
902 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
904 attr_list = get_attr_list(memctx, sidmap_attr_list);
907 /* if we are requested just one mapping use the simple filter */
909 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%s))",
910 LDAP_OBJ_IDMAP_ENTRY,
912 sid_string_talloc(memctx, ids[0]->sid));
913 CHECK_ALLOC_DONE(filter);
914 DEBUG(10, ("Filter: [%s]\n", filter));
916 /* multiple mappings */
920 for (i = 0; ids[i]; i++) {
921 ids[i]->status = ID_UNKNOWN;
928 filter = talloc_asprintf(memctx,
929 "(&(objectClass=%s)(|",
930 LDAP_OBJ_IDMAP_ENTRY);
931 CHECK_ALLOC_DONE(filter);
934 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
935 filter = talloc_asprintf_append_buffer(filter, "(%s=%s)",
937 sid_string_talloc(memctx,
939 CHECK_ALLOC_DONE(filter);
941 filter = talloc_asprintf_append_buffer(filter, "))");
942 CHECK_ALLOC_DONE(filter);
943 DEBUG(10, ("Filter: [%s]", filter));
949 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
950 filter, attr_list, 0, &result);
952 if (rc != LDAP_SUCCESS) {
953 DEBUG(3,("Failure looking up sids (%s)\n",
954 ldap_err2string(rc)));
955 ret = NT_STATUS_UNSUCCESSFUL;
959 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
962 DEBUG(10, ("NO SIDs found\n"));
965 for (i = 0; i < count; i++) {
973 if (i == 0) { /* first entry */
974 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
976 } else { /* following ones */
977 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
981 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
986 /* first check if the SID is present */
987 sidstr = smbldap_talloc_single_attribute(
988 ctx->smbldap_state->ldap_struct,
989 entry, LDAP_ATTRIBUTE_SID, memctx);
990 if ( ! sidstr) { /* no sid ??, skip entry */
991 DEBUG(2, ("WARNING SID not found on entry\n"));
995 if ( ! string_to_sid(&sid, sidstr)) {
996 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
1001 map = find_map_by_sid(&ids[bidx], &sid);
1003 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
1005 TALLOC_FREE(sidstr);
1009 /* now try to see if it is a uid, if not try with a gid
1010 * (gid is more common, but in case both uidNumber and
1011 * gidNumber are returned the SID is mapped to the uid
1014 tmp = smbldap_talloc_single_attribute(
1015 ctx->smbldap_state->ldap_struct,
1016 entry, uidNumber, memctx);
1019 tmp = smbldap_talloc_single_attribute(
1020 ctx->smbldap_state->ldap_struct,
1021 entry, gidNumber, memctx);
1023 if ( ! tmp) { /* no ids ?? */
1024 DEBUG(5, ("no uidNumber, "
1025 "nor gidNumber attributes found\n"));
1026 TALLOC_FREE(sidstr);
1030 id = strtoul(tmp, NULL, 10);
1031 if (!idmap_unix_id_is_in_range(id, dom)) {
1032 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1034 dom->low_id, dom->high_id));
1035 TALLOC_FREE(sidstr);
1041 if (map->status == ID_MAPPED) {
1042 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1043 "overwriting mapping %s -> %u with %s -> %u\n",
1044 (type == ID_TYPE_UID) ? "UID" : "GID",
1045 sidstr, map->xid.id, sidstr, id));
1048 TALLOC_FREE(sidstr);
1051 map->xid.type = type;
1053 map->status = ID_MAPPED;
1055 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1056 (unsigned long)map->xid.id, map->xid.type));
1059 /* free the ldap results */
1061 ldap_msgfree(result);
1065 if (multi && ids[idx]) { /* still some values to map */
1071 /* mark all unknwon/expired ones as unmapped */
1072 for (i = 0; ids[i]; i++) {
1073 if (ids[i]->status != ID_MAPPED)
1074 ids[i]->status = ID_UNMAPPED;
1078 talloc_free(memctx);
1082 /**********************************
1084 **********************************/
1086 /* TODO: change this: This function cannot be called to modify a mapping,
1087 * only set a new one */
1089 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
1090 const struct id_map *map)
1094 struct idmap_ldap_context *ctx;
1095 LDAPMessage *entry = NULL;
1096 LDAPMod **mods = NULL;
1103 /* Only do query if we are online */
1104 if (idmap_is_offline()) {
1105 return NT_STATUS_FILE_IS_OFFLINE;
1108 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
1110 switch(map->xid.type) {
1112 type = get_attr_key2string(sidmap_attr_list,
1113 LDAP_ATTR_UIDNUMBER);
1117 type = get_attr_key2string(sidmap_attr_list,
1118 LDAP_ATTR_GIDNUMBER);
1122 return NT_STATUS_INVALID_PARAMETER;
1125 memctx = talloc_new(ctx);
1127 DEBUG(0, ("Out of memory!\n"));
1128 return NT_STATUS_NO_MEMORY;
1131 id_str = talloc_asprintf(memctx, "%lu", (unsigned long)map->xid.id);
1132 CHECK_ALLOC_DONE(id_str);
1134 sid = talloc_strdup(memctx, sid_string_talloc(memctx, map->sid));
1135 CHECK_ALLOC_DONE(sid);
1137 dn = talloc_asprintf(memctx, "%s=%s,%s",
1138 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1141 CHECK_ALLOC_DONE(dn);
1143 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1144 "objectClass", LDAP_OBJ_IDMAP_ENTRY);
1146 smbldap_make_mod(ctx->smbldap_state->ldap_struct,
1147 entry, &mods, type, id_str);
1149 smbldap_make_mod(ctx->smbldap_state->ldap_struct, entry, &mods,
1150 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1154 DEBUG(2, ("ERROR: No mods?\n"));
1155 ret = NT_STATUS_UNSUCCESSFUL;
1159 /* TODO: remove conflicting mappings! */
1161 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SID_ENTRY);
1163 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn, sid, id_str));
1165 rc = smbldap_add(ctx->smbldap_state, dn, mods);
1166 ldap_mods_free(mods, True);
1168 if (rc != LDAP_SUCCESS) {
1169 char *ld_error = NULL;
1170 ldap_get_option(ctx->smbldap_state->ldap_struct,
1171 LDAP_OPT_ERROR_STRING, &ld_error);
1172 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
1173 "mapping [%s]\n", sid,
1174 (unsigned long)map->xid.id, type));
1175 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
1176 ld_error ? ld_error : "(NULL)", ldap_err2string (rc)));
1178 ldap_memfree(ld_error);
1180 ret = NT_STATUS_UNSUCCESSFUL;
1184 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
1185 "%lu [%s]\n", sid, (unsigned long)map->xid.id, type));
1190 talloc_free(memctx);
1194 /**********************************
1195 Close the idmap ldap instance
1196 **********************************/
1198 static NTSTATUS idmap_ldap_close(struct idmap_domain *dom)
1200 struct idmap_ldap_context *ctx;
1202 if (dom->private_data) {
1203 ctx = talloc_get_type(dom->private_data,
1204 struct idmap_ldap_context);
1207 dom->private_data = NULL;
1210 return NT_STATUS_OK;
1213 static struct idmap_methods idmap_ldap_methods = {
1215 .init = idmap_ldap_db_init,
1216 .unixids_to_sids = idmap_ldap_unixids_to_sids,
1217 .sids_to_unixids = idmap_ldap_sids_to_unixids,
1218 .allocate_id = idmap_ldap_get_new_id,
1219 .close_fn = idmap_ldap_close
1222 NTSTATUS idmap_ldap_init(void);
1223 NTSTATUS idmap_ldap_init(void)
1225 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1226 &idmap_ldap_methods);