s3:idmap_ldap: remove the (now unused) range from idmap_ldap_alloc_context
[Samba.git] / source3 / winbindd / idmap_ldap.c
blob9aa1925bc0dff50bf56d28f34390a786892b8692
1 /*
2 Unix SMB/CIFS implementation.
4 idmap LDAP backend
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/>.
25 #include "includes.h"
26 #include "winbindd.h"
27 #include "secrets.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_IDMAP
32 #include <lber.h>
33 #include <ldap.h>
35 #include "smbldap.h"
37 static char *idmap_fetch_secret(const char *backend, bool alloc,
38 const char *domain, const char *identity)
40 char *tmp, *ret;
41 int r;
43 if (alloc) {
44 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
45 } else {
46 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
49 if (r < 0)
50 return NULL;
52 strupper_m(tmp); /* make sure the key is case insensitive */
53 ret = secrets_fetch_generic(tmp, identity);
55 SAFE_FREE(tmp);
57 return ret;
60 struct idmap_ldap_context {
61 struct smbldap_state *smbldap_state;
62 char *url;
63 char *suffix;
64 char *user_dn;
65 bool anon;
68 struct idmap_ldap_alloc_context {
69 struct smbldap_state *smbldap_state;
70 char *url;
71 char *suffix;
72 char *user_dn;
75 #define CHECK_ALLOC_DONE(mem) do { \
76 if (!mem) { \
77 DEBUG(0, ("Out of memory!\n")); \
78 ret = NT_STATUS_NO_MEMORY; \
79 goto done; \
80 } } while (0)
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,
95 char **dn )
97 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
98 char *secret = NULL;
99 const char *tmp = NULL;
100 char *user_dn = NULL;
101 bool anon = False;
103 /* assume anonymous if we don't have a specified user */
105 tmp = lp_parm_const_string(-1, config_option, "ldap_user_dn", NULL);
107 if ( tmp ) {
108 if (!dom) {
109 /* only the alloc backend can pass in a NULL dom */
110 secret = idmap_fetch_secret("ldap", True,
111 NULL, tmp);
112 } else {
113 secret = idmap_fetch_secret("ldap", False,
114 dom->name, tmp);
117 if (!secret) {
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;
122 goto done;
124 *dn = talloc_strdup(mem_ctx, tmp);
125 CHECK_ALLOC_DONE(*dn);
126 } else {
127 if (!fetch_ldap_pw(&user_dn, &secret)) {
128 DEBUG(2, ("get_credentials: Failed to lookup ldap "
129 "bind creds. Using anonymous connection.\n"));
130 anon = True;
131 *dn = NULL;
132 } else {
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);
140 ret = NT_STATUS_OK;
142 done:
143 SAFE_FREE(secret);
145 return ret;
149 /**********************************************************************
150 Verify the sambaUnixIdPool entry in the directory.
151 **********************************************************************/
153 static NTSTATUS verify_idpool(struct idmap_domain *dom)
155 NTSTATUS ret;
156 TALLOC_CTX *ctx;
157 LDAPMessage *result = NULL;
158 LDAPMod **mods = NULL;
159 const char **attr_list;
160 char *filter;
161 int count;
162 int rc;
164 if ( ! idmap_alloc_ldap) {
165 return NT_STATUS_UNSUCCESSFUL;
168 ctx = talloc_new(idmap_alloc_ldap);
169 if ( ! ctx) {
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,
182 LDAP_SCOPE_SUBTREE,
183 filter,
184 attr_list,
186 &result);
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,
195 result);
197 ldap_msgfree(result);
199 if ( count > 1 ) {
200 DEBUG(0,("Multiple entries returned from %s (base == %s)\n",
201 filter, idmap_alloc_ldap->suffix));
202 ret = NT_STATUS_UNSUCCESSFUL;
203 goto done;
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),
218 uid_str);
219 smbldap_set_mod(&mods, LDAP_MOD_ADD,
220 get_attr_key2string(idpool_attr_list,
221 LDAP_ATTR_GIDNUMBER),
222 gid_str);
223 if (mods) {
224 rc = smbldap_modify(idmap_alloc_ldap->smbldap_state,
225 idmap_alloc_ldap->suffix,
226 mods);
227 ldap_mods_free(mods, True);
228 } else {
229 ret = NT_STATUS_UNSUCCESSFUL;
230 goto done;
234 ret = (rc == LDAP_SUCCESS)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
235 done:
236 talloc_free(ctx);
237 return ret;
240 /*****************************************************************************
241 Initialise idmap database.
242 *****************************************************************************/
244 static NTSTATUS idmap_ldap_alloc_init(struct idmap_domain *dom,
245 const char *params)
247 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
248 const char *tmp;
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);
261 } else {
262 tmp = lp_parm_const_string(-1, "idmap alloc config",
263 "ldap_url", NULL);
265 if ( ! tmp) {
266 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
267 ret = NT_STATUS_UNSUCCESSFUL;
268 goto done;
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();
281 if ( ! tmp) {
282 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
283 ret = NT_STATUS_UNSUCCESSFUL;
284 goto done;
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));
297 goto done;
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)));
307 goto done;
310 /* see if the idmap suffix and sub entries exists */
312 ret = verify_idpool(dom);
314 done:
315 if ( !NT_STATUS_IS_OK( ret ) )
316 TALLOC_FREE( idmap_alloc_ldap );
318 return ret;
321 /********************************
322 Allocate a new uid or gid
323 ********************************/
325 static NTSTATUS idmap_ldap_allocate_id(struct idmap_domain *dom,
326 struct unixid *xid)
328 TALLOC_CTX *ctx;
329 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
330 int rc = LDAP_SERVER_DOWN;
331 int count = 0;
332 LDAPMessage *result = NULL;
333 LDAPMessage *entry = NULL;
334 LDAPMod **mods = NULL;
335 char *id_str;
336 char *new_id_str;
337 char *filter = NULL;
338 const char *dn = NULL;
339 const char **attr_list;
340 const char *type;
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);
352 if ( ! ctx) {
353 DEBUG(0, ("Out of memory!\n"));
354 return NT_STATUS_NO_MEMORY;
357 /* get type */
358 switch (xid->type) {
360 case ID_TYPE_UID:
361 type = get_attr_key2string(idpool_attr_list,
362 LDAP_ATTR_UIDNUMBER);
363 break;
365 case ID_TYPE_GID:
366 type = get_attr_key2string(idpool_attr_list,
367 LDAP_ATTR_GIDNUMBER);
368 break;
370 default:
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));
390 goto done;
393 talloc_autofree_ldapmsg(ctx, result);
395 count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
396 result);
397 if (count != 1) {
398 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
399 goto done;
402 entry = ldap_first_entry(idmap_alloc_ldap->smbldap_state->ldap_struct,
403 result);
405 dn = smbldap_talloc_dn(ctx,
406 idmap_alloc_ldap->smbldap_state->ldap_struct,
407 entry);
408 if ( ! dn) {
409 goto done;
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;
416 goto done;
419 xid->id = strtoul(id_str, NULL, 10);
421 /* make sure we still have room to grow */
423 switch (xid->type) {
424 case ID_TYPE_UID:
425 if (xid->id > dom->high_id) {
426 DEBUG(0,("Cannot allocate uid above %lu!\n",
427 (unsigned long)dom->high_id));
428 goto done;
430 break;
432 case ID_TYPE_GID:
433 if (xid->id > dom->high_id) {
434 DEBUG(0,("Cannot allocate gid above %lu!\n",
435 (unsigned long)dom->high_id));
436 goto done;
438 break;
440 default:
441 /* impossible */
442 goto done;
445 new_id_str = talloc_asprintf(ctx, "%lu", (unsigned long)xid->id + 1);
446 if ( ! new_id_str) {
447 DEBUG(0,("Out of memory\n"));
448 ret = NT_STATUS_NO_MEMORY;
449 goto done;
452 smbldap_set_mod(&mods, LDAP_MOD_DELETE, type, id_str);
453 smbldap_set_mod(&mods, LDAP_MOD_ADD, type, new_id_str);
455 if (mods == NULL) {
456 DEBUG(0,("smbldap_set_mod() failed.\n"));
457 goto done;
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));
470 goto done;
473 ret = NT_STATUS_OK;
475 done:
476 talloc_free(ctx);
477 return ret;
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);
492 return NT_STATUS_OK;
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 */
506 return 0;
509 /********************************
510 Initialise idmap database.
511 ********************************/
513 static NTSTATUS idmap_ldap_db_init(struct idmap_domain *dom,
514 const char *params)
516 NTSTATUS ret;
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);
527 if ( ! ctx) {
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 */
534 } else {
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;
539 goto done;
543 if (params != NULL) {
544 /* assume location is the only parameter */
545 ctx->url = talloc_strdup(ctx, params);
546 } else {
547 tmp = lp_parm_const_string(-1, config_option, "ldap_url", NULL);
549 if ( ! tmp) {
550 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
551 ret = NT_STATUS_UNSUCCESSFUL;
552 goto done;
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();
564 if ( ! tmp) {
565 DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
566 ret = NT_STATUS_UNSUCCESSFUL;
567 goto done;
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));
578 goto done;
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)));
586 goto done;
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 talloc_free(config_option);
597 return NT_STATUS_OK;
599 /*failed */
600 done:
601 talloc_free(ctx);
602 return ret;
605 /* max number of ids requested per batch query */
606 #define IDMAP_LDAP_MAX_IDS 30
608 /**********************************
609 lookup a set of unix ids.
610 **********************************/
612 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
613 * in maps for a match */
614 static struct id_map *find_map_by_id(struct id_map **maps,
615 enum id_type type,
616 uint32_t id)
618 int i;
620 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
621 if (maps[i] == NULL) { /* end of the run */
622 return NULL;
624 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
625 return maps[i];
629 return NULL;
632 static NTSTATUS idmap_ldap_unixids_to_sids(struct idmap_domain *dom,
633 struct id_map **ids)
635 NTSTATUS ret;
636 TALLOC_CTX *memctx;
637 struct idmap_ldap_context *ctx;
638 LDAPMessage *result = NULL;
639 LDAPMessage *entry = NULL;
640 const char *uidNumber;
641 const char *gidNumber;
642 const char **attr_list;
643 char *filter = NULL;
644 bool multi = False;
645 int idx = 0;
646 int bidx = 0;
647 int count;
648 int rc;
649 int i;
651 /* Only do query if we are online */
652 if (idmap_is_offline()) {
653 return NT_STATUS_FILE_IS_OFFLINE;
656 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
658 memctx = talloc_new(ctx);
659 if ( ! memctx) {
660 DEBUG(0, ("Out of memory!\n"));
661 return NT_STATUS_NO_MEMORY;
664 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
665 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
667 attr_list = get_attr_list(memctx, sidmap_attr_list);
669 if ( ! ids[1]) {
670 /* if we are requested just one mapping use the simple filter */
672 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%lu))",
673 LDAP_OBJ_IDMAP_ENTRY,
674 (ids[0]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
675 (unsigned long)ids[0]->xid.id);
676 CHECK_ALLOC_DONE(filter);
677 DEBUG(10, ("Filter: [%s]\n", filter));
678 } else {
679 /* multiple mappings */
680 multi = True;
683 for (i = 0; ids[i]; i++) {
684 ids[i]->status = ID_UNKNOWN;
687 again:
688 if (multi) {
690 talloc_free(filter);
691 filter = talloc_asprintf(memctx,
692 "(&(objectClass=%s)(|",
693 LDAP_OBJ_IDMAP_ENTRY);
694 CHECK_ALLOC_DONE(filter);
696 bidx = idx;
697 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
698 filter = talloc_asprintf_append_buffer(filter, "(%s=%lu)",
699 (ids[idx]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
700 (unsigned long)ids[idx]->xid.id);
701 CHECK_ALLOC_DONE(filter);
703 filter = talloc_asprintf_append_buffer(filter, "))");
704 CHECK_ALLOC_DONE(filter);
705 DEBUG(10, ("Filter: [%s]\n", filter));
706 } else {
707 bidx = 0;
708 idx = 1;
711 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
712 filter, attr_list, 0, &result);
714 if (rc != LDAP_SUCCESS) {
715 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc)));
716 ret = NT_STATUS_UNSUCCESSFUL;
717 goto done;
720 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
722 if (count == 0) {
723 DEBUG(10, ("NO SIDs found\n"));
726 for (i = 0; i < count; i++) {
727 char *sidstr = NULL;
728 char *tmp = NULL;
729 enum id_type type;
730 struct id_map *map;
731 uint32_t id;
733 if (i == 0) { /* first entry */
734 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
735 result);
736 } else { /* following ones */
737 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
738 entry);
740 if ( ! entry) {
741 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
742 "from results\n"));
743 break;
746 /* first check if the SID is present */
747 sidstr = smbldap_talloc_single_attribute(
748 ctx->smbldap_state->ldap_struct,
749 entry, LDAP_ATTRIBUTE_SID, memctx);
750 if ( ! sidstr) { /* no sid, skip entry */
751 DEBUG(2, ("WARNING SID not found on entry\n"));
752 continue;
755 /* now try to see if it is a uid, if not try with a gid
756 * (gid is more common, but in case both uidNumber and
757 * gidNumber are returned the SID is mapped to the uid
758 *not the gid) */
759 type = ID_TYPE_UID;
760 tmp = smbldap_talloc_single_attribute(
761 ctx->smbldap_state->ldap_struct,
762 entry, uidNumber, memctx);
763 if ( ! tmp) {
764 type = ID_TYPE_GID;
765 tmp = smbldap_talloc_single_attribute(
766 ctx->smbldap_state->ldap_struct,
767 entry, gidNumber, memctx);
769 if ( ! tmp) { /* wow very strange entry, how did it match ? */
770 DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
771 "nor gidNumber returned\n", sidstr));
772 TALLOC_FREE(sidstr);
773 continue;
776 id = strtoul(tmp, NULL, 10);
777 if (!idmap_unix_id_is_in_range(id, dom)) {
778 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
779 "Filtered!\n", id,
780 dom->low_id, dom->high_id));
781 TALLOC_FREE(sidstr);
782 TALLOC_FREE(tmp);
783 continue;
785 TALLOC_FREE(tmp);
787 map = find_map_by_id(&ids[bidx], type, id);
788 if (!map) {
789 DEBUG(2, ("WARNING: couldn't match sid (%s) "
790 "with requested ids\n", sidstr));
791 TALLOC_FREE(sidstr);
792 continue;
795 if ( ! string_to_sid(map->sid, sidstr)) {
796 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
797 TALLOC_FREE(sidstr);
798 continue;
801 if (map->status == ID_MAPPED) {
802 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
803 "overwriting mapping %u -> %s with %u -> %s\n",
804 (type == ID_TYPE_UID) ? "UID" : "GID",
805 id, sid_string_dbg(map->sid), id, sidstr));
808 TALLOC_FREE(sidstr);
810 /* mapped */
811 map->status = ID_MAPPED;
813 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
814 (unsigned long)map->xid.id, map->xid.type));
817 /* free the ldap results */
818 if (result) {
819 ldap_msgfree(result);
820 result = NULL;
823 if (multi && ids[idx]) { /* still some values to map */
824 goto again;
827 ret = NT_STATUS_OK;
829 /* mark all unknwon/expired ones as unmapped */
830 for (i = 0; ids[i]; i++) {
831 if (ids[i]->status != ID_MAPPED)
832 ids[i]->status = ID_UNMAPPED;
835 done:
836 talloc_free(memctx);
837 return ret;
840 /**********************************
841 lookup a set of sids.
842 **********************************/
844 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
845 * in maps for a match */
846 static struct id_map *find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
848 int i;
850 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
851 if (maps[i] == NULL) { /* end of the run */
852 return NULL;
854 if (sid_equal(maps[i]->sid, sid)) {
855 return maps[i];
859 return NULL;
862 static NTSTATUS idmap_ldap_sids_to_unixids(struct idmap_domain *dom,
863 struct id_map **ids)
865 LDAPMessage *entry = NULL;
866 NTSTATUS ret;
867 TALLOC_CTX *memctx;
868 struct idmap_ldap_context *ctx;
869 LDAPMessage *result = NULL;
870 const char *uidNumber;
871 const char *gidNumber;
872 const char **attr_list;
873 char *filter = NULL;
874 bool multi = False;
875 int idx = 0;
876 int bidx = 0;
877 int count;
878 int rc;
879 int i;
881 /* Only do query if we are online */
882 if (idmap_is_offline()) {
883 return NT_STATUS_FILE_IS_OFFLINE;
886 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
888 memctx = talloc_new(ctx);
889 if ( ! memctx) {
890 DEBUG(0, ("Out of memory!\n"));
891 return NT_STATUS_NO_MEMORY;
894 uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
895 gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
897 attr_list = get_attr_list(memctx, sidmap_attr_list);
899 if ( ! ids[1]) {
900 /* if we are requested just one mapping use the simple filter */
902 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%s))",
903 LDAP_OBJ_IDMAP_ENTRY,
904 LDAP_ATTRIBUTE_SID,
905 sid_string_talloc(memctx, ids[0]->sid));
906 CHECK_ALLOC_DONE(filter);
907 DEBUG(10, ("Filter: [%s]\n", filter));
908 } else {
909 /* multiple mappings */
910 multi = True;
913 for (i = 0; ids[i]; i++) {
914 ids[i]->status = ID_UNKNOWN;
917 again:
918 if (multi) {
920 TALLOC_FREE(filter);
921 filter = talloc_asprintf(memctx,
922 "(&(objectClass=%s)(|",
923 LDAP_OBJ_IDMAP_ENTRY);
924 CHECK_ALLOC_DONE(filter);
926 bidx = idx;
927 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
928 filter = talloc_asprintf_append_buffer(filter, "(%s=%s)",
929 LDAP_ATTRIBUTE_SID,
930 sid_string_talloc(memctx,
931 ids[idx]->sid));
932 CHECK_ALLOC_DONE(filter);
934 filter = talloc_asprintf_append_buffer(filter, "))");
935 CHECK_ALLOC_DONE(filter);
936 DEBUG(10, ("Filter: [%s]", filter));
937 } else {
938 bidx = 0;
939 idx = 1;
942 rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
943 filter, attr_list, 0, &result);
945 if (rc != LDAP_SUCCESS) {
946 DEBUG(3,("Failure looking up sids (%s)\n",
947 ldap_err2string(rc)));
948 ret = NT_STATUS_UNSUCCESSFUL;
949 goto done;
952 count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
954 if (count == 0) {
955 DEBUG(10, ("NO SIDs found\n"));
958 for (i = 0; i < count; i++) {
959 char *sidstr = NULL;
960 char *tmp = NULL;
961 enum id_type type;
962 struct id_map *map;
963 struct dom_sid sid;
964 uint32_t id;
966 if (i == 0) { /* first entry */
967 entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
968 result);
969 } else { /* following ones */
970 entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
971 entry);
973 if ( ! entry) {
974 DEBUG(2, ("ERROR: Unable to fetch ldap entries "
975 "from results\n"));
976 break;
979 /* first check if the SID is present */
980 sidstr = smbldap_talloc_single_attribute(
981 ctx->smbldap_state->ldap_struct,
982 entry, LDAP_ATTRIBUTE_SID, memctx);
983 if ( ! sidstr) { /* no sid ??, skip entry */
984 DEBUG(2, ("WARNING SID not found on entry\n"));
985 continue;
988 if ( ! string_to_sid(&sid, sidstr)) {
989 DEBUG(2, ("ERROR: Invalid SID on entry\n"));
990 TALLOC_FREE(sidstr);
991 continue;
994 map = find_map_by_sid(&ids[bidx], &sid);
995 if (!map) {
996 DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
997 "in ids", sidstr));
998 TALLOC_FREE(sidstr);
999 continue;
1002 /* now try to see if it is a uid, if not try with a gid
1003 * (gid is more common, but in case both uidNumber and
1004 * gidNumber are returned the SID is mapped to the uid
1005 * not the gid) */
1006 type = ID_TYPE_UID;
1007 tmp = smbldap_talloc_single_attribute(
1008 ctx->smbldap_state->ldap_struct,
1009 entry, uidNumber, memctx);
1010 if ( ! tmp) {
1011 type = ID_TYPE_GID;
1012 tmp = smbldap_talloc_single_attribute(
1013 ctx->smbldap_state->ldap_struct,
1014 entry, gidNumber, memctx);
1016 if ( ! tmp) { /* no ids ?? */
1017 DEBUG(5, ("no uidNumber, "
1018 "nor gidNumber attributes found\n"));
1019 TALLOC_FREE(sidstr);
1020 continue;
1023 id = strtoul(tmp, NULL, 10);
1024 if (!idmap_unix_id_is_in_range(id, dom)) {
1025 DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1026 "Filtered!\n", id,
1027 dom->low_id, dom->high_id));
1028 TALLOC_FREE(sidstr);
1029 TALLOC_FREE(tmp);
1030 continue;
1032 TALLOC_FREE(tmp);
1034 if (map->status == ID_MAPPED) {
1035 DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1036 "overwriting mapping %s -> %u with %s -> %u\n",
1037 (type == ID_TYPE_UID) ? "UID" : "GID",
1038 sidstr, map->xid.id, sidstr, id));
1041 TALLOC_FREE(sidstr);
1043 /* mapped */
1044 map->xid.type = type;
1045 map->xid.id = id;
1046 map->status = ID_MAPPED;
1048 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1049 (unsigned long)map->xid.id, map->xid.type));
1052 /* free the ldap results */
1053 if (result) {
1054 ldap_msgfree(result);
1055 result = NULL;
1058 if (multi && ids[idx]) { /* still some values to map */
1059 goto again;
1062 ret = NT_STATUS_OK;
1064 /* mark all unknwon/expired ones as unmapped */
1065 for (i = 0; ids[i]; i++) {
1066 if (ids[i]->status != ID_MAPPED)
1067 ids[i]->status = ID_UNMAPPED;
1070 done:
1071 talloc_free(memctx);
1072 return ret;
1075 /**********************************
1076 set a mapping.
1077 **********************************/
1079 /* TODO: change this: This function cannot be called to modify a mapping,
1080 * only set a new one */
1082 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
1083 const struct id_map *map)
1085 NTSTATUS ret;
1086 TALLOC_CTX *memctx;
1087 struct idmap_ldap_context *ctx;
1088 LDAPMessage *entry = NULL;
1089 LDAPMod **mods = NULL;
1090 const char *type;
1091 char *id_str;
1092 char *sid;
1093 char *dn;
1094 int rc = -1;
1096 /* Only do query if we are online */
1097 if (idmap_is_offline()) {
1098 return NT_STATUS_FILE_IS_OFFLINE;
1101 ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
1103 switch(map->xid.type) {
1104 case ID_TYPE_UID:
1105 type = get_attr_key2string(sidmap_attr_list,
1106 LDAP_ATTR_UIDNUMBER);
1107 break;
1109 case ID_TYPE_GID:
1110 type = get_attr_key2string(sidmap_attr_list,
1111 LDAP_ATTR_GIDNUMBER);
1112 break;
1114 default:
1115 return NT_STATUS_INVALID_PARAMETER;
1118 memctx = talloc_new(ctx);
1119 if ( ! memctx) {
1120 DEBUG(0, ("Out of memory!\n"));
1121 return NT_STATUS_NO_MEMORY;
1124 id_str = talloc_asprintf(memctx, "%lu", (unsigned long)map->xid.id);
1125 CHECK_ALLOC_DONE(id_str);
1127 sid = talloc_strdup(memctx, sid_string_talloc(memctx, map->sid));
1128 CHECK_ALLOC_DONE(sid);
1130 dn = talloc_asprintf(memctx, "%s=%s,%s",
1131 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1132 sid,
1133 ctx->suffix);
1134 CHECK_ALLOC_DONE(dn);
1136 smbldap_set_mod(&mods, LDAP_MOD_ADD,
1137 "objectClass", LDAP_OBJ_IDMAP_ENTRY);
1139 smbldap_make_mod(ctx->smbldap_state->ldap_struct,
1140 entry, &mods, type, id_str);
1142 smbldap_make_mod(ctx->smbldap_state->ldap_struct, entry, &mods,
1143 get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1144 sid);
1146 if ( ! mods) {
1147 DEBUG(2, ("ERROR: No mods?\n"));
1148 ret = NT_STATUS_UNSUCCESSFUL;
1149 goto done;
1152 /* TODO: remove conflicting mappings! */
1154 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SID_ENTRY);
1156 DEBUG(10, ("Set DN %s (%s -> %s)\n", dn, sid, id_str));
1158 rc = smbldap_add(ctx->smbldap_state, dn, mods);
1159 ldap_mods_free(mods, True);
1161 if (rc != LDAP_SUCCESS) {
1162 char *ld_error = NULL;
1163 ldap_get_option(ctx->smbldap_state->ldap_struct,
1164 LDAP_OPT_ERROR_STRING, &ld_error);
1165 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
1166 "mapping [%s]\n", sid,
1167 (unsigned long)map->xid.id, type));
1168 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
1169 ld_error ? ld_error : "(NULL)", ldap_err2string (rc)));
1170 if (ld_error) {
1171 ldap_memfree(ld_error);
1173 ret = NT_STATUS_UNSUCCESSFUL;
1174 goto done;
1177 DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
1178 "%lu [%s]\n", sid, (unsigned long)map->xid.id, type));
1180 ret = NT_STATUS_OK;
1182 done:
1183 talloc_free(memctx);
1184 return ret;
1187 /**********************************
1188 Close the idmap ldap instance
1189 **********************************/
1191 static NTSTATUS idmap_ldap_close(struct idmap_domain *dom)
1193 struct idmap_ldap_context *ctx;
1195 if (dom->private_data) {
1196 ctx = talloc_get_type(dom->private_data,
1197 struct idmap_ldap_context);
1199 talloc_free(ctx);
1200 dom->private_data = NULL;
1203 return NT_STATUS_OK;
1206 static struct idmap_methods idmap_ldap_methods = {
1208 .init = idmap_ldap_db_init,
1209 .unixids_to_sids = idmap_ldap_unixids_to_sids,
1210 .sids_to_unixids = idmap_ldap_sids_to_unixids,
1211 .allocate_id = idmap_ldap_get_new_id,
1212 .close_fn = idmap_ldap_close
1215 NTSTATUS idmap_ldap_init(void);
1216 NTSTATUS idmap_ldap_init(void)
1218 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1219 &idmap_ldap_methods);