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/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 autentication identity, commonly 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 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
44 " [!]manageDSAit (alternate form, see -M)\n"
46 " -f file read operations from `file'\n"
47 " -h host LDAP server(s)\n"
48 " -H URI LDAP Uniform Resource Indentifier(s)\n"
49 " -I use SASL Interactive mode\n"
50 " -n show what would be done but don't actually update\n"
51 " -O props SASL security properties\n"
52 " -p port port on LDAP server\n"
53 " -Q use SASL Quiet mode\n"
54 " -R realm SASL realm\n"
55 " -U authcid SASL authentication identity\n"
56 " -v run in verbose mode (diagnostics to standard output)\n"
57 " -w passwd bind passwd (for simple authentication)\n"
58 " -W prompt for bind passwd\n"
59 " -x Simple authentication\n"
60 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
61 " -Y mech SASL mechanism\n"
62 " -Z Start TLS request (-ZZ to require successful response)\n"
69 main( int argc, char *argv[] )
73 char *ldaphost = NULL;
79 struct berval passwd = { 0, NULL };
96 char *control, *cvalue;
97 #ifdef HAVE_CYRUS_SASL
98 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
99 char *sasl_realm = NULL;
100 char *sasl_authc_id = NULL;
101 char *sasl_authz_id = NULL;
102 char *sasl_mech = NULL;
103 char *sasl_secprops = NULL;
108 struct berval *bv = NULL;
110 int id, code = LDAP_OTHER;
112 char *matcheddn = NULL, *text = NULL, **refs = NULL;
114 struct berval *retdata = NULL;
116 prog = lutil_progname( "ldappasswd", argc, argv );
118 while( (i = getopt( argc, argv, "Aa:Ss:"
119 "Cd:D:e:h:H:InO:p:QR:U:vw:WxX:Y:Z" )) != EOF )
122 /* Password Options */
123 case 'A': /* prompt for old password */
127 case 'a': /* old password (secret) */
128 oldpw = strdup (optarg);
133 for( p = optarg; *p != '\0'; p++ ) {
139 case 'E': /* passwd controls */
140 if( version == LDAP_VERSION2 ) {
141 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
146 /* should be extended to support comma separated list of
147 * [!]key[=value] parameters, e.g. -E !foo,bar=567
152 if( optarg[0] == '!' ) {
157 control = strdup( optarg );
158 if ( (cvalue = strchr( control, '=' )) != NULL ) {
161 fprintf( stderr, "Invalid passwd control name: %s\n", control );
164 case 'S': /* prompt for user password */
168 case 's': /* new password (secret) */
169 newpw = strdup (optarg);
173 for( p = optarg; *p != '\0'; p++ ) {
179 /* Common Options (including options we don't use) */
184 debug |= atoi( optarg );
186 case 'D': /* bind DN */
187 if( binddn != NULL ) {
188 fprintf( stderr, "%s: -D previously specified\n", prog );
191 binddn = strdup( optarg );
193 case 'e': /* general controls */
194 if( version == LDAP_VERSION2 ) {
195 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
200 /* should be extended to support comma separated list of
201 * [!]key[=value] parameters, e.g. -e !foo,bar=567
206 if( optarg[0] == '!' ) {
211 control = strdup( optarg );
212 if ( (cvalue = strchr( control, '=' )) != NULL ) {
216 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
218 fprintf( stderr, "manageDSAit control previously specified");
221 if( cvalue != NULL ) {
222 fprintf( stderr, "manageDSAit: no control value expected" );
227 manageDSAit = 1 + crit;
231 } else if ( strcasecmp( control, "noop" ) == 0 ) {
232 if( cvalue != NULL ) {
233 fprintf( stderr, "noop: no control value expected" );
243 fprintf( stderr, "Invalid general control name: %s\n", control );
247 case 'h': /* ldap host */
248 if( ldapuri != NULL ) {
249 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
252 if( ldaphost != NULL ) {
253 fprintf( stderr, "%s: -h previously specified\n", prog );
256 ldaphost = strdup( optarg );
258 case 'H': /* ldap URI */
259 if( ldaphost != NULL ) {
260 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
264 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
267 if( ldapuri != NULL ) {
268 fprintf( stderr, "%s: -H previously specified\n", prog );
271 ldapuri = strdup( optarg );
274 #ifdef HAVE_CYRUS_SASL
275 if( version == LDAP_VERSION2 ) {
276 fprintf( stderr, "%s: -I incompatible with version %d\n",
280 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
281 fprintf( stderr, "%s: incompatible previous "
282 "authentication choice\n",
286 authmethod = LDAP_AUTH_SASL;
287 version = LDAP_VERSION3;
288 sasl_flags = LDAP_SASL_INTERACTIVE;
291 fprintf( stderr, "%s: was not compiled with SASL support\n",
293 return( EXIT_FAILURE );
295 case 'k': /* kerberos bind */
296 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
297 if( version > LDAP_VERSION2 ) {
298 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
303 if( authmethod != -1 ) {
304 fprintf( stderr, "%s: -k incompatible with previous "
305 "authentication choice\n", prog );
309 authmethod = LDAP_AUTH_KRBV4;
311 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
315 case 'K': /* kerberos bind, part one only */
316 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
317 if( version > LDAP_VERSION2 ) {
318 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
322 if( authmethod != -1 ) {
323 fprintf( stderr, "%s: incompatible with previous "
324 "authentication choice\n", prog );
328 authmethod = LDAP_AUTH_KRBV41;
330 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
331 return( EXIT_FAILURE );
335 /* enable Manage DSA IT */
336 if( version == LDAP_VERSION2 ) {
337 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
342 version = LDAP_VERSION3;
344 case 'n': /* print deletes, don't actually do them */
348 #ifdef HAVE_CYRUS_SASL
349 if( sasl_secprops != NULL ) {
350 fprintf( stderr, "%s: -O previously specified\n", prog );
353 if( version == LDAP_VERSION2 ) {
354 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
358 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
359 fprintf( stderr, "%s: incompatible previous "
360 "authentication choice\n", prog );
363 authmethod = LDAP_AUTH_SASL;
364 version = LDAP_VERSION3;
365 sasl_secprops = strdup( optarg );
367 fprintf( stderr, "%s: not compiled with SASL support\n",
369 return( EXIT_FAILURE );
374 fprintf( stderr, "%s: -p previously specified\n", prog );
377 ldapport = atoi( optarg );
380 switch( atoi(optarg) ) {
382 if( version == LDAP_VERSION3 ) {
383 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
387 version = LDAP_VERSION2;
390 if( version == LDAP_VERSION2 ) {
391 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
395 version = LDAP_VERSION3;
398 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
401 return( EXIT_FAILURE );
404 #ifdef HAVE_CYRUS_SASL
405 if( version == LDAP_VERSION2 ) {
406 fprintf( stderr, "%s: -Q incompatible with version %d\n",
410 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
411 fprintf( stderr, "%s: incompatible previous "
412 "authentication choice\n",
416 authmethod = LDAP_AUTH_SASL;
417 version = LDAP_VERSION3;
418 sasl_flags = LDAP_SASL_QUIET;
421 fprintf( stderr, "%s: not compiled with SASL support\n",
423 return( EXIT_FAILURE );
426 #ifdef HAVE_CYRUS_SASL
427 if( sasl_realm != NULL ) {
428 fprintf( stderr, "%s: -R previously specified\n", prog );
431 if( version == LDAP_VERSION2 ) {
432 fprintf( stderr, "%s: -R incompatible with version %d\n",
436 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
437 fprintf( stderr, "%s: incompatible previous "
438 "authentication choice\n",
442 authmethod = LDAP_AUTH_SASL;
443 version = LDAP_VERSION3;
444 sasl_realm = strdup( optarg );
446 fprintf( stderr, "%s: not compiled with SASL support\n",
448 return( EXIT_FAILURE );
452 #ifdef HAVE_CYRUS_SASL
453 if( sasl_authc_id != NULL ) {
454 fprintf( stderr, "%s: -U previously specified\n", prog );
457 if( version == LDAP_VERSION2 ) {
458 fprintf( stderr, "%s: -U incompatible with version %d\n",
462 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
463 fprintf( stderr, "%s: incompatible previous "
464 "authentication choice\n",
468 authmethod = LDAP_AUTH_SASL;
469 version = LDAP_VERSION3;
470 sasl_authc_id = strdup( optarg );
472 fprintf( stderr, "%s: not compiled with SASL support\n",
474 return( EXIT_FAILURE );
477 case 'v': /* verbose mode */
480 case 'w': /* password */
481 passwd.bv_val = strdup( optarg );
485 for( p = optarg; *p != '\0'; p++ ) {
489 passwd.bv_len = strlen( passwd.bv_val );
495 #ifdef HAVE_CYRUS_SASL
496 if( sasl_mech != NULL ) {
497 fprintf( stderr, "%s: -Y previously specified\n", prog );
500 if( version == LDAP_VERSION2 ) {
501 fprintf( stderr, "%s: -Y incompatible with version %d\n",
505 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
506 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
509 authmethod = LDAP_AUTH_SASL;
510 version = LDAP_VERSION3;
511 sasl_mech = strdup( optarg );
513 fprintf( stderr, "%s: not compiled with SASL support\n",
515 return( EXIT_FAILURE );
519 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
520 fprintf( stderr, "%s: incompatible with previous "
521 "authentication choice\n", prog );
524 authmethod = LDAP_AUTH_SIMPLE;
527 #ifdef HAVE_CYRUS_SASL
528 if( sasl_authz_id != NULL ) {
529 fprintf( stderr, "%s: -X previously specified\n", prog );
532 if( version == LDAP_VERSION2 ) {
533 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
537 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
538 fprintf( stderr, "%s: -X incompatible with "
539 "authentication choice\n", prog );
542 authmethod = LDAP_AUTH_SASL;
543 version = LDAP_VERSION3;
544 sasl_authz_id = strdup( optarg );
546 fprintf( stderr, "%s: not compiled with SASL support\n",
548 return( EXIT_FAILURE );
553 if( version == LDAP_VERSION2 ) {
554 fprintf( stderr, "%s: -Z incompatible with version %d\n",
558 version = LDAP_VERSION3;
561 fprintf( stderr, "%s: not compiled with TLS support\n",
563 return( EXIT_FAILURE );
569 fprintf( stderr, "%s: unrecognized option -%c\n",
575 if (authmethod == -1) {
576 #ifdef HAVE_CYRUS_SASL
577 authmethod = LDAP_AUTH_SASL;
579 authmethod = LDAP_AUTH_SIMPLE;
583 if( argc - optind > 1 ) {
585 } else if ( argc - optind == 1 ) {
586 user = strdup( argv[optind] );
591 if( want_oldpw && oldpw == NULL ) {
592 /* prompt for old password */
594 oldpw = strdup(getpassphrase("Old password: "));
595 ckoldpw = getpassphrase("Re-enter old password: ");
597 if( oldpw== NULL || ckoldpw == NULL ||
598 strcmp( oldpw, ckoldpw ))
600 fprintf( stderr, "passwords do not match\n" );
605 if( want_newpw && newpw == NULL ) {
606 /* prompt for new password */
608 newpw = strdup(getpassphrase("New password: "));
609 cknewpw = getpassphrase("Re-enter new password: ");
611 if( newpw== NULL || cknewpw == NULL ||
612 strcmp( newpw, cknewpw ))
614 fprintf( stderr, "passwords do not match\n" );
619 if (want_bindpw && passwd.bv_val == NULL ) {
620 /* handle bind password */
621 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
622 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
626 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
627 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
629 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
630 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
635 (void) SIGNAL( SIGPIPE, SIG_IGN );
638 /* connect to server */
639 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
641 fprintf( stderr, "ldap_init( %s, %d )\n",
642 ldaphost != NULL ? ldaphost : "<DEFAULT>",
646 ld = ldap_init( ldaphost, ldapport );
648 perror("ldapsearch: ldap_init");
654 fprintf( stderr, "ldap_initialize( %s )\n",
655 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
658 rc = ldap_initialize( &ld, ldapuri );
659 if( rc != LDAP_SUCCESS ) {
660 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
661 rc, ldap_err2string(rc) );
667 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
668 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
670 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
671 referrals ? "on" : "off" );
676 version = LDAP_VERSION3;
677 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
679 if(rc != LDAP_OPT_SUCCESS ) {
680 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
684 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
685 ldap_perror( ld, "ldap_start_tls" );
687 return( EXIT_FAILURE );
691 if ( authmethod == LDAP_AUTH_SASL ) {
692 #ifdef HAVE_CYRUS_SASL
695 if( sasl_secprops != NULL ) {
696 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
697 (void *) sasl_secprops );
699 if( rc != LDAP_OPT_SUCCESS ) {
701 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
703 return( EXIT_FAILURE );
707 defaults = lutil_sasl_defaults( ld,
714 rc = ldap_sasl_interactive_bind_s( ld, binddn,
715 sasl_mech, NULL, NULL,
716 sasl_flags, lutil_sasl_interact, defaults );
718 if( rc != LDAP_SUCCESS ) {
719 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
720 return( EXIT_FAILURE );
723 fprintf( stderr, "%s: not compiled with SASL support\n",
725 return( EXIT_FAILURE );
729 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
731 ldap_perror( ld, "ldap_bind" );
732 return( EXIT_FAILURE );
736 if ( manageDSAit || noop ) {
739 LDAPControl *ctrls[3];
744 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
745 c1.ldctl_value.bv_val = NULL;
746 c1.ldctl_value.bv_len = 0;
747 c1.ldctl_iscritical = manageDSAit > 1;
754 c2.ldctl_oid = LDAP_CONTROL_NOOP;
755 c2.ldctl_value.bv_val = NULL;
756 c2.ldctl_value.bv_len = 0;
757 c2.ldctl_iscritical = noop > 1;
760 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
762 if( err != LDAP_OPT_SUCCESS ) {
763 fprintf( stderr, "Could not set %scontrols\n",
764 (c1.ldctl_iscritical || c2.ldctl_iscritical)
765 ? "critical " : "" );
766 if ( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
772 if( user != NULL || oldpw != NULL || newpw != NULL ) {
773 /* build change password control */
774 BerElement *ber = ber_alloc_t( LBER_USE_DER );
777 perror( "ber_alloc_t" );
782 ber_printf( ber, "{" /*}*/ );
785 ber_printf( ber, "ts",
786 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
790 if( oldpw != NULL ) {
791 ber_printf( ber, "ts",
792 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
796 if( newpw != NULL ) {
797 ber_printf( ber, "ts",
798 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
802 ber_printf( ber, /*{*/ "N}" );
804 rc = ber_flatten( ber, &bv );
807 perror( "ber_flatten" );
820 rc = ldap_extended_operation( ld,
821 LDAP_EXOP_MODIFY_PASSWD, bv,
826 if( rc != LDAP_SUCCESS ) {
827 ldap_perror( ld, "ldap_extended_operation" );
832 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
834 ldap_perror( ld, "ldappasswd: ldap_result" );
838 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
840 if( rc != LDAP_SUCCESS ) {
841 ldap_perror( ld, "ldap_parse_result" );
845 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
847 if( rc != LDAP_SUCCESS ) {
848 ldap_perror( ld, "ldap_parse_result" );
852 if( retdata != NULL ) {
855 BerElement *ber = ber_init( retdata );
858 perror( "ber_init" );
863 /* we should check the tag */
864 tag = ber_scanf( ber, "{a}", &s);
866 if( tag == LBER_ERROR ) {
867 perror( "ber_scanf" );
869 printf("New password: %s\n", s);
876 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
877 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
879 if( text && *text ) {
880 printf( "Additional info: %s\n", text );
883 if( matcheddn && *matcheddn ) {
884 printf( "Matched DN: %s\n", matcheddn );
889 for( i=0; refs[i]; i++ ) {
890 printf("Referral: %s\n", refs[i] );
896 ber_memfree( matcheddn );
897 ber_memvfree( (void **) refs );
898 ber_memfree( retoid );
899 ber_bvfree( retdata );
902 /* disconnect from server */
905 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;