]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
liberally accept many LDAPv2/LDAPv3 stuff in DN (quoted parts, ';' as rdn separator...
[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         return( dn );
258         
259 #else /* !USE_LDAP_DN_PARSING */
260         char *out;
261         struct berval *bvdn, *nbvdn;
262
263         out = NULL;
264         bvdn = ber_bvstr( dn );
265         
266         if ( dnNormalize( NULL, bvdn, &nbvdn ) == LDAP_SUCCESS ) {
267                 if ( nbvdn->bv_len <= bvdn->bv_len ) {
268                         out = dn;
269                         strcpy( out, nbvdn->bv_val );
270                 }
271                 ber_bvfree( nbvdn );
272         }
273         bvdn->bv_val = NULL; /* prevent bvfree from freeing dn */
274         ber_bvfree( bvdn );
275
276         return( out );
277 #endif /* !USE_LDAP_DN_PARSING */
278 }
279
280 int
281 dn_match( const char *val, const char *asserted )
282 {
283         struct berval   bval, basserted;
284
285         if ( val == NULL || asserted == NULL ) {
286                 return 0;
287         }
288
289         bval.bv_val = ( char * )val;
290         bval.bv_len = strlen( val );
291
292         basserted.bv_val = ( char * )asserted;
293         basserted.bv_len = strlen( asserted);
294
295         return dnMatch( NULL, 0, NULL, NULL, &bval, &basserted);
296 }
297
298 /*
299  * dn_parent - return a copy of the dn of dn's parent
300  */
301
302 char *
303 dn_parent(
304         Backend *be,
305         const char      *dn
306 )
307 {
308         const char      *s;
309         int     inquote;
310
311         if( dn == NULL ) {
312                 return NULL;
313         }
314
315         while(*dn != '\0' && ASCII_SPACE(*dn)) {
316                 dn++;
317         }
318
319         if( *dn == '\0' ) {
320                 return NULL;
321         }
322
323         if ( be != NULL && be_issuffix( be, dn ) ) {
324                 return NULL;
325         }
326
327         /*
328          * assume it is an X.500-style name, which looks like
329          * foo=bar,sha=baz,...
330          */
331
332         inquote = 0;
333         for ( s = dn; *s; s++ ) {
334                 if ( *s == '\\' ) {
335                         if ( *(s + 1) ) {
336                                 s++;
337                         }
338                         continue;
339                 }
340                 if ( inquote ) {
341                         if ( *s == '"' ) {
342                                 inquote = 0;
343                         }
344                 } else {
345                         if ( *s == '"' ) {
346                                 inquote = 1;
347                         } else if ( DN_SEPARATOR( *s ) ) {
348                                 return ch_strdup( &s[1] );
349                         }
350                 }
351         }
352
353         return ch_strdup( "" );
354 }
355
356 char * dn_rdn(
357         Backend *be,
358         const char      *dn_in )
359 {
360         char    *dn, *s;
361         int     inquote;
362
363         if( dn_in == NULL ) {
364                 return NULL;
365         }
366
367         while(*dn_in && ASCII_SPACE(*dn_in)) {
368                 dn_in++;
369         }
370
371         if( *dn_in == '\0' ) {
372                 return( NULL );
373         }
374
375         if ( be != NULL && be_issuffix( be, dn_in ) ) {
376                 return( NULL );
377         }
378
379         dn = ch_strdup( dn_in );
380
381         inquote = 0;
382
383         for ( s = dn; *s; s++ ) {
384                 if ( *s == '\\' ) {
385                         if ( *(s + 1) ) {
386                                 s++;
387                         }
388                         continue;
389                 }
390                 if ( inquote ) {
391                         if ( *s == '"' ) {
392                                 inquote = 0;
393                         }
394                 } else {
395                         if ( *s == '"' ) {
396                                 inquote = 1;
397                         } else if ( DN_SEPARATOR( *s ) ) {
398                                 *s = '\0';
399                                 return( dn );
400                         }
401                 }
402         }
403
404         return( dn );
405 }
406
407
408 /*
409  * return a charray of all subtrees to which the DN resides in
410  */
411 char **dn_subtree(
412         Backend *be,
413         const char      *dn )
414 {
415         char *child, *parent;
416         char **subtree = NULL;
417         
418         child = ch_strdup( dn );
419
420         do {
421                 charray_add( &subtree, child );
422
423                 parent = dn_parent( be, child );
424
425                 free( child );
426
427                 child = parent;
428         } while ( child != NULL );
429
430         return subtree;
431 }
432
433
434 /*
435  * dn_issuffix - tells whether suffix is a suffix of dn. Both dn
436  * and suffix must be normalized.
437  */
438
439 int
440 dn_issuffix(
441         const char      *dn,
442         const char      *suffix
443 )
444 {
445         int     dnlen, suffixlen;
446
447         if ( dn == NULL ) {
448                 return( 0 );
449         }
450
451         suffixlen = strlen( suffix );
452         dnlen = strlen( dn );
453
454         if ( suffixlen > dnlen ) {
455                 return( 0 );
456         }
457
458         return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
459 }
460
461 /*
462  * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
463  * build_new_dn().
464  *
465  * Copyright 1999, Juan C. Gomez, All rights reserved.
466  * This software is not subject to any license of Silicon Graphics
467  * Inc. or Purdue University.
468  *
469  * Redistribution and use in source and binary forms are permitted
470  * without restriction or fee of any kind as long as this notice
471  * is preserved.
472  *
473  */
474
475 /* get_next_substring:
476  *
477  * Gets next substring in s, using d (or the end of the string '\0') as a
478  * string delimiter, and places it in a duplicated memory space. Leading
479  * spaces are ignored. String s **must** be null-terminated.
480  */
481
482 static char *
483 get_next_substring( const char * s, char d )
484 {
485
486         char    *str, *r;
487
488         r = str = ch_malloc( strlen(s) + 1 );
489
490         /* Skip leading spaces */
491         
492         while ( *s && ASCII_SPACE(*s) ) {
493                 s++;
494         }
495         
496         /* Copy word */
497
498         while ( *s && (*s != d) ) {
499
500                 /* Don't stop when you see trailing spaces may be a multi-word
501                 * string, i.e. name=John Doe!
502                 */
503
504                 *str++ = *s++;
505         }
506         
507         *str = '\0';
508         
509         return r;
510         
511 }
512
513
514 /* rdn_attr_type:
515  *
516  * Given a string (i.e. an rdn) of the form:
517  *       "attribute_type = attribute_value"
518  * this function returns the type of an attribute, that is the
519  * string "attribute_type" which is placed in newly allocated
520  * memory. The returned string will be null-terminated.
521  */
522
523 char * rdn_attr_type( const char * s )
524 {
525         return get_next_substring( s, '=' );
526 }
527
528
529 /* rdn_attr_value:
530  *
531  * Given a string (i.e. an rdn) of the form:
532  *       "attribute_type = attribute_value"
533  * this function returns "attribute_type" which is placed in newly allocated
534  * memory. The returned string will be null-terminated and may contain
535  * spaces (i.e. "John Doe\0").
536  */
537
538 char *
539 rdn_attr_value( const char * rdn )
540 {
541
542         const char      *str;
543
544         if ( (str = strchr( rdn, '=' )) != NULL ) {
545                 return get_next_substring(++str, '\0');
546         }
547
548         return NULL;
549
550 }
551
552
553 /* rdn_attrs:
554  *
555  * Given a string (i.e. an rdn) of the form:
556  *   "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
557  * this function stores the types of the attributes in ptypes, that is the
558  * array of strings "attribute_type" which is placed in newly allocated
559  * memory, and the values of the attributes in pvalues, that is the
560  * array of strings "attribute_value" which is placed in newly allocated
561  * memory. Returns 0 on success, -1 on failure.
562  *
563  * note: got part of the code from dn_validate
564  */
565
566 int
567 rdn_attrs( const char * rdn_in, char ***ptypes, char ***pvalues)
568 {
569         char **parts, **p;
570
571         *ptypes = NULL;
572         *pvalues = NULL;
573
574         /*
575          * explode the rdn in parts
576          */
577         parts = ldap_explode_rdn( rdn_in, 0 );
578
579         if ( parts == NULL ) {
580                 return( -1 );
581         }
582
583         for ( p = parts; p[0]; p++ ) {
584                 char *s, *e, *d;
585                 
586                 /* split each rdn part in type value */
587                 s = strchr( p[0], '=' );
588                 if ( s == NULL ) {
589                         charray_free( *ptypes );
590                         charray_free( *pvalues );
591                         charray_free( parts );
592                         return( -1 );
593                 }
594                 
595                 /* type should be fine */
596                 charray_add_n( ptypes, p[0], ( s-p[0] ) );
597
598                 /* value needs to be unescaped
599                  * (maybe this should be moved to ldap_explode_rdn?) */
600                 for ( e = d = s + 1; e[0]; e++ ) {
601                         if ( *e != '\\' ) {
602                                 *d++ = *e;
603                         }
604                 }
605                 d[0] = '\0';
606                 charray_add( pvalues, s + 1 );
607         }
608
609         /* free array */
610         charray_free( parts );
611
612         return( 0 );
613 }
614
615
616 /* rdn_validate:
617  *
618  * 1 if rdn is a legal rdn;
619  * 0 otherwise (including a sequence of rdns)
620  *
621  * note: got it from dn_rdn; it should be rewritten
622  * according to dn_validate
623  */
624 int
625 rdn_validate( const char * rdn )
626 {
627         int     inquote;
628
629         if ( rdn == NULL ) {
630                 return( 0 );
631         }
632
633         if ( strchr( rdn, '=' ) == NULL ) {
634                 return( 0 );
635         }
636
637         while ( *rdn && ASCII_SPACE( *rdn ) ) {
638                 rdn++;
639         }
640
641         if( *rdn == '\0' ) {
642                 return( 0 );
643         }
644
645         inquote = 0;
646
647         for ( ; *rdn; rdn++ ) {
648                 if ( *rdn == '\\' ) {
649                         if ( *(rdn + 1) ) {
650                                 rdn++;
651                         }
652                         continue;
653                 }
654                 if ( inquote ) {
655                         if ( *rdn == '"' ) {
656                                 inquote = 0;
657                         }
658                 } else {
659                         if ( *rdn == '"' ) {
660                                 inquote = 1;
661                         } else if ( DN_SEPARATOR( *rdn ) ) {
662                                 return( 0 );
663                         }
664                 }
665         }
666
667         return( 1 );
668 }
669
670
671 /* build_new_dn:
672  *
673  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
674  * renamed.
675  *
676  * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
677  */
678
679 void
680 build_new_dn( char ** new_dn,
681         const char *e_dn,
682         const char * p_dn,
683         const char * newrdn )
684 {
685
686         if ( p_dn == NULL ) {
687                 *new_dn = ch_strdup( newrdn );
688                 return;
689         }
690
691         *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
692
693         strcpy( *new_dn, newrdn );
694         strcat( *new_dn, "," );
695         strcat( *new_dn, p_dn );
696 }