2 * idmap_ad: map between Active Directory and RFC 2307 accounts
4 * Copyright (C) Volker Lendecke 2015
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "tldap_gensec_bind.h"
24 #include "tldap_util.h"
25 #include "lib/param/param.h"
26 #include "utils/net.h"
27 #include "auth/gensec/gensec.h"
28 #include "librpc/gen_ndr/ndr_netlogon.h"
29 #include "libads/ldap_schema_oids.h"
30 #include "../libds/common/flags.h"
31 #include "libcli/ldap/ldap_ndr.h"
32 #include "libcli/security/dom_sid.h"
34 struct idmap_ad_schema_names;
36 struct idmap_ad_context {
37 struct idmap_domain *dom;
38 struct tldap_context *ld;
39 struct idmap_ad_schema_names *schema;
40 const char *default_nc;
43 static char *get_schema_path(TALLOC_CTX *mem_ctx, struct tldap_context *ld)
45 struct tldap_message *rootdse;
47 rootdse = tldap_rootdse(ld);
48 if (rootdse == NULL) {
52 return tldap_talloc_single_attribute(rootdse, "schemaNamingContext",
56 static char *get_default_nc(TALLOC_CTX *mem_ctx, struct tldap_context *ld)
58 struct tldap_message *rootdse;
60 rootdse = tldap_rootdse(ld);
61 if (rootdse == NULL) {
65 return tldap_talloc_single_attribute(rootdse, "defaultNamingContext",
69 struct idmap_ad_schema_names {
78 static TLDAPRC get_attrnames_by_oids(struct tldap_context *ld,
80 const char *schema_path,
86 const char *attrs[] = { "lDAPDisplayName", "attributeId" };
89 struct tldap_message **msgs;
92 filter = talloc_strdup(mem_ctx, "(|");
94 return TLDAP_NO_MEMORY;
97 for (i=0; i<num_oids; i++) {
98 filter = talloc_asprintf_append_buffer(
99 filter, "(attributeId=%s)", oids[i]);
100 if (filter == NULL) {
101 return TLDAP_NO_MEMORY;
105 filter = talloc_asprintf_append_buffer(filter, ")");
106 if (filter == NULL) {
107 return TLDAP_NO_MEMORY;
110 rc = tldap_search(ld, schema_path, TLDAP_SCOPE_SUB, filter,
111 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
112 0, 0, 0, mem_ctx, &msgs);;
114 if (!TLDAP_RC_IS_SUCCESS(rc)) {
118 for (i=0; i<num_oids; i++) {
122 num_msgs = talloc_array_length(msgs);
124 for (i=0; i<num_msgs; i++) {
125 struct tldap_message *msg = msgs[i];
129 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
130 /* Could be a TLDAP_RES_SEARCH_REFERENCE */
134 oid = tldap_talloc_single_attribute(
135 msg, "attributeId", msg);
140 for (j=0; j<num_oids; j++) {
141 if (strequal(oid, oids[j])) {
152 names[j] = tldap_talloc_single_attribute(
153 msg, "lDAPDisplayName", mem_ctx);
158 return TLDAP_SUCCESS;
161 static TLDAPRC get_posix_schema_names(struct tldap_context *ld,
162 const char *schema_mode,
164 struct idmap_ad_schema_names **pschema)
167 struct idmap_ad_schema_names *schema;
169 const char *oids_sfu[] = {
170 ADS_ATTR_SFU_UIDNUMBER_OID,
171 ADS_ATTR_SFU_GIDNUMBER_OID,
172 ADS_ATTR_SFU_HOMEDIR_OID,
173 ADS_ATTR_SFU_SHELL_OID,
174 ADS_ATTR_SFU_GECOS_OID,
177 const char *oids_sfu20[] = {
178 ADS_ATTR_SFU20_UIDNUMBER_OID,
179 ADS_ATTR_SFU20_GIDNUMBER_OID,
180 ADS_ATTR_SFU20_HOMEDIR_OID,
181 ADS_ATTR_SFU20_SHELL_OID,
182 ADS_ATTR_SFU20_GECOS_OID,
183 ADS_ATTR_SFU20_UID_OID
185 const char *oids_rfc2307[] = {
186 ADS_ATTR_RFC2307_UIDNUMBER_OID,
187 ADS_ATTR_RFC2307_GIDNUMBER_OID,
188 ADS_ATTR_RFC2307_HOMEDIR_OID,
189 ADS_ATTR_RFC2307_SHELL_OID,
190 ADS_ATTR_RFC2307_GECOS_OID,
191 ADS_ATTR_RFC2307_UID_OID
197 schema = talloc(mem_ctx, struct idmap_ad_schema_names);
198 if (schema == NULL) {
199 return TLDAP_NO_MEMORY;
202 schema_path = get_schema_path(schema, ld);
203 if (schema_path == NULL) {
205 return TLDAP_NO_MEMORY;
210 if ((schema_mode != NULL) && (schema_mode[0] != '\0')) {
211 if (strequal(schema_mode, "sfu")) {
213 } else if (strequal(schema_mode, "sfu20")) {
215 } else if (strequal(schema_mode, "rfc2307" )) {
218 DBG_WARNING("Unknown schema mode %s\n", schema_mode);
222 rc = get_attrnames_by_oids(ld, schema, schema_path, 6, oids, names);
223 TALLOC_FREE(schema_path);
224 if (!TLDAP_RC_IS_SUCCESS(rc)) {
229 schema->uid = names[0];
230 schema->gid = names[1];
231 schema->dir = names[2];
232 schema->shell = names[3];
233 schema->gecos = names[4];
234 schema->name = names[5];
238 return TLDAP_SUCCESS;
241 static NTSTATUS idmap_ad_get_tldap_ctx(TALLOC_CTX *mem_ctx,
243 struct tldap_context **pld)
245 struct netr_DsRGetDCNameInfo *dcinfo;
246 struct sockaddr_storage dcaddr;
247 struct cli_credentials *creds;
248 struct loadparm_context *lp_ctx;
249 struct tldap_context *ld;
255 status = wb_dsgetdcname_gencache_get(mem_ctx, domname, &dcinfo);
256 if (!NT_STATUS_IS_OK(status)) {
257 DBG_DEBUG("Could not get dcinfo for %s: %s\n", domname,
262 if (dcinfo->dc_unc == NULL) {
264 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
266 if (dcinfo->dc_unc[0] == '\\') {
269 if (dcinfo->dc_unc[0] == '\\') {
273 ok = resolve_name(dcinfo->dc_unc, &dcaddr, 0x20, true);
275 DBG_DEBUG("Could not resolve name %s\n", dcinfo->dc_unc);
277 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
280 status = open_socket_out(&dcaddr, 389, 10000, &fd);
281 if (!NT_STATUS_IS_OK(status)) {
282 DBG_DEBUG("open_socket_out failed: %s\n", nt_errstr(status));
287 ld = tldap_context_create(dcinfo, fd);
289 DBG_DEBUG("tldap_context_create failed\n");
292 return NT_STATUS_NO_MEMORY;
295 creds = cli_credentials_init(dcinfo);
297 DBG_DEBUG("cli_credentials_init failed\n");
299 return NT_STATUS_NO_MEMORY;
302 lp_ctx = loadparm_init_s3(dcinfo, loadparm_s3_helpers());
303 if (lp_ctx == NULL) {
304 DBG_DEBUG("loadparm_init_s3 failed\n");
306 return NT_STATUS_NO_MEMORY;
309 cli_credentials_set_conf(creds, lp_ctx);
311 status = cli_credentials_set_machine_account(creds, lp_ctx);
312 if (!NT_STATUS_IS_OK(status)) {
313 DBG_DEBUG("cli_credentials_set_machine_account "
314 "failed: %s\n", nt_errstr(status));
319 rc = tldap_gensec_bind(ld, creds, "ldap", dcinfo->dc_unc, NULL, lp_ctx,
320 GENSEC_FEATURE_SIGN | GENSEC_FEATURE_SEAL);
321 if (!TLDAP_RC_IS_SUCCESS(rc)) {
322 DBG_DEBUG("tldap_gensec_bind failed: %s\n",
323 tldap_errstr(dcinfo, ld, rc));
325 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
328 rc = tldap_fetch_rootdse(ld);
329 if (!TLDAP_RC_IS_SUCCESS(rc)) {
330 DBG_DEBUG("tldap_fetch_rootdse failed: %s\n",
331 tldap_errstr(dcinfo, ld, rc));
333 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
336 *pld = talloc_move(mem_ctx, &ld);
341 static int idmap_ad_context_destructor(struct idmap_ad_context *ctx)
343 if ((ctx->dom != NULL) && (ctx->dom->private_data == ctx)) {
344 ctx->dom->private_data = NULL;
349 static NTSTATUS idmap_ad_context_create(TALLOC_CTX *mem_ctx,
350 struct idmap_domain *dom,
352 struct idmap_ad_context **pctx)
354 struct idmap_ad_context *ctx;
355 char *schema_config_option;
356 const char *schema_mode;
360 ctx = talloc(mem_ctx, struct idmap_ad_context);
362 return NT_STATUS_NO_MEMORY;
366 talloc_set_destructor(ctx, idmap_ad_context_destructor);
368 status = idmap_ad_get_tldap_ctx(ctx, domname, &ctx->ld);
369 if (!NT_STATUS_IS_OK(status)) {
370 DBG_DEBUG("idmap_ad_get_tldap_ctx failed: %s\n",
376 ctx->default_nc = get_default_nc(ctx, ctx->ld);
377 if (ctx->default_nc == NULL) {
378 DBG_DEBUG("No default nc\n");
383 schema_config_option = talloc_asprintf(
384 ctx, "idmap config %s", domname);
385 if (schema_config_option == NULL) {
387 return NT_STATUS_NO_MEMORY;
390 schema_mode = lp_parm_const_string(
391 -1, schema_config_option, "schema_mode", "rfc2307");
392 TALLOC_FREE(schema_config_option);
394 rc = get_posix_schema_names(ctx->ld, schema_mode, ctx, &ctx->schema);
395 if (!TLDAP_RC_IS_SUCCESS(rc)) {
396 DBG_DEBUG("get_posix_schema_names failed: %s\n",
397 tldap_errstr(ctx, ctx->ld, rc));
399 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
406 static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom)
408 dom->private_data = NULL;
412 static NTSTATUS idmap_ad_get_context(struct idmap_domain *dom,
413 struct idmap_ad_context **pctx)
415 struct idmap_ad_context *ctx = NULL;
418 if (dom->private_data != NULL) {
419 *pctx = talloc_get_type_abort(dom->private_data,
420 struct idmap_ad_context);
424 status = idmap_ad_context_create(dom, dom, dom->name, &ctx);
425 if (!NT_STATUS_IS_OK(status)) {
426 DBG_DEBUG("idmap_ad_context_create failed: %s\n",
431 dom->private_data = ctx;
436 static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom,
439 struct idmap_ad_context *ctx;
442 struct tldap_message **msgs;
445 char *u_filter, *g_filter, *filter;
447 const char *attrs[] = {
450 NULL, /* attr_uidnumber */
451 NULL, /* attr_gidnumber */
454 status = idmap_ad_get_context(dom, &ctx);
455 if (!NT_STATUS_IS_OK(status)) {
459 attrs[2] = ctx->schema->uid;
460 attrs[3] = ctx->schema->gid;
462 u_filter = talloc_strdup(talloc_tos(), "");
463 if (u_filter == NULL) {
464 return NT_STATUS_NO_MEMORY;
467 g_filter = talloc_strdup(talloc_tos(), "");
468 if (g_filter == NULL) {
469 return NT_STATUS_NO_MEMORY;
472 for (i=0; ids[i] != NULL; i++) {
473 struct id_map *id = ids[i];
475 id->status = ID_UNKNOWN;
477 switch (id->xid.type) {
479 u_filter = talloc_asprintf_append_buffer(
480 u_filter, "(%s=%ju)", ctx->schema->uid,
481 (uintmax_t)id->xid.id);
482 if (u_filter == NULL) {
483 return NT_STATUS_NO_MEMORY;
489 g_filter = talloc_asprintf_append_buffer(
490 g_filter, "(%s=%ju)", ctx->schema->gid,
491 (uintmax_t)id->xid.id);
492 if (g_filter == NULL) {
493 return NT_STATUS_NO_MEMORY;
499 DBG_WARNING("Unknown id type: %u\n",
500 (unsigned)id->xid.type);
505 filter = talloc_strdup(talloc_tos(), "(|");
506 if (filter == NULL) {
507 return NT_STATUS_NO_MEMORY;
510 if (*u_filter != '\0') {
511 filter = talloc_asprintf_append_buffer(
513 "(&(|(sAMAccountType=%d)(sAMAccountType=%d)"
514 "(sAMAccountType=%d))(|%s))",
515 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST,
516 ATYPE_INTERDOMAIN_TRUST, u_filter);
517 if (filter == NULL) {
518 return NT_STATUS_NO_MEMORY;
521 TALLOC_FREE(u_filter);
523 if (*g_filter != '\0') {
524 filter = talloc_asprintf_append_buffer(
526 "(&(|(sAMAccountType=%d)(sAMAccountType=%d))(|%s))",
527 ATYPE_SECURITY_GLOBAL_GROUP,
528 ATYPE_SECURITY_LOCAL_GROUP,
530 if (filter == NULL) {
531 return NT_STATUS_NO_MEMORY;
534 TALLOC_FREE(g_filter);
536 filter = talloc_asprintf_append_buffer(filter, ")");
537 if (filter == NULL) {
538 return NT_STATUS_NO_MEMORY;
541 DBG_DEBUG("Filter: [%s]\n", filter);
543 rc = tldap_search(ctx->ld, ctx->default_nc, TLDAP_SCOPE_SUB, filter,
544 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
545 0, 0, 0, talloc_tos(), &msgs);
546 if (!TLDAP_RC_IS_SUCCESS(rc)) {
547 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
552 num_msgs = talloc_array_length(msgs);
554 for (i=0; i<num_msgs; i++) {
555 struct tldap_message *msg = msgs[i];
564 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
568 ok = tldap_entry_dn(msg, &dn);
570 DBG_DEBUG("No dn found in msg %zu\n", i);
574 ok = tldap_pull_uint32(msg, "sAMAccountType", &atype);
576 DBG_DEBUG("No atype in object %s\n", dn);
580 switch (atype & 0xF0000000) {
581 case ATYPE_SECURITY_GLOBAL_GROUP:
582 case ATYPE_SECURITY_LOCAL_GROUP:
585 case ATYPE_NORMAL_ACCOUNT:
586 case ATYPE_WORKSTATION_TRUST:
587 case ATYPE_INTERDOMAIN_TRUST:
591 DBG_WARNING("unrecognized SAM account type %08x\n",
596 ok = tldap_pull_uint32(msg, (type == ID_TYPE_UID) ?
597 ctx->schema->uid : ctx->schema->gid,
600 DBG_WARNING("No unix id in object %s\n", dn);
604 ok = tldap_pull_binsid(msg, "objectSid", &sid);
606 DBG_DEBUG("No objectSid in object %s\n", dn);
611 for (j=0; ids[j]; j++) {
612 if ((type == ids[j]->xid.type) &&
613 (xid == ids[j]->xid.id)) {
619 DBG_DEBUG("Got unexpected sid %s from object %s\n",
620 sid_string_tos(&sid), dn);
624 sid_copy(map->sid, &sid);
625 map->status = ID_MAPPED;
627 DBG_DEBUG("Mapped %s -> %ju (%d)\n", sid_string_dbg(map->sid),
628 (uintmax_t)map->xid.id, map->xid.type);
636 static NTSTATUS idmap_ad_sids_to_unixids(struct idmap_domain *dom,
639 struct idmap_ad_context *ctx;
642 struct tldap_message **msgs;
647 const char *attrs[] = {
650 NULL, /* attr_uidnumber */
651 NULL, /* attr_gidnumber */
654 status = idmap_ad_get_context(dom, &ctx);
655 if (!NT_STATUS_IS_OK(status)) {
659 attrs[2] = ctx->schema->uid;
660 attrs[3] = ctx->schema->gid;
662 filter = talloc_asprintf(
664 "(&(|(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)"
665 "(sAMAccountType=%d)(sAMAccountType=%d))(|",
666 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST,
667 ATYPE_INTERDOMAIN_TRUST, ATYPE_SECURITY_GLOBAL_GROUP,
668 ATYPE_SECURITY_LOCAL_GROUP);
669 if (filter == NULL) {
670 return NT_STATUS_NO_MEMORY;
673 for (i=0; ids[i]; i++) {
676 ids[i]->status = ID_UNKNOWN;
678 sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), ids[i]->sid);
679 if (sidstr == NULL) {
680 return NT_STATUS_NO_MEMORY;
683 filter = talloc_asprintf_append_buffer(
684 filter, "(objectSid=%s)", sidstr);
686 if (filter == NULL) {
687 return NT_STATUS_NO_MEMORY;
691 filter = talloc_asprintf_append_buffer(filter, "))");
692 if (filter == NULL) {
693 return NT_STATUS_NO_MEMORY;
696 DBG_DEBUG("Filter: [%s]\n", filter);
698 rc = tldap_search(ctx->ld, ctx->default_nc, TLDAP_SCOPE_SUB, filter,
699 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
700 0, 0, 0, talloc_tos(), &msgs);
701 if (!TLDAP_RC_IS_SUCCESS(rc)) {
702 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
707 num_msgs = talloc_array_length(msgs);
709 for (i=0; i<num_msgs; i++) {
710 struct tldap_message *msg = msgs[i];
716 uint64_t account_type, xid;
719 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
723 ok = tldap_entry_dn(msg, &dn);
725 DBG_DEBUG("No dn found in msg %zu\n", i);
729 ok = tldap_pull_binsid(msg, "objectSid", &sid);
731 DBG_DEBUG("No objectSid in object %s\n", dn);
736 for (j=0; ids[j]; j++) {
737 if (dom_sid_equal(&sid, ids[j]->sid)) {
743 DBG_DEBUG("Got unexpected sid %s from object %s\n",
744 sid_string_tos(&sid), dn);
748 ok = tldap_pull_uint64(msg, "sAMAccountType", &account_type);
750 DBG_DEBUG("No sAMAccountType in %s\n", dn);
754 switch (account_type & 0xF0000000) {
755 case ATYPE_SECURITY_GLOBAL_GROUP:
756 case ATYPE_SECURITY_LOCAL_GROUP:
759 case ATYPE_NORMAL_ACCOUNT:
760 case ATYPE_WORKSTATION_TRUST:
761 case ATYPE_INTERDOMAIN_TRUST:
765 DBG_WARNING("unrecognized SAM account type %"PRIu64"\n",
770 ok = tldap_pull_uint64(msg,
771 type == ID_TYPE_UID ?
772 ctx->schema->uid : ctx->schema->gid,
775 DBG_DEBUG("No xid in %s\n", dn);
780 map->xid.type = type;
782 map->status = ID_MAPPED;
784 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
785 (unsigned long)map->xid.id, map->xid.type));
793 static NTSTATUS idmap_ad_unixids_to_sids_retry(struct idmap_domain *dom,
796 const NTSTATUS status_server_down =
797 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN));
800 status = idmap_ad_unixids_to_sids(dom, ids);
802 if (NT_STATUS_EQUAL(status, status_server_down)) {
803 TALLOC_FREE(dom->private_data);
804 status = idmap_ad_unixids_to_sids(dom, ids);
810 static NTSTATUS idmap_ad_sids_to_unixids_retry(struct idmap_domain *dom,
813 const NTSTATUS status_server_down =
814 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN));
817 status = idmap_ad_sids_to_unixids(dom, ids);
819 if (NT_STATUS_EQUAL(status, status_server_down)) {
820 TALLOC_FREE(dom->private_data);
821 status = idmap_ad_sids_to_unixids(dom, ids);
827 static struct idmap_methods ad_methods = {
828 .init = idmap_ad_initialize,
829 .unixids_to_sids = idmap_ad_unixids_to_sids_retry,
830 .sids_to_unixids = idmap_ad_sids_to_unixids_retry,
834 NTSTATUS idmap_ad_init(void)
838 status = smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
840 if (!NT_STATUS_IS_OK(status)) {
844 status = idmap_ad_nss_init();
845 if (!NT_STATUS_IS_OK(status)) {