]> git.sur5r.net Git - openldap/blob - clients/tools/ldappasswd.c
d87730bf986411d6835e812ce0384c7928ecb73f
[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;
55         int             ldapport = 0;
56         int             debug = 0;
57         int             version = -1;
58         int             want_bindpw = 0;
59         LDAP           *ld;
60         struct berval *bv = NULL;
61         BerElement *ber;
62
63         char    *retoid;
64         struct berval *retdata;
65
66         if (argc == 1)
67                 usage (argv[0]);
68
69         while( (i = getopt( argc, argv,
70                 "D:d:h:np:s:vWw:" )) != EOF )
71         {
72                 switch (i) {
73                 case 'D':       /* bind distinguished name */
74                         binddn = strdup (optarg);
75                         break;
76
77                 case 'd':       /* debugging option */
78                         debug |= atoi (optarg);
79                         break;
80
81                 case 'h':       /* ldap host */
82                         ldaphost = strdup (optarg);
83                         break;
84
85                 case 'n':       /* don't update entry(s) */
86                         noupdates++;
87                         break;
88
89                 case 'p':       /* ldap port */
90                         ldapport = strtol( optarg, NULL, 10 );
91                         break;
92
93                 case 's':       /* new password (secret) */
94                         newpw = strdup (optarg);
95                         break;
96
97                 case 'v':       /* verbose */
98                         verbose++;
99                         break;
100
101                 case 'W':       /* prompt for bind password */
102                         want_bindpw++;
103                         break;
104
105                 case 'w':       /* bind password */
106                         bindpw = strdup (optarg);
107                         {
108                                 char* p;
109
110                                 for( p = optarg; *p == '\0'; p++ ) {
111                                         *p = '*';
112                                 }
113                         }
114                         break;
115
116
117                 default:
118                         usage (argv[0]);
119                 }
120         }
121
122         if( newpw == NULL ) {
123                 /* prompt for new password */
124                 char *cknewpw;
125                 newpw = strdup(getpass("New password: "));
126                 cknewpw = getpass("Re-enter new password: ");
127
128                 if( strncmp( newpw, cknewpw, strlen(newpw) )) {
129                         fprintf( stderr, "passwords do not match\n" );
130                         return EXIT_FAILURE;
131                 }
132         }
133
134         if( binddn == NULL ) {
135                 fprintf( stderr, "no bind DN specified\n" );
136                 return EXIT_FAILURE;
137         }
138
139         /* handle bind password */
140         if (want_bindpw) {
141                 fprintf( stderr, "Bind DN: %s\n", binddn );
142                 bindpw = strdup( getpass("Enter bind password: "));
143         }
144
145         if ( debug ) {
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 );
148                 }
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 );
151                 }
152         }
153
154 #ifdef SIGPIPE
155         (void) SIGNAL( SIGPIPE, SIG_IGN );
156 #endif
157
158         /* connect to server */
159         if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
160                 perror("ldap_init");
161                 return EXIT_FAILURE;
162         }
163
164         /* don't chase referrals */
165         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
166
167         version = 3;
168         rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
169
170         if(rc != LDAP_OPT_SUCCESS ) {
171                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
172         }
173
174         rc = ldap_bind_s( ld, binddn, bindpw, LDAP_AUTH_SIMPLE );
175
176         if ( rc != LDAP_SUCCESS ) {
177                 ldap_perror( ld, "ldap_bind" );
178                 ldap_unbind( ld );
179                 return EXIT_FAILURE;
180         }
181
182         /* build change password control */
183         ber = ber_alloc_t( LBER_USE_DER );
184
185         if( ber == NULL ) {
186                 perror( "ber_alloc_t" );
187                 ldap_unbind( ld );
188                 return EXIT_FAILURE;
189         }
190
191         ber_printf( ber, "{es}",
192                 (ber_int_t) 0,
193                 newpw );
194
195         rc = ber_flatten( ber, &bv );
196
197         if( rc < 0 ) {
198                 perror( "ber_flatten" );
199                 ldap_unbind( ld );
200                 return EXIT_FAILURE;
201         }
202
203         ber_free( ber, 1 );
204
205         rc = ldap_extended_operation_s( ld,
206                 LDAP_EXOP_X_MODIFY_PASSWD, bv, 
207                 NULL, NULL,
208                 &retoid, &retdata );
209
210         ber_bvfree( bv );
211
212         if ( rc != LDAP_SUCCESS ) {
213                 ldap_perror( ld, "ldap_extended_operation" );
214                 ldap_unbind( ld );
215                 return EXIT_FAILURE;
216         }
217
218         ldap_memfree( retoid );
219         ber_bvfree( retdata );
220
221         /* disconnect from server */
222         ldap_unbind (ld);
223
224         return ( EXIT_SUCCESS );
225 }