]> git.sur5r.net Git - openldap/blob - libraries/libldap/ldifutil.c
2c58f638127450fc3c78eb1f5e4aed9edbf67945
[openldap] / libraries / libldap / ldifutil.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2017 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
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
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;
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         new_entry = ldapadd;
125
126         rc = got_all = delete_entry = modop = expect_modop = 0;
127         expect_sep = 0;
128         version = 0;
129         pmods = NULL;
130         pctrls = NULL;
131         dn = NULL;
132
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 );
135         if ( !lr->lr_btype )
136                 return LDAP_NO_MEMORY;
137
138         lr->lr_vals = lr->lr_btype+lr->lr_lines+1;
139         lr->lr_freeval = (char *)(lr->lr_vals+lr->lr_lines+1);
140         i = -1;
141
142         while ( rc == 0 && ( line = ldif_getline( &rbuf->bv_val )) != NULL ) {
143                 int freev;
144
145                 if ( *line == '\n' || *line == '\0' ) {
146                         break;
147                 }
148
149                 ++i;
150
151                 if ( line[0] == '-' && !line[1] ) {
152                         BER_BVZERO( lr->lr_btype+i );
153                         lr->lr_freeval[i] = 0;
154                         continue;
155                 }
156         
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;
161                         goto leave;
162                 }
163                 lr->lr_freeval[i] = freev;
164
165                 if ( dn == NULL ) {
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)
169                                  */
170 #if 0
171                                 int     v;
172                                 if( lr->lr_vals[i].bv_len == 0 || lutil_atoi( &v, lr->lr_vals[i].bv_val) != 0 || v != 1 )
173 #endif
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 )
176                                 {
177                                         fprintf( stderr,
178                                                 _("%s: invalid version %s, line %lu (ignored)\n"),
179                                                 errstr, lr->lr_vals[i].bv_val, linenum );
180                                 }
181                                 version++;
182
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 */
186                                 idn = i;
187                         }
188                         /* skip all lines until we see "dn:" */
189                 }
190         }
191
192         /* check to make sure there was a dn: line */
193         if ( !dn ) {
194                 rc = 0;
195                 goto leave;
196         }
197
198         lr->lr_lines = i+1;
199
200         if( lr->lr_lines == 0 ) {
201                 rc = 0;
202                 goto leave;
203         }
204
205         if( version && lr->lr_lines == 1 ) {
206                 rc = 0;
207                 goto leave;
208         }
209
210         i = idn+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 );
216                         if (rc != 0) {
217                                 fprintf( stderr,
218                                                  _("%s: Error processing %s line, line %lu: %s\n"),
219                                                  errstr, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
220                         }
221                 }
222                 i++;
223                 if ( i>= lr->lr_lines ) {
224 short_input:
225                         fprintf( stderr,
226                                 _("%s: Expecting more input after %s line, line %lu\n"),
227                                 errstr, lr->lr_btype[i-1].bv_val, linenum+i );
228                         
229                         rc = LDAP_PARAM_ERROR;
230                         goto leave;
231                 }
232         }
233
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 ...) */
238                 int icnt;
239                 for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
240                         if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) {
241                                 break;
242                         }
243                 }
244
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';
250                 }
251 #endif /* LIBERAL_CHANGETYPE_MODOP */
252
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"),
260                                                                         errstr, linenum,
261                                                                         (int)lr->lr_vals[i].bv_len,
262                                                                         (const char *)lr->lr_vals[i].bv_val );
263                                 goto leave;
264                         }
265                 }
266
267                 if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODIFYCT )) {
268                         new_entry = 0;
269                         expect_modop = 1;
270                 } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) {
271                         new_entry = 1;
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 ))
276                 {
277                         i++;
278                         if ( i >= lr->lr_lines )
279                                 goto short_input;
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;
285                                 goto leave;
286                         }
287                         lr->lrop_newrdn = lr->lr_vals[i];
288                         i++;
289                         if ( i >= lr->lr_lines )
290                                 goto short_input;
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;
296                                 goto leave;
297                         }
298                         lr->lrop_delold = ( lr->lr_vals[i].bv_val[0] == '0' ) ? 0 : 1;
299                         i++;
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;
306                                         goto leave;
307                                 }
308                                 lr->lrop_newsup = lr->lr_vals[i];
309                                 i++;
310                         }
311                         got_all = 1;
312                 } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_DELETECT )) {
313                         got_all = delete_entry = 1;
314                 } else {
315                         fprintf( stderr,
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;
319                         goto leave;
320                 }
321                 i++;
322         } else if ( ldapadd ) {         /*  missing changetype => add */
323                 new_entry = 1;
324                 modop = LDAP_MOD_ADD;
325         } else {
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"),
333                                                                 errstr, linenum );
334                         goto leave;
335                 }
336                 expect_modop = 1;       /* missing changetype => modify */
337         }
338
339         if ( got_all ) {
340                 if ( i < lr->lr_lines ) {
341                         fprintf( stderr,
342                                 _("%s: extra lines at end (line %lu, entry \"%s\")\n"),
343                                 errstr, linenum+i, dn );
344                         rc = LDAP_PARAM_ERROR;
345                         goto leave;
346                 }
347                 goto doit;
348         }
349
350         nmods = lr->lr_lines - i;
351         idn = i;
352
353         if ( new_entry ) {
354                 int fv;
355
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 ) {
360                                         fprintf( stderr,
361                                                 _("%s: missing attributeDescription (line %lu, entry \"%s\")\n"),
362                                                 errstr, linenum+j, dn );
363                                         rc = LDAP_PARAM_ERROR;
364                                         goto leave;
365                                 }
366                                 if ( BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+j )) {
367                                         nmods--;
368                                         /* out of order, move intervening attributes down */
369                                         if ( j != i+1 ) {
370                                                 bv = lr->lr_vals[j];
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];
376                                                 }
377                                                 k++;
378                                                 lr->lr_btype[k] = lr->lr_btype[i];
379                                                 lr->lr_vals[k] = bv;
380                                                 lr->lr_freeval[k] = fv;
381                                         }
382                                         i++;
383                                 }
384                         }
385                 }
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...
389                  */
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);
395
396                 j = 0;
397                 k = -1;
398                 BER_BVZERO(&bv);
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 );
405                         }
406                         if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
407                                 bvl[k++] = NULL;
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;
413                                 j++;
414                         }
415                         bvl[k++] = lr->lr_vals+i;
416                 }
417                 bvl[k] = NULL;
418                 pmods[j] = NULL;
419                 goto doit;
420         }
421
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;
425
426         for ( ; i<lr->lr_lines; i++ ) {
427                 if ( expect_modop ) {
428 #ifdef LIBERAL_CHANGETYPE_MODOP
429                         /* trim trailing spaces (and log warning ...) */
430                     int icnt;
431                     for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
432                                 if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) break;
433                         }
434     
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';
440                         }
441 #endif /* LIBERAL_CHANGETYPE_MODOP */
442
443                         expect_modop = 0;
444                         expect_sep = 1;
445                         if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPADD )) {
446                                 modop = LDAP_MOD_ADD;
447                                 lr->lr_mops[i] = M_SEP;
448                                 nmods--;
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.
454                          */
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;
465                                 nmods--;
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;
471                                 goto leave;
472                         }
473                         bv = lr->lr_vals[i];
474                 } else if ( expect_sep && BER_BVISEMPTY( lr->lr_btype+i )) {
475                         lr->lr_mops[i] = M_SEP;
476                         expect_sep = 0;
477                         expect_modop = 1;
478                         nmods--;
479                 } else {
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;
485                                 goto leave;
486                         }
487                         lr->lr_mops[i] = modop;
488                         /* If prev op was deferred and matches this type,
489                          * clear the flag
490                          */
491                         if ( (lr->lr_mops[i-1] & LDAP_MOD_BVALUES)
492                                 && BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+i-1 ))
493                         {
494                                 lr->lr_mops[i-1] = M_SEP;
495                                 nmods--;
496                         }
497                 }
498         }
499
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...
503          */
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);
509
510         j = 0;
511         k = -1;
512         BER_BVZERO(&bv);
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 )
516                         continue;
517                 if ( lr->lr_mops[i] != lr->lr_mops[i-1] || !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
518                         bvl[k++] = NULL;
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;
524                         } else {
525                                 lr->lr_lm[j].mod_bvalues = bvl+k;
526                         }
527                         pmods[j] = lr->lr_lm+j;
528                         j++;
529                 }
530                 bvl[k++] = lr->lr_vals+i;
531         }
532         bvl[k] = NULL;
533         pmods[j] = NULL;
534
535 doit:
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;
543         } else {
544                 /* for now, either add or modify */
545                 lr->lrop_mods = pmods;
546                 if ( new_entry ) {
547                         lr->lr_op = LDAP_REQ_ADD;
548                 } else {
549                         lr->lr_op = LDAP_REQ_MODIFY;
550                 }
551         }
552
553 leave:
554         if ( rc != LDAP_SUCCESS ) {
555                 ldap_ldif_record_done( lr );
556         }
557
558         return( rc );
559 }
560
561 /* Same as ldap_parse_ldif_record_x()
562  * public API does not expose memory context
563  */
564 int
565 ldap_parse_ldif_record(
566         struct berval *rbuf,
567         unsigned long linenum,
568         LDIFRecord *lr,
569         const char *errstr,
570         unsigned int flags )
571 {
572         return ldap_parse_ldif_record_x( rbuf, linenum, lr, errstr, flags, NULL );
573 }
574
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.
580 */      
581 static int
582 parse_ldif_control(
583         struct berval *bval,
584         LDAPControl ***ppctrls)
585 {
586         char *oid = NULL;
587         int criticality = 0;   /* Default is false if not present */
588         int i, rc=0;
589         char *s, *oidStart;
590         LDAPControl *newctrl = NULL;
591         LDAPControl **pctrls = NULL;
592         struct berval type, bv = BER_BVNULL;
593         int freeval = 0;
594
595         if (ppctrls) pctrls = *ppctrls;
596         /* OID should come first. Validate and extract it. */
597         s = bval->bv_val;
598         if (*s == 0) return ( LDAP_PARAM_ERROR );
599         oidStart = s;
600         while (isdigit((unsigned char)*s) || *s == '.') {
601                 s++;                           /* OID should be digits or . */
602         }
603         if (s == oidStart) { 
604                 return ( LDAP_PARAM_ERROR );   /* OID was not present */
605         }
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 */
609                 }
610                 *s++ = 0;                    /* Replace space with null to terminate */
611         }
612
613         oid = ber_strdup(oidStart);
614         if (oid == NULL) return ( LDAP_NO_MEMORY );
615
616         /* Optional Criticality field is next. */
617         while (*s && isspace((unsigned char)*s)) {
618                 s++;                         /* Skip white space before criticality */
619         }
620         if (strncasecmp(s, "true", 4) == 0) {
621                 criticality = 1;
622                 s += 4;
623         } 
624         else if (strncasecmp(s, "false", 5) == 0) {
625                 criticality = 0;
626                 s += 5;
627         }
628
629         /* Optional value field is next */
630         while (*s && isspace((unsigned char)*s)) {
631                 s++;                         /* Skip white space before value */
632         }
633         if (*s) {
634                 if (*s != ':') {           /* If value is present, must start with : */
635                         rc = LDAP_PARAM_ERROR;
636                         goto cleanup;
637                 }
638
639                 /* Back up so value is in the form
640                      a: value
641                      a:: base64-value
642                      a:< url
643                    Then we can use ldif_parse_line2 to extract and decode the value
644                 */
645                 s--;
646                 *s = 'a';
647
648                 rc = ldif_parse_line2(s, &type, &bv, &freeval);
649                 if (rc < 0) {
650                         rc = LDAP_PARAM_ERROR;
651                         goto cleanup;
652                 }
653     }
654
655         /* Create a new LDAPControl structure. */
656         newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
657         if ( newctrl == NULL ) {
658                 rc = LDAP_NO_MEMORY;
659                 goto cleanup;
660         }
661         newctrl->ldctl_oid = oid;
662         oid = NULL;
663         newctrl->ldctl_iscritical = criticality;
664         if ( freeval )
665                 newctrl->ldctl_value = bv;
666         else
667                 ber_dupbv( &newctrl->ldctl_value, &bv );
668
669         /* Add the new control to the passed-in list of controls. */
670         i = 0;
671         if (pctrls) {
672                 while ( pctrls[i] ) {    /* Count the # of controls passed in */
673                         i++;
674                 }
675         }
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) {
680                 rc = LDAP_NO_MEMORY;
681                 goto cleanup;
682         }
683         pctrls[i] = newctrl;
684         newctrl = NULL;
685         pctrls[i+1] = NULL;
686         *ppctrls = pctrls;
687
688 cleanup:
689         if (newctrl) {
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);
693                 }
694                 ber_memfree(newctrl);
695         }
696         if (oid) ber_memfree(oid);
697
698         return( rc );
699 }
700
701