Back to index

openldap  2.4.31
ldapurl.c
Go to the documentation of this file.
00001 /* ldapurl -- a tool for generating LDAP URLs */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2008-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2008 Pierangelo Masarati, SysNet
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
00018  * All rights reserved.
00019  *
00020  * Redistribution and use in source and binary forms are permitted
00021  * provided that this notice is preserved and that due credit is given
00022  * to the University of Michigan at Ann Arbor.  The name of the
00023  * University may not be used to endorse or promote products derived
00024  * from this software without specific prior written permission.  This
00025  * software is provided ``as is'' without express or implied warranty.
00026  */
00027 /* ACKNOWLEDGEMENTS:
00028  * This work was originally developed by Pierangelo Masarati
00029  * for inclusion in OpenLDAP software.
00030  */
00031 
00032 #include "portable.h"
00033 
00034 #include <ac/stdlib.h>
00035 #include <stdio.h>
00036 #include <ac/unistd.h>
00037 
00038 #include "ldap.h"
00039 #include "ldap_pvt.h"
00040 #include "lutil.h"
00041 
00042 static int
00043 usage(void)
00044 {
00045        fprintf( stderr, _("usage: %s [options]\n\n"), "ldapurl" );
00046        fprintf( stderr, _("generates RFC 4516 LDAP URL with extensions\n\n" ) );
00047        fprintf( stderr, _("URL options:\n"));
00048        fprintf( stderr, _("  -a attrs   comma separated list of attributes\n" ) );
00049        fprintf( stderr, _("  -b base    (RFC 4514 LDAP DN)\n" ) );
00050        fprintf( stderr, _("  -E ext     (format: \"ext=value\"; multiple occurrences allowed)\n" ) );
00051        fprintf( stderr, _("  -f filter  (RFC 4515 LDAP filter)\n" ) );
00052        fprintf( stderr, _("  -h host    \n" ) );
00053        fprintf( stderr, _("  -p port    (default: 389 for ldap, 636 for ldaps)\n" ) );
00054        fprintf( stderr, _("  -s scope   (RFC 4511 searchScope and extensions)\n" ) );
00055        fprintf( stderr, _("  -S scheme  (RFC 4516 LDAP URL scheme and extensions)\n" ) );
00056        exit( EXIT_FAILURE );
00057 }
00058 
00059 static int
00060 do_uri_create( LDAPURLDesc *lud )
00061 {
00062        char   *uri;
00063 
00064        if ( lud->lud_scheme == NULL ) {
00065               lud->lud_scheme = "ldap";
00066        }
00067 
00068        if ( lud->lud_port == -1 ) {
00069               if ( strcasecmp( lud->lud_scheme, "ldap" ) == 0 ) {
00070                      lud->lud_port = LDAP_PORT;
00071 
00072               } else if ( strcasecmp( lud->lud_scheme, "ldaps" ) == 0 ) {
00073                      lud->lud_port = LDAPS_PORT;
00074 
00075               } else if ( strcasecmp( lud->lud_scheme, "ldapi" ) == 0 ) {
00076                      lud->lud_port = 0;
00077 
00078               } else {
00079                      /* forgiving... */
00080                      lud->lud_port = 0;
00081               }
00082        }
00083 
00084        if ( lud->lud_scope == -1 ) {
00085               lud->lud_scope = LDAP_SCOPE_DEFAULT;
00086        }
00087 
00088        uri = ldap_url_desc2str( lud );
00089 
00090        if ( lud->lud_attrs != NULL ) {
00091               ldap_charray_free( lud->lud_attrs );
00092               lud->lud_attrs = NULL;
00093        }
00094 
00095        if ( lud->lud_exts != NULL ) {
00096               free( lud->lud_exts );
00097               lud->lud_exts = NULL;
00098        }
00099 
00100        if ( uri == NULL ) {
00101               fprintf( stderr, "unable to generate URI\n" );
00102               exit( EXIT_FAILURE );
00103        }
00104 
00105        printf( "%s\n", uri );
00106        free( uri );
00107 
00108        return 0;
00109 }
00110 
00111 static int
00112 do_uri_explode( const char *uri )
00113 {
00114        LDAPURLDesc   *lud;
00115        int           rc;
00116 
00117        rc = ldap_url_parse( uri, &lud );
00118        if ( rc != LDAP_URL_SUCCESS ) {
00119               fprintf( stderr, "unable to parse URI \"%s\"\n", uri );
00120               return 1;
00121        }
00122 
00123        if ( lud->lud_scheme != NULL && lud->lud_scheme[0] != '\0' ) {
00124               printf( "scheme: %s\n", lud->lud_scheme );
00125        }
00126 
00127        if ( lud->lud_host != NULL && lud->lud_host[0] != '\0' ) {
00128               printf( "host: %s\n", lud->lud_host );
00129        }
00130 
00131        if ( lud->lud_port != 0 ) {
00132               printf( "port: %d\n", lud->lud_port );
00133        }
00134 
00135        if ( lud->lud_dn != NULL && lud->lud_dn[0] != '\0' ) {
00136               printf( "dn: %s\n", lud->lud_dn );
00137        }
00138 
00139        if ( lud->lud_attrs != NULL ) {
00140               int    i;
00141 
00142               for ( i = 0; lud->lud_attrs[i] != NULL; i++ ) {
00143                      printf( "selector: %s\n", lud->lud_attrs[i] );
00144               }
00145        }
00146 
00147        if ( lud->lud_scope != LDAP_SCOPE_DEFAULT ) {
00148               printf( "scope: %s\n", ldap_pvt_scope2str( lud->lud_scope ) );
00149        }
00150 
00151        if ( lud->lud_filter != NULL && lud->lud_filter[0] != '\0' ) {
00152               printf( "filter: %s\n", lud->lud_filter );
00153        }
00154 
00155        if ( lud->lud_exts != NULL ) {
00156               int    i;
00157 
00158               for ( i = 0; lud->lud_exts[i] != NULL; i++ ) {
00159                      printf( "extension: %s\n", lud->lud_exts[i] );
00160               }
00161        }
00162 
00163        return 0;
00164 }
00165 
00166 int
00167 main( int argc, char *argv[])
00168 {
00169        LDAPURLDesc   lud = { 0 };
00170        char          *uri = NULL;
00171        int           gotlud = 0;
00172        int           nexts = 0;
00173 
00174        lud.lud_port = -1;
00175        lud.lud_scope = -1;
00176 
00177        while ( 1 ) {
00178               int opt = getopt( argc, argv, "S:h:p:b:a:s:f:E:H:" );
00179 
00180               if ( opt == EOF ) {
00181                      break;
00182               }
00183 
00184               if ( opt == 'H' ) {
00185                      if ( gotlud ) {
00186                             fprintf( stderr, "option -H incompatible with previous options\n" );
00187                             usage();
00188                      }
00189 
00190                      if ( uri != NULL ) {
00191                             fprintf( stderr, "URI already provided\n" );
00192                             usage();
00193                      }
00194 
00195                      uri = optarg;
00196                      continue;
00197               }
00198 
00199               switch ( opt ) {
00200               case 'S':
00201               case 'h':
00202               case 'p':
00203               case 'b':
00204               case 'a':
00205               case 's':
00206               case 'f':
00207               case 'E':
00208                      if ( uri != NULL ) {
00209                             fprintf( stderr, "option -%c incompatible with -H\n", opt );
00210                             usage();
00211                      }
00212                      gotlud++;
00213               }
00214 
00215               switch ( opt ) {
00216               case 'S':
00217                      if ( lud.lud_scheme != NULL ) {
00218                             fprintf( stderr, "scheme already provided\n" );
00219                             usage();
00220                      }
00221                      lud.lud_scheme = optarg;
00222                      break;
00223 
00224               case 'h':
00225                      if ( lud.lud_host != NULL ) {
00226                             fprintf( stderr, "host already provided\n" );
00227                             usage();
00228                      }
00229                      lud.lud_host = optarg;
00230                      break;
00231 
00232               case 'p':
00233                      if ( lud.lud_port != -1 ) {
00234                             fprintf( stderr, "port already provided\n" );
00235                             usage();
00236                      }
00237 
00238                      if ( lutil_atoi( &lud.lud_port, optarg ) ) {
00239                             fprintf( stderr, "unable to parse port \"%s\"\n", optarg );
00240                             usage();
00241                      }
00242                      break;
00243 
00244               case 'b':
00245                      if ( lud.lud_dn != NULL ) {
00246                             fprintf( stderr, "base already provided\n" );
00247                             usage();
00248                      }
00249                      lud.lud_dn = optarg;
00250                      break;
00251 
00252               case 'a':
00253                      if ( lud.lud_attrs != NULL ) {
00254                             fprintf( stderr, "attrs already provided\n" );
00255                             usage();
00256                      }
00257                      lud.lud_attrs = ldap_str2charray( optarg, "," );
00258                      if ( lud.lud_attrs == NULL ) {
00259                             fprintf( stderr, "unable to parse attrs list \"%s\"\n", optarg );
00260                             usage();
00261                      }
00262                      break;
00263 
00264               case 's':
00265                      if ( lud.lud_scope != -1 ) {
00266                             fprintf( stderr, "scope already provided\n" );
00267                             usage();
00268                      }
00269 
00270                      lud.lud_scope = ldap_pvt_str2scope( optarg );
00271                      if ( lud.lud_scope == -1 ) {
00272                             fprintf( stderr, "unable to parse scope \"%s\"\n", optarg );
00273                             usage();
00274                      }
00275                      break;
00276 
00277               case 'f':
00278                      if ( lud.lud_filter != NULL ) {
00279                             fprintf( stderr, "filter already provided\n" );
00280                             usage();
00281                      }
00282                      lud.lud_filter = optarg;
00283                      break;
00284 
00285               case 'E':
00286                      lud.lud_exts = (char **)realloc( lud.lud_exts,
00287                             sizeof( char * ) * ( nexts + 2 ) );
00288                      lud.lud_exts[ nexts++ ] = optarg;
00289                      lud.lud_exts[ nexts ] = NULL;
00290                      break;
00291 
00292               default:
00293                      assert( opt != 'H' );
00294                      usage();
00295               }
00296        }
00297 
00298        if ( uri != NULL ) {
00299               return do_uri_explode( uri );
00300 
00301        }
00302 
00303        return do_uri_create( &lud );
00304 }