Revert "Fix indentation"
[Samba.git] / source3 / winbindd / idmap_ad.c
blobca1cf81ca9a97227a9bd1b2687de7c84e08a8628
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
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include "includes.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_IDMAP
32 #define WINBIND_CCACHE_NAME "MEMORY:winbind_ccache"
34 #define IDMAP_AD_MAX_IDS 30
35 #define CHECK_ALLOC_DONE(mem) do { \
36 if (!mem) { \
37 DEBUG(0, ("Out of memory!\n")); \
38 ret = NT_STATUS_NO_MEMORY; \
39 goto done; \
40 } \
41 } while (0)
43 struct idmap_ad_context {
44 uint32_t filter_low_id;
45 uint32_t filter_high_id;
48 NTSTATUS init_module(void);
50 static ADS_STRUCT *ad_idmap_ads = NULL;
51 static struct posix_schema *ad_schema = NULL;
52 static enum wb_posix_mapping ad_map_type = WB_POSIX_MAP_UNKNOWN;
54 /************************************************************************
55 ***********************************************************************/
57 static ADS_STRUCT *ad_idmap_cached_connection_internal(void)
59 ADS_STRUCT *ads;
60 ADS_STATUS status;
61 bool local = False;
62 fstring dc_name;
63 struct sockaddr_storage dc_ip;
65 if (ad_idmap_ads != NULL) {
67 time_t expire;
68 time_t now = time(NULL);
70 ads = ad_idmap_ads;
72 expire = MIN(ads->auth.tgt_expire, ads->auth.tgs_expire);
74 /* check for a valid structure */
75 DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
76 (uint32)expire-(uint32)now, (uint32) expire, (uint32) now));
78 if ( ads->config.realm && (expire > time(NULL))) {
79 return ads;
82 /* we own this ADS_STRUCT so make sure it goes away */
83 DEBUG(7,("Deleting expired krb5 credential cache\n"));
84 ads->is_mine = True;
85 ads_destroy( &ads );
86 ads_kdestroy(WINBIND_CCACHE_NAME);
87 ad_idmap_ads = NULL;
88 TALLOC_FREE( ad_schema );
91 if (!local) {
92 /* we don't want this to affect the users ccache */
93 setenv("KRB5CCNAME", WINBIND_CCACHE_NAME, 1);
96 if ( (ads = ads_init(lp_realm(), lp_workgroup(), NULL)) == NULL ) {
97 DEBUG(1,("ads_init failed\n"));
98 return NULL;
101 /* the machine acct password might have changed - fetch it every
102 * time */
103 SAFE_FREE(ads->auth.password);
104 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
106 SAFE_FREE(ads->auth.realm);
107 ads->auth.realm = SMB_STRDUP(lp_realm());
109 /* setup server affinity */
111 get_dc_name( NULL, ads->auth.realm, dc_name, &dc_ip );
113 status = ads_connect(ads);
114 if (!ADS_ERR_OK(status)) {
115 DEBUG(1, ("ad_idmap_init: failed to connect to AD\n"));
116 ads_destroy(&ads);
117 return NULL;
120 ads->is_mine = False;
122 ad_idmap_ads = ads;
124 return ads;
127 /************************************************************************
128 ***********************************************************************/
130 static ADS_STRUCT *ad_idmap_cached_connection(void)
132 ADS_STRUCT *ads = ad_idmap_cached_connection_internal();
134 if ( !ads )
135 return NULL;
137 /* if we have a valid ADS_STRUCT and the schema model is
138 defined, then we can return here. */
140 if ( ad_schema )
141 return ads;
143 /* Otherwise, set the schema model */
145 if ( (ad_map_type == WB_POSIX_MAP_SFU) ||
146 (ad_map_type == WB_POSIX_MAP_SFU20) ||
147 (ad_map_type == WB_POSIX_MAP_RFC2307) )
149 ADS_STATUS schema_status;
151 schema_status = ads_check_posix_schema_mapping( NULL, ads, ad_map_type, &ad_schema);
152 if ( !ADS_ERR_OK(schema_status) ) {
153 DEBUG(2,("ad_idmap_cached_connection: Failed to obtain schema details!\n"));
154 return NULL;
158 return ads;
161 /************************************************************************
162 ***********************************************************************/
164 static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom)
166 struct idmap_ad_context *ctx;
167 char *config_option;
168 const char *range = NULL;
169 const char *schema_mode = NULL;
171 if ( (ctx = TALLOC_ZERO_P(dom, struct idmap_ad_context)) == NULL ) {
172 DEBUG(0, ("Out of memory!\n"));
173 return NT_STATUS_NO_MEMORY;
176 if ( (config_option = talloc_asprintf(ctx, "idmap config %s", dom->name)) == NULL ) {
177 DEBUG(0, ("Out of memory!\n"));
178 talloc_free(ctx);
179 return NT_STATUS_NO_MEMORY;
182 /* load ranges */
183 range = lp_parm_const_string(-1, config_option, "range", NULL);
184 if (range && range[0]) {
185 if ((sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2) ||
186 (ctx->filter_low_id > ctx->filter_high_id)) {
187 DEBUG(1, ("ERROR: invalid filter range [%s]", range));
188 ctx->filter_low_id = 0;
189 ctx->filter_high_id = 0;
193 /* schema mode */
194 if ( ad_map_type == WB_POSIX_MAP_UNKNOWN )
195 ad_map_type = WB_POSIX_MAP_RFC2307;
196 schema_mode = lp_parm_const_string(-1, config_option, "schema_mode", NULL);
197 if ( schema_mode && schema_mode[0] ) {
198 if ( strequal(schema_mode, "sfu") )
199 ad_map_type = WB_POSIX_MAP_SFU;
200 else if ( strequal(schema_mode, "sfu20" ) )
201 ad_map_type = WB_POSIX_MAP_SFU20;
202 else if ( strequal(schema_mode, "rfc2307" ) )
203 ad_map_type = WB_POSIX_MAP_RFC2307;
204 else
205 DEBUG(0,("idmap_ad_initialize: Unknown schema_mode (%s)\n",
206 schema_mode));
209 dom->private_data = ctx;
210 dom->initialized = True;
212 talloc_free(config_option);
214 return NT_STATUS_OK;
217 /************************************************************************
218 Search up to IDMAP_AD_MAX_IDS entries in maps for a match.
219 ***********************************************************************/
221 static struct id_map *find_map_by_id(struct id_map **maps, enum id_type type, uint32_t id)
223 int i;
225 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
226 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
227 return maps[i];
231 return NULL;
234 /************************************************************************
235 Search up to IDMAP_AD_MAX_IDS entries in maps for a match
236 ***********************************************************************/
238 static struct id_map *find_map_by_sid(struct id_map **maps, DOM_SID *sid)
240 int i;
242 for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
243 if (sid_equal(maps[i]->sid, sid)) {
244 return maps[i];
248 return NULL;
251 /************************************************************************
252 ***********************************************************************/
254 static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
256 NTSTATUS ret;
257 TALLOC_CTX *memctx;
258 struct idmap_ad_context *ctx;
259 ADS_STATUS rc;
260 ADS_STRUCT *ads;
261 const char *attrs[] = { "sAMAccountType",
262 "objectSid",
263 NULL, /* uidnumber */
264 NULL, /* gidnumber */
265 NULL };
266 LDAPMessage *res = NULL;
267 LDAPMessage *entry = NULL;
268 char *filter = NULL;
269 int idx = 0;
270 int bidx = 0;
271 int count;
272 int i;
273 char *u_filter = NULL;
274 char *g_filter = NULL;
276 /* Only do query if we are online */
277 if (idmap_is_offline()) {
278 return NT_STATUS_FILE_IS_OFFLINE;
281 /* Initilization my have been deferred because we were offline */
282 if ( ! dom->initialized) {
283 ret = idmap_ad_initialize(dom);
284 if ( ! NT_STATUS_IS_OK(ret)) {
285 return ret;
289 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
291 if ( (memctx = talloc_new(ctx)) == NULL ) {
292 DEBUG(0, ("Out of memory!\n"));
293 return NT_STATUS_NO_MEMORY;
296 if ( (ads = ad_idmap_cached_connection()) == NULL ) {
297 DEBUG(1, ("ADS uninitialized\n"));
298 ret = NT_STATUS_UNSUCCESSFUL;
299 goto done;
302 attrs[2] = ad_schema->posix_uidnumber_attr;
303 attrs[3] = ad_schema->posix_gidnumber_attr;
305 again:
306 bidx = idx;
307 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
308 switch (ids[idx]->xid.type) {
309 case ID_TYPE_UID:
310 if ( ! u_filter) {
311 u_filter = talloc_asprintf(memctx, "(&(|"
312 "(sAMAccountType=%d)"
313 "(sAMAccountType=%d)"
314 "(sAMAccountType=%d))(|",
315 ATYPE_NORMAL_ACCOUNT,
316 ATYPE_WORKSTATION_TRUST,
317 ATYPE_INTERDOMAIN_TRUST);
319 u_filter = talloc_asprintf_append_buffer(u_filter, "(%s=%lu)",
320 ad_schema->posix_uidnumber_attr,
321 (unsigned long)ids[idx]->xid.id);
322 CHECK_ALLOC_DONE(u_filter);
323 break;
325 case ID_TYPE_GID:
326 if ( ! g_filter) {
327 g_filter = talloc_asprintf(memctx, "(&(|"
328 "(sAMAccountType=%d)"
329 "(sAMAccountType=%d))(|",
330 ATYPE_SECURITY_GLOBAL_GROUP,
331 ATYPE_SECURITY_LOCAL_GROUP);
333 g_filter = talloc_asprintf_append_buffer(g_filter, "(%s=%lu)",
334 ad_schema->posix_gidnumber_attr,
335 (unsigned long)ids[idx]->xid.id);
336 CHECK_ALLOC_DONE(g_filter);
337 break;
339 default:
340 DEBUG(3, ("Error: mapping requested but Unknown ID type\n"));
341 ids[idx]->status = ID_UNKNOWN;
342 continue;
345 filter = talloc_asprintf(memctx, "(|");
346 CHECK_ALLOC_DONE(filter);
347 if ( u_filter) {
348 filter = talloc_asprintf_append_buffer(filter, "%s))", u_filter);
349 CHECK_ALLOC_DONE(filter);
350 TALLOC_FREE(u_filter);
352 if ( g_filter) {
353 filter = talloc_asprintf_append_buffer(filter, "%s))", g_filter);
354 CHECK_ALLOC_DONE(filter);
355 TALLOC_FREE(g_filter);
357 filter = talloc_asprintf_append_buffer(filter, ")");
358 CHECK_ALLOC_DONE(filter);
360 rc = ads_search_retry(ads, &res, filter, attrs);
361 if (!ADS_ERR_OK(rc)) {
362 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
363 ret = NT_STATUS_UNSUCCESSFUL;
364 goto done;
367 if ( (count = ads_count_replies(ads, res)) == 0 ) {
368 DEBUG(10, ("No IDs found\n"));
371 entry = res;
372 for (i = 0; (i < count) && entry; i++) {
373 DOM_SID sid;
374 enum id_type type;
375 struct id_map *map;
376 uint32_t id;
377 uint32_t atype;
379 if (i == 0) { /* first entry */
380 entry = ads_first_entry(ads, entry);
381 } else { /* following ones */
382 entry = ads_next_entry(ads, entry);
385 if ( !entry ) {
386 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
387 break;
390 /* first check if the SID is present */
391 if (!ads_pull_sid(ads, entry, "objectSid", &sid)) {
392 DEBUG(2, ("Could not retrieve SID from entry\n"));
393 continue;
396 /* get type */
397 if (!ads_pull_uint32(ads, entry, "sAMAccountType", &atype)) {
398 DEBUG(1, ("could not get SAM account type\n"));
399 continue;
402 switch (atype & 0xF0000000) {
403 case ATYPE_SECURITY_GLOBAL_GROUP:
404 case ATYPE_SECURITY_LOCAL_GROUP:
405 type = ID_TYPE_GID;
406 break;
407 case ATYPE_NORMAL_ACCOUNT:
408 case ATYPE_WORKSTATION_TRUST:
409 case ATYPE_INTERDOMAIN_TRUST:
410 type = ID_TYPE_UID;
411 break;
412 default:
413 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
414 continue;
417 if (!ads_pull_uint32(ads, entry, (type==ID_TYPE_UID) ?
418 ad_schema->posix_uidnumber_attr :
419 ad_schema->posix_gidnumber_attr,
420 &id))
422 DEBUG(1, ("Could not get unix ID\n"));
423 continue;
426 if ((id == 0) ||
427 (ctx->filter_low_id && (id < ctx->filter_low_id)) ||
428 (ctx->filter_high_id && (id > ctx->filter_high_id))) {
429 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
430 id, ctx->filter_low_id, ctx->filter_high_id));
431 continue;
434 map = find_map_by_id(&ids[bidx], type, id);
435 if (!map) {
436 DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
437 continue;
440 sid_copy(map->sid, &sid);
442 /* mapped */
443 map->status = ID_MAPPED;
445 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
446 (unsigned long)map->xid.id,
447 map->xid.type));
450 if (res) {
451 ads_msgfree(ads, res);
454 if (ids[idx]) { /* still some values to map */
455 goto again;
458 ret = NT_STATUS_OK;
460 /* mark all unknown/expired ones as unmapped */
461 for (i = 0; ids[i]; i++) {
462 if (ids[i]->status != ID_MAPPED)
463 ids[i]->status = ID_UNMAPPED;
466 done:
467 talloc_free(memctx);
468 return ret;
471 /************************************************************************
472 ***********************************************************************/
474 static NTSTATUS idmap_ad_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
476 NTSTATUS ret;
477 TALLOC_CTX *memctx;
478 struct idmap_ad_context *ctx;
479 ADS_STATUS rc;
480 ADS_STRUCT *ads;
481 const char *attrs[] = { "sAMAccountType",
482 "objectSid",
483 NULL, /* attr_uidnumber */
484 NULL, /* attr_gidnumber */
485 NULL };
486 LDAPMessage *res = NULL;
487 LDAPMessage *entry = NULL;
488 char *filter = NULL;
489 int idx = 0;
490 int bidx = 0;
491 int count;
492 int i;
493 char *sidstr;
495 /* Only do query if we are online */
496 if (idmap_is_offline()) {
497 return NT_STATUS_FILE_IS_OFFLINE;
500 /* Initilization my have been deferred because we were offline */
501 if ( ! dom->initialized) {
502 ret = idmap_ad_initialize(dom);
503 if ( ! NT_STATUS_IS_OK(ret)) {
504 return ret;
508 ctx = talloc_get_type(dom->private_data, struct idmap_ad_context);
510 if ( (memctx = talloc_new(ctx)) == NULL ) {
511 DEBUG(0, ("Out of memory!\n"));
512 return NT_STATUS_NO_MEMORY;
515 if ( (ads = ad_idmap_cached_connection()) == NULL ) {
516 DEBUG(1, ("ADS uninitialized\n"));
517 ret = NT_STATUS_UNSUCCESSFUL;
518 goto done;
521 attrs[2] = ad_schema->posix_uidnumber_attr;
522 attrs[3] = ad_schema->posix_gidnumber_attr;
524 again:
525 filter = talloc_asprintf(memctx, "(&(|"
526 "(sAMAccountType=%d)(sAMAccountType=%d)(sAMAccountType=%d)" /* user account types */
527 "(sAMAccountType=%d)(sAMAccountType=%d)" /* group account types */
528 ")(|",
529 ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST, ATYPE_INTERDOMAIN_TRUST,
530 ATYPE_SECURITY_GLOBAL_GROUP, ATYPE_SECURITY_LOCAL_GROUP);
532 CHECK_ALLOC_DONE(filter);
534 bidx = idx;
535 for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
537 sidstr = sid_binstring(ids[idx]->sid);
538 filter = talloc_asprintf_append_buffer(filter, "(objectSid=%s)", sidstr);
540 free(sidstr);
541 CHECK_ALLOC_DONE(filter);
543 filter = talloc_asprintf_append_buffer(filter, "))");
544 CHECK_ALLOC_DONE(filter);
545 DEBUG(10, ("Filter: [%s]\n", filter));
547 rc = ads_search_retry(ads, &res, filter, attrs);
548 if (!ADS_ERR_OK(rc)) {
549 DEBUG(1, ("ERROR: ads search returned: %s\n", ads_errstr(rc)));
550 ret = NT_STATUS_UNSUCCESSFUL;
551 goto done;
554 if ( (count = ads_count_replies(ads, res)) == 0 ) {
555 DEBUG(10, ("No IDs found\n"));
558 entry = res;
559 for (i = 0; (i < count) && entry; i++) {
560 DOM_SID sid;
561 enum id_type type;
562 struct id_map *map;
563 uint32_t id;
564 uint32_t atype;
566 if (i == 0) { /* first entry */
567 entry = ads_first_entry(ads, entry);
568 } else { /* following ones */
569 entry = ads_next_entry(ads, entry);
572 if ( !entry ) {
573 DEBUG(2, ("ERROR: Unable to fetch ldap entries from results\n"));
574 break;
577 /* first check if the SID is present */
578 if (!ads_pull_sid(ads, entry, "objectSid", &sid)) {
579 DEBUG(2, ("Could not retrieve SID from entry\n"));
580 continue;
583 map = find_map_by_sid(&ids[bidx], &sid);
584 if (!map) {
585 DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
586 continue;
589 /* get type */
590 if (!ads_pull_uint32(ads, entry, "sAMAccountType", &atype)) {
591 DEBUG(1, ("could not get SAM account type\n"));
592 continue;
595 switch (atype & 0xF0000000) {
596 case ATYPE_SECURITY_GLOBAL_GROUP:
597 case ATYPE_SECURITY_LOCAL_GROUP:
598 type = ID_TYPE_GID;
599 break;
600 case ATYPE_NORMAL_ACCOUNT:
601 case ATYPE_WORKSTATION_TRUST:
602 case ATYPE_INTERDOMAIN_TRUST:
603 type = ID_TYPE_UID;
604 break;
605 default:
606 DEBUG(1, ("unrecognized SAM account type %08x\n", atype));
607 continue;
610 if (!ads_pull_uint32(ads, entry, (type==ID_TYPE_UID) ?
611 ad_schema->posix_uidnumber_attr :
612 ad_schema->posix_gidnumber_attr,
613 &id))
615 DEBUG(1, ("Could not get unix ID\n"));
616 continue;
618 if ((id == 0) ||
619 (ctx->filter_low_id && (id < ctx->filter_low_id)) ||
620 (ctx->filter_high_id && (id > ctx->filter_high_id))) {
621 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
622 id, ctx->filter_low_id, ctx->filter_high_id));
623 continue;
626 /* mapped */
627 map->xid.type = type;
628 map->xid.id = id;
629 map->status = ID_MAPPED;
631 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
632 (unsigned long)map->xid.id,
633 map->xid.type));
636 if (res) {
637 ads_msgfree(ads, res);
640 if (ids[idx]) { /* still some values to map */
641 goto again;
644 ret = NT_STATUS_OK;
646 /* mark all unknwoni/expired ones as unmapped */
647 for (i = 0; ids[i]; i++) {
648 if (ids[i]->status != ID_MAPPED)
649 ids[i]->status = ID_UNMAPPED;
652 done:
653 talloc_free(memctx);
654 return ret;
657 /************************************************************************
658 ***********************************************************************/
660 static NTSTATUS idmap_ad_close(struct idmap_domain *dom)
662 ADS_STRUCT *ads = ad_idmap_ads;
664 if (ads != NULL) {
665 /* we own this ADS_STRUCT so make sure it goes away */
666 ads->is_mine = True;
667 ads_destroy( &ads );
668 ad_idmap_ads = NULL;
671 TALLOC_FREE( ad_schema );
673 return NT_STATUS_OK;
677 * nss_info_{sfu,sfu20,rfc2307}
680 /************************************************************************
681 Initialize the {sfu,sfu20,rfc2307} state
682 ***********************************************************************/
684 static NTSTATUS nss_sfu_init( struct nss_domain_entry *e )
686 /* Sanity check if we have previously been called with a
687 different schema model */
689 if ( (ad_map_type != WB_POSIX_MAP_UNKNOWN) &&
690 (ad_map_type != WB_POSIX_MAP_SFU) )
692 DEBUG(0,("nss_sfu_init: Posix Map type has already been set. "
693 "Mixed schema models not supported!\n"));
694 return NT_STATUS_NOT_SUPPORTED;
697 ad_map_type = WB_POSIX_MAP_SFU;
699 return NT_STATUS_OK;
702 static NTSTATUS nss_sfu20_init( struct nss_domain_entry *e )
704 /* Sanity check if we have previously been called with a
705 different schema model */
707 if ( (ad_map_type != WB_POSIX_MAP_UNKNOWN) &&
708 (ad_map_type != WB_POSIX_MAP_SFU20) )
710 DEBUG(0,("nss_sfu20_init: Posix Map type has already been set. "
711 "Mixed schema models not supported!\n"));
712 return NT_STATUS_NOT_SUPPORTED;
715 ad_map_type = WB_POSIX_MAP_SFU20;
717 return NT_STATUS_OK;
720 static NTSTATUS nss_rfc2307_init( struct nss_domain_entry *e )
722 /* Sanity check if we have previously been called with a
723 different schema model */
725 if ( (ad_map_type != WB_POSIX_MAP_UNKNOWN) &&
726 (ad_map_type != WB_POSIX_MAP_RFC2307) )
728 DEBUG(0,("nss_rfc2307_init: Posix Map type has already been set. "
729 "Mixed schema models not supported!\n"));
730 return NT_STATUS_NOT_SUPPORTED;
733 ad_map_type = WB_POSIX_MAP_RFC2307;
735 return NT_STATUS_OK;
739 /************************************************************************
740 ***********************************************************************/
741 static NTSTATUS nss_ad_get_info( struct nss_domain_entry *e,
742 const DOM_SID *sid,
743 TALLOC_CTX *ctx,
744 ADS_STRUCT *ads,
745 LDAPMessage *msg,
746 char **homedir,
747 char **shell,
748 char **gecos,
749 uint32 *gid )
751 ADS_STRUCT *ads_internal = NULL;
753 /* Only do query if we are online */
754 if (idmap_is_offline()) {
755 return NT_STATUS_FILE_IS_OFFLINE;
758 /* We are assuming that the internal ADS_STRUCT is for the
759 same forest as the incoming *ads pointer */
761 ads_internal = ad_idmap_cached_connection();
763 if ( !ads_internal || !ad_schema )
764 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
766 if ( !homedir || !shell || !gecos )
767 return NT_STATUS_INVALID_PARAMETER;
769 *homedir = ads_pull_string( ads, ctx, msg, ad_schema->posix_homedir_attr );
770 *shell = ads_pull_string( ads, ctx, msg, ad_schema->posix_shell_attr );
771 *gecos = ads_pull_string( ads, ctx, msg, ad_schema->posix_gecos_attr );
773 if ( gid ) {
774 if ( !ads_pull_uint32(ads, msg, ad_schema->posix_gidnumber_attr, gid ) )
775 *gid = (uint32)-1;
778 return NT_STATUS_OK;
781 /************************************************************************
782 ***********************************************************************/
784 static NTSTATUS nss_ad_close( void )
786 /* nothing to do. All memory is free()'d by the idmap close_fn() */
788 return NT_STATUS_OK;
791 /************************************************************************
792 Function dispatch tables for the idmap and nss plugins
793 ***********************************************************************/
795 static struct idmap_methods ad_methods = {
796 .init = idmap_ad_initialize,
797 .unixids_to_sids = idmap_ad_unixids_to_sids,
798 .sids_to_unixids = idmap_ad_sids_to_unixids,
799 .close_fn = idmap_ad_close
802 /* The SFU and RFC2307 NSS plugins share everything but the init
803 function which sets the intended schema model to use */
805 static struct nss_info_methods nss_rfc2307_methods = {
806 .init = nss_rfc2307_init,
807 .get_nss_info = nss_ad_get_info,
808 .close_fn = nss_ad_close
811 static struct nss_info_methods nss_sfu_methods = {
812 .init = nss_sfu_init,
813 .get_nss_info = nss_ad_get_info,
814 .close_fn = nss_ad_close
817 static struct nss_info_methods nss_sfu20_methods = {
818 .init = nss_sfu20_init,
819 .get_nss_info = nss_ad_get_info,
820 .close_fn = nss_ad_close
825 /************************************************************************
826 Initialize the plugins
827 ***********************************************************************/
829 NTSTATUS idmap_ad_init(void)
831 static NTSTATUS status_idmap_ad = NT_STATUS_UNSUCCESSFUL;
832 static NTSTATUS status_nss_rfc2307 = NT_STATUS_UNSUCCESSFUL;
833 static NTSTATUS status_nss_sfu = NT_STATUS_UNSUCCESSFUL;
834 static NTSTATUS status_nss_sfu20 = NT_STATUS_UNSUCCESSFUL;
836 /* Always register the AD method first in order to get the
837 idmap_domain interface called */
839 if ( !NT_STATUS_IS_OK(status_idmap_ad) ) {
840 status_idmap_ad = smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
841 "ad", &ad_methods);
842 if ( !NT_STATUS_IS_OK(status_idmap_ad) )
843 return status_idmap_ad;
846 if ( !NT_STATUS_IS_OK( status_nss_rfc2307 ) ) {
847 status_nss_rfc2307 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
848 "rfc2307", &nss_rfc2307_methods );
849 if ( !NT_STATUS_IS_OK(status_nss_rfc2307) )
850 return status_nss_rfc2307;
853 if ( !NT_STATUS_IS_OK( status_nss_sfu ) ) {
854 status_nss_sfu = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
855 "sfu", &nss_sfu_methods );
856 if ( !NT_STATUS_IS_OK(status_nss_sfu) )
857 return status_nss_sfu;
860 if ( !NT_STATUS_IS_OK( status_nss_sfu20 ) ) {
861 status_nss_sfu20 = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
862 "sfu20", &nss_sfu20_methods );
863 if ( !NT_STATUS_IS_OK(status_nss_sfu20) )
864 return status_nss_sfu20;
867 return NT_STATUS_OK;