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[] )
64 char *ldaphost = NULL;
69 struct berval passwd = { 0, NULL };
83 #ifdef HAVE_CYRUS_SASL
84 char *sasl_authc_id = NULL;
85 char *sasl_authz_id = NULL;
86 char *sasl_mech = NULL;
87 char *sasl_secprops = NULL;
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:Ss:" "Cd:D:h:nO:p:U:vw:WxX:Y:Z" )) != EOF )
107 /* Password Options */
108 case 'A': /* prompt for old password */
112 case 'a': /* old password (secret) */
113 oldpw = strdup (optarg);
118 for( p = optarg; *p == '\0'; p++ ) {
124 case 'S': /* prompt for user password */
128 case 's': /* new password (secret) */
129 newpw = strdup (optarg);
133 for( p = optarg; *p == '\0'; p++ ) {
144 case 'D': /* bind distinguished name */
145 binddn = strdup (optarg);
148 case 'd': /* debugging option */
149 debug |= atoi (optarg);
152 case 'h': /* ldap host */
153 ldaphost = strdup (optarg);
156 case 'n': /* don't update entry(s) */
160 case 'p': /* ldap port */
161 ldapport = strtol( optarg, NULL, 10 );
164 case 'v': /* verbose */
168 case 'W': /* prompt for bind password */
172 case 'w': /* bind password */
173 passwd.bv_val = strdup (optarg);
177 for( p = optarg; *p == '\0'; p++ ) {
181 passwd.bv_len = strlen( passwd.bv_val );
185 #ifdef HAVE_CYRUS_SASL
186 sasl_secprops = strdup( optarg );
187 authmethod = LDAP_AUTH_SASL;
189 fprintf( stderr, "%s was not compiled with SASL support\n",
191 return( EXIT_FAILURE );
195 #ifdef HAVE_CYRUS_SASL
196 if ( strcasecmp( optarg, "any" ) &&
197 strcmp( optarg, "*" ) ) {
198 sasl_mech = strdup( optarg );
200 authmethod = LDAP_AUTH_SASL;
202 fprintf( stderr, "%s was not compiled with SASL "
203 "support\n", argv[0] );
204 return( EXIT_FAILURE );
208 #ifdef HAVE_CYRUS_SASL
209 sasl_authc_id = strdup( optarg );
210 authmethod = LDAP_AUTH_SASL;
212 fprintf( stderr, "%s was not compiled with SASL "
213 "support\n", argv[0] );
214 return( EXIT_FAILURE );
218 #ifdef HAVE_CYRUS_SASL
219 sasl_authz_id = strdup( optarg );
220 authmethod = LDAP_AUTH_SASL;
222 fprintf( stderr, "%s was not compiled with SASL "
223 "support\n", argv[0] );
224 return( EXIT_FAILURE );
231 fprintf( stderr, "%s was not compiled with TLS "
232 "support\n", argv[0] );
233 return( EXIT_FAILURE );
242 if( argc - optind != 1 ) {
246 dn = strdup( argv[optind] );
248 if( want_oldpw && oldpw == NULL ) {
249 /* prompt for old password */
251 newpw = strdup(getpassphrase("Old password: "));
252 ckoldpw = getpassphrase("Re-enter old password: ");
254 if( newpw== NULL || ckoldpw == NULL ||
255 strncmp( oldpw, ckoldpw, strlen(oldpw) ))
257 fprintf( stderr, "passwords do not match\n" );
262 if( want_newpw && newpw == NULL ) {
263 /* prompt for new password */
265 newpw = strdup(getpassphrase("New password: "));
266 cknewpw = getpassphrase("Re-enter new password: ");
268 if( newpw== NULL || cknewpw == NULL ||
269 strncmp( newpw, cknewpw, strlen(newpw) ))
271 fprintf( stderr, "passwords do not match\n" );
276 if( binddn == NULL && dn != NULL ) {
280 if( passwd.bv_val == NULL ) {
281 passwd.bv_val = oldpw;
282 passwd.bv_len = oldpw == NULL ? 0 : strlen( oldpw );
286 if (want_bindpw && passwd.bv_val == NULL ) {
287 /* handle bind password */
288 fprintf( stderr, "Bind DN: %s\n", binddn );
289 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
290 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
294 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
295 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
297 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
298 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
303 (void) SIGNAL( SIGPIPE, SIG_IGN );
306 /* connect to server */
307 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
313 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
314 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
316 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
317 referrals ? "on" : "off" );
323 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
325 if(rc != LDAP_OPT_SUCCESS ) {
326 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
330 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
332 ldap_perror( ld, "ldap_start_tls" );
333 return( EXIT_FAILURE );
335 fprintf( stderr, "WARNING: could not start TLS\n" );
338 if ( authmethod == LDAP_AUTH_SASL ) {
339 #ifdef HAVE_CYRUS_SASL
340 ldap_set_sasl_interact_proc( ld, lutil_sasl_interact );
342 if( sasl_secprops != NULL ) {
343 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
344 (void *) sasl_secprops );
346 if( rc != LDAP_OPT_SUCCESS ) {
348 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
350 return( EXIT_FAILURE );
354 rc = ldap_sasl_interactive_bind_s( ld, binddn,
355 sasl_mech, NULL, NULL );
357 if( rc != LDAP_SUCCESS ) {
358 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
359 return( EXIT_FAILURE );
362 fprintf( stderr, "%s was not compiled with SASL support\n",
364 return( EXIT_FAILURE );
368 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
370 ldap_perror( ld, "ldap_bind" );
371 return( EXIT_FAILURE );
375 if( dn != NULL || oldpw != NULL || newpw != NULL ) {
376 /* build change password control */
377 BerElement *ber = ber_alloc_t( LBER_USE_DER );
380 perror( "ber_alloc_t" );
385 ber_printf( ber, "{" /*}*/ );
388 ber_printf( ber, "ts",
389 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
393 if( oldpw != NULL ) {
394 ber_printf( ber, "ts",
395 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
399 if( newpw != NULL ) {
400 ber_printf( ber, "ts",
401 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
405 ber_printf( ber, /*{*/ "N}" );
407 rc = ber_flatten( ber, &bv );
410 perror( "ber_flatten" );
423 rc = ldap_extended_operation( ld,
424 LDAP_EXOP_X_MODIFY_PASSWD, bv,
429 if( rc != LDAP_SUCCESS ) {
430 ldap_perror( ld, "ldap_extended_operation" );
435 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
437 ldap_perror( ld, "ldappasswd: ldap_result" );
441 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
443 if( rc != LDAP_SUCCESS ) {
444 ldap_perror( ld, "ldap_parse_result" );
448 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
450 if( rc != LDAP_SUCCESS ) {
451 ldap_perror( ld, "ldap_parse_result" );
455 if( retdata != NULL ) {
458 BerElement *ber = ber_init( retdata );
461 perror( "ber_init" );
466 /* we should check the tag */
467 tag = ber_scanf( ber, "{a}", &s);
469 if( tag == LBER_ERROR ) {
470 perror( "ber_scanf" );
472 printf("New password: %s\n", s);
479 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
480 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
482 if( text && *text ) {
483 printf( "Additional info: %s\n", text );
486 if( matcheddn && *matcheddn ) {
487 printf( "Matched DN: %s\n", matcheddn );
492 for( i=0; refs[i]; i++ ) {
493 printf("Referral: %s\n", refs[i] );
499 ber_memfree( matcheddn );
500 ber_memvfree( (void **) refs );
501 ber_memfree( retoid );
502 ber_bvfree( retdata );
505 /* disconnect from server */