xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/urltest.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: urltest.c,v 1.1.1.2 2010/03/08 02:14:20 lukem Exp $	*/
2 
3 /* urltest.c -- OpenLDAP URL API Test Program */
4 /* OpenLDAP: pkg/ldap/libraries/libldap/urltest.c,v 1.1.2.5 2009/01/22 00:00:56 kurt Exp */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2009 The OpenLDAP Foundation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENT:
19  * This program was initially developed by Pierangelo Masarati
20  * <ando@OpenLDAP.org> for inclusion in OpenLDAP Software.
21  */
22 
23 /*
24  * This program is designed to test the ldap_url_* functions
25  */
26 
27 #include "portable.h"
28 
29 #include <stdio.h>
30 
31 #include <ac/stdlib.h>
32 #include <ac/string.h>
33 #include <ac/unistd.h>
34 
35 #include <ldap.h>
36 
37 #include "ldap-int.h"
38 
39 #include "ldap_defaults.h"
40 
41 int
42 main(int argc, char *argv[])
43 {
44 	const char	*url,
45 			*scope = NULL;
46 	LDAPURLDesc	*lud;
47 	enum {
48 		IS_LDAP = 0,
49 		IS_LDAPS,
50 		IS_LDAPI
51 	} type = IS_LDAP;
52 	int		rc;
53 
54 	if ( argc != 2 ) {
55 		fprintf( stderr, "usage: urltest <url>\n" );
56 		exit( EXIT_FAILURE );
57 	}
58 
59 	url = argv[ 1 ];
60 
61 	if ( ldap_is_ldaps_url( url ) ) {
62 		fprintf( stdout, "LDAPS url\n" );
63 		type = IS_LDAPS;
64 
65 	} else if ( ldap_is_ldapi_url( url ) ) {
66 		fprintf( stdout, "LDAPI url\n" );
67 		type = IS_LDAPI;
68 
69 	} else if ( ldap_is_ldap_url( url ) ) {
70 		fprintf( stdout, "generic LDAP url\n" );
71 
72 	} else {
73 		fprintf( stderr, "Need a valid LDAP url\n" );
74 		exit( EXIT_FAILURE );
75 	}
76 
77 	rc = ldap_url_parse( url, &lud );
78 	if ( rc != LDAP_URL_SUCCESS ) {
79 		fprintf( stderr, "ldap_url_parse(%s) failed (%d)\n", url, rc );
80 		exit( EXIT_FAILURE );
81 	}
82 
83 	fprintf( stdout, "PROTO: %s\n", lud->lud_scheme );
84 	switch ( type ) {
85 	case IS_LDAPI:
86 		fprintf( stdout, "PATH: %s\n", lud->lud_host );
87 		break;
88 
89 	default:
90 		fprintf( stdout, "HOST: %s\n", lud->lud_host );
91 		if ( lud->lud_port != 0 ) {
92 			fprintf( stdout, "PORT: %d\n", lud->lud_port );
93 		}
94 	}
95 
96 	if ( lud->lud_dn && lud->lud_dn[ 0 ] ) {
97 		fprintf( stdout, "DN: %s\n", lud->lud_dn );
98 	}
99 
100 	if ( lud->lud_attrs ) {
101 		int	i;
102 
103 		fprintf( stdout, "ATTRS:\n" );
104 		for ( i = 0; lud->lud_attrs[ i ]; i++ ) {
105 			fprintf( stdout, "\t%s\n", lud->lud_attrs[ i ] );
106 		}
107 	}
108 
109 	scope = ldap_pvt_scope2str( lud->lud_scope );
110 	if ( scope ) {
111 		fprintf( stdout, "SCOPE: %s\n", scope );
112 	}
113 
114 	if ( lud->lud_filter ) {
115 		fprintf( stdout, "FILTER: %s\n", lud->lud_filter );
116 	}
117 
118 	if ( lud->lud_exts ) {
119 		int	i;
120 
121 		fprintf( stdout, "EXTS:\n" );
122 		for ( i = 0; lud->lud_exts[ i ]; i++ ) {
123 			fprintf( stdout, "\t%s\n", lud->lud_exts[ i ] );
124 		}
125 	}
126 
127 	fprintf( stdout, "URL: %s\n", ldap_url_desc2str( lud ));
128 
129 	return EXIT_SUCCESS;
130 }
131