]> git.sur5r.net Git - openldap/blob - clients/tools/ldapexop.c
Use : to separate OID/data in exop - will use :: for base64 encoding data
[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 if ( tool_is_oid( argv[ 0 ] ) ) {
186                 struct berval reqdata;
187                 struct berval *reqdatap;
188                 char *p;
189
190                 if ( argc > 1 ) {
191                         usage();
192                 }
193
194                 p = strchr( argv[ 0 ], ':' );
195                 if ( p != NULL ) {
196                         *p++ = '\0';
197
198                         reqdata.bv_val = p;
199                         reqdata.bv_len = strlen( reqdata.bv_val );
200                         reqdatap = &reqdata;
201                 } else
202                         reqdatap = NULL;
203
204                 tool_server_controls( ld, NULL, 0 );
205
206                 rc = ldap_extended_operation( ld, argv[ 0 ], reqdatap, 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         for ( ; ; ) {
218                 struct timeval  tv;
219
220                 if ( tool_check_abandon( ld, id ) ) {
221                         return LDAP_CANCELLED;
222                 }
223
224                 tv.tv_sec = 0;
225                 tv.tv_usec = 100000;
226
227                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
228                 if ( rc < 0 ) {
229                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
230                         rc = EXIT_FAILURE;
231                         goto skip;
232                 }
233
234                 if ( rc != 0 ) {
235                         break;
236                 }
237         }
238
239         rc = ldap_parse_result( ld, res,
240                 &code, &matcheddn, &text, &refs, NULL, 0 );
241         if ( rc == LDAP_SUCCESS ) {
242                 rc = code;
243         }
244
245         if ( rc != LDAP_SUCCESS ) {
246                 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
247                 rc = EXIT_FAILURE;
248                 goto skip;
249         }
250
251         if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) {
252                 char            *retoid = NULL;
253                 struct berval   *retdata = NULL;
254
255                 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
256
257                 if ( rc != LDAP_SUCCESS ) {
258                         tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
259                         rc = EXIT_FAILURE;
260                         goto skip;
261                 }
262
263                 if ( retdata != NULL ) {
264                         if ( retdata->bv_len == 0 ) {
265                                 printf(_("anonymous\n") );
266                         } else {
267                                 printf("%s\n", retdata->bv_val );
268                         }
269                 }
270
271                 ber_memfree( retoid );
272                 ber_bvfree( retdata );
273
274         } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
275                 /* no extended response; returns specific errors */
276                 assert( 0 );
277
278         } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) {
279                 /* TODO */
280
281         } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) {
282                 int     newttl;
283
284                 rc = ldap_parse_refresh( ld, res, &newttl );
285
286                 if ( rc != LDAP_SUCCESS ) {
287                         tool_perror( "ldap_parse_refresh", rc, NULL, NULL, NULL, NULL );
288                         rc = EXIT_FAILURE;
289                         goto skip;
290                 }
291
292                 printf( "newttl=%d\n", newttl );
293
294         } else if ( tool_is_oid( argv[ 0 ] ) ) {
295                 char            *retoid = NULL;
296                 struct berval   *retdata = NULL;
297
298                 if( ldif < 2 ) {
299                         printf(_("# extended operation response\n"));
300                 }
301
302                 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
303                 if ( rc != LDAP_SUCCESS ) {
304                         tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
305                         rc = EXIT_FAILURE;
306                         goto skip;
307                 }
308
309                 if ( ldif < 2 && retoid != NULL ) {
310                         tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
311                                 "oid", retoid, strlen(retoid) );
312                 }
313
314                 ber_memfree( retoid );
315
316                 if( retdata != NULL ) {
317                         if ( ldif < 2 ) {
318                                 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
319                                         "data", retdata->bv_val, retdata->bv_len );
320                         }
321
322                         ber_bvfree( retdata );
323                 }
324         }
325
326         if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
327                 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
328
329                 if( text && *text ) {
330                         printf( _("Additional info: %s\n"), text );
331                 }
332
333                 if( matcheddn && *matcheddn ) {
334                         printf( _("Matched DN: %s\n"), matcheddn );
335                 }
336
337                 if( refs ) {
338                         int i;
339                         for( i=0; refs[i]; i++ ) {
340                                 printf(_("Referral: %s\n"), refs[i] );
341                         }
342                 }
343         }
344
345         ber_memfree( text );
346         ber_memfree( matcheddn );
347         ber_memvfree( (void **) refs );
348
349 skip:
350         /* disconnect from server */
351         tool_unbind( ld );
352         tool_destroy();
353
354         return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
355 }