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