]> git.sur5r.net Git - openldap/blob - clients/tools/ldapexop.c
rework control response handling
[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-2006 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 "lutil.h"
35 #include "lutil_ldap.h"
36 #include "ldap_defaults.h"
37
38 #include "common.h"
39
40
41 void
42 usage( void )
43 {
44         fprintf( stderr, _("Issue LDAP extended operations\n\n"));
45         fprintf( stderr, _("usage: %s [options]\n"), prog);
46         tool_common_usage();
47         exit( EXIT_FAILURE );
48 }
49
50
51 const char options[] = ""
52         "d:D:e:h:H:InO: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         char            *user = NULL;
70
71         LDAP            *ld = NULL;
72
73         char            *matcheddn = NULL, *text = NULL, **refs = 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                 switch ( argc ) {
108                 case 2:
109                         user = argv[ 1 ];
110
111                 case 1:
112                         break;
113
114                 default:
115                         fprintf( stderr, "need [user]\n\n" );
116                         usage();
117                 }
118
119                 if ( assertion || manageDSAit || noop ) {
120                         fprintf( stderr, _("controls incompatible with WhoAmI exop\n\n") );
121                         usage();
122                 }
123
124                 if ( authzid ) {
125                         tool_server_controls( ld, NULL, 0 );
126                 }
127
128                 rc = ldap_whoami( ld, NULL, NULL, &id ); 
129                 if ( rc != LDAP_SUCCESS ) {
130                         tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
131                         rc = EXIT_FAILURE;
132                         goto skip;
133                 }
134
135         } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
136                 int             cancelid;
137
138                 switch ( argc ) {
139                 case 2:
140                          if ( lutil_atoi( &cancelid, argv[ 1 ] ) != 0 || cancelid < 0 ) {
141                                 fprintf( stderr, "invalid cancelid=%s\n\n", argv[ 1 ] );
142                                 usage();
143                         }
144                         break;
145
146                 default:
147                         fprintf( stderr, "need cancelid\n\n" );
148                         usage();
149                 }
150
151                 rc = ldap_cancel( ld, cancelid, NULL, NULL, &id );
152                 if ( rc != LDAP_SUCCESS ) {
153                         tool_perror( "ldap_cancel", rc, NULL, NULL, NULL, NULL );
154                         rc = EXIT_FAILURE;
155                         goto skip;
156                 }
157
158         } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
159                 /* do we need this? */
160
161         } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
162                 int             ttl = 3600;
163                 struct berval   dn;
164
165                 switch ( argc ) {
166                 case 3:
167                         ttl = atoi( argv[ 2 ] );
168
169                 case 2:
170                         dn.bv_val = argv[ 1 ];
171                         dn.bv_len = strlen( dn.bv_val );
172
173                 case 1:
174                         break;
175
176                 default:
177                         fprintf( stderr, _("need DN [ttl]\n\n") );
178                         usage();
179                 }
180                 
181                 if ( assertion || manageDSAit || noop || authzid ) {
182                         tool_server_controls( ld, NULL, 0 );
183                 }
184
185                 rc = ldap_refresh( ld, &dn, ttl, NULL, NULL, &id ); 
186                 if ( rc != LDAP_SUCCESS ) {
187                         tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
188                         rc = EXIT_FAILURE;
189                         goto skip;
190                 }
191
192         } else if ( tool_is_oid( argv[ 0 ] ) ) {
193                 
194         } else {
195                 fprintf( stderr, "unknown exop \"%s\"\n\n", argv[ 0 ] );
196                 usage();
197         }
198
199         for ( ; ; ) {
200                 struct timeval  tv;
201
202                 if ( tool_check_abandon( ld, id ) ) {
203                         return LDAP_CANCELLED;
204                 }
205
206                 tv.tv_sec = 0;
207                 tv.tv_usec = 100000;
208
209                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
210                 if ( rc < 0 ) {
211                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
212                         rc = EXIT_FAILURE;
213                         goto skip;
214                 }
215
216                 if ( rc != 0 ) {
217                         break;
218                 }
219         }
220
221         rc = ldap_parse_result( ld, res,
222                 &code, &matcheddn, &text, &refs, NULL, 0 );
223         if ( rc == LDAP_SUCCESS ) {
224                 rc = code;
225         }
226
227         if ( rc != LDAP_SUCCESS ) {
228                 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
229                 rc = EXIT_FAILURE;
230                 goto skip;
231         }
232
233         if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
234                 char            *retoid = NULL;
235                 struct berval   *retdata = NULL;
236
237                 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
238
239                 if ( rc != LDAP_SUCCESS ) {
240                         tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
241                         rc = EXIT_FAILURE;
242                         goto skip;
243                 }
244
245                 if ( retdata != NULL ) {
246                         if ( retdata->bv_len == 0 ) {
247                                 printf(_("anonymous\n") );
248                         } else {
249                                 printf("%s\n", retdata->bv_val );
250                         }
251                 }
252
253                 ber_memfree( retoid );
254                 ber_bvfree( retdata );
255
256         } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
257                 /* no extended response; returns specific errors */
258                 assert( 0 );
259
260         } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
261
262         } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
263                 int     newttl;
264
265                 rc = ldap_parse_refresh( ld, res, &newttl );
266
267                 if ( rc != LDAP_SUCCESS ) {
268                         tool_perror( "ldap_parse_refresh", rc, NULL, NULL, NULL, NULL );
269                         rc = EXIT_FAILURE;
270                         goto skip;
271                 }
272
273                 printf( "newttl=%d\n", newttl );
274
275         } else if ( tool_is_oid( argv[ 0 ] ) ) {
276                 /* ... */
277         }
278
279         if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
280                 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
281
282                 if( text && *text ) {
283                         printf( _("Additional info: %s\n"), text );
284                 }
285
286                 if( matcheddn && *matcheddn ) {
287                         printf( _("Matched DN: %s\n"), matcheddn );
288                 }
289
290                 if( refs ) {
291                         int i;
292                         for( i=0; refs[i]; i++ ) {
293                                 printf(_("Referral: %s\n"), refs[i] );
294                         }
295                 }
296         }
297
298         ber_memfree( text );
299         ber_memfree( matcheddn );
300         ber_memvfree( (void **) refs );
301
302 skip:
303         /* disconnect from server */
304         tool_unbind( ld );
305         tool_destroy();
306
307         return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
308 }