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;
95 struct berval *retdata;
100 while( (i = getopt( argc, argv,
101 "Aa:CD:d:EIh:np:Ss:U:vWw:X:Y:Z" )) != EOF )
104 case 'A': /* prompt for oldr password */
107 case 'a': /* old password (secret) */
108 oldpw = strdup (optarg);
113 for( p = optarg; *p == '\0'; p++ ) {
121 case 'D': /* bind distinguished name */
122 binddn = strdup (optarg);
125 case 'd': /* debugging option */
126 debug |= atoi (optarg);
129 case 'h': /* ldap host */
130 ldaphost = strdup (optarg);
133 case 'n': /* don't update entry(s) */
137 case 'p': /* ldap port */
138 ldapport = strtol( optarg, NULL, 10 );
141 case 'S': /* prompt for user password */
145 case 's': /* new password (secret) */
146 newpw = strdup (optarg);
150 for( p = optarg; *p == '\0'; p++ ) {
156 case 'v': /* verbose */
160 case 'W': /* prompt for bind password */
164 case 'w': /* bind password */
165 passwd.bv_val = strdup (optarg);
169 for( p = optarg; *p == '\0'; p++ ) {
173 passwd.bv_len = strlen( passwd.bv_val );
177 #ifdef HAVE_CYRUS_SASL
179 authmethod = LDAP_AUTH_SASL;
181 fprintf( stderr, "%s was not compiled with SASL "
182 "support\n", argv[0] );
183 return( EXIT_FAILURE );
187 #ifdef HAVE_CYRUS_SASL
189 authmethod = LDAP_AUTH_SASL;
191 fprintf( stderr, "%s was not compiled with SASL "
192 "support\n", argv[0] );
193 return( EXIT_FAILURE );
197 #ifdef HAVE_CYRUS_SASL
198 if ( strcasecmp( optarg, "any" ) &&
199 strcmp( optarg, "*" ) ) {
200 sasl_mech = strdup( optarg );
202 authmethod = LDAP_AUTH_SASL;
204 fprintf( stderr, "%s was not compiled with SASL "
205 "support\n", argv[0] );
206 return( EXIT_FAILURE );
210 #ifdef HAVE_CYRUS_SASL
211 sasl_authc_id = strdup( optarg );
212 authmethod = LDAP_AUTH_SASL;
214 fprintf( stderr, "%s was not compiled with SASL "
215 "support\n", argv[0] );
216 return( EXIT_FAILURE );
220 #ifdef HAVE_CYRUS_SASL
221 sasl_authz_id = strdup( optarg );
222 authmethod = LDAP_AUTH_SASL;
224 fprintf( stderr, "%s was not compiled with SASL "
225 "support\n", argv[0] );
226 return( EXIT_FAILURE );
233 fprintf( stderr, "%s was not compiled with TLS "
234 "support\n", argv[0] );
235 return( EXIT_FAILURE );
244 if( argc - optind != 1 ) {
248 dn = strdup( argv[optind] );
250 if( want_oldpw && oldpw == NULL ) {
251 /* prompt for old password */
253 newpw = strdup(getpassphrase("Old password: "));
254 ckoldpw = getpassphrase("Re-enter old password: ");
256 if( newpw== NULL || ckoldpw == NULL ||
257 strncmp( oldpw, ckoldpw, strlen(oldpw) ))
259 fprintf( stderr, "passwords do not match\n" );
264 if( want_newpw && newpw == NULL ) {
265 /* prompt for new password */
267 newpw = strdup(getpassphrase("New password: "));
268 cknewpw = getpassphrase("Re-enter new password: ");
270 if( newpw== NULL || cknewpw == NULL ||
271 strncmp( newpw, cknewpw, strlen(newpw) ))
273 fprintf( stderr, "passwords do not match\n" );
278 if( binddn == NULL && dn != NULL ) {
282 if( passwd.bv_val == NULL ) {
283 passwd.bv_val = oldpw;
284 passwd.bv_len = oldpw == NULL ? 0 : strlen( oldpw );
288 if (want_bindpw && passwd.bv_val == NULL ) {
289 /* handle bind password */
290 fprintf( stderr, "Bind DN: %s\n", binddn );
291 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
292 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
296 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
297 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
299 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
300 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
305 (void) SIGNAL( SIGPIPE, SIG_IGN );
308 /* connect to server */
309 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
315 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
316 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
318 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
319 referrals ? "on" : "off" );
325 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
327 if(rc != LDAP_OPT_SUCCESS ) {
328 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
332 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
334 ldap_perror( ld, "ldap_start_tls" );
335 return( EXIT_FAILURE );
337 fprintf( stderr, "WARNING: could not start TLS\n" );
340 if ( authmethod == LDAP_AUTH_SASL ) {
341 #ifdef HAVE_CYRUS_SASL
342 int minssf = 0, maxssf = 0;
344 if ( sasl_integrity > 0 )
346 if ( sasl_integrity > 1 )
348 if ( sasl_privacy > 0 )
349 maxssf = 100000; /* Something big value */
350 if ( sasl_privacy > 1 )
353 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
354 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
355 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
357 return( EXIT_FAILURE );
359 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
360 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
361 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
363 return( EXIT_FAILURE );
366 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
367 sasl_authz_id, sasl_mech,
368 passwd.bv_len ? &passwd : NULL,
371 if( rc != LDAP_SUCCESS ) {
372 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
373 return( EXIT_FAILURE );
376 fprintf( stderr, "%s was not compiled with SASL support\n",
378 return( EXIT_FAILURE );
382 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
384 ldap_perror( ld, "ldap_bind" );
385 return( EXIT_FAILURE );
389 if( dn != NULL || oldpw != NULL || newpw != NULL ) {
390 /* build change password control */
391 BerElement *ber = ber_alloc_t( LBER_USE_DER );
394 perror( "ber_alloc_t" );
399 ber_printf( ber, "{" /*}*/ );
402 ber_printf( ber, "ts",
403 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
407 if( oldpw != NULL ) {
408 ber_printf( ber, "ts",
409 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
413 if( newpw != NULL ) {
414 ber_printf( ber, "ts",
415 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
419 ber_printf( ber, /*{*/ "N}" );
421 rc = ber_flatten( ber, &bv );
424 perror( "ber_flatten" );
432 rc = ldap_extended_operation_s( ld,
433 LDAP_EXOP_X_MODIFY_PASSWD, bv,
439 if( retdata != NULL ) {
442 BerElement *ber = ber_init( retdata );
445 perror( "ber_init" );
450 /* we should check the tag */
451 tag = ber_scanf( ber, "{a}", &s);
453 if( tag == LBER_ERROR ) {
454 perror( "ber_scanf" );
456 printf("New password: %s\n", s);
463 if ( rc != LDAP_SUCCESS ) {
464 ldap_perror( ld, "ldap_extended_operation" );
469 ldap_memfree( retoid );
470 ber_bvfree( retdata );
472 /* disconnect from server */