1 /* ldappasswd -- a tool for change LDAP passwords */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
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.
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.
29 /* The original ldappasswd(1) tool was developed by Dave Storey (F5
30 * Network), based on other OpenLDAP client tools (which are, of
31 * course, based on U-MICH LDAP). This version was rewritten
32 * by Kurt D. Zeilenga (based on other OpenLDAP client tools).
39 #include <ac/stdlib.h>
42 #include <ac/socket.h>
43 #include <ac/string.h>
45 #include <ac/unistd.h>
49 #include "lutil_ldap.h"
50 #include "ldap_defaults.h"
55 static struct berval newpw = { 0, NULL };
56 static struct berval oldpw = { 0, NULL };
58 static int want_newpw = 0;
59 static int want_oldpw = 0;
61 static char *oldpwfile = NULL;
62 static char *newpwfile = NULL;
67 fprintf( stderr, _("Change password of an LDAP user\n\n"));
68 fprintf( stderr,_("usage: %s [options] [user]\n"), prog);
69 fprintf( stderr, _(" user: the autentication identity, commonly a DN\n"));
70 fprintf( stderr, _("Password change options:\n"));
71 fprintf( stderr, _(" -a secret old password\n"));
72 fprintf( stderr, _(" -A prompt for old password\n"));
73 fprintf( stderr, _(" -t file read file for old password\n"));
74 fprintf( stderr, _(" -s secret new password\n"));
75 fprintf( stderr, _(" -S prompt for new password\n"));
76 fprintf( stderr, _(" -T file read file for new password\n"));
82 const char options[] = "a:As:St:T:"
83 "Cd:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
86 handle_private_option( int i )
90 case 'E': /* passwd controls */ {
92 char *control, *cvalue;
93 if( protocol == LDAP_VERSION2 ) {
94 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
99 /* should be extended to support comma separated list of
100 * [!]key[=value] parameters, e.g. -E !foo,bar=567
105 if( optarg[0] == '!' ) {
110 control = strdup( optarg );
111 if ( (cvalue = strchr( control, '=' )) != NULL ) {
114 fprintf( stderr, _("Invalid passwd control name: %s\n"), control );
119 case 'a': /* old password (secret) */
120 oldpw.bv_val = strdup( optarg );
123 for( p = optarg; *p != '\0'; p++ ) {
127 oldpw.bv_len = strlen( oldpw.bv_val );
130 case 'A': /* prompt for old password */
134 case 's': /* new password (secret) */
135 newpw.bv_val = strdup (optarg);
138 for( p = optarg; *p != '\0'; p++ ) {
142 newpw.bv_len = strlen( newpw.bv_val );
145 case 'S': /* prompt for user password */
165 main( int argc, char *argv[] )
171 struct berval bv = {0, NULL};
172 BerElement *ber = NULL;
174 int id, code = LDAP_OTHER;
176 char *matcheddn = NULL, *text = NULL, **refs = NULL;
178 struct berval *retdata = NULL;
181 prog = lutil_progname( "ldappasswd", argc, argv );
184 protocol = LDAP_VERSION3;
186 tool_args( argc, argv );
188 if( argc - optind > 1 ) {
190 } else if ( argc - optind == 1 ) {
191 user = strdup( argv[optind] );
197 rc = lutil_get_filed_password( oldpwfile, &oldpw );
198 if( rc ) return EXIT_FAILURE;
201 if( want_oldpw && oldpw.bv_val == NULL ) {
202 /* prompt for old password */
204 oldpw.bv_val = strdup(getpassphrase(_("Old password: ")));
205 ckoldpw = getpassphrase(_("Re-enter old password: "));
207 if( oldpw.bv_val == NULL || ckoldpw == NULL ||
208 strcmp( oldpw.bv_val, ckoldpw ))
210 fprintf( stderr, _("passwords do not match\n") );
214 oldpw.bv_len = strlen( oldpw.bv_val );
218 rc = lutil_get_filed_password( newpwfile, &newpw );
219 if( rc ) return EXIT_FAILURE;
222 if( want_newpw && newpw.bv_val == NULL ) {
223 /* prompt for new password */
225 newpw.bv_val = strdup(getpassphrase(_("New password: ")));
226 cknewpw = getpassphrase(_("Re-enter new password: "));
228 if( newpw.bv_val == NULL || cknewpw == NULL ||
229 strcmp( newpw.bv_val, cknewpw ))
231 fprintf( stderr, _("passwords do not match\n") );
235 newpw.bv_len = strlen( newpw.bv_val );
239 rc = lutil_get_filed_password( pw_file, &passwd );
240 if( rc ) return EXIT_FAILURE;
242 } else if ( want_bindpw ) {
243 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
244 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
247 ld = tool_conn_setup( 0, 0 );
251 if ( assertion || authzid || manageDSAit || noop ) {
252 tool_server_controls( ld, NULL, 0 );
255 if( user != NULL || oldpw.bv_val != NULL || newpw.bv_val != NULL ) {
256 /* build change password control */
257 ber = ber_alloc_t( LBER_USE_DER );
260 perror( "ber_alloc_t" );
265 ber_printf( ber, "{" /*}*/ );
268 ber_printf( ber, "ts",
269 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
273 if( oldpw.bv_val != NULL ) {
274 ber_printf( ber, "tO",
275 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &oldpw );
279 if( newpw.bv_val != NULL ) {
280 ber_printf( ber, "tO",
281 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &newpw );
285 ber_printf( ber, /*{*/ "N}" );
287 rc = ber_flatten2( ber, &bv, 0 );
290 perror( "ber_flatten2" );
301 rc = ldap_extended_operation( ld,
302 LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL,
307 if( rc != LDAP_SUCCESS ) {
308 ldap_perror( ld, "ldap_extended_operation" );
313 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
315 ldap_perror( ld, "ldappasswd: ldap_result" );
319 rc = ldap_parse_result( ld, res,
320 &code, &matcheddn, &text, &refs, NULL, 0 );
322 if( rc != LDAP_SUCCESS ) {
323 ldap_perror( ld, "ldap_parse_result" );
327 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
329 if( rc != LDAP_SUCCESS ) {
330 ldap_perror( ld, "ldap_parse_result" );
334 if( retdata != NULL ) {
337 ber = ber_init( retdata );
340 perror( "ber_init" );
345 /* we should check the tag */
346 tag = ber_scanf( ber, "{a}", &s);
348 if( tag == LBER_ERROR ) {
349 perror( "ber_scanf" );
351 printf(_("New password: %s\n"), s);
358 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
359 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
361 if( text && *text ) {
362 printf( _("Additional info: %s\n"), text );
365 if( matcheddn && *matcheddn ) {
366 printf( _("Matched DN: %s\n"), matcheddn );
371 for( i=0; refs[i]; i++ ) {
372 printf(_("Referral: %s\n"), refs[i] );
378 ber_memfree( matcheddn );
379 ber_memvfree( (void **) refs );
380 ber_memfree( retoid );
381 ber_bvfree( retdata );
384 /* disconnect from server */
387 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;