1 /* $NetBSD: ldapexop.c,v 1.1.1.2 2010/03/08 02:14:20 lukem Exp $ */ 2 3 /* ldapexop.c -- a tool for performing well-known extended operations */ 4 /* OpenLDAP: pkg/ldap/clients/tools/ldapexop.c,v 1.9.2.6 2009/08/14 20:51:14 quanah Exp */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 2005-2009 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in the file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18 /* ACKNOWLEDGEMENTS: 19 * This work was originally developed by Pierangelo Masarati for inclusion 20 * in OpenLDAP Software based, in part, on other client tools. 21 */ 22 23 #include "portable.h" 24 25 #include <stdio.h> 26 27 #include <ac/stdlib.h> 28 29 #include <ac/ctype.h> 30 #include <ac/socket.h> 31 #include <ac/string.h> 32 #include <ac/time.h> 33 #include <ac/unistd.h> 34 35 #include <ldap.h> 36 #include "ldif.h" 37 #include "lutil.h" 38 #include "lutil_ldap.h" 39 #include "ldap_defaults.h" 40 41 #include "common.h" 42 43 void 44 usage( void ) 45 { 46 fprintf( stderr, _("Issue LDAP extended operations\n\n")); 47 fprintf( stderr, _("usage: %s [options] <oid|oid:data|oid::b64data>\n"), prog); 48 fprintf( stderr, _(" %s [options] whoami\n"), prog); 49 fprintf( stderr, _(" %s [options] cancel <id>\n"), prog); 50 fprintf( stderr, _(" %s [options] refresh <DN> [<ttl>]\n"), prog); 51 tool_common_usage(); 52 exit( EXIT_FAILURE ); 53 } 54 55 56 const char options[] = "" 57 "d:D:e:h:H:InNO:o:p:QR:U:vVw:WxX:y:Y:Z"; 58 59 int 60 handle_private_option( int i ) 61 { 62 switch ( i ) { 63 default: 64 return 0; 65 } 66 return 1; 67 } 68 69 70 int 71 main( int argc, char *argv[] ) 72 { 73 int rc; 74 75 LDAP *ld = NULL; 76 77 char *matcheddn = NULL, *text = NULL, **refs = NULL; 78 LDAPControl **ctrls = NULL; 79 int id, code; 80 LDAPMessage *res; 81 82 tool_init( TOOL_EXOP ); 83 prog = lutil_progname( "ldapexop", argc, argv ); 84 85 /* LDAPv3 only */ 86 protocol = LDAP_VERSION3; 87 88 tool_args( argc, argv ); 89 90 if ( argc - optind < 1 ) { 91 usage(); 92 } 93 94 if ( pw_file || want_bindpw ) { 95 if ( pw_file ) { 96 rc = lutil_get_filed_password( pw_file, &passwd ); 97 if( rc ) return EXIT_FAILURE; 98 } else { 99 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") ); 100 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0; 101 } 102 } 103 104 ld = tool_conn_setup( 0, 0 ); 105 106 tool_bind( ld ); 107 108 argv += optind; 109 argc -= optind; 110 111 if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) { 112 tool_server_controls( ld, NULL, 0 ); 113 114 rc = ldap_whoami( ld, NULL, NULL, &id ); 115 if ( rc != LDAP_SUCCESS ) { 116 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); 117 rc = EXIT_FAILURE; 118 goto skip; 119 } 120 121 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) { 122 int cancelid; 123 124 switch ( argc ) { 125 case 2: 126 if ( lutil_atoi( &cancelid, argv[ 1 ] ) != 0 || cancelid < 0 ) { 127 fprintf( stderr, "invalid cancelid=%s\n\n", argv[ 1 ] ); 128 usage(); 129 } 130 break; 131 132 default: 133 fprintf( stderr, "need cancelid\n\n" ); 134 usage(); 135 } 136 137 rc = ldap_cancel( ld, cancelid, NULL, NULL, &id ); 138 if ( rc != LDAP_SUCCESS ) { 139 tool_perror( "ldap_cancel", rc, NULL, NULL, NULL, NULL ); 140 rc = EXIT_FAILURE; 141 goto skip; 142 } 143 144 } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) { 145 fprintf( stderr, "use ldappasswd(1) instead.\n\n", argv[ 0 ] ); 146 usage(); 147 /* TODO? */ 148 149 } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) { 150 int ttl = 3600; 151 struct berval dn; 152 153 switch ( argc ) { 154 case 3: 155 ttl = atoi( argv[ 2 ] ); 156 157 case 2: 158 dn.bv_val = argv[ 1 ]; 159 dn.bv_len = strlen( dn.bv_val ); 160 break; 161 162 default: 163 fprintf( stderr, _("need DN [ttl]\n\n") ); 164 usage(); 165 } 166 167 tool_server_controls( ld, NULL, 0 ); 168 169 rc = ldap_refresh( ld, &dn, ttl, NULL, NULL, &id ); 170 if ( rc != LDAP_SUCCESS ) { 171 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); 172 rc = EXIT_FAILURE; 173 goto skip; 174 } 175 176 } else { 177 char *p; 178 179 if ( argc != 1 ) { 180 usage(); 181 } 182 183 p = strchr( argv[ 0 ], ':' ); 184 if ( p == argv[ 0 ] ) { 185 usage(); 186 } 187 188 if ( p != NULL ) 189 *p++ = '\0'; 190 191 if ( tool_is_oid( argv[ 0 ] ) ) { 192 struct berval reqdata; 193 struct berval type; 194 struct berval value; 195 int freeval; 196 197 if ( p != NULL ) { 198 p[ -1 ] = ':'; 199 ldif_parse_line2( argv[ 0 ], &type, &value, &freeval ); 200 p[ -1 ] = '\0'; 201 202 if ( freeval ) { 203 reqdata = value; 204 } else { 205 ber_dupbv( &reqdata, &value ); 206 } 207 } 208 209 210 tool_server_controls( ld, NULL, 0 ); 211 212 rc = ldap_extended_operation( ld, argv[ 0 ], p ? &reqdata : NULL, NULL, NULL, &id ); 213 if ( rc != LDAP_SUCCESS ) { 214 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); 215 rc = EXIT_FAILURE; 216 goto skip; 217 } 218 } else { 219 fprintf( stderr, "unknown exop \"%s\"\n\n", argv[ 0 ] ); 220 usage(); 221 } 222 } 223 224 for ( ; ; ) { 225 struct timeval tv; 226 227 if ( tool_check_abandon( ld, id ) ) { 228 return LDAP_CANCELLED; 229 } 230 231 tv.tv_sec = 0; 232 tv.tv_usec = 100000; 233 234 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); 235 if ( rc < 0 ) { 236 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); 237 rc = EXIT_FAILURE; 238 goto skip; 239 } 240 241 if ( rc != 0 ) { 242 break; 243 } 244 } 245 246 rc = ldap_parse_result( ld, res, 247 &code, &matcheddn, &text, &refs, &ctrls, 0 ); 248 if ( rc == LDAP_SUCCESS ) { 249 rc = code; 250 } 251 252 if ( rc != LDAP_SUCCESS ) { 253 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs ); 254 rc = EXIT_FAILURE; 255 goto skip; 256 } 257 258 if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) { 259 char *retoid = NULL; 260 struct berval *retdata = NULL; 261 262 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 ); 263 264 if ( rc != LDAP_SUCCESS ) { 265 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); 266 rc = EXIT_FAILURE; 267 goto skip; 268 } 269 270 if ( retdata != NULL ) { 271 if ( retdata->bv_len == 0 ) { 272 printf(_("anonymous\n") ); 273 } else { 274 printf("%s\n", retdata->bv_val ); 275 } 276 } 277 278 ber_memfree( retoid ); 279 ber_bvfree( retdata ); 280 281 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) { 282 /* no extended response; returns specific errors */ 283 assert( 0 ); 284 285 } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) { 286 /* TODO */ 287 288 } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) { 289 int newttl; 290 291 rc = ldap_parse_refresh( ld, res, &newttl ); 292 293 if ( rc != LDAP_SUCCESS ) { 294 tool_perror( "ldap_parse_refresh", rc, NULL, NULL, NULL, NULL ); 295 rc = EXIT_FAILURE; 296 goto skip; 297 } 298 299 printf( "newttl=%d\n", newttl ); 300 301 } else if ( tool_is_oid( argv[ 0 ] ) ) { 302 char *retoid = NULL; 303 struct berval *retdata = NULL; 304 305 if( ldif < 2 ) { 306 printf(_("# extended operation response\n")); 307 } 308 309 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 ); 310 if ( rc != LDAP_SUCCESS ) { 311 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); 312 rc = EXIT_FAILURE; 313 goto skip; 314 } 315 316 if ( ldif < 2 && retoid != NULL ) { 317 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE, 318 "oid", retoid, strlen(retoid) ); 319 } 320 321 ber_memfree( retoid ); 322 323 if( retdata != NULL ) { 324 if ( ldif < 2 ) { 325 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY, 326 "data", retdata->bv_val, retdata->bv_len ); 327 } 328 329 ber_bvfree( retdata ); 330 } 331 } 332 333 if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) { 334 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code ); 335 336 if( text && *text ) { 337 printf( _("Additional info: %s\n"), text ); 338 } 339 340 if( matcheddn && *matcheddn ) { 341 printf( _("Matched DN: %s\n"), matcheddn ); 342 } 343 344 if( refs ) { 345 int i; 346 for( i=0; refs[i]; i++ ) { 347 printf(_("Referral: %s\n"), refs[i] ); 348 } 349 } 350 } 351 352 if (ctrls) { 353 tool_print_ctrls( ld, ctrls ); 354 ldap_controls_free( ctrls ); 355 } 356 357 ber_memfree( text ); 358 ber_memfree( matcheddn ); 359 ber_memvfree( (void **) refs ); 360 361 skip: 362 /* disconnect from server */ 363 tool_unbind( ld ); 364 tool_destroy(); 365 366 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE; 367 } 368