]> git.sur5r.net Git - openldap/blob - clients/tools/ldappasswd.c
c8337121baf0117809c358cd5f60a7d1e5c76c09
[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]\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"
40                 , s );
41
42         exit( EXIT_FAILURE );
43 }
44
45 int
46 main( int argc, char *argv[] )
47 {
48         int rc;
49         char    *binddn = NULL;
50         char    *bindpw = NULL;
51         char    *ldaphost = NULL;
52         char    *newpw = NULL;
53         int             noupdates = 0;
54         int             i, j;
55         int             ldapport = 0;
56         int             debug = 0;
57         int             version = -1;
58         int             want_bindpw = 0;
59         LDAP           *ld;
60         struct berval cred;
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( newpw == NULL ) {
124                 /* prompt for new password */
125                 char *cknewpw;
126                 newpw = strdup(getpass("New password: "));
127                 cknewpw = getpass("Re-enter new password: ");
128
129                 if( strncmp( newpw, cknewpw, strlen(newpw) )) {
130                         fprintf( stderr, "passwords do not match\n" );
131                         return EXIT_FAILURE;
132                 }
133         }
134
135         if( binddn == NULL ) {
136                 fprintf( stderr, "no bind DN specified\n" );
137                 return EXIT_FAILURE;
138         }
139
140         /* handle bind password */
141         if (want_bindpw) {
142                 fprintf( stderr, "Bind DN: %s\n", binddn );
143                 bindpw = strdup( getpass("Enter bind password: "));
144         }
145
146         if ( debug ) {
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 );
149                 }
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 );
152                 }
153         }
154
155 #ifdef SIGPIPE
156         (void) SIGNAL( SIGPIPE, SIG_IGN );
157 #endif
158
159         /* connect to server */
160         if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
161                 perror("ldap_init");
162                 return EXIT_FAILURE;
163         }
164
165         /* don't chase referrals */
166         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
167
168         version = 3;
169         rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
170
171         if(rc != LDAP_OPT_SUCCESS ) {
172                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
173         }
174
175         rc = ldap_bind_s( ld, binddn, bindpw, LDAP_AUTH_SIMPLE );
176
177         if ( rc != LDAP_SUCCESS ) {
178                 ldap_perror( ld, "ldap_bind" );
179                 ldap_unbind( ld );
180                 return EXIT_FAILURE;
181         }
182
183         /* build change password control */
184         ber = ber_alloc_t( LBER_USE_DER );
185
186         if( ber == NULL ) {
187                 perror( "ber_alloc_t" );
188                 ldap_unbind( ld );
189                 return EXIT_FAILURE;
190         }
191
192         ber_printf( ber, "{es}",
193                 (ber_int_t) 0,
194                 newpw );
195
196         rc = ber_flatten( ber, &bv );
197
198         if( rc < 0 ) {
199                 perror( "ber_flatten" );
200                 ldap_unbind( ld );
201                 return EXIT_FAILURE;
202         }
203
204         ber_free( ber, 1 );
205
206         rc = ldap_extended_operation_s( ld,
207                 LDAP_EXOP_X_MODIFY_PASSWD, bv, 
208                 NULL, NULL,
209                 &retoid, &retdata );
210
211         ber_bvfree( bv );
212
213         if ( rc != LDAP_SUCCESS ) {
214                 ldap_perror( ld, "ldap_extended_operation" );
215                 ldap_unbind( ld );
216                 return EXIT_FAILURE;
217         }
218
219         ldap_memfree( retoid );
220         ber_bvfree( retdata );
221
222         /* disconnect from server */
223         ldap_unbind (ld);
224
225         return ( EXIT_SUCCESS );
226 }