1 /* ldapurl -- a tool for generating LDAP URLs */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2010 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] );
167 main( int argc, char *argv[])
169 LDAPURLDesc lud = { 0 };
178 int opt = getopt( argc, argv, "S:h:p:b:a:s:f:E:H:" );
186 fprintf( stderr, "option -H incompatible with previous options\n" );
191 fprintf( stderr, "URI already provided\n" );
209 fprintf( stderr, "option -%c incompatible with -H\n", opt );
217 if ( lud.lud_scheme != NULL ) {
218 fprintf( stderr, "scheme already provided\n" );
221 lud.lud_scheme = optarg;
225 if ( lud.lud_host != NULL ) {
226 fprintf( stderr, "host already provided\n" );
229 lud.lud_host = optarg;
233 if ( lud.lud_port != -1 ) {
234 fprintf( stderr, "port already provided\n" );
238 if ( lutil_atoi( &lud.lud_port, optarg ) ) {
239 fprintf( stderr, "unable to parse port \"%s\"\n", optarg );
245 if ( lud.lud_dn != NULL ) {
246 fprintf( stderr, "base already provided\n" );
253 if ( lud.lud_attrs != NULL ) {
254 fprintf( stderr, "attrs already provided\n" );
257 lud.lud_attrs = ldap_str2charray( optarg, "," );
258 if ( lud.lud_attrs == NULL ) {
259 fprintf( stderr, "unable to parse attrs list \"%s\"\n", optarg );
265 if ( lud.lud_scope != -1 ) {
266 fprintf( stderr, "scope already provided\n" );
270 lud.lud_scope = ldap_pvt_str2scope( optarg );
271 if ( lud.lud_scope == -1 ) {
272 fprintf( stderr, "unable to parse scope \"%s\"\n", optarg );
278 if ( lud.lud_filter != NULL ) {
279 fprintf( stderr, "filter already provided\n" );
282 lud.lud_filter = optarg;
286 lud.lud_exts = (char **)realloc( lud.lud_exts,
287 sizeof( char * ) * ( nexts + 2 ) );
288 lud.lud_exts[ nexts++ ] = optarg;
289 lud.lud_exts[ nexts ] = NULL;
293 assert( opt != 'H' );
299 return do_uri_explode( uri );
303 return do_uri_create( &lud );