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 the password of an LDAP entry\n\n"
32 "usage: %s [options] dn\n"
33 " dn: the DN of the entry whose password must be changed\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 " -n\t\tmake no modifications\n"
46 " -O secprops\tSASL security properties\n"
47 " -p port\t\tport on LDAP server\n"
48 " -U user\t\tSASL authentication identity (username)\n"
49 " -v\t\tverbose mode\n"
50 " -w passwd\tbind password (for simple authentication)\n"
51 " -W\t\tprompt for bind password\n"
52 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
53 " -Y mech\t\tSASL mechanism\n"
54 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
61 main( int argc, char *argv[] )
65 char *ldaphost = NULL;
70 struct berval passwd = { 0, NULL };
84 #ifdef HAVE_CYRUS_SASL
85 char *sasl_authc_id = NULL;
86 char *sasl_authz_id = NULL;
87 char *sasl_mech = NULL;
88 char *sasl_secprops = NULL;
93 struct berval *bv = NULL;
97 char *matcheddn = NULL, *text = NULL, **refs = NULL;
99 struct berval *retdata = NULL;
101 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
106 while( (i = getopt( argc, argv,
107 "Aa:Ss:" "Cd:D:h:nO:p:U:vw:WxX:Y:Z" )) != EOF )
110 /* Password Options */
111 case 'A': /* prompt for old password */
115 case 'a': /* old password (secret) */
116 oldpw = strdup (optarg);
121 for( p = optarg; *p == '\0'; p++ ) {
127 case 'S': /* prompt for user password */
131 case 's': /* new password (secret) */
132 newpw = strdup (optarg);
136 for( p = optarg; *p == '\0'; p++ ) {
147 case 'D': /* bind distinguished name */
148 binddn = strdup (optarg);
151 case 'd': /* debugging option */
152 debug |= atoi (optarg);
155 case 'h': /* ldap host */
156 ldaphost = strdup (optarg);
159 case 'n': /* don't update entry(s) */
163 case 'p': /* ldap port */
164 ldapport = strtol( optarg, NULL, 10 );
167 case 'v': /* verbose */
171 case 'W': /* prompt for bind password */
175 case 'w': /* bind password */
176 passwd.bv_val = strdup (optarg);
180 for( p = optarg; *p == '\0'; p++ ) {
184 passwd.bv_len = strlen( passwd.bv_val );
188 #ifdef HAVE_CYRUS_SASL
189 sasl_secprops = strdup( optarg );
190 authmethod = LDAP_AUTH_SASL;
192 fprintf( stderr, "%s was not compiled with SASL support\n",
194 return( EXIT_FAILURE );
198 #ifdef HAVE_CYRUS_SASL
199 if ( strcasecmp( optarg, "any" ) &&
200 strcmp( optarg, "*" ) ) {
201 sasl_mech = strdup( optarg );
203 authmethod = LDAP_AUTH_SASL;
205 fprintf( stderr, "%s was not compiled with SASL "
206 "support\n", argv[0] );
207 return( EXIT_FAILURE );
211 #ifdef HAVE_CYRUS_SASL
212 sasl_authc_id = strdup( optarg );
213 authmethod = LDAP_AUTH_SASL;
215 fprintf( stderr, "%s was not compiled with SASL "
216 "support\n", argv[0] );
217 return( EXIT_FAILURE );
221 #ifdef HAVE_CYRUS_SASL
222 sasl_authz_id = strdup( optarg );
223 authmethod = LDAP_AUTH_SASL;
225 fprintf( stderr, "%s was not compiled with SASL "
226 "support\n", argv[0] );
227 return( EXIT_FAILURE );
234 fprintf( stderr, "%s was not compiled with TLS "
235 "support\n", argv[0] );
236 return( EXIT_FAILURE );
241 fprintf( stderr, "%s: unrecongized option -%c\n",
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 if( sasl_secprops != NULL ) {
346 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
347 (void *) sasl_secprops );
349 if( rc != LDAP_OPT_SUCCESS ) {
351 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
353 return( EXIT_FAILURE );
357 rc = ldap_sasl_interactive_bind_s( ld, binddn,
358 sasl_mech, NULL, NULL, lutil_sasl_interact );
360 if( rc != LDAP_SUCCESS ) {
361 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
362 return( EXIT_FAILURE );
365 fprintf( stderr, "%s was not compiled with SASL support\n",
367 return( EXIT_FAILURE );
371 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
373 ldap_perror( ld, "ldap_bind" );
374 return( EXIT_FAILURE );
378 if( dn != NULL || oldpw != NULL || newpw != NULL ) {
379 /* build change password control */
380 BerElement *ber = ber_alloc_t( LBER_USE_DER );
383 perror( "ber_alloc_t" );
388 ber_printf( ber, "{" /*}*/ );
391 ber_printf( ber, "ts",
392 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
396 if( oldpw != NULL ) {
397 ber_printf( ber, "ts",
398 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
402 if( newpw != NULL ) {
403 ber_printf( ber, "ts",
404 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
408 ber_printf( ber, /*{*/ "N}" );
410 rc = ber_flatten( ber, &bv );
413 perror( "ber_flatten" );
426 rc = ldap_extended_operation( ld,
427 LDAP_EXOP_X_MODIFY_PASSWD, bv,
432 if( rc != LDAP_SUCCESS ) {
433 ldap_perror( ld, "ldap_extended_operation" );
438 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
440 ldap_perror( ld, "ldappasswd: ldap_result" );
444 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
446 if( rc != LDAP_SUCCESS ) {
447 ldap_perror( ld, "ldap_parse_result" );
451 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
453 if( rc != LDAP_SUCCESS ) {
454 ldap_perror( ld, "ldap_parse_result" );
458 if( retdata != NULL ) {
461 BerElement *ber = ber_init( retdata );
464 perror( "ber_init" );
469 /* we should check the tag */
470 tag = ber_scanf( ber, "{a}", &s);
472 if( tag == LBER_ERROR ) {
473 perror( "ber_scanf" );
475 printf("New password: %s\n", s);
482 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
483 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
485 if( text && *text ) {
486 printf( "Additional info: %s\n", text );
489 if( matcheddn && *matcheddn ) {
490 printf( "Matched DN: %s\n", matcheddn );
495 for( i=0; refs[i]; i++ ) {
496 printf("Referral: %s\n", refs[i] );
502 ber_memfree( matcheddn );
503 ber_memvfree( (void **) refs );
504 ber_memfree( retoid );
505 ber_bvfree( retdata );
508 /* disconnect from server */