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 "ldap_defaults.h"
24 static int verbose = 0;
30 "Change the password of an LDAP entry\n\n"
31 "usage: %s [options] dn\n"
32 " dn: the DN of the entry whose password must be changed\n"
34 " -a secret\told password\n"
35 " -A\t\tprompt for old password\n"
36 " -d level\tdebugging level\n"
37 " -C\t\tchase referrals\n"
38 " -D binddn\tbind DN\n"
39 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
40 " -h host\t\tLDAP server (default: localhost)\n"
41 " -I\t\trequest SASL integrity checking (-II to make it\n"
43 " -n\t\tmake no modifications\n"
44 " -p port\t\tport on LDAP server\n"
45 " -S\t\tprompt for new password\n"
46 " -s secret\tnew password\n"
47 " -U user\t\tSASL authentication identity (username)\n"
48 " -v\t\tverbose mode\n"
49 " -w passwd\tbind password (for simple authentication)\n"
50 " -W\t\tprompt for bind password\n"
51 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
52 " -Y mech\t\tSASL mechanism\n"
53 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
60 main( int argc, char *argv[] )
63 char *ldaphost = NULL;
68 struct berval passwd = { 0, NULL};
81 int authmethod = LDAP_AUTH_SIMPLE;
82 #ifdef HAVE_CYRUS_SASL
83 char *sasl_authc_id = NULL;
84 char *sasl_authz_id = NULL;
85 char *sasl_mech = NULL;
86 int sasl_integrity = 0;
92 struct berval *bv = NULL;
96 char *matcheddn = NULL, *text = NULL, **refs = NULL;
98 struct berval *retdata = NULL;
103 while( (i = getopt( argc, argv,
104 "Aa:CD:d:EIh:np:Ss:U:vWw:X:Y:Z" )) != EOF )
107 case 'A': /* prompt for oldr password */
110 case 'a': /* old password (secret) */
111 oldpw = strdup (optarg);
116 for( p = optarg; *p == '\0'; p++ ) {
124 case 'D': /* bind distinguished name */
125 binddn = strdup (optarg);
128 case 'd': /* debugging option */
129 debug |= atoi (optarg);
132 case 'h': /* ldap host */
133 ldaphost = strdup (optarg);
136 case 'n': /* don't update entry(s) */
140 case 'p': /* ldap port */
141 ldapport = strtol( optarg, NULL, 10 );
144 case 'S': /* prompt for user password */
148 case 's': /* new password (secret) */
149 newpw = strdup (optarg);
153 for( p = optarg; *p == '\0'; p++ ) {
159 case 'v': /* verbose */
163 case 'W': /* prompt for bind password */
167 case 'w': /* bind password */
168 passwd.bv_val = strdup (optarg);
172 for( p = optarg; *p == '\0'; p++ ) {
176 passwd.bv_len = strlen( passwd.bv_val );
180 #ifdef HAVE_CYRUS_SASL
182 authmethod = LDAP_AUTH_SASL;
184 fprintf( stderr, "%s was not compiled with SASL "
185 "support\n", argv[0] );
186 return( EXIT_FAILURE );
190 #ifdef HAVE_CYRUS_SASL
192 authmethod = LDAP_AUTH_SASL;
194 fprintf( stderr, "%s was not compiled with SASL "
195 "support\n", argv[0] );
196 return( EXIT_FAILURE );
200 #ifdef HAVE_CYRUS_SASL
201 if ( strcasecmp( optarg, "any" ) &&
202 strcmp( optarg, "*" ) ) {
203 sasl_mech = strdup( optarg );
205 authmethod = LDAP_AUTH_SASL;
207 fprintf( stderr, "%s was not compiled with SASL "
208 "support\n", argv[0] );
209 return( EXIT_FAILURE );
213 #ifdef HAVE_CYRUS_SASL
214 sasl_authc_id = strdup( optarg );
215 authmethod = LDAP_AUTH_SASL;
217 fprintf( stderr, "%s was not compiled with SASL "
218 "support\n", argv[0] );
219 return( EXIT_FAILURE );
223 #ifdef HAVE_CYRUS_SASL
224 sasl_authz_id = strdup( optarg );
225 authmethod = LDAP_AUTH_SASL;
227 fprintf( stderr, "%s was not compiled with SASL "
228 "support\n", argv[0] );
229 return( EXIT_FAILURE );
236 fprintf( stderr, "%s was not compiled with TLS "
237 "support\n", argv[0] );
238 return( EXIT_FAILURE );
247 if( argc - optind != 1 ) {
251 dn = strdup( argv[optind] );
253 if( want_oldpw && oldpw == NULL ) {
254 /* prompt for old password */
256 newpw = strdup(getpassphrase("Old password: "));
257 ckoldpw = getpassphrase("Re-enter old password: ");
259 if( newpw== NULL || ckoldpw == NULL ||
260 strncmp( oldpw, ckoldpw, strlen(oldpw) ))
262 fprintf( stderr, "passwords do not match\n" );
267 if( want_newpw && newpw == NULL ) {
268 /* prompt for new password */
270 newpw = strdup(getpassphrase("New password: "));
271 cknewpw = getpassphrase("Re-enter new password: ");
273 if( newpw== NULL || cknewpw == NULL ||
274 strncmp( newpw, cknewpw, strlen(newpw) ))
276 fprintf( stderr, "passwords do not match\n" );
281 if( binddn == NULL && dn != NULL ) {
285 if( passwd.bv_val == NULL ) {
286 passwd.bv_val = oldpw;
287 passwd.bv_len = oldpw == NULL ? 0 : strlen( oldpw );
291 if (want_bindpw && passwd.bv_val == NULL ) {
292 /* handle bind password */
293 fprintf( stderr, "Bind DN: %s\n", binddn );
294 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
295 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
299 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
300 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
302 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
303 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
308 (void) SIGNAL( SIGPIPE, SIG_IGN );
311 /* connect to server */
312 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
318 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
319 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
321 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
322 referrals ? "on" : "off" );
328 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
330 if(rc != LDAP_OPT_SUCCESS ) {
331 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
335 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
337 ldap_perror( ld, "ldap_start_tls" );
338 return( EXIT_FAILURE );
340 fprintf( stderr, "WARNING: could not start TLS\n" );
343 if ( authmethod == LDAP_AUTH_SASL ) {
344 #ifdef HAVE_CYRUS_SASL
345 int minssf = 0, maxssf = 0;
347 if ( sasl_integrity > 0 )
349 if ( sasl_integrity > 1 )
351 if ( sasl_privacy > 0 )
352 maxssf = 100000; /* Something big value */
353 if ( sasl_privacy > 1 )
356 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
357 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
358 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
360 return( EXIT_FAILURE );
362 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
363 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
364 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
366 return( EXIT_FAILURE );
369 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
370 sasl_authz_id, sasl_mech,
371 passwd.bv_len ? &passwd : NULL,
374 if( rc != LDAP_SUCCESS ) {
375 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
376 return( EXIT_FAILURE );
379 fprintf( stderr, "%s was not compiled with SASL support\n",
381 return( EXIT_FAILURE );
385 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
387 ldap_perror( ld, "ldap_bind" );
388 return( EXIT_FAILURE );
392 if( dn != NULL || oldpw != NULL || newpw != NULL ) {
393 /* build change password control */
394 BerElement *ber = ber_alloc_t( LBER_USE_DER );
397 perror( "ber_alloc_t" );
402 ber_printf( ber, "{" /*}*/ );
405 ber_printf( ber, "ts",
406 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
410 if( oldpw != NULL ) {
411 ber_printf( ber, "ts",
412 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
416 if( newpw != NULL ) {
417 ber_printf( ber, "ts",
418 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
422 ber_printf( ber, /*{*/ "N}" );
424 rc = ber_flatten( ber, &bv );
427 perror( "ber_flatten" );
440 rc = ldap_extended_operation( ld,
441 LDAP_EXOP_X_MODIFY_PASSWD, bv,
446 if( rc != LDAP_SUCCESS ) {
447 ldap_perror( ld, "ldap_extended_operation" );
452 rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res );
453 if ( rc != LDAP_SUCCESS ) {
454 ldap_perror( ld, "ldap_result" );
458 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
460 if( rc != LDAP_SUCCESS ) {
461 ldap_perror( ld, "ldap_parse_result" );
465 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
467 if( rc != LDAP_SUCCESS ) {
468 ldap_perror( ld, "ldap_parse_result" );
472 if( retdata != NULL ) {
475 BerElement *ber = ber_init( retdata );
478 perror( "ber_init" );
483 /* we should check the tag */
484 tag = ber_scanf( ber, "{a}", &s);
486 if( tag == LBER_ERROR ) {
487 perror( "ber_scanf" );
489 printf("New password: %s\n", s);
496 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
497 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
499 if( text && *text ) {
500 printf( "Additional info: %s\n", text );
503 if( matcheddn && *matcheddn ) {
504 printf( "Matched DN: %s\n", matcheddn );
509 for( i=0; refs[i]; i++ ) {
510 printf("Referral: %s\n", refs[i] );
516 ber_memfree( matcheddn );
517 ber_memvfree( refs );
518 ber_memfree( retoid );
519 ber_bvfree( retdata );
522 /* disconnect from server */