]> git.sur5r.net Git - openldap/blob - contrib/tweb/dn.c
Fix dynamic linking dependencies for NT and Unix
[openldap] / contrib / tweb / dn.c
1 /*_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 *                                                                          *
3 * dn.c.......                                                              *
4 *                                                                          *
5 * Function:..DN-Handling-Functions                                         *
6 *                                                                          *
7 *            from LDAP3.2 University of Michigan                           *
8 *                                                                          *
9 *                                                                          *
10 *                                                                          *
11 * Authors:...Dr. Kurt Spanier & Bernhard Winkler,                          *
12 *            Zentrum fuer Datenverarbeitung, Bereich Entwicklung           *
13 *            neuer Dienste, Universitaet Tuebingen, GERMANY                *
14 *                                                                          *
15 *                                       ZZZZZ  DDD    V   V                *
16 *            Creation date:                Z   D  D   V   V                *
17 *            April 24 1996                Z    D   D   V V                 *
18 *            Last modification:          Z     D  D    V V                 *
19 *            September 13 1999          ZZZZ   DDD      V                  *
20 *                                                                          *
21 _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_*/
22
23 /*
24  * $Id: dn.c,v 1.8 1999/09/13 13:47:44 zrnsk01 Exp $
25  *
26  */
27
28 /* dn.c - routines for dealing with distinguished names */
29
30 #include "tgeneral.h"
31 #include "tglobal.h"
32 #include "strng_exp.h"
33 #include "dn.h"
34
35 #if OL_LDAPV == 2
36 #define LDAP_DEBUG_ANY  0xffff
37 #endif
38
39
40 /*
41  * dn_normalize - put dn into a canonical format.  the dn is
42  * normalized in place, as well as returned.
43  */
44
45 PUBLIC char * dn_normalize( dn )
46 char *dn;
47 {
48         char    *d, *s;
49         int     state, gotesc;
50
51         /* Debug( LDAP_DEBUG_TRACE, "=> dn_normalize \"%s\"\n", dn, 0, 0 ); */
52
53         gotesc = 0;
54         state = B4TYPE;
55         for ( d = s = dn; *s; s++ ) {
56                 switch ( state ) {
57                 case B4TYPE:
58                         if ( ! SPACE( *s ) ) {
59                                 state = INTYPE;
60                                 *d++ = *s;
61                         }
62                         break;
63                 case INTYPE:
64                         if ( *s == '=' ) {
65                                 state = B4VALUE;
66                                 *d++ = *s;
67                         } else if ( SPACE( *s ) ) {
68                                 state = B4EQUAL;
69                         } else {
70                                 *d++ = *s;
71                         }
72                         break;
73                 case B4EQUAL:
74                         if ( *s == '=' ) {
75                                 state = B4VALUE;
76                                 *d++ = *s;
77                         } else if ( ! SPACE( *s ) ) {
78                                 /* not a valid dn - but what can we do here? */
79                                 *d++ = *s;
80                         }
81                         break;
82                 case B4VALUE:
83                         if ( *s == '"' ) {
84                                 state = INQUOTEDVALUE;
85                                 *d++ = *s;
86                         } else if ( ! SPACE( *s ) ) { 
87                                 state = INVALUE;
88                                 *d++ = *s;
89                         }
90                         break;
91                 case INVALUE:
92                         if ( !gotesc && SEPARATOR( *s ) ) {
93                                 while ( SPACE( *(d - 1) ) )
94                                         d--;
95                                 state = B4TYPE;
96                                 if ( *s == '+' ) {
97                                         *d++ = *s;
98                                 } else {
99                                         *d++ = ',';
100                                 }
101                         } else if ( gotesc && !NEEDSESCAPE( *s ) &&
102                             !SEPARATOR( *s ) ) {
103                                 *--d = *s;
104                                 d++;
105                         } else {
106                                 *d++ = *s;
107                         }
108                         break;
109                 case INQUOTEDVALUE:
110                         if ( !gotesc && *s == '"' ) {
111                                 state = B4SEPARATOR;
112                                 *d++ = *s;
113                         } else if ( gotesc && !NEEDSESCAPE( *s ) ) {
114                                 *--d = *s;
115                                 d++;
116                         } else {
117                                 *d++ = *s;
118                         }
119                         break;
120                 case B4SEPARATOR:
121                         if ( SEPARATOR( *s ) ) {
122                                 state = B4TYPE;
123                                 *d++ = *s;
124                         }
125                         break;
126                 default:
127
128 #if OL_LDAPV >= 2
129
130             if ( ldap_debug & LDAP_DEBUG_ANY )
131                 fprintf( stderr, "dn_normalize - unknown state %d\n", state );
132
133             if ( ldap_syslog & LDAP_DEBUG_ANY )
134                 syslog( ldap_syslog_level,
135                              "dn_normalize - unknown state %d\n", state );
136
137 #else
138                         Debug( LDAP_DEBUG_ANY,
139                             "dn_normalize - unknown state %d\n", state, 0, 0 );
140 #endif
141
142                         break;
143                 }
144                 if ( *s == '\\' ) {
145                         gotesc = 1;
146                 } else {
147                         gotesc = 0;
148                 }
149         }
150         *d = '\0';
151
152         /* Debug( LDAP_DEBUG_TRACE, "<= dn_normalize \"%s\"\n", dn, 0, 0 ); */
153         return( dn );
154 }
155 /* end of function: dn_normalize */
156
157 /*
158  * dn_normalize_case - put dn into a canonical form suitable for storing
159  * in a hash database.  this involves normalizing the case as well as
160  * the format.  the dn is normalized in place as well as returned.
161  */
162
163 PUBLIC char * dn_normalize_case( dn )
164 char *dn;
165 {
166
167         /* normalize format */
168         dn_normalize( dn );
169
170         /* normalize case */
171         str_toupper( dn );
172
173         return( dn );
174 }
175 /* end of function: dn_normalize_case */
176
177 /*
178  * dn_issuffix - tells whether suffix is a suffix of dn.  both dn
179  * and suffix must be normalized.
180  */
181
182 PUBLIC int dn_issuffix( dn, suffix )
183 char    *dn;
184 char    *suffix;
185 {
186         int     dnlen, suffixlen;
187
188         if ( dn == NULL ) {
189                 return( 0 );
190         }
191
192         suffixlen = strlen( suffix );
193         dnlen = strlen( dn );
194
195         if ( suffixlen > dnlen ) {
196                 return( 0 );
197         }
198
199         return( strcasecmp( dn + dnlen - suffixlen, suffix ) == 0 );
200 }
201 /* end of function: dn_issuffix */
202