s3 WHATSNEW: Mention the changes to net
[Samba.git] / source3 / winbindd / idmap_adex / likewise_cell.c
blobd666d8c01a50a8c958bb5e45ccddc8d5736acb64
1 /*
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.
21 #include "includes.h"
22 #include "idmap_adex.h"
24 #undef DBGC_CLASS
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)
35 return _lw_cell_list;
39 /**********************************************************************
40 *********************************************************************/
42 void cell_destroy(struct likewise_cell *c)
44 if (!c)
45 return;
47 if (c->conn)
48 ads_destroy(&c->conn);
50 talloc_destroy(c);
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;
61 while (p) {
62 struct likewise_cell *q = p->next;
64 cell_destroy(p);
66 p = q;
69 _lw_cell_list = NULL;
71 return;
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);
85 if (!c) {
86 DEBUG(0,("cell_new: memory allocation failure!\n"));
87 return NULL;
90 return c;
93 /**********************************************************************
94 Add a new cell structure to the list
95 *********************************************************************/
97 bool cell_list_add(struct likewise_cell * cell)
99 if (!cell) {
100 return false;
103 /* Always add to the end */
105 DLIST_ADD_END(_lw_cell_list, cell, struct likewise_cell *);
107 return true;
110 /**********************************************************************
111 Add a new cell structure to the list
112 *********************************************************************/
114 bool cell_list_remove(struct likewise_cell * cell)
116 if (!cell) {
117 return false;
120 /* Remove and drop the cell structure */
122 DLIST_REMOVE(_lw_cell_list, cell);
123 talloc_destroy(cell);
125 return true;
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)
143 c->conn = ads;
146 /**********************************************************************
147 *********************************************************************/
149 void cell_set_flags(struct likewise_cell *c, uint32_t flags)
151 c->flags |= flags;
154 /**********************************************************************
155 *********************************************************************/
157 void cell_clear_flags(struct likewise_cell *c, uint32_t flags)
159 c->flags &= ~flags;
162 /**********************************************************************
163 Set the Cell's DN
164 *********************************************************************/
166 void cell_set_dn(struct likewise_cell *c, const char *dn)
168 if ( c->dn) {
169 talloc_free(c->dn);
170 c->dn = NULL;
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);
185 * Query Routines
188 /**********************************************************************
189 *********************************************************************/
191 const char* cell_search_base(struct likewise_cell *c)
193 if (!c)
194 return NULL;
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)
214 if (!c)
215 return 0;
217 return c->flags;
220 /**********************************************************************
221 *********************************************************************/
223 const char *cell_dns_domain(struct likewise_cell *c)
225 if (!c)
226 return NULL;
228 return c->dns_domain;
231 /**********************************************************************
232 *********************************************************************/
234 ADS_STRUCT *cell_connection(struct likewise_cell *c)
236 if (!c)
237 return NULL;
239 return c->conn;
243 * Connection functions
246 /********************************************************************
247 *******************************************************************/
249 NTSTATUS cell_connect(struct likewise_cell *c)
251 ADS_STRUCT *ads = NULL;
252 ADS_STATUS ads_status;
253 fstring dc_name;
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 */
266 if (c->conn) {
267 ads_destroy(&c->conn);
268 c->conn = NULL;
271 /* now setup the new connection */
273 ads = ads_init(c->dns_domain, NULL, NULL);
274 BAIL_ON_PTR_ERROR(ads, nt_status);
276 ads->auth.password =
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);
285 } else {
286 /* Set up server affinity for normal cells and the client
287 site name cache */
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);
298 c->conn = ads;
300 nt_status = ads_ntstatus(ads_status);
302 done:
303 if (!NT_STATUS_IS_OK(nt_status)) {
304 ads_destroy(&ads);
305 c->conn = NULL;
308 return 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;
320 if (*c || !dn) {
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);
341 *c = new_cell;
343 done:
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);
350 return nt_status;
354 /********************************************************************
355 *******************************************************************/
357 #define MAX_SEARCH_COUNT 2
359 ADS_STATUS cell_do_search(struct likewise_cell *c,
360 const char *search_base,
361 int scope,
362 const char *expr,
363 const char **attrs,
364 LDAPMessage ** msg)
366 int search_count = 0;
367 ADS_STATUS status;
368 NTSTATUS nt_status;
370 /* check for a NULL connection */
372 if (!c->conn) {
373 nt_status = cell_connect(c);
374 if (!NT_STATUS_IS_OK(nt_status)) {
375 status = ADS_ERROR_NT(nt_status);
376 return 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) {
388 *msg = NULL;
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);
400 e!=NULL;
401 e = ads_next_entry(c->conn, e))
403 char *dn = ads_get_dn(c->conn, talloc_tos(), e);
405 DEBUGADD(10,(" dn: %s\n", dn ? dn : "<NULL>"));
406 TALLOC_FREE(dn);
410 return status;
414 DEBUG(5, ("cell_do_search: search[%d] failed (%s)\n",
415 search_count, ads_errstr(status)));
417 search_count++;
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:
424 case LDAP_TIMEOUT:
425 case -1: /* we get this error if we cannot contact
426 the LDAP server */
427 nt_status = cell_connect(c);
428 if (!NT_STATUS_IS_OK(nt_status)) {
429 status = ADS_ERROR_NT(nt_status);
430 return status;
432 break;
433 default:
434 /* we're all done here */
435 return status;
440 DEBUG(5, ("cell_do_search: exceeded maximum search count!\n"));
442 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);