xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/urltest.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: urltest.c,v 1.3 2021/08/14 16:14:56 christos Exp $	*/
2 
3 /* urltest.c -- OpenLDAP URL API Test Program */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2021 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 <sys/cdefs.h>
28 __RCSID("$NetBSD: urltest.c,v 1.3 2021/08/14 16:14:56 christos Exp $");
29 
30 #include "portable.h"
31 
32 #include <stdio.h>
33 
34 #include <ac/stdlib.h>
35 #include <ac/string.h>
36 #include <ac/unistd.h>
37 
38 #include <ldap.h>
39 
40 #include "ldap-int.h"
41 
42 #include "ldap_defaults.h"
43 
44 int
main(int argc,char * argv[])45 main(int argc, char *argv[])
46 {
47 	const char	*url,
48 			*scope = NULL;
49 	LDAPURLDesc	*lud;
50 	enum {
51 		IS_LDAP = 0,
52 		IS_LDAPS,
53 		IS_LDAPI
54 	} type = IS_LDAP;
55 	int		rc;
56 
57 	if ( argc != 2 ) {
58 		fprintf( stderr, "usage: urltest <url>\n" );
59 		exit( EXIT_FAILURE );
60 	}
61 
62 	url = argv[ 1 ];
63 
64 	if ( ldap_is_ldaps_url( url ) ) {
65 		fprintf( stdout, "LDAPS url\n" );
66 		type = IS_LDAPS;
67 
68 	} else if ( ldap_is_ldapi_url( url ) ) {
69 		fprintf( stdout, "LDAPI url\n" );
70 		type = IS_LDAPI;
71 
72 	} else if ( ldap_is_ldap_url( url ) ) {
73 		fprintf( stdout, "generic LDAP url\n" );
74 
75 	} else {
76 		fprintf( stderr, "Need a valid LDAP url\n" );
77 		exit( EXIT_FAILURE );
78 	}
79 
80 	rc = ldap_url_parse( url, &lud );
81 	if ( rc != LDAP_URL_SUCCESS ) {
82 		fprintf( stderr, "ldap_url_parse(%s) failed (%d)\n", url, rc );
83 		exit( EXIT_FAILURE );
84 	}
85 
86 	fprintf( stdout, "PROTO: %s\n", lud->lud_scheme );
87 	switch ( type ) {
88 	case IS_LDAPI:
89 		fprintf( stdout, "PATH: %s\n", lud->lud_host );
90 		break;
91 
92 	default:
93 		fprintf( stdout, "HOST: %s\n", lud->lud_host );
94 		if ( lud->lud_port != 0 ) {
95 			fprintf( stdout, "PORT: %d\n", lud->lud_port );
96 		}
97 	}
98 
99 	if ( lud->lud_dn && lud->lud_dn[ 0 ] ) {
100 		fprintf( stdout, "DN: %s\n", lud->lud_dn );
101 	}
102 
103 	if ( lud->lud_attrs ) {
104 		int	i;
105 
106 		fprintf( stdout, "ATTRS:\n" );
107 		for ( i = 0; lud->lud_attrs[ i ]; i++ ) {
108 			fprintf( stdout, "\t%s\n", lud->lud_attrs[ i ] );
109 		}
110 	}
111 
112 	scope = ldap_pvt_scope2str( lud->lud_scope );
113 	if ( scope ) {
114 		fprintf( stdout, "SCOPE: %s\n", scope );
115 	}
116 
117 	if ( lud->lud_filter ) {
118 		fprintf( stdout, "FILTER: %s\n", lud->lud_filter );
119 	}
120 
121 	if ( lud->lud_exts ) {
122 		int	i;
123 
124 		fprintf( stdout, "EXTS:\n" );
125 		for ( i = 0; lud->lud_exts[ i ]; i++ ) {
126 			fprintf( stdout, "\t%s\n", lud->lud_exts[ i ] );
127 		}
128 	}
129 
130 	fprintf( stdout, "URL: %s\n", ldap_url_desc2str( lud ));
131 
132 	return EXIT_SUCCESS;
133 }
134