1 /* $NetBSD: ldapwhoami.c,v 1.1.1.2 2010/03/08 02:14:20 lukem 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.5 2009/01/22 00:00:43 kurt Exp */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2009 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 if ( pw_file || want_bindpw ) { 135 if ( pw_file ) { 136 rc = lutil_get_filed_password( pw_file, &passwd ); 137 if( rc ) return EXIT_FAILURE; 138 } else { 139 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") ); 140 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0; 141 } 142 } 143 144 ld = tool_conn_setup( 0, 0 ); 145 146 tool_bind( ld ); 147 148 if ( dont ) { 149 rc = LDAP_SUCCESS; 150 goto skip; 151 } 152 153 tool_server_controls( ld, NULL, 0 ); 154 155 rc = ldap_whoami( ld, NULL, NULL, &id ); 156 157 if( rc != LDAP_SUCCESS ) { 158 tool_perror( "ldap_whoami", rc, NULL, NULL, NULL, NULL ); 159 rc = EXIT_FAILURE; 160 goto skip; 161 } 162 163 for ( ; ; ) { 164 struct timeval tv; 165 166 if ( tool_check_abandon( ld, id ) ) { 167 return LDAP_CANCELLED; 168 } 169 170 tv.tv_sec = 0; 171 tv.tv_usec = 100000; 172 173 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); 174 if ( rc < 0 ) { 175 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); 176 return rc; 177 } 178 179 if ( rc != 0 ) { 180 break; 181 } 182 } 183 184 rc = ldap_parse_result( ld, res, 185 &code, &matcheddn, &text, &refs, &ctrls, 0 ); 186 187 if ( rc == LDAP_SUCCESS ) { 188 rc = code; 189 } 190 191 if ( rc != LDAP_SUCCESS ) { 192 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs ); 193 rc = EXIT_FAILURE; 194 goto skip; 195 } 196 197 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 ); 198 199 if( rc != LDAP_SUCCESS ) { 200 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); 201 rc = EXIT_FAILURE; 202 goto skip; 203 } 204 205 if( retdata != NULL ) { 206 if( retdata->bv_len == 0 ) { 207 printf(_("anonymous\n") ); 208 } else { 209 printf("%s\n", retdata->bv_val ); 210 } 211 } 212 213 skip: 214 if ( verbose || ( code != LDAP_SUCCESS ) || 215 matcheddn || text || refs || ctrls ) 216 { 217 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code ); 218 219 if( text && *text ) { 220 printf( _("Additional info: %s\n"), text ); 221 } 222 223 if( matcheddn && *matcheddn ) { 224 printf( _("Matched DN: %s\n"), matcheddn ); 225 } 226 227 if( refs ) { 228 int i; 229 for( i=0; refs[i]; i++ ) { 230 printf(_("Referral: %s\n"), refs[i] ); 231 } 232 } 233 234 if (ctrls) { 235 tool_print_ctrls( ld, ctrls ); 236 ldap_controls_free( ctrls ); 237 } 238 } 239 240 ber_memfree( text ); 241 ber_memfree( matcheddn ); 242 ber_memvfree( (void **) refs ); 243 ber_memfree( retoid ); 244 ber_bvfree( retdata ); 245 246 /* disconnect from server */ 247 tool_unbind( ld ); 248 tool_destroy(); 249 250 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE; 251 } 252