Back to index

openldap  2.4.31
urltest.c
Go to the documentation of this file.
00001 /* urltest.c -- OpenLDAP URL API Test Program */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENT:
00017  * This program was initially developed by Pierangelo Masarati
00018  * <ando@OpenLDAP.org> for inclusion in OpenLDAP Software.
00019  */
00020 
00021 /*
00022  * This program is designed to test the ldap_url_* functions
00023  */
00024 
00025 #include "portable.h"
00026 
00027 #include <stdio.h>
00028 
00029 #include <ac/stdlib.h>
00030 #include <ac/string.h>
00031 #include <ac/unistd.h>
00032 
00033 #include <ldap.h>
00034 
00035 #include "ldap-int.h"
00036 
00037 #include "ldap_defaults.h"
00038 
00039 int
00040 main(int argc, char *argv[])
00041 {
00042        const char    *url,
00043                      *scope = NULL;
00044        LDAPURLDesc   *lud;
00045        enum {
00046               IS_LDAP = 0,
00047               IS_LDAPS,
00048               IS_LDAPI
00049        } type = IS_LDAP;
00050        int           rc;
00051 
00052        if ( argc != 2 ) {
00053               fprintf( stderr, "usage: urltest <url>\n" );
00054               exit( EXIT_FAILURE );
00055        }
00056 
00057        url = argv[ 1 ];
00058 
00059        if ( ldap_is_ldaps_url( url ) ) {
00060               fprintf( stdout, "LDAPS url\n" );
00061               type = IS_LDAPS;
00062 
00063        } else if ( ldap_is_ldapi_url( url ) ) {
00064               fprintf( stdout, "LDAPI url\n" );
00065               type = IS_LDAPI;
00066 
00067        } else if ( ldap_is_ldap_url( url ) ) {
00068               fprintf( stdout, "generic LDAP url\n" );
00069 
00070        } else {
00071               fprintf( stderr, "Need a valid LDAP url\n" );
00072               exit( EXIT_FAILURE );
00073        }
00074 
00075        rc = ldap_url_parse( url, &lud );
00076        if ( rc != LDAP_URL_SUCCESS ) {
00077               fprintf( stderr, "ldap_url_parse(%s) failed (%d)\n", url, rc );
00078               exit( EXIT_FAILURE );
00079        }
00080 
00081        fprintf( stdout, "PROTO: %s\n", lud->lud_scheme );
00082        switch ( type ) {
00083        case IS_LDAPI:
00084               fprintf( stdout, "PATH: %s\n", lud->lud_host );
00085               break;
00086 
00087        default:
00088               fprintf( stdout, "HOST: %s\n", lud->lud_host );
00089               if ( lud->lud_port != 0 ) {
00090                      fprintf( stdout, "PORT: %d\n", lud->lud_port );
00091               }
00092        }
00093 
00094        if ( lud->lud_dn && lud->lud_dn[ 0 ] ) {
00095               fprintf( stdout, "DN: %s\n", lud->lud_dn );
00096        }
00097 
00098        if ( lud->lud_attrs ) {
00099               int    i;
00100 
00101               fprintf( stdout, "ATTRS:\n" );
00102               for ( i = 0; lud->lud_attrs[ i ]; i++ ) {
00103                      fprintf( stdout, "\t%s\n", lud->lud_attrs[ i ] );
00104               }
00105        }
00106 
00107        scope = ldap_pvt_scope2str( lud->lud_scope );
00108        if ( scope ) {
00109               fprintf( stdout, "SCOPE: %s\n", scope );
00110        }
00111 
00112        if ( lud->lud_filter ) {
00113               fprintf( stdout, "FILTER: %s\n", lud->lud_filter );
00114        }
00115 
00116        if ( lud->lud_exts ) {
00117               int    i;
00118 
00119               fprintf( stdout, "EXTS:\n" );
00120               for ( i = 0; lud->lud_exts[ i ]; i++ ) {
00121                      fprintf( stdout, "\t%s\n", lud->lud_exts[ i ] );
00122               }
00123        }
00124 
00125        fprintf( stdout, "URL: %s\n", ldap_url_desc2str( lud ));
00126 
00127        return EXIT_SUCCESS;
00128 }