3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
17 #include <ac/unistd.h>
21 #include "lutil_ldap.h"
22 #include "ldap_defaults.h"
27 static char *newpw = NULL;
28 static char *oldpw = NULL;
29 static int want_newpw = 0;
30 static int want_oldpw = 0;
37 "Change password of an LDAP user\n\n"
38 "usage: %s [options] [user]\n"
39 " user: the autentication identity, commonly a DN\n"
40 "Password change options:\n"
41 " -a secret old password\n"
42 " -A prompt for old password\n"
43 " -s secret new password\n"
44 " -S prompt for new password\n"
51 const char options[] = "a:As:S" "Cd:D:e:h:H:InO:p:QR:U:vw:WxX:Y:Z";
54 handle_private_option( int i )
59 char *control, *cvalue;
60 case 'E': /* passwd controls */
61 if( version == LDAP_VERSION2 ) {
62 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
67 /* should be extended to support comma separated list of
68 * [!]key[=value] parameters, e.g. -E !foo,bar=567
73 if( optarg[0] == '!' ) {
78 control = strdup( optarg );
79 if ( (cvalue = strchr( control, '=' )) != NULL ) {
82 fprintf( stderr, "Invalid passwd control name: %s\n", control );
86 case 'a': /* old password (secret) */
87 oldpw = strdup (optarg);
91 for( p = optarg; *p != '\0'; p++ ) {
97 case 'A': /* prompt for old password */
101 case 's': /* new password (secret) */
102 newpw = strdup (optarg);
105 for( p = optarg; *p != '\0'; p++ ) {
111 case 'S': /* prompt for user password */
123 main( int argc, char *argv[] )
129 struct berval bv = {0};
130 BerElement *ber = NULL;
132 int id, code = LDAP_OTHER;
134 char *matcheddn = NULL, *text = NULL, **refs = NULL;
136 struct berval *retdata = NULL;
138 prog = lutil_progname( "ldappasswd", argc, argv );
141 version = LDAP_VERSION3;
143 tool_args( argc, argv );
145 if( argc - optind > 1 ) {
147 } else if ( argc - optind == 1 ) {
148 user = strdup( argv[optind] );
153 if( want_oldpw && oldpw == NULL ) {
154 /* prompt for old password */
156 oldpw = strdup(getpassphrase("Old password: "));
157 ckoldpw = getpassphrase("Re-enter old password: ");
159 if( oldpw== NULL || ckoldpw == NULL ||
160 strcmp( oldpw, ckoldpw ))
162 fprintf( stderr, "passwords do not match\n" );
167 if( want_newpw && newpw == NULL ) {
168 /* prompt for new password */
170 newpw = strdup(getpassphrase("New password: "));
171 cknewpw = getpassphrase("Re-enter new password: ");
173 if( newpw== NULL || cknewpw == NULL ||
174 strcmp( newpw, cknewpw ))
176 fprintf( stderr, "passwords do not match\n" );
181 if (want_bindpw && passwd.bv_val == NULL ) {
182 /* handle bind password */
183 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
184 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
187 ld = tool_conn_setup( 0, 0 );
191 if ( authzid || manageDSAit || noop )
192 tool_server_controls( ld, NULL, 0 );
194 if( user != NULL || oldpw != NULL || newpw != NULL ) {
195 /* build change password control */
196 ber = ber_alloc_t( LBER_USE_DER );
199 perror( "ber_alloc_t" );
204 ber_printf( ber, "{" /*}*/ );
207 ber_printf( ber, "ts",
208 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
212 if( oldpw != NULL ) {
213 ber_printf( ber, "ts",
214 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
218 if( newpw != NULL ) {
219 ber_printf( ber, "ts",
220 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
224 ber_printf( ber, /*{*/ "N}" );
226 rc = ber_flatten2( ber, &bv, 0 );
229 perror( "ber_flatten2" );
240 rc = ldap_extended_operation( ld,
241 LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL,
246 if( rc != LDAP_SUCCESS ) {
247 ldap_perror( ld, "ldap_extended_operation" );
252 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
254 ldap_perror( ld, "ldappasswd: ldap_result" );
258 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
260 if( rc != LDAP_SUCCESS ) {
261 ldap_perror( ld, "ldap_parse_result" );
265 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
267 if( rc != LDAP_SUCCESS ) {
268 ldap_perror( ld, "ldap_parse_result" );
272 if( retdata != NULL ) {
275 ber = ber_init( retdata );
278 perror( "ber_init" );
283 /* we should check the tag */
284 tag = ber_scanf( ber, "{a}", &s);
286 if( tag == LBER_ERROR ) {
287 perror( "ber_scanf" );
289 printf("New password: %s\n", s);
296 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
297 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
299 if( text && *text ) {
300 printf( "Additional info: %s\n", text );
303 if( matcheddn && *matcheddn ) {
304 printf( "Matched DN: %s\n", matcheddn );
309 for( i=0; refs[i]; i++ ) {
310 printf("Referral: %s\n", refs[i] );
316 ber_memfree( matcheddn );
317 ber_memvfree( (void **) refs );
318 ber_memfree( retoid );
319 ber_bvfree( retdata );
322 /* disconnect from server */
325 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;