1 /* ldapexop.c -- a tool for performing well-known extended operations */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was originally developed by Pierangelo Masarati for inclusion
18 * in OpenLDAP Software based, in part, on other client tools.
25 #include <ac/stdlib.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
31 #include <ac/unistd.h>
36 #include "lutil_ldap.h"
37 #include "ldap_defaults.h"
44 fprintf( stderr, _("Issue LDAP extended operations\n\n"));
45 fprintf( stderr, _("usage: %s [options] <oid|oid:data|oid::b64data>\n"), prog);
51 const char options[] = ""
52 "d:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
55 handle_private_option( int i )
66 main( int argc, char *argv[] )
73 char *matcheddn = NULL, *text = NULL, **refs = NULL;
77 tool_init( TOOL_EXOP );
78 prog = lutil_progname( "ldapexop", argc, argv );
81 protocol = LDAP_VERSION3;
83 tool_args( argc, argv );
85 if ( argc - optind < 1 ) {
89 if ( pw_file || want_bindpw ) {
91 rc = lutil_get_filed_password( pw_file, &passwd );
92 if( rc ) return EXIT_FAILURE;
94 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
95 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
99 ld = tool_conn_setup( 0, 0 );
106 if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
115 fprintf( stderr, "need [user]\n\n" );
119 tool_server_controls( ld, NULL, 0 );
121 rc = ldap_whoami( ld, NULL, NULL, &id );
122 if ( rc != LDAP_SUCCESS ) {
123 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
128 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
133 if ( lutil_atoi( &cancelid, argv[ 1 ] ) != 0 || cancelid < 0 ) {
134 fprintf( stderr, "invalid cancelid=%s\n\n", argv[ 1 ] );
140 fprintf( stderr, "need cancelid\n\n" );
144 rc = ldap_cancel( ld, cancelid, NULL, NULL, &id );
145 if ( rc != LDAP_SUCCESS ) {
146 tool_perror( "ldap_cancel", rc, NULL, NULL, NULL, NULL );
151 } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
152 fprintf( stderr, "use ldappasswd(1) instead.\n\n", argv[ 0 ] );
156 } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
162 ttl = atoi( argv[ 2 ] );
165 dn.bv_val = argv[ 1 ];
166 dn.bv_len = strlen( dn.bv_val );
172 fprintf( stderr, _("need DN [ttl]\n\n") );
176 tool_server_controls( ld, NULL, 0 );
178 rc = ldap_refresh( ld, &dn, ttl, NULL, NULL, &id );
179 if ( rc != LDAP_SUCCESS ) {
180 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
192 p = strchr( argv[ 0 ], ':' );
193 if ( p == argv[ 0 ] ) {
200 if ( tool_is_oid( argv[ 0 ] ) ) {
201 struct berval reqdata;
208 ldif_parse_line2( argv[ 0 ], &type, &value, &freeval );
214 ber_dupbv( &reqdata, &value );
219 tool_server_controls( ld, NULL, 0 );
221 rc = ldap_extended_operation( ld, argv[ 0 ], p ? &reqdata : NULL, NULL, NULL, &id );
222 if ( rc != LDAP_SUCCESS ) {
223 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
228 fprintf( stderr, "unknown exop \"%s\"\n\n", argv[ 0 ] );
236 if ( tool_check_abandon( ld, id ) ) {
237 return LDAP_CANCELLED;
243 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
245 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
255 rc = ldap_parse_result( ld, res,
256 &code, &matcheddn, &text, &refs, NULL, 0 );
257 if ( rc == LDAP_SUCCESS ) {
261 if ( rc != LDAP_SUCCESS ) {
262 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
267 if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
269 struct berval *retdata = NULL;
271 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
273 if ( rc != LDAP_SUCCESS ) {
274 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
279 if ( retdata != NULL ) {
280 if ( retdata->bv_len == 0 ) {
281 printf(_("anonymous\n") );
283 printf("%s\n", retdata->bv_val );
287 ber_memfree( retoid );
288 ber_bvfree( retdata );
290 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
291 /* no extended response; returns specific errors */
294 } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
297 } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
300 rc = ldap_parse_refresh( ld, res, &newttl );
302 if ( rc != LDAP_SUCCESS ) {
303 tool_perror( "ldap_parse_refresh", rc, NULL, NULL, NULL, NULL );
308 printf( "newttl=%d\n", newttl );
310 } else if ( tool_is_oid( argv[ 0 ] ) ) {
312 struct berval *retdata = NULL;
315 printf(_("# extended operation response\n"));
318 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
319 if ( rc != LDAP_SUCCESS ) {
320 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
325 if ( ldif < 2 && retoid != NULL ) {
326 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
327 "oid", retoid, strlen(retoid) );
330 ber_memfree( retoid );
332 if( retdata != NULL ) {
334 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
335 "data", retdata->bv_val, retdata->bv_len );
338 ber_bvfree( retdata );
342 if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
343 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
345 if( text && *text ) {
346 printf( _("Additional info: %s\n"), text );
349 if( matcheddn && *matcheddn ) {
350 printf( _("Matched DN: %s\n"), matcheddn );
355 for( i=0; refs[i]; i++ ) {
356 printf(_("Referral: %s\n"), refs[i] );
362 ber_memfree( matcheddn );
363 ber_memvfree( (void **) refs );
366 /* disconnect from server */
370 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;