1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1998-2021 The OpenLDAP Foundation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
9 * Public License.
10 *
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
14 */
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
17 */
18
19 /*
20 * This file contains public API to help with parsing LDIF
21 */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/stdlib.h>
28 #include <ac/ctype.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
31 #include <ac/socket.h>
32 #include <ac/time.h>
33
34 #include "ldap-int.h"
35 #include "ldif.h"
36
37 #define M_SEP 0x7f
38
39 /* strings found in LDIF entries */
40 static struct berval BV_VERSION = BER_BVC("version");
41 static struct berval BV_DN = BER_BVC("dn");
42 static struct berval BV_CONTROL = BER_BVC("control");
43 static struct berval BV_CHANGETYPE = BER_BVC("changetype");
44 static struct berval BV_ADDCT = BER_BVC("add");
45 static struct berval BV_MODIFYCT = BER_BVC("modify");
46 static struct berval BV_DELETECT = BER_BVC("delete");
47 static struct berval BV_MODRDNCT = BER_BVC("modrdn");
48 static struct berval BV_MODDNCT = BER_BVC("moddn");
49 static struct berval BV_RENAMECT = BER_BVC("rename");
50 static struct berval BV_MODOPADD = BER_BVC("add");
51 static struct berval BV_MODOPREPLACE = BER_BVC("replace");
52 static struct berval BV_MODOPDELETE = BER_BVC("delete");
53 static struct berval BV_MODOPINCREMENT = BER_BVC("increment");
54 static struct berval BV_NEWRDN = BER_BVC("newrdn");
55 static struct berval BV_DELETEOLDRDN = BER_BVC("deleteoldrdn");
56 static struct berval BV_NEWSUP = BER_BVC("newsuperior");
57
58 #define BV_CASEMATCH(a, b) \
59 ((a)->bv_len == (b)->bv_len && 0 == strcasecmp((a)->bv_val, (b)->bv_val))
60
61 static int parse_ldif_control LDAP_P(( struct berval *bval, LDAPControl ***ppctrls ));
62
63 void
ldap_ldif_record_done(LDIFRecord * lr)64 ldap_ldif_record_done( LDIFRecord *lr )
65 {
66 int i;
67
68 /* the LDAPControl stuff does not allow the use of memory contexts */
69 if (lr->lr_ctrls != NULL) {
70 ldap_controls_free( lr->lr_ctrls );
71 }
72 if ( lr->lr_lm != NULL ) {
73 ber_memfree_x( lr->lr_lm, lr->lr_ctx );
74 }
75 if ( lr->lr_mops != NULL ) {
76 ber_memfree_x( lr->lr_mops, lr->lr_ctx );
77 }
78 for (i=lr->lr_lines-1; i>=0; i--)
79 if ( lr->lr_freeval[i] ) ber_memfree_x( lr->lr_vals[i].bv_val, lr->lr_ctx );
80 ber_memfree_x( lr->lr_btype, lr->lr_ctx );
81
82 memset( lr, 0, sizeof(LDIFRecord) );
83 }
84
85 /*
86 * ldap_parse_ldif_record_x() will convert an LDIF record read with ldif_read_record()
87 * into an array of LDAPMod* and an array of LDAPControl*, suitable for passing
88 * directly to any other LDAP API function that takes LDAPMod** and LDAPControl**
89 * arguments, such as ldap_modify_s().
90 *
91 * rbuf - the ldif record buffer returned from ldif_read_record - rbuf.bv_val must be
92 * writable - will use ldif_getline to read from it
93 * linenum - the ldif line number returned from ldif_read_record
94 * - used for logging errors (e.g. error at line N)
95 * lr - holds the data to return
96 * errstr - a string used for logging (usually the program name e.g. "ldapmodify"
97 * flags - 0 or some combination of LDIF_DEFAULT_ADD LDIF_ENTRIES_ONLY LDIF_NO_CONTROLS
98 * ctx is the memory allocation context - if NULL, use the standard memory allocator
99 */
100 int
ldap_parse_ldif_record_x(struct berval * rbuf,unsigned long linenum,LDIFRecord * lr,const char * errstr,unsigned int flags,void * ctx)101 ldap_parse_ldif_record_x(
102 struct berval *rbuf,
103 unsigned long linenum,
104 LDIFRecord *lr,
105 const char *errstr,
106 unsigned int flags,
107 void *ctx )
108 {
109 char *line, *dn;
110 int rc, modop;
111 int expect_modop, expect_sep;
112 int ldapadd, new_entry, delete_entry, got_all, no_dn;
113 LDAPMod **pmods;
114 int version;
115 LDAPControl **pctrls;
116 int i, j, k, idn, nmods;
117 struct berval **bvl, bv;
118
119 assert( lr != NULL );
120 assert( rbuf != NULL );
121 memset( lr, 0, sizeof(LDIFRecord) );
122 lr->lr_ctx = ctx; /* save memory context for later */
123 ldapadd = flags & LDIF_DEFAULT_ADD;
124 no_dn = flags & LDIF_NO_DN;
125 expect_modop = flags & LDIF_MODS_ONLY;
126 new_entry = ldapadd;
127
128 rc = got_all = delete_entry = modop = 0;
129 expect_sep = 0;
130 version = 0;
131 pmods = NULL;
132 pctrls = NULL;
133 dn = NULL;
134
135 lr->lr_lines = ldif_countlines( rbuf->bv_val );
136 lr->lr_btype = ber_memcalloc_x( 1, (lr->lr_lines+1)*2*sizeof(struct berval)+lr->lr_lines, ctx );
137 if ( !lr->lr_btype )
138 return LDAP_NO_MEMORY;
139
140 lr->lr_vals = lr->lr_btype+lr->lr_lines+1;
141 lr->lr_freeval = (char *)(lr->lr_vals+lr->lr_lines+1);
142 i = -1;
143
144 while ( rc == 0 && ( line = ldif_getline( &rbuf->bv_val )) != NULL ) {
145 int freev;
146
147 if ( *line == '\n' || *line == '\0' ) {
148 break;
149 }
150
151 ++i;
152
153 if ( line[0] == '-' && !line[1] ) {
154 BER_BVZERO( lr->lr_btype+i );
155 lr->lr_freeval[i] = 0;
156 continue;
157 }
158
159 if ( ( rc = ldif_parse_line2( line, lr->lr_btype+i, lr->lr_vals+i, &freev ) ) < 0 ) {
160 fprintf( stderr, _("%s: invalid format (line %lu) entry: \"%s\"\n"),
161 errstr, linenum+i, dn == NULL ? "" : dn );
162 rc = LDAP_PARAM_ERROR;
163 goto leave;
164 }
165 lr->lr_freeval[i] = freev;
166
167 if ( dn == NULL && !no_dn ) {
168 if ( linenum+i == 1 && BV_CASEMATCH( lr->lr_btype+i, &BV_VERSION )) {
169 /* lutil_atoi() introduces a dependence of libldap
170 * on liblutil; we only allow version 1 by now (ITS#6654)
171 */
172 #if 0
173 int v;
174 if( lr->lr_vals[i].bv_len == 0 || lutil_atoi( &v, lr->lr_vals[i].bv_val) != 0 || v != 1 )
175 #endif
176 static const struct berval version1 = { 1, "1" };
177 if ( lr->lr_vals[i].bv_len != version1.bv_len || strncmp( lr->lr_vals[i].bv_val, version1.bv_val, version1.bv_len ) != 0 )
178 {
179 fprintf( stderr,
180 _("%s: invalid version %s, line %lu (ignored)\n"),
181 errstr, lr->lr_vals[i].bv_val, linenum );
182 }
183 version++;
184
185 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) {
186 lr->lr_dn = lr->lr_vals[i];
187 dn = lr->lr_dn.bv_val; /* primarily for logging */
188 idn = i;
189 }
190 /* skip all lines until we see "dn:" */
191 }
192 }
193
194 /* check to make sure there was a dn: line */
195 if ( !dn && !no_dn ) {
196 rc = 0;
197 goto leave;
198 }
199
200 lr->lr_lines = i+1;
201
202 if( lr->lr_lines == 0 ) {
203 rc = 0;
204 goto leave;
205 }
206
207 if( version && lr->lr_lines == 1 ) {
208 rc = 0;
209 goto leave;
210 }
211
212 if ( no_dn ) {
213 i = 0;
214 } else {
215 i = idn+1;
216 /* Check for "control" tag after dn and before changetype. */
217 if ( BV_CASEMATCH( lr->lr_btype+i, &BV_CONTROL )) {
218 /* Parse and add it to the list of controls */
219 if ( !( flags & LDIF_NO_CONTROLS ) ) {
220 rc = parse_ldif_control( lr->lr_vals+i, &pctrls );
221 if (rc != 0) {
222 fprintf( stderr,
223 _("%s: Error processing %s line, line %lu: %s\n"),
224 errstr, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
225 }
226 }
227 i++;
228 if ( i>= lr->lr_lines ) {
229 short_input:
230 fprintf( stderr,
231 _("%s: Expecting more input after %s line, line %lu\n"),
232 errstr, lr->lr_btype[i-1].bv_val, linenum+i );
233
234 rc = LDAP_PARAM_ERROR;
235 goto leave;
236 }
237 }
238 }
239
240 /* Check for changetype */
241 if ( BV_CASEMATCH( lr->lr_btype+i, &BV_CHANGETYPE )) {
242 #ifdef LIBERAL_CHANGETYPE_MODOP
243 /* trim trailing spaces (and log warning ...) */
244 int icnt;
245 for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
246 if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) {
247 break;
248 }
249 }
250
251 if ( ++icnt != lr->lr_vals[i].bv_len ) {
252 fprintf( stderr, _("%s: illegal trailing space after"
253 " \"%s: %s\" trimmed (line %lu, entry \"%s\")\n"),
254 errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn );
255 lr->lr_vals[i].bv_val[icnt] = '\0';
256 }
257 #endif /* LIBERAL_CHANGETYPE_MODOP */
258
259 /* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or
260 there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */
261 if ( flags & LDIF_ENTRIES_ONLY ) {
262 if ( !( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) ) {
263 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
264 _("%s: skipping LDIF record beginning at line %lu: "
265 "changetype '%.*s' found but entries only was requested\n"),
266 errstr, linenum,
267 (int)lr->lr_vals[i].bv_len,
268 (const char *)lr->lr_vals[i].bv_val );
269 goto leave;
270 }
271 }
272
273 if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODIFYCT )) {
274 new_entry = 0;
275 expect_modop = 1;
276 } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) {
277 new_entry = 1;
278 modop = LDAP_MOD_ADD;
279 } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODRDNCT )
280 || BV_CASEMATCH( lr->lr_vals+i, &BV_MODDNCT )
281 || BV_CASEMATCH( lr->lr_vals+i, &BV_RENAMECT ))
282 {
283 i++;
284 if ( i >= lr->lr_lines )
285 goto short_input;
286 if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWRDN )) {
287 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
288 " \"%s:\" (line %lu, entry \"%s\")\n"),
289 errstr, BV_NEWRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
290 rc = LDAP_PARAM_ERROR;
291 goto leave;
292 }
293 lr->lrop_newrdn = lr->lr_vals[i];
294 i++;
295 if ( i >= lr->lr_lines )
296 goto short_input;
297 if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_DELETEOLDRDN )) {
298 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
299 " \"%s:\" (line %lu, entry \"%s\")\n"),
300 errstr, BV_DELETEOLDRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
301 rc = LDAP_PARAM_ERROR;
302 goto leave;
303 }
304 lr->lrop_delold = ( lr->lr_vals[i].bv_val[0] == '0' ) ? 0 : 1;
305 i++;
306 if ( i < lr->lr_lines ) {
307 if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWSUP )) {
308 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
309 " \"%s:\" (line %lu, entry \"%s\")\n"),
310 errstr, BV_NEWSUP.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
311 rc = LDAP_PARAM_ERROR;
312 goto leave;
313 }
314 lr->lrop_newsup = lr->lr_vals[i];
315 i++;
316 }
317 got_all = 1;
318 } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_DELETECT )) {
319 got_all = delete_entry = 1;
320 } else {
321 fprintf( stderr,
322 _("%s: unknown %s \"%s\" (line %lu, entry \"%s\")\n"),
323 errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn );
324 rc = LDAP_PARAM_ERROR;
325 goto leave;
326 }
327 i++;
328 } else if ( ldapadd ) { /* missing changetype => add */
329 new_entry = 1;
330 modop = LDAP_MOD_ADD;
331 } else {
332 /* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or
333 there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */
334 if ( flags & LDIF_ENTRIES_ONLY ) {
335 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
336 _("%s: skipping LDIF record beginning at line %lu: "
337 "no changetype found but entries only was requested and "
338 "the default setting for missing changetype is modify\n"),
339 errstr, linenum );
340 goto leave;
341 }
342 expect_modop = 1; /* missing changetype => modify */
343 }
344
345 if ( got_all ) {
346 if ( i < lr->lr_lines ) {
347 fprintf( stderr,
348 _("%s: extra lines at end (line %lu, entry \"%s\")\n"),
349 errstr, linenum+i, dn );
350 rc = LDAP_PARAM_ERROR;
351 goto leave;
352 }
353 goto doit;
354 }
355
356 nmods = lr->lr_lines - i;
357 idn = i;
358
359 if ( new_entry ) {
360 int fv;
361
362 /* Make sure all attributes with multiple values are contiguous */
363 for (; i<lr->lr_lines; i++) {
364 for (j=i+1; j<lr->lr_lines; j++) {
365 if ( !lr->lr_btype[j].bv_val ) {
366 fprintf( stderr,
367 _("%s: missing attributeDescription (line %lu, entry \"%s\")\n"),
368 errstr, linenum+j, dn );
369 rc = LDAP_PARAM_ERROR;
370 goto leave;
371 }
372 if ( BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+j )) {
373 nmods--;
374 /* out of order, move intervening attributes down */
375 if ( j != i+1 ) {
376 bv = lr->lr_vals[j];
377 fv = lr->lr_freeval[j];
378 for (k=j; k>i; k--) {
379 lr->lr_btype[k] = lr->lr_btype[k-1];
380 lr->lr_vals[k] = lr->lr_vals[k-1];
381 lr->lr_freeval[k] = lr->lr_freeval[k-1];
382 }
383 k++;
384 lr->lr_btype[k] = lr->lr_btype[i];
385 lr->lr_vals[k] = bv;
386 lr->lr_freeval[k] = fv;
387 }
388 i++;
389 }
390 }
391 }
392 /* Allocate space for array of mods, array of pointers to mods,
393 * and array of pointers to values, allowing for NULL terminators
394 * for the pointer arrays...
395 */
396 lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
397 (nmods+1) * sizeof(LDAPMod*) +
398 (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
399 if ( lr->lr_lm == NULL ) {
400 rc = LDAP_NO_MEMORY;
401 goto leave;
402 }
403
404 pmods = (LDAPMod **)(lr->lr_lm+nmods);
405 bvl = (struct berval **)(pmods+nmods+1);
406
407 j = 0;
408 k = -1;
409 BER_BVZERO(&bv);
410 for (i=idn; i<lr->lr_lines; i++) {
411 if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) {
412 fprintf( stderr, _("%s: attributeDescription \"%s\":"
413 " (possible missing newline"
414 " after line %lu, entry \"%s\"?)\n"),
415 errstr, lr->lr_btype[i].bv_val, linenum+i - 1, dn );
416 }
417 if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
418 bvl[k++] = NULL;
419 bv = lr->lr_btype[i];
420 lr->lr_lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
421 lr->lr_lm[j].mod_type = bv.bv_val;
422 lr->lr_lm[j].mod_bvalues = bvl+k;
423 pmods[j] = lr->lr_lm+j;
424 j++;
425 }
426 bvl[k++] = lr->lr_vals+i;
427 }
428 bvl[k] = NULL;
429 pmods[j] = NULL;
430 goto doit;
431 }
432
433 lr->lr_mops = ber_memalloc_x( lr->lr_lines+1, ctx );
434 if ( lr->lr_mops == NULL ) {
435 rc = LDAP_NO_MEMORY;
436 goto leave;
437 }
438
439 lr->lr_mops[lr->lr_lines] = M_SEP;
440 if ( i > 0 )
441 lr->lr_mops[i-1] = M_SEP;
442
443 for ( ; i<lr->lr_lines; i++ ) {
444 if ( expect_modop ) {
445 #ifdef LIBERAL_CHANGETYPE_MODOP
446 /* trim trailing spaces (and log warning ...) */
447 int icnt;
448 for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
449 if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) break;
450 }
451
452 if ( ++icnt != lr->lr_vals[i].bv_len ) {
453 fprintf( stderr, _("%s: illegal trailing space after"
454 " \"%s: %s\" trimmed (line %lu, entry \"%s\")\n"),
455 errstr, type, lr->lr_vals[i].bv_val, linenum+i, dn );
456 lr->lr_vals[i].bv_val[icnt] = '\0';
457 }
458 #endif /* LIBERAL_CHANGETYPE_MODOP */
459
460 expect_modop = 0;
461 expect_sep = 1;
462 if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPADD )) {
463 modop = LDAP_MOD_ADD;
464 lr->lr_mops[i] = M_SEP;
465 nmods--;
466 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPREPLACE )) {
467 /* defer handling these since they might have no values.
468 * Use the BVALUES flag to signal that these were
469 * deferred. If values are provided later, this
470 * flag will be switched off.
471 */
472 modop = LDAP_MOD_REPLACE;
473 lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
474 lr->lr_btype[i] = lr->lr_vals[i];
475 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPDELETE )) {
476 modop = LDAP_MOD_DELETE;
477 lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
478 lr->lr_btype[i] = lr->lr_vals[i];
479 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPINCREMENT )) {
480 modop = LDAP_MOD_INCREMENT;
481 lr->lr_mops[i] = M_SEP;
482 nmods--;
483 } else { /* no modify op: invalid LDIF */
484 fprintf( stderr, _("%s: modify operation type is missing at"
485 " line %lu, entry \"%s\"\n"),
486 errstr, linenum+i, dn );
487 rc = LDAP_PARAM_ERROR;
488 goto leave;
489 }
490 bv = lr->lr_vals[i];
491 } else if ( expect_sep && BER_BVISEMPTY( lr->lr_btype+i )) {
492 lr->lr_mops[i] = M_SEP;
493 expect_sep = 0;
494 expect_modop = 1;
495 nmods--;
496 } else {
497 if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
498 fprintf( stderr, _("%s: wrong attributeType at"
499 " line %lu, entry \"%s\"\n"),
500 errstr, linenum+i, dn );
501 rc = LDAP_PARAM_ERROR;
502 goto leave;
503 }
504 lr->lr_mops[i] = modop;
505 /* If prev op was deferred and matches this type,
506 * clear the flag
507 */
508 if ( (lr->lr_mops[i-1] & LDAP_MOD_BVALUES)
509 && BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+i-1 ))
510 {
511 lr->lr_mops[i-1] = M_SEP;
512 nmods--;
513 }
514 }
515 }
516
517 /* Allocate space for array of mods, array of pointers to mods,
518 * and array of pointers to values, allowing for NULL terminators
519 * for the pointer arrays...
520 */
521 lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
522 (nmods+1) * sizeof(LDAPMod*) +
523 (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
524 if ( lr->lr_lm == NULL ) {
525 rc = LDAP_NO_MEMORY;
526 goto leave;
527 }
528
529 pmods = (LDAPMod **)(lr->lr_lm+nmods);
530 bvl = (struct berval **)(pmods+nmods+1);
531
532 j = 0;
533 k = -1;
534 BER_BVZERO(&bv);
535 if ( idn > 0 )
536 lr->lr_mops[idn-1] = M_SEP;
537 for (i=idn; i<lr->lr_lines; i++) {
538 if ( lr->lr_mops[i] == M_SEP )
539 continue;
540 if ( lr->lr_mops[i] != lr->lr_mops[i-1] || !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
541 bvl[k++] = NULL;
542 bv = lr->lr_btype[i];
543 lr->lr_lm[j].mod_op = lr->lr_mops[i] | LDAP_MOD_BVALUES;
544 lr->lr_lm[j].mod_type = bv.bv_val;
545 if ( lr->lr_mops[i] & LDAP_MOD_BVALUES ) {
546 lr->lr_lm[j].mod_bvalues = NULL;
547 } else {
548 lr->lr_lm[j].mod_bvalues = bvl+k;
549 }
550 pmods[j] = lr->lr_lm+j;
551 j++;
552 }
553 bvl[k++] = lr->lr_vals+i;
554 }
555 bvl[k] = NULL;
556 pmods[j] = NULL;
557
558 doit:
559 /* first, set the common fields */
560 lr->lr_ctrls = pctrls;
561 /* next, set the op */
562 if ( delete_entry ) {
563 lr->lr_op = LDAP_REQ_DELETE;
564 } else if ( lr->lrop_newrdn.bv_val != NULL ) {
565 lr->lr_op = LDAP_REQ_MODDN;
566 } else {
567 /* for now, either add or modify */
568 lr->lrop_mods = pmods;
569 if ( new_entry ) {
570 lr->lr_op = LDAP_REQ_ADD;
571 } else {
572 lr->lr_op = LDAP_REQ_MODIFY;
573 }
574 }
575
576 leave:
577 if ( rc != LDAP_SUCCESS ) {
578 ldap_ldif_record_done( lr );
579 }
580
581 return( rc );
582 }
583
584 /* Same as ldap_parse_ldif_record_x()
585 * public API does not expose memory context
586 */
587 int
ldap_parse_ldif_record(struct berval * rbuf,unsigned long linenum,LDIFRecord * lr,const char * errstr,unsigned int flags)588 ldap_parse_ldif_record(
589 struct berval *rbuf,
590 unsigned long linenum,
591 LDIFRecord *lr,
592 const char *errstr,
593 unsigned int flags )
594 {
595 return ldap_parse_ldif_record_x( rbuf, linenum, lr, errstr, flags, NULL );
596 }
597
598 /* Parse an LDIF control line of the form
599 control: oid [true/false] [: value] or
600 control: oid [true/false] [:: base64-value] or
601 control: oid [true/false] [:< url]
602 The control is added to the list of controls in *ppctrls.
603 */
604 static int
parse_ldif_control(struct berval * bval,LDAPControl *** ppctrls)605 parse_ldif_control(
606 struct berval *bval,
607 LDAPControl ***ppctrls)
608 {
609 char *oid = NULL;
610 int criticality = 0; /* Default is false if not present */
611 int i, rc=0;
612 char *s, *oidStart;
613 LDAPControl *newctrl = NULL;
614 LDAPControl **pctrls = NULL;
615 struct berval type, bv = BER_BVNULL;
616 int freeval = 0;
617
618 if (ppctrls) pctrls = *ppctrls;
619 /* OID should come first. Validate and extract it. */
620 s = bval->bv_val;
621 if (*s == 0) return ( LDAP_PARAM_ERROR );
622 oidStart = s;
623 while (isdigit((unsigned char)*s) || *s == '.') {
624 s++; /* OID should be digits or . */
625 }
626 if (s == oidStart) {
627 return ( LDAP_PARAM_ERROR ); /* OID was not present */
628 }
629 if (*s) { /* End of OID should be space or NULL */
630 if (!isspace((unsigned char)*s)) {
631 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
632 }
633 *s++ = 0; /* Replace space with null to terminate */
634 }
635
636 oid = ber_strdup(oidStart);
637 if (oid == NULL) return ( LDAP_NO_MEMORY );
638
639 /* Optional Criticality field is next. */
640 while (*s && isspace((unsigned char)*s)) {
641 s++; /* Skip white space before criticality */
642 }
643 if (strncasecmp(s, "true", 4) == 0) {
644 criticality = 1;
645 s += 4;
646 }
647 else if (strncasecmp(s, "false", 5) == 0) {
648 criticality = 0;
649 s += 5;
650 }
651
652 /* Optional value field is next */
653 while (*s && isspace((unsigned char)*s)) {
654 s++; /* Skip white space before value */
655 }
656 if (*s) {
657 if (*s != ':') { /* If value is present, must start with : */
658 rc = LDAP_PARAM_ERROR;
659 goto cleanup;
660 }
661
662 /* Back up so value is in the form
663 a: value
664 a:: base64-value
665 a:< url
666 Then we can use ldif_parse_line2 to extract and decode the value
667 */
668 s--;
669 *s = 'a';
670
671 rc = ldif_parse_line2(s, &type, &bv, &freeval);
672 if (rc < 0) {
673 rc = LDAP_PARAM_ERROR;
674 goto cleanup;
675 }
676 }
677
678 /* Create a new LDAPControl structure. */
679 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
680 if ( newctrl == NULL ) {
681 rc = LDAP_NO_MEMORY;
682 goto cleanup;
683 }
684 newctrl->ldctl_oid = oid;
685 oid = NULL;
686 newctrl->ldctl_iscritical = criticality;
687 if ( freeval )
688 newctrl->ldctl_value = bv;
689 else
690 ber_dupbv( &newctrl->ldctl_value, &bv );
691
692 /* Add the new control to the passed-in list of controls. */
693 i = 0;
694 if (pctrls) {
695 while ( pctrls[i] ) { /* Count the # of controls passed in */
696 i++;
697 }
698 }
699 /* Allocate 1 more slot for the new control and 1 for the NULL. */
700 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
701 (i+2)*(sizeof(LDAPControl *)));
702 if (pctrls == NULL) {
703 rc = LDAP_NO_MEMORY;
704 goto cleanup;
705 }
706 pctrls[i] = newctrl;
707 newctrl = NULL;
708 pctrls[i+1] = NULL;
709 *ppctrls = pctrls;
710
711 cleanup:
712 if (newctrl) {
713 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
714 if (newctrl->ldctl_value.bv_val) {
715 ber_memfree(newctrl->ldctl_value.bv_val);
716 }
717 ber_memfree(newctrl);
718 }
719 if (oid) ber_memfree(oid);
720
721 return( rc );
722 }
723
724
725