]> git.sur5r.net Git - openldap/blob - clients/tools/ldappasswd.c
6a995374f663ed8e34367cf0d129b27e16aad556
[openldap] / clients / tools / ldappasswd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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 "lutil_ldap.h"
23 #include "ldap_defaults.h"
24
25 static int      verbose = 0;
26
27 static void
28 usage(const char *s)
29 {
30         fprintf(stderr,
31 "Change the password of an LDAP entry\n\n"
32 "usage: %s [options] dn\n"
33 "       dn: the DN of the entry whose password must be changed\n"
34 "Password change options:\n"
35 "       -a secret\told password\n"
36 "       -A\t\tprompt for old password\n"
37 "       -s secret\tnew password\n"
38 "       -S\t\tprompt for new password\n"
39
40 "Common options:\n"
41 "       -d level\tdebugging level\n"
42 "       -C\t\tchase referrals\n"
43 "       -D binddn\tbind DN\n"
44 "       -h host\t\tLDAP server (default: localhost)\n"
45 "       -n\t\tmake no modifications\n"
46 "       -O secprops\tSASL security properties\n"
47 "       -p port\t\tport on LDAP server\n"
48 "       -U user\t\tSASL authentication identity (username)\n"
49 "       -v\t\tverbose mode\n"
50 "       -w passwd\tbind password (for simple authentication)\n"
51 "       -W\t\tprompt for bind password\n"
52 "       -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
53 "       -Y mech\t\tSASL mechanism\n"
54 "       -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
55                 , s );
56
57         exit( EXIT_FAILURE );
58 }
59
60 int
61 main( int argc, char *argv[] )
62 {
63         int rc;
64         char    *prog = NULL;
65         char    *ldaphost = NULL;
66
67         char    *dn = NULL;
68         char    *binddn = NULL;
69
70         struct berval passwd = { 0, NULL };
71         char    *newpw = NULL;
72         char    *oldpw = NULL;
73
74         int             want_bindpw = 0;
75         int             want_newpw = 0;
76         int             want_oldpw = 0;
77
78         int             noupdates = 0;
79         int             i;
80         int             ldapport = 0;
81         int             debug = 0;
82         int             version = -1;
83         int             authmethod = -1;
84 #ifdef HAVE_CYRUS_SASL
85         char            *sasl_authc_id = NULL;
86         char            *sasl_authz_id = NULL;
87         char            *sasl_mech = NULL;
88         char            *sasl_secprops = NULL;
89 #endif
90         int             use_tls = 0;
91         int             referrals = 0;
92         LDAP           *ld;
93         struct berval *bv = NULL;
94
95         int id, code;
96         LDAPMessage *res;
97         char *matcheddn = NULL, *text = NULL, **refs = NULL;
98         char    *retoid = NULL;
99         struct berval *retdata = NULL;
100
101     prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
102
103         if (argc == 1)
104                 usage (argv[0]);
105
106         while( (i = getopt( argc, argv,
107                 "Aa:Ss:" "Cd:D:h:nO:p:U:vw:WxX:Y:Z" )) != EOF )
108         {
109                 switch (i) {
110                 /* Password Options */
111                 case 'A':       /* prompt for old password */
112                         want_oldpw++;
113                         break;
114
115                 case 'a':       /* old password (secret) */
116                         oldpw = strdup (optarg);
117
118                         {
119                                 char* p;
120
121                                 for( p = optarg; *p == '\0'; p++ ) {
122                                         *p = '*';
123                                 }
124                         }
125                         break;
126
127                 case 'S':       /* prompt for user password */
128                         want_newpw++;
129                         break;
130
131                 case 's':       /* new password (secret) */
132                         newpw = strdup (optarg);
133                         {
134                                 char* p;
135
136                                 for( p = optarg; *p == '\0'; p++ ) {
137                                         *p = '*';
138                                 }
139                         }
140                         break;
141
142                 /* Common Options */
143                 case 'C':
144                         referrals++;
145                         break;
146
147                 case 'D':       /* bind distinguished name */
148                         binddn = strdup (optarg);
149                         break;
150
151                 case 'd':       /* debugging option */
152                         debug |= atoi (optarg);
153                         break;
154
155                 case 'h':       /* ldap host */
156                         ldaphost = strdup (optarg);
157                         break;
158
159                 case 'n':       /* don't update entry(s) */
160                         noupdates++;
161                         break;
162
163                 case 'p':       /* ldap port */
164                         ldapport = strtol( optarg, NULL, 10 );
165                         break;
166
167                 case 'v':       /* verbose */
168                         verbose++;
169                         break;
170
171                 case 'W':       /* prompt for bind password */
172                         want_bindpw++;
173                         break;
174
175                 case 'w':       /* bind password */
176                         passwd.bv_val = strdup (optarg);
177                         {
178                                 char* p;
179
180                                 for( p = optarg; *p == '\0'; p++ ) {
181                                         *p = '*';
182                                 }
183                         }
184                         passwd.bv_len = strlen( passwd.bv_val );
185                         break;
186
187                 case 'O':
188 #ifdef HAVE_CYRUS_SASL
189                         sasl_secprops = strdup( optarg );
190                         authmethod = LDAP_AUTH_SASL;
191 #else
192                         fprintf( stderr, "%s was not compiled with SASL support\n",
193                                 argv[0] );
194                         return( EXIT_FAILURE );
195 #endif
196                         break;
197                 case 'Y':
198 #ifdef HAVE_CYRUS_SASL
199                         if ( strcasecmp( optarg, "any" ) &&
200                                         strcmp( optarg, "*" ) ) {
201                                 sasl_mech = strdup( optarg );
202                         }
203                         authmethod = LDAP_AUTH_SASL;
204 #else
205                         fprintf( stderr, "%s was not compiled with SASL "
206                                 "support\n", argv[0] );
207                         return( EXIT_FAILURE );
208 #endif
209                         break;
210                 case 'U':
211 #ifdef HAVE_CYRUS_SASL
212                         sasl_authc_id = strdup( optarg );
213                         authmethod = LDAP_AUTH_SASL;
214 #else
215                         fprintf( stderr, "%s was not compiled with SASL "
216                                 "support\n", argv[0] );
217                         return( EXIT_FAILURE );
218 #endif
219                         break;
220                 case 'X':
221 #ifdef HAVE_CYRUS_SASL
222                         sasl_authz_id = strdup( optarg );
223                         authmethod = LDAP_AUTH_SASL;
224 #else
225                         fprintf( stderr, "%s was not compiled with SASL "
226                                 "support\n", argv[0] );
227                         return( EXIT_FAILURE );
228 #endif
229                         break;
230                 case 'Z':
231 #ifdef HAVE_TLS
232                         use_tls++;
233 #else
234                         fprintf( stderr, "%s was not compiled with TLS "
235                                 "support\n", argv[0] );
236                         return( EXIT_FAILURE );
237 #endif
238                         break;
239
240                 default:
241                         fprintf( stderr, "%s: unrecongized option -%c\n",
242                                 prog, optopt );
243                         usage (argv[0]);
244                 }
245         }
246
247         if( argc - optind != 1 ) {
248                 usage( argv[0] );
249         } 
250
251         dn = strdup( argv[optind] );
252
253         if( want_oldpw && oldpw == NULL ) {
254                 /* prompt for old password */
255                 char *ckoldpw;
256                 newpw = strdup(getpassphrase("Old password: "));
257                 ckoldpw = getpassphrase("Re-enter old password: ");
258
259                 if( newpw== NULL || ckoldpw == NULL ||
260                         strncmp( oldpw, ckoldpw, strlen(oldpw) ))
261                 {
262                         fprintf( stderr, "passwords do not match\n" );
263                         return EXIT_FAILURE;
264                 }
265         }
266
267         if( want_newpw && newpw == NULL ) {
268                 /* prompt for new password */
269                 char *cknewpw;
270                 newpw = strdup(getpassphrase("New password: "));
271                 cknewpw = getpassphrase("Re-enter new password: ");
272
273                 if( newpw== NULL || cknewpw == NULL ||
274                         strncmp( newpw, cknewpw, strlen(newpw) ))
275                 {
276                         fprintf( stderr, "passwords do not match\n" );
277                         return EXIT_FAILURE;
278                 }
279         }
280
281         if( binddn == NULL && dn != NULL ) {
282                 binddn = dn;
283                 dn = NULL;
284
285                 if( passwd.bv_val == NULL ) {
286                         passwd.bv_val = oldpw;
287                         passwd.bv_len = oldpw == NULL ? 0 : strlen( oldpw );
288                 }
289         }
290
291         if (want_bindpw && passwd.bv_val == NULL ) {
292                 /* handle bind password */
293                 fprintf( stderr, "Bind DN: %s\n", binddn );
294                 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
295                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
296         }
297
298         if ( debug ) {
299                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
300                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
301                 }
302                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
303                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
304                 }
305         }
306
307 #ifdef SIGPIPE
308         (void) SIGNAL( SIGPIPE, SIG_IGN );
309 #endif
310
311         /* connect to server */
312         if ((ld = ldap_init( ldaphost, ldapport )) == NULL) {
313                 perror("ldap_init");
314                 return EXIT_FAILURE;
315         }
316
317         /* referrals */
318         if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
319                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
320         {
321                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
322                         referrals ? "on" : "off" );
323                 return EXIT_FAILURE;
324         }
325
326         /* LDAPv3 only */
327         version = 3;
328         rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
329
330         if(rc != LDAP_OPT_SUCCESS ) {
331                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
332                 return EXIT_FAILURE;
333         }
334
335         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
336                 if ( use_tls > 1 ) {
337                         ldap_perror( ld, "ldap_start_tls" );
338                         return( EXIT_FAILURE );
339                 }
340                 fprintf( stderr, "WARNING: could not start TLS\n" );
341         }
342
343         if ( authmethod == LDAP_AUTH_SASL ) {
344 #ifdef HAVE_CYRUS_SASL
345                 if( sasl_secprops != NULL ) {
346                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
347                                 (void *) sasl_secprops );
348                         
349                         if( rc != LDAP_OPT_SUCCESS ) {
350                                 fprintf( stderr,
351                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
352                                         sasl_secprops );
353                                 return( EXIT_FAILURE );
354                         }
355                 }
356                 
357                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
358                         sasl_mech, NULL, NULL, lutil_sasl_interact );
359
360                 if( rc != LDAP_SUCCESS ) {
361                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
362                         return( EXIT_FAILURE );
363                 }
364 #else
365                 fprintf( stderr, "%s was not compiled with SASL support\n",
366                         argv[0] );
367                 return( EXIT_FAILURE );
368 #endif
369         }
370         else {
371                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
372                                 != LDAP_SUCCESS ) {
373                         ldap_perror( ld, "ldap_bind" );
374                         return( EXIT_FAILURE );
375                 }
376         }
377
378         if( dn != NULL || oldpw != NULL || newpw != NULL ) {
379                 /* build change password control */
380                 BerElement *ber = ber_alloc_t( LBER_USE_DER );
381
382                 if( ber == NULL ) {
383                         perror( "ber_alloc_t" );
384                         ldap_unbind( ld );
385                         return EXIT_FAILURE;
386                 }
387
388                 ber_printf( ber, "{" /*}*/ );
389
390                 if( dn != NULL ) {
391                         ber_printf( ber, "ts",
392                                 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
393                         free(dn);
394                 }
395
396                 if( oldpw != NULL ) {
397                         ber_printf( ber, "ts",
398                                 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
399                         free(oldpw);
400                 }
401
402                 if( newpw != NULL ) {
403                         ber_printf( ber, "ts",
404                                 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
405                         free(newpw);
406                 }
407
408                 ber_printf( ber, /*{*/ "N}" );
409
410                 rc = ber_flatten( ber, &bv );
411
412                 if( rc < 0 ) {
413                         perror( "ber_flatten" );
414                         ldap_unbind( ld );
415                         return EXIT_FAILURE;
416                 }
417
418                 ber_free( ber, 1 );
419         }
420
421         if ( noupdates ) {
422                 rc = LDAP_SUCCESS;
423                 goto skip;
424         }
425
426         rc = ldap_extended_operation( ld,
427                 LDAP_EXOP_X_MODIFY_PASSWD, bv, 
428                 NULL, NULL, &id );
429
430         ber_bvfree( bv );
431
432         if( rc != LDAP_SUCCESS ) {
433                 ldap_perror( ld, "ldap_extended_operation" );
434                 ldap_unbind( ld );
435                 return EXIT_FAILURE;
436         }
437
438         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
439         if ( rc < 0 ) {
440                 ldap_perror( ld, "ldappasswd: ldap_result" );
441                 return rc;
442         }
443
444         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
445
446         if( rc != LDAP_SUCCESS ) {
447                 ldap_perror( ld, "ldap_parse_result" );
448                 return rc;
449         }
450
451         rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
452
453         if( rc != LDAP_SUCCESS ) {
454                 ldap_perror( ld, "ldap_parse_result" );
455                 return rc;
456         }
457
458         if( retdata != NULL ) {
459                 ber_tag_t tag;
460                 char *s;
461                 BerElement *ber = ber_init( retdata );
462
463                 if( ber == NULL ) {
464                         perror( "ber_init" );
465                         ldap_unbind( ld );
466                         return EXIT_FAILURE;
467                 }
468
469                 /* we should check the tag */
470                 tag = ber_scanf( ber, "{a}", &s);
471
472                 if( tag == LBER_ERROR ) {
473                         perror( "ber_scanf" );
474                 } else {
475                         printf("New password: %s\n", s);
476                         free( s );
477                 }
478
479                 ber_free( ber, 1 );
480         }
481
482         if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
483                 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
484
485                 if( text && *text ) {
486                         printf( "Additional info: %s\n", text );
487                 }
488
489                 if( matcheddn && *matcheddn ) {
490                         printf( "Matched DN: %s\n", matcheddn );
491                 }
492
493                 if( refs ) {
494                         int i;
495                         for( i=0; refs[i]; i++ ) {
496                                 printf("Referral: %s\n", refs[i] );
497                         }
498                 }
499         }
500
501         ber_memfree( text );
502         ber_memfree( matcheddn );
503         ber_memvfree( (void **) refs );
504         ber_memfree( retoid );
505         ber_bvfree( retdata );
506
507 skip:
508         /* disconnect from server */
509         ldap_unbind (ld);
510
511         return EXIT_SUCCESS;
512 }