Back to index

openldap  2.4.31
ldapwhoami.c
Go to the documentation of this file.
00001 /* ldapwhoami.c -- a tool for asking the directory "Who Am I?" */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
00007  * Portions Copyright 1998-2001 Net Boolean Incorporated.
00008  * Portions Copyright 2001-2003 IBM Corporation.
00009  * All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted only as authorized by the OpenLDAP
00013  * Public License.
00014  *
00015  * A copy of this license is available in the file LICENSE in the
00016  * top-level directory of the distribution or, alternatively, at
00017  * <http://www.OpenLDAP.org/license.html>.
00018  */
00019 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
00020  * All rights reserved.
00021  *
00022  * Redistribution and use in source and binary forms are permitted
00023  * provided that this notice is preserved and that due credit is given
00024  * to the University of Michigan at Ann Arbor.  The name of the
00025  * University may not be used to endorse or promote products derived
00026  * from this software without specific prior written permission.  This
00027  * software is provided ``as is'' without express or implied warranty.
00028  */
00029 /* ACKNOWLEDGEMENTS:
00030  * This work was originally developed by Kurt D. Zeilenga for inclusion
00031  * in OpenLDAP Software based, in part, on other client tools.
00032  */
00033 
00034 #include "portable.h"
00035 
00036 #include <stdio.h>
00037 
00038 #include <ac/stdlib.h>
00039 
00040 #include <ac/ctype.h>
00041 #include <ac/socket.h>
00042 #include <ac/string.h>
00043 #include <ac/time.h>
00044 #include <ac/unistd.h>
00045 
00046 #include <ldap.h>
00047 #include "lutil.h"
00048 #include "lutil_ldap.h"
00049 #include "ldap_defaults.h"
00050 
00051 #include "common.h"
00052 
00053 
00054 void
00055 usage( void )
00056 {
00057        fprintf( stderr, _("Issue LDAP Who am I? operation to request user's authzid\n\n"));
00058        fprintf( stderr, _("usage: %s [options]\n"), prog);
00059        tool_common_usage();
00060        exit( EXIT_FAILURE );
00061 }
00062 
00063 
00064 const char options[] = ""
00065        "d:D:e:h:H:InNO:o:p:QR:U:vVw:WxX:y:Y:Z";
00066 
00067 int
00068 handle_private_option( int i )
00069 {
00070        switch ( i ) {
00071 #if 0
00072               char   *control, *cvalue;
00073               int           crit;
00074        case 'E': /* whoami extension */
00075               if( protocol == LDAP_VERSION2 ) {
00076                      fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
00077                             prog, protocol );
00078                      exit( EXIT_FAILURE );
00079               }
00080 
00081               /* should be extended to support comma separated list of
00082                *     [!]key[=value] parameters, e.g.  -E !foo,bar=567
00083                */
00084 
00085               crit = 0;
00086               cvalue = NULL;
00087               if( optarg[0] == '!' ) {
00088                      crit = 1;
00089                      optarg++;
00090               }
00091 
00092               control = strdup( optarg );
00093               if ( (cvalue = strchr( control, '=' )) != NULL ) {
00094                      *cvalue++ = '\0';
00095               }
00096 
00097               fprintf( stderr, _("Invalid whoami extension name: %s\n"), control );
00098               usage();
00099 #endif
00100 
00101        default:
00102               return 0;
00103        }
00104        return 1;
00105 }
00106 
00107 
00108 int
00109 main( int argc, char *argv[] )
00110 {
00111        int           rc;
00112        LDAP          *ld = NULL;
00113        char          *matcheddn = NULL, *text = NULL, **refs = NULL;
00114        struct berval *authzid = NULL;
00115        int           id, code = 0;
00116        LDAPMessage   *res = NULL;
00117        LDAPControl   **ctrls = NULL;
00118 
00119        tool_init( TOOL_WHOAMI );
00120        prog = lutil_progname( "ldapwhoami", argc, argv );
00121 
00122        /* LDAPv3 only */
00123        protocol = LDAP_VERSION3;
00124 
00125        tool_args( argc, argv );
00126 
00127        if( argc - optind > 0 ) {
00128               usage();
00129        }
00130 
00131        ld = tool_conn_setup( 0, 0 );
00132 
00133        tool_bind( ld );
00134 
00135        if ( dont ) {
00136               rc = LDAP_SUCCESS;
00137               goto skip;
00138        }
00139 
00140        tool_server_controls( ld, NULL, 0 );
00141 
00142        rc = ldap_whoami( ld, NULL, NULL, &id ); 
00143 
00144        if( rc != LDAP_SUCCESS ) {
00145               tool_perror( "ldap_whoami", rc, NULL, NULL, NULL, NULL );
00146               rc = EXIT_FAILURE;
00147               goto skip;
00148        }
00149 
00150        for ( ; ; ) {
00151               struct timeval       tv;
00152 
00153               if ( tool_check_abandon( ld, id ) ) {
00154                      tool_exit( ld, LDAP_CANCELLED );
00155               }
00156 
00157               tv.tv_sec = 0;
00158               tv.tv_usec = 100000;
00159 
00160               rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
00161               if ( rc < 0 ) {
00162                      tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
00163                      tool_exit( ld, rc );
00164               }
00165 
00166               if ( rc != 0 ) {
00167                      break;
00168               }
00169        }
00170 
00171        rc = ldap_parse_result( ld, res,
00172               &code, &matcheddn, &text, &refs, &ctrls, 0 );
00173 
00174        if ( rc == LDAP_SUCCESS ) {
00175               rc = code;
00176        }
00177 
00178        if ( rc != LDAP_SUCCESS ) {
00179               tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
00180               rc = EXIT_FAILURE;
00181               goto skip;
00182        }
00183 
00184        rc = ldap_parse_whoami( ld, res, &authzid );
00185 
00186        if( rc != LDAP_SUCCESS ) {
00187               tool_perror( "ldap_parse_whoami", rc, NULL, NULL, NULL, NULL );
00188               rc = EXIT_FAILURE;
00189               goto skip;
00190        }
00191 
00192        if( authzid != NULL ) {
00193               if( authzid->bv_len == 0 ) {
00194                      printf(_("anonymous\n") );
00195               } else {
00196                      printf("%s\n", authzid->bv_val );
00197               }
00198        }
00199 
00200 skip:
00201        ldap_msgfree(res);
00202        if ( verbose || code != LDAP_SUCCESS ||
00203               ( matcheddn && *matcheddn ) || ( text && *text ) || refs || ctrls )
00204        {
00205               printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
00206 
00207               if( text && *text ) {
00208                      printf( _("Additional info: %s\n"), text );
00209               }
00210 
00211               if( matcheddn && *matcheddn ) {
00212                      printf( _("Matched DN: %s\n"), matcheddn );
00213               }
00214 
00215               if( refs ) {
00216                      int i;
00217                      for( i=0; refs[i]; i++ ) {
00218                             printf(_("Referral: %s\n"), refs[i] );
00219                      }
00220               }
00221 
00222               if (ctrls) {
00223                      tool_print_ctrls( ld, ctrls );
00224                      ldap_controls_free( ctrls );
00225               }
00226        }
00227 
00228        ber_memfree( text );
00229        ber_memfree( matcheddn );
00230        ber_memvfree( (void **) refs );
00231        ber_bvfree( authzid );
00232 
00233        /* disconnect from server */
00234        tool_exit( ld, code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE );
00235 }