2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 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(
103 unsigned long linenum,
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 %lu) 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 %lu (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 %lu: %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 %lu\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 %lu, 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 %lu: "
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 %lu, 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 %lu, 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 %lu, 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 %lu, 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 %lu: "
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 %lu, 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 ( !lr->lr_btype[j].bv_val ) {
361 _("%s: missing attributeDescription (line %lu, entry \"%s\")\n"),
362 errstr, linenum+j, dn );
363 rc = LDAP_PARAM_ERROR;
366 if ( BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+j )) {
368 /* out of order, move intervening attributes down */
371 fv = lr->lr_freeval[j];
372 for (k=j; k>i; k--) {
373 lr->lr_btype[k] = lr->lr_btype[k-1];
374 lr->lr_vals[k] = lr->lr_vals[k-1];
375 lr->lr_freeval[k] = lr->lr_freeval[k-1];
378 lr->lr_btype[k] = lr->lr_btype[i];
380 lr->lr_freeval[k] = fv;
386 /* Allocate space for array of mods, array of pointers to mods,
387 * and array of pointers to values, allowing for NULL terminators
388 * for the pointer arrays...
390 lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
391 (nmods+1) * sizeof(LDAPMod*) +
392 (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
393 pmods = (LDAPMod **)(lr->lr_lm+nmods);
394 bvl = (struct berval **)(pmods+nmods+1);
399 for (i=idn; i<lr->lr_lines; i++) {
400 if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) {
401 fprintf( stderr, _("%s: attributeDescription \"%s\":"
402 " (possible missing newline"
403 " after line %lu, entry \"%s\"?)\n"),
404 errstr, lr->lr_btype[i].bv_val, linenum+i - 1, dn );
406 if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
408 bv = lr->lr_btype[i];
409 lr->lr_lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
410 lr->lr_lm[j].mod_type = bv.bv_val;
411 lr->lr_lm[j].mod_bvalues = bvl+k;
412 pmods[j] = lr->lr_lm+j;
415 bvl[k++] = lr->lr_vals+i;
422 lr->lr_mops = ber_memalloc_x( lr->lr_lines+1, ctx );
423 lr->lr_mops[lr->lr_lines] = M_SEP;
424 lr->lr_mops[i-1] = M_SEP;
426 for ( ; i<lr->lr_lines; i++ ) {
427 if ( expect_modop ) {
428 #ifdef LIBERAL_CHANGETYPE_MODOP
429 /* trim trailing spaces (and log warning ...) */
431 for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
432 if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) break;
435 if ( ++icnt != lr->lr_vals[i].bv_len ) {
436 fprintf( stderr, _("%s: illegal trailing space after"
437 " \"%s: %s\" trimmed (line %lu, entry \"%s\")\n"),
438 errstr, type, lr->lr_vals[i].bv_val, linenum+i, dn );
439 lr->lr_vals[i].bv_val[icnt] = '\0';
441 #endif /* LIBERAL_CHANGETYPE_MODOP */
445 if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPADD )) {
446 modop = LDAP_MOD_ADD;
447 lr->lr_mops[i] = M_SEP;
449 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPREPLACE )) {
450 /* defer handling these since they might have no values.
451 * Use the BVALUES flag to signal that these were
452 * deferred. If values are provided later, this
453 * flag will be switched off.
455 modop = LDAP_MOD_REPLACE;
456 lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
457 lr->lr_btype[i] = lr->lr_vals[i];
458 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPDELETE )) {
459 modop = LDAP_MOD_DELETE;
460 lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
461 lr->lr_btype[i] = lr->lr_vals[i];
462 } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPINCREMENT )) {
463 modop = LDAP_MOD_INCREMENT;
464 lr->lr_mops[i] = M_SEP;
466 } else { /* no modify op: invalid LDIF */
467 fprintf( stderr, _("%s: modify operation type is missing at"
468 " line %lu, entry \"%s\"\n"),
469 errstr, linenum+i, dn );
470 rc = LDAP_PARAM_ERROR;
474 } else if ( expect_sep && BER_BVISEMPTY( lr->lr_btype+i )) {
475 lr->lr_mops[i] = M_SEP;
480 if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
481 fprintf( stderr, _("%s: wrong attributeType at"
482 " line %lu, entry \"%s\"\n"),
483 errstr, linenum+i, dn );
484 rc = LDAP_PARAM_ERROR;
487 lr->lr_mops[i] = modop;
488 /* If prev op was deferred and matches this type,
491 if ( (lr->lr_mops[i-1] & LDAP_MOD_BVALUES)
492 && BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+i-1 ))
494 lr->lr_mops[i-1] = M_SEP;
500 /* Allocate space for array of mods, array of pointers to mods,
501 * and array of pointers to values, allowing for NULL terminators
502 * for the pointer arrays...
504 lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
505 (nmods+1) * sizeof(LDAPMod*) +
506 (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
507 pmods = (LDAPMod **)(lr->lr_lm+nmods);
508 bvl = (struct berval **)(pmods+nmods+1);
513 lr->lr_mops[idn-1] = M_SEP;
514 for (i=idn; i<lr->lr_lines; i++) {
515 if ( lr->lr_mops[i] == M_SEP )
517 if ( lr->lr_mops[i] != lr->lr_mops[i-1] || !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
519 bv = lr->lr_btype[i];
520 lr->lr_lm[j].mod_op = lr->lr_mops[i] | LDAP_MOD_BVALUES;
521 lr->lr_lm[j].mod_type = bv.bv_val;
522 if ( lr->lr_mops[i] & LDAP_MOD_BVALUES ) {
523 lr->lr_lm[j].mod_bvalues = NULL;
525 lr->lr_lm[j].mod_bvalues = bvl+k;
527 pmods[j] = lr->lr_lm+j;
530 bvl[k++] = lr->lr_vals+i;
536 /* first, set the common fields */
537 lr->lr_ctrls = pctrls;
538 /* next, set the op */
539 if ( delete_entry ) {
540 lr->lr_op = LDAP_REQ_DELETE;
541 } else if ( lr->lrop_newrdn.bv_val != NULL ) {
542 lr->lr_op = LDAP_REQ_MODDN;
544 /* for now, either add or modify */
545 lr->lrop_mods = pmods;
547 lr->lr_op = LDAP_REQ_ADD;
549 lr->lr_op = LDAP_REQ_MODIFY;
554 if ( rc != LDAP_SUCCESS ) {
555 ldap_ldif_record_done( lr );
561 /* Same as ldap_parse_ldif_record_x()
562 * public API does not expose memory context
565 ldap_parse_ldif_record(
567 unsigned long linenum,
572 return ldap_parse_ldif_record_x( rbuf, linenum, lr, errstr, flags, NULL );
575 /* Parse an LDIF control line of the form
576 control: oid [true/false] [: value] or
577 control: oid [true/false] [:: base64-value] or
578 control: oid [true/false] [:< url]
579 The control is added to the list of controls in *ppctrls.
584 LDAPControl ***ppctrls)
587 int criticality = 0; /* Default is false if not present */
590 LDAPControl *newctrl = NULL;
591 LDAPControl **pctrls = NULL;
592 struct berval type, bv = BER_BVNULL;
595 if (ppctrls) pctrls = *ppctrls;
596 /* OID should come first. Validate and extract it. */
598 if (*s == 0) return ( LDAP_PARAM_ERROR );
600 while (isdigit((unsigned char)*s) || *s == '.') {
601 s++; /* OID should be digits or . */
604 return ( LDAP_PARAM_ERROR ); /* OID was not present */
606 if (*s) { /* End of OID should be space or NULL */
607 if (!isspace((unsigned char)*s)) {
608 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
610 *s++ = 0; /* Replace space with null to terminate */
613 oid = ber_strdup(oidStart);
614 if (oid == NULL) return ( LDAP_NO_MEMORY );
616 /* Optional Criticality field is next. */
617 while (*s && isspace((unsigned char)*s)) {
618 s++; /* Skip white space before criticality */
620 if (strncasecmp(s, "true", 4) == 0) {
624 else if (strncasecmp(s, "false", 5) == 0) {
629 /* Optional value field is next */
630 while (*s && isspace((unsigned char)*s)) {
631 s++; /* Skip white space before value */
634 if (*s != ':') { /* If value is present, must start with : */
635 rc = LDAP_PARAM_ERROR;
639 /* Back up so value is in the form
643 Then we can use ldif_parse_line2 to extract and decode the value
648 rc = ldif_parse_line2(s, &type, &bv, &freeval);
650 rc = LDAP_PARAM_ERROR;
655 /* Create a new LDAPControl structure. */
656 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
657 if ( newctrl == NULL ) {
661 newctrl->ldctl_oid = oid;
663 newctrl->ldctl_iscritical = criticality;
665 newctrl->ldctl_value = bv;
667 ber_dupbv( &newctrl->ldctl_value, &bv );
669 /* Add the new control to the passed-in list of controls. */
672 while ( pctrls[i] ) { /* Count the # of controls passed in */
676 /* Allocate 1 more slot for the new control and 1 for the NULL. */
677 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
678 (i+2)*(sizeof(LDAPControl *)));
679 if (pctrls == NULL) {
690 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
691 if (newctrl->ldctl_value.bv_val) {
692 ber_memfree(newctrl->ldctl_value.bv_val);
694 ber_memfree(newctrl);
696 if (oid) ber_memfree(oid);