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.
23 #include "idmap_adex.h"
26 #define DBGC_CLASS DBGC_IDMAP
28 static struct likewise_cell *_lw_cell_list = NULL;
30 /**********************************************************************
31 Return the current HEAD of the list
32 *********************************************************************/
34 struct likewise_cell *cell_list_head(void)
40 /**********************************************************************
41 *********************************************************************/
43 void cell_destroy(struct likewise_cell *c)
49 ads_destroy(&c->conn);
54 /**********************************************************************
55 Free all cell entries and reset the list head to NULL
56 *********************************************************************/
58 void cell_list_destroy(void)
60 struct likewise_cell *p = _lw_cell_list;
63 struct likewise_cell *q = p->next;
75 /**********************************************************************
76 Add a new cell structure to the list
77 *********************************************************************/
79 struct likewise_cell* cell_new(void)
81 struct likewise_cell *c;
83 /* Each cell struct is a TALLOC_CTX* */
85 c = TALLOC_ZERO_P(NULL, struct likewise_cell);
87 DEBUG(0,("cell_new: memory allocation failure!\n"));
94 /**********************************************************************
95 Add a new cell structure to the list
96 *********************************************************************/
98 bool cell_list_add(struct likewise_cell * cell)
104 /* Always add to the end */
106 DLIST_ADD_END(_lw_cell_list, cell, struct likewise_cell *);
111 /**********************************************************************
112 Add a new cell structure to the list
113 *********************************************************************/
115 bool cell_list_remove(struct likewise_cell * cell)
121 /* Remove and drop the cell structure */
123 DLIST_REMOVE(_lw_cell_list, cell);
124 talloc_destroy(cell);
129 /**********************************************************************
130 Set the containing DNS domain for a cell
131 *********************************************************************/
133 void cell_set_dns_domain(struct likewise_cell *c, const char *dns_domain)
135 c->dns_domain = talloc_strdup(c, dns_domain);
138 /**********************************************************************
139 Set ADS connection for a cell
140 *********************************************************************/
142 void cell_set_connection(struct likewise_cell *c, ADS_STRUCT *ads)
147 /**********************************************************************
148 *********************************************************************/
150 void cell_set_flags(struct likewise_cell *c, uint32_t flags)
155 /**********************************************************************
156 *********************************************************************/
158 void cell_clear_flags(struct likewise_cell *c, uint32_t flags)
163 /**********************************************************************
165 *********************************************************************/
167 void cell_set_dn(struct likewise_cell *c, const char *dn)
174 c->dn = talloc_strdup(c, dn);
177 /**********************************************************************
178 *********************************************************************/
180 void cell_set_domain_sid(struct likewise_cell *c, struct dom_sid *sid)
182 sid_copy(&c->domain_sid, sid);
189 /**********************************************************************
190 *********************************************************************/
192 const char* cell_search_base(struct likewise_cell *c)
197 return talloc_asprintf(c, "cn=%s,%s", ADEX_CELL_RDN, c->dn);
200 /**********************************************************************
201 *********************************************************************/
203 bool cell_search_forest(struct likewise_cell *c)
205 uint32_t test_flags = LWCELL_FLAG_SEARCH_FOREST;
207 return ((c->flags & test_flags) == test_flags);
210 /**********************************************************************
211 *********************************************************************/
213 uint32_t cell_flags(struct likewise_cell *c)
221 /**********************************************************************
222 *********************************************************************/
224 const char *cell_dns_domain(struct likewise_cell *c)
229 return c->dns_domain;
232 /**********************************************************************
233 *********************************************************************/
235 ADS_STRUCT *cell_connection(struct likewise_cell *c)
244 * Connection functions
247 /********************************************************************
248 *******************************************************************/
250 NTSTATUS cell_connect(struct likewise_cell *c)
252 ADS_STRUCT *ads = NULL;
253 ADS_STATUS ads_status;
255 struct sockaddr_storage dcip;
256 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
258 /* have to at least have the AD domain name */
260 if (!c->dns_domain) {
261 nt_status = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
262 BAIL_ON_NTSTATUS_ERROR(nt_status);
265 /* clear out any old information */
268 ads_destroy(&c->conn);
272 /* now setup the new connection */
274 ads = ads_init(c->dns_domain, NULL, NULL);
275 BAIL_ON_PTR_ERROR(ads, nt_status);
278 secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
279 ads->auth.realm = SMB_STRDUP(lp_realm());
281 /* Make the connection. We should already have an initial
282 TGT using the machine creds */
284 if (cell_flags(c) & LWCELL_FLAG_GC_CELL) {
285 ads_status = ads_connect_gc(ads);
287 /* Set up server affinity for normal cells and the client
290 if (!get_dc_name("", c->dns_domain, dc_name, &dcip)) {
291 nt_status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
292 BAIL_ON_NTSTATUS_ERROR(nt_status);
295 ads_status = ads_connect(ads);
301 nt_status = ads_ntstatus(ads_status);
304 if (!NT_STATUS_IS_OK(nt_status)) {
312 /********************************************************************
313 *******************************************************************/
315 NTSTATUS cell_connect_dn(struct likewise_cell **c, const char *dn)
317 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
318 struct likewise_cell *new_cell = NULL;
319 char *dns_domain = NULL;
322 nt_status = NT_STATUS_INVALID_PARAMETER;
323 BAIL_ON_NTSTATUS_ERROR(nt_status);
326 if ((new_cell = cell_new()) == NULL) {
327 nt_status = NT_STATUS_NO_MEMORY;
328 BAIL_ON_NTSTATUS_ERROR(nt_status);
331 /* Set the DNS domain, dn, etc ... and add it to the list */
333 dns_domain = cell_dn_to_dns(dn);
334 cell_set_dns_domain(new_cell, dns_domain);
335 SAFE_FREE(dns_domain);
337 cell_set_dn(new_cell, dn);
339 nt_status = cell_connect(new_cell);
340 BAIL_ON_NTSTATUS_ERROR(nt_status);
345 if (!NT_STATUS_IS_OK(nt_status)) {
346 DEBUG(1,("LWI: Failled to connect to cell \"%s\" (%s)\n",
347 dn ? dn : "NULL", nt_errstr(nt_status)));
348 talloc_destroy(new_cell);
355 /********************************************************************
356 *******************************************************************/
358 #define MAX_SEARCH_COUNT 2
360 ADS_STATUS cell_do_search(struct likewise_cell *c,
361 const char *search_base,
367 int search_count = 0;
371 /* check for a NULL connection */
374 nt_status = cell_connect(c);
375 if (!NT_STATUS_IS_OK(nt_status)) {
376 status = ADS_ERROR_NT(nt_status);
381 DEBUG(10, ("cell_do_search: Base = %s, Filter = %s, Scope = %d, GC = %s\n",
382 search_base, expr, scope,
383 c->conn->server.gc ? "yes" : "no"));
385 /* we try multiple times in case the ADS_STRUCT is bad
386 and we need to reconnect */
388 while (search_count < MAX_SEARCH_COUNT) {
390 status = ads_do_search(c->conn, search_base,
391 scope, expr, attrs, msg);
392 if (ADS_ERR_OK(status)) {
393 if (DEBUGLEVEL >= 10) {
394 LDAPMessage *e = NULL;
396 int n = ads_count_replies(c->conn, *msg);
398 DEBUG(10,("cell_do_search: Located %d entries\n", n));
400 for (e=ads_first_entry(c->conn, *msg);
402 e = ads_next_entry(c->conn, e))
404 char *dn = ads_get_dn(c->conn, talloc_tos(), e);
406 DEBUGADD(10,(" dn: %s\n", dn ? dn : "<NULL>"));
415 DEBUG(5, ("cell_do_search: search[%d] failed (%s)\n",
416 search_count, ads_errstr(status)));
420 /* Houston, we have a problem */
422 if (status.error_type == ENUM_ADS_ERROR_LDAP) {
423 switch (status.err.rc) {
424 case LDAP_TIMELIMIT_EXCEEDED:
426 case -1: /* we get this error if we cannot contact
428 nt_status = cell_connect(c);
429 if (!NT_STATUS_IS_OK(nt_status)) {
430 status = ADS_ERROR_NT(nt_status);
435 /* we're all done here */
441 DEBUG(5, ("cell_do_search: exceeded maximum search count!\n"));
443 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);