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-2009 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);
46 fprintf( stderr, _(" %s [options] whoami\n"), prog);
47 fprintf( stderr, _(" %s [options] cancel <id>\n"), prog);
48 fprintf( stderr, _(" %s [options] refresh <DN> [<ttl>]\n"), prog);
54 const char options[] = ""
55 "d:D:e:h:H:InNO:o:p:QR:U:vVw:WxX:y:Y:Z";
58 handle_private_option( int i )
69 main( int argc, char *argv[] )
75 char *matcheddn = NULL, *text = NULL, **refs = NULL;
76 LDAPControl **ctrls = NULL;
80 tool_init( TOOL_EXOP );
81 prog = lutil_progname( "ldapexop", argc, argv );
84 protocol = LDAP_VERSION3;
86 tool_args( argc, argv );
88 if ( argc - optind < 1 ) {
92 if ( pw_file || want_bindpw ) {
94 rc = lutil_get_filed_password( pw_file, &passwd );
95 if( rc ) return EXIT_FAILURE;
97 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
98 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
102 ld = tool_conn_setup( 0, 0 );
109 if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
110 tool_server_controls( ld, NULL, 0 );
112 rc = ldap_whoami( ld, NULL, NULL, &id );
113 if ( rc != LDAP_SUCCESS ) {
114 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
119 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
124 if ( lutil_atoi( &cancelid, argv[ 1 ] ) != 0 || cancelid < 0 ) {
125 fprintf( stderr, "invalid cancelid=%s\n\n", argv[ 1 ] );
131 fprintf( stderr, "need cancelid\n\n" );
135 rc = ldap_cancel( ld, cancelid, NULL, NULL, &id );
136 if ( rc != LDAP_SUCCESS ) {
137 tool_perror( "ldap_cancel", rc, NULL, NULL, NULL, NULL );
142 } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
143 fprintf( stderr, "use ldappasswd(1) instead.\n\n", argv[ 0 ] );
147 } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
153 ttl = atoi( argv[ 2 ] );
156 dn.bv_val = argv[ 1 ];
157 dn.bv_len = strlen( dn.bv_val );
161 fprintf( stderr, _("need DN [ttl]\n\n") );
165 tool_server_controls( ld, NULL, 0 );
167 rc = ldap_refresh( ld, &dn, ttl, NULL, NULL, &id );
168 if ( rc != LDAP_SUCCESS ) {
169 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
181 p = strchr( argv[ 0 ], ':' );
182 if ( p == argv[ 0 ] ) {
189 if ( tool_is_oid( argv[ 0 ] ) ) {
190 struct berval reqdata;
197 ldif_parse_line2( argv[ 0 ], &type, &value, &freeval );
203 ber_dupbv( &reqdata, &value );
208 tool_server_controls( ld, NULL, 0 );
210 rc = ldap_extended_operation( ld, argv[ 0 ], p ? &reqdata : NULL, NULL, NULL, &id );
211 if ( rc != LDAP_SUCCESS ) {
212 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
217 fprintf( stderr, "unknown exop \"%s\"\n\n", argv[ 0 ] );
225 if ( tool_check_abandon( ld, id ) ) {
226 return LDAP_CANCELLED;
232 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
234 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
244 rc = ldap_parse_result( ld, res,
245 &code, &matcheddn, &text, &refs, &ctrls, 0 );
246 if ( rc == LDAP_SUCCESS ) {
250 if ( rc != LDAP_SUCCESS ) {
251 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
256 if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
258 struct berval *retdata = NULL;
260 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
262 if ( rc != LDAP_SUCCESS ) {
263 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
268 if ( retdata != NULL ) {
269 if ( retdata->bv_len == 0 ) {
270 printf(_("anonymous\n") );
272 printf("%s\n", retdata->bv_val );
276 ber_memfree( retoid );
277 ber_bvfree( retdata );
279 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
280 /* no extended response; returns specific errors */
283 } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
286 } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
289 rc = ldap_parse_refresh( ld, res, &newttl );
291 if ( rc != LDAP_SUCCESS ) {
292 tool_perror( "ldap_parse_refresh", rc, NULL, NULL, NULL, NULL );
297 printf( "newttl=%d\n", newttl );
299 } else if ( tool_is_oid( argv[ 0 ] ) ) {
301 struct berval *retdata = NULL;
304 printf(_("# extended operation response\n"));
307 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
308 if ( rc != LDAP_SUCCESS ) {
309 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
314 if ( ldif < 2 && retoid != NULL ) {
315 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
316 "oid", retoid, strlen(retoid) );
319 ber_memfree( retoid );
321 if( retdata != NULL ) {
323 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
324 "data", retdata->bv_val, retdata->bv_len );
327 ber_bvfree( retdata );
331 if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
332 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
334 if( text && *text ) {
335 printf( _("Additional info: %s\n"), text );
338 if( matcheddn && *matcheddn ) {
339 printf( _("Matched DN: %s\n"), matcheddn );
344 for( i=0; refs[i]; i++ ) {
345 printf(_("Referral: %s\n"), refs[i] );
351 tool_print_ctrls( ld, ctrls );
352 ldap_controls_free( ctrls );
356 ber_memfree( matcheddn );
357 ber_memvfree( (void **) refs );
360 /* disconnect from server */
364 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;