2 * Unix SMB/CIFS implementation.
4 * Id mapping using LDAP records as defined in RFC 2307
6 * The SID<->uid/gid mapping is performed in two steps: 1) Query the
7 * AD server for the name<->sid mapping. 2) Query an LDAP server
8 * according to RFC 2307 for the name<->uid/gid mapping.
10 * Copyright (C) Christof Schmitt 2012,2013
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 #include "nsswitch/winbind_client.h"
32 #include "lib/winbind_util.h"
35 * Config and connection info per domain.
37 struct idmap_rfc2307_context {
38 const char *bind_path_user;
39 const char *bind_path_group;
40 const char *ldap_domain;
45 * Pointer to ldap struct in ads or smbldap_state, has to be
46 * updated after connecting to server
50 /* Optional function to check connection to server */
51 NTSTATUS (*check_connection)(struct idmap_domain *dom);
53 /* Issue ldap query */
54 NTSTATUS (*search)(struct idmap_rfc2307_context *ctx,
55 const char *bind_path, const char *expr,
56 const char **attrs, LDAPMessage **res);
58 /* Access to LDAP in AD server */
61 /* Access to stand-alone LDAP server */
62 struct smbldap_state *smbldap_state;
66 * backend functions for LDAP queries through ADS
69 static NTSTATUS idmap_rfc2307_ads_check_connection(struct idmap_domain *dom)
71 struct idmap_rfc2307_context *ctx;
72 const char *dom_name = dom->name;
75 DEBUG(10, ("ad_idmap_cached_connection: called for domain '%s'\n",
78 ctx = talloc_get_type(dom->private_data, struct idmap_rfc2307_context);
79 dom_name = ctx->ldap_domain ? ctx->ldap_domain : dom->name;
81 status = ads_idmap_cached_connection(&ctx->ads, dom_name);
82 if (ADS_ERR_OK(status)) {
83 ctx->ldap = ctx->ads->ldap.ld;
85 DEBUG(1, ("Could not connect to domain %s: %s\n", dom->name,
89 return ads_ntstatus(status);
92 static NTSTATUS idmap_rfc2307_ads_search(struct idmap_rfc2307_context *ctx,
93 const char *bind_path,
100 status = ads_do_search_retry(ctx->ads, bind_path,
101 LDAP_SCOPE_SUBTREE, expr, attrs, result);
103 if (!ADS_ERR_OK(status)) {
104 return ads_ntstatus(status);
107 ctx->ldap = ctx->ads->ldap.ld;
108 return ads_ntstatus(status);
111 static NTSTATUS idmap_rfc2307_init_ads(struct idmap_rfc2307_context *ctx,
112 const char *domain_name)
114 const char *ldap_domain;
116 ctx->search = idmap_rfc2307_ads_search;
117 ctx->check_connection = idmap_rfc2307_ads_check_connection;
119 ldap_domain = idmap_config_const_string(domain_name, "ldap_domain",
122 ctx->ldap_domain = talloc_strdup(ctx, ldap_domain);
123 if (ctx->ldap_domain == NULL) {
124 return NT_STATUS_NO_MEMORY;
132 * backend function for LDAP queries through stand-alone LDAP server
135 static NTSTATUS idmap_rfc2307_ldap_search(struct idmap_rfc2307_context *ctx,
136 const char *bind_path,
139 LDAPMessage **result)
143 ret = smbldap_search(ctx->smbldap_state, bind_path, LDAP_SCOPE_SUBTREE,
144 expr, attrs, 0, result);
145 ctx->ldap = ctx->smbldap_state->ldap_struct;
147 if (ret == LDAP_SUCCESS) {
151 return NT_STATUS_LDAP(ret);
154 static bool idmap_rfc2307_get_uint32(LDAP *ldap, LDAPMessage *entry,
155 const char *field, uint32_t *value)
160 b = smbldap_get_single_attribute(ldap, entry, field, str, sizeof(str));
169 static NTSTATUS idmap_rfc2307_init_ldap(struct idmap_rfc2307_context *ctx,
170 const char *domain_name)
175 const char *ldap_url, *user_dn;
176 TALLOC_CTX *mem_ctx = ctx;
178 ldap_url = idmap_config_const_string(domain_name, "ldap_url", NULL);
180 DEBUG(1, ("ERROR: missing idmap ldap url\n"));
181 return NT_STATUS_UNSUCCESSFUL;
184 url = talloc_strdup(talloc_tos(), ldap_url);
186 user_dn = idmap_config_const_string(domain_name, "ldap_user_dn", NULL);
188 secret = idmap_fetch_secret("ldap", domain_name, user_dn);
190 ret = NT_STATUS_ACCESS_DENIED;
195 /* assume anonymous if we don't have a specified user */
196 ret = smbldap_init(mem_ctx, winbind_event_context(), url,
197 (user_dn == NULL), user_dn, secret,
198 &ctx->smbldap_state);
200 if (!NT_STATUS_IS_OK(ret)) {
201 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", url));
205 ctx->search = idmap_rfc2307_ldap_search;
213 * common code for stand-alone LDAP and ADS
216 static void idmap_rfc2307_map_sid_results(struct idmap_rfc2307_context *ctx,
220 const char *dom_name,
221 const char **attrs, int type)
226 count = ldap_count_entries(ctx->ldap, result);
228 for (i = 0; i < count; i++) {
230 enum lsa_SidType lsa_type;
236 entry = ldap_first_entry(ctx->ldap, result);
238 entry = ldap_next_entry(ctx->ldap, result);
241 DEBUG(2, ("Unable to fetch entry.\n"));
245 name = smbldap_talloc_single_attribute(ctx->ldap, entry,
248 DEBUG(1, ("Could not get user name\n"));
252 b = idmap_rfc2307_get_uint32(ctx->ldap, entry, attrs[1], &id);
254 DEBUG(1, ("Could not pull id for record %s\n", name));
258 map = idmap_find_map_by_id(ids, type, id);
260 DEBUG(1, ("Could not find id %d, name %s\n", id, name));
264 if (ctx->realm != NULL) {
265 /* Strip @realm from user or group name */
268 delim = strchr(name, '@');
274 /* by default calls to winbindd are disabled
275 the following call will not recurse so this is safe */
277 /* Lookup name from PDC using lsa_lookup_names() */
278 b = winbind_lookup_name(dom_name, name, map->sid, &lsa_type);
282 DEBUG(1, ("SID lookup failed for id %d, %s\n",
287 if (type == ID_TYPE_UID && lsa_type != SID_NAME_USER) {
288 DEBUG(1, ("Wrong type %d for user name %s\n",
293 if (type == ID_TYPE_GID && lsa_type != SID_NAME_DOM_GRP &&
294 lsa_type != SID_NAME_ALIAS &&
295 lsa_type != SID_NAME_WKN_GRP) {
296 DEBUG(1, ("Wrong type %d for group name %s\n",
301 map->status = ID_MAPPED;
306 * Map unixids to names and then to sids.
308 static NTSTATUS idmap_rfc2307_unixids_to_sids(struct idmap_domain *dom,
311 struct idmap_rfc2307_context *ctx;
312 char *fltr_usr = NULL, *fltr_grp = NULL;
314 int cnt_usr = 0, cnt_grp = 0, idx = 0, bidx = 0;
315 LDAPMessage *result = NULL;
318 ctx = talloc_get_type(dom->private_data, struct idmap_rfc2307_context);
319 mem_ctx = talloc_new(ctx);
321 return NT_STATUS_NO_MEMORY;
324 if (ctx->check_connection) {
325 ret = ctx->check_connection(dom);
326 if (!NT_STATUS_IS_OK(ret)) {
335 /* prepare new user query, see getpwuid() in RFC2307 */
336 fltr_usr = talloc_asprintf(mem_ctx,
337 "(&(objectClass=posixAccount)(|");
341 /* prepare new group query, see getgrgid() in RFC2307 */
342 fltr_grp = talloc_asprintf(mem_ctx,
343 "(&(objectClass=posixGroup)(|");
346 if (!fltr_usr || !fltr_grp) {
347 ret = NT_STATUS_NO_MEMORY;
351 while (cnt_usr < IDMAP_LDAP_MAX_IDS &&
352 cnt_grp < IDMAP_LDAP_MAX_IDS && ids[idx]) {
354 switch (ids[idx]->xid.type) {
356 fltr_usr = talloc_asprintf_append_buffer(fltr_usr,
357 "(uidNumber=%d)", ids[idx]->xid.id);
361 fltr_grp = talloc_asprintf_append_buffer(fltr_grp,
362 "(gidNumber=%d)", ids[idx]->xid.id);
366 DEBUG(3, ("Error: unknown ID type %d\n",
367 ids[idx]->xid.type));
368 ret = NT_STATUS_UNSUCCESSFUL;
372 if (!fltr_usr || !fltr_grp) {
373 ret = NT_STATUS_NO_MEMORY;
380 if (cnt_usr == IDMAP_LDAP_MAX_IDS || (cnt_usr != 0 && !ids[idx])) {
381 const char *attrs[] = { NULL, /* uid or cn */
385 fltr_usr = talloc_strdup_append(fltr_usr, "))");
387 ret = NT_STATUS_NO_MEMORY;
391 attrs[0] = ctx->user_cn ? "cn" : "uid";
392 ret = ctx->search(ctx, ctx->bind_path_user, fltr_usr, attrs,
394 if (!NT_STATUS_IS_OK(ret)) {
398 idmap_rfc2307_map_sid_results(ctx, mem_ctx, &ids[bidx], result,
399 dom->name, attrs, ID_TYPE_UID);
401 TALLOC_FREE(fltr_usr);
404 if (cnt_grp == IDMAP_LDAP_MAX_IDS || (cnt_grp != 0 && !ids[idx])) {
405 const char *attrs[] = { "cn", "gidNumber", NULL };
407 fltr_grp = talloc_strdup_append(fltr_grp, "))");
409 ret = NT_STATUS_NO_MEMORY;
412 ret = ctx->search(ctx, ctx->bind_path_group, fltr_grp, attrs,
414 if (!NT_STATUS_IS_OK(ret)) {
418 idmap_rfc2307_map_sid_results(ctx, mem_ctx, &ids[bidx], result,
419 dom->name, attrs, ID_TYPE_GID);
421 TALLOC_FREE(fltr_grp);
431 talloc_free(mem_ctx);
435 struct idmap_rfc2307_map {
442 * Lookup names for SIDS and store the data in the local mapping
445 static NTSTATUS idmap_rfc_2307_sids_to_names(TALLOC_CTX *mem_ctx,
447 struct idmap_rfc2307_map *maps,
448 struct idmap_rfc2307_context *ctx)
452 for (i = 0; ids[i]; i++) {
453 const char *domain, *name;
454 enum lsa_SidType lsa_type;
455 struct id_map *id = ids[i];
456 struct idmap_rfc2307_map *map = &maps[i];
459 /* by default calls to winbindd are disabled
460 the following call will not recurse so this is safe */
462 b = winbind_lookup_sid(mem_ctx, ids[i]->sid, &domain, &name,
467 DEBUG(1, ("Lookup sid %s failed.\n",
468 sid_string_dbg(ids[i]->sid)));
474 id->xid.type = map->type = ID_TYPE_UID;
475 if (ctx->user_cn && ctx->realm != NULL) {
476 name = talloc_asprintf(mem_ctx, "%s@%s",
479 id->xid.type = map->type = ID_TYPE_UID;
482 case SID_NAME_DOM_GRP:
484 case SID_NAME_WKN_GRP:
485 if (ctx->realm != NULL) {
486 name = talloc_asprintf(mem_ctx, "%s@%s",
489 id->xid.type = map->type = ID_TYPE_GID;
493 DEBUG(1, ("Unknown lsa type %d for sid %s\n",
494 lsa_type, sid_string_dbg(id->sid)));
495 id->status = ID_UNMAPPED;
500 id->status = ID_UNKNOWN;
501 map->name = strupper_talloc(mem_ctx, name);
504 return NT_STATUS_NO_MEMORY;
512 * Find id_map entry by looking up the name in the internal
515 static struct id_map* idmap_rfc2307_find_map(struct idmap_rfc2307_map *maps,
521 DEBUG(10, ("Looking for name %s, type %d\n", name, type));
523 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
524 if (maps[i].map == NULL) { /* end of the run */
527 DEBUG(10, ("Entry %d: name %s, type %d\n",
528 i, maps[i].name, maps[i].type));
529 if (type == maps[i].type && strcmp(name, maps[i].name) == 0) {
537 static void idmap_rfc2307_map_xid_results(struct idmap_rfc2307_context *ctx,
539 struct idmap_rfc2307_map *maps,
541 struct idmap_domain *dom,
542 const char **attrs, enum id_type type)
547 count = ldap_count_entries(ctx->ldap, result);
549 for (i = 0; i < count; i++) {
553 struct id_map *id_map;
556 entry = ldap_first_entry(ctx->ldap, result);
558 entry = ldap_next_entry(ctx->ldap, result);
561 DEBUG(2, ("Unable to fetch entry.\n"));
565 name = smbldap_talloc_single_attribute(ctx->ldap, entry,
568 DEBUG(1, ("Could not get user name\n"));
572 b = idmap_rfc2307_get_uint32(ctx->ldap, entry, attrs[1], &id);
574 DEBUG(5, ("Could not pull id for record %s\n", name));
578 if (!idmap_unix_id_is_in_range(id, dom)) {
579 DEBUG(5, ("Requested id (%u) out of range (%u - %u).\n",
580 id, dom->low_id, dom->high_id));
584 if (!strupper_m(name)) {
585 DEBUG(5, ("Could not convert %s to uppercase\n", name));
588 id_map = idmap_rfc2307_find_map(maps, type, name);
590 DEBUG(0, ("Could not find mapping entry for name %s\n",
596 id_map->status = ID_MAPPED;
601 * Map sids to names and then to unixids.
603 static NTSTATUS idmap_rfc2307_sids_to_unixids(struct idmap_domain *dom,
606 struct idmap_rfc2307_context *ctx;
608 struct idmap_rfc2307_map *int_maps;
609 int cnt_usr = 0, cnt_grp = 0, idx = 0;
610 char *fltr_usr = NULL, *fltr_grp = NULL;
614 ctx = talloc_get_type(dom->private_data, struct idmap_rfc2307_context);
615 mem_ctx = talloc_new(talloc_tos());
617 return NT_STATUS_NO_MEMORY;
620 if (ctx->check_connection) {
621 ret = ctx->check_connection(dom);
622 if (!NT_STATUS_IS_OK(ret)) {
627 for (i = 0; ids[i]; i++);
628 int_maps = talloc_zero_array(mem_ctx, struct idmap_rfc2307_map, i);
630 ret = NT_STATUS_NO_MEMORY;
634 ret = idmap_rfc_2307_sids_to_names(mem_ctx, ids, int_maps, ctx);
635 if (!NT_STATUS_IS_OK(ret)) {
641 /* prepare new user query, see getpwuid() in RFC2307 */
642 fltr_usr = talloc_asprintf(mem_ctx,
643 "(&(objectClass=posixAccount)(|");
647 /* prepare new group query, see getgrgid() in RFC2307 */
648 fltr_grp = talloc_asprintf(mem_ctx,
649 "(&(objectClass=posixGroup)(|");
652 if (!fltr_usr || !fltr_grp) {
653 ret = NT_STATUS_NO_MEMORY;
657 while (cnt_usr < IDMAP_LDAP_MAX_IDS &&
658 cnt_grp < IDMAP_LDAP_MAX_IDS && ids[idx]) {
659 struct id_map *id = ids[idx];
660 struct idmap_rfc2307_map *map = &int_maps[idx];
662 switch(id->xid.type) {
664 fltr_usr = talloc_asprintf_append_buffer(fltr_usr,
665 "(%s=%s)", (ctx->user_cn ? "cn" : "uid"),
671 fltr_grp = talloc_asprintf_append_buffer(fltr_grp,
672 "(cn=%s)", map->name);
680 if (!fltr_usr || !fltr_grp) {
681 ret = NT_STATUS_NO_MEMORY;
688 if (cnt_usr == IDMAP_LDAP_MAX_IDS || (cnt_usr != 0 && !ids[idx])) {
689 const char *attrs[] = { NULL, /* uid or cn */
694 fltr_usr = talloc_strdup_append(fltr_usr, "))");
696 ret = NT_STATUS_NO_MEMORY;
700 attrs[0] = ctx->user_cn ? "cn" : "uid";
701 ret = ctx->search(ctx, ctx->bind_path_user, fltr_usr, attrs,
703 if (!NT_STATUS_IS_OK(ret)) {
707 idmap_rfc2307_map_xid_results(ctx, mem_ctx, int_maps,
708 result, dom, attrs, ID_TYPE_UID);
711 TALLOC_FREE(fltr_usr);
714 if (cnt_grp == IDMAP_LDAP_MAX_IDS || (cnt_grp != 0 && !ids[idx])) {
715 const char *attrs[] = {"cn", "gidNumber", NULL };
718 fltr_grp = talloc_strdup_append(fltr_grp, "))");
720 ret = NT_STATUS_NO_MEMORY;
724 ret = ctx->search(ctx, ctx->bind_path_group, fltr_grp, attrs,
726 if (!NT_STATUS_IS_OK(ret)) {
730 idmap_rfc2307_map_xid_results(ctx, mem_ctx, int_maps, result,
731 dom, attrs, ID_TYPE_GID);
733 TALLOC_FREE(fltr_grp);
743 talloc_free(mem_ctx);
747 static int idmap_rfc2307_context_destructor(struct idmap_rfc2307_context *ctx)
749 if (ctx->ads != NULL) {
750 /* we own this ADS_STRUCT so make sure it goes away */
751 ctx->ads->is_mine = True;
752 ads_destroy( &ctx->ads );
756 if (ctx->smbldap_state != NULL) {
757 smbldap_free_struct(&ctx->smbldap_state);
763 static NTSTATUS idmap_rfc2307_initialize(struct idmap_domain *domain)
765 struct idmap_rfc2307_context *ctx;
767 const char *bind_path_user, *bind_path_group, *ldap_server, *realm;
770 ctx = talloc_zero(domain, struct idmap_rfc2307_context);
772 return NT_STATUS_NO_MEMORY;
774 talloc_set_destructor(ctx, idmap_rfc2307_context_destructor);
776 cfg_opt = talloc_asprintf(ctx, "idmap config %s", domain->name);
777 if (cfg_opt == NULL) {
778 status = NT_STATUS_NO_MEMORY;
782 bind_path_user = idmap_config_const_string(
783 domain->name, "bind_path_user", NULL);
784 if (bind_path_user) {
785 ctx->bind_path_user = talloc_strdup(ctx, bind_path_user);
786 if (ctx->bind_path_user == NULL) {
787 status = NT_STATUS_NO_MEMORY;
791 status = NT_STATUS_INVALID_PARAMETER;
795 bind_path_group = idmap_config_const_string(
796 domain->name, "bind_path_group", NULL);
797 if (bind_path_group) {
798 ctx->bind_path_group = talloc_strdup(ctx, bind_path_group);
799 if (ctx->bind_path_group == NULL) {
800 status = NT_STATUS_NO_MEMORY;
804 status = NT_STATUS_INVALID_PARAMETER;
808 ldap_server = idmap_config_const_string(
809 domain->name, "ldap_server", NULL);
811 status = NT_STATUS_INVALID_PARAMETER;
815 if (strcmp(ldap_server, "stand-alone") == 0) {
816 status = idmap_rfc2307_init_ldap(ctx, domain->name);
818 } else if (strcmp(ldap_server, "ad") == 0) {
819 status = idmap_rfc2307_init_ads(ctx, domain->name);
822 status = NT_STATUS_INVALID_PARAMETER;
825 if (!NT_STATUS_IS_OK(status)) {
829 realm = idmap_config_const_string(domain->name, "realm", NULL);
831 ctx->realm = talloc_strdup(ctx, realm);
832 if (ctx->realm == NULL) {
833 status = NT_STATUS_NO_MEMORY;
838 ctx->user_cn = lp_parm_bool(-1, cfg_opt, "user_cn", false);
840 domain->private_data = ctx;
841 talloc_free(cfg_opt);
845 talloc_free(cfg_opt);
850 static struct idmap_methods rfc2307_methods = {
851 .init = idmap_rfc2307_initialize,
852 .unixids_to_sids = idmap_rfc2307_unixids_to_sids,
853 .sids_to_unixids = idmap_rfc2307_sids_to_unixids,
857 NTSTATUS idmap_rfc2307_init(void)
859 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "rfc2307",