2 * idmap_adex: Support for AD Forests
4 * Copyright (C) Gerald (Jerry) Carter 2006-2008
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 2 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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "idmap_adex.h"
25 #define DBGC_CLASS DBGC_IDMAP
27 static struct likewise_cell
*_lw_cell_list
= NULL
;
29 /**********************************************************************
30 Return the current HEAD of the list
31 *********************************************************************/
33 struct likewise_cell
*cell_list_head(void)
39 /**********************************************************************
40 *********************************************************************/
42 void cell_destroy(struct likewise_cell
*c
)
48 ads_destroy(&c
->conn
);
53 /**********************************************************************
54 Free all cell entries and reset the list head to NULL
55 *********************************************************************/
57 void cell_list_destroy(void)
59 struct likewise_cell
*p
= _lw_cell_list
;
62 struct likewise_cell
*q
= p
->next
;
74 /**********************************************************************
75 Add a new cell structure to the list
76 *********************************************************************/
78 struct likewise_cell
* cell_new(void)
80 struct likewise_cell
*c
;
82 /* Each cell struct is a TALLOC_CTX* */
84 c
= TALLOC_ZERO_P(NULL
, struct likewise_cell
);
86 DEBUG(0,("cell_new: memory allocation failure!\n"));
93 /**********************************************************************
94 Add a new cell structure to the list
95 *********************************************************************/
97 bool cell_list_add(struct likewise_cell
* cell
)
103 /* Always add to the end */
105 DLIST_ADD_END(_lw_cell_list
, cell
, struct likewise_cell
*);
110 /**********************************************************************
111 Add a new cell structure to the list
112 *********************************************************************/
114 bool cell_list_remove(struct likewise_cell
* cell
)
120 /* Remove and drop the cell structure */
122 DLIST_REMOVE(_lw_cell_list
, cell
);
123 talloc_destroy(cell
);
128 /**********************************************************************
129 Set the containing DNS domain for a cell
130 *********************************************************************/
132 void cell_set_dns_domain(struct likewise_cell
*c
, const char *dns_domain
)
134 c
->dns_domain
= talloc_strdup(c
, dns_domain
);
137 /**********************************************************************
138 Set ADS connection for a cell
139 *********************************************************************/
141 void cell_set_connection(struct likewise_cell
*c
, ADS_STRUCT
*ads
)
146 /**********************************************************************
147 *********************************************************************/
149 void cell_set_flags(struct likewise_cell
*c
, uint32_t flags
)
154 /**********************************************************************
155 *********************************************************************/
157 void cell_clear_flags(struct likewise_cell
*c
, uint32_t flags
)
162 /**********************************************************************
164 *********************************************************************/
166 void cell_set_dn(struct likewise_cell
*c
, const char *dn
)
173 c
->dn
= talloc_strdup(c
, dn
);
176 /**********************************************************************
177 *********************************************************************/
179 void cell_set_domain_sid(struct likewise_cell
*c
, DOM_SID
*sid
)
181 sid_copy(&c
->domain_sid
, sid
);
188 /**********************************************************************
189 *********************************************************************/
191 const char* cell_search_base(struct likewise_cell
*c
)
196 return talloc_asprintf(c
, "cn=%s,%s", ADEX_CELL_RDN
, c
->dn
);
199 /**********************************************************************
200 *********************************************************************/
202 bool cell_search_forest(struct likewise_cell
*c
)
204 uint32_t test_flags
= LWCELL_FLAG_SEARCH_FOREST
;
206 return ((c
->flags
& test_flags
) == test_flags
);
209 /**********************************************************************
210 *********************************************************************/
212 uint32_t cell_flags(struct likewise_cell
*c
)
220 /**********************************************************************
221 *********************************************************************/
223 const char *cell_dns_domain(struct likewise_cell
*c
)
228 return c
->dns_domain
;
231 /**********************************************************************
232 *********************************************************************/
234 ADS_STRUCT
*cell_connection(struct likewise_cell
*c
)
243 * Connection functions
246 /********************************************************************
247 *******************************************************************/
249 NTSTATUS
cell_connect(struct likewise_cell
*c
)
251 ADS_STRUCT
*ads
= NULL
;
252 ADS_STATUS ads_status
;
254 struct sockaddr_storage dcip
;
255 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
257 /* have to at least have the AD domain name */
259 if (!c
->dns_domain
) {
260 nt_status
= NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
261 BAIL_ON_NTSTATUS_ERROR(nt_status
);
264 /* clear out any old information */
267 ads_destroy(&c
->conn
);
271 /* now setup the new connection */
273 ads
= ads_init(c
->dns_domain
, NULL
, NULL
);
274 BAIL_ON_PTR_ERROR(ads
, nt_status
);
277 secrets_fetch_machine_password(lp_workgroup(), NULL
, NULL
);
278 ads
->auth
.realm
= SMB_STRDUP(lp_realm());
280 /* Make the connection. We should already have an initial
281 TGT using the machine creds */
283 if (cell_flags(c
) & LWCELL_FLAG_GC_CELL
) {
284 ads_status
= ads_connect_gc(ads
);
286 /* Set up server affinity for normal cells and the client
289 if (!get_dc_name("", c
->dns_domain
, dc_name
, &dcip
)) {
290 nt_status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
291 BAIL_ON_NTSTATUS_ERROR(nt_status
);
294 ads_status
= ads_connect(ads
);
300 nt_status
= ads_ntstatus(ads_status
);
303 if (!NT_STATUS_IS_OK(nt_status
)) {
311 /********************************************************************
312 *******************************************************************/
314 NTSTATUS
cell_connect_dn(struct likewise_cell
**c
, const char *dn
)
316 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
317 struct likewise_cell
*new_cell
= NULL
;
318 char *dns_domain
= NULL
;
321 nt_status
= NT_STATUS_INVALID_PARAMETER
;
322 BAIL_ON_NTSTATUS_ERROR(nt_status
);
325 if ((new_cell
= cell_new()) == NULL
) {
326 nt_status
= NT_STATUS_NO_MEMORY
;
327 BAIL_ON_NTSTATUS_ERROR(nt_status
);
330 /* Set the DNS domain, dn, etc ... and add it to the list */
332 dns_domain
= cell_dn_to_dns(dn
);
333 cell_set_dns_domain(new_cell
, dns_domain
);
334 SAFE_FREE(dns_domain
);
336 cell_set_dn(new_cell
, dn
);
338 nt_status
= cell_connect(new_cell
);
339 BAIL_ON_NTSTATUS_ERROR(nt_status
);
344 if (!NT_STATUS_IS_OK(nt_status
)) {
345 DEBUG(1,("LWI: Failled to connect to cell \"%s\" (%s)\n",
346 dn
? dn
: "NULL", nt_errstr(nt_status
)));
347 talloc_destroy(new_cell
);
354 /********************************************************************
355 *******************************************************************/
357 #define MAX_SEARCH_COUNT 2
359 ADS_STATUS
cell_do_search(struct likewise_cell
*c
,
360 const char *search_base
,
366 int search_count
= 0;
370 /* check for a NULL connection */
373 nt_status
= cell_connect(c
);
374 if (!NT_STATUS_IS_OK(nt_status
)) {
375 status
= ADS_ERROR_NT(nt_status
);
380 DEBUG(10, ("cell_do_search: Base = %s, Filter = %s, Scope = %d, GC = %s\n",
381 search_base
, expr
, scope
,
382 c
->conn
->server
.gc
? "yes" : "no"));
384 /* we try multiple times in case the ADS_STRUCT is bad
385 and we need to reconnect */
387 while (search_count
< MAX_SEARCH_COUNT
) {
389 status
= ads_do_search(c
->conn
, search_base
,
390 scope
, expr
, attrs
, msg
);
391 if (ADS_ERR_OK(status
)) {
392 if (DEBUGLEVEL
>= 10) {
393 LDAPMessage
*e
= NULL
;
395 int n
= ads_count_replies(c
->conn
, *msg
);
397 DEBUG(10,("cell_do_search: Located %d entries\n", n
));
399 for (e
=ads_first_entry(c
->conn
, *msg
);
401 e
= ads_next_entry(c
->conn
, e
))
403 char *dn
= ads_get_dn(c
->conn
, NULL
, e
);
405 DEBUGADD(10,(" dn: %s\n", dn
? dn
: "<NULL>"));
414 DEBUG(5, ("cell_do_search: search[%d] failed (%s)\n",
415 search_count
, ads_errstr(status
)));
419 /* Houston, we have a problem */
421 if (status
.error_type
== ENUM_ADS_ERROR_LDAP
) {
422 switch (status
.err
.rc
) {
423 case LDAP_TIMELIMIT_EXCEEDED
:
425 case -1: /* we get this error if we cannot contact
427 nt_status
= cell_connect(c
);
428 if (!NT_STATUS_IS_OK(nt_status
)) {
429 status
= ADS_ERROR_NT(nt_status
);
434 /* we're all done here */
440 DEBUG(5, ("cell_do_search: exceeded maximum search count!\n"));
442 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);