1 /* $NetBSD: ldapwhoami.c,v 1.1.1.3 2010/12/12 15:18:13 adam Exp $ */ 2 3 /* ldapwhoami.c -- a tool for asking the directory "Who Am I?" */ 4 /* OpenLDAP: pkg/ldap/clients/tools/ldapwhoami.c,v 1.42.2.7 2010/04/15 22:16:50 quanah Exp */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2010 The OpenLDAP Foundation. 8 * Portions Copyright 1998-2003 Kurt D. Zeilenga. 9 * Portions Copyright 1998-2001 Net Boolean Incorporated. 10 * Portions Copyright 2001-2003 IBM Corporation. 11 * All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted only as authorized by the OpenLDAP 15 * Public License. 16 * 17 * A copy of this license is available in the file LICENSE in the 18 * top-level directory of the distribution or, alternatively, at 19 * <http://www.OpenLDAP.org/license.html>. 20 */ 21 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan. 22 * All rights reserved. 23 * 24 * Redistribution and use in source and binary forms are permitted 25 * provided that this notice is preserved and that due credit is given 26 * to the University of Michigan at Ann Arbor. The name of the 27 * University may not be used to endorse or promote products derived 28 * from this software without specific prior written permission. This 29 * software is provided ``as is'' without express or implied warranty. 30 */ 31 /* ACKNOWLEDGEMENTS: 32 * This work was originally developed by Kurt D. Zeilenga for inclusion 33 * in OpenLDAP Software based, in part, on other client tools. 34 */ 35 36 #include "portable.h" 37 38 #include <stdio.h> 39 40 #include <ac/stdlib.h> 41 42 #include <ac/ctype.h> 43 #include <ac/socket.h> 44 #include <ac/string.h> 45 #include <ac/time.h> 46 #include <ac/unistd.h> 47 48 #include <ldap.h> 49 #include "lutil.h" 50 #include "lutil_ldap.h" 51 #include "ldap_defaults.h" 52 53 #include "common.h" 54 55 56 void 57 usage( void ) 58 { 59 fprintf( stderr, _("Issue LDAP Who am I? operation to request user's authzid\n\n")); 60 fprintf( stderr, _("usage: %s [options]\n"), prog); 61 tool_common_usage(); 62 exit( EXIT_FAILURE ); 63 } 64 65 66 const char options[] = "" 67 "d:D:e:h:H:InNO:o:p:QR:U:vVw:WxX:y:Y:Z"; 68 69 int 70 handle_private_option( int i ) 71 { 72 switch ( i ) { 73 #if 0 74 char *control, *cvalue; 75 int crit; 76 case 'E': /* whoami extension */ 77 if( protocol == LDAP_VERSION2 ) { 78 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"), 79 prog, protocol ); 80 exit( EXIT_FAILURE ); 81 } 82 83 /* should be extended to support comma separated list of 84 * [!]key[=value] parameters, e.g. -E !foo,bar=567 85 */ 86 87 crit = 0; 88 cvalue = NULL; 89 if( optarg[0] == '!' ) { 90 crit = 1; 91 optarg++; 92 } 93 94 control = strdup( optarg ); 95 if ( (cvalue = strchr( control, '=' )) != NULL ) { 96 *cvalue++ = '\0'; 97 } 98 99 fprintf( stderr, _("Invalid whoami extension name: %s\n"), control ); 100 usage(); 101 #endif 102 103 default: 104 return 0; 105 } 106 return 1; 107 } 108 109 110 int 111 main( int argc, char *argv[] ) 112 { 113 int rc; 114 LDAP *ld = NULL; 115 char *matcheddn = NULL, *text = NULL, **refs = NULL; 116 char *retoid = NULL; 117 struct berval *retdata = NULL; 118 int id, code = 0; 119 LDAPMessage *res; 120 LDAPControl **ctrls = NULL; 121 122 tool_init( TOOL_WHOAMI ); 123 prog = lutil_progname( "ldapwhoami", argc, argv ); 124 125 /* LDAPv3 only */ 126 protocol = LDAP_VERSION3; 127 128 tool_args( argc, argv ); 129 130 if( argc - optind > 0 ) { 131 usage(); 132 } 133 134 ld = tool_conn_setup( 0, 0 ); 135 136 tool_bind( ld ); 137 138 if ( dont ) { 139 rc = LDAP_SUCCESS; 140 goto skip; 141 } 142 143 tool_server_controls( ld, NULL, 0 ); 144 145 rc = ldap_whoami( ld, NULL, NULL, &id ); 146 147 if( rc != LDAP_SUCCESS ) { 148 tool_perror( "ldap_whoami", rc, NULL, NULL, NULL, NULL ); 149 rc = EXIT_FAILURE; 150 goto skip; 151 } 152 153 for ( ; ; ) { 154 struct timeval tv; 155 156 if ( tool_check_abandon( ld, id ) ) { 157 return LDAP_CANCELLED; 158 } 159 160 tv.tv_sec = 0; 161 tv.tv_usec = 100000; 162 163 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); 164 if ( rc < 0 ) { 165 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); 166 return rc; 167 } 168 169 if ( rc != 0 ) { 170 break; 171 } 172 } 173 174 rc = ldap_parse_result( ld, res, 175 &code, &matcheddn, &text, &refs, &ctrls, 0 ); 176 177 if ( rc == LDAP_SUCCESS ) { 178 rc = code; 179 } 180 181 if ( rc != LDAP_SUCCESS ) { 182 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs ); 183 rc = EXIT_FAILURE; 184 goto skip; 185 } 186 187 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 ); 188 189 if( rc != LDAP_SUCCESS ) { 190 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); 191 rc = EXIT_FAILURE; 192 goto skip; 193 } 194 195 if( retdata != NULL ) { 196 if( retdata->bv_len == 0 ) { 197 printf(_("anonymous\n") ); 198 } else { 199 printf("%s\n", retdata->bv_val ); 200 } 201 } 202 203 skip: 204 if ( verbose || ( code != LDAP_SUCCESS ) || 205 matcheddn || text || refs || ctrls ) 206 { 207 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code ); 208 209 if( text && *text ) { 210 printf( _("Additional info: %s\n"), text ); 211 } 212 213 if( matcheddn && *matcheddn ) { 214 printf( _("Matched DN: %s\n"), matcheddn ); 215 } 216 217 if( refs ) { 218 int i; 219 for( i=0; refs[i]; i++ ) { 220 printf(_("Referral: %s\n"), refs[i] ); 221 } 222 } 223 224 if (ctrls) { 225 tool_print_ctrls( ld, ctrls ); 226 ldap_controls_free( ctrls ); 227 } 228 } 229 230 ber_memfree( text ); 231 ber_memfree( matcheddn ); 232 ber_memvfree( (void **) refs ); 233 ber_memfree( retoid ); 234 ber_bvfree( retdata ); 235 236 /* disconnect from server */ 237 tool_unbind( ld ); 238 tool_destroy(); 239 240 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE; 241 } 242