]> git.sur5r.net Git - openldap/blob - clients/tools/ldapexop.c
Parse server controls on each response, and print them.
[openldap] / clients / tools / ldapexop.c
1 /* ldapexop.c -- a tool for performing well-known extended operations */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
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.
11  *
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>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was originally developed by Pierangelo Masarati for inclusion
18  * in OpenLDAP Software based, in part, on other client tools.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24
25 #include <ac/stdlib.h>
26
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>
32
33 #include <ldap.h>
34 #include "ldif.h"
35 #include "lutil.h"
36 #include "lutil_ldap.h"
37 #include "ldap_defaults.h"
38
39 #include "common.h"
40
41 void
42 usage( void )
43 {
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 );
48 }
49
50
51 const char options[] = ""
52         "d:D:e:h:H:InO:o:p:QR:U:vVw:WxX:y:Y:Z";
53
54 int
55 handle_private_option( int i )
56 {
57         switch ( i ) {
58         default:
59                 return 0;
60         }
61         return 1;
62 }
63
64
65 int
66 main( int argc, char *argv[] )
67 {
68         int             rc;
69
70         LDAP            *ld = NULL;
71
72         char            *matcheddn = NULL, *text = NULL, **refs = NULL;
73         LDAPControl **ctrls = NULL;
74         int             id, code;
75         LDAPMessage     *res;
76
77         tool_init( TOOL_EXOP );
78         prog = lutil_progname( "ldapexop", argc, argv );
79
80         /* LDAPv3 only */
81         protocol = LDAP_VERSION3;
82
83         tool_args( argc, argv );
84
85         if ( argc - optind < 1 ) {
86                 usage();
87         }
88
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;
96                 }
97         }
98
99         ld = tool_conn_setup( 0, 0 );
100
101         tool_bind( ld );
102
103         argv += optind;
104         argc -= optind;
105
106         if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
107                 tool_server_controls( ld, NULL, 0 );
108
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;
114                 }
115
116         } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
117                 int             cancelid;
118
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();
124                         }
125                         break;
126
127                 default:
128                         fprintf( stderr, "need cancelid\n\n" );
129                         usage();
130                 }
131
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;
137                 }
138
139         } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
140                 fprintf( stderr, "use ldappasswd(1) instead.\n\n", argv[ 0 ] );
141                 usage();
142                 /* TODO? */
143
144         } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
145                 int             ttl = 3600;
146                 struct berval   dn;
147
148                 switch ( argc ) {
149                 case 3:
150                         ttl = atoi( argv[ 2 ] );
151
152                 case 2:
153                         dn.bv_val = argv[ 1 ];
154                         dn.bv_len = strlen( dn.bv_val );
155
156                 case 1:
157                         break;
158
159                 default:
160                         fprintf( stderr, _("need DN [ttl]\n\n") );
161                         usage();
162                 }
163                 
164                 tool_server_controls( ld, NULL, 0 );
165
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;
171                 }
172
173         } else {
174                 char *p;
175
176                 if ( argc != 1 ) {
177                         usage();
178                 }
179
180                 p = strchr( argv[ 0 ], ':' );
181                 if ( p == argv[ 0 ] ) {
182                         usage();
183                 }
184
185                 if ( p != NULL )
186                         *p++ = '\0';
187
188                 if ( tool_is_oid( argv[ 0 ] ) ) {
189                         struct berval   reqdata;
190                         struct berval   type;
191                         struct berval   value;
192                         int             freeval;
193
194                         if ( p != NULL ) {
195                                 p[ -1 ] = ':';
196                                 ldif_parse_line2( argv[ 0 ], &type, &value, &freeval );
197                                 p[ -1 ] = '\0';
198
199                                 if ( freeval ) {
200                                         reqdata = value;
201                                 } else {
202                                         ber_dupbv( &reqdata, &value );
203                                 }
204                         }
205
206
207                         tool_server_controls( ld, NULL, 0 );
208
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;
214                         }
215                 } else {
216                         fprintf( stderr, "unknown exop \"%s\"\n\n", argv[ 0 ] );
217                         usage();
218                 }
219         }
220
221         for ( ; ; ) {
222                 struct timeval  tv;
223
224                 if ( tool_check_abandon( ld, id ) ) {
225                         return LDAP_CANCELLED;
226                 }
227
228                 tv.tv_sec = 0;
229                 tv.tv_usec = 100000;
230
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;
236                 }
237
238                 if ( rc != 0 ) {
239                         break;
240                 }
241         }
242
243         rc = ldap_parse_result( ld, res,
244                 &code, &matcheddn, &text, &refs, &ctrls, 0 );
245         if ( rc == LDAP_SUCCESS ) {
246                 rc = code;
247         }
248
249         if ( rc != LDAP_SUCCESS ) {
250                 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
251                 rc = EXIT_FAILURE;
252                 goto skip;
253         }
254
255         if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
256                 char            *retoid = NULL;
257                 struct berval   *retdata = NULL;
258
259                 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
260
261                 if ( rc != LDAP_SUCCESS ) {
262                         tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
263                         rc = EXIT_FAILURE;
264                         goto skip;
265                 }
266
267                 if ( retdata != NULL ) {
268                         if ( retdata->bv_len == 0 ) {
269                                 printf(_("anonymous\n") );
270                         } else {
271                                 printf("%s\n", retdata->bv_val );
272                         }
273                 }
274
275                 ber_memfree( retoid );
276                 ber_bvfree( retdata );
277
278         } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
279                 /* no extended response; returns specific errors */
280                 assert( 0 );
281
282         } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
283                 /* TODO */
284
285         } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
286                 int     newttl;
287
288                 rc = ldap_parse_refresh( ld, res, &newttl );
289
290                 if ( rc != LDAP_SUCCESS ) {
291                         tool_perror( "ldap_parse_refresh", rc, NULL, NULL, NULL, NULL );
292                         rc = EXIT_FAILURE;
293                         goto skip;
294                 }
295
296                 printf( "newttl=%d\n", newttl );
297
298         } else if ( tool_is_oid( argv[ 0 ] ) ) {
299                 char            *retoid = NULL;
300                 struct berval   *retdata = NULL;
301
302                 if( ldif < 2 ) {
303                         printf(_("# extended operation response\n"));
304                 }
305
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;
311                 }
312
313                 if ( ldif < 2 && retoid != NULL ) {
314                         tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
315                                 "oid", retoid, strlen(retoid) );
316                 }
317
318                 ber_memfree( retoid );
319
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 );
324                         }
325
326                         ber_bvfree( retdata );
327                 }
328         }
329
330         if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
331                 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
332
333                 if( text && *text ) {
334                         printf( _("Additional info: %s\n"), text );
335                 }
336
337                 if( matcheddn && *matcheddn ) {
338                         printf( _("Matched DN: %s\n"), matcheddn );
339                 }
340
341                 if( refs ) {
342                         int i;
343                         for( i=0; refs[i]; i++ ) {
344                                 printf(_("Referral: %s\n"), refs[i] );
345                         }
346                 }
347         }
348
349     if (ctrls) {
350                 tool_print_ctrls( ld, ctrls );
351                 ldap_controls_free( ctrls );
352         }
353
354         ber_memfree( text );
355         ber_memfree( matcheddn );
356         ber_memvfree( (void **) refs );
357
358 skip:
359         /* disconnect from server */
360         tool_unbind( ld );
361         tool_destroy();
362
363         return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
364 }