1 | /*
|
---|
2 | Unix SMB/CIFS mplementation.
|
---|
3 | LDAP protocol helper functions for SAMBA
|
---|
4 | Copyright (C) Jean François Micouleau 1998
|
---|
5 | Copyright (C) Gerald Carter 2001-2003
|
---|
6 | Copyright (C) Shahms King 2001
|
---|
7 | Copyright (C) Andrew Bartlett 2002-2003
|
---|
8 | Copyright (C) Stefan (metze) Metzmacher 2002-2003
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify
|
---|
11 | it under the terms of the GNU General Public License as published by
|
---|
12 | the Free Software Foundation; either version 2 of the License, or
|
---|
13 | (at your option) any later version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | GNU General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU General Public License
|
---|
21 | along with this program; if not, write to the Free Software
|
---|
22 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 |
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "includes.h"
|
---|
27 | #include "smbldap.h"
|
---|
28 |
|
---|
29 | /**********************************************************************
|
---|
30 | Add the account-policies below the sambaDomain object to LDAP,
|
---|
31 | *********************************************************************/
|
---|
32 |
|
---|
33 | static NTSTATUS add_new_domain_account_policies(struct smbldap_state *ldap_state,
|
---|
34 | const char *domain_name)
|
---|
35 | {
|
---|
36 | NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
|
---|
37 | int i, rc;
|
---|
38 | uint32 policy_default;
|
---|
39 | const char *policy_attr = NULL;
|
---|
40 | pstring dn;
|
---|
41 | LDAPMod **mods = NULL;
|
---|
42 | char *escape_domain_name;
|
---|
43 |
|
---|
44 | DEBUG(3,("add_new_domain_account_policies: Adding new account policies for domain\n"));
|
---|
45 |
|
---|
46 | escape_domain_name = escape_rdn_val_string_alloc(domain_name);
|
---|
47 | if (!escape_domain_name) {
|
---|
48 | DEBUG(0, ("Out of memory!\n"));
|
---|
49 | return NT_STATUS_NO_MEMORY;
|
---|
50 | }
|
---|
51 |
|
---|
52 | pstr_sprintf(dn, "%s=%s,%s",
|
---|
53 | get_attr_key2string(dominfo_attr_list, LDAP_ATTR_DOMAIN),
|
---|
54 | escape_domain_name, lp_ldap_suffix());
|
---|
55 |
|
---|
56 | SAFE_FREE(escape_domain_name);
|
---|
57 |
|
---|
58 | for (i=1; decode_account_policy_name(i) != NULL; i++) {
|
---|
59 |
|
---|
60 | pstring val;
|
---|
61 |
|
---|
62 | policy_attr = get_account_policy_attr(i);
|
---|
63 | if (!policy_attr) {
|
---|
64 | DEBUG(0,("add_new_domain_account_policies: ops. no policy!\n"));
|
---|
65 | continue;
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (!account_policy_get_default(i, &policy_default)) {
|
---|
69 | DEBUG(0,("add_new_domain_account_policies: failed to get default account policy\n"));
|
---|
70 | return ntstatus;
|
---|
71 | }
|
---|
72 |
|
---|
73 | DEBUG(10,("add_new_domain_account_policies: adding \"%s\" with value: %d\n", policy_attr, policy_default));
|
---|
74 |
|
---|
75 | pstr_sprintf(val, "%d", policy_default);
|
---|
76 |
|
---|
77 | smbldap_set_mod( &mods, LDAP_MOD_REPLACE, policy_attr, val);
|
---|
78 |
|
---|
79 | rc = smbldap_modify(ldap_state, dn, mods);
|
---|
80 |
|
---|
81 | if (rc!=LDAP_SUCCESS) {
|
---|
82 | char *ld_error = NULL;
|
---|
83 | ldap_get_option(ldap_state->ldap_struct, LDAP_OPT_ERROR_STRING, &ld_error);
|
---|
84 | DEBUG(1,("add_new_domain_account_policies: failed to add account policies to dn= %s with: %s\n\t%s\n",
|
---|
85 | dn, ldap_err2string(rc),
|
---|
86 | ld_error ? ld_error : "unknown"));
|
---|
87 | SAFE_FREE(ld_error);
|
---|
88 | ldap_mods_free(mods, True);
|
---|
89 | return ntstatus;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | ldap_mods_free(mods, True);
|
---|
94 |
|
---|
95 | return NT_STATUS_OK;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**********************************************************************
|
---|
99 | Add the sambaDomain to LDAP, so we don't have to search for this stuff
|
---|
100 | again. This is a once-add operation for now.
|
---|
101 |
|
---|
102 | TODO: Add other attributes, and allow modification.
|
---|
103 | *********************************************************************/
|
---|
104 |
|
---|
105 | static NTSTATUS add_new_domain_info(struct smbldap_state *ldap_state,
|
---|
106 | const char *domain_name)
|
---|
107 | {
|
---|
108 | fstring sid_string;
|
---|
109 | fstring algorithmic_rid_base_string;
|
---|
110 | pstring filter, dn;
|
---|
111 | LDAPMod **mods = NULL;
|
---|
112 | int rc;
|
---|
113 | LDAPMessage *result = NULL;
|
---|
114 | int num_result;
|
---|
115 | const char **attr_list;
|
---|
116 | char *escape_domain_name;
|
---|
117 |
|
---|
118 | /* escape for filter */
|
---|
119 | escape_domain_name = escape_ldap_string_alloc(domain_name);
|
---|
120 | if (!escape_domain_name) {
|
---|
121 | DEBUG(0, ("Out of memory!\n"));
|
---|
122 | return NT_STATUS_NO_MEMORY;
|
---|
123 | }
|
---|
124 |
|
---|
125 | slprintf (filter, sizeof (filter) - 1, "(&(%s=%s)(objectclass=%s))",
|
---|
126 | get_attr_key2string(dominfo_attr_list, LDAP_ATTR_DOMAIN),
|
---|
127 | escape_domain_name, LDAP_OBJ_DOMINFO);
|
---|
128 |
|
---|
129 | SAFE_FREE(escape_domain_name);
|
---|
130 |
|
---|
131 | attr_list = get_attr_list( NULL, dominfo_attr_list );
|
---|
132 | rc = smbldap_search_suffix(ldap_state, filter, attr_list, &result);
|
---|
133 | TALLOC_FREE( attr_list );
|
---|
134 |
|
---|
135 | if (rc != LDAP_SUCCESS) {
|
---|
136 | return NT_STATUS_UNSUCCESSFUL;
|
---|
137 | }
|
---|
138 |
|
---|
139 | num_result = ldap_count_entries(ldap_state->ldap_struct, result);
|
---|
140 |
|
---|
141 | if (num_result > 1) {
|
---|
142 | DEBUG (0, ("add_new_domain_info: More than domain with that name exists: bailing "
|
---|
143 | "out!\n"));
|
---|
144 | ldap_msgfree(result);
|
---|
145 | return NT_STATUS_UNSUCCESSFUL;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /* Check if we need to add an entry */
|
---|
149 | DEBUG(3,("add_new_domain_info: Adding new domain\n"));
|
---|
150 |
|
---|
151 | /* this time escape for DN */
|
---|
152 | escape_domain_name = escape_rdn_val_string_alloc(domain_name);
|
---|
153 | if (!escape_domain_name) {
|
---|
154 | DEBUG(0, ("Out of memory!\n"));
|
---|
155 | return NT_STATUS_NO_MEMORY;
|
---|
156 | }
|
---|
157 |
|
---|
158 | pstr_sprintf(dn, "%s=%s,%s",
|
---|
159 | get_attr_key2string(dominfo_attr_list, LDAP_ATTR_DOMAIN),
|
---|
160 | escape_domain_name, lp_ldap_suffix());
|
---|
161 |
|
---|
162 | SAFE_FREE(escape_domain_name);
|
---|
163 |
|
---|
164 | /* Free original search */
|
---|
165 | ldap_msgfree(result);
|
---|
166 |
|
---|
167 | /* make the changes - the entry *must* not already have samba
|
---|
168 | * attributes */
|
---|
169 |
|
---|
170 | smbldap_set_mod(&mods, LDAP_MOD_ADD,
|
---|
171 | get_attr_key2string(dominfo_attr_list,
|
---|
172 | LDAP_ATTR_DOMAIN),
|
---|
173 | domain_name);
|
---|
174 |
|
---|
175 | /* If we don't have an entry, then ask secrets.tdb for what it thinks.
|
---|
176 | It may choose to make it up */
|
---|
177 |
|
---|
178 | sid_to_string(sid_string, get_global_sam_sid());
|
---|
179 | smbldap_set_mod(&mods, LDAP_MOD_ADD,
|
---|
180 | get_attr_key2string(dominfo_attr_list,
|
---|
181 | LDAP_ATTR_DOM_SID),
|
---|
182 | sid_string);
|
---|
183 |
|
---|
184 | slprintf(algorithmic_rid_base_string,
|
---|
185 | sizeof(algorithmic_rid_base_string) - 1, "%i",
|
---|
186 | algorithmic_rid_base());
|
---|
187 | smbldap_set_mod(&mods, LDAP_MOD_ADD,
|
---|
188 | get_attr_key2string(dominfo_attr_list,
|
---|
189 | LDAP_ATTR_ALGORITHMIC_RID_BASE),
|
---|
190 | algorithmic_rid_base_string);
|
---|
191 | smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_DOMINFO);
|
---|
192 |
|
---|
193 | /* add the sambaNextUserRid attributes. */
|
---|
194 |
|
---|
195 | {
|
---|
196 | uint32 rid = BASE_RID;
|
---|
197 | fstring rid_str;
|
---|
198 |
|
---|
199 | fstr_sprintf( rid_str, "%i", rid );
|
---|
200 | DEBUG(10,("add_new_domain_info: setting next available user rid [%s]\n", rid_str));
|
---|
201 | smbldap_set_mod(&mods, LDAP_MOD_ADD,
|
---|
202 | get_attr_key2string(dominfo_attr_list,
|
---|
203 | LDAP_ATTR_NEXT_USERRID),
|
---|
204 | rid_str);
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | rc = smbldap_add(ldap_state, dn, mods);
|
---|
209 |
|
---|
210 | if (rc!=LDAP_SUCCESS) {
|
---|
211 | char *ld_error = NULL;
|
---|
212 | ldap_get_option(ldap_state->ldap_struct,
|
---|
213 | LDAP_OPT_ERROR_STRING, &ld_error);
|
---|
214 | DEBUG(1,("add_new_domain_info: failed to add domain dn= %s with: %s\n\t%s\n",
|
---|
215 | dn, ldap_err2string(rc),
|
---|
216 | ld_error?ld_error:"unknown"));
|
---|
217 | SAFE_FREE(ld_error);
|
---|
218 |
|
---|
219 | ldap_mods_free(mods, True);
|
---|
220 | return NT_STATUS_UNSUCCESSFUL;
|
---|
221 | }
|
---|
222 |
|
---|
223 | DEBUG(2,("add_new_domain_info: added: domain = %s in the LDAP database\n", domain_name));
|
---|
224 | ldap_mods_free(mods, True);
|
---|
225 | return NT_STATUS_OK;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**********************************************************************
|
---|
229 | Search for the domain info entry
|
---|
230 | *********************************************************************/
|
---|
231 |
|
---|
232 | NTSTATUS smbldap_search_domain_info(struct smbldap_state *ldap_state,
|
---|
233 | LDAPMessage ** result, const char *domain_name,
|
---|
234 | BOOL try_add)
|
---|
235 | {
|
---|
236 | NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
|
---|
237 | pstring filter;
|
---|
238 | int rc;
|
---|
239 | const char **attr_list;
|
---|
240 | int count;
|
---|
241 | char *escape_domain_name;
|
---|
242 |
|
---|
243 | escape_domain_name = escape_ldap_string_alloc(domain_name);
|
---|
244 | if (!escape_domain_name) {
|
---|
245 | DEBUG(0, ("Out of memory!\n"));
|
---|
246 | return NT_STATUS_NO_MEMORY;
|
---|
247 | }
|
---|
248 |
|
---|
249 | pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))",
|
---|
250 | LDAP_OBJ_DOMINFO,
|
---|
251 | get_attr_key2string(dominfo_attr_list, LDAP_ATTR_DOMAIN),
|
---|
252 | escape_domain_name);
|
---|
253 |
|
---|
254 | SAFE_FREE(escape_domain_name);
|
---|
255 |
|
---|
256 | DEBUG(2, ("smbldap_search_domain_info: Searching for:[%s]\n", filter));
|
---|
257 |
|
---|
258 | attr_list = get_attr_list( NULL, dominfo_attr_list );
|
---|
259 | rc = smbldap_search_suffix(ldap_state, filter, attr_list , result);
|
---|
260 | TALLOC_FREE( attr_list );
|
---|
261 |
|
---|
262 | if (rc != LDAP_SUCCESS) {
|
---|
263 | DEBUG(2,("smbldap_search_domain_info: Problem during LDAPsearch: %s\n", ldap_err2string (rc)));
|
---|
264 | DEBUG(2,("smbldap_search_domain_info: Query was: %s, %s\n", lp_ldap_suffix(), filter));
|
---|
265 | goto failed;
|
---|
266 | }
|
---|
267 |
|
---|
268 | count = ldap_count_entries(ldap_state->ldap_struct, *result);
|
---|
269 |
|
---|
270 | if (count == 1)
|
---|
271 | return NT_STATUS_OK;
|
---|
272 |
|
---|
273 | ldap_msgfree(*result);
|
---|
274 | *result = NULL;
|
---|
275 |
|
---|
276 | if (count < 1) {
|
---|
277 |
|
---|
278 | DEBUG(3, ("smbldap_search_domain_info: Got no domain info entries for domain\n"));
|
---|
279 |
|
---|
280 | if (!try_add)
|
---|
281 | goto failed;
|
---|
282 |
|
---|
283 | status = add_new_domain_info(ldap_state, domain_name);
|
---|
284 | if (!NT_STATUS_IS_OK(status)) {
|
---|
285 | DEBUG(0, ("smbldap_search_domain_info: Adding domain info for %s failed with %s\n",
|
---|
286 | domain_name, nt_errstr(status)));
|
---|
287 | goto failed;
|
---|
288 | }
|
---|
289 |
|
---|
290 | status = add_new_domain_account_policies(ldap_state, domain_name);
|
---|
291 | if (!NT_STATUS_IS_OK(status)) {
|
---|
292 | DEBUG(0, ("smbldap_search_domain_info: Adding domain account policies for %s failed with %s\n",
|
---|
293 | domain_name, nt_errstr(status)));
|
---|
294 | goto failed;
|
---|
295 | }
|
---|
296 |
|
---|
297 | return smbldap_search_domain_info(ldap_state, result, domain_name, False);
|
---|
298 |
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (count > 1 ) {
|
---|
302 |
|
---|
303 | DEBUG(0, ("smbldap_search_domain_info: Got too many (%d) domain info entries for domain %s\n",
|
---|
304 | count, domain_name));
|
---|
305 | goto failed;
|
---|
306 | }
|
---|
307 |
|
---|
308 | failed:
|
---|
309 | return status;
|
---|
310 | }
|
---|