]> git.sur5r.net Git - openldap/blob - clients/tools/ldappasswd.c
85db4949a7097da8db3cb425a5e366cb69dec5d6
[openldap] / clients / tools / ldappasswd.c
1 /* ldappasswd -- a tool for change LDAP passwords */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * Portions Copyright 1998-2001 Net Boolean Incorporated.
8  * Portions Copyright 2001-2003 IBM Corporation.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms are permitted
23  * provided that this notice is preserved and that due credit is given
24  * to the University of Michigan at Ann Arbor.  The name of the
25  * University may not be used to endorse or promote products derived
26  * from this software without specific prior written permission.  This
27  * software is provided ``as is'' without express or implied warranty.
28  */
29 /* The original ldappasswd(1) tool was developed by Dave Storey (F5
30  * Network), based on other OpenLDAP client tools (which are, of
31  * course, based on U-MICH LDAP).  This version was rewritten
32  * by Kurt D. Zeilenga (based on other OpenLDAP client tools).
33  */
34
35 #include "portable.h"
36
37 #include <stdio.h>
38
39 #include <ac/stdlib.h>
40
41 #include <ac/ctype.h>
42 #include <ac/socket.h>
43 #include <ac/string.h>
44 #include <ac/time.h>
45 #include <ac/unistd.h>
46
47 #include <ldap.h>
48 #include "lutil.h"
49 #include "lutil_ldap.h"
50 #include "ldap_defaults.h"
51
52 #include "common.h"
53
54
55 static struct berval newpw = { 0, NULL };
56 static struct berval oldpw = { 0, NULL };
57
58 static int   want_newpw = 0;
59 static int   want_oldpw = 0;
60
61 static char *oldpwfile = NULL;
62 static char *newpwfile = NULL;
63
64 void
65 usage( void )
66 {
67         fprintf( stderr, _("Change password of an LDAP user\n\n"));
68         fprintf( stderr,_("usage: %s [options] [user]\n"), prog);
69         fprintf( stderr, _("  user: the autentication identity, commonly a DN\n"));
70         fprintf( stderr, _("Password change options:\n"));
71         fprintf( stderr, _("  -a secret  old password\n"));
72         fprintf( stderr, _("  -A         prompt for old password\n"));
73         fprintf( stderr, _("  -t file    read file for old password\n"));
74         fprintf( stderr, _("  -s secret  new password\n"));
75         fprintf( stderr, _("  -S         prompt for new password\n"));
76         fprintf( stderr, _("  -T file    read file for new password\n"));
77         tool_common_usage();
78         exit( EXIT_FAILURE );
79 }
80
81
82 const char options[] = "a:As:St:T:"
83         "Cd:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
84
85 int
86 handle_private_option( int i )
87 {
88         switch ( i ) {
89 #if 0
90         case 'E': /* passwd controls */ {
91                 int             crit;
92                 char    *control, *cvalue;
93                 if( protocol == LDAP_VERSION2 ) {
94                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
95                                  prog, protocol );
96                         exit( EXIT_FAILURE );
97                 }
98
99                 /* should be extended to support comma separated list of
100                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
101                  */
102
103                 crit = 0;
104                 cvalue = NULL;
105                 if( optarg[0] == '!' ) {
106                         crit = 1;
107                         optarg++;
108                 }
109
110                 control = strdup( optarg );
111                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
112                         *cvalue++ = '\0';
113                 }
114                 fprintf( stderr, _("Invalid passwd control name: %s\n"), control );
115                 usage();
116                 }
117 #endif
118
119         case 'a':       /* old password (secret) */
120                 oldpw.bv_val = strdup( optarg );
121                 {
122                         char* p;
123                         for( p = optarg; *p != '\0'; p++ ) {
124                                 *p = '\0';
125                         }
126                 }
127                 oldpw.bv_len = strlen( oldpw.bv_val );
128                 break;
129
130         case 'A':       /* prompt for old password */
131                 want_oldpw++;
132                 break;
133
134         case 's':       /* new password (secret) */
135                 newpw.bv_val = strdup (optarg);
136                 {
137                         char* p;
138                         for( p = optarg; *p != '\0'; p++ ) {
139                                 *p = '\0';
140                         }
141                 }
142                 newpw.bv_len = strlen( newpw.bv_val );
143                 break;
144
145         case 'S':       /* prompt for user password */
146                 want_newpw++;
147                 break;
148
149         case 't':
150                 oldpwfile = optarg;
151                 break;
152
153         case 'T':
154                 newpwfile = optarg;
155                 break;
156
157         default:
158                 return 0;
159         }
160         return 1;
161 }
162
163
164 int
165 main( int argc, char *argv[] )
166 {
167         int rc;
168         char    *user = NULL;
169
170         LDAP           *ld = NULL;
171         struct berval bv = {0, NULL};
172         BerElement  *ber = NULL;
173
174         int id, code = LDAP_OTHER;
175         LDAPMessage *res;
176         char *matcheddn = NULL, *text = NULL, **refs = NULL;
177         char    *retoid = NULL;
178         struct berval *retdata = NULL;
179
180     tool_init();
181         prog = lutil_progname( "ldappasswd", argc, argv );
182
183         /* LDAPv3 only */
184         protocol = LDAP_VERSION3;
185
186         tool_args( argc, argv );
187
188         if( argc - optind > 1 ) {
189                 usage();
190         } else if ( argc - optind == 1 ) {
191                 user = strdup( argv[optind] );
192         } else {
193                 user = NULL;
194         }
195
196         if( oldpwfile ) {
197                 rc = lutil_get_filed_password( oldpwfile, &oldpw );
198                 if( rc ) return EXIT_FAILURE;
199         }
200
201         if( want_oldpw && oldpw.bv_val == NULL ) {
202                 /* prompt for old password */
203                 char *ckoldpw;
204                 oldpw.bv_val = strdup(getpassphrase(_("Old password: ")));
205                 ckoldpw = getpassphrase(_("Re-enter old password: "));
206
207                 if( oldpw.bv_val == NULL || ckoldpw == NULL ||
208                         strcmp( oldpw.bv_val, ckoldpw ))
209                 {
210                         fprintf( stderr, _("passwords do not match\n") );
211                         return EXIT_FAILURE;
212                 }
213
214                 oldpw.bv_len = strlen( oldpw.bv_val );
215         }
216
217         if( newpwfile ) {
218                 rc = lutil_get_filed_password( newpwfile, &newpw );
219                 if( rc ) return EXIT_FAILURE;
220         }
221
222         if( want_newpw && newpw.bv_val == NULL ) {
223                 /* prompt for new password */
224                 char *cknewpw;
225                 newpw.bv_val = strdup(getpassphrase(_("New password: ")));
226                 cknewpw = getpassphrase(_("Re-enter new password: "));
227
228                 if( newpw.bv_val == NULL || cknewpw == NULL ||
229                         strcmp( newpw.bv_val, cknewpw ))
230                 {
231                         fprintf( stderr, _("passwords do not match\n") );
232                         return EXIT_FAILURE;
233                 }
234
235                 newpw.bv_len = strlen( newpw.bv_val );
236         }
237
238         if ( pw_file ) {
239                 rc = lutil_get_filed_password( pw_file, &passwd );
240                 if( rc ) return EXIT_FAILURE;
241
242         } else if ( want_bindpw ) {
243                 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
244                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
245         }
246
247         ld = tool_conn_setup( 0, 0 );
248
249         tool_bind( ld );
250
251         if ( assertion || authzid || manageDSAit || noop ) {
252                 tool_server_controls( ld, NULL, 0 );
253         }
254
255         if( user != NULL || oldpw.bv_val != NULL || newpw.bv_val != NULL ) {
256                 /* build change password control */
257                 ber = ber_alloc_t( LBER_USE_DER );
258
259                 if( ber == NULL ) {
260                         perror( "ber_alloc_t" );
261                         ldap_unbind( ld );
262                         return EXIT_FAILURE;
263                 }
264
265                 ber_printf( ber, "{" /*}*/ );
266
267                 if( user != NULL ) {
268                         ber_printf( ber, "ts",
269                                 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
270                         free(user);
271                 }
272
273                 if( oldpw.bv_val != NULL ) {
274                         ber_printf( ber, "tO",
275                                 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &oldpw );
276                         free(oldpw.bv_val);
277                 }
278
279                 if( newpw.bv_val != NULL ) {
280                         ber_printf( ber, "tO",
281                                 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &newpw );
282                         free(newpw.bv_val);
283                 }
284
285                 ber_printf( ber, /*{*/ "N}" );
286
287                 rc = ber_flatten2( ber, &bv, 0 );
288
289                 if( rc < 0 ) {
290                         perror( "ber_flatten2" );
291                         ldap_unbind( ld );
292                         return EXIT_FAILURE;
293                 }
294         }
295
296         if ( not ) {
297                 rc = LDAP_SUCCESS;
298                 goto skip;
299         }
300
301         rc = ldap_extended_operation( ld,
302                 LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL, 
303                 NULL, NULL, &id );
304
305         ber_free( ber, 1 );
306
307         if( rc != LDAP_SUCCESS ) {
308                 ldap_perror( ld, "ldap_extended_operation" );
309                 ldap_unbind( ld );
310                 return EXIT_FAILURE;
311         }
312
313         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
314         if ( rc < 0 ) {
315                 ldap_perror( ld, "ldappasswd: ldap_result" );
316                 return rc;
317         }
318
319         rc = ldap_parse_result( ld, res,
320                 &code, &matcheddn, &text, &refs, NULL, 0 );
321
322         if( rc != LDAP_SUCCESS ) {
323                 ldap_perror( ld, "ldap_parse_result" );
324                 return rc;
325         }
326
327         rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
328
329         if( rc != LDAP_SUCCESS ) {
330                 ldap_perror( ld, "ldap_parse_result" );
331                 return rc;
332         }
333
334         if( retdata != NULL ) {
335                 ber_tag_t tag;
336                 char *s;
337                 ber = ber_init( retdata );
338
339                 if( ber == NULL ) {
340                         perror( "ber_init" );
341                         ldap_unbind( ld );
342                         return EXIT_FAILURE;
343                 }
344
345                 /* we should check the tag */
346                 tag = ber_scanf( ber, "{a}", &s);
347
348                 if( tag == LBER_ERROR ) {
349                         perror( "ber_scanf" );
350                 } else {
351                         printf(_("New password: %s\n"), s);
352                         free( s );
353                 }
354
355                 ber_free( ber, 1 );
356         }
357
358         if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
359                 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
360
361                 if( text && *text ) {
362                         printf( _("Additional info: %s\n"), text );
363                 }
364
365                 if( matcheddn && *matcheddn ) {
366                         printf( _("Matched DN: %s\n"), matcheddn );
367                 }
368
369                 if( refs ) {
370                         int i;
371                         for( i=0; refs[i]; i++ ) {
372                                 printf(_("Referral: %s\n"), refs[i] );
373                         }
374                 }
375         }
376
377         ber_memfree( text );
378         ber_memfree( matcheddn );
379         ber_memvfree( (void **) refs );
380         ber_memfree( retoid );
381         ber_bvfree( retdata );
382
383 skip:
384         /* disconnect from server */
385         ldap_unbind (ld);
386
387         return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
388 }