tevent: Fix typos
[Samba.git] / source3 / winbindd / idmap_ad.c
blob315a9444a19ae16f66d1cdf899c567d31504bd99
1 /*
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "idmap.h"
23 #include "tldap_gensec_bind.h"
24 #include "tldap_util.h"
25 #include "passdb.h"
26 #include "lib/param/param.h"
27 #include "utils/net.h"
28 #include "auth/gensec/gensec.h"
29 #include "librpc/gen_ndr/ndr_netlogon.h"
30 #include "libads/ldap_schema_oids.h"
31 #include "../libds/common/flags.h"
32 #include "libcli/ldap/ldap_ndr.h"
33 #include "libcli/security/dom_sid.h"
35 struct idmap_ad_schema_names;
37 struct idmap_ad_context {
38 struct idmap_domain *dom;
39 struct tldap_context *ld;
40 struct idmap_ad_schema_names *schema;
41 const char *default_nc;
43 bool unix_primary_group;
44 bool unix_nss_info;
47 static NTSTATUS idmap_ad_get_context(struct idmap_domain *dom,
48 struct idmap_ad_context **pctx);
50 static char *get_schema_path(TALLOC_CTX *mem_ctx, struct tldap_context *ld)
52 struct tldap_message *rootdse;
54 rootdse = tldap_rootdse(ld);
55 if (rootdse == NULL) {
56 return NULL;
59 return tldap_talloc_single_attribute(rootdse, "schemaNamingContext",
60 mem_ctx);
63 static char *get_default_nc(TALLOC_CTX *mem_ctx, struct tldap_context *ld)
65 struct tldap_message *rootdse;
67 rootdse = tldap_rootdse(ld);
68 if (rootdse == NULL) {
69 return NULL;
72 return tldap_talloc_single_attribute(rootdse, "defaultNamingContext",
73 mem_ctx);
76 struct idmap_ad_schema_names {
77 char *name;
78 char *uid;
79 char *gid;
80 char *gecos;
81 char *dir;
82 char *shell;
85 static TLDAPRC get_attrnames_by_oids(struct tldap_context *ld,
86 TALLOC_CTX *mem_ctx,
87 const char *schema_path,
88 size_t num_oids,
89 const char **oids,
90 char **names)
92 char *filter;
93 const char *attrs[] = { "lDAPDisplayName", "attributeId" };
94 size_t i;
95 TLDAPRC rc;
96 struct tldap_message **msgs;
97 size_t num_msgs;
99 filter = talloc_strdup(mem_ctx, "(|");
100 if (filter == NULL) {
101 return TLDAP_NO_MEMORY;
104 for (i=0; i<num_oids; i++) {
105 filter = talloc_asprintf_append_buffer(
106 filter, "(attributeId=%s)", oids[i]);
107 if (filter == NULL) {
108 return TLDAP_NO_MEMORY;
112 filter = talloc_asprintf_append_buffer(filter, ")");
113 if (filter == NULL) {
114 return TLDAP_NO_MEMORY;
117 rc = tldap_search(ld, schema_path, TLDAP_SCOPE_SUB, filter,
118 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
119 0, 0, 0, mem_ctx, &msgs);;
120 TALLOC_FREE(filter);
121 if (!TLDAP_RC_IS_SUCCESS(rc)) {
122 return rc;
125 for (i=0; i<num_oids; i++) {
126 names[i] = NULL;
129 num_msgs = talloc_array_length(msgs);
131 for (i=0; i<num_msgs; i++) {
132 struct tldap_message *msg = msgs[i];
133 char *oid;
134 size_t j;
136 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
137 /* Could be a TLDAP_RES_SEARCH_REFERENCE */
138 continue;
141 oid = tldap_talloc_single_attribute(
142 msg, "attributeId", msg);
143 if (oid == NULL) {
144 continue;
147 for (j=0; j<num_oids; j++) {
148 if (strequal(oid, oids[j])) {
149 break;
152 TALLOC_FREE(oid);
154 if (j == num_oids) {
155 /* not found */
156 continue;
159 names[j] = tldap_talloc_single_attribute(
160 msg, "lDAPDisplayName", mem_ctx);
163 TALLOC_FREE(msgs);
165 return TLDAP_SUCCESS;
168 static TLDAPRC get_posix_schema_names(struct tldap_context *ld,
169 const char *schema_mode,
170 TALLOC_CTX *mem_ctx,
171 struct idmap_ad_schema_names **pschema)
173 char *schema_path;
174 struct idmap_ad_schema_names *schema;
175 char *names[6];
176 const char *oids_sfu[] = {
177 ADS_ATTR_SFU_UIDNUMBER_OID,
178 ADS_ATTR_SFU_GIDNUMBER_OID,
179 ADS_ATTR_SFU_HOMEDIR_OID,
180 ADS_ATTR_SFU_SHELL_OID,
181 ADS_ATTR_SFU_GECOS_OID,
182 ADS_ATTR_SFU_UID_OID
184 const char *oids_sfu20[] = {
185 ADS_ATTR_SFU20_UIDNUMBER_OID,
186 ADS_ATTR_SFU20_GIDNUMBER_OID,
187 ADS_ATTR_SFU20_HOMEDIR_OID,
188 ADS_ATTR_SFU20_SHELL_OID,
189 ADS_ATTR_SFU20_GECOS_OID,
190 ADS_ATTR_SFU20_UID_OID
192 const char *oids_rfc2307[] = {
193 ADS_ATTR_RFC2307_UIDNUMBER_OID,
194 ADS_ATTR_RFC2307_GIDNUMBER_OID,
195 ADS_ATTR_RFC2307_HOMEDIR_OID,
196 ADS_ATTR_RFC2307_SHELL_OID,
197 ADS_ATTR_RFC2307_GECOS_OID,
198 ADS_ATTR_RFC2307_UID_OID
200 const char **oids;
202 TLDAPRC rc;
204 schema = talloc(mem_ctx, struct idmap_ad_schema_names);
205 if (schema == NULL) {
206 return TLDAP_NO_MEMORY;
209 schema_path = get_schema_path(schema, ld);
210 if (schema_path == NULL) {
211 TALLOC_FREE(schema);
212 return TLDAP_NO_MEMORY;
215 oids = oids_rfc2307;
217 if ((schema_mode != NULL) && (schema_mode[0] != '\0')) {
218 if (strequal(schema_mode, "sfu")) {
219 oids = oids_sfu;
220 } else if (strequal(schema_mode, "sfu20")) {
221 oids = oids_sfu20;
222 } else if (strequal(schema_mode, "rfc2307" )) {
223 oids = oids_rfc2307;
224 } else {
225 DBG_WARNING("Unknown schema mode %s\n", schema_mode);
229 rc = get_attrnames_by_oids(ld, schema, schema_path, 6, oids, names);
230 TALLOC_FREE(schema_path);
231 if (!TLDAP_RC_IS_SUCCESS(rc)) {
232 TALLOC_FREE(schema);
233 return rc;
236 schema->uid = names[0];
237 schema->gid = names[1];
238 schema->dir = names[2];
239 schema->shell = names[3];
240 schema->gecos = names[4];
241 schema->name = names[5];
243 *pschema = schema;
245 return TLDAP_SUCCESS;
248 static NTSTATUS idmap_ad_get_tldap_ctx(TALLOC_CTX *mem_ctx,
249 const char *domname,
250 struct tldap_context **pld)
252 struct netr_DsRGetDCNameInfo *dcinfo;
253 struct sockaddr_storage dcaddr;
254 struct cli_credentials *creds;
255 struct loadparm_context *lp_ctx;
256 struct tldap_context *ld;
257 int fd;
258 NTSTATUS status;
259 bool ok;
260 TLDAPRC rc;
262 status = wb_dsgetdcname_gencache_get(mem_ctx, domname, &dcinfo);
263 if (!NT_STATUS_IS_OK(status)) {
264 DBG_DEBUG("Could not get dcinfo for %s: %s\n", domname,
265 nt_errstr(status));
266 return status;
269 if (dcinfo->dc_unc == NULL) {
270 TALLOC_FREE(dcinfo);
271 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
273 if (dcinfo->dc_unc[0] == '\\') {
274 dcinfo->dc_unc += 1;
276 if (dcinfo->dc_unc[0] == '\\') {
277 dcinfo->dc_unc += 1;
280 ok = resolve_name(dcinfo->dc_unc, &dcaddr, 0x20, true);
281 if (!ok) {
282 DBG_DEBUG("Could not resolve name %s\n", dcinfo->dc_unc);
283 TALLOC_FREE(dcinfo);
284 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
287 status = open_socket_out(&dcaddr, 389, 10000, &fd);
288 if (!NT_STATUS_IS_OK(status)) {
289 DBG_DEBUG("open_socket_out failed: %s\n", nt_errstr(status));
290 TALLOC_FREE(dcinfo);
291 return status;
294 ld = tldap_context_create(dcinfo, fd);
295 if (ld == NULL) {
296 DBG_DEBUG("tldap_context_create failed\n");
297 close(fd);
298 TALLOC_FREE(dcinfo);
299 return NT_STATUS_NO_MEMORY;
303 * Here we use or own machine account as
304 * we run as domain member.
306 status = pdb_get_trust_credentials(lp_workgroup(),
307 lp_realm(),
308 dcinfo,
309 &creds);
310 if (!NT_STATUS_IS_OK(status)) {
311 DBG_DEBUG("pdb_get_trust_credentials() failed - %s\n",
312 nt_errstr(status));
313 TALLOC_FREE(dcinfo);
314 return status;
317 lp_ctx = loadparm_init_s3(dcinfo, loadparm_s3_helpers());
318 if (lp_ctx == NULL) {
319 DBG_DEBUG("loadparm_init_s3 failed\n");
320 TALLOC_FREE(dcinfo);
321 return NT_STATUS_NO_MEMORY;
324 rc = tldap_gensec_bind(ld, creds, "ldap", dcinfo->dc_unc, NULL, lp_ctx,
325 GENSEC_FEATURE_SIGN | GENSEC_FEATURE_SEAL);
326 if (!TLDAP_RC_IS_SUCCESS(rc)) {
327 DBG_DEBUG("tldap_gensec_bind failed: %s\n",
328 tldap_errstr(dcinfo, ld, rc));
329 TALLOC_FREE(dcinfo);
330 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
333 rc = tldap_fetch_rootdse(ld);
334 if (!TLDAP_RC_IS_SUCCESS(rc)) {
335 DBG_DEBUG("tldap_fetch_rootdse failed: %s\n",
336 tldap_errstr(dcinfo, ld, rc));
337 TALLOC_FREE(dcinfo);
338 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
341 *pld = talloc_move(mem_ctx, &ld);
342 TALLOC_FREE(dcinfo);
343 return NT_STATUS_OK;
346 static int idmap_ad_context_destructor(struct idmap_ad_context *ctx)
348 if ((ctx->dom != NULL) && (ctx->dom->private_data == ctx)) {
349 ctx->dom->private_data = NULL;
351 return 0;
354 static NTSTATUS idmap_ad_context_create(TALLOC_CTX *mem_ctx,
355 struct idmap_domain *dom,
356 const char *domname,
357 struct idmap_ad_context **pctx)
359 struct idmap_ad_context *ctx;
360 const char *schema_mode;
361 NTSTATUS status;
362 TLDAPRC rc;
364 ctx = talloc(mem_ctx, struct idmap_ad_context);
365 if (ctx == NULL) {
366 return NT_STATUS_NO_MEMORY;
368 ctx->dom = dom;
370 talloc_set_destructor(ctx, idmap_ad_context_destructor);
372 status = idmap_ad_get_tldap_ctx(ctx, domname, &ctx->ld);
373 if (!NT_STATUS_IS_OK(status)) {
374 DBG_DEBUG("idmap_ad_get_tldap_ctx failed: %s\n",
375 nt_errstr(status));
376 TALLOC_FREE(ctx);
377 return status;
380 ctx->default_nc = get_default_nc(ctx, ctx->ld);
381 if (ctx->default_nc == NULL) {
382 DBG_DEBUG("No default nc\n");
383 TALLOC_FREE(ctx);
384 return status;
387 ctx->unix_primary_group = idmap_config_bool(
388 domname, "unix_primary_group", false);
389 ctx->unix_nss_info = idmap_config_bool(
390 domname, "unix_nss_info", false);
392 schema_mode = idmap_config_const_string(
393 domname, "schema_mode", "rfc2307");
395 rc = get_posix_schema_names(ctx->ld, schema_mode, ctx, &ctx->schema);
396 if (!TLDAP_RC_IS_SUCCESS(rc)) {
397 DBG_DEBUG("get_posix_schema_names failed: %s\n",
398 tldap_errstr(ctx, ctx->ld, rc));
399 TALLOC_FREE(ctx);
400 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
403 *pctx = ctx;
404 return NT_STATUS_OK;
407 static NTSTATUS idmap_ad_query_user(struct idmap_domain *domain,
408 struct wbint_userinfo *info)
410 struct idmap_ad_context *ctx;
411 TLDAPRC rc;
412 NTSTATUS status;
413 char *sidstr, *filter;
414 const char *attrs[4];
415 size_t i, num_msgs;
416 struct tldap_message **msgs;
418 status = idmap_ad_get_context(domain, &ctx);
419 if (!NT_STATUS_IS_OK(status)) {
420 return status;
423 if (!(ctx->unix_primary_group || ctx->unix_nss_info)) {
424 return NT_STATUS_OK;
427 attrs[0] = ctx->schema->gid;
428 attrs[1] = ctx->schema->gecos;
429 attrs[2] = ctx->schema->dir;
430 attrs[3] = ctx->schema->shell;
432 sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), &info->user_sid);
433 if (sidstr == NULL) {
434 return NT_STATUS_NO_MEMORY;
437 filter = talloc_asprintf(talloc_tos(), "(objectsid=%s)", sidstr);
438 TALLOC_FREE(sidstr);
439 if (filter == NULL) {
440 return NT_STATUS_NO_MEMORY;
443 DBG_DEBUG("Filter: [%s]\n", filter);
445 rc = tldap_search(ctx->ld, ctx->default_nc, TLDAP_SCOPE_SUB, filter,
446 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
447 0, 0, 0, talloc_tos(), &msgs);
448 if (!TLDAP_RC_IS_SUCCESS(rc)) {
449 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
452 TALLOC_FREE(filter);
454 num_msgs = talloc_array_length(msgs);
456 for (i=0; i<num_msgs; i++) {
457 struct tldap_message *msg = msgs[i];
459 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
460 continue;
463 if (ctx->unix_primary_group) {
464 bool ok;
465 uint32_t gid;
467 ok = tldap_pull_uint32(msg, ctx->schema->gid, &gid);
468 if (ok) {
469 DBG_DEBUG("Setting primary group "
470 "to %"PRIu32" from attr %s\n",
471 gid, ctx->schema->gid);
472 info->primary_gid = gid;
476 if (ctx->unix_nss_info) {
477 char *attr;
479 attr = tldap_talloc_single_attribute(
480 msg, ctx->schema->dir, talloc_tos());
481 if (attr != NULL) {
482 info->homedir = talloc_move(info, &attr);
484 TALLOC_FREE(attr);
486 attr = tldap_talloc_single_attribute(
487 msg, ctx->schema->shell, talloc_tos());
488 if (attr != NULL) {
489 info->shell = talloc_move(info, &attr);
491 TALLOC_FREE(attr);
493 attr = tldap_talloc_single_attribute(
494 msg, ctx->schema->gecos, talloc_tos());
495 if (attr != NULL) {
496 info->full_name = talloc_move(info, &attr);
498 TALLOC_FREE(attr);
502 return NT_STATUS_OK;
505 static NTSTATUS idmap_ad_query_user_retry(struct idmap_domain *domain,
506 struct wbint_userinfo *info)
508 const NTSTATUS status_server_down =
509 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN));
510 NTSTATUS status;
512 status = idmap_ad_query_user(domain, info);
514 if (NT_STATUS_EQUAL(status, status_server_down)) {
515 TALLOC_FREE(domain->private_data);
516 status = idmap_ad_query_user(domain, info);
519 return status;
522 static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom)
524 dom->query_user = idmap_ad_query_user_retry;
525 dom->private_data = NULL;
526 return NT_STATUS_OK;
529 static NTSTATUS idmap_ad_get_context(struct idmap_domain *dom,
530 struct idmap_ad_context **pctx)
532 struct idmap_ad_context *ctx = NULL;
533 NTSTATUS status;
535 if (dom->private_data != NULL) {
536 *pctx = talloc_get_type_abort(dom->private_data,
537 struct idmap_ad_context);
538 return NT_STATUS_OK;
541 status = idmap_ad_context_create(dom, dom, dom->name, &ctx);
542 if (!NT_STATUS_IS_OK(status)) {
543 DBG_DEBUG("idmap_ad_context_create failed: %s\n",
544 nt_errstr(status));
545 return status;
548 dom->private_data = ctx;
549 *pctx = ctx;
550 return NT_STATUS_OK;
553 static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom,
554 struct id_map **ids)
556 struct idmap_ad_context *ctx;
557 TLDAPRC rc;
558 NTSTATUS status;
559 struct tldap_message **msgs;
561 size_t i, num_msgs;
562 char *u_filter, *g_filter, *filter;
564 const char *attrs[] = {
565 "sAMAccountType",
566 "objectSid",
567 NULL, /* attr_uidnumber */
568 NULL, /* attr_gidnumber */
571 status = idmap_ad_get_context(dom, &ctx);
572 if (!NT_STATUS_IS_OK(status)) {
573 return status;
576 attrs[2] = ctx->schema->uid;
577 attrs[3] = ctx->schema->gid;
579 u_filter = talloc_strdup(talloc_tos(), "");
580 if (u_filter == NULL) {
581 return NT_STATUS_NO_MEMORY;
584 g_filter = talloc_strdup(talloc_tos(), "");
585 if (g_filter == NULL) {
586 return NT_STATUS_NO_MEMORY;
589 for (i=0; ids[i] != NULL; i++) {
590 struct id_map *id = ids[i];
592 id->status = ID_UNKNOWN;
594 switch (id->xid.type) {
595 case ID_TYPE_UID: {
596 u_filter = talloc_asprintf_append_buffer(
597 u_filter, "(%s=%ju)", ctx->schema->uid,
598 (uintmax_t)id->xid.id);
599 if (u_filter == NULL) {
600 return NT_STATUS_NO_MEMORY;
602 break;
605 case ID_TYPE_GID: {
606 g_filter = talloc_asprintf_append_buffer(
607 g_filter, "(%s=%ju)", ctx->schema->gid,
608 (uintmax_t)id->xid.id);
609 if (g_filter == NULL) {
610 return NT_STATUS_NO_MEMORY;
612 break;
615 default:
616 DBG_WARNING("Unknown id type: %u\n",
617 (unsigned)id->xid.type);
618 break;
622 filter = talloc_strdup(talloc_tos(), "(|");
623 if (filter == NULL) {
624 return NT_STATUS_NO_MEMORY;
627 if (*u_filter != '\0') {
628 filter = talloc_asprintf_append_buffer(
629 filter,
630 "(&(|(sAMAccountType=%d)(sAMAccountType=%d)"
631 "(sAMAccountType=%d))(|%s))",
632 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST,
633 ATYPE_INTERDOMAIN_TRUST, u_filter);
634 if (filter == NULL) {
635 return NT_STATUS_NO_MEMORY;
638 TALLOC_FREE(u_filter);
640 if (*g_filter != '\0') {
641 filter = talloc_asprintf_append_buffer(
642 filter,
643 "(&(|(sAMAccountType=%d)(sAMAccountType=%d))(|%s))",
644 ATYPE_SECURITY_GLOBAL_GROUP,
645 ATYPE_SECURITY_LOCAL_GROUP,
646 g_filter);
647 if (filter == NULL) {
648 return NT_STATUS_NO_MEMORY;
651 TALLOC_FREE(g_filter);
653 filter = talloc_asprintf_append_buffer(filter, ")");
654 if (filter == NULL) {
655 return NT_STATUS_NO_MEMORY;
658 DBG_DEBUG("Filter: [%s]\n", filter);
660 rc = tldap_search(ctx->ld, ctx->default_nc, TLDAP_SCOPE_SUB, filter,
661 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
662 0, 0, 0, talloc_tos(), &msgs);
663 if (!TLDAP_RC_IS_SUCCESS(rc)) {
664 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
667 TALLOC_FREE(filter);
669 num_msgs = talloc_array_length(msgs);
671 for (i=0; i<num_msgs; i++) {
672 struct tldap_message *msg = msgs[i];
673 char *dn;
674 struct id_map *map;
675 struct dom_sid sid;
676 size_t j;
677 bool ok;
678 uint32_t atype, xid;
679 enum id_type type;
681 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
682 continue;
685 ok = tldap_entry_dn(msg, &dn);
686 if (!ok) {
687 DBG_DEBUG("No dn found in msg %zu\n", i);
688 continue;
691 ok = tldap_pull_uint32(msg, "sAMAccountType", &atype);
692 if (!ok) {
693 DBG_DEBUG("No atype in object %s\n", dn);
694 continue;
697 switch (atype & 0xF0000000) {
698 case ATYPE_SECURITY_GLOBAL_GROUP:
699 case ATYPE_SECURITY_LOCAL_GROUP:
700 type = ID_TYPE_GID;
701 break;
702 case ATYPE_NORMAL_ACCOUNT:
703 case ATYPE_WORKSTATION_TRUST:
704 case ATYPE_INTERDOMAIN_TRUST:
705 type = ID_TYPE_UID;
706 break;
707 default:
708 DBG_WARNING("unrecognized SAM account type %08x\n",
709 atype);
710 continue;
713 ok = tldap_pull_uint32(msg, (type == ID_TYPE_UID) ?
714 ctx->schema->uid : ctx->schema->gid,
715 &xid);
716 if (!ok) {
717 DBG_WARNING("No unix id in object %s\n", dn);
718 continue;
721 ok = tldap_pull_binsid(msg, "objectSid", &sid);
722 if (!ok) {
723 DBG_DEBUG("No objectSid in object %s\n", dn);
724 continue;
727 map = NULL;
728 for (j=0; ids[j]; j++) {
729 if ((type == ids[j]->xid.type) &&
730 (xid == ids[j]->xid.id)) {
731 map = ids[j];
732 break;
735 if (map == NULL) {
736 DBG_DEBUG("Got unexpected sid %s from object %s\n",
737 sid_string_tos(&sid), dn);
738 continue;
741 sid_copy(map->sid, &sid);
742 map->status = ID_MAPPED;
744 DBG_DEBUG("Mapped %s -> %ju (%d)\n", sid_string_dbg(map->sid),
745 (uintmax_t)map->xid.id, map->xid.type);
748 TALLOC_FREE(msgs);
750 return NT_STATUS_OK;
753 static NTSTATUS idmap_ad_sids_to_unixids(struct idmap_domain *dom,
754 struct id_map **ids)
756 struct idmap_ad_context *ctx;
757 TLDAPRC rc;
758 NTSTATUS status;
759 struct tldap_message **msgs;
761 char *filter;
762 size_t i, num_msgs;
764 const char *attrs[] = {
765 "sAMAccountType",
766 "objectSid",
767 NULL, /* attr_uidnumber */
768 NULL, /* attr_gidnumber */
771 status = idmap_ad_get_context(dom, &ctx);
772 if (!NT_STATUS_IS_OK(status)) {
773 return status;
776 attrs[2] = ctx->schema->uid;
777 attrs[3] = ctx->schema->gid;
779 filter = talloc_asprintf(
780 talloc_tos(),
781 "(&(|(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)"
782 "(sAMAccountType=%d)(sAMAccountType=%d))(|",
783 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST,
784 ATYPE_INTERDOMAIN_TRUST, ATYPE_SECURITY_GLOBAL_GROUP,
785 ATYPE_SECURITY_LOCAL_GROUP);
786 if (filter == NULL) {
787 return NT_STATUS_NO_MEMORY;
790 for (i=0; ids[i]; i++) {
791 char *sidstr;
793 ids[i]->status = ID_UNKNOWN;
795 sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), ids[i]->sid);
796 if (sidstr == NULL) {
797 return NT_STATUS_NO_MEMORY;
800 filter = talloc_asprintf_append_buffer(
801 filter, "(objectSid=%s)", sidstr);
802 TALLOC_FREE(sidstr);
803 if (filter == NULL) {
804 return NT_STATUS_NO_MEMORY;
808 filter = talloc_asprintf_append_buffer(filter, "))");
809 if (filter == NULL) {
810 return NT_STATUS_NO_MEMORY;
813 DBG_DEBUG("Filter: [%s]\n", filter);
815 rc = tldap_search(ctx->ld, ctx->default_nc, TLDAP_SCOPE_SUB, filter,
816 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0,
817 0, 0, 0, talloc_tos(), &msgs);
818 if (!TLDAP_RC_IS_SUCCESS(rc)) {
819 return NT_STATUS_LDAP(TLDAP_RC_V(rc));
822 TALLOC_FREE(filter);
824 num_msgs = talloc_array_length(msgs);
826 for (i=0; i<num_msgs; i++) {
827 struct tldap_message *msg = msgs[i];
828 char *dn;
829 struct id_map *map;
830 struct dom_sid sid;
831 size_t j;
832 bool ok;
833 uint64_t account_type, xid;
834 enum id_type type;
836 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
837 continue;
840 ok = tldap_entry_dn(msg, &dn);
841 if (!ok) {
842 DBG_DEBUG("No dn found in msg %zu\n", i);
843 continue;
846 ok = tldap_pull_binsid(msg, "objectSid", &sid);
847 if (!ok) {
848 DBG_DEBUG("No objectSid in object %s\n", dn);
849 continue;
852 map = NULL;
853 for (j=0; ids[j]; j++) {
854 if (dom_sid_equal(&sid, ids[j]->sid)) {
855 map = ids[j];
856 break;
859 if (map == NULL) {
860 DBG_DEBUG("Got unexpected sid %s from object %s\n",
861 sid_string_tos(&sid), dn);
862 continue;
865 ok = tldap_pull_uint64(msg, "sAMAccountType", &account_type);
866 if (!ok) {
867 DBG_DEBUG("No sAMAccountType in %s\n", dn);
868 continue;
871 switch (account_type & 0xF0000000) {
872 case ATYPE_SECURITY_GLOBAL_GROUP:
873 case ATYPE_SECURITY_LOCAL_GROUP:
874 type = ID_TYPE_GID;
875 break;
876 case ATYPE_NORMAL_ACCOUNT:
877 case ATYPE_WORKSTATION_TRUST:
878 case ATYPE_INTERDOMAIN_TRUST:
879 type = ID_TYPE_UID;
880 break;
881 default:
882 DBG_WARNING("unrecognized SAM account type %"PRIu64"\n",
883 account_type);
884 continue;
887 ok = tldap_pull_uint64(msg,
888 type == ID_TYPE_UID ?
889 ctx->schema->uid : ctx->schema->gid,
890 &xid);
891 if (!ok) {
892 DBG_DEBUG("No xid in %s\n", dn);
893 continue;
896 /* mapped */
897 map->xid.type = type;
898 map->xid.id = xid;
899 map->status = ID_MAPPED;
901 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
902 (unsigned long)map->xid.id, map->xid.type));
905 TALLOC_FREE(msgs);
907 return NT_STATUS_OK;
910 static NTSTATUS idmap_ad_unixids_to_sids_retry(struct idmap_domain *dom,
911 struct id_map **ids)
913 const NTSTATUS status_server_down =
914 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN));
915 NTSTATUS status;
917 status = idmap_ad_unixids_to_sids(dom, ids);
919 if (NT_STATUS_EQUAL(status, status_server_down)) {
920 TALLOC_FREE(dom->private_data);
921 status = idmap_ad_unixids_to_sids(dom, ids);
924 return status;
927 static NTSTATUS idmap_ad_sids_to_unixids_retry(struct idmap_domain *dom,
928 struct id_map **ids)
930 const NTSTATUS status_server_down =
931 NT_STATUS_LDAP(TLDAP_RC_V(TLDAP_SERVER_DOWN));
932 NTSTATUS status;
934 status = idmap_ad_sids_to_unixids(dom, ids);
936 if (NT_STATUS_EQUAL(status, status_server_down)) {
937 TALLOC_FREE(dom->private_data);
938 status = idmap_ad_sids_to_unixids(dom, ids);
941 return status;
944 static struct idmap_methods ad_methods = {
945 .init = idmap_ad_initialize,
946 .unixids_to_sids = idmap_ad_unixids_to_sids_retry,
947 .sids_to_unixids = idmap_ad_sids_to_unixids_retry,
950 static_decl_idmap;
951 NTSTATUS idmap_ad_init(TALLOC_CTX *ctx)
953 NTSTATUS status;
955 status = smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
956 "ad", &ad_methods);
957 if (!NT_STATUS_IS_OK(status)) {
958 return status;
961 status = idmap_ad_nss_init(ctx);
962 if (!NT_STATUS_IS_OK(status)) {
963 return status;
966 return NT_STATUS_OK;