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 " -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\t\tSimple authentication\n"
53 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
54 " -Y mech\t\tSASL mechanism\n"
55 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
62 main( int argc, char *argv[] )
66 char *ldaphost = NULL;
71 struct berval passwd = { 0, NULL };
85 #ifdef HAVE_CYRUS_SASL
86 char *sasl_authc_id = NULL;
87 char *sasl_authz_id = NULL;
88 char *sasl_mech = NULL;
89 char *sasl_secprops = NULL;
94 struct berval *bv = NULL;
98 char *matcheddn = NULL, *text = NULL, **refs = NULL;
100 struct berval *retdata = NULL;
102 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
107 while( (i = getopt( argc, argv,
108 "Aa:Ss:" "Cd:D:h:nO:p:U:vw:WxX:Y:Z" )) != EOF )
111 /* Password Options */
112 case 'A': /* prompt for old password */
116 case 'a': /* old password (secret) */
117 oldpw = strdup (optarg);
122 for( p = optarg; *p == '\0'; p++ ) {
128 case 'S': /* prompt for user password */
132 case 's': /* new password (secret) */
133 newpw = strdup (optarg);
137 for( p = optarg; *p == '\0'; p++ ) {
148 case 'D': /* bind distinguished name */
149 binddn = strdup (optarg);
152 case 'd': /* debugging option */
153 debug |= atoi (optarg);
156 case 'h': /* ldap host */
157 ldaphost = strdup (optarg);
160 case 'n': /* don't update entry(s) */
164 case 'p': /* ldap port */
165 ldapport = strtol( optarg, NULL, 10 );
168 case 'v': /* verbose */
172 case 'W': /* prompt for bind password */
176 case 'w': /* bind password */
177 passwd.bv_val = strdup (optarg);
181 for( p = optarg; *p == '\0'; p++ ) {
185 passwd.bv_len = strlen( passwd.bv_val );
189 #ifdef HAVE_CYRUS_SASL
190 sasl_secprops = strdup( optarg );
191 authmethod = LDAP_AUTH_SASL;
193 fprintf( stderr, "%s was not compiled with SASL support\n",
195 return( EXIT_FAILURE );
199 #ifdef HAVE_CYRUS_SASL
200 if ( strcasecmp( optarg, "any" ) &&
201 strcmp( optarg, "*" ) ) {
202 sasl_mech = strdup( optarg );
204 authmethod = LDAP_AUTH_SASL;
206 fprintf( stderr, "%s was not compiled with SASL "
207 "support\n", argv[0] );
208 return( EXIT_FAILURE );
212 #ifdef HAVE_CYRUS_SASL
213 sasl_authc_id = strdup( optarg );
214 authmethod = LDAP_AUTH_SASL;
216 fprintf( stderr, "%s was not compiled with SASL "
217 "support\n", argv[0] );
218 return( EXIT_FAILURE );
222 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
223 fprintf( stderr, "%s: incompatible with previous "
224 "authentication choice\n", prog );
227 authmethod = LDAP_AUTH_SIMPLE;
230 #ifdef HAVE_CYRUS_SASL
231 sasl_authz_id = strdup( optarg );
232 authmethod = LDAP_AUTH_SASL;
234 fprintf( stderr, "%s was not compiled with SASL "
235 "support\n", argv[0] );
236 return( EXIT_FAILURE );
243 fprintf( stderr, "%s was not compiled with TLS "
244 "support\n", argv[0] );
245 return( EXIT_FAILURE );
250 fprintf( stderr, "%s: unrecongized option -%c\n",
256 if( argc - optind != 1 ) {
260 if (authmethod == -1) {
261 #ifdef HAVE_CYRUS_SASL
262 authmethod = LDAP_AUTH_SASL;
264 authmethod = LDAP_AUTH_SIMPLE;
268 dn = strdup( argv[optind] );
270 if( want_oldpw && oldpw == NULL ) {
271 /* prompt for old password */
273 newpw = strdup(getpassphrase("Old password: "));
274 ckoldpw = getpassphrase("Re-enter old password: ");
276 if( newpw== NULL || ckoldpw == NULL ||
277 strncmp( oldpw, ckoldpw, strlen(oldpw) ))
279 fprintf( stderr, "passwords do not match\n" );
284 if( want_newpw && newpw == NULL ) {
285 /* prompt for new password */
287 newpw = strdup(getpassphrase("New password: "));
288 cknewpw = getpassphrase("Re-enter new password: ");
290 if( newpw== NULL || cknewpw == NULL ||
291 strncmp( newpw, cknewpw, strlen(newpw) ))
293 fprintf( stderr, "passwords do not match\n" );
298 if( binddn == NULL && dn != NULL ) {
302 if( passwd.bv_val == NULL ) {
303 passwd.bv_val = oldpw;
304 passwd.bv_len = oldpw == NULL ? 0 : strlen( oldpw );
308 if (want_bindpw && passwd.bv_val == NULL ) {
309 /* handle bind password */
310 fprintf( stderr, "Bind DN: %s\n", binddn );
311 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
312 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
316 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
317 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
319 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
320 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
325 (void) SIGNAL( SIGPIPE, SIG_IGN );
328 /* connect to server */
329 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
335 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
336 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
338 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
339 referrals ? "on" : "off" );
345 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
347 if(rc != LDAP_OPT_SUCCESS ) {
348 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
352 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
354 ldap_perror( ld, "ldap_start_tls" );
355 return( EXIT_FAILURE );
357 fprintf( stderr, "WARNING: could not start TLS\n" );
360 if ( authmethod == LDAP_AUTH_SASL ) {
361 #ifdef HAVE_CYRUS_SASL
362 if( sasl_secprops != NULL ) {
363 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
364 (void *) sasl_secprops );
366 if( rc != LDAP_OPT_SUCCESS ) {
368 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
370 return( EXIT_FAILURE );
374 rc = ldap_sasl_interactive_bind_s( ld, binddn,
375 sasl_mech, NULL, NULL, lutil_sasl_interact );
377 if( rc != LDAP_SUCCESS ) {
378 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
379 return( EXIT_FAILURE );
382 fprintf( stderr, "%s was not compiled with SASL support\n",
384 return( EXIT_FAILURE );
388 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
390 ldap_perror( ld, "ldap_bind" );
391 return( EXIT_FAILURE );
395 if( dn != NULL || oldpw != NULL || newpw != NULL ) {
396 /* build change password control */
397 BerElement *ber = ber_alloc_t( LBER_USE_DER );
400 perror( "ber_alloc_t" );
405 ber_printf( ber, "{" /*}*/ );
408 ber_printf( ber, "ts",
409 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
413 if( oldpw != NULL ) {
414 ber_printf( ber, "ts",
415 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
419 if( newpw != NULL ) {
420 ber_printf( ber, "ts",
421 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
425 ber_printf( ber, /*{*/ "N}" );
427 rc = ber_flatten( ber, &bv );
430 perror( "ber_flatten" );
443 rc = ldap_extended_operation( ld,
444 LDAP_EXOP_X_MODIFY_PASSWD, bv,
449 if( rc != LDAP_SUCCESS ) {
450 ldap_perror( ld, "ldap_extended_operation" );
455 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
457 ldap_perror( ld, "ldappasswd: ldap_result" );
461 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
463 if( rc != LDAP_SUCCESS ) {
464 ldap_perror( ld, "ldap_parse_result" );
468 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
470 if( rc != LDAP_SUCCESS ) {
471 ldap_perror( ld, "ldap_parse_result" );
475 if( retdata != NULL ) {
478 BerElement *ber = ber_init( retdata );
481 perror( "ber_init" );
486 /* we should check the tag */
487 tag = ber_scanf( ber, "{a}", &s);
489 if( tag == LBER_ERROR ) {
490 perror( "ber_scanf" );
492 printf("New password: %s\n", s);
499 if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
500 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
502 if( text && *text ) {
503 printf( "Additional info: %s\n", text );
506 if( matcheddn && *matcheddn ) {
507 printf( "Matched DN: %s\n", matcheddn );
512 for( i=0; refs[i]; i++ ) {
513 printf("Referral: %s\n", refs[i] );
519 ber_memfree( matcheddn );
520 ber_memvfree( (void **) refs );
521 ber_memfree( retoid );
522 ber_bvfree( retdata );
525 /* disconnect from server */