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