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] dn\n"
31 " -D binddn\tbind dn\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[] )
52 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( argc - optind != 1 ) {
127 dn = strdup( argv[optind] );
129 if( newpw == NULL ) {
130 /* prompt for new password */
132 newpw = strdup(getpass("New password: "));
133 cknewpw = getpass("Re-enter new password: ");
135 if( strncmp( newpw, cknewpw, strlen(newpw) )) {
136 fprintf( stderr, "passwords do not match\n" );
141 if( binddn == NULL ) {
146 /* handle bind password */
148 fprintf( stderr, "Bind DN: %s\n", binddn );
149 bindpw = strdup( getpass("Enter bind password: "));
153 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
154 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
156 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
157 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
162 (void) SIGNAL( SIGPIPE, SIG_IGN );
165 /* connect to server */
166 if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
171 /* don't chase referrals */
172 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
175 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
177 if(rc != LDAP_OPT_SUCCESS ) {
178 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
181 rc = ldap_bind_s( ld, binddn, bindpw, LDAP_AUTH_SIMPLE );
183 if ( rc != LDAP_SUCCESS ) {
184 ldap_perror( ld, "ldap_bind" );
189 /* build change password control */
190 ber = ber_alloc_t( LBER_USE_DER );
193 perror( "ber_alloc_t" );
199 ber_printf( ber, "{tsts}",
200 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn,
201 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
206 ber_printf( ber, "{ts}",
207 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
212 rc = ber_flatten( ber, &bv );
215 perror( "ber_flatten" );
222 rc = ldap_extended_operation_s( ld,
223 LDAP_EXOP_X_MODIFY_PASSWD, bv,
229 if ( rc != LDAP_SUCCESS ) {
230 ldap_perror( ld, "ldap_extended_operation" );
235 ldap_memfree( retoid );
236 ber_bvfree( retdata );
238 /* disconnect from server */
241 return ( EXIT_SUCCESS );