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 " -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 update\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 authcid SASL authentication identity\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 authzid 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;
104 int id, code = LDAP_OTHER;
106 char *matcheddn = NULL, *text = NULL, **refs = NULL;
108 struct berval *retdata = NULL;
110 prog = lutil_progname( "ldappasswd", argc, argv );
112 while( (i = getopt( argc, argv, "Aa:Ss:"
113 "Cd:D:h:H:InO:p:QR:U:vw:WxX:Y:Z" )) != EOF )
116 /* Password Options */
117 case 'A': /* prompt for old password */
121 case 'a': /* old password (secret) */
122 oldpw = strdup (optarg);
127 for( p = optarg; *p != '\0'; p++ ) {
133 case 'S': /* prompt for user password */
137 case 's': /* new password (secret) */
138 newpw = strdup (optarg);
142 for( p = optarg; *p != '\0'; p++ ) {
148 /* Common Options (including options we don't use) */
153 debug |= atoi( optarg );
155 case 'D': /* bind DN */
156 if( binddn != NULL ) {
157 fprintf( stderr, "%s: -D previously specified\n", prog );
160 binddn = strdup( optarg );
162 case 'h': /* ldap host */
163 if( ldapuri != NULL ) {
164 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
167 if( ldaphost != NULL ) {
168 fprintf( stderr, "%s: -h previously specified\n", prog );
171 ldaphost = strdup( optarg );
173 case 'H': /* ldap URI */
174 if( ldaphost != NULL ) {
175 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
179 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
182 if( ldapuri != NULL ) {
183 fprintf( stderr, "%s: -H previously specified\n", prog );
186 ldapuri = strdup( optarg );
189 #ifdef HAVE_CYRUS_SASL
190 if( version == LDAP_VERSION2 ) {
191 fprintf( stderr, "%s: -I incompatible with version %d\n",
195 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
196 fprintf( stderr, "%s: incompatible previous "
197 "authentication choice\n",
201 authmethod = LDAP_AUTH_SASL;
202 version = LDAP_VERSION3;
203 sasl_flags = LDAP_SASL_INTERACTIVE;
206 fprintf( stderr, "%s: was not compiled with SASL support\n",
208 return( EXIT_FAILURE );
210 case 'k': /* kerberos bind */
211 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
212 if( version > LDAP_VERSION2 ) {
213 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
218 if( authmethod != -1 ) {
219 fprintf( stderr, "%s: -k incompatible with previous "
220 "authentication choice\n", prog );
224 authmethod = LDAP_AUTH_KRBV4;
226 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
230 case 'K': /* kerberos bind, part one only */
231 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
232 if( version > LDAP_VERSION2 ) {
233 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
237 if( authmethod != -1 ) {
238 fprintf( stderr, "%s: incompatible with previous "
239 "authentication choice\n", prog );
243 authmethod = LDAP_AUTH_KRBV41;
245 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
246 return( EXIT_FAILURE );
250 /* enable Manage DSA IT */
251 if( version == LDAP_VERSION2 ) {
252 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
257 version = LDAP_VERSION3;
259 case 'n': /* print deletes, don't actually do them */
263 #ifdef HAVE_CYRUS_SASL
264 if( sasl_secprops != NULL ) {
265 fprintf( stderr, "%s: -O previously specified\n", prog );
268 if( version == LDAP_VERSION2 ) {
269 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
273 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
274 fprintf( stderr, "%s: incompatible previous "
275 "authentication choice\n", prog );
278 authmethod = LDAP_AUTH_SASL;
279 version = LDAP_VERSION3;
280 sasl_secprops = strdup( optarg );
282 fprintf( stderr, "%s: not compiled with SASL support\n",
284 return( EXIT_FAILURE );
289 fprintf( stderr, "%s: -p previously specified\n", prog );
292 ldapport = atoi( optarg );
295 switch( atoi(optarg) ) {
297 if( version == LDAP_VERSION3 ) {
298 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
302 version = LDAP_VERSION2;
305 if( version == LDAP_VERSION2 ) {
306 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
310 version = LDAP_VERSION3;
313 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
316 return( EXIT_FAILURE );
319 #ifdef HAVE_CYRUS_SASL
320 if( version == LDAP_VERSION2 ) {
321 fprintf( stderr, "%s: -Q incompatible with version %d\n",
325 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
326 fprintf( stderr, "%s: incompatible previous "
327 "authentication choice\n",
331 authmethod = LDAP_AUTH_SASL;
332 version = LDAP_VERSION3;
333 sasl_flags = LDAP_SASL_QUIET;
336 fprintf( stderr, "%s: not compiled with SASL support\n",
338 return( EXIT_FAILURE );
341 #ifdef HAVE_CYRUS_SASL
342 if( sasl_realm != NULL ) {
343 fprintf( stderr, "%s: -R previously specified\n", prog );
346 if( version == LDAP_VERSION2 ) {
347 fprintf( stderr, "%s: -R incompatible with version %d\n",
351 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
352 fprintf( stderr, "%s: incompatible previous "
353 "authentication choice\n",
357 authmethod = LDAP_AUTH_SASL;
358 version = LDAP_VERSION3;
359 sasl_realm = strdup( optarg );
361 fprintf( stderr, "%s: not compiled with SASL support\n",
363 return( EXIT_FAILURE );
367 #ifdef HAVE_CYRUS_SASL
368 if( sasl_authc_id != NULL ) {
369 fprintf( stderr, "%s: -U previously specified\n", prog );
372 if( version == LDAP_VERSION2 ) {
373 fprintf( stderr, "%s: -U incompatible with version %d\n",
377 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
378 fprintf( stderr, "%s: incompatible previous "
379 "authentication choice\n",
383 authmethod = LDAP_AUTH_SASL;
384 version = LDAP_VERSION3;
385 sasl_authc_id = strdup( optarg );
387 fprintf( stderr, "%s: not compiled with SASL support\n",
389 return( EXIT_FAILURE );
392 case 'v': /* verbose mode */
395 case 'w': /* password */
396 passwd.bv_val = strdup( optarg );
400 for( p = optarg; *p != '\0'; p++ ) {
404 passwd.bv_len = strlen( passwd.bv_val );
410 #ifdef HAVE_CYRUS_SASL
411 if( sasl_mech != NULL ) {
412 fprintf( stderr, "%s: -Y previously specified\n", prog );
415 if( version == LDAP_VERSION2 ) {
416 fprintf( stderr, "%s: -Y incompatible with version %d\n",
420 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
421 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
424 authmethod = LDAP_AUTH_SASL;
425 version = LDAP_VERSION3;
426 sasl_mech = strdup( optarg );
428 fprintf( stderr, "%s: not compiled with SASL support\n",
430 return( EXIT_FAILURE );
434 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
435 fprintf( stderr, "%s: incompatible with previous "
436 "authentication choice\n", prog );
439 authmethod = LDAP_AUTH_SIMPLE;
442 #ifdef HAVE_CYRUS_SASL
443 if( sasl_authz_id != NULL ) {
444 fprintf( stderr, "%s: -X previously specified\n", prog );
447 if( version == LDAP_VERSION2 ) {
448 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
452 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
453 fprintf( stderr, "%s: -X incompatible with "
454 "authentication choice\n", prog );
457 authmethod = LDAP_AUTH_SASL;
458 version = LDAP_VERSION3;
459 sasl_authz_id = strdup( optarg );
461 fprintf( stderr, "%s: not compiled with SASL support\n",
463 return( EXIT_FAILURE );
468 if( version == LDAP_VERSION2 ) {
469 fprintf( stderr, "%s: -Z incompatible with version %d\n",
473 version = LDAP_VERSION3;
476 fprintf( stderr, "%s: not compiled with TLS support\n",
478 return( EXIT_FAILURE );
484 fprintf( stderr, "%s: unrecognized option -%c\n",
490 if (authmethod == -1) {
491 #ifdef HAVE_CYRUS_SASL
492 authmethod = LDAP_AUTH_SASL;
494 authmethod = LDAP_AUTH_SIMPLE;
498 if( argc - optind > 1 ) {
500 } else if ( argc - optind == 1 ) {
501 user = strdup( argv[optind] );
506 if( want_oldpw && oldpw == NULL ) {
507 /* prompt for old password */
509 oldpw = strdup(getpassphrase("Old password: "));
510 ckoldpw = getpassphrase("Re-enter old password: ");
512 if( oldpw== NULL || ckoldpw == NULL ||
513 strcmp( oldpw, ckoldpw ))
515 fprintf( stderr, "passwords do not match\n" );
520 if( want_newpw && newpw == NULL ) {
521 /* prompt for new password */
523 newpw = strdup(getpassphrase("New password: "));
524 cknewpw = getpassphrase("Re-enter new password: ");
526 if( newpw== NULL || cknewpw == NULL ||
527 strcmp( newpw, cknewpw ))
529 fprintf( stderr, "passwords do not match\n" );
534 if (want_bindpw && passwd.bv_val == NULL ) {
535 /* handle bind password */
536 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
537 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
541 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
542 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
544 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
545 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
550 (void) SIGNAL( SIGPIPE, SIG_IGN );
553 /* connect to server */
554 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
556 fprintf( stderr, "ldap_init( %s, %d )\n",
557 ldaphost != NULL ? ldaphost : "<DEFAULT>",
561 ld = ldap_init( ldaphost, ldapport );
563 perror("ldapsearch: ldap_init");
569 fprintf( stderr, "ldap_initialize( %s )\n",
570 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
573 rc = ldap_initialize( &ld, ldapuri );
574 if( rc != LDAP_SUCCESS ) {
575 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
576 rc, ldap_err2string(rc) );
582 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
583 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
585 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
586 referrals ? "on" : "off" );
591 version = LDAP_VERSION3;
592 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
594 if(rc != LDAP_OPT_SUCCESS ) {
595 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
599 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
600 ldap_perror( ld, "ldap_start_tls" );
602 return( EXIT_FAILURE );
606 if ( authmethod == LDAP_AUTH_SASL ) {
607 #ifdef HAVE_CYRUS_SASL
610 if( sasl_secprops != NULL ) {
611 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
612 (void *) sasl_secprops );
614 if( rc != LDAP_OPT_SUCCESS ) {
616 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
618 return( EXIT_FAILURE );
622 defaults = lutil_sasl_defaults( ld,
629 rc = ldap_sasl_interactive_bind_s( ld, binddn,
630 sasl_mech, NULL, NULL,
631 sasl_flags, lutil_sasl_interact, defaults );
633 if( rc != LDAP_SUCCESS ) {
634 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
635 return( EXIT_FAILURE );
638 fprintf( stderr, "%s: not compiled with SASL support\n",
640 return( EXIT_FAILURE );
644 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
646 ldap_perror( ld, "ldap_bind" );
647 return( EXIT_FAILURE );
651 if( user != NULL || oldpw != NULL || newpw != NULL ) {
652 /* build change password control */
653 BerElement *ber = ber_alloc_t( LBER_USE_DER );
656 perror( "ber_alloc_t" );
661 ber_printf( ber, "{" /*}*/ );
664 ber_printf( ber, "ts",
665 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
669 if( oldpw != NULL ) {
670 ber_printf( ber, "ts",
671 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
675 if( newpw != NULL ) {
676 ber_printf( ber, "ts",
677 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
681 ber_printf( ber, /*{*/ "N}" );
683 rc = ber_flatten( ber, &bv );
686 perror( "ber_flatten" );
699 rc = ldap_extended_operation( ld,
700 LDAP_EXOP_MODIFY_PASSWD, bv,
705 if( rc != LDAP_SUCCESS ) {
706 ldap_perror( ld, "ldap_extended_operation" );
711 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
713 ldap_perror( ld, "ldappasswd: ldap_result" );
717 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
719 if( rc != LDAP_SUCCESS ) {
720 ldap_perror( ld, "ldap_parse_result" );
724 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
726 if( rc != LDAP_SUCCESS ) {
727 ldap_perror( ld, "ldap_parse_result" );
731 if( retdata != NULL ) {
734 BerElement *ber = ber_init( retdata );
737 perror( "ber_init" );
742 /* we should check the tag */
743 tag = ber_scanf( ber, "{a}", &s);
745 if( tag == LBER_ERROR ) {
746 perror( "ber_scanf" );
748 printf("New password: %s\n", s);
755 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
756 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
758 if( text && *text ) {
759 printf( "Additional info: %s\n", text );
762 if( matcheddn && *matcheddn ) {
763 printf( "Matched DN: %s\n", matcheddn );
768 for( i=0; refs[i]; i++ ) {
769 printf("Referral: %s\n", refs[i] );
775 ber_memfree( matcheddn );
776 ber_memvfree( (void **) refs );
777 ber_memfree( retoid );
778 ber_bvfree( retdata );
781 /* disconnect from server */
784 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;