]> git.sur5r.net Git - openldap/blob - clients/tools/ldappasswd.c
c61c6e5eec575c51e9fe12c1a1d03c96a2cf9fee
[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 "ldap_defaults.h"
23
24 static int      verbose = 0;
25
26 static void
27 usage(const char *s)
28 {
29         fprintf(stderr,
30 "Change the password of an LDAP entry\n\n"
31 "usage: %s [options] dn\n"
32 "       dn: the DN of the entry whose password must be changed\n"
33 "options:\n"
34 "       -a secret\told password\n"
35 "       -A\t\tprompt for old password\n"
36 "       -d level\tdebugging level\n"
37 "       -C\t\tchase referrals\n"
38 "       -D binddn\tbind DN\n"
39 "       -E\t\trequest SASL privacy (-EE to make it critical)\n"
40 "       -h host\t\tLDAP server (default: localhost)\n"
41 "       -I\t\trequest SASL integrity checking (-II to make it\n"
42 "               \tcritical)\n"
43 "       -n\t\tmake no modifications\n"
44 "       -p port\t\tport on LDAP server\n"
45 "       -S\t\tprompt for new password\n"
46 "       -s secret\tnew password\n"
47 "       -U user\t\tSASL authentication identity (username)\n"
48 "       -v\t\tverbose mode\n"
49 "       -w passwd\tbind password (for simple authentication)\n"
50 "       -W\t\tprompt for bind password\n"
51 "       -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
52 "       -Y mech\t\tSASL mechanism\n"
53 "       -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
54                 , s );
55
56         exit( EXIT_FAILURE );
57 }
58
59 int
60 main( int argc, char *argv[] )
61 {
62         int rc;
63         char    *ldaphost = NULL;
64
65         char    *dn = NULL;
66         char    *binddn = NULL;
67
68         struct berval passwd = { 0, NULL};
69         char    *newpw = NULL;
70         char    *oldpw = NULL;
71
72         int             want_bindpw = 0;
73         int             want_newpw = 0;
74         int             want_oldpw = 0;
75
76         int             noupdates = 0;
77         int             i;
78         int             ldapport = 0;
79         int             debug = 0;
80         int             version = -1;
81         int             authmethod = LDAP_AUTH_SIMPLE;
82 #ifdef HAVE_CYRUS_SASL
83         char            *sasl_authc_id = NULL;
84         char            *sasl_authz_id = NULL;
85         char            *sasl_mech = NULL;
86         int             sasl_integrity = 0;
87         int             sasl_privacy = 0;
88 #endif
89         int             use_tls = 0;
90         int             referrals = 0;
91         LDAP           *ld;
92         struct berval *bv = NULL;
93
94         int id, code;
95         LDAPMessage *res;
96         char *matcheddn = NULL, *text = NULL, **refs = NULL;
97         char    *retoid = NULL;
98         struct berval *retdata = NULL;
99
100         if (argc == 1)
101                 usage (argv[0]);
102
103         while( (i = getopt( argc, argv,
104                 "Aa:CD:d:EIh:np:Ss:U:vWw:X:Y:Z" )) != EOF )
105         {
106                 switch (i) {
107                 case 'A':       /* prompt for oldr password */
108                         want_oldpw++;
109                         break;
110                 case 'a':       /* old password (secret) */
111                         oldpw = strdup (optarg);
112
113                         {
114                                 char* p;
115
116                                 for( p = optarg; *p == '\0'; p++ ) {
117                                         *p = '*';
118                                 }
119                         }
120                         break;
121                 case 'C':
122                         referrals++;
123                         break;
124                 case 'D':       /* bind distinguished name */
125                         binddn = strdup (optarg);
126                         break;
127
128                 case 'd':       /* debugging option */
129                         debug |= atoi (optarg);
130                         break;
131
132                 case 'h':       /* ldap host */
133                         ldaphost = strdup (optarg);
134                         break;
135
136                 case 'n':       /* don't update entry(s) */
137                         noupdates++;
138                         break;
139
140                 case 'p':       /* ldap port */
141                         ldapport = strtol( optarg, NULL, 10 );
142                         break;
143
144                 case 'S':       /* prompt for user password */
145                         want_newpw++;
146                         break;
147
148                 case 's':       /* new password (secret) */
149                         newpw = strdup (optarg);
150                         {
151                                 char* p;
152
153                                 for( p = optarg; *p == '\0'; p++ ) {
154                                         *p = '*';
155                                 }
156                         }
157                         break;
158
159                 case 'v':       /* verbose */
160                         verbose++;
161                         break;
162
163                 case 'W':       /* prompt for bind password */
164                         want_bindpw++;
165                         break;
166
167                 case 'w':       /* bind password */
168                         passwd.bv_val = strdup (optarg);
169                         {
170                                 char* p;
171
172                                 for( p = optarg; *p == '\0'; p++ ) {
173                                         *p = '*';
174                                 }
175                         }
176                         passwd.bv_len = strlen( passwd.bv_val );
177                         break;
178
179                 case 'I':
180 #ifdef HAVE_CYRUS_SASL
181                         sasl_integrity++;
182                         authmethod = LDAP_AUTH_SASL;
183 #else
184                         fprintf( stderr, "%s was not compiled with SASL "
185                                 "support\n", argv[0] );
186                         return( EXIT_FAILURE );
187 #endif
188                         break;
189                 case 'E':
190 #ifdef HAVE_CYRUS_SASL
191                         sasl_privacy++;
192                         authmethod = LDAP_AUTH_SASL;
193 #else
194                         fprintf( stderr, "%s was not compiled with SASL "
195                                 "support\n", argv[0] );
196                         return( EXIT_FAILURE );
197 #endif
198                         break;
199                 case 'Y':
200 #ifdef HAVE_CYRUS_SASL
201                         if ( strcasecmp( optarg, "any" ) &&
202                                         strcmp( optarg, "*" ) ) {
203                                 sasl_mech = strdup( optarg );
204                         }
205                         authmethod = LDAP_AUTH_SASL;
206 #else
207                         fprintf( stderr, "%s was not compiled with SASL "
208                                 "support\n", argv[0] );
209                         return( EXIT_FAILURE );
210 #endif
211                         break;
212                 case 'U':
213 #ifdef HAVE_CYRUS_SASL
214                         sasl_authc_id = strdup( optarg );
215                         authmethod = LDAP_AUTH_SASL;
216 #else
217                         fprintf( stderr, "%s was not compiled with SASL "
218                                 "support\n", argv[0] );
219                         return( EXIT_FAILURE );
220 #endif
221                         break;
222                 case 'X':
223 #ifdef HAVE_CYRUS_SASL
224                         sasl_authz_id = strdup( optarg );
225                         authmethod = LDAP_AUTH_SASL;
226 #else
227                         fprintf( stderr, "%s was not compiled with SASL "
228                                 "support\n", argv[0] );
229                         return( EXIT_FAILURE );
230 #endif
231                         break;
232                 case 'Z':
233 #ifdef HAVE_TLS
234                         use_tls++;
235 #else
236                         fprintf( stderr, "%s was not compiled with TLS "
237                                 "support\n", argv[0] );
238                         return( EXIT_FAILURE );
239 #endif
240                         break;
241
242                 default:
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                 int     minssf = 0, maxssf = 0;
346
347                 if ( sasl_integrity > 0 )
348                         maxssf = 1;
349                 if ( sasl_integrity > 1 )
350                         minssf = 1;
351                 if ( sasl_privacy > 0 )
352                         maxssf = 100000; /* Something big value */
353                 if ( sasl_privacy > 1 )
354                         minssf = 56;
355                 
356                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
357                                 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
358                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
359                                 "%d\n", minssf);
360                         return( EXIT_FAILURE );
361                 }
362                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
363                                 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
364                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
365                                 "%d\n", maxssf);
366                         return( EXIT_FAILURE );
367                 }
368                 
369                 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
370                                 sasl_authz_id, sasl_mech,
371                                 passwd.bv_len ? &passwd : NULL,
372                                 NULL, NULL );
373
374                 if( rc != LDAP_SUCCESS ) {
375                         ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
376                         return( EXIT_FAILURE );
377                 }
378 #else
379                 fprintf( stderr, "%s was not compiled with SASL support\n",
380                         argv[0] );
381                 return( EXIT_FAILURE );
382 #endif
383         }
384         else {
385                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
386                                 != LDAP_SUCCESS ) {
387                         ldap_perror( ld, "ldap_bind" );
388                         return( EXIT_FAILURE );
389                 }
390         }
391
392         if( dn != NULL || oldpw != NULL || newpw != NULL ) {
393                 /* build change password control */
394                 BerElement *ber = ber_alloc_t( LBER_USE_DER );
395
396                 if( ber == NULL ) {
397                         perror( "ber_alloc_t" );
398                         ldap_unbind( ld );
399                         return EXIT_FAILURE;
400                 }
401
402                 ber_printf( ber, "{" /*}*/ );
403
404                 if( dn != NULL ) {
405                         ber_printf( ber, "ts",
406                                 LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
407                         free(dn);
408                 }
409
410                 if( oldpw != NULL ) {
411                         ber_printf( ber, "ts",
412                                 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
413                         free(oldpw);
414                 }
415
416                 if( newpw != NULL ) {
417                         ber_printf( ber, "ts",
418                                 LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, newpw );
419                         free(newpw);
420                 }
421
422                 ber_printf( ber, /*{*/ "N}" );
423
424                 rc = ber_flatten( ber, &bv );
425
426                 if( rc < 0 ) {
427                         perror( "ber_flatten" );
428                         ldap_unbind( ld );
429                         return EXIT_FAILURE;
430                 }
431
432                 ber_free( ber, 1 );
433         }
434
435         if ( noupdates ) {
436                 rc = LDAP_SUCCESS;
437                 goto skip;
438         }
439
440         rc = ldap_extended_operation( ld,
441                 LDAP_EXOP_X_MODIFY_PASSWD, bv, 
442                 NULL, NULL, &id );
443
444         ber_bvfree( bv );
445
446         if( rc != LDAP_SUCCESS ) {
447                 ldap_perror( ld, "ldap_extended_operation" );
448                 ldap_unbind( ld );
449                 return EXIT_FAILURE;
450         }
451
452         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
453         if ( rc != LDAP_SUCCESS ) {
454                 ldap_perror( ld, "ldap_result" );
455                 return rc;
456         }
457
458         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
459
460         if( rc != LDAP_SUCCESS ) {
461                 ldap_perror( ld, "ldap_parse_result" );
462                 return rc;
463         }
464
465         rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
466
467         if( rc != LDAP_SUCCESS ) {
468                 ldap_perror( ld, "ldap_parse_result" );
469                 return rc;
470         }
471
472         if( retdata != NULL ) {
473                 ber_tag_t tag;
474                 char *s;
475                 BerElement *ber = ber_init( retdata );
476
477                 if( ber == NULL ) {
478                         perror( "ber_init" );
479                         ldap_unbind( ld );
480                         return EXIT_FAILURE;
481                 }
482
483                 /* we should check the tag */
484                 tag = ber_scanf( ber, "{a}", &s);
485
486                 if( tag == LBER_ERROR ) {
487                         perror( "ber_scanf" );
488                 } else {
489                         printf("New password: %s\n", s);
490                         free( s );
491                 }
492
493                 ber_free( ber, 1 );
494         }
495
496         if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
497                 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
498
499                 if( text && *text ) {
500                         printf( "Additional info: %s\n", text );
501                 }
502
503                 if( matcheddn && *matcheddn ) {
504                         printf( "Matched DN: %s\n", matcheddn );
505                 }
506
507                 if( refs ) {
508                         int i;
509                         for( i=0; refs[i]; i++ ) {
510                                 printf("Referral: %s\n", refs[i] );
511                         }
512                 }
513         }
514
515         ber_memfree( text );
516         ber_memfree( matcheddn );
517         ber_memvfree( refs );
518         ber_memfree( retoid );
519         ber_bvfree( retdata );
520
521 skip:
522         /* disconnect from server */
523         ldap_unbind (ld);
524
525         return EXIT_SUCCESS;
526 }