]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
Fixup bdb_entry_release now that entry_decode uses two memory blocks
[openldap] / servers / slapd / dn.c
1 /* dn.c - routines for dealing with distinguished names */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16
17 #include "ldap_pvt.h"
18
19 #include "slap.h"
20
21 #define B4LEADTYPE              0
22 #define B4TYPE                  1
23 #define INOIDTYPE               2
24 #define INKEYTYPE               3
25 #define B4EQUAL                 4
26 #define B4VALUE                 5
27 #define INVALUE                 6
28 #define INQUOTEDVALUE   7
29 #define B4SEPARATOR             8
30
31 /*
32  * dn_validate - validate and compress dn.  the dn is
33  * compressed in place are returned if valid.
34  */
35
36 char *
37 dn_validate( char *dn_in )
38 {
39 #ifdef USE_LDAP_DN_PARSING
40         struct berval   val, *normalized;
41         int             rc;
42
43         if ( dn_in == NULL || dn_in[ 0 ] == '\0' ) {
44                 return( dn_in );
45         }
46
47         val.bv_val = dn_in;
48         val.bv_len = strlen( dn_in );
49
50         rc = dnPretty( NULL, &val, &normalized );
51         if ( rc != LDAP_SUCCESS ) {
52                 return( NULL );
53         }
54
55         if ( val.bv_len < normalized->bv_len ) {
56                 ber_bvfree( normalized );
57                 return( NULL );
58         }
59
60         AC_MEMCPY( dn_in, normalized->bv_val, normalized->bv_len + 1 );
61         ber_bvfree( normalized );
62
63         return( dn_in );
64         
65 #else /* !USE_LDAP_DN_PARSING */
66         char    *d, *s;
67         int     state, gotesc;
68         char    *dn = dn_in;
69
70         gotesc = 0;
71         state = B4LEADTYPE;
72         for ( d = s = dn; *s; s++ ) {
73                 switch ( state ) {
74                 case B4LEADTYPE:
75                 case B4TYPE:
76                         if ( OID_LEADCHAR(*s) ) {
77                                 state = INOIDTYPE;
78                                 *d++ = *s;
79                         } else if ( ATTR_LEADCHAR(*s) ) {
80                                 state = INKEYTYPE;
81                                 *d++ = *s;
82                         } else if ( ! ASCII_SPACE( *s ) ) {
83                                 dn = NULL;
84                                 state = INKEYTYPE;
85                                 *d++ = *s;
86                         }
87                         break;
88
89                 case INOIDTYPE:
90                         if ( OID_CHAR(*s) ) {
91                                 *d++ = *s;
92                         } else if ( *s == '=' ) {
93                                 state = B4VALUE;
94                                 *d++ = *s;
95                         } else if ( ASCII_SPACE( *s ) ) {
96                                 state = B4EQUAL;
97                         } else {
98                                 dn = NULL;
99                                 *d++ = *s;
100                         }
101                         break;
102
103                 case INKEYTYPE:
104                         if ( ATTR_CHAR(*s) ) {
105                                 *d++ = *s;
106                         } else if ( *s == '=' ) {
107                                 state = B4VALUE;
108                                 *d++ = *s;
109                         } else if ( ASCII_SPACE( *s ) ) {
110                                 state = B4EQUAL;
111                         } else {
112                                 dn = NULL;
113                                 *d++ = *s;
114                         }
115                         break;
116
117                 case B4EQUAL:
118                         if ( *s == '=' ) {
119                                 state = B4VALUE;
120                                 *d++ = *s;
121                         } else if ( ! ASCII_SPACE( *s ) ) {
122                                 /* not a valid dn - but what can we do here? */
123                                 *d++ = *s;
124                                 dn = NULL;
125                         }
126                         break;
127
128                 case B4VALUE:
129                         if ( *s == '"' ) {
130                                 state = INQUOTEDVALUE;
131                                 *d++ = *s;
132                         } else if ( ! ASCII_SPACE( *s ) ) {
133                                 state = INVALUE;
134                                 *d++ = *s;
135                         }
136                         break;
137
138                 case INVALUE:
139                         if ( !gotesc && RDN_SEPARATOR( *s ) ) {
140                                 while ( ASCII_SPACE( *(d - 1) ) )
141                                         d--;
142                                 state = B4TYPE;
143                                 if ( *s == '+' ) {
144                                         *d++ = *s;
145                                 } else {
146                                         *d++ = ',';
147                                 }
148                         } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) &&
149                                 !RDN_SEPARATOR( *s ) )
150                         {
151                                 *--d = *s;
152                                 d++;
153                         } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
154                                 *d++ = *s;
155                         }
156                         break;
157
158                 case INQUOTEDVALUE:
159                         if ( !gotesc && *s == '"' ) {
160                                 state = B4SEPARATOR;
161                                 *d++ = *s;
162                         } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) ) {
163                                 *--d = *s;
164                                 d++;
165                         } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
166                                 *d++ = *s;
167                         }
168                         break;
169
170                 case B4SEPARATOR:
171                         if ( RDN_SEPARATOR( *s ) ) {
172                                 state = B4TYPE;
173                                 *d++ = *s;
174                         } else if ( !ASCII_SPACE( *s ) ) {
175                                 dn = NULL;
176                         }
177                         break;
178
179                 default:
180                         dn = NULL;
181 #ifdef NEW_LOGGING
182                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
183                                 "dn_validate: unknown state %d for dn \"%s\".\n",
184                                 state, dn_in ));
185 #else
186                         Debug( LDAP_DEBUG_ANY,
187                                 "dn_validate - unknown state %d\n", state, 0, 0 );
188 #endif
189                         break;
190                 }
191
192                 if ( *s == '\\' ) {
193                         gotesc = 1;
194                 } else {
195                         gotesc = 0;
196                 }
197         }
198
199         /* trim trailing spaces */
200         while( d > dn_in && ASCII_SPACE( *(d-1) ) ) {
201                 --d;
202         }
203         *d = '\0';
204
205         if( gotesc ) {
206                 /* shouldn't be left in escape */
207                 dn = NULL;
208         }
209
210         /* check end state */
211         switch( state ) {
212         case B4LEADTYPE:        /* looking for first type */
213         case B4SEPARATOR:       /* looking for separator */
214         case INVALUE:           /* inside value */
215                 break;
216         default:
217                 dn = NULL;
218         }
219
220         return( dn );
221 #endif /* !USE_LDAP_DN_PARSING */
222 }
223
224 /*
225  * dn_normalize - put dn into a canonical form suitable for storing
226  * in a hash database.  this involves normalizing the case as well as
227  * the format.  the dn is normalized in place as well as returned if valid.
228  */
229
230 char *
231 dn_normalize( char *dn )
232 {
233 #ifdef USE_LDAP_DN_PARSING
234         struct berval   val, *normalized;
235         int             rc;
236
237         if ( dn == NULL || dn[ 0 ] == '\0' ) {
238                 return( dn );
239         }
240
241         val.bv_val = dn;
242         val.bv_len = strlen( dn );
243
244         rc = dnNormalize( NULL, &val, &normalized );
245         if ( rc != LDAP_SUCCESS ) {
246                 return( NULL );
247         }
248
249         if ( val.bv_len < normalized->bv_len ) {
250                 ber_bvfree( normalized );
251                 return( NULL );
252         }
253
254         AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
255         ber_bvfree( normalized );
256
257         ( void )ldap_pvt_str2upper( dn );
258
259         return( dn );
260         
261 #else /* !USE_LDAP_DN_PARSING */
262         char *out;
263         struct berval *bvdn, *nbvdn;
264
265         out = NULL;
266         bvdn = ber_bvstr( dn );
267         
268         if ( dnNormalize( NULL, bvdn, &nbvdn ) == LDAP_SUCCESS ) {
269                 if ( nbvdn->bv_len <= bvdn->bv_len ) {
270                         out = dn;
271                         strcpy( out, nbvdn->bv_val );
272                 }
273                 ber_bvfree( nbvdn );
274         }
275         bvdn->bv_val = NULL; /* prevent bvfree from freeing dn */
276         ber_bvfree( bvdn );
277
278         return( out );
279 #endif /* !USE_LDAP_DN_PARSING */
280 }
281
282 int
283 dn_match( const char *val, const char *asserted )
284 {
285         struct berval   bval, basserted;
286
287         if ( val == NULL || asserted == NULL ) {
288                 return 0;
289         }
290
291         bval.bv_val = ( char * )val;
292         bval.bv_len = strlen( val );
293
294         basserted.bv_val = ( char * )asserted;
295         basserted.bv_len = strlen( asserted);
296
297         return dnMatch( NULL, 0, NULL, NULL, &bval, &basserted);
298 }
299
300 /*
301  * dn_parent - return a copy of the dn of dn's parent
302  */
303
304 char *
305 dn_parent(
306         Backend *be,
307         const char      *dn
308 )
309 {
310         const char      *s;
311         int     inquote;
312
313         if( dn == NULL ) {
314                 return NULL;
315         }
316
317         while(*dn != '\0' && ASCII_SPACE(*dn)) {
318                 dn++;
319         }
320
321         if( *dn == '\0' ) {
322                 return NULL;
323         }
324
325         if ( be != NULL && be_issuffix( be, dn ) ) {
326                 return NULL;
327         }
328
329         /*
330          * assume it is an X.500-style name, which looks like
331          * foo=bar,sha=baz,...
332          */
333
334         inquote = 0;
335         for ( s = dn; *s; s++ ) {
336                 if ( *s == '\\' ) {
337                         if ( *(s + 1) ) {
338                                 s++;
339                         }
340                         continue;
341                 }
342                 if ( inquote ) {
343                         if ( *s == '"' ) {
344                                 inquote = 0;
345                         }
346                 } else {
347                         if ( *s == '"' ) {
348                                 inquote = 1;
349                         } else if ( DN_SEPARATOR( *s ) ) {
350                                 return ch_strdup( &s[1] );
351                         }
352                 }
353         }
354
355         return ch_strdup( "" );
356 }
357
358 char * dn_rdn(
359         Backend *be,
360         const char      *dn_in )
361 {
362         char    *dn, *s;
363         int     inquote;
364
365         if( dn_in == NULL ) {
366                 return NULL;
367         }
368
369         while(*dn_in && ASCII_SPACE(*dn_in)) {
370                 dn_in++;
371         }
372
373         if( *dn_in == '\0' ) {
374                 return( NULL );
375         }
376
377         if ( be != NULL && be_issuffix( be, dn_in ) ) {
378                 return( NULL );
379         }
380
381         dn = ch_strdup( dn_in );
382
383         inquote = 0;
384
385         for ( s = dn; *s; s++ ) {
386                 if ( *s == '\\' ) {
387                         if ( *(s + 1) ) {
388                                 s++;
389                         }
390                         continue;
391                 }
392                 if ( inquote ) {
393                         if ( *s == '"' ) {
394                                 inquote = 0;
395                         }
396                 } else {
397                         if ( *s == '"' ) {
398                                 inquote = 1;
399                         } else if ( DN_SEPARATOR( *s ) ) {
400                                 *s = '\0';
401                                 return( dn );
402                         }
403                 }
404         }
405
406         return( dn );
407 }
408
409
410 /*
411  * return a charray of all subtrees to which the DN resides in
412  */
413 char **dn_subtree(
414         Backend *be,
415         const char      *dn )
416 {
417         char *child, *parent;
418         char **subtree = NULL;
419         
420         child = ch_strdup( dn );
421
422         do {
423                 charray_add( &subtree, child );
424
425                 parent = dn_parent( be, child );
426
427                 free( child );
428
429                 child = parent;
430         } while ( child != NULL );
431
432         return subtree;
433 }
434
435
436 /*
437  * dn_issuffix - tells whether suffix is a suffix of dn. Both dn
438  * and suffix must be normalized.
439  */
440
441 int
442 dn_issuffix(
443         const char      *dn,
444         const char      *suffix
445 )
446 {
447         int     dnlen, suffixlen;
448
449         if ( dn == NULL ) {
450                 return( 0 );
451         }
452
453         suffixlen = strlen( suffix );
454         dnlen = strlen( dn );
455
456         if ( suffixlen > dnlen ) {
457                 return( 0 );
458         }
459
460         return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
461 }
462
463 /*
464  * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
465  * build_new_dn().
466  *
467  * Copyright 1999, Juan C. Gomez, All rights reserved.
468  * This software is not subject to any license of Silicon Graphics
469  * Inc. or Purdue University.
470  *
471  * Redistribution and use in source and binary forms are permitted
472  * without restriction or fee of any kind as long as this notice
473  * is preserved.
474  *
475  */
476
477 /* get_next_substring:
478  *
479  * Gets next substring in s, using d (or the end of the string '\0') as a
480  * string delimiter, and places it in a duplicated memory space. Leading
481  * spaces are ignored. String s **must** be null-terminated.
482  */
483
484 static char *
485 get_next_substring( const char * s, char d )
486 {
487
488         char    *str, *r;
489
490         r = str = ch_malloc( strlen(s) + 1 );
491
492         /* Skip leading spaces */
493         
494         while ( *s && ASCII_SPACE(*s) ) {
495                 s++;
496         }
497         
498         /* Copy word */
499
500         while ( *s && (*s != d) ) {
501
502                 /* Don't stop when you see trailing spaces may be a multi-word
503                 * string, i.e. name=John Doe!
504                 */
505
506                 *str++ = *s++;
507         }
508         
509         *str = '\0';
510         
511         return r;
512         
513 }
514
515
516 /* rdn_attr_type:
517  *
518  * Given a string (i.e. an rdn) of the form:
519  *       "attribute_type = attribute_value"
520  * this function returns the type of an attribute, that is the
521  * string "attribute_type" which is placed in newly allocated
522  * memory. The returned string will be null-terminated.
523  */
524
525 char * rdn_attr_type( const char * s )
526 {
527         return get_next_substring( s, '=' );
528 }
529
530
531 /* rdn_attr_value:
532  *
533  * Given a string (i.e. an rdn) of the form:
534  *       "attribute_type = attribute_value"
535  * this function returns "attribute_type" which is placed in newly allocated
536  * memory. The returned string will be null-terminated and may contain
537  * spaces (i.e. "John Doe\0").
538  */
539
540 char *
541 rdn_attr_value( const char * rdn )
542 {
543
544         const char      *str;
545
546         if ( (str = strchr( rdn, '=' )) != NULL ) {
547                 return get_next_substring(++str, '\0');
548         }
549
550         return NULL;
551
552 }
553
554
555 /* rdn_attrs:
556  *
557  * Given a string (i.e. an rdn) of the form:
558  *   "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
559  * this function stores the types of the attributes in ptypes, that is the
560  * array of strings "attribute_type" which is placed in newly allocated
561  * memory, and the values of the attributes in pvalues, that is the
562  * array of strings "attribute_value" which is placed in newly allocated
563  * memory. Returns 0 on success, -1 on failure.
564  *
565  * note: got part of the code from dn_validate
566  */
567
568 int
569 rdn_attrs( const char * rdn_in, char ***ptypes, char ***pvalues)
570 {
571         char **parts, **p;
572
573         *ptypes = NULL;
574         *pvalues = NULL;
575
576         /*
577          * explode the rdn in parts
578          */
579         parts = ldap_explode_rdn( rdn_in, 0 );
580
581         if ( parts == NULL ) {
582                 return( -1 );
583         }
584
585         for ( p = parts; p[0]; p++ ) {
586                 char *s, *e, *d;
587                 
588                 /* split each rdn part in type value */
589                 s = strchr( p[0], '=' );
590                 if ( s == NULL ) {
591                         charray_free( *ptypes );
592                         charray_free( *pvalues );
593                         charray_free( parts );
594                         return( -1 );
595                 }
596                 
597                 /* type should be fine */
598                 charray_add_n( ptypes, p[0], ( s-p[0] ) );
599
600                 /* value needs to be unescaped
601                  * (maybe this should be moved to ldap_explode_rdn?) */
602                 for ( e = d = s + 1; e[0]; e++ ) {
603                         if ( *e != '\\' ) {
604                                 *d++ = *e;
605                         }
606                 }
607                 d[0] = '\0';
608                 charray_add( pvalues, s + 1 );
609         }
610
611         /* free array */
612         charray_free( parts );
613
614         return( 0 );
615 }
616
617
618 /* rdn_validate:
619  *
620  * 1 if rdn is a legal rdn;
621  * 0 otherwise (including a sequence of rdns)
622  *
623  * note: got it from dn_rdn; it should be rewritten
624  * according to dn_validate
625  */
626 int
627 rdn_validate( const char * rdn )
628 {
629         int     inquote;
630
631         if ( rdn == NULL ) {
632                 return( 0 );
633         }
634
635         if ( strchr( rdn, '=' ) == NULL ) {
636                 return( 0 );
637         }
638
639         while ( *rdn && ASCII_SPACE( *rdn ) ) {
640                 rdn++;
641         }
642
643         if( *rdn == '\0' ) {
644                 return( 0 );
645         }
646
647         inquote = 0;
648
649         for ( ; *rdn; rdn++ ) {
650                 if ( *rdn == '\\' ) {
651                         if ( *(rdn + 1) ) {
652                                 rdn++;
653                         }
654                         continue;
655                 }
656                 if ( inquote ) {
657                         if ( *rdn == '"' ) {
658                                 inquote = 0;
659                         }
660                 } else {
661                         if ( *rdn == '"' ) {
662                                 inquote = 1;
663                         } else if ( DN_SEPARATOR( *rdn ) ) {
664                                 return( 0 );
665                         }
666                 }
667         }
668
669         return( 1 );
670 }
671
672
673 /* build_new_dn:
674  *
675  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
676  * renamed.
677  *
678  * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
679  */
680
681 void
682 build_new_dn( char ** new_dn,
683         const char *e_dn,
684         const char * p_dn,
685         const char * newrdn )
686 {
687
688         if ( p_dn == NULL ) {
689                 *new_dn = ch_strdup( newrdn );
690                 return;
691         }
692
693         *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
694
695         strcpy( *new_dn, newrdn );
696         strcat( *new_dn, "," );
697         strcat( *new_dn, p_dn );
698 }