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\n"
45 " -I use SASL Interactive mode\n"
46 " -n show what would be done but don't actually search\n"
47 " -O props SASL security properties\n"
48 " -p port port on LDAP server\n"
49 " -Q use SASL Quiet mode\n"
50 " -R realm SASL realm\n"
51 " -U user SASL authentication identity (username)\n"
52 " -v run in verbose mode (diagnostics to standard output)\n"
53 " -w passwd bind passwd (for simple authentication)\n"
54 " -W prompt for bind passwd\n"
55 " -x Simple authentication\n"
56 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
57 " -Y mech SASL mechanism\n"
58 " -Z Start TLS request (-ZZ to require successful response)\n"
65 main( int argc, char *argv[] )
69 char *ldaphost = NULL;
74 struct berval passwd = { 0, NULL };
89 #ifdef HAVE_CYRUS_SASL
90 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
91 char *sasl_realm = NULL;
92 char *sasl_authc_id = NULL;
93 char *sasl_authz_id = NULL;
94 char *sasl_mech = NULL;
95 char *sasl_secprops = NULL;
100 struct berval *bv = NULL;
104 char *matcheddn = NULL, *text = NULL, **refs = NULL;
106 struct berval *retdata = NULL;
108 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
113 while( (i = getopt( argc, argv,
114 "Aa:Ss:" "Cd:D:h:InO:p:QRU:vw:WxX:Y:Z" )) != EOF )
117 /* Password Options */
118 case 'A': /* prompt for old password */
122 case 'a': /* old password (secret) */
123 oldpw = strdup (optarg);
128 for( p = optarg; *p == '\0'; p++ ) {
134 case 'S': /* prompt for user password */
138 case 's': /* new password (secret) */
139 newpw = strdup (optarg);
143 for( p = optarg; *p == '\0'; p++ ) {
149 /* Common Options (including options we don't use) */
154 debug |= atoi( optarg );
156 case 'D': /* bind DN */
157 if( binddn != NULL ) {
158 fprintf( stderr, "%s: -D previously specified\n" );
161 binddn = strdup( optarg );
163 case 'h': /* ldap host */
164 if( ldaphost != NULL ) {
165 fprintf( stderr, "%s: -h previously specified\n" );
168 ldaphost = strdup( optarg );
171 #ifdef HAVE_CYRUS_SASL
172 if( version == LDAP_VERSION2 ) {
173 fprintf( stderr, "%s: -I incompatible with version %d\n",
177 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
178 fprintf( stderr, "%s: incompatible previous "
179 "authentication choice\n",
183 authmethod = LDAP_AUTH_SASL;
184 version = LDAP_VERSION3;
185 sasl_flags = LDAP_SASL_INTERACTIVE;
188 fprintf( stderr, "%s: was not compiled with SASL support\n",
190 return( EXIT_FAILURE );
192 case 'k': /* kerberos bind */
193 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
194 if( version > LDAP_VERSION2 ) {
195 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
200 if( authmethod != -1 ) {
201 fprintf( stderr, "%s: -k incompatible with previous "
202 "authentication choice\n", prog );
206 authmethod = LDAP_AUTH_KRBV4;
208 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
212 case 'K': /* kerberos bind, part one only */
213 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
214 if( version > LDAP_VERSION2 ) {
215 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
219 if( authmethod != -1 ) {
220 fprintf( stderr, "%s: incompatible with previous "
221 "authentication choice\n", prog );
225 authmethod = LDAP_AUTH_KRBV41;
227 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
228 return( EXIT_FAILURE );
232 /* enable Manage DSA IT */
233 if( version == LDAP_VERSION2 ) {
234 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
239 version = LDAP_VERSION3;
241 case 'n': /* print deletes, don't actually do them */
245 #ifdef HAVE_CYRUS_SASL
246 if( sasl_secprops != NULL ) {
247 fprintf( stderr, "%s: -O previously specified\n" );
250 if( version == LDAP_VERSION2 ) {
251 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
255 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
256 fprintf( stderr, "%s: incompatible previous "
257 "authentication choice\n", prog );
260 authmethod = LDAP_AUTH_SASL;
261 version = LDAP_VERSION3;
262 sasl_secprops = strdup( optarg );
264 fprintf( stderr, "%s: not compiled with SASL support\n",
266 return( EXIT_FAILURE );
271 fprintf( stderr, "%s: -p previously specified\n" );
274 ldapport = atoi( optarg );
277 switch( atoi(optarg) ) {
279 if( version == LDAP_VERSION3 ) {
280 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
284 version = LDAP_VERSION2;
287 if( version == LDAP_VERSION2 ) {
288 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
292 version = LDAP_VERSION3;
295 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
298 return( EXIT_FAILURE );
301 #ifdef HAVE_CYRUS_SASL
302 if( version == LDAP_VERSION2 ) {
303 fprintf( stderr, "%s: -Q incompatible with version %d\n",
307 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
308 fprintf( stderr, "%s: incompatible previous "
309 "authentication choice\n",
313 authmethod = LDAP_AUTH_SASL;
314 version = LDAP_VERSION3;
315 sasl_flags = LDAP_SASL_QUIET;
318 fprintf( stderr, "%s: not compiled with SASL support\n",
320 return( EXIT_FAILURE );
323 #ifdef HAVE_CYRUS_SASL
324 if( sasl_realm != NULL ) {
325 fprintf( stderr, "%s: -R previously specified\n" );
328 if( version == LDAP_VERSION2 ) {
329 fprintf( stderr, "%s: -R incompatible with version %d\n",
333 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
334 fprintf( stderr, "%s: incompatible previous "
335 "authentication choice\n",
339 authmethod = LDAP_AUTH_SASL;
340 version = LDAP_VERSION3;
341 sasl_realm = strdup( optarg );
343 fprintf( stderr, "%s: not compiled with SASL support\n",
345 return( EXIT_FAILURE );
349 #ifdef HAVE_CYRUS_SASL
350 if( sasl_authc_id != NULL ) {
351 fprintf( stderr, "%s: -U previously specified\n" );
354 if( version == LDAP_VERSION2 ) {
355 fprintf( stderr, "%s: -U incompatible with version %d\n",
359 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
360 fprintf( stderr, "%s: incompatible previous "
361 "authentication choice\n",
365 authmethod = LDAP_AUTH_SASL;
366 version = LDAP_VERSION3;
367 sasl_authc_id = strdup( optarg );
369 fprintf( stderr, "%s: not compiled with SASL support\n",
371 return( EXIT_FAILURE );
374 case 'v': /* verbose mode */
377 case 'w': /* password */
378 passwd.bv_val = strdup( optarg );
382 for( p = optarg; *p == '\0'; p++ ) {
386 passwd.bv_len = strlen( passwd.bv_val );
392 #ifdef HAVE_CYRUS_SASL
393 if( sasl_mech != NULL ) {
394 fprintf( stderr, "%s: -Y previously specified\n" );
397 if( version == LDAP_VERSION2 ) {
398 fprintf( stderr, "%s: -Y incompatible with version %d\n",
402 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
403 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
406 authmethod = LDAP_AUTH_SASL;
407 version = LDAP_VERSION3;
408 sasl_mech = strdup( optarg );
410 fprintf( stderr, "%s: not compiled with SASL support\n",
412 return( EXIT_FAILURE );
416 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
417 fprintf( stderr, "%s: incompatible with previous "
418 "authentication choice\n", prog );
421 authmethod = LDAP_AUTH_SIMPLE;
424 #ifdef HAVE_CYRUS_SASL
425 if( sasl_authz_id != NULL ) {
426 fprintf( stderr, "%s: -X previously specified\n" );
429 if( version == LDAP_VERSION2 ) {
430 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
434 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
435 fprintf( stderr, "%s: -X incompatible with "
436 "authentication choice\n", prog );
439 authmethod = LDAP_AUTH_SASL;
440 version = LDAP_VERSION3;
441 sasl_authz_id = strdup( optarg );
443 fprintf( stderr, "%s: not compiled with SASL support\n",
445 return( EXIT_FAILURE );
450 if( version == LDAP_VERSION2 ) {
451 fprintf( stderr, "%s: -Z incompatible with version %d\n",
455 version = LDAP_VERSION3;
458 fprintf( stderr, "%s: not compiled with TLS support\n",
460 return( EXIT_FAILURE );
466 fprintf( stderr, "%s: unrecongized option -%c\n",
472 if( argc - optind != 1 ) {
476 if (authmethod == -1) {
477 #ifdef HAVE_CYRUS_SASL
478 authmethod = LDAP_AUTH_SASL;
480 authmethod = LDAP_AUTH_SIMPLE;
484 dn = strdup( argv[optind] );
486 if( want_oldpw && oldpw == NULL ) {
487 /* prompt for old password */
489 newpw = strdup(getpassphrase("Old password: "));
490 ckoldpw = getpassphrase("Re-enter old password: ");
492 if( newpw== NULL || ckoldpw == NULL ||
493 strncmp( oldpw, ckoldpw, strlen(oldpw) ))
495 fprintf( stderr, "passwords do not match\n" );
500 if( want_newpw && newpw == NULL ) {
501 /* prompt for new password */
503 newpw = strdup(getpassphrase("New password: "));
504 cknewpw = getpassphrase("Re-enter new password: ");
506 if( newpw== NULL || cknewpw == NULL ||
507 strncmp( newpw, cknewpw, strlen(newpw) ))
509 fprintf( stderr, "passwords do not match\n" );
514 if( binddn == NULL && dn != NULL ) {
518 if( passwd.bv_val == NULL ) {
519 passwd.bv_val = oldpw;
520 passwd.bv_len = oldpw == NULL ? 0 : strlen( oldpw );
524 if (want_bindpw && passwd.bv_val == NULL ) {
525 /* handle bind password */
526 fprintf( stderr, "Bind DN: %s\n", binddn );
527 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
528 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
532 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
533 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
535 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
536 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
541 (void) SIGNAL( SIGPIPE, SIG_IGN );
544 /* connect to server */
545 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
551 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
552 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
554 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
555 referrals ? "on" : "off" );
561 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
563 if(rc != LDAP_OPT_SUCCESS ) {
564 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
568 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
570 ldap_perror( ld, "ldap_start_tls" );
571 return( EXIT_FAILURE );
573 fprintf( stderr, "WARNING: could not start TLS\n" );
576 if ( authmethod == LDAP_AUTH_SASL ) {
577 #ifdef HAVE_CYRUS_SASL
580 if( sasl_secprops != NULL ) {
581 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
582 (void *) sasl_secprops );
584 if( rc != LDAP_OPT_SUCCESS ) {
586 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
588 return( EXIT_FAILURE );
592 defaults = lutil_sasl_defaults( ld,
599 rc = ldap_sasl_interactive_bind_s( ld, binddn,
600 sasl_mech, NULL, NULL,
601 sasl_flags, lutil_sasl_interact, defaults );
603 if( rc != LDAP_SUCCESS ) {
604 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
605 return( EXIT_FAILURE );
608 fprintf( stderr, "%s: not compiled with SASL support\n",
610 return( EXIT_FAILURE );
614 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
616 ldap_perror( ld, "ldap_bind" );
617 return( EXIT_FAILURE );
621 if( dn != NULL || oldpw != NULL || newpw != NULL ) {
622 /* build change password control */
623 BerElement *ber = ber_alloc_t( LBER_USE_DER );
626 perror( "ber_alloc_t" );
631 ber_printf( ber, "{" /*}*/ );
634 ber_printf( ber, "ts",
635 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
639 if( oldpw != NULL ) {
640 ber_printf( ber, "ts",
641 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
645 if( newpw != NULL ) {
646 ber_printf( ber, "ts",
647 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
651 ber_printf( ber, /*{*/ "N}" );
653 rc = ber_flatten( ber, &bv );
656 perror( "ber_flatten" );
669 rc = ldap_extended_operation( ld,
670 LDAP_EXOP_X_MODIFY_PASSWD, bv,
675 if( rc != LDAP_SUCCESS ) {
676 ldap_perror( ld, "ldap_extended_operation" );
681 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
683 ldap_perror( ld, "ldappasswd: ldap_result" );
687 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
689 if( rc != LDAP_SUCCESS ) {
690 ldap_perror( ld, "ldap_parse_result" );
694 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
696 if( rc != LDAP_SUCCESS ) {
697 ldap_perror( ld, "ldap_parse_result" );
701 if( retdata != NULL ) {
704 BerElement *ber = ber_init( retdata );
707 perror( "ber_init" );
712 /* we should check the tag */
713 tag = ber_scanf( ber, "{a}", &s);
715 if( tag == LBER_ERROR ) {
716 perror( "ber_scanf" );
718 printf("New password: %s\n", s);
725 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
726 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
728 if( text && *text ) {
729 printf( "Additional info: %s\n", text );
732 if( matcheddn && *matcheddn ) {
733 printf( "Matched DN: %s\n", matcheddn );
738 for( i=0; refs[i]; i++ ) {
739 printf("Referral: %s\n", refs[i] );
745 ber_memfree( matcheddn );
746 ber_memvfree( (void **) refs );
747 ber_memfree( retoid );
748 ber_bvfree( retdata );
751 /* disconnect from server */