Add tldap_pull_uint32
[Samba.git] / source3 / lib / tldap_util.c
blob1b0c3446ee633e78d5655d1ba86d44b535a37b64
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async ldap client requests
4 Copyright (C) Volker Lendecke 2009
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
23 int *num_values, DATA_BLOB **values)
25 struct tldap_attribute *attributes;
26 int i, num_attributes;
28 if (!tldap_entry_attributes(msg, &num_attributes, &attributes)) {
29 return false;
32 for (i=0; i<num_attributes; i++) {
33 if (strequal(attribute, attributes[i].name)) {
34 break;
37 if (i == num_attributes) {
38 return false;
40 *num_values = attributes[i].num_values;
41 *values = attributes[i].values;
42 return true;
45 bool tldap_get_single_valueblob(struct tldap_message *msg,
46 const char *attribute, DATA_BLOB *blob)
48 int num_values;
49 DATA_BLOB *values;
51 if (attribute == NULL) {
52 return NULL;
54 if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
55 return NULL;
57 if (num_values != 1) {
58 return NULL;
60 *blob = values[0];
61 return true;
64 char *tldap_talloc_single_attribute(struct tldap_message *msg,
65 const char *attribute,
66 TALLOC_CTX *mem_ctx)
68 DATA_BLOB val;
69 char *result;
70 size_t len;
72 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
73 return false;
75 if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
76 val.data, val.length,
77 &result, &len, false)) {
78 return NULL;
80 return result;
83 bool tldap_pull_binsid(struct tldap_message *msg, const char *attribute,
84 struct dom_sid *sid)
86 DATA_BLOB val;
88 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
89 return false;
91 return sid_parse((char *)val.data, val.length, sid);
94 static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
95 int num_newvals, DATA_BLOB *newvals)
97 int num_values = talloc_array_length(mod->values);
98 int i;
99 DATA_BLOB *tmp;
101 tmp = talloc_realloc(mem_ctx, mod->values, DATA_BLOB,
102 num_values + num_newvals);
103 if (tmp == NULL) {
104 return false;
106 mod->values = tmp;
108 for (i=0; i<num_newvals; i++) {
109 mod->values[i+num_values].data = (uint8_t *)talloc_memdup(
110 mod->values, newvals[i].data, newvals[i].length);
111 if (mod->values[i+num_values].data == NULL) {
112 return false;
114 mod->values[i+num_values].length = newvals[i].length;
116 mod->num_values = num_values + num_newvals;
117 return true;
120 bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
121 int mod_op, const char *attrib,
122 int num_newvals, DATA_BLOB *newvals)
124 struct tldap_mod new_mod;
125 struct tldap_mod *mods = *pmods;
126 struct tldap_mod *mod = NULL;
127 int i, num_mods;
129 if (mods == NULL) {
130 mods = talloc_array(mem_ctx, struct tldap_mod, 0);
132 if (mods == NULL) {
133 return false;
136 num_mods = talloc_array_length(mods);
138 for (i=0; i<num_mods; i++) {
139 if ((mods[i].mod_op == mod_op)
140 && strequal(mods[i].attribute, attrib)) {
141 mod = &mods[i];
142 break;
146 if (mod == NULL) {
147 new_mod.mod_op = mod_op;
148 new_mod.attribute = talloc_strdup(mods, attrib);
149 if (new_mod.attribute == NULL) {
150 return false;
152 new_mod.num_values = 0;
153 new_mod.values = NULL;
154 mod = &new_mod;
157 if ((num_newvals != 0)
158 && !tldap_add_blob_vals(mods, mod, num_newvals, newvals)) {
159 return false;
162 if (i == num_mods) {
163 mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
164 num_mods+1);
165 if (mods == NULL) {
166 return false;
168 mods[num_mods] = *mod;
171 *pmods = mods;
172 return true;
175 static bool tldap_make_mod_blob_int(struct tldap_message *existing,
176 TALLOC_CTX *mem_ctx,
177 int *pnum_mods, struct tldap_mod **pmods,
178 const char *attrib, DATA_BLOB newval,
179 int (*comparison)(const DATA_BLOB *d1,
180 const DATA_BLOB *d2))
182 int num_values = 0;
183 DATA_BLOB *values = NULL;
184 DATA_BLOB oldval = data_blob_null;
186 if ((existing != NULL)
187 && tldap_entry_values(existing, attrib, &num_values, &values)) {
189 if (num_values > 1) {
190 /* can't change multivalue attributes atm */
191 return false;
193 if (num_values == 1) {
194 oldval = values[0];
198 if ((oldval.data != NULL) && (newval.data != NULL)
199 && (comparison(&oldval, &newval) == 0)) {
200 /* Believe it or not, but LDAP will deny a delete and
201 an add at the same time if the values are the
202 same... */
203 DEBUG(10,("smbldap_make_mod_blob: attribute |%s| not "
204 "changed.\n", attrib));
205 return true;
208 if (oldval.data != NULL) {
209 /* By deleting exactly the value we found in the entry this
210 * should be race-free in the sense that the LDAP-Server will
211 * deny the complete operation if somebody changed the
212 * attribute behind our back. */
213 /* This will also allow modifying single valued attributes in
214 * Novell NDS. In NDS you have to first remove attribute and
215 * then you could add new value */
217 DEBUG(10, ("smbldap_make_mod_blob: deleting attribute |%s|\n",
218 attrib));
219 if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_DELETE,
220 attrib, 1, &oldval)) {
221 return false;
225 /* Regardless of the real operation (add or modify)
226 we add the new value here. We rely on deleting
227 the old value, should it exist. */
229 if (newval.data != NULL) {
230 DEBUG(10, ("smbldap_make_mod: adding attribute |%s| value len "
231 "%d\n", attrib, (int)newval.length));
232 if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_ADD,
233 attrib, 1, &newval)) {
234 return false;
237 *pnum_mods = talloc_array_length(*pmods);
238 return true;
241 bool tldap_make_mod_blob(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
242 int *pnum_mods, struct tldap_mod **pmods,
243 const char *attrib, DATA_BLOB newval)
245 return tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
246 attrib, newval, data_blob_cmp);
249 static int compare_utf8_blobs(const DATA_BLOB *d1, const DATA_BLOB *d2)
251 char *s1, *s2;
252 size_t s1len, s2len;
253 int ret;
255 if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d1->data,
256 d1->length, &s1, &s1len, false)) {
257 /* can't do much here */
258 return 0;
260 if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d2->data,
261 d2->length, &s2, &s2len, false)) {
262 /* can't do much here */
263 TALLOC_FREE(s1);
264 return 0;
266 ret = StrCaseCmp(s1, s2);
267 TALLOC_FREE(s2);
268 TALLOC_FREE(s1);
269 return ret;
272 bool tldap_make_mod_fmt(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
273 int *pnum_mods, struct tldap_mod **pmods,
274 const char *attrib, const char *fmt, ...)
276 va_list ap;
277 char *newval;
278 bool ret;
279 DATA_BLOB blob = data_blob_null;
281 va_start(ap, fmt);
282 newval = talloc_vasprintf(talloc_tos(), fmt, ap);
283 va_end(ap);
285 if (newval == NULL) {
286 return false;
289 blob.length = strlen(newval);
290 if (blob.length != 0) {
291 blob.data = CONST_DISCARD(uint8_t *, newval);
293 ret = tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
294 attrib, blob, compare_utf8_blobs);
295 TALLOC_FREE(newval);
296 return ret;
299 const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld, int rc)
301 const char *ld_error = NULL;
302 char *res;
304 ld_error = tldap_ctx_diagnosticmessage(ld);
305 res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s", rc,
306 tldap_err2string(rc),
307 ld_error ? ld_error : "unknown");
308 return res;
311 int tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
312 const char *attrs[], int num_attrs, int attrsonly,
313 TALLOC_CTX *mem_ctx, struct tldap_message ***res,
314 const char *fmt, ...)
316 va_list ap;
317 char *filter;
318 int ret;
320 va_start(ap, fmt);
321 filter = talloc_vasprintf(talloc_tos(), fmt, ap);
322 va_end(ap);
324 if (filter == NULL) {
325 return TLDAP_NO_MEMORY;
327 ret = tldap_search(ld, base, scope, filter,
328 attrs, num_attrs, attrsonly,
329 NULL /*sctrls*/, NULL /*cctrls*/,
330 0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
331 mem_ctx, res, NULL);
332 TALLOC_FREE(filter);
333 return ret;
336 bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
337 uint64_t *presult)
339 char *str;
340 uint64_t result;
342 str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
343 if (str == NULL) {
344 DEBUG(10, ("Could not find attribute %s\n", attr));
345 return false;
347 result = strtoull(str, NULL, 10);
348 TALLOC_FREE(str);
349 *presult = result;
350 return true;
353 bool tldap_pull_uint32(struct tldap_message *msg, const char *attr,
354 uint32_t *presult)
356 uint64_t result;
358 if (!tldap_pull_uint64(msg, attr, &result)) {
359 return false;
361 *presult = (uint32_t)result;
362 return true;