c32bb9179d5e5c81e5bf588bd228ae477fc568c3
[samba.git] / source3 / winbindd / idmap_adex / cell_util.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 "../libds/common/flags.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_IDMAP
28
29 /**********************************************************************
30 **********************************************************************/
31
32  char *find_attr_string(char **list, size_t num_lines, const char *substr)
33 {
34         int i;
35         int cmplen = strlen(substr);
36
37         for (i = 0; i < num_lines; i++) {
38                 /* make sure to avoid substring matches like uid
39                    and uidNumber */
40                 if ((StrnCaseCmp(list[i], substr, cmplen) == 0) &&
41                     (list[i][cmplen] == '=')) {
42                         /* Don't return an empty string */
43                         if (list[i][cmplen + 1] != '\0')
44                                 return &(list[i][cmplen + 1]);
45
46                         return NULL;
47                 }
48         }
49
50         return NULL;
51 }
52
53 /**********************************************************************
54 **********************************************************************/
55
56  bool is_object_class(char **list, size_t num_lines, const char *substr)
57 {
58         int i;
59
60         for (i = 0; i < num_lines; i++) {
61                 if (strequal(list[i], substr)) {
62                         return true;
63                 }
64         }
65
66         return false;
67 }
68
69 /**********************************************************************
70  Find out about the cell (e.g. use2307Attrs, etc...)
71 **********************************************************************/
72
73  NTSTATUS cell_lookup_settings(struct likewise_cell * cell)
74 {
75         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
76
77         /* Parameter check */
78
79         if (!cell) {
80                 nt_status = NT_STATUS_INVALID_PARAMETER;
81                 BAIL_ON_NTSTATUS_ERROR(nt_status);
82         }
83
84         /* Only supporting Forest-wide, schema based searches */
85
86         cell_set_flags(cell, LWCELL_FLAG_USE_RFC2307_ATTRS);
87         cell_set_flags(cell, LWCELL_FLAG_SEARCH_FOREST);
88
89         cell->provider = &ccp_unified;
90
91         nt_status = NT_STATUS_OK;
92
93 done:
94         if (!NT_STATUS_IS_OK(nt_status)) {
95                 DEBUG(1,("LWI: Failed to obtain cell settings (%s)\n",
96                          nt_errstr(nt_status)));
97         }
98
99         return nt_status;
100 }
101
102
103 static NTSTATUS cell_lookup_forest(struct likewise_cell *c)
104 {
105         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
106         struct gc_info *gc = NULL;
107
108         if (!c) {
109                 return NT_STATUS_INVALID_PARAMETER;
110         }
111
112         if ((gc = TALLOC_ZERO_P(NULL, struct gc_info)) == NULL) {
113                 nt_status = NT_STATUS_NO_MEMORY;
114                 BAIL_ON_NTSTATUS_ERROR(nt_status);
115         }
116
117         /* Query the rootDSE for the forest root naming conect first.
118            Check that the a GC server for the forest has not already
119            been added */
120
121         nt_status = gc_find_forest_root(gc, cell_dns_domain(c));
122         BAIL_ON_NTSTATUS_ERROR(nt_status);
123
124         c->forest_name = talloc_strdup(c, gc->forest_name);
125         BAIL_ON_PTR_ERROR(c->forest_name, nt_status);
126
127 done:
128         if (gc) {
129                 talloc_free(gc);
130         }
131
132         return nt_status;
133 }
134
135 /**********************************************************************
136 **********************************************************************/
137
138  NTSTATUS cell_locate_membership(ADS_STRUCT * ads)
139 {
140         ADS_STATUS status;
141         char *domain_dn = ads_build_dn(lp_realm());
142         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
143         struct dom_sid sid;
144         struct likewise_cell *cell = NULL;
145
146         /* In the Likewise plugin, I had to support the concept of cells
147            based on the machine's membership in an OU.  However, now I'll
148            just assume our membership in the forest cell */
149
150         DEBUG(2, ("locate_cell_membership: Located membership "
151                   "in cell \"%s\"\n", domain_dn));
152
153         if ((cell = cell_new()) == NULL) {
154                 nt_status = NT_STATUS_NO_MEMORY;
155                 BAIL_ON_NTSTATUS_ERROR(nt_status);
156         }
157
158         status = ads_domain_sid(ads, &sid);
159         if (!ADS_ERR_OK(status)) {
160                 DEBUG(3,("locate_cell_membership: Failed to find "
161                          "domain SID for %s\n", domain_dn));
162         }
163
164         /* save the SID and search base for our domain */
165
166         cell_set_dns_domain(cell, lp_realm());
167         cell_set_connection(cell, ads);
168         cell_set_dn(cell, domain_dn);
169         cell_set_domain_sid(cell, &sid);
170
171         /* Now save our forest root */
172
173         cell_lookup_forest(cell);
174
175         /* Add the cell to the list */
176
177         if (!cell_list_add(cell)) {
178                 nt_status = NT_STATUS_INSUFFICIENT_RESOURCES;
179                 BAIL_ON_NTSTATUS_ERROR(nt_status);
180         }
181
182         /* Done! */
183         nt_status = NT_STATUS_OK;
184
185 done:
186         if (!NT_STATUS_IS_OK(nt_status)) {
187                 DEBUG(0,("LWI: Failed to locate cell membership (%s)\n",
188                          nt_errstr(nt_status)));
189         }
190
191         SAFE_FREE(domain_dn);
192
193         return nt_status;
194 }
195
196 /*********************************************************************
197  ********************************************************************/
198
199  int min_id_value(void)
200 {
201         int id_val;
202
203         id_val = lp_parm_int(-1, "lwidentity", "min_id_value", MIN_ID_VALUE);
204
205         /* Still don't let it go below 50 */
206
207         return MAX(50, id_val);
208 }
209
210 /********************************************************************
211  *******************************************************************/
212
213  char *cell_dn_to_dns(const char *dn)
214 {
215         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
216         char *domain = NULL;
217         char *dns_name = NULL;
218         const char *tmp_dn;
219         char *buffer = NULL;
220         TALLOC_CTX *frame = talloc_stackframe();
221
222         if (!dn || !*dn) {
223                 goto done;
224         }
225
226         tmp_dn = talloc_strdup(frame, dn);
227         BAIL_ON_PTR_ERROR(tmp_dn, nt_status);
228
229         while (next_token_talloc(frame, &tmp_dn, &buffer, ",")) {
230
231                 /* skip everything up the where DC=... begins */
232                 if (StrnCaseCmp(buffer, "DC=", 3) != 0)
233                         continue;
234
235                 if (!domain) {
236                         domain = talloc_strdup(frame, &buffer[3]);
237                 } else {
238                         domain = talloc_asprintf_append(domain, ".%s",
239                                                         &buffer[3]);
240                 }
241                 BAIL_ON_PTR_ERROR(domain, nt_status);
242         }
243
244         dns_name = SMB_STRDUP(domain);
245         BAIL_ON_PTR_ERROR(dns_name, nt_status);
246
247         nt_status = NT_STATUS_OK;
248
249 done:
250         PRINT_NTSTATUS_ERROR(nt_status, "cell_dn_to_dns", 1);
251
252         talloc_destroy(frame);
253
254         return dns_name;
255 }
256
257 /*********************************************************************
258  ********************************************************************/
259
260  NTSTATUS get_sid_type(ADS_STRUCT *ads,
261                        LDAPMessage *msg,
262                        enum lsa_SidType *type)
263 {
264         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
265         uint32_t atype;
266
267         if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype)) {
268                 nt_status = NT_STATUS_INVALID_USER_BUFFER;
269                 BAIL_ON_NTSTATUS_ERROR(nt_status);
270         }
271
272         switch (atype &0xF0000000) {
273         case ATYPE_SECURITY_GLOBAL_GROUP:
274                 *type = SID_NAME_DOM_GRP;
275                 break;
276         case ATYPE_SECURITY_LOCAL_GROUP:
277                 *type = SID_NAME_ALIAS;
278                 break;
279         case ATYPE_NORMAL_ACCOUNT:
280         case ATYPE_WORKSTATION_TRUST:
281         case ATYPE_INTERDOMAIN_TRUST:
282                 *type = SID_NAME_USER;
283                 break;
284         default:
285                 *type = SID_NAME_USE_NONE;
286                 nt_status = NT_STATUS_INVALID_ACCOUNT_NAME;
287                 BAIL_ON_NTSTATUS_ERROR(nt_status);
288         }
289
290         nt_status = NT_STATUS_OK;
291
292 done:
293         return nt_status;
294 }