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;
64 uint32_t filter_low_id, filter_high_id; /* Filter range */
68 struct idmap_ldap_alloc_context {
69 struct smbldap_state *smbldap_state;
73 uid_t low_uid, high_uid; /* Range of uids */
74 gid_t low_gid, high_gid; /* Range of gids */
78 #define CHECK_ALLOC_DONE(mem) do { \
80 DEBUG(0, ("Out of memory!\n")); \
81 ret = NT_STATUS_NO_MEMORY; \
85 /**********************************************************************
86 IDMAP ALLOC TDB BACKEND
87 **********************************************************************/
89 static struct idmap_ldap_alloc_context *idmap_alloc_ldap;
91 /*********************************************************************
92 ********************************************************************/
94 static NTSTATUS get_credentials( TALLOC_CTX *mem_ctx,
95 struct smbldap_state *ldap_state,
96 const char *config_option,
97 struct idmap_domain *dom,
100 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
102 const char *tmp = NULL;
103 char *user_dn = NULL;
106 /* assume anonymous if we don't have a specified user */
108 tmp = lp_parm_const_string(-1, config_option, "ldap_user_dn", NULL);
112 /* only the alloc backend can pass in a NULL dom */
113 secret = idmap_fetch_secret("ldap", True,
116 secret = idmap_fetch_secret("ldap", False,
121 DEBUG(0, ("get_credentials: Unable to fetch "
122 "auth credentials for %s in %s\n",
123 tmp, (dom==NULL)?"ALLOC":dom->name));
124 ret = NT_STATUS_ACCESS_DENIED;
127 *dn = talloc_strdup(mem_ctx, tmp);
128 CHECK_ALLOC_DONE(*dn);
130 if (!fetch_ldap_pw(&user_dn, &secret)) {
131 DEBUG(2, ("get_credentials: Failed to lookup ldap "
132 "bind creds. Using anonymous connection.\n"));
135 *dn = talloc_strdup(mem_ctx, user_dn);
136 SAFE_FREE( user_dn );
137 CHECK_ALLOC_DONE(*dn);
141 smbldap_set_creds(ldap_state, anon, *dn, secret);
151 /**********************************************************************
152 Verify the sambaUnixIdPool entry in the directory.
153 **********************************************************************/
155 static NTSTATUS verify_idpool(void)
159 LDAPMessage *result = NULL;
160 LDAPMod **mods = NULL;
161 const char **attr_list;
166 if ( ! idmap_alloc_ldap) {
167 return NT_STATUS_UNSUCCESSFUL;
170 ctx = talloc_new(idmap_alloc_ldap);
172 DEBUG(0, ("Out of memory!\n"));
173 return NT_STATUS_NO_MEMORY;
176 filter = talloc_asprintf(ctx, "(objectclass=%s)", LDAP_OBJ_IDPOOL);
177 CHECK_ALLOC_DONE(filter);
179 attr_list = get_attr_list(ctx, idpool_attr_list);
180 CHECK_ALLOC_DONE(attr_list);
182 rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
183 idmap_alloc_ldap->suffix,
190 if (rc != LDAP_SUCCESS) {
191 DEBUG(1, ("Unable to verify the idpool, "
192 "cannot continue initialization!\n"));
193 return NT_STATUS_UNSUCCESSFUL;
196 count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
199 ldap_msgfree(result);
202 DEBUG(0,("Multiple entries returned from %s (base == %s)\n",
203 filter, idmap_alloc_ldap->suffix));
204 ret = NT_STATUS_UNSUCCESSFUL;
207 else if (count == 0) {
208 char *uid_str, *gid_str;
210 uid_str = talloc_asprintf(ctx, "%lu",
211 (unsigned long)idmap_alloc_ldap->low_uid);
212 gid_str = talloc_asprintf(ctx, "%lu",
213 (unsigned long)idmap_alloc_ldap->low_gid);
215 smbldap_set_mod(&mods, LDAP_MOD_ADD,
216 "objectClass", LDAP_OBJ_IDPOOL);
217 smbldap_set_mod(&mods, LDAP_MOD_ADD,
218 get_attr_key2string(idpool_attr_list,
219 LDAP_ATTR_UIDNUMBER),
221 smbldap_set_mod(&mods, LDAP_MOD_ADD,
222 get_attr_key2string(idpool_attr_list,
223 LDAP_ATTR_GIDNUMBER),
226 rc = smbldap_modify(idmap_alloc_ldap->smbldap_state,
227 idmap_alloc_ldap->suffix,
229 ldap_mods_free(mods, True);
231 ret = NT_STATUS_UNSUCCESSFUL;
236 ret = (rc == LDAP_SUCCESS)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
242 /*****************************************************************************
243 Initialise idmap database.
244 *****************************************************************************/
246 static NTSTATUS idmap_ldap_alloc_init(const char *params)
248 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
256 /* Only do init if we are online */
257 if (idmap_is_offline()) {
258 return NT_STATUS_FILE_IS_OFFLINE;
261 idmap_alloc_ldap = TALLOC_ZERO_P(NULL, struct idmap_ldap_alloc_context);
262 CHECK_ALLOC_DONE( idmap_alloc_ldap );
266 if (!lp_idmap_uid(&low_uid, &high_uid)
267 || !lp_idmap_gid(&low_gid, &high_gid)) {
268 DEBUG(1, ("idmap uid or idmap gid missing\n"));
269 ret = NT_STATUS_UNSUCCESSFUL;
273 idmap_alloc_ldap->low_uid = low_uid;
274 idmap_alloc_ldap->high_uid = high_uid;
275 idmap_alloc_ldap->low_gid = low_gid;
276 idmap_alloc_ldap->high_gid= high_gid;
278 if (idmap_alloc_ldap->high_uid <= idmap_alloc_ldap->low_uid) {
279 DEBUG(1, ("idmap uid range invalid\n"));
280 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
281 ret = NT_STATUS_UNSUCCESSFUL;
285 if (idmap_alloc_ldap->high_gid <= idmap_alloc_ldap->low_gid) {
286 DEBUG(1, ("idmap gid range invalid\n"));
287 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
288 ret = NT_STATUS_UNSUCCESSFUL;
292 if (params && *params) {
293 /* assume location is the only parameter */
294 idmap_alloc_ldap->url = talloc_strdup(idmap_alloc_ldap, params);
296 tmp = lp_parm_const_string(-1, "idmap alloc config",
300 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
301 ret = NT_STATUS_UNSUCCESSFUL;
305 idmap_alloc_ldap->url = talloc_strdup(idmap_alloc_ldap, tmp);
307 CHECK_ALLOC_DONE( idmap_alloc_ldap->url );
309 tmp = lp_parm_const_string(-1, "idmap alloc config",
310 "ldap_base_dn", NULL);
311 if ( ! tmp || ! *tmp) {
312 tmp = lp_ldap_idmap_suffix();
314 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
315 ret = NT_STATUS_UNSUCCESSFUL;
320 idmap_alloc_ldap->suffix = talloc_strdup(idmap_alloc_ldap, tmp);
321 CHECK_ALLOC_DONE( idmap_alloc_ldap->suffix );
323 ret = smbldap_init(idmap_alloc_ldap, winbind_event_context(),
324 idmap_alloc_ldap->url,
325 &idmap_alloc_ldap->smbldap_state);
326 if (!NT_STATUS_IS_OK(ret)) {
327 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n",
328 idmap_alloc_ldap->url));
332 ret = get_credentials( idmap_alloc_ldap,
333 idmap_alloc_ldap->smbldap_state,
334 "idmap alloc config", NULL,
335 &idmap_alloc_ldap->user_dn );
336 if ( !NT_STATUS_IS_OK(ret) ) {
337 DEBUG(1,("idmap_ldap_alloc_init: Failed to get connection "
338 "credentials (%s)\n", nt_errstr(ret)));
342 /* see if the idmap suffix and sub entries exists */
344 ret = verify_idpool();
347 if ( !NT_STATUS_IS_OK( ret ) )
348 TALLOC_FREE( idmap_alloc_ldap );
353 /********************************
354 Allocate a new uid or gid
355 ********************************/
357 static NTSTATUS idmap_ldap_allocate_id(struct unixid *xid)
360 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
361 int rc = LDAP_SERVER_DOWN;
363 LDAPMessage *result = NULL;
364 LDAPMessage *entry = NULL;
365 LDAPMod **mods = NULL;
369 const char *dn = NULL;
370 const char **attr_list;
373 /* Only do query if we are online */
374 if (idmap_is_offline()) {
375 return NT_STATUS_FILE_IS_OFFLINE;
378 if ( ! idmap_alloc_ldap) {
379 return NT_STATUS_UNSUCCESSFUL;
382 ctx = talloc_new(idmap_alloc_ldap);
384 DEBUG(0, ("Out of memory!\n"));
385 return NT_STATUS_NO_MEMORY;
392 type = get_attr_key2string(idpool_attr_list,
393 LDAP_ATTR_UIDNUMBER);
397 type = get_attr_key2string(idpool_attr_list,
398 LDAP_ATTR_GIDNUMBER);
402 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
403 return NT_STATUS_INVALID_PARAMETER;
406 filter = talloc_asprintf(ctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
407 CHECK_ALLOC_DONE(filter);
409 attr_list = get_attr_list(ctx, idpool_attr_list);
410 CHECK_ALLOC_DONE(attr_list);
412 DEBUG(10, ("Search of the id pool (filter: %s)\n", filter));
414 rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
415 idmap_alloc_ldap->suffix,
416 LDAP_SCOPE_SUBTREE, filter,
417 attr_list, 0, &result);
419 if (rc != LDAP_SUCCESS) {
420 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
424 talloc_autofree_ldapmsg(ctx, result);
426 count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
429 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
433 entry = ldap_first_entry(idmap_alloc_ldap->smbldap_state->ldap_struct,
436 dn = smbldap_talloc_dn(ctx,
437 idmap_alloc_ldap->smbldap_state->ldap_struct,
443 if ( ! (id_str = smbldap_talloc_single_attribute(idmap_alloc_ldap->smbldap_state->ldap_struct,
444 entry, type, ctx))) {
445 DEBUG(0,("%s attribute not found\n", type));
449 DEBUG(0,("Out of memory\n"));
450 ret = NT_STATUS_NO_MEMORY;
454 xid->id = strtoul(id_str, NULL, 10);
456 /* make sure we still have room to grow */
460 if (xid->id > idmap_alloc_ldap->high_uid) {
461 DEBUG(0,("Cannot allocate uid above %lu!\n",
462 (unsigned long)idmap_alloc_ldap->high_uid));
468 if (xid->id > idmap_alloc_ldap->high_gid) {
469 DEBUG(0,("Cannot allocate gid above %lu!\n",
470 (unsigned long)idmap_alloc_ldap->high_uid));
480 new_id_str = talloc_asprintf(ctx, "%lu", (unsigned long)xid->id + 1);
482 DEBUG(0,("Out of memory\n"));
483 ret = NT_STATUS_NO_MEMORY;
487 smbldap_set_mod(&mods, LDAP_MOD_DELETE, type, id_str);
488 smbldap_set_mod(&mods, LDAP_MOD_ADD, type, new_id_str);
491 DEBUG(0,("smbldap_set_mod() failed.\n"));
495 DEBUG(10, ("Try to atomically increment the id (%s -> %s)\n",
496 id_str, new_id_str));
498 rc = smbldap_modify(idmap_alloc_ldap->smbldap_state, dn, mods);
500 ldap_mods_free(mods, True);
502 if (rc != LDAP_SUCCESS) {
503 DEBUG(1,("Failed to allocate new %s. "
504 "smbldap_modify() failed.\n", type));
515 /**********************************
516 Get current highest id.
517 **********************************/
519 static NTSTATUS idmap_ldap_get_hwm(struct unixid *xid)
522 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
523 int rc = LDAP_SERVER_DOWN;
525 LDAPMessage *result = NULL;
526 LDAPMessage *entry = NULL;
529 const char **attr_list;
532 /* Only do query if we are online */
533 if (idmap_is_offline()) {
534 return NT_STATUS_FILE_IS_OFFLINE;
537 if ( ! idmap_alloc_ldap) {
538 return NT_STATUS_UNSUCCESSFUL;
541 memctx = talloc_new(idmap_alloc_ldap);
543 DEBUG(0, ("Out of memory!\n"));
544 return NT_STATUS_NO_MEMORY;
551 type = get_attr_key2string(idpool_attr_list,
552 LDAP_ATTR_UIDNUMBER);
556 type = get_attr_key2string(idpool_attr_list,
557 LDAP_ATTR_GIDNUMBER);
561 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
562 return NT_STATUS_INVALID_PARAMETER;
565 filter = talloc_asprintf(memctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
566 CHECK_ALLOC_DONE(filter);
568 attr_list = get_attr_list(memctx, idpool_attr_list);
569 CHECK_ALLOC_DONE(attr_list);
571 rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
572 idmap_alloc_ldap->suffix,
573 LDAP_SCOPE_SUBTREE, filter,
574 attr_list, 0, &result);
576 if (rc != LDAP_SUCCESS) {
577 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
581 talloc_autofree_ldapmsg(memctx, result);
583 count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
586 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
590 entry = ldap_first_entry(idmap_alloc_ldap->smbldap_state->ldap_struct,
593 id_str = smbldap_talloc_single_attribute(idmap_alloc_ldap->smbldap_state->ldap_struct,
594 entry, type, memctx);
596 DEBUG(0,("%s attribute not found\n", type));
600 DEBUG(0,("Out of memory\n"));
601 ret = NT_STATUS_NO_MEMORY;
605 xid->id = strtoul(id_str, NULL, 10);
612 /**********************************
614 **********************************/
616 static NTSTATUS idmap_ldap_set_hwm(struct unixid *xid)
619 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
620 int rc = LDAP_SERVER_DOWN;
622 LDAPMessage *result = NULL;
623 LDAPMessage *entry = NULL;
624 LDAPMod **mods = NULL;
627 const char *dn = NULL;
628 const char **attr_list;
631 /* Only do query if we are online */
632 if (idmap_is_offline()) {
633 return NT_STATUS_FILE_IS_OFFLINE;
636 if ( ! idmap_alloc_ldap) {
637 return NT_STATUS_UNSUCCESSFUL;
640 ctx = talloc_new(idmap_alloc_ldap);
642 DEBUG(0, ("Out of memory!\n"));
643 return NT_STATUS_NO_MEMORY;
650 type = get_attr_key2string(idpool_attr_list,
651 LDAP_ATTR_UIDNUMBER);
655 type = get_attr_key2string(idpool_attr_list,
656 LDAP_ATTR_GIDNUMBER);
660 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
661 return NT_STATUS_INVALID_PARAMETER;
664 filter = talloc_asprintf(ctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
665 CHECK_ALLOC_DONE(filter);
667 attr_list = get_attr_list(ctx, idpool_attr_list);
668 CHECK_ALLOC_DONE(attr_list);
670 rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
671 idmap_alloc_ldap->suffix,
672 LDAP_SCOPE_SUBTREE, filter,
673 attr_list, 0, &result);
675 if (rc != LDAP_SUCCESS) {
676 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
680 talloc_autofree_ldapmsg(ctx, result);
682 count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
685 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
689 entry = ldap_first_entry(idmap_alloc_ldap->smbldap_state->ldap_struct,
692 dn = smbldap_talloc_dn(ctx,
693 idmap_alloc_ldap->smbldap_state->ldap_struct,
699 new_id_str = talloc_asprintf(ctx, "%lu", (unsigned long)xid->id);
701 DEBUG(0,("Out of memory\n"));
702 ret = NT_STATUS_NO_MEMORY;
706 smbldap_set_mod(&mods, LDAP_MOD_REPLACE, type, new_id_str);
709 DEBUG(0,("smbldap_set_mod() failed.\n"));
713 rc = smbldap_modify(idmap_alloc_ldap->smbldap_state, dn, mods);
715 ldap_mods_free(mods, True);
717 if (rc != LDAP_SUCCESS) {
718 DEBUG(1,("Failed to allocate new %s. "
719 "smbldap_modify() failed.\n", type));
730 /**********************************
731 Close idmap ldap alloc
732 **********************************/
734 static NTSTATUS idmap_ldap_alloc_close(void)
736 if (idmap_alloc_ldap) {
737 smbldap_free_struct(&idmap_alloc_ldap->smbldap_state);
738 DEBUG(5,("The connection to the LDAP server was closed\n"));
739 /* maybe free the results here --metze */
740 TALLOC_FREE(idmap_alloc_ldap);
746 /**********************************************************************
747 IDMAP MAPPING LDAP BACKEND
748 **********************************************************************/
750 static int idmap_ldap_close_destructor(struct idmap_ldap_context *ctx)
752 smbldap_free_struct(&ctx->smbldap_state);
753 DEBUG(5,("The connection to the LDAP server was closed\n"));
754 /* maybe free the results here --metze */
759 /********************************
760 Initialise idmap database.
761 ********************************/
763 static NTSTATUS idmap_ldap_db_init(struct idmap_domain *dom,
767 struct idmap_ldap_context *ctx = NULL;
768 char *config_option = NULL;
769 const char *range = NULL;
770 const char *tmp = NULL;
772 /* Only do init if we are online */
773 if (idmap_is_offline()) {
774 return NT_STATUS_FILE_IS_OFFLINE;
777 ctx = TALLOC_ZERO_P(dom, struct idmap_ldap_context);
779 DEBUG(0, ("Out of memory!\n"));
780 return NT_STATUS_NO_MEMORY;
783 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
784 if ( ! config_option) {
785 DEBUG(0, ("Out of memory!\n"));
786 ret = NT_STATUS_NO_MEMORY;
791 range = lp_parm_const_string(-1, config_option, "range", NULL);
792 if (range && range[0]) {
793 if ((sscanf(range, "%u - %u", &ctx->filter_low_id,
794 &ctx->filter_high_id) != 2) ||
795 (ctx->filter_low_id > ctx->filter_high_id)) {
796 DEBUG(1, ("ERROR: invalid filter range [%s]", range));
797 ctx->filter_low_id = 0;
798 ctx->filter_high_id = 0;
802 if (params != NULL) {
803 /* assume location is the only parameter */
804 ctx->url = talloc_strdup(ctx, params);
806 tmp = lp_parm_const_string(-1, config_option, "ldap_url", NULL);
809 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
810 ret = NT_STATUS_UNSUCCESSFUL;
814 ctx->url = talloc_strdup(ctx, tmp);
816 CHECK_ALLOC_DONE(ctx->url);
818 tmp = lp_parm_const_string(-1, config_option, "ldap_base_dn", NULL);
819 if ( ! tmp || ! *tmp) {
820 tmp = lp_ldap_idmap_suffix();
822 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
823 ret = NT_STATUS_UNSUCCESSFUL;
828 ctx->suffix = talloc_strdup(ctx, tmp);
829 CHECK_ALLOC_DONE(ctx->suffix);
831 ret = smbldap_init(ctx, winbind_event_context(), ctx->url,
832 &ctx->smbldap_state);
833 if (!NT_STATUS_IS_OK(ret)) {
834 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", ctx->url));
838 ret = get_credentials( ctx, ctx->smbldap_state, config_option,
839 dom, &ctx->user_dn );
840 if ( !NT_STATUS_IS_OK(ret) ) {
841 DEBUG(1,("idmap_ldap_db_init: Failed to get connection "
842 "credentials (%s)\n", nt_errstr(ret)));
846 /* set the destructor on the context, so that resource are properly
847 freed if the contexts is released */
849 talloc_set_destructor(ctx, idmap_ldap_close_destructor);
851 dom->private_data = ctx;
853 talloc_free(config_option);
862 /* max number of ids requested per batch query */
863 #define IDMAP_LDAP_MAX_IDS 30
865 /**********************************
866 lookup a set of unix ids.
867 **********************************/
869 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
870 * in maps for a match */
871 static struct id_map *find_map_by_id(struct id_map **maps,
877 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
878 if (maps[i] == NULL) { /* end of the run */
881 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
889 static NTSTATUS idmap_ldap_unixids_to_sids(struct idmap_domain *dom,
894 struct idmap_ldap_context *ctx;
895 LDAPMessage *result = NULL;
896 const char *uidNumber;
897 const char *gidNumber;
898 const char **attr_list;
907 /* Only do query if we are online */
908 if (idmap_is_offline()) {
909 return NT_STATUS_FILE_IS_OFFLINE;
912 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
914 memctx = talloc_new(ctx);
916 DEBUG(0, ("Out of memory!\n"));
917 return NT_STATUS_NO_MEMORY;
920 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
921 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
923 attr_list = get_attr_list(memctx, sidmap_attr_list);
926 /* if we are requested just one mapping use the simple filter */
928 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%lu))",
929 LDAP_OBJ_IDMAP_ENTRY,
930 (ids[0]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
931 (unsigned long)ids[0]->xid.id);
932 CHECK_ALLOC_DONE(filter);
933 DEBUG(10, ("Filter: [%s]\n", filter));
935 /* multiple mappings */
943 filter = talloc_asprintf(memctx,
944 "(&(objectClass=%s)(|",
945 LDAP_OBJ_IDMAP_ENTRY);
946 CHECK_ALLOC_DONE(filter);
949 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
950 filter = talloc_asprintf_append_buffer(filter, "(%s=%lu)",
951 (ids[idx]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
952 (unsigned long)ids[idx]->xid.id);
953 CHECK_ALLOC_DONE(filter);
955 filter = talloc_asprintf_append_buffer(filter, "))");
956 CHECK_ALLOC_DONE(filter);
957 DEBUG(10, ("Filter: [%s]\n", filter));
963 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
964 filter, attr_list, 0, &result);
966 if (rc != LDAP_SUCCESS) {
967 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc)));
968 ret = NT_STATUS_UNSUCCESSFUL;
972 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
975 DEBUG(10, ("NO SIDs found\n"));
978 for (i = 0; i < count; i++) {
979 LDAPMessage *entry = NULL;
986 if (i == 0) { /* first entry */
987 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
989 } else { /* following ones */
990 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
994 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
999 /* first check if the SID is present */
1000 sidstr = smbldap_talloc_single_attribute(
1001 ctx->smbldap_state->ldap_struct,
1002 entry, LDAP_ATTRIBUTE_SID, memctx);
1003 if ( ! sidstr) { /* no sid, skip entry */
1004 DEBUG(2, ("WARNING SID not found on entry\n"));
1008 /* now try to see if it is a uid, if not try with a gid
1009 * (gid is more common, but in case both uidNumber and
1010 * gidNumber are returned the SID is mapped to the uid
1013 tmp = smbldap_talloc_single_attribute(
1014 ctx->smbldap_state->ldap_struct,
1015 entry, uidNumber, memctx);
1018 tmp = smbldap_talloc_single_attribute(
1019 ctx->smbldap_state->ldap_struct,
1020 entry, gidNumber, memctx);
1022 if ( ! tmp) { /* wow very strange entry, how did it match ? */
1023 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
1024 "nor gidNumber returned\n", sidstr));
1025 TALLOC_FREE(sidstr);
1029 id = strtoul(tmp, NULL, 10);
1031 (ctx->filter_low_id && (id < ctx->filter_low_id)) ||
1032 (ctx->filter_high_id && (id > ctx->filter_high_id))) {
1033 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1035 ctx->filter_low_id, ctx->filter_high_id));
1036 TALLOC_FREE(sidstr);
1042 map = find_map_by_id(&ids[bidx], type, id);
1044 DEBUG(2, ("WARNING: couldn't match sid (%s) "
1045 "with requested ids\n", sidstr));
1046 TALLOC_FREE(sidstr);
1050 if ( ! string_to_sid(map->sid, sidstr)) {
1051 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
1052 TALLOC_FREE(sidstr);
1055 TALLOC_FREE(sidstr);
1058 map->status = ID_MAPPED;
1060 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1061 (unsigned long)map->xid.id, map->xid.type));
1064 /* free the ldap results */
1066 ldap_msgfree(result);
1070 if (multi && ids[idx]) { /* still some values to map */
1076 /* mark all unknwon/expired ones as unmapped */
1077 for (i = 0; ids[i]; i++) {
1078 if (ids[i]->status != ID_MAPPED)
1079 ids[i]->status = ID_UNMAPPED;
1083 talloc_free(memctx);
1087 /**********************************
1088 lookup a set of sids.
1089 **********************************/
1091 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
1092 * in maps for a match */
1093 static struct id_map *find_map_by_sid(struct id_map **maps, DOM_SID *sid)
1097 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
1098 if (maps[i] == NULL) { /* end of the run */
1101 if (sid_equal(maps[i]->sid, sid)) {
1109 static NTSTATUS idmap_ldap_sids_to_unixids(struct idmap_domain *dom,
1110 struct id_map **ids)
1112 LDAPMessage *entry = NULL;
1115 struct idmap_ldap_context *ctx;
1116 LDAPMessage *result = NULL;
1117 const char *uidNumber;
1118 const char *gidNumber;
1119 const char **attr_list;
1120 char *filter = NULL;
1128 /* Only do query if we are online */
1129 if (idmap_is_offline()) {
1130 return NT_STATUS_FILE_IS_OFFLINE;
1133 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
1135 memctx = talloc_new(ctx);
1137 DEBUG(0, ("Out of memory!\n"));
1138 return NT_STATUS_NO_MEMORY;
1141 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
1142 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
1144 attr_list = get_attr_list(memctx, sidmap_attr_list);
1147 /* if we are requested just one mapping use the simple filter */
1149 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%s))",
1150 LDAP_OBJ_IDMAP_ENTRY,
1152 sid_string_talloc(memctx, ids[0]->sid));
1153 CHECK_ALLOC_DONE(filter);
1154 DEBUG(10, ("Filter: [%s]\n", filter));
1156 /* multiple mappings */
1163 TALLOC_FREE(filter);
1164 filter = talloc_asprintf(memctx,
1165 "(&(objectClass=%s)(|",
1166 LDAP_OBJ_IDMAP_ENTRY);
1167 CHECK_ALLOC_DONE(filter);
1170 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
1171 filter = talloc_asprintf_append_buffer(filter, "(%s=%s)",
1173 sid_string_talloc(memctx,
1175 CHECK_ALLOC_DONE(filter);
1177 filter = talloc_asprintf_append_buffer(filter, "))");
1178 CHECK_ALLOC_DONE(filter);
1179 DEBUG(10, ("Filter: [%s]", filter));
1185 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
1186 filter, attr_list, 0, &result);
1188 if (rc != LDAP_SUCCESS) {
1189 DEBUG(3,("Failure looking up sids (%s)\n",
1190 ldap_err2string(rc)));
1191 ret = NT_STATUS_UNSUCCESSFUL;
1195 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
1198 DEBUG(10, ("NO SIDs found\n"));
1201 for (i = 0; i < count; i++) {
1202 char *sidstr = NULL;
1209 if (i == 0) { /* first entry */
1210 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
1212 } else { /* following ones */
1213 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
1217 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
1222 /* first check if the SID is present */
1223 sidstr = smbldap_talloc_single_attribute(
1224 ctx->smbldap_state->ldap_struct,
1225 entry, LDAP_ATTRIBUTE_SID, memctx);
1226 if ( ! sidstr) { /* no sid ??, skip entry */
1227 DEBUG(2, ("WARNING SID not found on entry\n"));
1231 if ( ! string_to_sid(&sid, sidstr)) {
1232 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
1233 TALLOC_FREE(sidstr);
1237 map = find_map_by_sid(&ids[bidx], &sid);
1239 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
1241 TALLOC_FREE(sidstr);
1245 TALLOC_FREE(sidstr);
1247 /* now try to see if it is a uid, if not try with a gid
1248 * (gid is more common, but in case both uidNumber and
1249 * gidNumber are returned the SID is mapped to the uid
1252 tmp = smbldap_talloc_single_attribute(
1253 ctx->smbldap_state->ldap_struct,
1254 entry, uidNumber, memctx);
1257 tmp = smbldap_talloc_single_attribute(
1258 ctx->smbldap_state->ldap_struct,
1259 entry, gidNumber, memctx);
1261 if ( ! tmp) { /* no ids ?? */
1262 DEBUG(5, ("no uidNumber, "
1263 "nor gidNumber attributes found\n"));
1267 id = strtoul(tmp, NULL, 10);
1269 (ctx->filter_low_id && (id < ctx->filter_low_id)) ||
1270 (ctx->filter_high_id && (id > ctx->filter_high_id))) {
1271 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1273 ctx->filter_low_id, ctx->filter_high_id));
1280 map->xid.type = type;
1282 map->status = ID_MAPPED;
1284 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1285 (unsigned long)map->xid.id, map->xid.type));
1288 /* free the ldap results */
1290 ldap_msgfree(result);
1294 if (multi && ids[idx]) { /* still some values to map */
1300 /* mark all unknwon/expired ones as unmapped */
1301 for (i = 0; ids[i]; i++) {
1302 if (ids[i]->status != ID_MAPPED)
1303 ids[i]->status = ID_UNMAPPED;
1307 talloc_free(memctx);
1311 /**********************************
1313 **********************************/
1315 /* TODO: change this: This function cannot be called to modify a mapping,
1316 * only set a new one */
1318 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
1319 const struct id_map *map)
1323 struct idmap_ldap_context *ctx;
1324 LDAPMessage *entry = NULL;
1325 LDAPMod **mods = NULL;
1332 /* Only do query if we are online */
1333 if (idmap_is_offline()) {
1334 return NT_STATUS_FILE_IS_OFFLINE;
1337 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
1339 switch(map->xid.type) {
1341 type = get_attr_key2string(sidmap_attr_list,
1342 LDAP_ATTR_UIDNUMBER);
1346 type = get_attr_key2string(sidmap_attr_list,
1347 LDAP_ATTR_GIDNUMBER);
1351 return NT_STATUS_INVALID_PARAMETER;
1354 memctx = talloc_new(ctx);
1356 DEBUG(0, ("Out of memory!\n"));
1357 return NT_STATUS_NO_MEMORY;
1360 id_str = talloc_asprintf(memctx, "%lu", (unsigned long)map->xid.id);
1361 CHECK_ALLOC_DONE(id_str);
1363 sid = talloc_strdup(memctx, sid_string_talloc(memctx, map->sid));
1364 CHECK_ALLOC_DONE(sid);
1366 dn = talloc_asprintf(memctx, "%s=%s,%s",
1367 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1370 CHECK_ALLOC_DONE(dn);
1372 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1373 "objectClass", LDAP_OBJ_IDMAP_ENTRY);
1375 smbldap_make_mod(ctx->smbldap_state->ldap_struct,
1376 entry, &mods, type, id_str);
1378 smbldap_make_mod(ctx->smbldap_state->ldap_struct, entry, &mods,
1379 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1383 DEBUG(2, ("ERROR: No mods?\n"));
1384 ret = NT_STATUS_UNSUCCESSFUL;
1388 /* TODO: remove conflicting mappings! */
1390 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SID_ENTRY);
1392 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn, sid, id_str));
1394 rc = smbldap_add(ctx->smbldap_state, dn, mods);
1395 ldap_mods_free(mods, True);
1397 if (rc != LDAP_SUCCESS) {
1398 char *ld_error = NULL;
1399 ldap_get_option(ctx->smbldap_state->ldap_struct,
1400 LDAP_OPT_ERROR_STRING, &ld_error);
1401 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
1402 "mapping [%s]\n", sid,
1403 (unsigned long)map->xid.id, type));
1404 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
1405 ld_error ? ld_error : "(NULL)", ldap_err2string (rc)));
1407 ldap_memfree(ld_error);
1409 ret = NT_STATUS_UNSUCCESSFUL;
1413 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
1414 "%lu [%s]\n", sid, (unsigned long)map->xid.id, type));
1419 talloc_free(memctx);
1423 /**********************************
1424 Close the idmap ldap instance
1425 **********************************/
1427 static NTSTATUS idmap_ldap_close(struct idmap_domain *dom)
1429 struct idmap_ldap_context *ctx;
1431 if (dom->private_data) {
1432 ctx = talloc_get_type(dom->private_data,
1433 struct idmap_ldap_context);
1436 dom->private_data = NULL;
1439 return NT_STATUS_OK;
1442 static struct idmap_methods idmap_ldap_methods = {
1444 .init = idmap_ldap_db_init,
1445 .unixids_to_sids = idmap_ldap_unixids_to_sids,
1446 .sids_to_unixids = idmap_ldap_sids_to_unixids,
1447 .set_mapping = idmap_ldap_set_mapping,
1448 .close_fn = idmap_ldap_close
1451 static struct idmap_alloc_methods idmap_ldap_alloc_methods = {
1453 .init = idmap_ldap_alloc_init,
1454 .allocate_id = idmap_ldap_allocate_id,
1455 .get_id_hwm = idmap_ldap_get_hwm,
1456 .set_id_hwm = idmap_ldap_set_hwm,
1457 .close_fn = idmap_ldap_alloc_close,
1458 /* .dump_data = TODO */
1461 NTSTATUS idmap_alloc_ldap_init(void)
1463 return smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1464 &idmap_ldap_alloc_methods);
1467 NTSTATUS idmap_ldap_init(void)
1471 /* FIXME: bad hack to actually register also the alloc_ldap module
1472 * without changining configure.in */
1473 ret = idmap_alloc_ldap_init();
1474 if (! NT_STATUS_IS_OK(ret)) {
1477 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1478 &idmap_ldap_methods);