1 /* ldappasswd -- a tool for change LDAP passwords */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
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>.
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20 * All rights reserved.
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.
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).
40 #include <ac/stdlib.h>
43 #include <ac/socket.h>
44 #include <ac/string.h>
46 #include <ac/unistd.h>
50 #include "lutil_ldap.h"
51 #include "ldap_defaults.h"
56 static struct berval newpw = { 0, NULL };
57 static struct berval oldpw = { 0, NULL };
59 static int want_newpw = 0;
60 static int want_oldpw = 0;
62 static char *oldpwfile = NULL;
63 static char *newpwfile = NULL;
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 authentication 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"));
83 const char options[] = "a:As:St:T:"
84 "d:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
87 handle_private_option( int i )
91 case 'E': /* passwd extensions */ {
93 char *control, *cvalue;
94 if( protocol == LDAP_VERSION2 ) {
95 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
100 /* should be extended to support comma separated list of
101 * [!]key[=value] parameters, e.g. -E !foo,bar=567
106 if( optarg[0] == '!' ) {
111 control = strdup( optarg );
112 if ( (cvalue = strchr( control, '=' )) != NULL ) {
115 fprintf( stderr, _("Invalid passwd extension name: %s\n"), control );
120 case 'a': /* old password (secret) */
121 oldpw.bv_val = strdup( optarg );
124 for( p = optarg; *p != '\0'; p++ ) {
128 oldpw.bv_len = strlen( oldpw.bv_val );
131 case 'A': /* prompt for old password */
135 case 's': /* new password (secret) */
136 newpw.bv_val = strdup (optarg);
139 for( p = optarg; *p != '\0'; p++ ) {
143 newpw.bv_len = strlen( newpw.bv_val );
146 case 'S': /* prompt for user password */
166 main( int argc, char *argv[] )
172 struct berval bv = {0, NULL};
173 BerElement *ber = NULL;
175 int id, code = LDAP_OTHER;
177 char *matcheddn = NULL, *text = NULL, **refs = NULL;
179 struct berval *retdata = NULL;
181 tool_init( TOOL_PASSWD );
182 prog = lutil_progname( "ldappasswd", argc, argv );
185 protocol = LDAP_VERSION3;
187 tool_args( argc, argv );
189 if( argc - optind > 1 ) {
191 } else if ( argc - optind == 1 ) {
192 user = strdup( argv[optind] );
198 rc = lutil_get_filed_password( oldpwfile, &oldpw );
205 if( want_oldpw && oldpw.bv_val == NULL ) {
206 /* prompt for old password */
208 oldpw.bv_val = strdup(getpassphrase(_("Old password: ")));
209 ckoldpw = getpassphrase(_("Re-enter old password: "));
211 if( oldpw.bv_val == NULL || ckoldpw == NULL ||
212 strcmp( oldpw.bv_val, ckoldpw ))
214 fprintf( stderr, _("passwords do not match\n") );
219 oldpw.bv_len = strlen( oldpw.bv_val );
223 rc = lutil_get_filed_password( newpwfile, &newpw );
230 if( want_newpw && newpw.bv_val == NULL ) {
231 /* prompt for new password */
233 newpw.bv_val = strdup(getpassphrase(_("New password: ")));
234 cknewpw = getpassphrase(_("Re-enter new password: "));
236 if( newpw.bv_val == NULL || cknewpw == NULL ||
237 strcmp( newpw.bv_val, cknewpw ))
239 fprintf( stderr, _("passwords do not match\n") );
244 newpw.bv_len = strlen( newpw.bv_val );
248 rc = lutil_get_filed_password( pw_file, &passwd );
254 } else if ( want_bindpw ) {
255 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
256 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
259 ld = tool_conn_setup( 0, 0 );
263 if ( assertion || authzid || manageDSAit || noop ) {
264 tool_server_controls( ld, NULL, 0 );
267 if( user != NULL || oldpw.bv_val != NULL || newpw.bv_val != NULL ) {
268 /* build change password control */
269 ber = ber_alloc_t( LBER_USE_DER );
272 perror( "ber_alloc_t" );
277 ber_printf( ber, "{" /*}*/ );
280 ber_printf( ber, "ts",
281 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
285 if( oldpw.bv_val != NULL ) {
286 ber_printf( ber, "tO",
287 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &oldpw );
291 if( newpw.bv_val != NULL ) {
292 ber_printf( ber, "tO",
293 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &newpw );
297 ber_printf( ber, /*{*/ "N}" );
299 rc = ber_flatten2( ber, &bv, 0 );
302 perror( "ber_flatten2" );
313 rc = ldap_extended_operation( ld,
314 LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL,
319 if( rc != LDAP_SUCCESS ) {
320 tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
328 if ( tool_check_abandon( ld, id ) ) {
329 return LDAP_CANCELLED;
335 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
337 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
346 rc = ldap_parse_result( ld, res,
347 &code, &matcheddn, &text, &refs, NULL, 0 );
348 if( rc != LDAP_SUCCESS ) {
349 tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
354 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
355 if( rc != LDAP_SUCCESS ) {
356 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
361 if( retdata != NULL ) {
364 ber = ber_init( retdata );
367 perror( "ber_init" );
372 /* we should check the tag */
373 tag = ber_scanf( ber, "{a}", &s);
375 if( tag == LBER_ERROR ) {
376 perror( "ber_scanf" );
378 printf(_("New password: %s\n"), s);
384 } else if ( newpw.bv_val == NULL ) {
385 tool_perror( "ldap_parse_extended_result", LDAP_DECODING_ERROR,
386 " new password expected", NULL, NULL, NULL );
389 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
390 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
392 if( text && *text ) {
393 printf( _("Additional info: %s\n"), text );
396 if( matcheddn && *matcheddn ) {
397 printf( _("Matched DN: %s\n"), matcheddn );
402 for( i=0; refs[i]; i++ ) {
403 printf(_("Referral: %s\n"), refs[i] );
409 ber_memfree( matcheddn );
410 ber_memvfree( (void **) refs );
411 ber_memfree( retoid );
412 ber_bvfree( retdata );
414 rc = ( code == LDAP_SUCCESS ) ? EXIT_SUCCESS : EXIT_FAILURE;
417 /* disconnect from server */