]> git.sur5r.net Git - openldap/blob - libraries/libldap/ldifutil.c
Merge branch 'mdb.master' of ssh://git-master.openldap.org/~git/git/openldap
[openldap] / libraries / libldap / ldifutil.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2011 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         int 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 %d) 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 %d (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 %d: %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 %d\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 %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';
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 %d: "
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 %d, 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 %d, 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 %d, 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 %d, 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 %d: "
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 %d, 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 ( BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+j )) {
360                                         nmods--;
361                                         /* out of order, move intervening attributes down */
362                                         if ( j != i+1 ) {
363                                                 bv = lr->lr_vals[j];
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];
369                                                 }
370                                                 k++;
371                                                 lr->lr_btype[k] = lr->lr_btype[i];
372                                                 lr->lr_vals[k] = bv;
373                                                 lr->lr_freeval[k] = fv;
374                                         }
375                                         i++;
376                                 }
377                         }
378                 }
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...
382                  */
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);
388
389                 j = 0;
390                 k = -1;
391                 BER_BVZERO(&bv);
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 );
398                         }
399                         if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
400                                 bvl[k++] = NULL;
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;
406                                 j++;
407                         }
408                         bvl[k++] = lr->lr_vals+i;
409                 }
410                 bvl[k] = NULL;
411                 pmods[j] = NULL;
412                 goto doit;
413         }
414
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;
418
419         for ( ; i<lr->lr_lines; i++ ) {
420                 if ( expect_modop ) {
421 #ifdef LIBERAL_CHANGETYPE_MODOP
422                         /* trim trailing spaces (and log warning ...) */
423                     int icnt;
424                     for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
425                                 if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) break;
426                         }
427     
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';
433                         }
434 #endif /* LIBERAL_CHANGETYPE_MODOP */
435
436                         expect_modop = 0;
437                         expect_sep = 1;
438                         if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPADD )) {
439                                 modop = LDAP_MOD_ADD;
440                                 lr->lr_mops[i] = M_SEP;
441                                 nmods--;
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.
447                          */
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;
458                                 nmods--;
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;
464                                 goto leave;
465                         }
466                         bv = lr->lr_vals[i];
467                 } else if ( expect_sep && BER_BVISEMPTY( lr->lr_btype+i )) {
468                         lr->lr_mops[i] = M_SEP;
469                         expect_sep = 0;
470                         expect_modop = 1;
471                         nmods--;
472                 } else {
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;
478                                 goto leave;
479                         }
480                         lr->lr_mops[i] = modop;
481                         /* If prev op was deferred and matches this type,
482                          * clear the flag
483                          */
484                         if ( (lr->lr_mops[i-1] & LDAP_MOD_BVALUES)
485                                 && BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+i-1 ))
486                         {
487                                 lr->lr_mops[i-1] = M_SEP;
488                                 nmods--;
489                         }
490                 }
491         }
492
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...
496          */
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);
502
503         j = 0;
504         k = -1;
505         BER_BVZERO(&bv);
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 )
509                         continue;
510                 if ( lr->lr_mops[i] != lr->lr_mops[i-1] || !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
511                         bvl[k++] = NULL;
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;
517                         } else {
518                                 lr->lr_lm[j].mod_bvalues = bvl+k;
519                         }
520                         pmods[j] = lr->lr_lm+j;
521                         j++;
522                 }
523                 bvl[k++] = lr->lr_vals+i;
524         }
525         bvl[k] = NULL;
526         pmods[j] = NULL;
527
528 doit:
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;
536         } else {
537                 /* for now, either add or modify */
538                 lr->lrop_mods = pmods;
539                 if ( new_entry ) {
540                         lr->lr_op = LDAP_REQ_ADD;
541                 } else {
542                         lr->lr_op = LDAP_REQ_MODIFY;
543                 }
544         }
545
546 leave:
547         if ( rc != LDAP_SUCCESS ) {
548                 ldap_ldif_record_done( lr );
549         }
550
551         return( rc );
552 }
553
554 /* Same as ldap_parse_ldif_record_x()
555  * public API does not expose memory context
556  */
557 int
558 ldap_parse_ldif_record(
559         struct berval *rbuf,
560         int linenum,
561         LDIFRecord *lr,
562         const char *errstr,
563         unsigned int flags )
564 {
565         return ldap_parse_ldif_record_x( rbuf, linenum, lr, errstr, flags, NULL );
566 }
567
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.
573 */      
574 static int
575 parse_ldif_control(
576         struct berval *bval,
577         LDAPControl ***ppctrls)
578 {
579         char *oid = NULL;
580         int criticality = 0;   /* Default is false if not present */
581         int i, rc=0;
582         char *s, *oidStart;
583         LDAPControl *newctrl = NULL;
584         LDAPControl **pctrls = NULL;
585         struct berval type, bv = BER_BVNULL;
586         int freeval = 0;
587
588         if (ppctrls) pctrls = *ppctrls;
589         /* OID should come first. Validate and extract it. */
590         s = bval->bv_val;
591         if (*s == 0) return ( LDAP_PARAM_ERROR );
592         oidStart = s;
593         while (isdigit((unsigned char)*s) || *s == '.') {
594                 s++;                           /* OID should be digits or . */
595         }
596         if (s == oidStart) { 
597                 return ( LDAP_PARAM_ERROR );   /* OID was not present */
598         }
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 */
602                 }
603                 *s++ = 0;                    /* Replace space with null to terminate */
604         }
605
606         oid = ber_strdup(oidStart);
607         if (oid == NULL) return ( LDAP_NO_MEMORY );
608
609         /* Optional Criticality field is next. */
610         while (*s && isspace((unsigned char)*s)) {
611                 s++;                         /* Skip white space before criticality */
612         }
613         if (strncasecmp(s, "true", 4) == 0) {
614                 criticality = 1;
615                 s += 4;
616         } 
617         else if (strncasecmp(s, "false", 5) == 0) {
618                 criticality = 0;
619                 s += 5;
620         }
621
622         /* Optional value field is next */
623         while (*s && isspace((unsigned char)*s)) {
624                 s++;                         /* Skip white space before value */
625         }
626         if (*s) {
627                 if (*s != ':') {           /* If value is present, must start with : */
628                         rc = LDAP_PARAM_ERROR;
629                         goto cleanup;
630                 }
631
632                 /* Back up so value is in the form
633                      a: value
634                      a:: base64-value
635                      a:< url
636                    Then we can use ldif_parse_line2 to extract and decode the value
637                 */
638                 s--;
639                 *s = 'a';
640
641                 rc = ldif_parse_line2(s, &type, &bv, &freeval);
642                 if (rc < 0) {
643                         rc = LDAP_PARAM_ERROR;
644                         goto cleanup;
645                 }
646     }
647
648         /* Create a new LDAPControl structure. */
649         newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
650         if ( newctrl == NULL ) {
651                 rc = LDAP_NO_MEMORY;
652                 goto cleanup;
653         }
654         newctrl->ldctl_oid = oid;
655         oid = NULL;
656         newctrl->ldctl_iscritical = criticality;
657         if ( freeval )
658                 newctrl->ldctl_value = bv;
659         else
660                 ber_dupbv( &newctrl->ldctl_value, &bv );
661
662         /* Add the new control to the passed-in list of controls. */
663         i = 0;
664         if (pctrls) {
665                 while ( pctrls[i] ) {    /* Count the # of controls passed in */
666                         i++;
667                 }
668         }
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) {
673                 rc = LDAP_NO_MEMORY;
674                 goto cleanup;
675         }
676         pctrls[i] = newctrl;
677         newctrl = NULL;
678         pctrls[i+1] = NULL;
679         *ppctrls = pctrls;
680
681 cleanup:
682         if (newctrl) {
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);
686                 }
687                 ber_memfree(newctrl);
688         }
689         if (oid) ber_memfree(oid);
690
691         return( rc );
692 }
693
694