3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/signal.h>
15 #include <ac/socket.h>
16 #include <ac/string.h>
18 #include <ac/unistd.h>
22 #include "lutil_ldap.h"
23 #include "ldap_defaults.h"
25 static int verbose = 0;
31 "Change the password of an LDAP entry\n\n"
32 "usage: %s [options] dn\n"
33 " dn: the DN of the entry whose password must be changed\n"
35 " -a secret\told password\n"
36 " -A\t\tprompt for old password\n"
37 " -d level\tdebugging level\n"
38 " -C\t\tchase referrals\n"
39 " -D binddn\tbind DN\n"
40 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
41 " -h host\t\tLDAP server (default: localhost)\n"
42 " -I\t\trequest SASL integrity checking (-II to make it\n"
44 " -n\t\tmake no modifications\n"
45 " -O secprops\tSASL security properties\n"
46 " -p port\t\tport on LDAP server\n"
47 " -S\t\tprompt for new password\n"
48 " -s secret\tnew password\n"
49 " -U user\t\tSASL authentication identity (username)\n"
50 " -v\t\tverbose mode\n"
51 " -w passwd\tbind password (for simple authentication)\n"
52 " -W\t\tprompt for bind password\n"
53 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
54 " -Y mech\t\tSASL mechanism\n"
55 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
62 main( int argc, char *argv[] )
65 char *ldaphost = NULL;
70 struct berval passwd = { 0, NULL };
83 int authmethod = LDAP_AUTH_SIMPLE;
84 #ifdef HAVE_CYRUS_SASL
85 char *sasl_authc_id = NULL;
86 char *sasl_authz_id = NULL;
87 char *sasl_mech = NULL;
88 char *sasl_secprops = NULL;
93 struct berval *bv = NULL;
97 char *matcheddn = NULL, *text = NULL, **refs = NULL;
99 struct berval *retdata = NULL;
104 while( (i = getopt( argc, argv,
105 "Aa:CD:d:h:nO:p:Ss:U:vWw:X:Y:Z" )) != EOF )
108 case 'A': /* prompt for oldr password */
111 case 'a': /* old password (secret) */
112 oldpw = strdup (optarg);
117 for( p = optarg; *p == '\0'; p++ ) {
125 case 'D': /* bind distinguished name */
126 binddn = strdup (optarg);
129 case 'd': /* debugging option */
130 debug |= atoi (optarg);
133 case 'h': /* ldap host */
134 ldaphost = strdup (optarg);
137 case 'n': /* don't update entry(s) */
141 case 'p': /* ldap port */
142 ldapport = strtol( optarg, NULL, 10 );
145 case 'S': /* prompt for user password */
149 case 's': /* new password (secret) */
150 newpw = strdup (optarg);
154 for( p = optarg; *p == '\0'; p++ ) {
160 case 'v': /* verbose */
164 case 'W': /* prompt for bind password */
168 case 'w': /* bind password */
169 passwd.bv_val = strdup (optarg);
173 for( p = optarg; *p == '\0'; p++ ) {
177 passwd.bv_len = strlen( passwd.bv_val );
181 #ifdef HAVE_CYRUS_SASL
182 sasl_secprops = strdup( optarg );
183 authmethod = LDAP_AUTH_SASL;
185 fprintf( stderr, "%s was not compiled with SASL support\n",
187 return( EXIT_FAILURE );
191 #ifdef HAVE_CYRUS_SASL
192 if ( strcasecmp( optarg, "any" ) &&
193 strcmp( optarg, "*" ) ) {
194 sasl_mech = strdup( optarg );
196 authmethod = LDAP_AUTH_SASL;
198 fprintf( stderr, "%s was not compiled with SASL "
199 "support\n", argv[0] );
200 return( EXIT_FAILURE );
204 #ifdef HAVE_CYRUS_SASL
205 sasl_authc_id = strdup( optarg );
206 authmethod = LDAP_AUTH_SASL;
208 fprintf( stderr, "%s was not compiled with SASL "
209 "support\n", argv[0] );
210 return( EXIT_FAILURE );
214 #ifdef HAVE_CYRUS_SASL
215 sasl_authz_id = strdup( optarg );
216 authmethod = LDAP_AUTH_SASL;
218 fprintf( stderr, "%s was not compiled with SASL "
219 "support\n", argv[0] );
220 return( EXIT_FAILURE );
227 fprintf( stderr, "%s was not compiled with TLS "
228 "support\n", argv[0] );
229 return( EXIT_FAILURE );
238 if( argc - optind != 1 ) {
242 dn = strdup( argv[optind] );
244 if( want_oldpw && oldpw == NULL ) {
245 /* prompt for old password */
247 newpw = strdup(getpassphrase("Old password: "));
248 ckoldpw = getpassphrase("Re-enter old password: ");
250 if( newpw== NULL || ckoldpw == NULL ||
251 strncmp( oldpw, ckoldpw, strlen(oldpw) ))
253 fprintf( stderr, "passwords do not match\n" );
258 if( want_newpw && newpw == NULL ) {
259 /* prompt for new password */
261 newpw = strdup(getpassphrase("New password: "));
262 cknewpw = getpassphrase("Re-enter new password: ");
264 if( newpw== NULL || cknewpw == NULL ||
265 strncmp( newpw, cknewpw, strlen(newpw) ))
267 fprintf( stderr, "passwords do not match\n" );
272 if( binddn == NULL && dn != NULL ) {
276 if( passwd.bv_val == NULL ) {
277 passwd.bv_val = oldpw;
278 passwd.bv_len = oldpw == NULL ? 0 : strlen( oldpw );
282 if (want_bindpw && passwd.bv_val == NULL ) {
283 /* handle bind password */
284 fprintf( stderr, "Bind DN: %s\n", binddn );
285 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
286 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
290 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
291 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
293 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
294 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
299 (void) SIGNAL( SIGPIPE, SIG_IGN );
302 /* connect to server */
303 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
309 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
310 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
312 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
313 referrals ? "on" : "off" );
319 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
321 if(rc != LDAP_OPT_SUCCESS ) {
322 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
326 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
328 ldap_perror( ld, "ldap_start_tls" );
329 return( EXIT_FAILURE );
331 fprintf( stderr, "WARNING: could not start TLS\n" );
334 if ( authmethod == LDAP_AUTH_SASL ) {
335 #ifdef HAVE_CYRUS_SASL
336 ldap_set_sasl_interact_proc( ld, lutil_sasl_interact );
338 if( sasl_secprops != NULL ) {
339 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
340 (void *) sasl_secprops );
342 if( rc != LDAP_OPT_SUCCESS ) {
344 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
346 return( EXIT_FAILURE );
350 rc = ldap_sasl_interactive_bind_s( ld, binddn,
351 sasl_mech, NULL, NULL );
353 if( rc != LDAP_SUCCESS ) {
354 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
355 return( EXIT_FAILURE );
358 fprintf( stderr, "%s was not compiled with SASL support\n",
360 return( EXIT_FAILURE );
364 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
366 ldap_perror( ld, "ldap_bind" );
367 return( EXIT_FAILURE );
371 if( dn != NULL || oldpw != NULL || newpw != NULL ) {
372 /* build change password control */
373 BerElement *ber = ber_alloc_t( LBER_USE_DER );
376 perror( "ber_alloc_t" );
381 ber_printf( ber, "{" /*}*/ );
384 ber_printf( ber, "ts",
385 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
389 if( oldpw != NULL ) {
390 ber_printf( ber, "ts",
391 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
395 if( newpw != NULL ) {
396 ber_printf( ber, "ts",
397 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
401 ber_printf( ber, /*{*/ "N}" );
403 rc = ber_flatten( ber, &bv );
406 perror( "ber_flatten" );
419 rc = ldap_extended_operation( ld,
420 LDAP_EXOP_X_MODIFY_PASSWD, bv,
425 if( rc != LDAP_SUCCESS ) {
426 ldap_perror( ld, "ldap_extended_operation" );
431 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
433 ldap_perror( ld, "ldappasswd: ldap_result" );
437 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
439 if( rc != LDAP_SUCCESS ) {
440 ldap_perror( ld, "ldap_parse_result" );
444 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
446 if( rc != LDAP_SUCCESS ) {
447 ldap_perror( ld, "ldap_parse_result" );
451 if( retdata != NULL ) {
454 BerElement *ber = ber_init( retdata );
457 perror( "ber_init" );
462 /* we should check the tag */
463 tag = ber_scanf( ber, "{a}", &s);
465 if( tag == LBER_ERROR ) {
466 perror( "ber_scanf" );
468 printf("New password: %s\n", s);
475 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
476 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
478 if( text && *text ) {
479 printf( "Additional info: %s\n", text );
482 if( matcheddn && *matcheddn ) {
483 printf( "Matched DN: %s\n", matcheddn );
488 for( i=0; refs[i]; i++ ) {
489 printf("Referral: %s\n", refs[i] );
495 ber_memfree( matcheddn );
496 ber_memvfree( (void **) refs );
497 ber_memfree( retoid );
498 ber_bvfree( retdata );
501 /* disconnect from server */