2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
20 * This file contains public API to help with parsing LDIF
27 #include <ac/stdlib.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
31 #include <ac/socket.h>
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");
58 #define BV_CASEMATCH(a, b) \
59 ((a)->bv_len == (b)->bv_len && 0 == strcasecmp((a)->bv_val, (b)->bv_val))
61 static int parse_ldif_control LDAP_P(( struct berval *bval, LDAPControl ***ppctrls ));
64 ldap_ldif_record_done( LDIFRecord *lr )
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 );
72 if ( lr->lr_lm != NULL ) {
73 ber_memfree_x( lr->lr_lm, lr->lr_ctx );
75 if ( lr->lr_mops != NULL ) {
76 ber_memfree_x( lr->lr_mops, lr->lr_ctx );
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 );
82 memset( lr, 0, sizeof(LDIFRecord) );
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().
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
101 ldap_parse_ldif_record_x(
111 int expect_modop, expect_sep;
112 int ldapadd, new_entry, delete_entry, got_all;
115 LDAPControl **pctrls;
116 int i, j, k, idn, nmods;
117 struct berval **bvl, bv;
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;
126 rc = got_all = delete_entry = modop = expect_modop = 0;
133 lr->lr_lines = ldif_countlines( rbuf->bv_val );
134 lr->lr_btype = ber_memcalloc_x( 1, (lr->lr_lines+1)*2*sizeof(struct berval)+lr->lr_lines, ctx );
136 return LDAP_NO_MEMORY;
138 lr->lr_vals = lr->lr_btype+lr->lr_lines+1;
139 lr->lr_freeval = (char *)(lr->lr_vals+lr->lr_lines+1);
142 while ( rc == 0 && ( line = ldif_getline( &rbuf->bv_val )) != NULL ) {
145 if ( *line == '\n' || *line == '\0' ) {
151 if ( line[0] == '-' && !line[1] ) {
152 BER_BVZERO( lr->lr_btype+i );
153 lr->lr_freeval[i] = 0;
157 if ( ( rc = ldif_parse_line2( line, lr->lr_btype+i, lr->lr_vals+i, &freev ) ) < 0 ) {
158 fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
159 errstr, linenum+i, dn == NULL ? "" : dn );
160 rc = LDAP_PARAM_ERROR;
163 lr->lr_freeval[i] = freev;
166 if ( linenum+i == 1 && BV_CASEMATCH( lr->lr_btype+i, &BV_VERSION )) {
167 /* lutil_atoi() introduces a dependence of libldap
168 * on liblutil; we only allow version 1 by now (ITS#6654)
172 if( lr->lr_vals[i].bv_len == 0 || lutil_atoi( &v, lr->lr_vals[i].bv_val) != 0 || v != 1 )
174 static const struct berval version1 = { 1, "1" };
175 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 _("%s: invalid version %s, line %d (ignored)\n"),
179 errstr, lr->lr_vals[i].bv_val, linenum );
183 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) {
184 lr->lr_dn = lr->lr_vals[i];
185 dn = lr->lr_dn.bv_val; /* primarily for logging */
188 /* skip all lines until we see "dn:" */
192 /* check to make sure there was a dn: line */
200 if( lr->lr_lines == 0 ) {
205 if( version && lr->lr_lines == 1 ) {
211 /* Check for "control" tag after dn and before changetype. */
212 if ( BV_CASEMATCH( lr->lr_btype+i, &BV_CONTROL )) {
213 /* Parse and add it to the list of controls */
214 if ( !( flags & LDIF_NO_CONTROLS ) ) {
215 rc = parse_ldif_control( lr->lr_vals+i, &pctrls );
218 _("%s: Error processing %s line, line %d: %s\n"),
219 errstr, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
223 if ( i>= lr->lr_lines ) {
226 _("%s: Expecting more input after %s line, line %d\n"),
227 errstr, lr->lr_btype[i-1].bv_val, linenum+i );
229 rc = LDAP_PARAM_ERROR;
234 /* Check for changetype */
235 if ( BV_CASEMATCH( lr->lr_btype+i, &BV_CHANGETYPE )) {
236 #ifdef LIBERAL_CHANGETYPE_MODOP
237 /* trim trailing spaces (and log warning ...) */
239 for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
240 if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) {
245 if ( ++icnt != lr->lr_vals[i].bv_len ) {
246 fprintf( stderr, _("%s: illegal trailing space after"
247 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
248 errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn );
249 lr->lr_vals[i].bv_val[icnt] = '\0';
251 #endif /* LIBERAL_CHANGETYPE_MODOP */
253 /* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or
254 there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */
255 if ( flags & LDIF_ENTRIES_ONLY ) {
256 if ( !( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) ) {
257 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
258 _("%s: skipping LDIF record beginning at line %d: "
259 "changetype '%.*s' found but entries only was requested\n"),
261 (int)lr->lr_vals[i].bv_len,
262 (const char *)lr->lr_vals[i].bv_val );
267 if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODIFYCT )) {
270 } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) {
272 modop = LDAP_MOD_ADD;
273 } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODRDNCT )
274 || BV_CASEMATCH( lr->lr_vals+i, &BV_MODDNCT )
275 || BV_CASEMATCH( lr->lr_vals+i, &BV_RENAMECT ))
278 if ( i >= lr->lr_lines )
280 if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWRDN )) {
281 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
282 " \"%s:\" (line %d, entry \"%s\")\n"),
283 errstr, BV_NEWRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
284 rc = LDAP_PARAM_ERROR;
287 lr->lrop_newrdn = lr->lr_vals[i];
289 if ( i >= lr->lr_lines )
291 if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_DELETEOLDRDN )) {
292 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
293 " \"%s:\" (line %d, entry \"%s\")\n"),
294 errstr, BV_DELETEOLDRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
295 rc = LDAP_PARAM_ERROR;
298 lr->lrop_delold = ( lr->lr_vals[i].bv_val[0] == '0' ) ? 0 : 1;
300 if ( i < lr->lr_lines ) {
301 if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWSUP )) {
302 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
303 " \"%s:\" (line %d, entry \"%s\")\n"),
304 errstr, BV_NEWSUP.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
305 rc = LDAP_PARAM_ERROR;
308 lr->lrop_newsup = lr->lr_vals[i];
312 } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_DELETECT )) {
313 got_all = delete_entry = 1;
316 _("%s: unknown %s \"%s\" (line %d, entry \"%s\")\n"),
317 errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn );
318 rc = LDAP_PARAM_ERROR;
322 } else if ( ldapadd ) { /* missing changetype => add */
324 modop = LDAP_MOD_ADD;
326 /* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or
327 there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */
328 if ( flags & LDIF_ENTRIES_ONLY ) {
329 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
330 _("%s: skipping LDIF record beginning at line %d: "
331 "no changetype found but entries only was requested and "
332 "the default setting for missing changetype is modify\n"),
336 expect_modop = 1; /* missing changetype => modify */
340 if ( i < lr->lr_lines ) {
342 _("%s: extra lines at end (line %d, entry \"%s\")\n"),
343 errstr, linenum+i, dn );
344 rc = LDAP_PARAM_ERROR;
350 nmods = lr->lr_lines - i;
356 /* Make sure all attributes with multiple values are contiguous */
357 for (; i<lr->lr_lines; i++) {
358 for (j=i+1; j<lr->lr_lines; j++) {
359 if ( BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+j )) {
361 /* out of order, move intervening attributes down */
364 fv = lr->lr_freeval[j];
365 for (k=j; k>i; k--) {
366 lr->lr_btype[k] = lr->lr_btype[k-1];
367 lr->lr_vals[k] = lr->lr_vals[k-1];
368 lr->lr_freeval[k] = lr->lr_freeval[k-1];
371 lr->lr_btype[k] = lr->lr_btype[i];
373 lr->lr_freeval[k] = fv;
379 /* Allocate space for array of mods, array of pointers to mods,
380 * and array of pointers to values, allowing for NULL terminators
381 * for the pointer arrays...
383 lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
384 (nmods+1) * sizeof(LDAPMod*) +
385 (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
386 pmods = (LDAPMod **)(lr->lr_lm+nmods);
387 bvl = (struct berval **)(pmods+nmods+1);
392 for (i=idn; i<lr->lr_lines; i++) {
393 if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) {
394 fprintf( stderr, _("%s: attributeDescription \"%s\":"
395 " (possible missing newline"
396 " after line %d, entry \"%s\"?)\n"),
397 errstr, lr->lr_btype[i].bv_val, linenum+i - 1, dn );
399 if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
401 bv = lr->lr_btype[i];
402 lr->lr_lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
403 lr->lr_lm[j].mod_type = bv.bv_val;
404 lr->lr_lm[j].mod_bvalues = bvl+k;
405 pmods[j] = lr->lr_lm+j;
408 bvl[k++] = lr->lr_vals+i;
415 lr->lr_mops = ber_memalloc_x( lr->lr_lines+1, ctx );
416 lr->lr_mops[lr->lr_lines] = M_SEP;
417 lr->lr_mops[i-1] = M_SEP;
419 for ( ; i<lr->lr_lines; i++ ) {
420 if ( expect_modop ) {
421 #ifdef LIBERAL_CHANGETYPE_MODOP
422 /* trim trailing spaces (and log warning ...) */
424 for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
425 if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) break;
428 if ( ++icnt != lr->lr_vals[i].bv_len ) {
429 fprintf( stderr, _("%s: illegal trailing space after"
430 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
431 errstr, type, lr->lr_vals[i].bv_val, linenum+i, dn );
432 lr->lr_vals[i].bv_val[icnt] = '\0';
434 #endif /* LIBERAL_CHANGETYPE_MODOP */
438 if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPADD )) {
439 modop = LDAP_MOD_ADD;
440 lr->lr_mops[i] = M_SEP;
442 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPREPLACE )) {
443 /* defer handling these since they might have no values.
444 * Use the BVALUES flag to signal that these were
445 * deferred. If values are provided later, this
446 * flag will be switched off.
448 modop = LDAP_MOD_REPLACE;
449 lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
450 lr->lr_btype[i] = lr->lr_vals[i];
451 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPDELETE )) {
452 modop = LDAP_MOD_DELETE;
453 lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
454 lr->lr_btype[i] = lr->lr_vals[i];
455 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPINCREMENT )) {
456 modop = LDAP_MOD_INCREMENT;
457 lr->lr_mops[i] = M_SEP;
459 } else { /* no modify op: invalid LDIF */
460 fprintf( stderr, _("%s: modify operation type is missing at"
461 " line %d, entry \"%s\"\n"),
462 errstr, linenum+i, dn );
463 rc = LDAP_PARAM_ERROR;
467 } else if ( expect_sep && BER_BVISEMPTY( lr->lr_btype+i )) {
468 lr->lr_mops[i] = M_SEP;
473 if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
474 fprintf( stderr, _("%s: wrong attributeType at"
475 " line %d, entry \"%s\"\n"),
476 errstr, linenum+i, dn );
477 rc = LDAP_PARAM_ERROR;
480 lr->lr_mops[i] = modop;
481 /* If prev op was deferred and matches this type,
484 if ( (lr->lr_mops[i-1] & LDAP_MOD_BVALUES)
485 && BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+i-1 ))
487 lr->lr_mops[i-1] = M_SEP;
493 /* Allocate space for array of mods, array of pointers to mods,
494 * and array of pointers to values, allowing for NULL terminators
495 * for the pointer arrays...
497 lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
498 (nmods+1) * sizeof(LDAPMod*) +
499 (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
500 pmods = (LDAPMod **)(lr->lr_lm+nmods);
501 bvl = (struct berval **)(pmods+nmods+1);
506 lr->lr_mops[idn-1] = M_SEP;
507 for (i=idn; i<lr->lr_lines; i++) {
508 if ( lr->lr_mops[i] == M_SEP )
510 if ( lr->lr_mops[i] != lr->lr_mops[i-1] || !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
512 bv = lr->lr_btype[i];
513 lr->lr_lm[j].mod_op = lr->lr_mops[i] | LDAP_MOD_BVALUES;
514 lr->lr_lm[j].mod_type = bv.bv_val;
515 if ( lr->lr_mops[i] & LDAP_MOD_BVALUES ) {
516 lr->lr_lm[j].mod_bvalues = NULL;
518 lr->lr_lm[j].mod_bvalues = bvl+k;
520 pmods[j] = lr->lr_lm+j;
523 bvl[k++] = lr->lr_vals+i;
529 /* first, set the common fields */
530 lr->lr_ctrls = pctrls;
531 /* next, set the op */
532 if ( delete_entry ) {
533 lr->lr_op = LDAP_REQ_DELETE;
534 } else if ( lr->lrop_newrdn.bv_val != NULL ) {
535 lr->lr_op = LDAP_REQ_MODDN;
537 /* for now, either add or modify */
538 lr->lrop_mods = pmods;
540 lr->lr_op = LDAP_REQ_ADD;
542 lr->lr_op = LDAP_REQ_MODIFY;
547 if ( rc != LDAP_SUCCESS ) {
548 ldap_ldif_record_done( lr );
554 /* Same as ldap_parse_ldif_record_x()
555 * public API does not expose memory context
558 ldap_parse_ldif_record(
565 return ldap_parse_ldif_record_x( rbuf, linenum, lr, errstr, flags, NULL );
568 /* Parse an LDIF control line of the form
569 control: oid [true/false] [: value] or
570 control: oid [true/false] [:: base64-value] or
571 control: oid [true/false] [:< url]
572 The control is added to the list of controls in *ppctrls.
577 LDAPControl ***ppctrls)
580 int criticality = 0; /* Default is false if not present */
583 LDAPControl *newctrl = NULL;
584 LDAPControl **pctrls = NULL;
585 struct berval type, bv = BER_BVNULL;
588 if (ppctrls) pctrls = *ppctrls;
589 /* OID should come first. Validate and extract it. */
591 if (*s == 0) return ( LDAP_PARAM_ERROR );
593 while (isdigit((unsigned char)*s) || *s == '.') {
594 s++; /* OID should be digits or . */
597 return ( LDAP_PARAM_ERROR ); /* OID was not present */
599 if (*s) { /* End of OID should be space or NULL */
600 if (!isspace((unsigned char)*s)) {
601 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
603 *s++ = 0; /* Replace space with null to terminate */
606 oid = ber_strdup(oidStart);
607 if (oid == NULL) return ( LDAP_NO_MEMORY );
609 /* Optional Criticality field is next. */
610 while (*s && isspace((unsigned char)*s)) {
611 s++; /* Skip white space before criticality */
613 if (strncasecmp(s, "true", 4) == 0) {
617 else if (strncasecmp(s, "false", 5) == 0) {
622 /* Optional value field is next */
623 while (*s && isspace((unsigned char)*s)) {
624 s++; /* Skip white space before value */
627 if (*s != ':') { /* If value is present, must start with : */
628 rc = LDAP_PARAM_ERROR;
632 /* Back up so value is in the form
636 Then we can use ldif_parse_line2 to extract and decode the value
641 rc = ldif_parse_line2(s, &type, &bv, &freeval);
643 rc = LDAP_PARAM_ERROR;
648 /* Create a new LDAPControl structure. */
649 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
650 if ( newctrl == NULL ) {
654 newctrl->ldctl_oid = oid;
656 newctrl->ldctl_iscritical = criticality;
658 newctrl->ldctl_value = bv;
660 ber_dupbv( &newctrl->ldctl_value, &bv );
662 /* Add the new control to the passed-in list of controls. */
665 while ( pctrls[i] ) { /* Count the # of controls passed in */
669 /* Allocate 1 more slot for the new control and 1 for the NULL. */
670 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
671 (i+2)*(sizeof(LDAPControl *)));
672 if (pctrls == NULL) {
683 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
684 if (newctrl->ldctl_value.bv_val) {
685 ber_memfree(newctrl->ldctl_value.bv_val);
687 ber_memfree(newctrl);
689 if (oid) ber_memfree(oid);