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 " -D binddn\tbind DN\n"
38 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
39 " -h host\t\tLDAP server (default: localhost)\n"
40 " -I\t\trequest SASL integrity checking (-II to make it\n"
42 " -n\t\tmake no modifications\n"
43 " -p port\t\tport on LDAP server\n"
44 " -S\t\tprompt for new password\n"
45 " -s secret\tnew password\n"
46 " -U user\t\tSASL authentication identity (username)\n"
47 " -v\t\tverbose mode\n"
48 " -w passwd\tbind password (for simple authentication)\n"
49 " -W\t\tprompt for bind password\n"
50 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
51 " -Y mech\t\tSASL mechanism\n"
52 " -Z\t\trequest the use of TLS (-ZZ to make it critical)\n"
59 main( int argc, char *argv[] )
62 char *ldaphost = NULL;
80 int authmethod = LDAP_AUTH_SIMPLE;
81 #ifdef HAVE_CYRUS_SASL
82 char *sasl_authc_id = NULL;
83 char *sasl_authz_id = NULL;
84 char *sasl_mech = NULL;
85 int sasl_integrity = 0;
90 struct berval *bv = NULL;
93 struct berval *retdata;
98 while( (i = getopt( argc, argv,
99 "Aa:D:d:EIh:np:Ss:U:vWw:X:Y:Z" )) != EOF )
102 case 'A': /* prompt for oldr password */
105 case 'a': /* old password (secret) */
106 oldpw = strdup (optarg);
111 for( p = optarg; *p == '\0'; p++ ) {
116 case 'D': /* bind distinguished name */
117 binddn = strdup (optarg);
120 case 'd': /* debugging option */
121 debug |= atoi (optarg);
124 case 'h': /* ldap host */
125 ldaphost = strdup (optarg);
128 case 'n': /* don't update entry(s) */
132 case 'p': /* ldap port */
133 ldapport = strtol( optarg, NULL, 10 );
136 case 'S': /* prompt for user password */
140 case 's': /* new password (secret) */
141 newpw = strdup (optarg);
146 for( p = optarg; *p == '\0'; p++ ) {
152 case 'v': /* verbose */
156 case 'W': /* prompt for bind password */
160 case 'w': /* bind password */
161 bindpw = strdup (optarg);
166 for( p = optarg; *p == '\0'; p++ ) {
173 #ifdef HAVE_CYRUS_SASL
175 authmethod = LDAP_AUTH_SASL;
177 fprintf( stderr, "%s was not compiled with SASL "
178 "support\n", argv[0] );
179 return( EXIT_FAILURE );
183 #ifdef HAVE_CYRUS_SASL
185 authmethod = LDAP_AUTH_SASL;
187 fprintf( stderr, "%s was not compiled with SASL "
188 "support\n", argv[0] );
189 return( EXIT_FAILURE );
193 #ifdef HAVE_CYRUS_SASL
194 if ( strcasecmp( optarg, "any" ) &&
195 strcmp( optarg, "*" ) ) {
196 sasl_mech = strdup( optarg );
198 authmethod = LDAP_AUTH_SASL;
200 fprintf( stderr, "%s was not compiled with SASL "
201 "support\n", argv[0] );
202 return( EXIT_FAILURE );
206 #ifdef HAVE_CYRUS_SASL
207 sasl_authc_id = strdup( optarg );
208 authmethod = LDAP_AUTH_SASL;
210 fprintf( stderr, "%s was not compiled with SASL "
211 "support\n", argv[0] );
212 return( EXIT_FAILURE );
216 #ifdef HAVE_CYRUS_SASL
217 sasl_authz_id = strdup( optarg );
218 authmethod = LDAP_AUTH_SASL;
220 fprintf( stderr, "%s was not compiled with SASL "
221 "support\n", argv[0] );
222 return( EXIT_FAILURE );
229 fprintf( stderr, "%s was not compiled with TLS "
230 "support\n", argv[0] );
231 return( EXIT_FAILURE );
240 if( argc - optind != 1 ) {
244 dn = strdup( argv[optind] );
246 if( want_oldpw && oldpw == NULL ) {
247 /* prompt for old password */
249 newpw = strdup(getpass("Old password: "));
250 ckoldpw = getpass("Re-enter old password: ");
252 if( strncmp( oldpw, ckoldpw, strlen(oldpw) )) {
253 fprintf( stderr, "passwords do not match\n" );
258 if( want_newpw && newpw == NULL ) {
259 /* prompt for new password */
261 newpw = strdup(getpass("New password: "));
262 cknewpw = getpass("Re-enter new password: ");
264 if( strncmp( newpw, cknewpw, strlen(newpw) )) {
265 fprintf( stderr, "passwords do not match\n" );
270 if( binddn == NULL && dn != NULL ) {
274 if( bindpw == NULL ) bindpw = oldpw;
277 if (want_bindpw && bindpw == NULL ) {
278 /* handle bind password */
279 fprintf( stderr, "Bind DN: %s\n", binddn );
280 bindpw = strdup( getpass("Enter bind password: "));
284 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
285 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
287 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
288 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
293 (void) SIGNAL( SIGPIPE, SIG_IGN );
296 /* connect to server */
297 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
302 /* don't chase referrals */
303 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
307 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
309 if(rc != LDAP_OPT_SUCCESS ) {
310 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
313 if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
315 ldap_perror( ld, "ldap_start_tls" );
316 return( EXIT_FAILURE );
320 if ( authmethod == LDAP_AUTH_SASL ) {
321 #ifdef HAVE_CYRUS_SASL
322 int minssf = 0, maxssf = 0;
324 if ( sasl_integrity > 0 )
326 if ( sasl_integrity > 1 )
328 if ( sasl_privacy > 0 )
329 maxssf = 100000; /* Something big value */
330 if ( sasl_privacy > 1 )
333 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
334 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
335 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
337 return( EXIT_FAILURE );
339 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
340 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
341 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
343 return( EXIT_FAILURE );
346 if ( ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
347 sasl_authz_id, sasl_mech, NULL, NULL, NULL )
349 ldap_perror( ld, "ldap_sasl_bind" );
350 return( EXIT_FAILURE );
353 fprintf( stderr, "%s was not compiled with SASL support\n",
355 return( EXIT_FAILURE );
359 if ( ldap_bind_s( ld, binddn, bindpw, authmethod )
361 ldap_perror( ld, "ldap_bind" );
362 return( EXIT_FAILURE );
366 if( dn != NULL || oldpw != NULL || newpw != NULL ) {
367 /* build change password control */
368 BerElement *ber = ber_alloc_t( LBER_USE_DER );
371 perror( "ber_alloc_t" );
376 ber_printf( ber, "{" /*}*/ );
379 ber_printf( ber, "ts",
380 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
384 if( oldpw != NULL ) {
385 ber_printf( ber, "ts",
386 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
390 if( newpw != NULL ) {
391 ber_printf( ber, "ts",
392 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
396 ber_printf( ber, /*{*/ "}" );
398 rc = ber_flatten( ber, &bv );
401 perror( "ber_flatten" );
409 rc = ldap_extended_operation_s( ld,
410 LDAP_EXOP_X_MODIFY_PASSWD, bv,
416 if( retdata != NULL ) {
419 BerElement *ber = ber_init( retdata );
422 perror( "ber_init" );
427 /* we should check the tag */
428 tag = ber_scanf( ber, "{a}", &s);
430 if( tag == LBER_ERROR ) {
431 perror( "ber_scanf" );
433 printf("New password: %s\n", s);
440 if ( rc != LDAP_SUCCESS ) {
441 ldap_perror( ld, "ldap_extended_operation" );
446 ldap_memfree( retoid );
447 ber_bvfree( retdata );
449 /* disconnect from server */