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;
65 uint32_t filter_low_id, filter_high_id; /* Filter range */
69 struct idmap_ldap_alloc_context {
70 struct smbldap_state *smbldap_state;
74 uid_t low_uid, high_uid; /* Range of uids */
75 gid_t low_gid, high_gid; /* Range of gids */
79 #define CHECK_ALLOC_DONE(mem) do { \
81 DEBUG(0, ("Out of memory!\n")); \
82 ret = NT_STATUS_NO_MEMORY; \
86 /**********************************************************************
87 IDMAP ALLOC TDB BACKEND
88 **********************************************************************/
90 static struct idmap_ldap_alloc_context *idmap_alloc_ldap;
92 /*********************************************************************
93 ********************************************************************/
95 static NTSTATUS get_credentials( TALLOC_CTX *mem_ctx,
96 struct smbldap_state *ldap_state,
97 const char *config_option,
98 struct idmap_domain *dom,
101 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
103 const char *tmp = NULL;
104 char *user_dn = NULL;
107 /* assume anonymous if we don't have a specified user */
109 tmp = lp_parm_const_string(-1, config_option, "ldap_user_dn", NULL);
113 /* only the alloc backend can pass in a NULL dom */
114 secret = idmap_fetch_secret("ldap", True,
117 secret = idmap_fetch_secret("ldap", False,
122 DEBUG(0, ("get_credentials: Unable to fetch "
123 "auth credentials for %s in %s\n",
124 tmp, (dom==NULL)?"ALLOC":dom->name));
125 ret = NT_STATUS_ACCESS_DENIED;
128 *dn = talloc_strdup(mem_ctx, tmp);
129 CHECK_ALLOC_DONE(*dn);
131 if (!fetch_ldap_pw(&user_dn, &secret)) {
132 DEBUG(2, ("get_credentials: Failed to lookup ldap "
133 "bind creds. Using anonymous connection.\n"));
137 *dn = talloc_strdup(mem_ctx, user_dn);
138 SAFE_FREE( user_dn );
139 CHECK_ALLOC_DONE(*dn);
143 smbldap_set_creds(ldap_state, anon, *dn, secret);
153 /**********************************************************************
154 Verify the sambaUnixIdPool entry in the directory.
155 **********************************************************************/
157 static NTSTATUS verify_idpool(void)
161 LDAPMessage *result = NULL;
162 LDAPMod **mods = NULL;
163 const char **attr_list;
168 if ( ! idmap_alloc_ldap) {
169 return NT_STATUS_UNSUCCESSFUL;
172 ctx = talloc_new(idmap_alloc_ldap);
174 DEBUG(0, ("Out of memory!\n"));
175 return NT_STATUS_NO_MEMORY;
178 filter = talloc_asprintf(ctx, "(objectclass=%s)", LDAP_OBJ_IDPOOL);
179 CHECK_ALLOC_DONE(filter);
181 attr_list = get_attr_list(ctx, idpool_attr_list);
182 CHECK_ALLOC_DONE(attr_list);
184 rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
185 idmap_alloc_ldap->suffix,
192 if (rc != LDAP_SUCCESS) {
193 DEBUG(1, ("Unable to verify the idpool, "
194 "cannot continue initialization!\n"));
195 return NT_STATUS_UNSUCCESSFUL;
198 count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
201 ldap_msgfree(result);
204 DEBUG(0,("Multiple entries returned from %s (base == %s)\n",
205 filter, idmap_alloc_ldap->suffix));
206 ret = NT_STATUS_UNSUCCESSFUL;
209 else if (count == 0) {
210 char *uid_str, *gid_str;
212 uid_str = talloc_asprintf(ctx, "%lu",
213 (unsigned long)idmap_alloc_ldap->low_uid);
214 gid_str = talloc_asprintf(ctx, "%lu",
215 (unsigned long)idmap_alloc_ldap->low_gid);
217 smbldap_set_mod(&mods, LDAP_MOD_ADD,
218 "objectClass", LDAP_OBJ_IDPOOL);
219 smbldap_set_mod(&mods, LDAP_MOD_ADD,
220 get_attr_key2string(idpool_attr_list,
221 LDAP_ATTR_UIDNUMBER),
223 smbldap_set_mod(&mods, LDAP_MOD_ADD,
224 get_attr_key2string(idpool_attr_list,
225 LDAP_ATTR_GIDNUMBER),
228 rc = smbldap_modify(idmap_alloc_ldap->smbldap_state,
229 idmap_alloc_ldap->suffix,
231 ldap_mods_free(mods, True);
233 ret = NT_STATUS_UNSUCCESSFUL;
238 ret = (rc == LDAP_SUCCESS)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
244 /*****************************************************************************
245 Initialise idmap database.
246 *****************************************************************************/
248 static NTSTATUS idmap_ldap_alloc_init(const char *params)
250 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
257 /* Only do init if we are online */
258 if (idmap_is_offline()) {
259 return NT_STATUS_FILE_IS_OFFLINE;
262 idmap_alloc_ldap = TALLOC_ZERO_P(NULL, struct idmap_ldap_alloc_context);
263 CHECK_ALLOC_DONE( idmap_alloc_ldap );
267 if (!lp_idmap_uid(&low_uid, &high_uid)
268 || !lp_idmap_gid(&low_gid, &high_gid)) {
269 DEBUG(1, ("idmap uid or idmap gid missing\n"));
270 ret = NT_STATUS_UNSUCCESSFUL;
274 idmap_alloc_ldap->low_uid = low_uid;
275 idmap_alloc_ldap->high_uid = high_uid;
276 idmap_alloc_ldap->low_gid = low_gid;
277 idmap_alloc_ldap->high_gid= high_gid;
279 if (idmap_alloc_ldap->high_uid <= idmap_alloc_ldap->low_uid) {
280 DEBUG(1, ("idmap uid range invalid\n"));
281 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
282 ret = NT_STATUS_UNSUCCESSFUL;
286 if (idmap_alloc_ldap->high_gid <= idmap_alloc_ldap->low_gid) {
287 DEBUG(1, ("idmap gid range invalid\n"));
288 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
289 ret = NT_STATUS_UNSUCCESSFUL;
293 if (params && *params) {
294 /* assume location is the only parameter */
295 idmap_alloc_ldap->url = talloc_strdup(idmap_alloc_ldap, params);
297 tmp = lp_parm_const_string(-1, "idmap alloc config",
301 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
302 ret = NT_STATUS_UNSUCCESSFUL;
306 idmap_alloc_ldap->url = talloc_strdup(idmap_alloc_ldap, tmp);
308 CHECK_ALLOC_DONE( idmap_alloc_ldap->url );
310 trim_char(idmap_alloc_ldap->url, '\"', '\"');
312 tmp = lp_parm_const_string(-1, "idmap alloc config",
313 "ldap_base_dn", NULL);
314 if ( ! tmp || ! *tmp) {
315 tmp = lp_ldap_idmap_suffix();
317 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
318 ret = NT_STATUS_UNSUCCESSFUL;
323 idmap_alloc_ldap->suffix = talloc_strdup(idmap_alloc_ldap, tmp);
324 CHECK_ALLOC_DONE( idmap_alloc_ldap->suffix );
326 ret = smbldap_init(idmap_alloc_ldap, winbind_event_context(),
327 idmap_alloc_ldap->url,
328 &idmap_alloc_ldap->smbldap_state);
329 if (!NT_STATUS_IS_OK(ret)) {
330 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n",
331 idmap_alloc_ldap->url));
335 ret = get_credentials( idmap_alloc_ldap,
336 idmap_alloc_ldap->smbldap_state,
337 "idmap alloc config", NULL,
338 &idmap_alloc_ldap->user_dn );
339 if ( !NT_STATUS_IS_OK(ret) ) {
340 DEBUG(1,("idmap_ldap_alloc_init: Failed to get connection "
341 "credentials (%s)\n", nt_errstr(ret)));
345 /* see if the idmap suffix and sub entries exists */
347 ret = verify_idpool();
350 if ( !NT_STATUS_IS_OK( ret ) )
351 TALLOC_FREE( idmap_alloc_ldap );
356 /********************************
357 Allocate a new uid or gid
358 ********************************/
360 static NTSTATUS idmap_ldap_allocate_id(struct unixid *xid)
363 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
364 int rc = LDAP_SERVER_DOWN;
366 LDAPMessage *result = NULL;
367 LDAPMessage *entry = NULL;
368 LDAPMod **mods = NULL;
372 const char *dn = NULL;
373 const char **attr_list;
376 /* Only do query if we are online */
377 if (idmap_is_offline()) {
378 return NT_STATUS_FILE_IS_OFFLINE;
381 if ( ! idmap_alloc_ldap) {
382 return NT_STATUS_UNSUCCESSFUL;
385 ctx = talloc_new(idmap_alloc_ldap);
387 DEBUG(0, ("Out of memory!\n"));
388 return NT_STATUS_NO_MEMORY;
395 type = get_attr_key2string(idpool_attr_list,
396 LDAP_ATTR_UIDNUMBER);
400 type = get_attr_key2string(idpool_attr_list,
401 LDAP_ATTR_GIDNUMBER);
405 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
406 return NT_STATUS_INVALID_PARAMETER;
409 filter = talloc_asprintf(ctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
410 CHECK_ALLOC_DONE(filter);
412 attr_list = get_attr_list(ctx, idpool_attr_list);
413 CHECK_ALLOC_DONE(attr_list);
415 DEBUG(10, ("Search of the id pool (filter: %s)\n", filter));
417 rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
418 idmap_alloc_ldap->suffix,
419 LDAP_SCOPE_SUBTREE, filter,
420 attr_list, 0, &result);
422 if (rc != LDAP_SUCCESS) {
423 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
427 talloc_autofree_ldapmsg(ctx, result);
429 count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
432 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
436 entry = ldap_first_entry(idmap_alloc_ldap->smbldap_state->ldap_struct,
439 dn = smbldap_talloc_dn(ctx,
440 idmap_alloc_ldap->smbldap_state->ldap_struct,
446 if ( ! (id_str = smbldap_talloc_single_attribute(idmap_alloc_ldap->smbldap_state->ldap_struct,
447 entry, type, ctx))) {
448 DEBUG(0,("%s attribute not found\n", type));
449 ret = NT_STATUS_UNSUCCESSFUL;
453 xid->id = strtoul(id_str, NULL, 10);
455 /* make sure we still have room to grow */
459 if (xid->id > idmap_alloc_ldap->high_uid) {
460 DEBUG(0,("Cannot allocate uid above %lu!\n",
461 (unsigned long)idmap_alloc_ldap->high_uid));
467 if (xid->id > idmap_alloc_ldap->high_gid) {
468 DEBUG(0,("Cannot allocate gid above %lu!\n",
469 (unsigned long)idmap_alloc_ldap->high_uid));
479 new_id_str = talloc_asprintf(ctx, "%lu", (unsigned long)xid->id + 1);
481 DEBUG(0,("Out of memory\n"));
482 ret = NT_STATUS_NO_MEMORY;
486 smbldap_set_mod(&mods, LDAP_MOD_DELETE, type, id_str);
487 smbldap_set_mod(&mods, LDAP_MOD_ADD, type, new_id_str);
490 DEBUG(0,("smbldap_set_mod() failed.\n"));
494 DEBUG(10, ("Try to atomically increment the id (%s -> %s)\n",
495 id_str, new_id_str));
497 rc = smbldap_modify(idmap_alloc_ldap->smbldap_state, dn, mods);
499 ldap_mods_free(mods, True);
501 if (rc != LDAP_SUCCESS) {
502 DEBUG(1,("Failed to allocate new %s. "
503 "smbldap_modify() failed.\n", type));
514 /**********************************
515 Close idmap ldap alloc
516 **********************************/
518 static NTSTATUS idmap_ldap_alloc_close(void)
520 if (idmap_alloc_ldap) {
521 smbldap_free_struct(&idmap_alloc_ldap->smbldap_state);
522 DEBUG(5,("The connection to the LDAP server was closed\n"));
523 /* maybe free the results here --metze */
524 TALLOC_FREE(idmap_alloc_ldap);
530 /**********************************************************************
531 IDMAP MAPPING LDAP BACKEND
532 **********************************************************************/
534 static int idmap_ldap_close_destructor(struct idmap_ldap_context *ctx)
536 smbldap_free_struct(&ctx->smbldap_state);
537 DEBUG(5,("The connection to the LDAP server was closed\n"));
538 /* maybe free the results here --metze */
543 /********************************
544 Initialise idmap database.
545 ********************************/
547 static NTSTATUS idmap_ldap_db_init(struct idmap_domain *dom,
551 struct idmap_ldap_context *ctx = NULL;
552 char *config_option = NULL;
553 const char *tmp = NULL;
555 /* Only do init if we are online */
556 if (idmap_is_offline()) {
557 return NT_STATUS_FILE_IS_OFFLINE;
560 ctx = TALLOC_ZERO_P(dom, struct idmap_ldap_context);
562 DEBUG(0, ("Out of memory!\n"));
563 return NT_STATUS_NO_MEMORY;
566 if (strequal(dom->name, "*")) {
567 /* more specific configuration can go here */
569 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
570 if ( ! config_option) {
571 DEBUG(0, ("Out of memory!\n"));
572 ret = NT_STATUS_NO_MEMORY;
577 if (params != NULL) {
578 /* assume location is the only parameter */
579 ctx->url = talloc_strdup(ctx, params);
581 tmp = lp_parm_const_string(-1, config_option, "ldap_url", NULL);
584 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
585 ret = NT_STATUS_UNSUCCESSFUL;
589 ctx->url = talloc_strdup(ctx, tmp);
591 CHECK_ALLOC_DONE(ctx->url);
593 trim_char(ctx->url, '\"', '\"');
595 tmp = lp_parm_const_string(-1, config_option, "ldap_base_dn", NULL);
596 if ( ! tmp || ! *tmp) {
597 tmp = lp_ldap_idmap_suffix();
599 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
600 ret = NT_STATUS_UNSUCCESSFUL;
605 ctx->suffix = talloc_strdup(ctx, tmp);
606 CHECK_ALLOC_DONE(ctx->suffix);
608 ret = smbldap_init(ctx, winbind_event_context(), ctx->url,
609 &ctx->smbldap_state);
610 if (!NT_STATUS_IS_OK(ret)) {
611 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", ctx->url));
615 ret = get_credentials( ctx, ctx->smbldap_state, config_option,
616 dom, &ctx->user_dn );
617 if ( !NT_STATUS_IS_OK(ret) ) {
618 DEBUG(1,("idmap_ldap_db_init: Failed to get connection "
619 "credentials (%s)\n", nt_errstr(ret)));
623 /* set the destructor on the context, so that resource are properly
624 freed if the contexts is released */
626 talloc_set_destructor(ctx, idmap_ldap_close_destructor);
628 dom->private_data = ctx;
630 talloc_free(config_option);
639 /* max number of ids requested per batch query */
640 #define IDMAP_LDAP_MAX_IDS 30
642 /**********************************
643 lookup a set of unix ids.
644 **********************************/
646 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
647 * in maps for a match */
648 static struct id_map *find_map_by_id(struct id_map **maps,
654 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
655 if (maps[i] == NULL) { /* end of the run */
658 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
666 static NTSTATUS idmap_ldap_unixids_to_sids(struct idmap_domain *dom,
671 struct idmap_ldap_context *ctx;
672 LDAPMessage *result = NULL;
673 LDAPMessage *entry = NULL;
674 const char *uidNumber;
675 const char *gidNumber;
676 const char **attr_list;
685 /* Only do query if we are online */
686 if (idmap_is_offline()) {
687 return NT_STATUS_FILE_IS_OFFLINE;
690 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
692 memctx = talloc_new(ctx);
694 DEBUG(0, ("Out of memory!\n"));
695 return NT_STATUS_NO_MEMORY;
698 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
699 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
701 attr_list = get_attr_list(memctx, sidmap_attr_list);
704 /* if we are requested just one mapping use the simple filter */
706 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%lu))",
707 LDAP_OBJ_IDMAP_ENTRY,
708 (ids[0]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
709 (unsigned long)ids[0]->xid.id);
710 CHECK_ALLOC_DONE(filter);
711 DEBUG(10, ("Filter: [%s]\n", filter));
713 /* multiple mappings */
717 for (i = 0; ids[i]; i++) {
718 ids[i]->status = ID_UNKNOWN;
725 filter = talloc_asprintf(memctx,
726 "(&(objectClass=%s)(|",
727 LDAP_OBJ_IDMAP_ENTRY);
728 CHECK_ALLOC_DONE(filter);
731 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
732 filter = talloc_asprintf_append_buffer(filter, "(%s=%lu)",
733 (ids[idx]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
734 (unsigned long)ids[idx]->xid.id);
735 CHECK_ALLOC_DONE(filter);
737 filter = talloc_asprintf_append_buffer(filter, "))");
738 CHECK_ALLOC_DONE(filter);
739 DEBUG(10, ("Filter: [%s]\n", filter));
745 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
746 filter, attr_list, 0, &result);
748 if (rc != LDAP_SUCCESS) {
749 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc)));
750 ret = NT_STATUS_UNSUCCESSFUL;
754 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
757 DEBUG(10, ("NO SIDs found\n"));
760 for (i = 0; i < count; i++) {
767 if (i == 0) { /* first entry */
768 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
770 } else { /* following ones */
771 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
775 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
780 /* first check if the SID is present */
781 sidstr = smbldap_talloc_single_attribute(
782 ctx->smbldap_state->ldap_struct,
783 entry, LDAP_ATTRIBUTE_SID, memctx);
784 if ( ! sidstr) { /* no sid, skip entry */
785 DEBUG(2, ("WARNING SID not found on entry\n"));
789 /* now try to see if it is a uid, if not try with a gid
790 * (gid is more common, but in case both uidNumber and
791 * gidNumber are returned the SID is mapped to the uid
794 tmp = smbldap_talloc_single_attribute(
795 ctx->smbldap_state->ldap_struct,
796 entry, uidNumber, memctx);
799 tmp = smbldap_talloc_single_attribute(
800 ctx->smbldap_state->ldap_struct,
801 entry, gidNumber, memctx);
803 if ( ! tmp) { /* wow very strange entry, how did it match ? */
804 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
805 "nor gidNumber returned\n", sidstr));
810 id = strtoul(tmp, NULL, 10);
811 if (!idmap_unix_id_is_in_range(id, dom)) {
812 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
814 dom->low_id, dom->high_id));
821 map = find_map_by_id(&ids[bidx], type, id);
823 DEBUG(2, ("WARNING: couldn't match sid (%s) "
824 "with requested ids\n", sidstr));
829 if ( ! string_to_sid(map->sid, sidstr)) {
830 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
835 if (map->status == ID_MAPPED) {
836 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
837 "overwriting mapping %u -> %s with %u -> %s\n",
838 (type == ID_TYPE_UID) ? "UID" : "GID",
839 id, sid_string_dbg(map->sid), id, sidstr));
845 map->status = ID_MAPPED;
847 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
848 (unsigned long)map->xid.id, map->xid.type));
851 /* free the ldap results */
853 ldap_msgfree(result);
857 if (multi && ids[idx]) { /* still some values to map */
863 /* mark all unknwon/expired ones as unmapped */
864 for (i = 0; ids[i]; i++) {
865 if (ids[i]->status != ID_MAPPED)
866 ids[i]->status = ID_UNMAPPED;
874 /**********************************
875 lookup a set of sids.
876 **********************************/
878 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
879 * in maps for a match */
880 static struct id_map *find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
884 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
885 if (maps[i] == NULL) { /* end of the run */
888 if (sid_equal(maps[i]->sid, sid)) {
896 static NTSTATUS idmap_ldap_sids_to_unixids(struct idmap_domain *dom,
899 LDAPMessage *entry = NULL;
902 struct idmap_ldap_context *ctx;
903 LDAPMessage *result = NULL;
904 const char *uidNumber;
905 const char *gidNumber;
906 const char **attr_list;
915 /* Only do query if we are online */
916 if (idmap_is_offline()) {
917 return NT_STATUS_FILE_IS_OFFLINE;
920 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
922 memctx = talloc_new(ctx);
924 DEBUG(0, ("Out of memory!\n"));
925 return NT_STATUS_NO_MEMORY;
928 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
929 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
931 attr_list = get_attr_list(memctx, sidmap_attr_list);
934 /* if we are requested just one mapping use the simple filter */
936 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%s))",
937 LDAP_OBJ_IDMAP_ENTRY,
939 sid_string_talloc(memctx, ids[0]->sid));
940 CHECK_ALLOC_DONE(filter);
941 DEBUG(10, ("Filter: [%s]\n", filter));
943 /* multiple mappings */
947 for (i = 0; ids[i]; i++) {
948 ids[i]->status = ID_UNKNOWN;
955 filter = talloc_asprintf(memctx,
956 "(&(objectClass=%s)(|",
957 LDAP_OBJ_IDMAP_ENTRY);
958 CHECK_ALLOC_DONE(filter);
961 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
962 filter = talloc_asprintf_append_buffer(filter, "(%s=%s)",
964 sid_string_talloc(memctx,
966 CHECK_ALLOC_DONE(filter);
968 filter = talloc_asprintf_append_buffer(filter, "))");
969 CHECK_ALLOC_DONE(filter);
970 DEBUG(10, ("Filter: [%s]", filter));
976 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
977 filter, attr_list, 0, &result);
979 if (rc != LDAP_SUCCESS) {
980 DEBUG(3,("Failure looking up sids (%s)\n",
981 ldap_err2string(rc)));
982 ret = NT_STATUS_UNSUCCESSFUL;
986 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
989 DEBUG(10, ("NO SIDs found\n"));
992 for (i = 0; i < count; i++) {
1000 if (i == 0) { /* first entry */
1001 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
1003 } else { /* following ones */
1004 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
1008 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
1013 /* first check if the SID is present */
1014 sidstr = smbldap_talloc_single_attribute(
1015 ctx->smbldap_state->ldap_struct,
1016 entry, LDAP_ATTRIBUTE_SID, memctx);
1017 if ( ! sidstr) { /* no sid ??, skip entry */
1018 DEBUG(2, ("WARNING SID not found on entry\n"));
1022 if ( ! string_to_sid(&sid, sidstr)) {
1023 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
1024 TALLOC_FREE(sidstr);
1028 map = find_map_by_sid(&ids[bidx], &sid);
1030 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
1032 TALLOC_FREE(sidstr);
1036 /* now try to see if it is a uid, if not try with a gid
1037 * (gid is more common, but in case both uidNumber and
1038 * gidNumber are returned the SID is mapped to the uid
1041 tmp = smbldap_talloc_single_attribute(
1042 ctx->smbldap_state->ldap_struct,
1043 entry, uidNumber, memctx);
1046 tmp = smbldap_talloc_single_attribute(
1047 ctx->smbldap_state->ldap_struct,
1048 entry, gidNumber, memctx);
1050 if ( ! tmp) { /* no ids ?? */
1051 DEBUG(5, ("no uidNumber, "
1052 "nor gidNumber attributes found\n"));
1053 TALLOC_FREE(sidstr);
1057 id = strtoul(tmp, NULL, 10);
1058 if (!idmap_unix_id_is_in_range(id, dom)) {
1059 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1061 dom->low_id, dom->high_id));
1062 TALLOC_FREE(sidstr);
1068 if (map->status == ID_MAPPED) {
1069 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1070 "overwriting mapping %s -> %u with %s -> %u\n",
1071 (type == ID_TYPE_UID) ? "UID" : "GID",
1072 sidstr, map->xid.id, sidstr, id));
1075 TALLOC_FREE(sidstr);
1078 map->xid.type = type;
1080 map->status = ID_MAPPED;
1082 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1083 (unsigned long)map->xid.id, map->xid.type));
1086 /* free the ldap results */
1088 ldap_msgfree(result);
1092 if (multi && ids[idx]) { /* still some values to map */
1098 /* mark all unknwon/expired ones as unmapped */
1099 for (i = 0; ids[i]; i++) {
1100 if (ids[i]->status != ID_MAPPED)
1101 ids[i]->status = ID_UNMAPPED;
1105 talloc_free(memctx);
1109 /**********************************
1111 **********************************/
1113 /* TODO: change this: This function cannot be called to modify a mapping,
1114 * only set a new one */
1116 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
1117 const struct id_map *map)
1121 struct idmap_ldap_context *ctx;
1122 LDAPMessage *entry = NULL;
1123 LDAPMod **mods = NULL;
1130 /* Only do query if we are online */
1131 if (idmap_is_offline()) {
1132 return NT_STATUS_FILE_IS_OFFLINE;
1135 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
1137 switch(map->xid.type) {
1139 type = get_attr_key2string(sidmap_attr_list,
1140 LDAP_ATTR_UIDNUMBER);
1144 type = get_attr_key2string(sidmap_attr_list,
1145 LDAP_ATTR_GIDNUMBER);
1149 return NT_STATUS_INVALID_PARAMETER;
1152 memctx = talloc_new(ctx);
1154 DEBUG(0, ("Out of memory!\n"));
1155 return NT_STATUS_NO_MEMORY;
1158 id_str = talloc_asprintf(memctx, "%lu", (unsigned long)map->xid.id);
1159 CHECK_ALLOC_DONE(id_str);
1161 sid = talloc_strdup(memctx, sid_string_talloc(memctx, map->sid));
1162 CHECK_ALLOC_DONE(sid);
1164 dn = talloc_asprintf(memctx, "%s=%s,%s",
1165 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1168 CHECK_ALLOC_DONE(dn);
1170 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1171 "objectClass", LDAP_OBJ_IDMAP_ENTRY);
1173 smbldap_make_mod(ctx->smbldap_state->ldap_struct,
1174 entry, &mods, type, id_str);
1176 smbldap_make_mod(ctx->smbldap_state->ldap_struct, entry, &mods,
1177 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1181 DEBUG(2, ("ERROR: No mods?\n"));
1182 ret = NT_STATUS_UNSUCCESSFUL;
1186 /* TODO: remove conflicting mappings! */
1188 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SID_ENTRY);
1190 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn, sid, id_str));
1192 rc = smbldap_add(ctx->smbldap_state, dn, mods);
1193 ldap_mods_free(mods, True);
1195 if (rc != LDAP_SUCCESS) {
1196 char *ld_error = NULL;
1197 ldap_get_option(ctx->smbldap_state->ldap_struct,
1198 LDAP_OPT_ERROR_STRING, &ld_error);
1199 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
1200 "mapping [%s]\n", sid,
1201 (unsigned long)map->xid.id, type));
1202 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
1203 ld_error ? ld_error : "(NULL)", ldap_err2string (rc)));
1205 ldap_memfree(ld_error);
1207 ret = NT_STATUS_UNSUCCESSFUL;
1211 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
1212 "%lu [%s]\n", sid, (unsigned long)map->xid.id, type));
1217 talloc_free(memctx);
1221 /**********************************
1222 Close the idmap ldap instance
1223 **********************************/
1225 static NTSTATUS idmap_ldap_close(struct idmap_domain *dom)
1227 struct idmap_ldap_context *ctx;
1229 if (dom->private_data) {
1230 ctx = talloc_get_type(dom->private_data,
1231 struct idmap_ldap_context);
1234 dom->private_data = NULL;
1237 return NT_STATUS_OK;
1240 static struct idmap_methods idmap_ldap_methods = {
1242 .init = idmap_ldap_db_init,
1243 .unixids_to_sids = idmap_ldap_unixids_to_sids,
1244 .sids_to_unixids = idmap_ldap_sids_to_unixids,
1245 .allocate_id = idmap_ldap_get_new_id,
1246 .close_fn = idmap_ldap_close
1249 NTSTATUS idmap_ldap_init(void);
1250 NTSTATUS idmap_ldap_init(void)
1252 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1253 &idmap_ldap_methods);