20
20
#include "includes.h"
22
#include "tldap_util.h"
23
#include "../libcli/security/security.h"
24
#include "../lib/util/asn1.h"
25
#include "../librpc/ndr/libndr.h"
22
27
bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
23
int *num_values, DATA_BLOB **values)
28
DATA_BLOB **values, int *num_values)
25
30
struct tldap_attribute *attributes;
26
31
int i, num_attributes;
28
if (!tldap_entry_attributes(msg, &num_attributes, &attributes)) {
33
if (!tldap_entry_attributes(msg, &attributes, &num_attributes)) {
51
56
if (attribute == NULL) {
54
if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
59
if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
57
62
if (num_values != 1) {
105
110
static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
106
int num_newvals, DATA_BLOB *newvals)
111
DATA_BLOB *newvals, int num_newvals)
108
113
int num_values = talloc_array_length(mod->values);
131
bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
136
bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx,
137
struct tldap_mod **pmods, int *pnum_mods,
132
138
int mod_op, const char *attrib,
133
int num_newvals, DATA_BLOB *newvals)
139
DATA_BLOB *newvals, int num_newvals)
135
141
struct tldap_mod new_mod;
136
142
struct tldap_mod *mods = *pmods;
168
174
if ((num_newvals != 0)
169
&& !tldap_add_blob_vals(mods, mod, num_newvals, newvals)) {
175
&& !tldap_add_blob_vals(mods, mod, newvals, num_newvals)) {
179
if ((i == num_mods) && (talloc_array_length(mods) < num_mods + 1)) {
174
180
mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
176
182
if (mods == NULL) {
186
bool tldap_add_mod_str(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
193
bool tldap_add_mod_str(TALLOC_CTX *mem_ctx,
194
struct tldap_mod **pmods, int *pnum_mods,
187
195
int mod_op, const char *attrib, const char *str)
198
ret = tldap_add_mod_blobs(mem_ctx, pmods, mod_op, attrib, 1, &utf8);
206
ret = tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods, mod_op, attrib,
199
208
TALLOC_FREE(utf8.data);
203
212
static bool tldap_make_mod_blob_int(struct tldap_message *existing,
204
213
TALLOC_CTX *mem_ctx,
205
int *pnum_mods, struct tldap_mod **pmods,
214
struct tldap_mod **pmods, int *pnum_mods,
206
215
const char *attrib, DATA_BLOB newval,
207
216
int (*comparison)(const DATA_BLOB *d1,
208
217
const DATA_BLOB *d2))
212
221
DATA_BLOB oldval = data_blob_null;
214
223
if ((existing != NULL)
215
&& tldap_entry_values(existing, attrib, &num_values, &values)) {
224
&& tldap_entry_values(existing, attrib, &values, &num_values)) {
217
226
if (num_values > 1) {
218
227
/* can't change multivalue attributes atm */
228
237
/* Believe it or not, but LDAP will deny a delete and
229
238
an add at the same time if the values are the
231
DEBUG(10,("smbldap_make_mod_blob: attribute |%s| not "
240
DEBUG(10,("tldap_make_mod_blob_int: attribute |%s| not "
232
241
"changed.\n", attrib));
242
251
* Novell NDS. In NDS you have to first remove attribute and
243
252
* then you could add new value */
245
DEBUG(10, ("smbldap_make_mod_blob: deleting attribute |%s|\n",
254
DEBUG(10, ("tldap_make_mod_blob_int: deleting attribute |%s|\n",
247
if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_DELETE,
248
attrib, 1, &oldval)) {
256
if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
258
attrib, &oldval, 1)) {
255
265
the old value, should it exist. */
257
267
if (newval.data != NULL) {
258
DEBUG(10, ("smbldap_make_mod: adding attribute |%s| value len "
268
DEBUG(10, ("tldap_make_mod_blob_int: adding attribute |%s| value len "
259
269
"%d\n", attrib, (int)newval.length));
260
if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_ADD,
261
attrib, 1, &newval)) {
270
if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
272
attrib, &newval, 1)) {
265
*pnum_mods = talloc_array_length(*pmods);
269
279
bool tldap_make_mod_blob(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
270
int *pnum_mods, struct tldap_mod **pmods,
280
struct tldap_mod **pmods, int *pnum_mods,
271
281
const char *attrib, DATA_BLOB newval)
273
return tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
283
return tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
274
284
attrib, newval, data_blob_cmp);
300
310
bool tldap_make_mod_fmt(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
301
int *pnum_mods, struct tldap_mod **pmods,
311
struct tldap_mod **pmods, int *pnum_mods,
302
312
const char *attrib, const char *fmt, ...)
318
328
if (blob.length != 0) {
319
329
blob.data = CONST_DISCARD(uint8_t *, newval);
321
ret = tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
331
ret = tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
322
332
attrib, blob, compare_utf8_blobs);
323
333
TALLOC_FREE(newval);
329
339
const char *ld_error = NULL;
332
ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
343
ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
333
345
res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s", rc,
334
346
tldap_err2string(rc),
335
347
ld_error ? ld_error : "unknown");
539
551
int i, num_values;
540
552
DATA_BLOB *values;
542
if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
554
if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
545
557
for (i=0; i<num_values; i++) {