Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / clients / tools / ldapexop.c
blobcc2b69e434483b64e4fe769d5afb8205c40cbe5b
1 /* ldapexop.c -- a tool for performing well-known extended operations */
2 /* $OpenLDAP: pkg/ldap/clients/tools/ldapexop.c,v 1.9.2.3 2008/02/11 23:26:38 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
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>.
16 /* ACKNOWLEDGEMENTS:
17 * This work was originally developed by Pierangelo Masarati for inclusion
18 * in OpenLDAP Software based, in part, on other client tools.
21 #include "portable.h"
23 #include <stdio.h>
25 #include <ac/stdlib.h>
27 #include <ac/ctype.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31 #include <ac/unistd.h>
33 #include <ldap.h>
34 #include "ldif.h"
35 #include "lutil.h"
36 #include "lutil_ldap.h"
37 #include "ldap_defaults.h"
39 #include "common.h"
41 void
42 usage( void )
44 fprintf( stderr, _("Issue LDAP extended operations\n\n"));
45 fprintf( stderr, _("usage: %s [options] <oid|oid:data|oid::b64data>\n"), prog);
46 tool_common_usage();
47 exit( EXIT_FAILURE );
51 const char options[] = ""
52 "d:D:e:h:H:InO:o:p:QR:U:vVw:WxX:y:Y:Z";
54 int
55 handle_private_option( int i )
57 switch ( i ) {
58 default:
59 return 0;
61 return 1;
65 int
66 main( int argc, char *argv[] )
68 int rc;
70 LDAP *ld = NULL;
72 char *matcheddn = NULL, *text = NULL, **refs = NULL;
73 LDAPControl **ctrls = NULL;
74 int id, code;
75 LDAPMessage *res;
77 tool_init( TOOL_EXOP );
78 prog = lutil_progname( "ldapexop", argc, argv );
80 /* LDAPv3 only */
81 protocol = LDAP_VERSION3;
83 tool_args( argc, argv );
85 if ( argc - optind < 1 ) {
86 usage();
89 if ( pw_file || want_bindpw ) {
90 if ( pw_file ) {
91 rc = lutil_get_filed_password( pw_file, &passwd );
92 if( rc ) return EXIT_FAILURE;
93 } else {
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 );
101 tool_bind( ld );
103 argv += optind;
104 argc -= optind;
106 if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
107 tool_server_controls( ld, NULL, 0 );
109 rc = ldap_whoami( ld, NULL, NULL, &id );
110 if ( rc != LDAP_SUCCESS ) {
111 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
112 rc = EXIT_FAILURE;
113 goto skip;
116 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
117 int cancelid;
119 switch ( argc ) {
120 case 2:
121 if ( lutil_atoi( &cancelid, argv[ 1 ] ) != 0 || cancelid < 0 ) {
122 fprintf( stderr, "invalid cancelid=%s\n\n", argv[ 1 ] );
123 usage();
125 break;
127 default:
128 fprintf( stderr, "need cancelid\n\n" );
129 usage();
132 rc = ldap_cancel( ld, cancelid, NULL, NULL, &id );
133 if ( rc != LDAP_SUCCESS ) {
134 tool_perror( "ldap_cancel", rc, NULL, NULL, NULL, NULL );
135 rc = EXIT_FAILURE;
136 goto skip;
139 } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
140 fprintf( stderr, "use ldappasswd(1) instead.\n\n", argv[ 0 ] );
141 usage();
142 /* TODO? */
144 } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
145 int ttl = 3600;
146 struct berval dn;
148 switch ( argc ) {
149 case 3:
150 ttl = atoi( argv[ 2 ] );
152 case 2:
153 dn.bv_val = argv[ 1 ];
154 dn.bv_len = strlen( dn.bv_val );
156 case 1:
157 break;
159 default:
160 fprintf( stderr, _("need DN [ttl]\n\n") );
161 usage();
164 tool_server_controls( ld, NULL, 0 );
166 rc = ldap_refresh( ld, &dn, ttl, NULL, NULL, &id );
167 if ( rc != LDAP_SUCCESS ) {
168 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
169 rc = EXIT_FAILURE;
170 goto skip;
173 } else {
174 char *p;
176 if ( argc != 1 ) {
177 usage();
180 p = strchr( argv[ 0 ], ':' );
181 if ( p == argv[ 0 ] ) {
182 usage();
185 if ( p != NULL )
186 *p++ = '\0';
188 if ( tool_is_oid( argv[ 0 ] ) ) {
189 struct berval reqdata;
190 struct berval type;
191 struct berval value;
192 int freeval;
194 if ( p != NULL ) {
195 p[ -1 ] = ':';
196 ldif_parse_line2( argv[ 0 ], &type, &value, &freeval );
197 p[ -1 ] = '\0';
199 if ( freeval ) {
200 reqdata = value;
201 } else {
202 ber_dupbv( &reqdata, &value );
207 tool_server_controls( ld, NULL, 0 );
209 rc = ldap_extended_operation( ld, argv[ 0 ], p ? &reqdata : NULL, NULL, NULL, &id );
210 if ( rc != LDAP_SUCCESS ) {
211 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
212 rc = EXIT_FAILURE;
213 goto skip;
215 } else {
216 fprintf( stderr, "unknown exop \"%s\"\n\n", argv[ 0 ] );
217 usage();
221 for ( ; ; ) {
222 struct timeval tv;
224 if ( tool_check_abandon( ld, id ) ) {
225 return LDAP_CANCELLED;
228 tv.tv_sec = 0;
229 tv.tv_usec = 100000;
231 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
232 if ( rc < 0 ) {
233 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
234 rc = EXIT_FAILURE;
235 goto skip;
238 if ( rc != 0 ) {
239 break;
243 rc = ldap_parse_result( ld, res,
244 &code, &matcheddn, &text, &refs, &ctrls, 0 );
245 if ( rc == LDAP_SUCCESS ) {
246 rc = code;
249 if ( rc != LDAP_SUCCESS ) {
250 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
251 rc = EXIT_FAILURE;
252 goto skip;
255 if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
256 char *retoid = NULL;
257 struct berval *retdata = NULL;
259 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
261 if ( rc != LDAP_SUCCESS ) {
262 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
263 rc = EXIT_FAILURE;
264 goto skip;
267 if ( retdata != NULL ) {
268 if ( retdata->bv_len == 0 ) {
269 printf(_("anonymous\n") );
270 } else {
271 printf("%s\n", retdata->bv_val );
275 ber_memfree( retoid );
276 ber_bvfree( retdata );
278 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
279 /* no extended response; returns specific errors */
280 assert( 0 );
282 } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
283 /* TODO */
285 } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
286 int newttl;
288 rc = ldap_parse_refresh( ld, res, &newttl );
290 if ( rc != LDAP_SUCCESS ) {
291 tool_perror( "ldap_parse_refresh", rc, NULL, NULL, NULL, NULL );
292 rc = EXIT_FAILURE;
293 goto skip;
296 printf( "newttl=%d\n", newttl );
298 } else if ( tool_is_oid( argv[ 0 ] ) ) {
299 char *retoid = NULL;
300 struct berval *retdata = NULL;
302 if( ldif < 2 ) {
303 printf(_("# extended operation response\n"));
306 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
307 if ( rc != LDAP_SUCCESS ) {
308 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
309 rc = EXIT_FAILURE;
310 goto skip;
313 if ( ldif < 2 && retoid != NULL ) {
314 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
315 "oid", retoid, strlen(retoid) );
318 ber_memfree( retoid );
320 if( retdata != NULL ) {
321 if ( ldif < 2 ) {
322 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
323 "data", retdata->bv_val, retdata->bv_len );
326 ber_bvfree( retdata );
330 if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
331 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
333 if( text && *text ) {
334 printf( _("Additional info: %s\n"), text );
337 if( matcheddn && *matcheddn ) {
338 printf( _("Matched DN: %s\n"), matcheddn );
341 if( refs ) {
342 int i;
343 for( i=0; refs[i]; i++ ) {
344 printf(_("Referral: %s\n"), refs[i] );
349 if (ctrls) {
350 tool_print_ctrls( ld, ctrls );
351 ldap_controls_free( ctrls );
354 ber_memfree( text );
355 ber_memfree( matcheddn );
356 ber_memvfree( (void **) refs );
358 skip:
359 /* disconnect from server */
360 tool_unbind( ld );
361 tool_destroy();
363 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;