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