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\told password\n"
36 " -A\t\tprompt for old password\n"
37 " -s secret\tnew password\n"
38 " -S\t\tprompt for new password\n"
41 " -d level\tdebugging level\n"
42 " -C\t\tchase referrals\n"
43 " -D binddn\tbind DN\n"
44 " -h host\t\tLDAP server (default: localhost)\n"
45 " -I\t\tuse SASL Interactive mode\n"
46 " -n\t\tmake no modifications\n"
47 " -O secprops\tSASL security properties\n"
48 " -p port\t\tport on LDAP server\n"
49 " -Q\t\tuse SASL Quiet mode\n"
50 " -R realm\tSASL realm\n"
51 " -U user\t\tSASL authentication identity (username)\n"
52 " -v\t\tverbose mode\n"
53 " -w passwd\tbind password (for simple authentication)\n"
54 " -W\t\tprompt for bind password\n"
55 " -x\t\tSimple authentication\n"
56 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
57 " -Y mech\t\tSASL mechanism\n"
58 " -Z\t\tissue 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 = LUTIL_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 );
170 case 'k': /* kerberos bind */
171 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
172 if( version > LDAP_VERSION2 ) {
173 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
178 if( authmethod != -1 ) {
179 fprintf( stderr, "%s: -k incompatible with previous "
180 "authentication choice\n", prog );
184 authmethod = LDAP_AUTH_KRBV4;
186 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
190 case 'K': /* kerberos bind, part one only */
191 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
192 if( version > LDAP_VERSION2 ) {
193 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
197 if( authmethod != -1 ) {
198 fprintf( stderr, "%s: incompatible with previous "
199 "authentication choice\n", prog );
203 authmethod = LDAP_AUTH_KRBV41;
205 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
206 return( EXIT_FAILURE );
210 /* enable Manage DSA IT */
211 if( version == LDAP_VERSION2 ) {
212 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
217 version = LDAP_VERSION3;
219 case 'n': /* print deletes, don't actually do them */
223 #ifdef HAVE_CYRUS_SASL
224 if( sasl_secprops != NULL ) {
225 fprintf( stderr, "%s: -O previously specified\n" );
228 if( version == LDAP_VERSION2 ) {
229 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
233 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
234 fprintf( stderr, "%s: incompatible previous "
235 "authentication choice\n", prog );
238 authmethod = LDAP_AUTH_SASL;
239 version = LDAP_VERSION3;
240 sasl_secprops = strdup( optarg );
242 fprintf( stderr, "%s: not compiled with SASL support\n",
244 return( EXIT_FAILURE );
249 fprintf( stderr, "%s: -p previously specified\n" );
252 ldapport = atoi( optarg );
255 switch( atoi(optarg) ) {
257 if( version == LDAP_VERSION3 ) {
258 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
262 version = LDAP_VERSION2;
265 if( version == LDAP_VERSION2 ) {
266 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
270 version = LDAP_VERSION3;
273 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
276 return( EXIT_FAILURE );
279 #ifdef HAVE_CYRUS_SASL
280 if( version == LDAP_VERSION2 ) {
281 fprintf( stderr, "%s: -Q incompatible with version %d\n",
285 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
286 fprintf( stderr, "%s: incompatible previous "
287 "authentication choice\n",
291 authmethod = LDAP_AUTH_SASL;
292 version = LDAP_VERSION3;
293 sasl_flags = LUTIL_SASL_QUIET;
295 fprintf( stderr, "%s: was not compiled with SASL support\n",
297 return( EXIT_FAILURE );
300 #ifdef HAVE_CYRUS_SASL
301 if( sasl_realm != NULL ) {
302 fprintf( stderr, "%s: -R previously specified\n" );
305 if( version == LDAP_VERSION2 ) {
306 fprintf( stderr, "%s: -R incompatible with version %d\n",
310 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
311 fprintf( stderr, "%s: incompatible previous "
312 "authentication choice\n",
316 authmethod = LDAP_AUTH_SASL;
317 version = LDAP_VERSION3;
318 sasl_realm = strdup( optarg );
320 fprintf( stderr, "%s: was not compiled with SASL support\n",
322 return( EXIT_FAILURE );
326 #ifdef HAVE_CYRUS_SASL
327 if( sasl_authc_id != NULL ) {
328 fprintf( stderr, "%s: -U previously specified\n" );
331 if( version == LDAP_VERSION2 ) {
332 fprintf( stderr, "%s: -U incompatible with version %d\n",
336 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
337 fprintf( stderr, "%s: incompatible previous "
338 "authentication choice\n",
342 authmethod = LDAP_AUTH_SASL;
343 version = LDAP_VERSION3;
344 sasl_authc_id = strdup( optarg );
346 fprintf( stderr, "%s: was not compiled with SASL support\n",
348 return( EXIT_FAILURE );
351 case 'v': /* verbose mode */
354 case 'w': /* password */
355 passwd.bv_val = strdup( optarg );
359 for( p = optarg; *p == '\0'; p++ ) {
363 passwd.bv_len = strlen( passwd.bv_val );
369 #ifdef HAVE_CYRUS_SASL
370 if( sasl_mech != NULL ) {
371 fprintf( stderr, "%s: -Y previously specified\n" );
374 if( version == LDAP_VERSION2 ) {
375 fprintf( stderr, "%s: -Y incompatible with version %d\n",
379 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
380 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
383 authmethod = LDAP_AUTH_SASL;
384 version = LDAP_VERSION3;
385 sasl_mech = strdup( optarg );
387 fprintf( stderr, "%s: was not compiled with SASL support\n",
389 return( EXIT_FAILURE );
393 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
394 fprintf( stderr, "%s: incompatible with previous "
395 "authentication choice\n", prog );
398 authmethod = LDAP_AUTH_SIMPLE;
401 #ifdef HAVE_CYRUS_SASL
402 if( sasl_authz_id != NULL ) {
403 fprintf( stderr, "%s: -X previously specified\n" );
406 if( version == LDAP_VERSION2 ) {
407 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
411 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
412 fprintf( stderr, "%s: -X incompatible with "
413 "authentication choice\n", prog );
416 authmethod = LDAP_AUTH_SASL;
417 version = LDAP_VERSION3;
418 sasl_authz_id = strdup( optarg );
420 fprintf( stderr, "%s: not compiled with SASL support\n",
422 return( EXIT_FAILURE );
427 if( version == LDAP_VERSION2 ) {
428 fprintf( stderr, "%s -Z incompatible with version %d\n",
432 version = LDAP_VERSION3;
435 fprintf( stderr, "%s: not compiled with TLS support\n",
437 return( EXIT_FAILURE );
443 fprintf( stderr, "%s: unrecongized option -%c\n",
449 if( argc - optind != 1 ) {
453 if (authmethod == -1) {
454 #ifdef HAVE_CYRUS_SASL
455 authmethod = LDAP_AUTH_SASL;
457 authmethod = LDAP_AUTH_SIMPLE;
461 dn = strdup( argv[optind] );
463 if( want_oldpw && oldpw == NULL ) {
464 /* prompt for old password */
466 newpw = strdup(getpassphrase("Old password: "));
467 ckoldpw = getpassphrase("Re-enter old password: ");
469 if( newpw== NULL || ckoldpw == NULL ||
470 strncmp( oldpw, ckoldpw, strlen(oldpw) ))
472 fprintf( stderr, "passwords do not match\n" );
477 if( want_newpw && newpw == NULL ) {
478 /* prompt for new password */
480 newpw = strdup(getpassphrase("New password: "));
481 cknewpw = getpassphrase("Re-enter new password: ");
483 if( newpw== NULL || cknewpw == NULL ||
484 strncmp( newpw, cknewpw, strlen(newpw) ))
486 fprintf( stderr, "passwords do not match\n" );
491 if( binddn == NULL && dn != NULL ) {
495 if( passwd.bv_val == NULL ) {
496 passwd.bv_val = oldpw;
497 passwd.bv_len = oldpw == NULL ? 0 : strlen( oldpw );
501 if (want_bindpw && passwd.bv_val == NULL ) {
502 /* handle bind password */
503 fprintf( stderr, "Bind DN: %s\n", binddn );
504 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
505 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
509 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
510 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
512 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
513 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
518 (void) SIGNAL( SIGPIPE, SIG_IGN );
521 /* connect to server */
522 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
528 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
529 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
531 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
532 referrals ? "on" : "off" );
538 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
540 if(rc != LDAP_OPT_SUCCESS ) {
541 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
545 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
547 ldap_perror( ld, "ldap_start_tls" );
548 return( EXIT_FAILURE );
550 fprintf( stderr, "WARNING: could not start TLS\n" );
553 if ( authmethod == LDAP_AUTH_SASL ) {
554 #ifdef HAVE_CYRUS_SASL
557 if( sasl_secprops != NULL ) {
558 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
559 (void *) sasl_secprops );
561 if( rc != LDAP_OPT_SUCCESS ) {
563 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
565 return( EXIT_FAILURE );
569 defaults = lutil_sasl_defaults( ld, sasl_flags,
576 rc = ldap_sasl_interactive_bind_s( ld, binddn,
577 sasl_mech, NULL, NULL,
578 lutil_sasl_interact, defaults );
580 if( rc != LDAP_SUCCESS ) {
581 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
582 return( EXIT_FAILURE );
585 fprintf( stderr, "%s was not compiled with SASL support\n",
587 return( EXIT_FAILURE );
591 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
593 ldap_perror( ld, "ldap_bind" );
594 return( EXIT_FAILURE );
598 if( dn != NULL || oldpw != NULL || newpw != NULL ) {
599 /* build change password control */
600 BerElement *ber = ber_alloc_t( LBER_USE_DER );
603 perror( "ber_alloc_t" );
608 ber_printf( ber, "{" /*}*/ );
611 ber_printf( ber, "ts",
612 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
616 if( oldpw != NULL ) {
617 ber_printf( ber, "ts",
618 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
622 if( newpw != NULL ) {
623 ber_printf( ber, "ts",
624 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
628 ber_printf( ber, /*{*/ "N}" );
630 rc = ber_flatten( ber, &bv );
633 perror( "ber_flatten" );
646 rc = ldap_extended_operation( ld,
647 LDAP_EXOP_X_MODIFY_PASSWD, bv,
652 if( rc != LDAP_SUCCESS ) {
653 ldap_perror( ld, "ldap_extended_operation" );
658 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
660 ldap_perror( ld, "ldappasswd: ldap_result" );
664 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
666 if( rc != LDAP_SUCCESS ) {
667 ldap_perror( ld, "ldap_parse_result" );
671 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
673 if( rc != LDAP_SUCCESS ) {
674 ldap_perror( ld, "ldap_parse_result" );
678 if( retdata != NULL ) {
681 BerElement *ber = ber_init( retdata );
684 perror( "ber_init" );
689 /* we should check the tag */
690 tag = ber_scanf( ber, "{a}", &s);
692 if( tag == LBER_ERROR ) {
693 perror( "ber_scanf" );
695 printf("New password: %s\n", s);
702 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
703 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
705 if( text && *text ) {
706 printf( "Additional info: %s\n", text );
709 if( matcheddn && *matcheddn ) {
710 printf( "Matched DN: %s\n", matcheddn );
715 for( i=0; refs[i]; i++ ) {
716 printf("Referral: %s\n", refs[i] );
722 ber_memfree( matcheddn );
723 ber_memvfree( (void **) refs );
724 ber_memfree( retoid );
725 ber_bvfree( retdata );
728 /* disconnect from server */