3 * Copyright 1998-1999 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 "Usage: %s [options]\n"
31 " -D binddn\tbind dn\tREQUIRED\n"
32 " -d level\tdebugging level\n"
33 " -h host\tldap server (default: localhost)\n"
34 " -n\t\tmake no modifications\n"
35 " -p port\tldap port\n"
36 " -s secret\tnew password\n"
37 " -v\t\tincrease verbosity\n"
38 " -W\t\tprompt for bind password\n"
39 " -w passwd\tbind password (for simple authentication)\n"
46 main( int argc, char *argv[] )
51 char *ldaphost = NULL;
61 struct berval *bv = NULL;
65 struct berval *retdata;
70 while( (i = getopt( argc, argv,
71 "D:d:h:np:s:vWw:" )) != EOF )
74 case 'D': /* bind distinguished name */
75 binddn = strdup (optarg);
78 case 'd': /* debugging option */
79 debug |= atoi (optarg);
82 case 'h': /* ldap host */
83 ldaphost = strdup (optarg);
86 case 'n': /* don't update entry(s) */
90 case 'p': /* ldap port */
91 ldapport = strtol( optarg, NULL, 10 );
94 case 's': /* new password (secret) */
95 newpw = strdup (optarg);
98 case 'v': /* verbose */
102 case 'W': /* prompt for bind password */
106 case 'w': /* bind password */
107 bindpw = strdup (optarg);
111 for( p = optarg; *p == '\0'; p++ ) {
123 if( newpw == NULL ) {
124 /* prompt for new password */
126 newpw = strdup(getpass("New password: "));
127 cknewpw = getpass("Re-enter new password: ");
129 if( strncmp( newpw, cknewpw, strlen(newpw) )) {
130 fprintf( stderr, "passwords do not match\n" );
135 if( binddn == NULL ) {
136 fprintf( stderr, "no bind DN specified\n" );
140 /* handle bind password */
142 fprintf( stderr, "Bind DN: %s\n", binddn );
143 bindpw = strdup( getpass("Enter bind password: "));
147 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
148 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
150 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
151 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
156 (void) SIGNAL( SIGPIPE, SIG_IGN );
159 /* connect to server */
160 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
165 /* don't chase referrals */
166 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
169 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
171 if(rc != LDAP_OPT_SUCCESS ) {
172 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
175 rc = ldap_bind_s( ld, binddn, bindpw, LDAP_AUTH_SIMPLE );
177 if ( rc != LDAP_SUCCESS ) {
178 ldap_perror( ld, "ldap_bind" );
183 /* build change password control */
184 ber = ber_alloc_t( LBER_USE_DER );
187 perror( "ber_alloc_t" );
192 ber_printf( ber, "{es}",
196 rc = ber_flatten( ber, &bv );
199 perror( "ber_flatten" );
206 rc = ldap_extended_operation_s( ld,
207 LDAP_EXOP_X_MODIFY_PASSWD, bv,
213 if ( rc != LDAP_SUCCESS ) {
214 ldap_perror( ld, "ldap_extended_operation" );
219 ldap_memfree( retoid );
220 ber_bvfree( retdata );
222 /* disconnect from server */
225 return ( EXIT_SUCCESS );