3e529b427a4e830ece3f391c8c9ecda82a0398f3
[metze/samba/wip.git] / source3 / winbindd / idmap_adex / likewise_cell.c
1 /*
2  * idmap_adex: Support for AD Forests
3  *
4  * Copyright (C) Gerald (Jerry) Carter 2006-2008
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "includes.h"
22 #include "ads.h"
23 #include "idmap_adex.h"
24 #include "secrets.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_IDMAP
28
29 static struct likewise_cell *_lw_cell_list = NULL;
30
31 /**********************************************************************
32  Return the current HEAD of the list
33  *********************************************************************/
34
35  struct likewise_cell *cell_list_head(void)
36 {
37         return _lw_cell_list;
38 }
39
40
41 /**********************************************************************
42  *********************************************************************/
43
44  void cell_destroy(struct likewise_cell *c)
45 {
46         if (!c)
47                 return;
48
49         if (c->conn)
50                 ads_destroy(&c->conn);
51
52         talloc_destroy(c);
53 }
54
55 /**********************************************************************
56  Free all cell entries and reset the list head to NULL
57  *********************************************************************/
58
59  void cell_list_destroy(void)
60 {
61         struct likewise_cell *p = _lw_cell_list;
62
63         while (p) {
64                 struct likewise_cell *q = p->next;
65
66                 cell_destroy(p);
67
68                 p = q;
69         }
70
71         _lw_cell_list = NULL;
72
73         return;
74 }
75
76 /**********************************************************************
77  Add a new cell structure to the list
78  *********************************************************************/
79
80  struct likewise_cell* cell_new(void)
81 {
82         struct likewise_cell *c;
83
84         /* Each cell struct is a TALLOC_CTX* */
85
86         c = TALLOC_ZERO_P(NULL, struct likewise_cell);
87         if (!c) {
88                 DEBUG(0,("cell_new: memory allocation failure!\n"));
89                 return NULL;
90         }
91
92         return c;
93 }
94
95 /**********************************************************************
96  Add a new cell structure to the list
97  *********************************************************************/
98
99  bool cell_list_add(struct likewise_cell * cell)
100 {
101         if (!cell) {
102                 return false;
103         }
104
105         /* Always add to the end */
106
107         DLIST_ADD_END(_lw_cell_list, cell, struct likewise_cell *);
108
109         return true;
110 }
111
112 /**********************************************************************
113  Add a new cell structure to the list
114  *********************************************************************/
115
116  bool cell_list_remove(struct likewise_cell * cell)
117 {
118         if (!cell) {
119                 return false;
120         }
121
122         /* Remove and drop the cell structure */
123
124         DLIST_REMOVE(_lw_cell_list, cell);
125         talloc_destroy(cell);
126
127         return true;
128 }
129
130 /**********************************************************************
131  Set the containing DNS domain for a cell
132  *********************************************************************/
133
134  void cell_set_dns_domain(struct likewise_cell *c, const char *dns_domain)
135 {
136         c->dns_domain = talloc_strdup(c, dns_domain);
137 }
138
139 /**********************************************************************
140  Set ADS connection for a cell
141  *********************************************************************/
142
143  void cell_set_connection(struct likewise_cell *c, ADS_STRUCT *ads)
144 {
145         c->conn = ads;
146 }
147
148 /**********************************************************************
149  *********************************************************************/
150
151  void cell_set_flags(struct likewise_cell *c, uint32_t flags)
152 {
153         c->flags |= flags;
154 }
155
156 /**********************************************************************
157  *********************************************************************/
158
159  void cell_clear_flags(struct likewise_cell *c, uint32_t flags)
160 {
161         c->flags &= ~flags;
162 }
163
164 /**********************************************************************
165  Set the Cell's DN
166  *********************************************************************/
167
168  void cell_set_dn(struct likewise_cell *c, const char *dn)
169 {
170         if ( c->dn) {
171                 talloc_free(c->dn);
172                 c->dn = NULL;
173         }
174
175         c->dn = talloc_strdup(c, dn);
176 }
177
178 /**********************************************************************
179  *********************************************************************/
180
181  void cell_set_domain_sid(struct likewise_cell *c, struct dom_sid *sid)
182 {
183         sid_copy(&c->domain_sid, sid);
184 }
185
186 /*
187  * Query Routines
188  */
189
190 /**********************************************************************
191  *********************************************************************/
192
193  const char* cell_search_base(struct likewise_cell *c)
194 {
195         if (!c)
196                 return NULL;
197
198         return talloc_asprintf(c, "cn=%s,%s", ADEX_CELL_RDN, c->dn);
199 }
200
201 /**********************************************************************
202  *********************************************************************/
203
204  bool cell_search_forest(struct likewise_cell *c)
205 {
206         uint32_t test_flags = LWCELL_FLAG_SEARCH_FOREST;
207
208         return ((c->flags & test_flags) == test_flags);
209 }
210
211 /**********************************************************************
212  *********************************************************************/
213
214  uint32_t cell_flags(struct likewise_cell *c)
215 {
216         if (!c)
217                 return 0;
218
219         return c->flags;
220 }
221
222 /**********************************************************************
223  *********************************************************************/
224
225  const char *cell_dns_domain(struct likewise_cell *c)
226 {
227         if (!c)
228                 return NULL;
229
230         return c->dns_domain;
231 }
232
233 /**********************************************************************
234  *********************************************************************/
235
236  ADS_STRUCT *cell_connection(struct likewise_cell *c)
237 {
238         if (!c)
239                 return NULL;
240
241         return c->conn;
242 }
243
244 /*
245  * Connection functions
246  */
247
248 /********************************************************************
249  *******************************************************************/
250
251  NTSTATUS cell_connect(struct likewise_cell *c)
252 {
253         ADS_STRUCT *ads = NULL;
254         ADS_STATUS ads_status;
255         fstring dc_name;
256         struct sockaddr_storage dcip;
257         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
258
259         /* have to at least have the AD domain name */
260
261         if (!c->dns_domain) {
262                 nt_status = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
263                 BAIL_ON_NTSTATUS_ERROR(nt_status);
264         }
265
266         /* clear out any old information */
267
268         if (c->conn) {
269                 ads_destroy(&c->conn);
270                 c->conn = NULL;
271         }
272
273         /* now setup the new connection */
274
275         ads = ads_init(c->dns_domain, NULL, NULL);
276         BAIL_ON_PTR_ERROR(ads, nt_status);
277
278         ads->auth.password =
279             secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
280         ads->auth.realm = SMB_STRDUP(lp_realm());
281
282         /* Make the connection.  We should already have an initial
283            TGT using the machine creds */
284
285         if (cell_flags(c) & LWCELL_FLAG_GC_CELL) {
286                 ads_status = ads_connect_gc(ads);
287         } else {
288           /* Set up server affinity for normal cells and the client
289              site name cache */
290
291           if (!get_dc_name("", c->dns_domain, dc_name, &dcip)) {
292             nt_status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
293             BAIL_ON_NTSTATUS_ERROR(nt_status);
294           }
295
296           ads_status = ads_connect(ads);
297         }
298
299
300         c->conn = ads;
301
302         nt_status = ads_ntstatus(ads_status);
303
304 done:
305         if (!NT_STATUS_IS_OK(nt_status)) {
306                 ads_destroy(&ads);
307                 c->conn = NULL;
308         }
309
310         return nt_status;
311 }
312
313 /********************************************************************
314  *******************************************************************/
315
316  NTSTATUS cell_connect_dn(struct likewise_cell **c, const char *dn)
317 {
318         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
319         struct likewise_cell *new_cell = NULL;
320         char *dns_domain = NULL;
321
322         if (*c || !dn) {
323                 nt_status = NT_STATUS_INVALID_PARAMETER;
324                 BAIL_ON_NTSTATUS_ERROR(nt_status);
325         }
326
327         if ((new_cell = cell_new()) == NULL) {
328                 nt_status = NT_STATUS_NO_MEMORY;
329                 BAIL_ON_NTSTATUS_ERROR(nt_status);
330         }
331
332         /* Set the DNS domain, dn, etc ... and add it to the list */
333
334         dns_domain = cell_dn_to_dns(dn);
335         cell_set_dns_domain(new_cell, dns_domain);
336         SAFE_FREE(dns_domain);
337
338         cell_set_dn(new_cell, dn);
339
340         nt_status = cell_connect(new_cell);
341         BAIL_ON_NTSTATUS_ERROR(nt_status);
342
343         *c = new_cell;
344
345 done:
346         if (!NT_STATUS_IS_OK(nt_status)) {
347                 DEBUG(1,("LWI: Failled to connect to cell \"%s\" (%s)\n",
348                          dn ? dn : "NULL", nt_errstr(nt_status)));
349                 talloc_destroy(new_cell);
350         }
351
352         return nt_status;
353 }
354
355
356 /********************************************************************
357  *******************************************************************/
358
359 #define MAX_SEARCH_COUNT    2
360
361  ADS_STATUS cell_do_search(struct likewise_cell *c,
362                           const char *search_base,
363                           int scope,
364                           const char *expr,
365                           const char **attrs,
366                           LDAPMessage ** msg)
367 {
368         int search_count = 0;
369         ADS_STATUS status;
370         NTSTATUS nt_status;
371
372         /* check for a NULL connection */
373
374         if (!c->conn) {
375                 nt_status = cell_connect(c);
376                 if (!NT_STATUS_IS_OK(nt_status)) {
377                         status = ADS_ERROR_NT(nt_status);
378                         return status;
379                 }
380         }
381
382         DEBUG(10, ("cell_do_search: Base = %s,  Filter = %s, Scope = %d, GC = %s\n",
383                    search_base, expr, scope,
384                    c->conn->server.gc ? "yes" : "no"));
385
386         /* we try multiple times in case the ADS_STRUCT is bad
387            and we need to reconnect */
388
389         while (search_count < MAX_SEARCH_COUNT) {
390                 *msg = NULL;
391                 status = ads_do_search(c->conn, search_base,
392                                        scope, expr, attrs, msg);
393                 if (ADS_ERR_OK(status)) {
394                         if (DEBUGLEVEL >= 10) {
395                                 LDAPMessage *e = NULL;
396
397                                 int n = ads_count_replies(c->conn, *msg);
398
399                                 DEBUG(10,("cell_do_search: Located %d entries\n", n));
400
401                                 for (e=ads_first_entry(c->conn, *msg);
402                                      e!=NULL;
403                                      e = ads_next_entry(c->conn, e))
404                                 {
405                                         char *dn = ads_get_dn(c->conn, talloc_tos(), e);
406
407                                         DEBUGADD(10,("   dn: %s\n", dn ? dn : "<NULL>"));
408                                         TALLOC_FREE(dn);
409                                 }
410                         }
411
412                         return status;
413                 }
414
415
416                 DEBUG(5, ("cell_do_search: search[%d] failed (%s)\n",
417                           search_count, ads_errstr(status)));
418
419                 search_count++;
420
421                 /* Houston, we have a problem */
422
423                 if (status.error_type == ENUM_ADS_ERROR_LDAP) {
424                         switch (status.err.rc) {
425                         case LDAP_TIMELIMIT_EXCEEDED:
426                         case LDAP_TIMEOUT:
427                         case -1:        /* we get this error if we cannot contact
428                                            the LDAP server */
429                                 nt_status = cell_connect(c);
430                                 if (!NT_STATUS_IS_OK(nt_status)) {
431                                         status = ADS_ERROR_NT(nt_status);
432                                         return status;
433                                 }
434                                 break;
435                         default:
436                                 /* we're all done here */
437                                 return status;
438                         }
439                 }
440         }
441
442         DEBUG(5, ("cell_do_search: exceeded maximum search count!\n"));
443
444         return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
445 }