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-2014 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;
78 LDAPMessage *res = 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 ld = tool_conn_setup( 0, 0 );
99 if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
100 tool_server_controls( ld, NULL, 0 );
102 rc = ldap_whoami( ld, NULL, NULL, &id );
103 if ( rc != LDAP_SUCCESS ) {
104 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
109 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
114 if ( lutil_atoi( &cancelid, argv[ 1 ] ) != 0 || cancelid < 0 ) {
115 fprintf( stderr, "invalid cancelid=%s\n\n", argv[ 1 ] );
121 fprintf( stderr, "need cancelid\n\n" );
125 rc = ldap_cancel( ld, cancelid, NULL, NULL, &id );
126 if ( rc != LDAP_SUCCESS ) {
127 tool_perror( "ldap_cancel", rc, NULL, NULL, NULL, NULL );
132 } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
133 fprintf( stderr, "use ldappasswd(1) instead.\n\n", argv[ 0 ] );
137 } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
143 ttl = atoi( argv[ 2 ] );
146 dn.bv_val = argv[ 1 ];
147 dn.bv_len = strlen( dn.bv_val );
151 fprintf( stderr, _("need DN [ttl]\n\n") );
155 tool_server_controls( ld, NULL, 0 );
157 rc = ldap_refresh( ld, &dn, ttl, NULL, NULL, &id );
158 if ( rc != LDAP_SUCCESS ) {
159 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
171 p = strchr( argv[ 0 ], ':' );
172 if ( p == argv[ 0 ] ) {
179 if ( tool_is_oid( argv[ 0 ] ) ) {
180 struct berval reqdata;
187 ldif_parse_line2( argv[ 0 ], &type, &value, &freeval );
193 ber_dupbv( &reqdata, &value );
198 tool_server_controls( ld, NULL, 0 );
200 rc = ldap_extended_operation( ld, argv[ 0 ], p ? &reqdata : NULL, NULL, NULL, &id );
201 if ( rc != LDAP_SUCCESS ) {
202 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
207 fprintf( stderr, "unknown exop \"%s\"\n\n", argv[ 0 ] );
215 if ( tool_check_abandon( ld, id ) ) {
216 tool_exit( ld, LDAP_CANCELLED );
222 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
224 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
234 rc = ldap_parse_result( ld, res,
235 &code, &matcheddn, &text, &refs, &ctrls, 0 );
236 if ( rc == LDAP_SUCCESS ) {
240 if ( rc != LDAP_SUCCESS ) {
241 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
246 if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
248 struct berval *retdata = NULL;
250 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 0 );
252 if ( rc != LDAP_SUCCESS ) {
253 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
258 if ( retdata != NULL ) {
259 if ( retdata->bv_len == 0 ) {
260 printf(_("anonymous\n") );
262 printf("%s\n", retdata->bv_val );
266 ber_memfree( retoid );
267 ber_bvfree( retdata );
269 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
270 /* no extended response; returns specific errors */
273 } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
276 } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
279 rc = ldap_parse_refresh( ld, res, &newttl );
281 if ( rc != LDAP_SUCCESS ) {
282 tool_perror( "ldap_parse_refresh", rc, NULL, NULL, NULL, NULL );
287 printf( "newttl=%d\n", newttl );
289 } else if ( tool_is_oid( argv[ 0 ] ) ) {
291 struct berval *retdata = NULL;
294 printf(_("# extended operation response\n"));
297 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 0 );
298 if ( rc != LDAP_SUCCESS ) {
299 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
304 if ( ldif < 2 && retoid != NULL ) {
305 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
306 "oid", retoid, strlen(retoid) );
309 ber_memfree( retoid );
311 if( retdata != NULL ) {
313 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
314 "data", retdata->bv_val, retdata->bv_len );
317 ber_bvfree( retdata );
321 if( verbose || code != LDAP_SUCCESS ||
322 ( matcheddn && *matcheddn ) || ( text && *text ) || refs ) {
323 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
325 if( text && *text ) {
326 printf( _("Additional info: %s\n"), text );
329 if( matcheddn && *matcheddn ) {
330 printf( _("Matched DN: %s\n"), matcheddn );
335 for( i=0; refs[i]; i++ ) {
336 printf(_("Referral: %s\n"), refs[i] );
342 tool_print_ctrls( ld, ctrls );
343 ldap_controls_free( ctrls );
347 ber_memfree( matcheddn );
348 ber_memvfree( (void **) refs );
351 /* disconnect from server */
354 tool_exit( ld, code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE );