]> git.sur5r.net Git - openldap/blob - clients/tools/ldappasswd.c
5e9737347976ea81bb7e03cbe0fd4aa90e5e129e
[openldap] / clients / tools / ldappasswd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12
13 #include <ac/ctype.h>
14 #include <ac/signal.h>
15 #include <ac/socket.h>
16 #include <ac/string.h>
17 #include <ac/time.h>
18 #include <ac/unistd.h>
19
20 #include <ldap.h>
21
22 #include "ldap_defaults.h"
23
24 static int      verbose = 0;
25
26 static void
27 usage(const char *s)
28 {
29         fprintf(stderr,
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"
40                 , s );
41
42         exit( EXIT_FAILURE );
43 }
44
45 int
46 main( int argc, char *argv[] )
47 {
48         int rc;
49         char    *dn = NULL;
50         char    *binddn = NULL;
51         char    *bindpw = NULL;
52         char    *ldaphost = NULL;
53         char    *newpw = NULL;
54         int             noupdates = 0;
55         int             i;
56         int             ldapport = 0;
57         int             debug = 0;
58         int             version = -1;
59         int             want_bindpw = 0;
60         LDAP           *ld;
61         struct berval *bv = NULL;
62         BerElement *ber;
63
64         char    *retoid;
65         struct berval *retdata;
66
67         if (argc == 1)
68                 usage (argv[0]);
69
70         while( (i = getopt( argc, argv,
71                 "D:d:h:np:s:vWw:" )) != EOF )
72         {
73                 switch (i) {
74                 case 'D':       /* bind distinguished name */
75                         binddn = strdup (optarg);
76                         break;
77
78                 case 'd':       /* debugging option */
79                         debug |= atoi (optarg);
80                         break;
81
82                 case 'h':       /* ldap host */
83                         ldaphost = strdup (optarg);
84                         break;
85
86                 case 'n':       /* don't update entry(s) */
87                         noupdates++;
88                         break;
89
90                 case 'p':       /* ldap port */
91                         ldapport = strtol( optarg, NULL, 10 );
92                         break;
93
94                 case 's':       /* new password (secret) */
95                         newpw = strdup (optarg);
96                         break;
97
98                 case 'v':       /* verbose */
99                         verbose++;
100                         break;
101
102                 case 'W':       /* prompt for bind password */
103                         want_bindpw++;
104                         break;
105
106                 case 'w':       /* bind password */
107                         bindpw = strdup (optarg);
108                         {
109                                 char* p;
110
111                                 for( p = optarg; *p == '\0'; p++ ) {
112                                         *p = '*';
113                                 }
114                         }
115                         break;
116
117
118                 default:
119                         usage (argv[0]);
120                 }
121         }
122
123         if( argc - optind != 1 ) {
124                 usage( argv[0] );
125         } 
126
127         dn = strdup( argv[optind] );
128
129         if( newpw == NULL ) {
130                 /* prompt for new password */
131                 char *cknewpw;
132                 newpw = strdup(getpass("New password: "));
133                 cknewpw = getpass("Re-enter new password: ");
134
135                 if( strncmp( newpw, cknewpw, strlen(newpw) )) {
136                         fprintf( stderr, "passwords do not match\n" );
137                         return EXIT_FAILURE;
138                 }
139         }
140
141         if( binddn == NULL ) {
142                 binddn = dn;
143                 dn = NULL;
144         }
145
146         /* handle bind password */
147         if (want_bindpw) {
148                 fprintf( stderr, "Bind DN: %s\n", binddn );
149                 bindpw = strdup( getpass("Enter bind password: "));
150         }
151
152         if ( debug ) {
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 );
155                 }
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 );
158                 }
159         }
160
161 #ifdef SIGPIPE
162         (void) SIGNAL( SIGPIPE, SIG_IGN );
163 #endif
164
165         /* connect to server */
166         if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
167                 perror("ldap_init");
168                 return EXIT_FAILURE;
169         }
170
171         /* don't chase referrals */
172         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
173
174         version = 3;
175         rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
176
177         if(rc != LDAP_OPT_SUCCESS ) {
178                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
179         }
180
181         rc = ldap_bind_s( ld, binddn, bindpw, LDAP_AUTH_SIMPLE );
182
183         if ( rc != LDAP_SUCCESS ) {
184                 ldap_perror( ld, "ldap_bind" );
185                 ldap_unbind( ld );
186                 return EXIT_FAILURE;
187         }
188
189         /* build change password control */
190         ber = ber_alloc_t( LBER_USE_DER );
191
192         if( ber == NULL ) {
193                 perror( "ber_alloc_t" );
194                 ldap_unbind( ld );
195                 return EXIT_FAILURE;
196         }
197
198         if( dn != NULL ) {
199                 ber_printf( ber, "{tsts}",
200                         LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn,
201                         LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
202
203                 free(dn);
204
205         } else {
206                 ber_printf( ber, "{ts}",
207                         LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
208         }
209
210         free(newpw);
211
212         rc = ber_flatten( ber, &bv );
213
214         if( rc < 0 ) {
215                 perror( "ber_flatten" );
216                 ldap_unbind( ld );
217                 return EXIT_FAILURE;
218         }
219
220         ber_free( ber, 1 );
221
222         rc = ldap_extended_operation_s( ld,
223                 LDAP_EXOP_X_MODIFY_PASSWD, bv, 
224                 NULL, NULL,
225                 &retoid, &retdata );
226
227         ber_bvfree( bv );
228
229         if ( rc != LDAP_SUCCESS ) {
230                 ldap_perror( ld, "ldap_extended_operation" );
231                 ldap_unbind( ld );
232                 return EXIT_FAILURE;
233         }
234
235         ldap_memfree( retoid );
236         ber_bvfree( retdata );
237
238         /* disconnect from server */
239         ldap_unbind (ld);
240
241         return ( EXIT_SUCCESS );
242 }