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