s3/ldbtools: don't neddlessly link against wbinbind libs
[Samba/gebeck_regimport.git] / source3 / winbindd / idmap_ad.c
blob5c29ba0b223dfd9aa604aaa11a9038634b0e7549
1 /*
2 * idmap_ad: map between Active Directory and RFC 2307 or "Services for Unix" (SFU) Accounts
4 * Unix SMB/CIFS implementation.
6 * Winbind ADS backend functions
8 * Copyright (C) Andrew Tridgell 2001
9 * Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
10 * Copyright (C) Gerald (Jerry) Carter 2004-2007
11 * Copyright (C) Luke Howard 2001-2004
12 * Copyright (C) Michael Adam 2008
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include "includes.h"
29 #include "winbindd.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_IDMAP
34 #define WINBIND_CCACHE_NAME "MEMORY:winbind_ccache"
36 #define IDMAP_AD_MAX_IDS 30
37 #define CHECK_ALLOC_DONE(mem) do { \
38 if (!mem) { \
39 DEBUG(0, ("Out of memory!\n")); \
40 ret = NT_STATUS_NO_MEMORY; \
41 goto done; \
42 } \
43 } while (0)
45 struct idmap_ad_context {
46 uint32_t filter_low_id;
47 uint32_t filter_high_id;
48 ADS_STRUCT *ads;
49 struct posix_schema *ad_schema;
50 enum wb_posix_mapping ad_map_type; /* WB_POSIX_MAP_UNKNOWN */
53 NTSTATUS init_module(void);
55 /************************************************************************
56 ***********************************************************************/
58 static ADS_STATUS ad_idmap_cached_connection_internal(struct idmap_domain *dom)
60 ADS_STRUCT *ads;
61 ADS_STATUS status;
62 bool local = False;
63 fstring dc_name;
64 struct sockaddr_storage dc_ip;
65 struct idmap_ad_context *ctx;
66 char *ldap_server = NULL;
67 char *realm = NULL;
68 struct winbindd_domain *wb_dom;
70 DEBUG(10, ("ad_idmap_cached_connection: called for domain '%s'\n",
71 dom->name));
73 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
75 if (ctx->ads != NULL) {
77 time_t expire;
78 time_t now = time(NULL);
80 ads = ctx->ads;
82 expire = MIN(ads->auth.tgt_expire, ads->auth.tgs_expire);
84 /* check for a valid structure */
85 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
86 (uint32)expire-(uint32)now, (uint32) expire, (uint32) now));
88 if ( ads->config.realm && (expire > time(NULL))) {
89 return ADS_SUCCESS;
90 } else {
91 /* we own this ADS_STRUCT so make sure it goes away */
92 DEBUG(7,("Deleting expired krb5 credential cache\n"));
93 ads->is_mine = True;
94 ads_destroy( &ads );
95 ads_kdestroy(WINBIND_CCACHE_NAME);
96 ctx->ads = NULL;
97 TALLOC_FREE( ctx->ad_schema );
101 if (!local) {
102 /* we don't want this to affect the users ccache */
103 setenv("KRB5CCNAME", WINBIND_CCACHE_NAME, 1);
107 * At this point we only have the NetBIOS domain name.
108 * Check if we can get server nam and realm from SAF cache
109 * and the domain list.
111 ldap_server = saf_fetch(dom->name);
112 DEBUG(10, ("ldap_server from saf cache: '%s'\n", ldap_server?ldap_server:""));
114 wb_dom = find_domain_from_name_noinit(dom->name);
115 if (wb_dom == NULL) {
116 DEBUG(10, ("find_domain_from_name_noinit did not find domain '%s'\n",
117 dom->name));
118 realm = NULL;
119 } else {
120 DEBUG(10, ("find_domain_from_name_noinit found realm '%s' for "
121 " domain '%s'\n", wb_dom->alt_name, dom->name));
122 realm = wb_dom->alt_name;
125 if ( (ads = ads_init(realm, dom->name, ldap_server)) == NULL ) {
126 DEBUG(1,("ads_init failed\n"));
127 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
130 /* the machine acct password might have change - fetch it every time */
131 SAFE_FREE(ads->auth.password);
132 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
134 SAFE_FREE(ads->auth.realm);
135 ads->auth.realm = SMB_STRDUP(lp_realm());
137 /* setup server affinity */
139 get_dc_name(dom->name, realm, dc_name, &dc_ip );
141 status = ads_connect(ads);
142 if (!ADS_ERR_OK(status)) {
143 DEBUG(1, ("ad_idmap_init: failed to connect to AD\n"));
144 ads_destroy(&ads);
145 return status;
148 ads->is_mine = False;
150 ctx->ads = ads;
152 return ADS_SUCCESS;
155 /************************************************************************
156 ***********************************************************************/
158 static ADS_STATUS ad_idmap_cached_connection(struct idmap_domain *dom)
160 ADS_STATUS status;
161 struct idmap_ad_context * ctx;
163 status = ad_idmap_cached_connection_internal(dom);
164 if (!ADS_ERR_OK(status)) {
165 return status;
168 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
170 /* if we have a valid ADS_STRUCT and the schema model is
171 defined, then we can return here. */
173 if ( ctx->ad_schema ) {
174 return ADS_SUCCESS;
177 /* Otherwise, set the schema model */
179 if ( (ctx->ad_map_type == WB_POSIX_MAP_SFU) ||
180 (ctx->ad_map_type == WB_POSIX_MAP_SFU20) ||
181 (ctx->ad_map_type == WB_POSIX_MAP_RFC2307) )
183 status = ads_check_posix_schema_mapping(NULL, ctx->ads, ctx->ad_map_type, &ctx->ad_schema);
184 if ( !ADS_ERR_OK(status) ) {
185 DEBUG(2,("ad_idmap_cached_connection: Failed to obtain schema details!\n"));
189 return status;
192 /************************************************************************
193 ***********************************************************************/
195 static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom,
196 const char *params)
198 struct idmap_ad_context *ctx;
199 char *config_option;
200 const char *range = NULL;
201 const char *schema_mode = NULL;
203 if ( (ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context)) == NULL ) {
204 DEBUG(0, ("Out of memory!\n"));
205 return NT_STATUS_NO_MEMORY;
208 if ( (config_option = talloc_asprintf(ctx, "idmap config %s", dom->name)) == NULL ) {
209 DEBUG(0, ("Out of memory!\n"));
210 talloc_free(ctx);
211 return NT_STATUS_NO_MEMORY;
214 /* load ranges */
215 range = lp_parm_const_string(-1, config_option, "range", NULL);
216 if (range && range[0]) {
217 if ((sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2) ||
218 (ctx->filter_low_id > ctx->filter_high_id)) {
219 DEBUG(1, ("ERROR: invalid filter range [%s]", range));
220 ctx->filter_low_id = 0;
221 ctx->filter_high_id = 0;
225 /* default map type */
226 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
228 /* schema mode */
229 schema_mode = lp_parm_const_string(-1, config_option, "schema_mode", NULL);
230 if ( schema_mode && schema_mode[0] ) {
231 if ( strequal(schema_mode, "sfu") )
232 ctx->ad_map_type = WB_POSIX_MAP_SFU;
233 else if ( strequal(schema_mode, "sfu20" ) )
234 ctx->ad_map_type = WB_POSIX_MAP_SFU20;
235 else if ( strequal(schema_mode, "rfc2307" ) )
236 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
237 else
238 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
239 schema_mode));
242 dom->private_data = ctx;
244 talloc_free(config_option);
246 return NT_STATUS_OK;
249 /************************************************************************
250 Search up to IDMAP_AD_MAX_IDS entries in maps for a match.
251 ***********************************************************************/
253 static struct id_map *find_map_by_id(struct id_map **maps, enum id_type type, uint32_t id)
255 int i;
257 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
258 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
259 return maps[i];
263 return NULL;
266 /************************************************************************
267 Search up to IDMAP_AD_MAX_IDS entries in maps for a match
268 ***********************************************************************/
270 static struct id_map *find_map_by_sid(struct id_map **maps, DOM_SID *sid)
272 int i;
274 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
275 if (sid_equal(maps[i]->sid, sid)) {
276 return maps[i];
280 return NULL;
283 /************************************************************************
284 ***********************************************************************/
286 static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
288 NTSTATUS ret;
289 TALLOC_CTX *memctx;
290 struct idmap_ad_context *ctx;
291 ADS_STATUS rc;
292 const char *attrs[] = { "sAMAccountType",
293 "objectSid",
294 NULL, /* uidnumber */
295 NULL, /* gidnumber */
296 NULL };
297 LDAPMessage *res = NULL;
298 LDAPMessage *entry = NULL;
299 char *filter = NULL;
300 int idx = 0;
301 int bidx = 0;
302 int count;
303 int i;
304 char *u_filter = NULL;
305 char *g_filter = NULL;
307 /* initialize the status to avoid suprise */
308 for (i = 0; ids[i]; i++) {
309 ids[i]->status = ID_UNKNOWN;
312 /* Only do query if we are online */
313 if (idmap_is_offline()) {
314 return NT_STATUS_FILE_IS_OFFLINE;
317 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
319 if ( (memctx = talloc_new(ctx)) == NULL ) {
320 DEBUG(0, ("Out of memory!\n"));
321 return NT_STATUS_NO_MEMORY;
324 rc = ad_idmap_cached_connection(dom);
325 if (!ADS_ERR_OK(rc)) {
326 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
327 ret = NT_STATUS_UNSUCCESSFUL;
328 /* ret = ads_ntstatus(rc); */
329 goto done;
332 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
333 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
335 again:
336 bidx = idx;
337 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
338 switch (ids[idx]->xid.type) {
339 case ID_TYPE_UID:
340 if ( ! u_filter) {
341 u_filter = talloc_asprintf(memctx, "(&(|"
342 "(sAMAccountType=%d)"
343 "(sAMAccountType=%d)"
344 "(sAMAccountType=%d))(|",
345 ATYPE_NORMAL_ACCOUNT,
346 ATYPE_WORKSTATION_TRUST,
347 ATYPE_INTERDOMAIN_TRUST);
349 u_filter = talloc_asprintf_append_buffer(u_filter, "(%s=%lu)",
350 ctx->ad_schema->posix_uidnumber_attr,
351 (unsigned long)ids[idx]->xid.id);
352 CHECK_ALLOC_DONE(u_filter);
353 break;
355 case ID_TYPE_GID:
356 if ( ! g_filter) {
357 g_filter = talloc_asprintf(memctx, "(&(|"
358 "(sAMAccountType=%d)"
359 "(sAMAccountType=%d))(|",
360 ATYPE_SECURITY_GLOBAL_GROUP,
361 ATYPE_SECURITY_LOCAL_GROUP);
363 g_filter = talloc_asprintf_append_buffer(g_filter, "(%s=%lu)",
364 ctx->ad_schema->posix_gidnumber_attr,
365 (unsigned long)ids[idx]->xid.id);
366 CHECK_ALLOC_DONE(g_filter);
367 break;
369 default:
370 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
371 ids[idx]->status = ID_UNKNOWN;
372 continue;
375 filter = talloc_asprintf(memctx, "(|");
376 CHECK_ALLOC_DONE(filter);
377 if ( u_filter) {
378 filter = talloc_asprintf_append_buffer(filter, "%s))", u_filter);
379 CHECK_ALLOC_DONE(filter);
380 TALLOC_FREE(u_filter);
382 if ( g_filter) {
383 filter = talloc_asprintf_append_buffer(filter, "%s))", g_filter);
384 CHECK_ALLOC_DONE(filter);
385 TALLOC_FREE(g_filter);
387 filter = talloc_asprintf_append_buffer(filter, ")");
388 CHECK_ALLOC_DONE(filter);
390 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
391 if (!ADS_ERR_OK(rc)) {
392 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
393 ret = NT_STATUS_UNSUCCESSFUL;
394 goto done;
397 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
398 DEBUG(10, ("No IDs found\n"));
401 entry = res;
402 for (i = 0; (i < count) && entry; i++) {
403 DOM_SID sid;
404 enum id_type type;
405 struct id_map *map;
406 uint32_t id;
407 uint32_t atype;
409 if (i == 0) { /* first entry */
410 entry = ads_first_entry(ctx->ads, entry);
411 } else { /* following ones */
412 entry = ads_next_entry(ctx->ads, entry);
415 if ( !entry ) {
416 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
417 break;
420 /* first check if the SID is present */
421 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
422 DEBUG(2, ("Could not retrieve SID from entry\n"));
423 continue;
426 /* get type */
427 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
428 DEBUG(1, ("could not get SAM account type\n"));
429 continue;
432 switch (atype & 0xF0000000) {
433 case ATYPE_SECURITY_GLOBAL_GROUP:
434 case ATYPE_SECURITY_LOCAL_GROUP:
435 type = ID_TYPE_GID;
436 break;
437 case ATYPE_NORMAL_ACCOUNT:
438 case ATYPE_WORKSTATION_TRUST:
439 case ATYPE_INTERDOMAIN_TRUST:
440 type = ID_TYPE_UID;
441 break;
442 default:
443 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
444 continue;
447 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
448 ctx->ad_schema->posix_uidnumber_attr :
449 ctx->ad_schema->posix_gidnumber_attr,
450 &id))
452 DEBUG(1, ("Could not get unix ID\n"));
453 continue;
456 if ((id == 0) ||
457 (ctx->filter_low_id && (id < ctx->filter_low_id)) ||
458 (ctx->filter_high_id && (id > ctx->filter_high_id))) {
459 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
460 id, ctx->filter_low_id, ctx->filter_high_id));
461 continue;
464 map = find_map_by_id(&ids[bidx], type, id);
465 if (!map) {
466 DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
467 continue;
470 sid_copy(map->sid, &sid);
472 /* mapped */
473 map->status = ID_MAPPED;
475 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
476 (unsigned long)map->xid.id,
477 map->xid.type));
480 if (res) {
481 ads_msgfree(ctx->ads, res);
484 if (ids[idx]) { /* still some values to map */
485 goto again;
488 ret = NT_STATUS_OK;
490 /* mark all unknown/expired ones as unmapped */
491 for (i = 0; ids[i]; i++) {
492 if (ids[i]->status != ID_MAPPED)
493 ids[i]->status = ID_UNMAPPED;
496 done:
497 talloc_free(memctx);
498 return ret;
501 /************************************************************************
502 ***********************************************************************/
504 static NTSTATUS idmap_ad_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
506 NTSTATUS ret;
507 TALLOC_CTX *memctx;
508 struct idmap_ad_context *ctx;
509 ADS_STATUS rc;
510 const char *attrs[] = { "sAMAccountType",
511 "objectSid",
512 NULL, /* attr_uidnumber */
513 NULL, /* attr_gidnumber */
514 NULL };
515 LDAPMessage *res = NULL;
516 LDAPMessage *entry = NULL;
517 char *filter = NULL;
518 int idx = 0;
519 int bidx = 0;
520 int count;
521 int i;
522 char *sidstr;
524 /* initialize the status to avoid suprise */
525 for (i = 0; ids[i]; i++) {
526 ids[i]->status = ID_UNKNOWN;
529 /* Only do query if we are online */
530 if (idmap_is_offline()) {
531 return NT_STATUS_FILE_IS_OFFLINE;
534 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
536 if ( (memctx = talloc_new(ctx)) == NULL ) {
537 DEBUG(0, ("Out of memory!\n"));
538 return NT_STATUS_NO_MEMORY;
541 rc = ad_idmap_cached_connection(dom);
542 if (!ADS_ERR_OK(rc)) {
543 DEBUG(1, ("ADS uninitialized: %s\n", ads_errstr(rc)));
544 ret = NT_STATUS_UNSUCCESSFUL;
545 /* ret = ads_ntstatus(rc); */
546 goto done;
549 if (ctx->ad_schema == NULL) {
550 DEBUG(0, ("haven't got ctx->ad_schema ! \n"));
551 ret = NT_STATUS_UNSUCCESSFUL;
552 goto done;
555 attrs[2] = ctx->ad_schema->posix_uidnumber_attr;
556 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
558 again:
559 filter = talloc_asprintf(memctx, "(&(|"
560 "(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)" /* user account types */
561 "(sAMAccountType=%d)(sAMAccountType=%d)" /* group account types */
562 ")(|",
563 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST, ATYPE_INTERDOMAIN_TRUST,
564 ATYPE_SECURITY_GLOBAL_GROUP, ATYPE_SECURITY_LOCAL_GROUP);
566 CHECK_ALLOC_DONE(filter);
568 bidx = idx;
569 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
571 ids[idx]->status = ID_UNKNOWN;
573 sidstr = sid_binstring(ids[idx]->sid);
574 filter = talloc_asprintf_append_buffer(filter, "(objectSid=%s)", sidstr);
576 free(sidstr);
577 CHECK_ALLOC_DONE(filter);
579 filter = talloc_asprintf_append_buffer(filter, "))");
580 CHECK_ALLOC_DONE(filter);
581 DEBUG(10, ("Filter: [%s]\n", filter));
583 rc = ads_search_retry(ctx->ads, &res, filter, attrs);
584 if (!ADS_ERR_OK(rc)) {
585 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
586 ret = NT_STATUS_UNSUCCESSFUL;
587 goto done;
590 if ( (count = ads_count_replies(ctx->ads, res)) == 0 ) {
591 DEBUG(10, ("No IDs found\n"));
594 entry = res;
595 for (i = 0; (i < count) && entry; i++) {
596 DOM_SID sid;
597 enum id_type type;
598 struct id_map *map;
599 uint32_t id;
600 uint32_t atype;
602 if (i == 0) { /* first entry */
603 entry = ads_first_entry(ctx->ads, entry);
604 } else { /* following ones */
605 entry = ads_next_entry(ctx->ads, entry);
608 if ( !entry ) {
609 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
610 break;
613 /* first check if the SID is present */
614 if (!ads_pull_sid(ctx->ads, entry, "objectSid", &sid)) {
615 DEBUG(2, ("Could not retrieve SID from entry\n"));
616 continue;
619 map = find_map_by_sid(&ids[bidx], &sid);
620 if (!map) {
621 DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
622 continue;
625 /* get type */
626 if (!ads_pull_uint32(ctx->ads, entry, "sAMAccountType", &atype)) {
627 DEBUG(1, ("could not get SAM account type\n"));
628 continue;
631 switch (atype & 0xF0000000) {
632 case ATYPE_SECURITY_GLOBAL_GROUP:
633 case ATYPE_SECURITY_LOCAL_GROUP:
634 type = ID_TYPE_GID;
635 break;
636 case ATYPE_NORMAL_ACCOUNT:
637 case ATYPE_WORKSTATION_TRUST:
638 case ATYPE_INTERDOMAIN_TRUST:
639 type = ID_TYPE_UID;
640 break;
641 default:
642 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
643 continue;
646 if (!ads_pull_uint32(ctx->ads, entry, (type==ID_TYPE_UID) ?
647 ctx->ad_schema->posix_uidnumber_attr :
648 ctx->ad_schema->posix_gidnumber_attr,
649 &id))
651 DEBUG(1, ("Could not get unix ID\n"));
652 continue;
654 if ((id == 0) ||
655 (ctx->filter_low_id && (id < ctx->filter_low_id)) ||
656 (ctx->filter_high_id && (id > ctx->filter_high_id))) {
657 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
658 id, ctx->filter_low_id, ctx->filter_high_id));
659 continue;
662 /* mapped */
663 map->xid.type = type;
664 map->xid.id = id;
665 map->status = ID_MAPPED;
667 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
668 (unsigned long)map->xid.id,
669 map->xid.type));
672 if (res) {
673 ads_msgfree(ctx->ads, res);
676 if (ids[idx]) { /* still some values to map */
677 goto again;
680 ret = NT_STATUS_OK;
682 /* mark all unknwoni/expired ones as unmapped */
683 for (i = 0; ids[i]; i++) {
684 if (ids[i]->status != ID_MAPPED)
685 ids[i]->status = ID_UNMAPPED;
688 done:
689 talloc_free(memctx);
690 return ret;
693 /************************************************************************
694 ***********************************************************************/
696 static NTSTATUS idmap_ad_close(struct idmap_domain *dom)
698 struct idmap_ad_context * ctx;
700 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
702 if (ctx->ads != NULL) {
703 /* we own this ADS_STRUCT so make sure it goes away */
704 ctx->ads->is_mine = True;
705 ads_destroy( &ctx->ads );
706 ctx->ads = NULL;
709 TALLOC_FREE( ctx->ad_schema );
711 return NT_STATUS_OK;
715 * nss_info_{sfu,sfu20,rfc2307}
718 /************************************************************************
719 Initialize the {sfu,sfu20,rfc2307} state
720 ***********************************************************************/
722 static const char *wb_posix_map_unknown_string = "WB_POSIX_MAP_UNKNOWN";
723 static const char *wb_posix_map_template_string = "WB_POSIX_MAP_TEMPLATE";
724 static const char *wb_posix_map_sfu_string = "WB_POSIX_MAP_SFU";
725 static const char *wb_posix_map_sfu20_string = "WB_POSIX_MAP_SFU20";
726 static const char *wb_posix_map_rfc2307_string = "WB_POSIX_MAP_RFC2307";
727 static const char *wb_posix_map_unixinfo_string = "WB_POSIX_MAP_UNIXINFO";
729 static const char *ad_map_type_string(enum wb_posix_mapping map_type)
731 switch (map_type) {
732 case WB_POSIX_MAP_TEMPLATE:
733 return wb_posix_map_template_string;
734 case WB_POSIX_MAP_SFU:
735 return wb_posix_map_sfu_string;
736 case WB_POSIX_MAP_SFU20:
737 return wb_posix_map_sfu20_string;
738 case WB_POSIX_MAP_RFC2307:
739 return wb_posix_map_rfc2307_string;
740 case WB_POSIX_MAP_UNIXINFO:
741 return wb_posix_map_unixinfo_string;
742 default:
743 return wb_posix_map_unknown_string;
747 static NTSTATUS nss_ad_generic_init(struct nss_domain_entry *e,
748 enum wb_posix_mapping new_ad_map_type)
750 struct idmap_domain *dom;
751 struct idmap_ad_context *ctx;
753 if (e->state != NULL) {
754 dom = talloc_get_type(e->state, struct idmap_domain);
755 } else {
756 dom = TALLOC_ZERO_P(e, struct idmap_domain);
757 if (dom == NULL) {
758 DEBUG(0, ("Out of memory!\n"));
759 return NT_STATUS_NO_MEMORY;
761 e->state = dom;
764 if (e->domain != NULL) {
765 dom->name = talloc_strdup(dom, e->domain);
766 if (dom->name == NULL) {
767 DEBUG(0, ("Out of memory!\n"));
768 return NT_STATUS_NO_MEMORY;
772 if (dom->private_data != NULL) {
773 ctx = talloc_get_type(dom->private_data,
774 struct idmap_ad_context);
775 } else {
776 ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context);
777 if (ctx == NULL) {
778 DEBUG(0, ("Out of memory!\n"));
779 return NT_STATUS_NO_MEMORY;
781 ctx->ad_map_type = WB_POSIX_MAP_RFC2307;
782 dom->private_data = ctx;
785 if ((ctx->ad_map_type != WB_POSIX_MAP_UNKNOWN) &&
786 (ctx->ad_map_type != new_ad_map_type))
788 DEBUG(2, ("nss_ad_generic_init: "
789 "Warning: overriding previously set posix map type "
790 "%s for domain %s with map type %s.\n",
791 ad_map_type_string(ctx->ad_map_type),
792 dom->name,
793 ad_map_type_string(new_ad_map_type)));
796 ctx->ad_map_type = new_ad_map_type;
798 return NT_STATUS_OK;
801 static NTSTATUS nss_sfu_init( struct nss_domain_entry *e )
803 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU);
806 static NTSTATUS nss_sfu20_init( struct nss_domain_entry *e )
808 return nss_ad_generic_init(e, WB_POSIX_MAP_SFU20);
811 static NTSTATUS nss_rfc2307_init( struct nss_domain_entry *e )
813 return nss_ad_generic_init(e, WB_POSIX_MAP_RFC2307);
817 /************************************************************************
818 ***********************************************************************/
820 static NTSTATUS nss_ad_get_info( struct nss_domain_entry *e,
821 const DOM_SID *sid,
822 TALLOC_CTX *mem_ctx,
823 ADS_STRUCT *ads,
824 LDAPMessage *msg,
825 char **homedir,
826 char **shell,
827 char **gecos,
828 uint32 *gid )
830 const char *attrs[] = {NULL, /* attr_homedir */
831 NULL, /* attr_shell */
832 NULL, /* attr_gecos */
833 NULL, /* attr_gidnumber */
834 NULL };
835 char *filter = NULL;
836 LDAPMessage *msg_internal = NULL;
837 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
838 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
839 char *sidstr = NULL;
840 struct idmap_domain *dom;
841 struct idmap_ad_context *ctx;
843 DEBUG(10, ("nss_ad_get_info called for sid [%s] in domain '%s'\n",
844 sid_string_dbg(sid), e->domain?e->domain:"NULL"));
846 /* Only do query if we are online */
847 if (idmap_is_offline()) {
848 return NT_STATUS_FILE_IS_OFFLINE;
851 dom = talloc_get_type(e->state, struct idmap_domain);
852 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
854 ads_status = ad_idmap_cached_connection(dom);
855 if (!ADS_ERR_OK(ads_status)) {
856 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
859 if (!ctx->ad_schema) {
860 DEBUG(10, ("nss_ad_get_info: no ad_schema configured!\n"));
861 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
864 if (!sid || !homedir || !shell || !gecos) {
865 return NT_STATUS_INVALID_PARAMETER;
868 /* See if we can use the ADS connection struct swe were given */
870 if (ads) {
871 DEBUG(10, ("nss_ad_get_info: using given ads connection and "
872 "LDAP message (%p)\n", msg));
874 *homedir = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_homedir_attr );
875 *shell = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_shell_attr );
876 *gecos = ads_pull_string( ads, mem_ctx, msg, ctx->ad_schema->posix_gecos_attr );
878 if (gid) {
879 if ( !ads_pull_uint32(ads, msg, ctx->ad_schema->posix_gidnumber_attr, gid ) )
880 *gid = (uint32)-1;
883 nt_status = NT_STATUS_OK;
884 goto done;
887 /* Have to do our own query */
889 DEBUG(10, ("nss_ad_get_info: no ads connection given, doing our "
890 "own query\n"));
892 attrs[0] = ctx->ad_schema->posix_homedir_attr;
893 attrs[1] = ctx->ad_schema->posix_shell_attr;
894 attrs[2] = ctx->ad_schema->posix_gecos_attr;
895 attrs[3] = ctx->ad_schema->posix_gidnumber_attr;
897 sidstr = sid_binstring(sid);
898 filter = talloc_asprintf(mem_ctx, "(objectSid=%s)", sidstr);
899 SAFE_FREE(sidstr);
901 if (!filter) {
902 nt_status = NT_STATUS_NO_MEMORY;
903 goto done;
906 ads_status = ads_search_retry(ctx->ads, &msg_internal, filter, attrs);
907 if (!ADS_ERR_OK(ads_status)) {
908 nt_status = ads_ntstatus(ads_status);
909 goto done;
912 *homedir = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_homedir_attr);
913 *shell = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_shell_attr);
914 *gecos = ads_pull_string(ctx->ads, mem_ctx, msg_internal, ctx->ad_schema->posix_gecos_attr);
916 if (gid) {
917 if (!ads_pull_uint32(ctx->ads, msg_internal, ctx->ad_schema->posix_gidnumber_attr, gid))
918 *gid = (uint32)-1;
921 nt_status = NT_STATUS_OK;
923 done:
924 if (msg_internal) {
925 ads_msgfree(ctx->ads, msg_internal);
928 return nt_status;
931 /**********************************************************************
932 *********************************************************************/
934 static NTSTATUS nss_ad_map_to_alias(TALLOC_CTX *mem_ctx,
935 struct nss_domain_entry *e,
936 const char *name,
937 char **alias)
939 const char *attrs[] = {NULL, /* attr_uid */
940 NULL };
941 char *filter = NULL;
942 LDAPMessage *msg = NULL;
943 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
944 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
945 struct idmap_domain *dom;
946 struct idmap_ad_context *ctx = NULL;
948 /* Check incoming parameters */
950 if ( !e || !e->domain || !name || !*alias) {
951 nt_status = NT_STATUS_INVALID_PARAMETER;
952 goto done;
955 /* Only do query if we are online */
957 if (idmap_is_offline()) {
958 nt_status = NT_STATUS_FILE_IS_OFFLINE;
959 goto done;
962 dom = talloc_get_type(e->state, struct idmap_domain);
963 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
965 ads_status = ad_idmap_cached_connection(dom);
966 if (!ADS_ERR_OK(ads_status)) {
967 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
970 if (!ctx->ad_schema) {
971 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
972 goto done;
975 attrs[0] = ctx->ad_schema->posix_uid_attr;
977 filter = talloc_asprintf(mem_ctx,
978 "(sAMAccountName=%s)",
979 name);
980 if (!filter) {
981 nt_status = NT_STATUS_NO_MEMORY;
982 goto done;
985 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
986 if (!ADS_ERR_OK(ads_status)) {
987 nt_status = ads_ntstatus(ads_status);
988 goto done;
991 *alias = ads_pull_string(ctx->ads, mem_ctx, msg, ctx->ad_schema->posix_uid_attr);
993 if (!*alias) {
994 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
997 nt_status = NT_STATUS_OK;
999 done:
1000 if (filter) {
1001 talloc_destroy(filter);
1003 if (msg) {
1004 ads_msgfree(ctx->ads, msg);
1007 return nt_status;
1010 /**********************************************************************
1011 *********************************************************************/
1013 static NTSTATUS nss_ad_map_from_alias( TALLOC_CTX *mem_ctx,
1014 struct nss_domain_entry *e,
1015 const char *alias,
1016 char **name )
1018 const char *attrs[] = {"sAMAccountName",
1019 NULL };
1020 char *filter = NULL;
1021 LDAPMessage *msg = NULL;
1022 ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1023 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1024 char *username;
1025 struct idmap_domain *dom;
1026 struct idmap_ad_context *ctx = NULL;
1028 /* Check incoming parameters */
1030 if ( !alias || !name) {
1031 nt_status = NT_STATUS_INVALID_PARAMETER;
1032 goto done;
1035 /* Only do query if we are online */
1037 if (idmap_is_offline()) {
1038 nt_status = NT_STATUS_FILE_IS_OFFLINE;
1039 goto done;
1042 dom = talloc_get_type(e->state, struct idmap_domain);
1043 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
1045 ads_status = ad_idmap_cached_connection(dom);
1046 if (!ADS_ERR_OK(ads_status)) {
1047 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1050 if (!ctx->ad_schema) {
1051 nt_status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1052 goto done;
1055 filter = talloc_asprintf(mem_ctx,
1056 "(%s=%s)",
1057 ctx->ad_schema->posix_uid_attr,
1058 alias);
1059 if (!filter) {
1060 nt_status = NT_STATUS_NO_MEMORY;
1061 goto done;
1064 ads_status = ads_search_retry(ctx->ads, &msg, filter, attrs);
1065 if (!ADS_ERR_OK(ads_status)) {
1066 nt_status = ads_ntstatus(ads_status);
1067 goto done;
1070 username = ads_pull_string(ctx->ads, mem_ctx, msg,
1071 "sAMAccountName");
1072 if (!username) {
1073 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1076 *name = talloc_asprintf(mem_ctx, "%s\\%s",
1077 lp_workgroup(),
1078 username);
1079 if (!*name) {
1080 nt_status = NT_STATUS_NO_MEMORY;
1081 goto done;
1084 nt_status = NT_STATUS_OK;
1086 done:
1087 if (filter) {
1088 talloc_destroy(filter);
1090 if (msg) {
1091 ads_msgfree(ctx->ads, msg);
1094 return nt_status;
1098 /************************************************************************
1099 ***********************************************************************/
1101 static NTSTATUS nss_ad_close( void )
1103 /* nothing to do. All memory is free()'d by the idmap close_fn() */
1105 return NT_STATUS_OK;
1108 /************************************************************************
1109 Function dispatch tables for the idmap and nss plugins
1110 ***********************************************************************/
1112 static struct idmap_methods ad_methods = {
1113 .init = idmap_ad_initialize,
1114 .unixids_to_sids = idmap_ad_unixids_to_sids,
1115 .sids_to_unixids = idmap_ad_sids_to_unixids,
1116 .close_fn = idmap_ad_close
1119 /* The SFU and RFC2307 NSS plugins share everything but the init
1120 function which sets the intended schema model to use */
1122 static struct nss_info_methods nss_rfc2307_methods = {
1123 .init = nss_rfc2307_init,
1124 .get_nss_info = nss_ad_get_info,
1125 .map_to_alias = nss_ad_map_to_alias,
1126 .map_from_alias = nss_ad_map_from_alias,
1127 .close_fn = nss_ad_close
1130 static struct nss_info_methods nss_sfu_methods = {
1131 .init = nss_sfu_init,
1132 .get_nss_info = nss_ad_get_info,
1133 .map_to_alias = nss_ad_map_to_alias,
1134 .map_from_alias = nss_ad_map_from_alias,
1135 .close_fn = nss_ad_close
1138 static struct nss_info_methods nss_sfu20_methods = {
1139 .init = nss_sfu20_init,
1140 .get_nss_info = nss_ad_get_info,
1141 .map_to_alias = nss_ad_map_to_alias,
1142 .map_from_alias = nss_ad_map_from_alias,
1143 .close_fn = nss_ad_close
1148 /************************************************************************
1149 Initialize the plugins
1150 ***********************************************************************/
1152 NTSTATUS idmap_ad_init(void)
1154 static NTSTATUS status_idmap_ad = NT_STATUS_UNSUCCESSFUL;
1155 static NTSTATUS status_nss_rfc2307 = NT_STATUS_UNSUCCESSFUL;
1156 static NTSTATUS status_nss_sfu = NT_STATUS_UNSUCCESSFUL;
1157 static NTSTATUS status_nss_sfu20 = NT_STATUS_UNSUCCESSFUL;
1159 /* Always register the AD method first in order to get the
1160 idmap_domain interface called */
1162 if ( !NT_STATUS_IS_OK(status_idmap_ad) ) {
1163 status_idmap_ad = smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
1164 "ad", &ad_methods);
1165 if ( !NT_STATUS_IS_OK(status_idmap_ad) )
1166 return status_idmap_ad;
1169 if ( !NT_STATUS_IS_OK( status_nss_rfc2307 ) ) {
1170 status_nss_rfc2307 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1171 "rfc2307", &nss_rfc2307_methods );
1172 if ( !NT_STATUS_IS_OK(status_nss_rfc2307) )
1173 return status_nss_rfc2307;
1176 if ( !NT_STATUS_IS_OK( status_nss_sfu ) ) {
1177 status_nss_sfu = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1178 "sfu", &nss_sfu_methods );
1179 if ( !NT_STATUS_IS_OK(status_nss_sfu) )
1180 return status_nss_sfu;
1183 if ( !NT_STATUS_IS_OK( status_nss_sfu20 ) ) {
1184 status_nss_sfu20 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
1185 "sfu20", &nss_sfu20_methods );
1186 if ( !NT_STATUS_IS_OK(status_nss_sfu20) )
1187 return status_nss_sfu20;
1190 return NT_STATUS_OK;