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;
60 struct berval *bv = NULL;
64 struct berval *retdata;
69 while( (i = getopt( argc, argv,
70 "D:d:h:np:s:vWw:" )) != EOF )
73 case 'D': /* bind distinguished name */
74 binddn = strdup (optarg);
77 case 'd': /* debugging option */
78 debug |= atoi (optarg);
81 case 'h': /* ldap host */
82 ldaphost = strdup (optarg);
85 case 'n': /* don't update entry(s) */
89 case 'p': /* ldap port */
90 ldapport = strtol( optarg, NULL, 10 );
93 case 's': /* new password (secret) */
94 newpw = strdup (optarg);
97 case 'v': /* verbose */
101 case 'W': /* prompt for bind password */
105 case 'w': /* bind password */
106 bindpw = strdup (optarg);
110 for( p = optarg; *p == '\0'; p++ ) {
122 if( newpw == NULL ) {
123 /* prompt for new password */
125 newpw = strdup(getpass("New password: "));
126 cknewpw = getpass("Re-enter new password: ");
128 if( strncmp( newpw, cknewpw, strlen(newpw) )) {
129 fprintf( stderr, "passwords do not match\n" );
134 if( binddn == NULL ) {
135 fprintf( stderr, "no bind DN specified\n" );
139 /* handle bind password */
141 fprintf( stderr, "Bind DN: %s\n", binddn );
142 bindpw = strdup( getpass("Enter bind password: "));
146 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
147 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
149 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
150 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
155 (void) SIGNAL( SIGPIPE, SIG_IGN );
158 /* connect to server */
159 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
164 /* don't chase referrals */
165 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
168 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
170 if(rc != LDAP_OPT_SUCCESS ) {
171 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
174 rc = ldap_bind_s( ld, binddn, bindpw, LDAP_AUTH_SIMPLE );
176 if ( rc != LDAP_SUCCESS ) {
177 ldap_perror( ld, "ldap_bind" );
182 /* build change password control */
183 ber = ber_alloc_t( LBER_USE_DER );
186 perror( "ber_alloc_t" );
191 ber_printf( ber, "{es}",
195 rc = ber_flatten( ber, &bv );
198 perror( "ber_flatten" );
205 rc = ldap_extended_operation_s( ld,
206 LDAP_EXOP_X_MODIFY_PASSWD, bv,
212 if ( rc != LDAP_SUCCESS ) {
213 ldap_perror( ld, "ldap_extended_operation" );
218 ldap_memfree( retoid );
219 ber_bvfree( retdata );
221 /* disconnect from server */
224 return ( EXIT_SUCCESS );