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 password of an LDAP user\n\n"
32 "usage: %s [options] user\n"
33 " user: the identity of the user, normally a DN\n"
34 "Password change options:\n"
35 " -a secret old password\n"
36 " -A prompt for old password\n"
37 " -s secret new password\n"
38 " -S prompt for new password\n"
41 " -d level set LDAP debugging level to `level'\n"
42 " -D binddn bind DN\n"
43 " -f file read operations from `file'\n"
44 " -h host LDAP server(s)\n"
45 " -H URI LDAP Uniform Resource Indentifier(s)\n"
46 " -I use SASL Interactive mode\n"
47 " -n show what would be done but don't actually search\n"
48 " -O props SASL security properties\n"
49 " -p port port on LDAP server\n"
50 " -Q use SASL Quiet mode\n"
51 " -R realm SASL realm\n"
52 " -U user SASL authentication identity (username)\n"
53 " -v run in verbose mode (diagnostics to standard output)\n"
54 " -w passwd bind passwd (for simple authentication)\n"
55 " -W prompt for bind passwd\n"
56 " -x Simple authentication\n"
57 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
58 " -Y mech SASL mechanism\n"
59 " -Z Start TLS request (-ZZ to require successful response)\n"
66 main( int argc, char *argv[] )
70 char *ldaphost = NULL;
76 struct berval passwd = { 0, NULL };
91 #ifdef HAVE_CYRUS_SASL
92 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
93 char *sasl_realm = NULL;
94 char *sasl_authc_id = NULL;
95 char *sasl_authz_id = NULL;
96 char *sasl_mech = NULL;
97 char *sasl_secprops = NULL;
102 struct berval *bv = NULL;
106 char *matcheddn = NULL, *text = NULL, **refs = NULL;
108 struct berval *retdata = NULL;
110 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
115 while( (i = getopt( argc, argv,
116 "Aa:Ss:" "Cd:D:h:H:InO:p:QRU:vw:WxX:Y:Z" )) != EOF )
119 /* Password Options */
120 case 'A': /* prompt for old password */
124 case 'a': /* old password (secret) */
125 oldpw = strdup (optarg);
130 for( p = optarg; *p == '\0'; p++ ) {
136 case 'S': /* prompt for user password */
140 case 's': /* new password (secret) */
141 newpw = strdup (optarg);
145 for( p = optarg; *p == '\0'; p++ ) {
151 /* Common Options (including options we don't use) */
156 debug |= atoi( optarg );
158 case 'D': /* bind DN */
159 if( binddn != NULL ) {
160 fprintf( stderr, "%s: -D previously specified\n" );
163 binddn = strdup( optarg );
165 case 'h': /* ldap host */
166 if( ldapuri != NULL ) {
167 fprintf( stderr, "%s: -h incompatible with -H\n" );
170 if( ldaphost != NULL ) {
171 fprintf( stderr, "%s: -h previously specified\n" );
174 ldaphost = strdup( optarg );
176 case 'H': /* ldap URI */
177 if( ldaphost != NULL ) {
178 fprintf( stderr, "%s: -H incompatible with -h\n" );
182 fprintf( stderr, "%s: -H incompatible with -p\n" );
185 if( ldapuri != NULL ) {
186 fprintf( stderr, "%s: -H previously specified\n" );
189 ldapuri = strdup( optarg );
192 #ifdef HAVE_CYRUS_SASL
193 if( version == LDAP_VERSION2 ) {
194 fprintf( stderr, "%s: -I incompatible with version %d\n",
198 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
199 fprintf( stderr, "%s: incompatible previous "
200 "authentication choice\n",
204 authmethod = LDAP_AUTH_SASL;
205 version = LDAP_VERSION3;
206 sasl_flags = LDAP_SASL_INTERACTIVE;
209 fprintf( stderr, "%s: was not compiled with SASL support\n",
211 return( EXIT_FAILURE );
213 case 'k': /* kerberos bind */
214 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
215 if( version > LDAP_VERSION2 ) {
216 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
221 if( authmethod != -1 ) {
222 fprintf( stderr, "%s: -k incompatible with previous "
223 "authentication choice\n", prog );
227 authmethod = LDAP_AUTH_KRBV4;
229 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
233 case 'K': /* kerberos bind, part one only */
234 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
235 if( version > LDAP_VERSION2 ) {
236 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
240 if( authmethod != -1 ) {
241 fprintf( stderr, "%s: incompatible with previous "
242 "authentication choice\n", prog );
246 authmethod = LDAP_AUTH_KRBV41;
248 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
249 return( EXIT_FAILURE );
253 /* enable Manage DSA IT */
254 if( version == LDAP_VERSION2 ) {
255 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
260 version = LDAP_VERSION3;
262 case 'n': /* print deletes, don't actually do them */
266 #ifdef HAVE_CYRUS_SASL
267 if( sasl_secprops != NULL ) {
268 fprintf( stderr, "%s: -O previously specified\n" );
271 if( version == LDAP_VERSION2 ) {
272 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
276 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
277 fprintf( stderr, "%s: incompatible previous "
278 "authentication choice\n", prog );
281 authmethod = LDAP_AUTH_SASL;
282 version = LDAP_VERSION3;
283 sasl_secprops = strdup( optarg );
285 fprintf( stderr, "%s: not compiled with SASL support\n",
287 return( EXIT_FAILURE );
292 fprintf( stderr, "%s: -p previously specified\n" );
295 ldapport = atoi( optarg );
298 switch( atoi(optarg) ) {
300 if( version == LDAP_VERSION3 ) {
301 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
305 version = LDAP_VERSION2;
308 if( version == LDAP_VERSION2 ) {
309 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
313 version = LDAP_VERSION3;
316 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
319 return( EXIT_FAILURE );
322 #ifdef HAVE_CYRUS_SASL
323 if( version == LDAP_VERSION2 ) {
324 fprintf( stderr, "%s: -Q incompatible with version %d\n",
328 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
329 fprintf( stderr, "%s: incompatible previous "
330 "authentication choice\n",
334 authmethod = LDAP_AUTH_SASL;
335 version = LDAP_VERSION3;
336 sasl_flags = LDAP_SASL_QUIET;
339 fprintf( stderr, "%s: not compiled with SASL support\n",
341 return( EXIT_FAILURE );
344 #ifdef HAVE_CYRUS_SASL
345 if( sasl_realm != NULL ) {
346 fprintf( stderr, "%s: -R previously specified\n" );
349 if( version == LDAP_VERSION2 ) {
350 fprintf( stderr, "%s: -R incompatible with version %d\n",
354 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
355 fprintf( stderr, "%s: incompatible previous "
356 "authentication choice\n",
360 authmethod = LDAP_AUTH_SASL;
361 version = LDAP_VERSION3;
362 sasl_realm = strdup( optarg );
364 fprintf( stderr, "%s: not compiled with SASL support\n",
366 return( EXIT_FAILURE );
370 #ifdef HAVE_CYRUS_SASL
371 if( sasl_authc_id != NULL ) {
372 fprintf( stderr, "%s: -U previously specified\n" );
375 if( version == LDAP_VERSION2 ) {
376 fprintf( stderr, "%s: -U incompatible with version %d\n",
380 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
381 fprintf( stderr, "%s: incompatible previous "
382 "authentication choice\n",
386 authmethod = LDAP_AUTH_SASL;
387 version = LDAP_VERSION3;
388 sasl_authc_id = strdup( optarg );
390 fprintf( stderr, "%s: not compiled with SASL support\n",
392 return( EXIT_FAILURE );
395 case 'v': /* verbose mode */
398 case 'w': /* password */
399 passwd.bv_val = strdup( optarg );
403 for( p = optarg; *p == '\0'; p++ ) {
407 passwd.bv_len = strlen( passwd.bv_val );
413 #ifdef HAVE_CYRUS_SASL
414 if( sasl_mech != NULL ) {
415 fprintf( stderr, "%s: -Y previously specified\n" );
418 if( version == LDAP_VERSION2 ) {
419 fprintf( stderr, "%s: -Y incompatible with version %d\n",
423 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
424 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
427 authmethod = LDAP_AUTH_SASL;
428 version = LDAP_VERSION3;
429 sasl_mech = strdup( optarg );
431 fprintf( stderr, "%s: not compiled with SASL support\n",
433 return( EXIT_FAILURE );
437 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
438 fprintf( stderr, "%s: incompatible with previous "
439 "authentication choice\n", prog );
442 authmethod = LDAP_AUTH_SIMPLE;
445 #ifdef HAVE_CYRUS_SASL
446 if( sasl_authz_id != NULL ) {
447 fprintf( stderr, "%s: -X previously specified\n" );
450 if( version == LDAP_VERSION2 ) {
451 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
455 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
456 fprintf( stderr, "%s: -X incompatible with "
457 "authentication choice\n", prog );
460 authmethod = LDAP_AUTH_SASL;
461 version = LDAP_VERSION3;
462 sasl_authz_id = strdup( optarg );
464 fprintf( stderr, "%s: not compiled with SASL support\n",
466 return( EXIT_FAILURE );
471 if( version == LDAP_VERSION2 ) {
472 fprintf( stderr, "%s: -Z incompatible with version %d\n",
476 version = LDAP_VERSION3;
479 fprintf( stderr, "%s: not compiled with TLS support\n",
481 return( EXIT_FAILURE );
487 fprintf( stderr, "%s: unrecongized option -%c\n",
493 if( argc - optind != 1 ) {
497 if (authmethod == -1) {
498 #ifdef HAVE_CYRUS_SASL
499 authmethod = LDAP_AUTH_SASL;
501 authmethod = LDAP_AUTH_SIMPLE;
505 dn = strdup( argv[optind] );
507 if( want_oldpw && oldpw == NULL ) {
508 /* prompt for old password */
510 newpw = strdup(getpassphrase("Old password: "));
511 ckoldpw = getpassphrase("Re-enter old password: ");
513 if( newpw== NULL || ckoldpw == NULL ||
514 strncmp( oldpw, ckoldpw, strlen(oldpw) ))
516 fprintf( stderr, "passwords do not match\n" );
521 if( want_newpw && newpw == NULL ) {
522 /* prompt for new password */
524 newpw = strdup(getpassphrase("New password: "));
525 cknewpw = getpassphrase("Re-enter new password: ");
527 if( newpw== NULL || cknewpw == NULL ||
528 strncmp( newpw, cknewpw, strlen(newpw) ))
530 fprintf( stderr, "passwords do not match\n" );
535 if( binddn == NULL && dn != NULL ) {
539 if( passwd.bv_val == NULL ) {
540 passwd.bv_val = oldpw;
541 passwd.bv_len = oldpw == NULL ? 0 : strlen( oldpw );
545 if (want_bindpw && passwd.bv_val == NULL ) {
546 /* handle bind password */
547 fprintf( stderr, "Bind DN: %s\n", binddn );
548 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
549 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
553 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
554 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
556 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
557 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
562 (void) SIGNAL( SIGPIPE, SIG_IGN );
565 /* connect to server */
566 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
568 fprintf( stderr, "ldap_init( %s, %d )\n",
569 ldaphost != NULL ? ldaphost : "<DEFAULT>",
572 ld = ldap_init( ldaphost, ldapport );
576 fprintf( stderr, "ldap_initialize( %s )\n",
577 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
579 (void) ldap_initialize( &ld, ldapuri );
583 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
584 rc, ldap_err2string(rc) );
589 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
590 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
592 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
593 referrals ? "on" : "off" );
599 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
601 if(rc != LDAP_OPT_SUCCESS ) {
602 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
606 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
608 ldap_perror( ld, "ldap_start_tls" );
609 return( EXIT_FAILURE );
611 fprintf( stderr, "WARNING: could not start TLS\n" );
614 if ( authmethod == LDAP_AUTH_SASL ) {
615 #ifdef HAVE_CYRUS_SASL
618 if( sasl_secprops != NULL ) {
619 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
620 (void *) sasl_secprops );
622 if( rc != LDAP_OPT_SUCCESS ) {
624 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
626 return( EXIT_FAILURE );
630 defaults = lutil_sasl_defaults( ld,
637 rc = ldap_sasl_interactive_bind_s( ld, binddn,
638 sasl_mech, NULL, NULL,
639 sasl_flags, lutil_sasl_interact, defaults );
641 if( rc != LDAP_SUCCESS ) {
642 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
643 return( EXIT_FAILURE );
646 fprintf( stderr, "%s: not compiled with SASL support\n",
648 return( EXIT_FAILURE );
652 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
654 ldap_perror( ld, "ldap_bind" );
655 return( EXIT_FAILURE );
659 if( dn != NULL || oldpw != NULL || newpw != NULL ) {
660 /* build change password control */
661 BerElement *ber = ber_alloc_t( LBER_USE_DER );
664 perror( "ber_alloc_t" );
669 ber_printf( ber, "{" /*}*/ );
672 ber_printf( ber, "ts",
673 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
677 if( oldpw != NULL ) {
678 ber_printf( ber, "ts",
679 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
683 if( newpw != NULL ) {
684 ber_printf( ber, "ts",
685 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
689 ber_printf( ber, /*{*/ "N}" );
691 rc = ber_flatten( ber, &bv );
694 perror( "ber_flatten" );
707 rc = ldap_extended_operation( ld,
708 LDAP_EXOP_X_MODIFY_PASSWD, bv,
713 if( rc != LDAP_SUCCESS ) {
714 ldap_perror( ld, "ldap_extended_operation" );
719 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
721 ldap_perror( ld, "ldappasswd: ldap_result" );
725 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
727 if( rc != LDAP_SUCCESS ) {
728 ldap_perror( ld, "ldap_parse_result" );
732 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
734 if( rc != LDAP_SUCCESS ) {
735 ldap_perror( ld, "ldap_parse_result" );
739 if( retdata != NULL ) {
742 BerElement *ber = ber_init( retdata );
745 perror( "ber_init" );
750 /* we should check the tag */
751 tag = ber_scanf( ber, "{a}", &s);
753 if( tag == LBER_ERROR ) {
754 perror( "ber_scanf" );
756 printf("New password: %s\n", s);
763 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
764 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
766 if( text && *text ) {
767 printf( "Additional info: %s\n", text );
770 if( matcheddn && *matcheddn ) {
771 printf( "Matched DN: %s\n", matcheddn );
776 for( i=0; refs[i]; i++ ) {
777 printf("Referral: %s\n", refs[i] );
783 ber_memfree( matcheddn );
784 ber_memvfree( (void **) refs );
785 ber_memfree( retoid );
786 ber_bvfree( retdata );
789 /* disconnect from server */