dntest.c 7.82 KB
Newer Older
1
/* dntest.c -- OpenLDAP DN API Test Program */
2
/* $OpenLDAP$ */
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 1998-2010 The OpenLDAP Foundation.
6
7
8
9
10
11
12
13
14
15
16
17
18
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENT:
 * This program was initially developed by Pierangelo Masarati <ando@OpenLDAP.org>
 * for inclusion in OpenLDAP Software.
19
 */
20

21
22
23
24
25
26
/*
 * This program is designed to test the ldap_str2dn/ldap_dn2str
 * functions
 */
#include "portable.h"

27
28
#include <stdio.h>

29
30
#include <ac/stdlib.h>
#include <ac/string.h>
31
#include <ac/unistd.h>
32
33

#include <ldap.h>
34
35
36

#include "ldap-int.h"

37
38
39
40
41
42
#include "ldif.h"
#include "lutil.h"
#include "lutil_ldap.h"
#include "ldap_defaults.h"

int
43
main( int argc, char *argv[] )
44
{
45
	int 		rc, i, debug = 0, f2 = 0;
Howard Chu's avatar
Howard Chu committed
46
	unsigned 	flags[ 2 ] = { 0U, 0 };
47
	char		*strin, *str = NULL, buf[ 1024 ];
Howard Chu's avatar
Howard Chu committed
48
	LDAPDN		dn, dn2 = NULL;
49

50
51
52
53
54
55
56
57
58
59
60
61
	while ( 1 ) {
		int opt = getopt( argc, argv, "d:" );

		if ( opt == EOF ) {
			break;
		}

		switch ( opt ) {
		case 'd':
			debug = atoi( optarg );
			break;
		}
62
63
	}

64
65
66
67
68
69
	optind--;
	argc -= optind;
	argv += optind;

	if ( argc < 2 ) {
		fprintf( stderr, "usage: dntest <dn> [flags-in[,...]] [flags-out[,...]]\n\n" );
70
71
72
		fprintf( stderr, "\tflags-in:   V3,V2,DCE,<flags>\n" );
		fprintf( stderr, "\tflags-out:  V3,V2,UFN,DCE,AD,<flags>\n\n" );
		fprintf( stderr, "\t<flags>: PRETTY,PEDANTIC,NOSPACES,NOONESPACE\n\n" );
73
		return( 0 );
74
	}
75
76
77
78
79
80

	if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
		fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
	}
	if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
		fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
81
82
	}

83
	if ( strcmp( argv[ 1 ], "-" ) == 0 ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
84
85
86
		size_t len = fgets( buf, sizeof( buf ), stdin ) ? strlen( buf ) : 0;

		if ( len == 0 || buf[ --len ] == '\n' ) {
87
			buf[ len ] = '\0';
88
		}
89
		strin = buf;
90
	} else {
91
		strin = argv[ 1 ];
92
93
	}

94
95
	if ( argc >= 3 ) {
		for ( i = 0; i < argc - 2; i++ ) {
96
			char *s, *e;
97
98
99
100
			for ( s = argv[ 2 + i ]; s; s = e ) {
				e = strchr( s, ',' );
				if ( e != NULL ) {
					e[ 0 ] = '\0';
101
102
103
					e++;
				}
	
104
105
106
107
108
109
110
111
112
113
				if ( !strcasecmp( s, "V3" ) ) {
					flags[ i ] |= LDAP_DN_FORMAT_LDAPV3;
				} else if ( !strcasecmp( s, "V2" ) ) {
					flags[ i ] |= LDAP_DN_FORMAT_LDAPV2;
				} else if ( !strcasecmp( s, "DCE" ) ) {
					flags[ i ] |= LDAP_DN_FORMAT_DCE;
				} else if ( !strcasecmp( s, "UFN" ) ) {
					flags[ i ] |= LDAP_DN_FORMAT_UFN;
				} else if ( !strcasecmp( s, "AD" ) ) {
					flags[ i ] |= LDAP_DN_FORMAT_AD_CANONICAL;
114
115
				} else if ( !strcasecmp( s, "PRETTY" ) ) {
					flags[ i ] |= LDAP_DN_PRETTY;
116
117
				} else if ( !strcasecmp( s, "PEDANTIC" ) ) {
					flags[ i ] |= LDAP_DN_PEDANTIC;
118
119
120
121
				} else if ( !strcasecmp( s, "NOSPACES" ) ) {
					flags[ i ] |= LDAP_DN_P_NOLEADTRAILSPACES;
				} else if ( !strcasecmp( s, "NOONESPACE" ) ) {
					flags[ i ] |= LDAP_DN_P_NOSPACEAFTERRDN;
122
123
124
125
				}
			}
		}
	}
126

Howard Chu's avatar
Howard Chu committed
127
128
129
	if ( flags[ 1 ] == 0 )
		flags[ 1 ] = LDAP_DN_FORMAT_LDAPV3;

130
	f2 = 1;
131
132

	rc = ldap_str2dn( strin, &dn, flags[ 0 ] );
133

134
135
	if ( rc == LDAP_SUCCESS ) {
		int i;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
136
		if ( dn ) {
Howard Chu's avatar
Howard Chu committed
137
138
			for ( i = 0; dn[ i ]; i++ ) {
				LDAPRDN		rdn = dn[ i ];
139
				char		*rstr = NULL;
140

Kurt Zeilenga's avatar
Kurt Zeilenga committed
141
142
143
144
				if ( ldap_rdn2str( rdn, &rstr, flags[ f2 ] ) ) {
					fprintf( stdout, "\tldap_rdn2str() failed\n" );
					continue;
				}
145

Kurt Zeilenga's avatar
Kurt Zeilenga committed
146
147
148
149
150
				fprintf( stdout, "\tldap_rdn2str() = \"%s\"\n", rstr );
				ldap_memfree( rstr );
			}
		} else {
			fprintf( stdout, "\tempty DN\n" );
151
152
153
		}
	}

154
	str = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
155
156
157
	if ( rc == LDAP_SUCCESS &&
		ldap_dn2str( dn, &str, flags[ f2 ] ) == LDAP_SUCCESS )
	{
158
		char	**values, *tmp, *tmp2, *str2 = NULL;
159
160
		int	n;
		
161
162
163
		fprintf( stdout, "\nldap_dn2str(ldap_str2dn(\"%s\"))\n"
				"\t= \"%s\"\n", strin, str );
			
164
165
166
167
168
169
170
		switch ( flags[ f2 ] & LDAP_DN_FORMAT_MASK ) {
		case LDAP_DN_FORMAT_UFN:
		case LDAP_DN_FORMAT_AD_CANONICAL:
			return( 0 );

		case LDAP_DN_FORMAT_LDAPV3:
		case LDAP_DN_FORMAT_LDAPV2:
171
			n = ldap_dn2domain( strin, &tmp );
172
			if ( n ) {
173
174
175
				fprintf( stdout, "\nldap_dn2domain(\"%s\") FAILED\n", strin );
			} else {
				fprintf( stdout, "\nldap_dn2domain(\"%s\")\n"
176
					"\t= \"%s\"\n", strin, tmp ? tmp : "" );
177
178
179
			}
			ldap_memfree( tmp );

180
			tmp = ldap_dn2ufn( strin );
181
			fprintf( stdout, "\nldap_dn2ufn(\"%s\")\n"
182
					"\t= \"%s\"\n", strin, tmp ? tmp : "" );
183
			ldap_memfree( tmp );
184

185
186
			tmp = ldap_dn2dcedn( strin );
			fprintf( stdout, "\nldap_dn2dcedn(\"%s\")\n"
187
					"\t= \"%s\"\n", strin, tmp ? tmp : "" );
188
189
			tmp2 = ldap_dcedn2dn( tmp );
			fprintf( stdout, "\nldap_dcedn2dn(\"%s\")\n"
190
191
					"\t= \"%s\"\n",
					tmp ? tmp : "", tmp2 ? tmp2 : "" );
192
193
			ldap_memfree( tmp );
			ldap_memfree( tmp2 );
194

195
196
			tmp = ldap_dn2ad_canonical( strin );
			fprintf( stdout, "\nldap_dn2ad_canonical(\"%s\")\n"
197
					"\t= \"%s\"\n", strin, tmp ? tmp : "" );
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
			ldap_memfree( tmp );

			fprintf( stdout, "\nldap_explode_dn(\"%s\"):\n", str );
			values = ldap_explode_dn( str, 0 );
			for ( n = 0; values && values[ n ]; n++ ) {
				char	**vv;
				int	nn;
				
				fprintf( stdout, "\t\"%s\"\n", values[ n ] );

				fprintf( stdout, "\tldap_explode_rdn(\"%s\")\n",
						values[ n ] );
				vv = ldap_explode_rdn( values[ n ], 0 );
				for ( nn = 0; vv && vv[ nn ]; nn++ ) {
					fprintf( stdout, "\t\t'%s'\n", 
							vv[ nn ] );
				}
				LDAP_VFREE( vv );

				fprintf( stdout, "\tldap_explode_rdn(\"%s\")"
					       " (no types)\n", values[ n ] );
				vv = ldap_explode_rdn( values[ n ], 1 );
				for ( nn = 0; vv && vv[ nn ]; nn++ ) {
					fprintf( stdout, "\t\t\t\"%s\"\n", 
							vv[ nn ] );
				}
				LDAP_VFREE( vv );
				
			}
			LDAP_VFREE( values );

			fprintf( stdout, "\nldap_explode_dn(\"%s\")"
					" (no types):\n", str );
			values = ldap_explode_dn( str, 1 );
			for ( n = 0; values && values[ n ]; n++ ) {
				fprintf( stdout, "\t\"%s\"\n", values[ n ] );
			}
			LDAP_VFREE( values );

			break;
		}
239
240

		dn2 = NULL;	
241
		rc = ldap_str2dn( str, &dn2, flags[ f2 ] );
242
		str2 = NULL;
243
		if ( rc == LDAP_SUCCESS && 
244
				ldap_dn2str( dn2, &str2, flags[ f2 ] )
245
				== LDAP_SUCCESS ) {
246
247
			int 	iRDN;
			
248
249
250
			fprintf( stdout, "\n\"%s\"\n\t == \"%s\" ? %s\n", 
				str, str2, 
				strcmp( str, str2 ) == 0 ? "yes" : "no" );
251

252
253
			if( dn != NULL && dn2 == NULL ) {
				fprintf( stdout, "dn mismatch\n" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
254
			} else if (( dn != NULL ) && (dn2 != NULL))
Howard Chu's avatar
Howard Chu committed
255
				for ( iRDN = 0; dn[ iRDN ] && dn2[ iRDN ]; iRDN++ )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
256
			{
Howard Chu's avatar
Howard Chu committed
257
258
				LDAPRDN 	r = dn[ iRDN ];
				LDAPRDN 	r2 = dn2[ iRDN ];
259
260
				int 		iAVA;
				
Howard Chu's avatar
Howard Chu committed
261
262
263
				for ( iAVA = 0; r[ iAVA ] && r2[ iAVA ]; iAVA++ ) {
					LDAPAVA		*a = r[ iAVA ];
					LDAPAVA		*a2 = r2[ iAVA ];
264

265
					if ( a->la_attr.bv_len != a2->la_attr.bv_len ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
266
						fprintf( stdout, "ava(%d), rdn(%d) attr len mismatch (%ld->%ld)\n", 
267
268
269
270
271
272
273
274
275
								iAVA + 1, iRDN + 1,
								a->la_attr.bv_len, a2->la_attr.bv_len );
					} else if ( memcmp( a->la_attr.bv_val, a2->la_attr.bv_val, a->la_attr.bv_len ) ) {
						fprintf( stdout, "ava(%d), rdn(%d) attr mismatch\n", 
								iAVA + 1, iRDN + 1 );
					} else if ( a->la_flags != a2->la_flags ) {
						fprintf( stdout, "ava(%d), rdn(%d) flag mismatch (%x->%x)\n", 
								iAVA + 1, iRDN + 1, a->la_flags, a2->la_flags );
					} else if ( a->la_value.bv_len != a2->la_value.bv_len ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
276
						fprintf( stdout, "ava(%d), rdn(%d) value len mismatch (%ld->%ld)\n", 
277
278
279
280
281
								iAVA + 1, iRDN + 1, 
								a->la_value.bv_len, a2->la_value.bv_len );
					} else if ( memcmp( a->la_value.bv_val, a2->la_value.bv_val, a->la_value.bv_len ) ) {
						fprintf( stdout, "ava(%d), rdn(%d) value mismatch\n", 
								iAVA + 1, iRDN + 1 );
282
283
284
285
					}
				}
			}
			
286
			ldap_dnfree( dn2 );
287
288
289
			ldap_memfree( str2 );
		}
		ldap_memfree( str );
290
	}
291
	ldap_dnfree( dn );
292

293
294
295
	/* note: dn is not freed */

	return( 0 );
296
}