]> git.sur5r.net Git - openldap/blob - clients/tools/ldapexop.c
Fix previous commit
[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 "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[:data]]\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                 tool_server_controls( ld, NULL, 0 );
120
121                 rc = ldap_whoami( ld, NULL, NULL, &id ); 
122                 if ( rc != LDAP_SUCCESS ) {
123                         tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
124                         rc = EXIT_FAILURE;
125                         goto skip;
126                 }
127
128         } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
129                 int             cancelid;
130
131                 switch ( argc ) {
132                 case 2:
133                          if ( lutil_atoi( &cancelid, argv[ 1 ] ) != 0 || cancelid < 0 ) {
134                                 fprintf( stderr, "invalid cancelid=%s\n\n", argv[ 1 ] );
135                                 usage();
136                         }
137                         break;
138
139                 default:
140                         fprintf( stderr, "need cancelid\n\n" );
141                         usage();
142                 }
143
144                 rc = ldap_cancel( ld, cancelid, NULL, NULL, &id );
145                 if ( rc != LDAP_SUCCESS ) {
146                         tool_perror( "ldap_cancel", rc, NULL, NULL, NULL, NULL );
147                         rc = EXIT_FAILURE;
148                         goto skip;
149                 }
150
151         } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
152                 fprintf( stderr, "use ldappasswd(1) instead.\n\n", argv[ 0 ] );
153                 usage();
154                 /* TODO? */
155
156         } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
157                 int             ttl = 3600;
158                 struct berval   dn;
159
160                 switch ( argc ) {
161                 case 3:
162                         ttl = atoi( argv[ 2 ] );
163
164                 case 2:
165                         dn.bv_val = argv[ 1 ];
166                         dn.bv_len = strlen( dn.bv_val );
167
168                 case 1:
169                         break;
170
171                 default:
172                         fprintf( stderr, _("need DN [ttl]\n\n") );
173                         usage();
174                 }
175                 
176                 tool_server_controls( ld, NULL, 0 );
177
178                 rc = ldap_refresh( ld, &dn, ttl, NULL, NULL, &id ); 
179                 if ( rc != LDAP_SUCCESS ) {
180                         tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
181                         rc = EXIT_FAILURE;
182                         goto skip;
183                 }
184
185         } else {
186                 char *p;
187
188                 if ( argc != 1 ) {
189                         usage();
190                 }
191
192                 p = strchr( argv[ 0 ], ':' );
193                 if ( p != NULL )
194                         *p++ = '\0';
195
196                 if ( tool_is_oid( argv[ 0 ] ) ) {
197                         struct berval reqdata;
198
199                         if ( p ) {
200                                 reqdata.bv_val = p;
201                                 reqdata.bv_len = strlen( reqdata.bv_val );
202                         }
203
204                         tool_server_controls( ld, NULL, 0 );
205
206                         rc = ldap_extended_operation( ld, argv[ 0 ], p ? &reqdata : NULL, NULL, NULL, &id );
207                         if ( rc != LDAP_SUCCESS ) {
208                                 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
209                                 rc = EXIT_FAILURE;
210                                 goto skip;
211                         }
212                 } else {
213                         fprintf( stderr, "unknown exop \"%s\"\n\n", argv[ 0 ] );
214                         usage();
215                 }
216         }
217
218         for ( ; ; ) {
219                 struct timeval  tv;
220
221                 if ( tool_check_abandon( ld, id ) ) {
222                         return LDAP_CANCELLED;
223                 }
224
225                 tv.tv_sec = 0;
226                 tv.tv_usec = 100000;
227
228                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
229                 if ( rc < 0 ) {
230                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
231                         rc = EXIT_FAILURE;
232                         goto skip;
233                 }
234
235                 if ( rc != 0 ) {
236                         break;
237                 }
238         }
239
240         rc = ldap_parse_result( ld, res,
241                 &code, &matcheddn, &text, &refs, NULL, 0 );
242         if ( rc == LDAP_SUCCESS ) {
243                 rc = code;
244         }
245
246         if ( rc != LDAP_SUCCESS ) {
247                 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
248                 rc = EXIT_FAILURE;
249                 goto skip;
250         }
251
252         if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
253                 char            *retoid = NULL;
254                 struct berval   *retdata = NULL;
255
256                 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
257
258                 if ( rc != LDAP_SUCCESS ) {
259                         tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
260                         rc = EXIT_FAILURE;
261                         goto skip;
262                 }
263
264                 if ( retdata != NULL ) {
265                         if ( retdata->bv_len == 0 ) {
266                                 printf(_("anonymous\n") );
267                         } else {
268                                 printf("%s\n", retdata->bv_val );
269                         }
270                 }
271
272                 ber_memfree( retoid );
273                 ber_bvfree( retdata );
274
275         } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
276                 /* no extended response; returns specific errors */
277                 assert( 0 );
278
279         } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
280                 /* TODO */
281
282         } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
283                 int     newttl;
284
285                 rc = ldap_parse_refresh( ld, res, &newttl );
286
287                 if ( rc != LDAP_SUCCESS ) {
288                         tool_perror( "ldap_parse_refresh", rc, NULL, NULL, NULL, NULL );
289                         rc = EXIT_FAILURE;
290                         goto skip;
291                 }
292
293                 printf( "newttl=%d\n", newttl );
294
295         } else if ( tool_is_oid( argv[ 0 ] ) ) {
296                 char            *retoid = NULL;
297                 struct berval   *retdata = NULL;
298
299                 if( ldif < 2 ) {
300                         printf(_("# extended operation response\n"));
301                 }
302
303                 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
304                 if ( rc != LDAP_SUCCESS ) {
305                         tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
306                         rc = EXIT_FAILURE;
307                         goto skip;
308                 }
309
310                 if ( ldif < 2 && retoid != NULL ) {
311                         tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
312                                 "oid", retoid, strlen(retoid) );
313                 }
314
315                 ber_memfree( retoid );
316
317                 if( retdata != NULL ) {
318                         if ( ldif < 2 ) {
319                                 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
320                                         "data", retdata->bv_val, retdata->bv_len );
321                         }
322
323                         ber_bvfree( retdata );
324                 }
325         }
326
327         if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
328                 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
329
330                 if( text && *text ) {
331                         printf( _("Additional info: %s\n"), text );
332                 }
333
334                 if( matcheddn && *matcheddn ) {
335                         printf( _("Matched DN: %s\n"), matcheddn );
336                 }
337
338                 if( refs ) {
339                         int i;
340                         for( i=0; refs[i]; i++ ) {
341                                 printf(_("Referral: %s\n"), refs[i] );
342                         }
343                 }
344         }
345
346         ber_memfree( text );
347         ber_memfree( matcheddn );
348         ber_memvfree( (void **) refs );
349
350 skip:
351         /* disconnect from server */
352         tool_unbind( ld );
353         tool_destroy();
354
355         return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
356 }