]> git.sur5r.net Git - openldap/blob - clients/tools/ldappasswd.c
Use ldap_unbind_ext(3)
[openldap] / clients / tools / ldappasswd.c
1 /* ldappasswd -- a tool for change LDAP passwords */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * Portions Copyright 1998-2001 Net Boolean Incorporated.
8  * Portions Copyright 2001-2003 IBM Corporation.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms are permitted
23  * provided that this notice is preserved and that due credit is given
24  * to the University of Michigan at Ann Arbor.  The name of the
25  * University may not be used to endorse or promote products derived
26  * from this software without specific prior written permission.  This
27  * software is provided ``as is'' without express or implied warranty.
28  */
29 /* ACKNOWLEDGEMENTS:
30  * The original ldappasswd(1) tool was developed by Dave Storey (F5
31  * Network), based on other OpenLDAP client tools (which are, of
32  * course, based on U-MICH LDAP).  This version was rewritten
33  * by Kurt D. Zeilenga (based on other OpenLDAP client tools).
34  */
35
36 #include "portable.h"
37
38 #include <stdio.h>
39
40 #include <ac/stdlib.h>
41
42 #include <ac/ctype.h>
43 #include <ac/socket.h>
44 #include <ac/string.h>
45 #include <ac/time.h>
46 #include <ac/unistd.h>
47
48 #include <ldap.h>
49 #include "lutil.h"
50 #include "lutil_ldap.h"
51 #include "ldap_defaults.h"
52
53 #include "common.h"
54
55
56 static struct berval newpw = { 0, NULL };
57 static struct berval oldpw = { 0, NULL };
58
59 static int   want_newpw = 0;
60 static int   want_oldpw = 0;
61
62 static char *oldpwfile = NULL;
63 static char *newpwfile = NULL;
64
65 void
66 usage( void )
67 {
68         fprintf( stderr, _("Change password of an LDAP user\n\n"));
69         fprintf( stderr,_("usage: %s [options] [user]\n"), prog);
70         fprintf( stderr, _("  user: the autentication identity, commonly a DN\n"));
71         fprintf( stderr, _("Password change options:\n"));
72         fprintf( stderr, _("  -a secret  old password\n"));
73         fprintf( stderr, _("  -A         prompt for old password\n"));
74         fprintf( stderr, _("  -t file    read file for old password\n"));
75         fprintf( stderr, _("  -s secret  new password\n"));
76         fprintf( stderr, _("  -S         prompt for new password\n"));
77         fprintf( stderr, _("  -T file    read file for new password\n"));
78         tool_common_usage();
79         exit( EXIT_FAILURE );
80 }
81
82
83 const char options[] = "a:As:St:T:"
84         "d:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
85
86 int
87 handle_private_option( int i )
88 {
89         switch ( i ) {
90 #if 0
91         case 'E': /* passwd controls */ {
92                 int             crit;
93                 char    *control, *cvalue;
94                 if( protocol == LDAP_VERSION2 ) {
95                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
96                                  prog, protocol );
97                         exit( EXIT_FAILURE );
98                 }
99
100                 /* should be extended to support comma separated list of
101                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
102                  */
103
104                 crit = 0;
105                 cvalue = NULL;
106                 if( optarg[0] == '!' ) {
107                         crit = 1;
108                         optarg++;
109                 }
110
111                 control = strdup( optarg );
112                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
113                         *cvalue++ = '\0';
114                 }
115                 fprintf( stderr, _("Invalid passwd control name: %s\n"), control );
116                 usage();
117                 }
118 #endif
119
120         case 'a':       /* old password (secret) */
121                 oldpw.bv_val = strdup( optarg );
122                 {
123                         char* p;
124                         for( p = optarg; *p != '\0'; p++ ) {
125                                 *p = '\0';
126                         }
127                 }
128                 oldpw.bv_len = strlen( oldpw.bv_val );
129                 break;
130
131         case 'A':       /* prompt for old password */
132                 want_oldpw++;
133                 break;
134
135         case 's':       /* new password (secret) */
136                 newpw.bv_val = strdup (optarg);
137                 {
138                         char* p;
139                         for( p = optarg; *p != '\0'; p++ ) {
140                                 *p = '\0';
141                         }
142                 }
143                 newpw.bv_len = strlen( newpw.bv_val );
144                 break;
145
146         case 'S':       /* prompt for user password */
147                 want_newpw++;
148                 break;
149
150         case 't':
151                 oldpwfile = optarg;
152                 break;
153
154         case 'T':
155                 newpwfile = optarg;
156                 break;
157
158         default:
159                 return 0;
160         }
161         return 1;
162 }
163
164
165 int
166 main( int argc, char *argv[] )
167 {
168         int rc;
169         char    *user = NULL;
170
171         LDAP           *ld = NULL;
172         struct berval bv = {0, NULL};
173         BerElement  *ber = NULL;
174
175         int id, code = LDAP_OTHER;
176         LDAPMessage *res;
177         char *matcheddn = NULL, *text = NULL, **refs = NULL;
178         char    *retoid = NULL;
179         struct berval *retdata = NULL;
180
181     tool_init();
182         prog = lutil_progname( "ldappasswd", argc, argv );
183
184         /* LDAPv3 only */
185         protocol = LDAP_VERSION3;
186
187         tool_args( argc, argv );
188
189         if( argc - optind > 1 ) {
190                 usage();
191         } else if ( argc - optind == 1 ) {
192                 user = strdup( argv[optind] );
193         } else {
194                 user = NULL;
195         }
196
197         if( oldpwfile ) {
198                 rc = lutil_get_filed_password( oldpwfile, &oldpw );
199                 if( rc ) return EXIT_FAILURE;
200         }
201
202         if( want_oldpw && oldpw.bv_val == NULL ) {
203                 /* prompt for old password */
204                 char *ckoldpw;
205                 oldpw.bv_val = strdup(getpassphrase(_("Old password: ")));
206                 ckoldpw = getpassphrase(_("Re-enter old password: "));
207
208                 if( oldpw.bv_val == NULL || ckoldpw == NULL ||
209                         strcmp( oldpw.bv_val, ckoldpw ))
210                 {
211                         fprintf( stderr, _("passwords do not match\n") );
212                         return EXIT_FAILURE;
213                 }
214
215                 oldpw.bv_len = strlen( oldpw.bv_val );
216         }
217
218         if( newpwfile ) {
219                 rc = lutil_get_filed_password( newpwfile, &newpw );
220                 if( rc ) return EXIT_FAILURE;
221         }
222
223         if( want_newpw && newpw.bv_val == NULL ) {
224                 /* prompt for new password */
225                 char *cknewpw;
226                 newpw.bv_val = strdup(getpassphrase(_("New password: ")));
227                 cknewpw = getpassphrase(_("Re-enter new password: "));
228
229                 if( newpw.bv_val == NULL || cknewpw == NULL ||
230                         strcmp( newpw.bv_val, cknewpw ))
231                 {
232                         fprintf( stderr, _("passwords do not match\n") );
233                         return EXIT_FAILURE;
234                 }
235
236                 newpw.bv_len = strlen( newpw.bv_val );
237         }
238
239         if ( pw_file ) {
240                 rc = lutil_get_filed_password( pw_file, &passwd );
241                 if( rc ) return EXIT_FAILURE;
242
243         } else if ( want_bindpw ) {
244                 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
245                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
246         }
247
248         ld = tool_conn_setup( 0, 0 );
249
250         tool_bind( ld );
251
252         if ( assertion || authzid || manageDSAit || noop ) {
253                 tool_server_controls( ld, NULL, 0 );
254         }
255
256         if( user != NULL || oldpw.bv_val != NULL || newpw.bv_val != NULL ) {
257                 /* build change password control */
258                 ber = ber_alloc_t( LBER_USE_DER );
259
260                 if( ber == NULL ) {
261                         perror( "ber_alloc_t" );
262                         ldap_unbind_ext( ld, NULL, NULL );
263                         return EXIT_FAILURE;
264                 }
265
266                 ber_printf( ber, "{" /*}*/ );
267
268                 if( user != NULL ) {
269                         ber_printf( ber, "ts",
270                                 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
271                         free(user);
272                 }
273
274                 if( oldpw.bv_val != NULL ) {
275                         ber_printf( ber, "tO",
276                                 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &oldpw );
277                         free(oldpw.bv_val);
278                 }
279
280                 if( newpw.bv_val != NULL ) {
281                         ber_printf( ber, "tO",
282                                 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &newpw );
283                         free(newpw.bv_val);
284                 }
285
286                 ber_printf( ber, /*{*/ "N}" );
287
288                 rc = ber_flatten2( ber, &bv, 0 );
289
290                 if( rc < 0 ) {
291                         perror( "ber_flatten2" );
292                         ldap_unbind_ext( ld, NULL, NULL );
293                         return EXIT_FAILURE;
294                 }
295         }
296
297         if ( not ) {
298                 rc = LDAP_SUCCESS;
299                 goto skip;
300         }
301
302         rc = ldap_extended_operation( ld,
303                 LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL, 
304                 NULL, NULL, &id );
305
306         ber_free( ber, 1 );
307
308         if( rc != LDAP_SUCCESS ) {
309                 ldap_perror( ld, "ldap_extended_operation" );
310                 ldap_unbind_ext( ld, NULL, NULL );
311                 return EXIT_FAILURE;
312         }
313
314         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
315         if ( rc < 0 ) {
316                 ldap_perror( ld, "ldappasswd: ldap_result" );
317                 return rc;
318         }
319
320         rc = ldap_parse_result( ld, res,
321                 &code, &matcheddn, &text, &refs, NULL, 0 );
322
323         if( rc != LDAP_SUCCESS ) {
324                 ldap_perror( ld, "ldap_parse_result" );
325                 return rc;
326         }
327
328         rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
329
330         if( rc != LDAP_SUCCESS ) {
331                 ldap_perror( ld, "ldap_parse_result" );
332                 return rc;
333         }
334
335         if( retdata != NULL ) {
336                 ber_tag_t tag;
337                 char *s;
338                 ber = ber_init( retdata );
339
340                 if( ber == NULL ) {
341                         perror( "ber_init" );
342                         ldap_unbind_ext( ld, NULL, NULL );
343                         return EXIT_FAILURE;
344                 }
345
346                 /* we should check the tag */
347                 tag = ber_scanf( ber, "{a}", &s);
348
349                 if( tag == LBER_ERROR ) {
350                         perror( "ber_scanf" );
351                 } else {
352                         printf(_("New password: %s\n"), s);
353                         free( s );
354                 }
355
356                 ber_free( ber, 1 );
357         }
358
359         if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
360                 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
361
362                 if( text && *text ) {
363                         printf( _("Additional info: %s\n"), text );
364                 }
365
366                 if( matcheddn && *matcheddn ) {
367                         printf( _("Matched DN: %s\n"), matcheddn );
368                 }
369
370                 if( refs ) {
371                         int i;
372                         for( i=0; refs[i]; i++ ) {
373                                 printf(_("Referral: %s\n"), refs[i] );
374                         }
375                 }
376         }
377
378         ber_memfree( text );
379         ber_memfree( matcheddn );
380         ber_memvfree( (void **) refs );
381         ber_memfree( retoid );
382         ber_bvfree( retdata );
383
384 skip:
385         /* disconnect from server */
386         ldap_unbind_ext( ld, NULL, NULL );
387
388         return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
389 }