]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
c88eb3c49b08dd622fb2d092db17dd34f303a60b
[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_parent1 - return the dn's parent, in-place
317  */
318
319 char *
320 dn_parent1(
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 /*
373  * dn_parent - return a copy of the dn of dn's parent
374  */
375
376 char *
377 dn_parent(
378         Backend *be,
379         const char      *dn
380 )
381 {
382         dn = dn_parent1( be, dn );
383         if( dn != NULL )
384                 dn = ch_strdup( dn );
385         return (char *)dn;
386 }
387
388 char * dn_rdn(
389         Backend *be,
390         const char      *dn_in )
391 {
392         char    *dn, *s;
393         int     inquote;
394
395         if( dn_in == NULL ) {
396                 return NULL;
397         }
398
399         while(*dn_in && ASCII_SPACE(*dn_in)) {
400                 dn_in++;
401         }
402
403         if( *dn_in == '\0' ) {
404                 return( NULL );
405         }
406
407         if ( be != NULL && be_issuffix( be, dn_in ) ) {
408                 return( NULL );
409         }
410
411         dn = ch_strdup( dn_in );
412
413         inquote = 0;
414
415         for ( s = dn; *s; s++ ) {
416                 if ( *s == '\\' ) {
417                         if ( *(s + 1) ) {
418                                 s++;
419                         }
420                         continue;
421                 }
422                 if ( inquote ) {
423                         if ( *s == '"' ) {
424                                 inquote = 0;
425                         }
426                 } else {
427                         if ( *s == '"' ) {
428                                 inquote = 1;
429                         } else if ( DN_SEPARATOR( *s ) ) {
430                                 *s = '\0';
431                                 return( dn );
432                         }
433                 }
434         }
435
436         return( dn );
437 }
438
439
440 /*
441  * return a charray of all subtrees to which the DN resides in
442  */
443 char **dn_subtree(
444         Backend *be,
445         const char      *dn )
446 {
447         char **subtree = NULL;
448         
449         do {
450                 charray_add( &subtree, dn );
451
452                 dn = dn_parent1( be, dn );
453
454         } while ( dn != NULL );
455
456         return subtree;
457 }
458
459
460 /*
461  * dn_issuffix - tells whether suffix is a suffix of dn. Both dn
462  * and suffix must be normalized.
463  */
464
465 int
466 dn_issuffix(
467         const char      *dn,
468         const char      *suffix
469 )
470 {
471         int     dnlen, suffixlen;
472
473         if ( dn == NULL ) {
474                 return( 0 );
475         }
476
477         suffixlen = strlen( suffix );
478         dnlen = strlen( dn );
479
480         if ( suffixlen > dnlen ) {
481                 return( 0 );
482         }
483
484         return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
485 }
486
487 /*
488  * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
489  * build_new_dn().
490  *
491  * Copyright 1999, Juan C. Gomez, All rights reserved.
492  * This software is not subject to any license of Silicon Graphics
493  * Inc. or Purdue University.
494  *
495  * Redistribution and use in source and binary forms are permitted
496  * without restriction or fee of any kind as long as this notice
497  * is preserved.
498  *
499  */
500
501 /* get_next_substring:
502  *
503  * Gets next substring in s, using d (or the end of the string '\0') as a
504  * string delimiter, and places it in a duplicated memory space. Leading
505  * spaces are ignored. String s **must** be null-terminated.
506  */
507
508 static char *
509 get_next_substring( const char * s, char d )
510 {
511
512         char    *str, *r;
513
514         r = str = ch_malloc( strlen(s) + 1 );
515
516         /* Skip leading spaces */
517         
518         while ( *s && ASCII_SPACE(*s) ) {
519                 s++;
520         }
521         
522         /* Copy word */
523
524         while ( *s && (*s != d) ) {
525
526                 /* Don't stop when you see trailing spaces may be a multi-word
527                 * string, i.e. name=John Doe!
528                 */
529
530                 *str++ = *s++;
531         }
532         
533         *str = '\0';
534         
535         return r;
536         
537 }
538
539
540 /* rdn_attr_type:
541  *
542  * Given a string (i.e. an rdn) of the form:
543  *       "attribute_type = attribute_value"
544  * this function returns the type of an attribute, that is the
545  * string "attribute_type" which is placed in newly allocated
546  * memory. The returned string will be null-terminated.
547  */
548
549 char * rdn_attr_type( const char * s )
550 {
551         return get_next_substring( s, '=' );
552 }
553
554
555 /* rdn_attr_value:
556  *
557  * Given a string (i.e. an rdn) of the form:
558  *       "attribute_type = attribute_value"
559  * this function returns "attribute_type" which is placed in newly allocated
560  * memory. The returned string will be null-terminated and may contain
561  * spaces (i.e. "John Doe\0").
562  */
563
564 char *
565 rdn_attr_value( const char * rdn )
566 {
567
568         const char      *str;
569
570         if ( (str = strchr( rdn, '=' )) != NULL ) {
571                 return get_next_substring(++str, '\0');
572         }
573
574         return NULL;
575
576 }
577
578
579 /* rdn_attrs:
580  *
581  * Given a string (i.e. an rdn) of the form:
582  *   "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
583  * this function stores the types of the attributes in ptypes, that is the
584  * array of strings "attribute_type" which is placed in newly allocated
585  * memory, and the values of the attributes in pvalues, that is the
586  * array of strings "attribute_value" which is placed in newly allocated
587  * memory. Returns 0 on success, -1 on failure.
588  *
589  * note: got part of the code from dn_validate
590  */
591
592 int
593 rdn_attrs( const char * rdn_in, char ***ptypes, char ***pvalues)
594 {
595         char **parts, **p;
596
597         *ptypes = NULL;
598         *pvalues = NULL;
599
600         /*
601          * explode the rdn in parts
602          */
603         parts = ldap_explode_rdn( rdn_in, 0 );
604
605         if ( parts == NULL ) {
606                 return( -1 );
607         }
608
609         for ( p = parts; p[0]; p++ ) {
610                 char *s, *e, *d;
611                 
612                 /* split each rdn part in type value */
613                 s = strchr( p[0], '=' );
614                 if ( s == NULL ) {
615                         charray_free( *ptypes );
616                         charray_free( *pvalues );
617                         charray_free( parts );
618                         return( -1 );
619                 }
620                 
621                 /* type should be fine */
622                 charray_add_n( ptypes, p[0], ( s-p[0] ) );
623
624                 /* value needs to be unescaped
625                  * (maybe this should be moved to ldap_explode_rdn?) */
626                 for ( e = d = s + 1; e[0]; e++ ) {
627                         if ( *e != '\\' ) {
628                                 *d++ = *e;
629                         }
630                 }
631                 d[0] = '\0';
632                 charray_add( pvalues, s + 1 );
633         }
634
635         /* free array */
636         charray_free( parts );
637
638         return( 0 );
639 }
640
641
642 /* rdn_validate:
643  *
644  * 1 if rdn is a legal rdn;
645  * 0 otherwise (including a sequence of rdns)
646  *
647  * note: got it from dn_rdn; it should be rewritten
648  * according to dn_validate
649  */
650 int
651 rdn_validate( const char * rdn )
652 {
653         int     inquote;
654
655         if ( rdn == NULL ) {
656                 return( 0 );
657         }
658
659         if ( strchr( rdn, '=' ) == NULL ) {
660                 return( 0 );
661         }
662
663         while ( *rdn && ASCII_SPACE( *rdn ) ) {
664                 rdn++;
665         }
666
667         if( *rdn == '\0' ) {
668                 return( 0 );
669         }
670
671         inquote = 0;
672
673         for ( ; *rdn; rdn++ ) {
674                 if ( *rdn == '\\' ) {
675                         if ( *(rdn + 1) ) {
676                                 rdn++;
677                         }
678                         continue;
679                 }
680                 if ( inquote ) {
681                         if ( *rdn == '"' ) {
682                                 inquote = 0;
683                         }
684                 } else {
685                         if ( *rdn == '"' ) {
686                                 inquote = 1;
687                         } else if ( DN_SEPARATOR( *rdn ) ) {
688                                 return( 0 );
689                         }
690                 }
691         }
692
693         return( 1 );
694 }
695
696
697 /* build_new_dn:
698  *
699  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
700  * renamed.
701  *
702  * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
703  */
704
705 void
706 build_new_dn( char ** new_dn,
707         const char *e_dn,
708         const char * p_dn,
709         const char * newrdn )
710 {
711
712         if ( p_dn == NULL ) {
713                 *new_dn = ch_strdup( newrdn );
714                 return;
715         }
716
717         *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
718
719         strcpy( *new_dn, newrdn );
720         strcat( *new_dn, "," );
721         strcat( *new_dn, p_dn );
722 }