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 ) {
217 if( cvalue != NULL ) {
218 fprintf( stderr, "manageDSAit: no control value expected" );
223 manageDSAit = 1 + crit;
227 } else if ( strcasecmp( control, "noop" ) == 0 ) {
228 if( cvalue != NULL ) {
229 fprintf( stderr, "noop: no control value expected" );
239 fprintf( stderr, "Invalid general control name: %s\n", control );
243 case 'h': /* ldap host */
244 if( ldapuri != NULL ) {
245 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
248 if( ldaphost != NULL ) {
249 fprintf( stderr, "%s: -h previously specified\n", prog );
252 ldaphost = strdup( optarg );
254 case 'H': /* ldap URI */
255 if( ldaphost != NULL ) {
256 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
260 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
263 if( ldapuri != NULL ) {
264 fprintf( stderr, "%s: -H previously specified\n", prog );
267 ldapuri = strdup( optarg );
270 #ifdef HAVE_CYRUS_SASL
271 if( version == LDAP_VERSION2 ) {
272 fprintf( stderr, "%s: -I incompatible with version %d\n",
276 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
277 fprintf( stderr, "%s: incompatible previous "
278 "authentication choice\n",
282 authmethod = LDAP_AUTH_SASL;
283 version = LDAP_VERSION3;
284 sasl_flags = LDAP_SASL_INTERACTIVE;
287 fprintf( stderr, "%s: was not compiled with SASL support\n",
289 return( EXIT_FAILURE );
291 case 'k': /* kerberos bind */
292 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
293 if( version > LDAP_VERSION2 ) {
294 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
299 if( authmethod != -1 ) {
300 fprintf( stderr, "%s: -k incompatible with previous "
301 "authentication choice\n", prog );
305 authmethod = LDAP_AUTH_KRBV4;
307 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
311 case 'K': /* kerberos bind, part one only */
312 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
313 if( version > LDAP_VERSION2 ) {
314 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
318 if( authmethod != -1 ) {
319 fprintf( stderr, "%s: incompatible with previous "
320 "authentication choice\n", prog );
324 authmethod = LDAP_AUTH_KRBV41;
326 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
327 return( EXIT_FAILURE );
331 /* enable Manage DSA IT */
332 if( version == LDAP_VERSION2 ) {
333 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
338 version = LDAP_VERSION3;
340 case 'n': /* print deletes, don't actually do them */
344 #ifdef HAVE_CYRUS_SASL
345 if( sasl_secprops != NULL ) {
346 fprintf( stderr, "%s: -O previously specified\n", prog );
349 if( version == LDAP_VERSION2 ) {
350 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
354 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
355 fprintf( stderr, "%s: incompatible previous "
356 "authentication choice\n", prog );
359 authmethod = LDAP_AUTH_SASL;
360 version = LDAP_VERSION3;
361 sasl_secprops = strdup( optarg );
363 fprintf( stderr, "%s: not compiled with SASL support\n",
365 return( EXIT_FAILURE );
370 fprintf( stderr, "%s: -p previously specified\n", prog );
373 ldapport = atoi( optarg );
376 switch( atoi(optarg) ) {
378 if( version == LDAP_VERSION3 ) {
379 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
383 version = LDAP_VERSION2;
386 if( version == LDAP_VERSION2 ) {
387 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
391 version = LDAP_VERSION3;
394 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
397 return( EXIT_FAILURE );
400 #ifdef HAVE_CYRUS_SASL
401 if( version == LDAP_VERSION2 ) {
402 fprintf( stderr, "%s: -Q incompatible with version %d\n",
406 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
407 fprintf( stderr, "%s: incompatible previous "
408 "authentication choice\n",
412 authmethod = LDAP_AUTH_SASL;
413 version = LDAP_VERSION3;
414 sasl_flags = LDAP_SASL_QUIET;
417 fprintf( stderr, "%s: not compiled with SASL support\n",
419 return( EXIT_FAILURE );
422 #ifdef HAVE_CYRUS_SASL
423 if( sasl_realm != NULL ) {
424 fprintf( stderr, "%s: -R previously specified\n", prog );
427 if( version == LDAP_VERSION2 ) {
428 fprintf( stderr, "%s: -R incompatible with version %d\n",
432 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
433 fprintf( stderr, "%s: incompatible previous "
434 "authentication choice\n",
438 authmethod = LDAP_AUTH_SASL;
439 version = LDAP_VERSION3;
440 sasl_realm = strdup( optarg );
442 fprintf( stderr, "%s: not compiled with SASL support\n",
444 return( EXIT_FAILURE );
448 #ifdef HAVE_CYRUS_SASL
449 if( sasl_authc_id != NULL ) {
450 fprintf( stderr, "%s: -U previously specified\n", prog );
453 if( version == LDAP_VERSION2 ) {
454 fprintf( stderr, "%s: -U incompatible with version %d\n",
458 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
459 fprintf( stderr, "%s: incompatible previous "
460 "authentication choice\n",
464 authmethod = LDAP_AUTH_SASL;
465 version = LDAP_VERSION3;
466 sasl_authc_id = strdup( optarg );
468 fprintf( stderr, "%s: not compiled with SASL support\n",
470 return( EXIT_FAILURE );
473 case 'v': /* verbose mode */
476 case 'w': /* password */
477 passwd.bv_val = strdup( optarg );
481 for( p = optarg; *p != '\0'; p++ ) {
485 passwd.bv_len = strlen( passwd.bv_val );
491 #ifdef HAVE_CYRUS_SASL
492 if( sasl_mech != NULL ) {
493 fprintf( stderr, "%s: -Y previously specified\n", prog );
496 if( version == LDAP_VERSION2 ) {
497 fprintf( stderr, "%s: -Y incompatible with version %d\n",
501 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
502 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
505 authmethod = LDAP_AUTH_SASL;
506 version = LDAP_VERSION3;
507 sasl_mech = strdup( optarg );
509 fprintf( stderr, "%s: not compiled with SASL support\n",
511 return( EXIT_FAILURE );
515 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
516 fprintf( stderr, "%s: incompatible with previous "
517 "authentication choice\n", prog );
520 authmethod = LDAP_AUTH_SIMPLE;
523 #ifdef HAVE_CYRUS_SASL
524 if( sasl_authz_id != NULL ) {
525 fprintf( stderr, "%s: -X previously specified\n", prog );
528 if( version == LDAP_VERSION2 ) {
529 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
533 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
534 fprintf( stderr, "%s: -X incompatible with "
535 "authentication choice\n", prog );
538 authmethod = LDAP_AUTH_SASL;
539 version = LDAP_VERSION3;
540 sasl_authz_id = strdup( optarg );
542 fprintf( stderr, "%s: not compiled with SASL support\n",
544 return( EXIT_FAILURE );
549 if( version == LDAP_VERSION2 ) {
550 fprintf( stderr, "%s: -Z incompatible with version %d\n",
554 version = LDAP_VERSION3;
557 fprintf( stderr, "%s: not compiled with TLS support\n",
559 return( EXIT_FAILURE );
565 fprintf( stderr, "%s: unrecognized option -%c\n",
571 if (authmethod == -1) {
572 #ifdef HAVE_CYRUS_SASL
573 authmethod = LDAP_AUTH_SASL;
575 authmethod = LDAP_AUTH_SIMPLE;
579 if( argc - optind > 1 ) {
581 } else if ( argc - optind == 1 ) {
582 user = strdup( argv[optind] );
587 if( want_oldpw && oldpw == NULL ) {
588 /* prompt for old password */
590 oldpw = strdup(getpassphrase("Old password: "));
591 ckoldpw = getpassphrase("Re-enter old password: ");
593 if( oldpw== NULL || ckoldpw == NULL ||
594 strcmp( oldpw, ckoldpw ))
596 fprintf( stderr, "passwords do not match\n" );
601 if( want_newpw && newpw == NULL ) {
602 /* prompt for new password */
604 newpw = strdup(getpassphrase("New password: "));
605 cknewpw = getpassphrase("Re-enter new password: ");
607 if( newpw== NULL || cknewpw == NULL ||
608 strcmp( newpw, cknewpw ))
610 fprintf( stderr, "passwords do not match\n" );
615 if (want_bindpw && passwd.bv_val == NULL ) {
616 /* handle bind password */
617 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
618 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
622 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
623 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
625 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
626 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
631 (void) SIGNAL( SIGPIPE, SIG_IGN );
634 /* connect to server */
635 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
637 fprintf( stderr, "ldap_init( %s, %d )\n",
638 ldaphost != NULL ? ldaphost : "<DEFAULT>",
642 ld = ldap_init( ldaphost, ldapport );
644 perror("ldapsearch: ldap_init");
650 fprintf( stderr, "ldap_initialize( %s )\n",
651 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
654 rc = ldap_initialize( &ld, ldapuri );
655 if( rc != LDAP_SUCCESS ) {
656 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
657 rc, ldap_err2string(rc) );
663 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
664 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
666 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
667 referrals ? "on" : "off" );
672 version = LDAP_VERSION3;
673 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
675 if(rc != LDAP_OPT_SUCCESS ) {
676 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
680 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
681 ldap_perror( ld, "ldap_start_tls" );
683 return( EXIT_FAILURE );
687 if ( authmethod == LDAP_AUTH_SASL ) {
688 #ifdef HAVE_CYRUS_SASL
691 if( sasl_secprops != NULL ) {
692 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
693 (void *) sasl_secprops );
695 if( rc != LDAP_OPT_SUCCESS ) {
697 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
699 return( EXIT_FAILURE );
703 defaults = lutil_sasl_defaults( ld,
710 rc = ldap_sasl_interactive_bind_s( ld, binddn,
711 sasl_mech, NULL, NULL,
712 sasl_flags, lutil_sasl_interact, defaults );
714 if( rc != LDAP_SUCCESS ) {
715 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
716 return( EXIT_FAILURE );
719 fprintf( stderr, "%s: not compiled with SASL support\n",
721 return( EXIT_FAILURE );
725 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
727 ldap_perror( ld, "ldap_bind" );
728 return( EXIT_FAILURE );
732 if ( manageDSAit || noop ) {
735 LDAPControl *ctrls[3];
740 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
741 c1.ldctl_value.bv_val = NULL;
742 c1.ldctl_value.bv_len = 0;
743 c1.ldctl_iscritical = manageDSAit > 1;
750 c2.ldctl_oid = LDAP_CONTROL_NOOP;
751 c2.ldctl_value.bv_val = NULL;
752 c2.ldctl_value.bv_len = 0;
753 c2.ldctl_iscritical = noop > 1;
756 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
758 if( err != LDAP_OPT_SUCCESS ) {
759 fprintf( stderr, "Could not set %scontrols\n",
760 (c1.ldctl_iscritical || c2.ldctl_iscritical)
761 ? "critical " : "" );
762 if ( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
768 if( user != NULL || oldpw != NULL || newpw != NULL ) {
769 /* build change password control */
770 BerElement *ber = ber_alloc_t( LBER_USE_DER );
773 perror( "ber_alloc_t" );
778 ber_printf( ber, "{" /*}*/ );
781 ber_printf( ber, "ts",
782 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
786 if( oldpw != NULL ) {
787 ber_printf( ber, "ts",
788 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
792 if( newpw != NULL ) {
793 ber_printf( ber, "ts",
794 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
798 ber_printf( ber, /*{*/ "N}" );
800 rc = ber_flatten( ber, &bv );
803 perror( "ber_flatten" );
816 rc = ldap_extended_operation( ld,
817 LDAP_EXOP_MODIFY_PASSWD, bv,
822 if( rc != LDAP_SUCCESS ) {
823 ldap_perror( ld, "ldap_extended_operation" );
828 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
830 ldap_perror( ld, "ldappasswd: ldap_result" );
834 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
836 if( rc != LDAP_SUCCESS ) {
837 ldap_perror( ld, "ldap_parse_result" );
841 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
843 if( rc != LDAP_SUCCESS ) {
844 ldap_perror( ld, "ldap_parse_result" );
848 if( retdata != NULL ) {
851 BerElement *ber = ber_init( retdata );
854 perror( "ber_init" );
859 /* we should check the tag */
860 tag = ber_scanf( ber, "{a}", &s);
862 if( tag == LBER_ERROR ) {
863 perror( "ber_scanf" );
865 printf("New password: %s\n", s);
872 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
873 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
875 if( text && *text ) {
876 printf( "Additional info: %s\n", text );
879 if( matcheddn && *matcheddn ) {
880 printf( "Matched DN: %s\n", matcheddn );
885 for( i=0; refs[i]; i++ ) {
886 printf("Referral: %s\n", refs[i] );
892 ber_memfree( matcheddn );
893 ber_memvfree( (void **) refs );
894 ber_memfree( retoid );
895 ber_bvfree( retdata );
898 /* disconnect from server */
901 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;