1 /* ldapurl -- a tool for generating LDAP URLs */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2017 The OpenLDAP Foundation.
6 * Portions Copyright 2008 Pierangelo Masarati, SysNet
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
17 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms are permitted
21 * provided that this notice is preserved and that due credit is given
22 * to the University of Michigan at Ann Arbor. The name of the
23 * University may not be used to endorse or promote products derived
24 * from this software without specific prior written permission. This
25 * software is provided ``as is'' without express or implied warranty.
28 * This work was originally developed by Pierangelo Masarati
29 * for inclusion in OpenLDAP software.
34 #include <ac/stdlib.h>
36 #include <ac/unistd.h>
45 fprintf( stderr, _("usage: %s [options]\n\n"), "ldapurl" );
46 fprintf( stderr, _("generates RFC 4516 LDAP URL with extensions\n\n" ) );
47 fprintf( stderr, _("URL options:\n"));
48 fprintf( stderr, _(" -a attrs comma separated list of attributes\n" ) );
49 fprintf( stderr, _(" -b base (RFC 4514 LDAP DN)\n" ) );
50 fprintf( stderr, _(" -E ext (format: \"ext=value\"; multiple occurrences allowed)\n" ) );
51 fprintf( stderr, _(" -f filter (RFC 4515 LDAP filter)\n" ) );
52 fprintf( stderr, _(" -h host \n" ) );
53 fprintf( stderr, _(" -p port (default: 389 for ldap, 636 for ldaps)\n" ) );
54 fprintf( stderr, _(" -s scope (RFC 4511 searchScope and extensions)\n" ) );
55 fprintf( stderr, _(" -S scheme (RFC 4516 LDAP URL scheme and extensions)\n" ) );
60 do_uri_create( LDAPURLDesc *lud )
64 if ( lud->lud_scheme == NULL ) {
65 lud->lud_scheme = "ldap";
68 if ( lud->lud_port == -1 ) {
69 if ( strcasecmp( lud->lud_scheme, "ldap" ) == 0 ) {
70 lud->lud_port = LDAP_PORT;
72 } else if ( strcasecmp( lud->lud_scheme, "ldaps" ) == 0 ) {
73 lud->lud_port = LDAPS_PORT;
75 } else if ( strcasecmp( lud->lud_scheme, "ldapi" ) == 0 ) {
84 if ( lud->lud_scope == -1 ) {
85 lud->lud_scope = LDAP_SCOPE_DEFAULT;
88 uri = ldap_url_desc2str( lud );
90 if ( lud->lud_attrs != NULL ) {
91 ldap_charray_free( lud->lud_attrs );
92 lud->lud_attrs = NULL;
95 if ( lud->lud_exts != NULL ) {
96 free( lud->lud_exts );
101 fprintf( stderr, "unable to generate URI\n" );
102 exit( EXIT_FAILURE );
105 printf( "%s\n", uri );
112 do_uri_explode( const char *uri )
117 rc = ldap_url_parse( uri, &lud );
118 if ( rc != LDAP_URL_SUCCESS ) {
119 fprintf( stderr, "unable to parse URI \"%s\"\n", uri );
123 if ( lud->lud_scheme != NULL && lud->lud_scheme[0] != '\0' ) {
124 printf( "scheme: %s\n", lud->lud_scheme );
127 if ( lud->lud_host != NULL && lud->lud_host[0] != '\0' ) {
128 printf( "host: %s\n", lud->lud_host );
131 if ( lud->lud_port != 0 ) {
132 printf( "port: %d\n", lud->lud_port );
135 if ( lud->lud_dn != NULL && lud->lud_dn[0] != '\0' ) {
136 printf( "dn: %s\n", lud->lud_dn );
139 if ( lud->lud_attrs != NULL ) {
142 for ( i = 0; lud->lud_attrs[i] != NULL; i++ ) {
143 printf( "selector: %s\n", lud->lud_attrs[i] );
147 if ( lud->lud_scope != LDAP_SCOPE_DEFAULT ) {
148 printf( "scope: %s\n", ldap_pvt_scope2str( lud->lud_scope ) );
151 if ( lud->lud_filter != NULL && lud->lud_filter[0] != '\0' ) {
152 printf( "filter: %s\n", lud->lud_filter );
155 if ( lud->lud_exts != NULL ) {
158 for ( i = 0; lud->lud_exts[i] != NULL; i++ ) {
159 printf( "extension: %s\n", lud->lud_exts[i] );
162 ldap_free_urldesc( lud );
168 main( int argc, char *argv[])
170 LDAPURLDesc lud = { 0 };
179 int opt = getopt( argc, argv, "S:h:p:b:a:s:f:E:H:" );
187 fprintf( stderr, "option -H incompatible with previous options\n" );
192 fprintf( stderr, "URI already provided\n" );
210 fprintf( stderr, "option -%c incompatible with -H\n", opt );
218 if ( lud.lud_scheme != NULL ) {
219 fprintf( stderr, "scheme already provided\n" );
222 lud.lud_scheme = optarg;
226 if ( lud.lud_host != NULL ) {
227 fprintf( stderr, "host already provided\n" );
230 lud.lud_host = optarg;
234 if ( lud.lud_port != -1 ) {
235 fprintf( stderr, "port already provided\n" );
239 if ( lutil_atoi( &lud.lud_port, optarg ) ) {
240 fprintf( stderr, "unable to parse port \"%s\"\n", optarg );
246 if ( lud.lud_dn != NULL ) {
247 fprintf( stderr, "base already provided\n" );
254 if ( lud.lud_attrs != NULL ) {
255 fprintf( stderr, "attrs already provided\n" );
258 lud.lud_attrs = ldap_str2charray( optarg, "," );
259 if ( lud.lud_attrs == NULL ) {
260 fprintf( stderr, "unable to parse attrs list \"%s\"\n", optarg );
266 if ( lud.lud_scope != -1 ) {
267 fprintf( stderr, "scope already provided\n" );
271 lud.lud_scope = ldap_pvt_str2scope( optarg );
272 if ( lud.lud_scope == -1 ) {
273 fprintf( stderr, "unable to parse scope \"%s\"\n", optarg );
279 if ( lud.lud_filter != NULL ) {
280 fprintf( stderr, "filter already provided\n" );
283 lud.lud_filter = optarg;
287 lud.lud_exts = (char **)realloc( lud.lud_exts,
288 sizeof( char * ) * ( nexts + 2 ) );
289 lud.lud_exts[ nexts++ ] = optarg;
290 lud.lud_exts[ nexts ] = NULL;
294 assert( opt != 'H' );
300 return do_uri_explode( uri );
304 return do_uri_create( &lud );