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.
24 #include "idmap_adex.h"
28 #define DBGC_CLASS DBGC_IDMAP
30 static struct likewise_cell *_lw_cell_list = NULL;
32 /**********************************************************************
33 Return the current HEAD of the list
34 *********************************************************************/
36 struct likewise_cell *cell_list_head(void)
42 /**********************************************************************
43 *********************************************************************/
45 void cell_destroy(struct likewise_cell *c)
51 ads_destroy(&c->conn);
56 /**********************************************************************
57 Free all cell entries and reset the list head to NULL
58 *********************************************************************/
60 void cell_list_destroy(void)
62 struct likewise_cell *p = _lw_cell_list;
65 struct likewise_cell *q = p->next;
77 /**********************************************************************
78 Add a new cell structure to the list
79 *********************************************************************/
81 struct likewise_cell* cell_new(void)
83 struct likewise_cell *c;
85 /* Each cell struct is a TALLOC_CTX* */
87 c = TALLOC_ZERO_P(NULL, struct likewise_cell);
89 DEBUG(0,("cell_new: memory allocation failure!\n"));
96 /**********************************************************************
97 Add a new cell structure to the list
98 *********************************************************************/
100 bool cell_list_add(struct likewise_cell * cell)
106 /* Always add to the end */
108 DLIST_ADD_END(_lw_cell_list, cell, struct likewise_cell *);
113 /**********************************************************************
114 Add a new cell structure to the list
115 *********************************************************************/
117 bool cell_list_remove(struct likewise_cell * cell)
123 /* Remove and drop the cell structure */
125 DLIST_REMOVE(_lw_cell_list, cell);
126 talloc_destroy(cell);
131 /**********************************************************************
132 Set the containing DNS domain for a cell
133 *********************************************************************/
135 void cell_set_dns_domain(struct likewise_cell *c, const char *dns_domain)
137 c->dns_domain = talloc_strdup(c, dns_domain);
140 /**********************************************************************
141 Set ADS connection for a cell
142 *********************************************************************/
144 void cell_set_connection(struct likewise_cell *c, ADS_STRUCT *ads)
149 /**********************************************************************
150 *********************************************************************/
152 void cell_set_flags(struct likewise_cell *c, uint32_t flags)
157 /**********************************************************************
158 *********************************************************************/
160 void cell_clear_flags(struct likewise_cell *c, uint32_t flags)
165 /**********************************************************************
167 *********************************************************************/
169 void cell_set_dn(struct likewise_cell *c, const char *dn)
176 c->dn = talloc_strdup(c, dn);
179 /**********************************************************************
180 *********************************************************************/
182 void cell_set_domain_sid(struct likewise_cell *c, struct dom_sid *sid)
184 sid_copy(&c->domain_sid, sid);
191 /**********************************************************************
192 *********************************************************************/
194 const char* cell_search_base(struct likewise_cell *c)
199 return talloc_asprintf(c, "cn=%s,%s", ADEX_CELL_RDN, c->dn);
202 /**********************************************************************
203 *********************************************************************/
205 bool cell_search_forest(struct likewise_cell *c)
207 uint32_t test_flags = LWCELL_FLAG_SEARCH_FOREST;
209 return ((c->flags & test_flags) == test_flags);
212 /**********************************************************************
213 *********************************************************************/
215 uint32_t cell_flags(struct likewise_cell *c)
223 /**********************************************************************
224 *********************************************************************/
226 const char *cell_dns_domain(struct likewise_cell *c)
231 return c->dns_domain;
234 /**********************************************************************
235 *********************************************************************/
237 ADS_STRUCT *cell_connection(struct likewise_cell *c)
246 * Connection functions
249 /********************************************************************
250 *******************************************************************/
252 NTSTATUS cell_connect(struct likewise_cell *c)
254 ADS_STRUCT *ads = NULL;
255 ADS_STATUS ads_status;
257 struct sockaddr_storage dcip;
258 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
260 /* have to at least have the AD domain name */
262 if (!c->dns_domain) {
263 nt_status = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
264 BAIL_ON_NTSTATUS_ERROR(nt_status);
267 /* clear out any old information */
270 ads_destroy(&c->conn);
274 /* now setup the new connection */
276 ads = ads_init(c->dns_domain, NULL, NULL);
277 BAIL_ON_PTR_ERROR(ads, nt_status);
280 secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
281 ads->auth.realm = SMB_STRDUP(lp_realm());
283 /* Make the connection. We should already have an initial
284 TGT using the machine creds */
286 if (cell_flags(c) & LWCELL_FLAG_GC_CELL) {
287 ads_status = ads_connect_gc(ads);
289 /* Set up server affinity for normal cells and the client
292 if (!get_dc_name("", c->dns_domain, dc_name, &dcip)) {
293 nt_status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
294 BAIL_ON_NTSTATUS_ERROR(nt_status);
297 ads_status = ads_connect(ads);
303 nt_status = ads_ntstatus(ads_status);
306 if (!NT_STATUS_IS_OK(nt_status)) {
314 /********************************************************************
315 *******************************************************************/
317 NTSTATUS cell_connect_dn(struct likewise_cell **c, const char *dn)
319 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
320 struct likewise_cell *new_cell = NULL;
321 char *dns_domain = NULL;
324 nt_status = NT_STATUS_INVALID_PARAMETER;
325 BAIL_ON_NTSTATUS_ERROR(nt_status);
328 if ((new_cell = cell_new()) == NULL) {
329 nt_status = NT_STATUS_NO_MEMORY;
330 BAIL_ON_NTSTATUS_ERROR(nt_status);
333 /* Set the DNS domain, dn, etc ... and add it to the list */
335 dns_domain = cell_dn_to_dns(dn);
336 cell_set_dns_domain(new_cell, dns_domain);
337 SAFE_FREE(dns_domain);
339 cell_set_dn(new_cell, dn);
341 nt_status = cell_connect(new_cell);
342 BAIL_ON_NTSTATUS_ERROR(nt_status);
347 if (!NT_STATUS_IS_OK(nt_status)) {
348 DEBUG(1,("LWI: Failled to connect to cell \"%s\" (%s)\n",
349 dn ? dn : "NULL", nt_errstr(nt_status)));
350 talloc_destroy(new_cell);
357 /********************************************************************
358 *******************************************************************/
360 #define MAX_SEARCH_COUNT 2
362 ADS_STATUS cell_do_search(struct likewise_cell *c,
363 const char *search_base,
369 int search_count = 0;
373 /* check for a NULL connection */
376 nt_status = cell_connect(c);
377 if (!NT_STATUS_IS_OK(nt_status)) {
378 status = ADS_ERROR_NT(nt_status);
383 DEBUG(10, ("cell_do_search: Base = %s, Filter = %s, Scope = %d, GC = %s\n",
384 search_base, expr, scope,
385 c->conn->server.gc ? "yes" : "no"));
387 /* we try multiple times in case the ADS_STRUCT is bad
388 and we need to reconnect */
390 while (search_count < MAX_SEARCH_COUNT) {
392 status = ads_do_search(c->conn, search_base,
393 scope, expr, attrs, msg);
394 if (ADS_ERR_OK(status)) {
395 if (DEBUGLEVEL >= 10) {
396 LDAPMessage *e = NULL;
398 int n = ads_count_replies(c->conn, *msg);
400 DEBUG(10,("cell_do_search: Located %d entries\n", n));
402 for (e=ads_first_entry(c->conn, *msg);
404 e = ads_next_entry(c->conn, e))
406 char *dn = ads_get_dn(c->conn, talloc_tos(), e);
408 DEBUGADD(10,(" dn: %s\n", dn ? dn : "<NULL>"));
417 DEBUG(5, ("cell_do_search: search[%d] failed (%s)\n",
418 search_count, ads_errstr(status)));
422 /* Houston, we have a problem */
424 if (status.error_type == ENUM_ADS_ERROR_LDAP) {
425 switch (status.err.rc) {
426 case LDAP_TIMELIMIT_EXCEEDED:
428 case -1: /* we get this error if we cannot contact
430 nt_status = cell_connect(c);
431 if (!NT_STATUS_IS_OK(nt_status)) {
432 status = ADS_ERROR_NT(nt_status);
437 /* we're all done here */
443 DEBUG(5, ("cell_do_search: exceeded maximum search count!\n"));
445 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);