1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1999-08-05 07:55:45 +08:00
|
|
|
/*
|
2003-01-04 04:20:47 +08:00
|
|
|
* Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
|
1999-08-05 07:55:45 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
1998-12-06 11:44:28 +08:00
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/socket.h>
|
1998-12-06 11:44:28 +08:00
|
|
|
#include <ac/string.h>
|
1999-05-19 09:12:33 +08:00
|
|
|
#include <ac/time.h>
|
1998-12-06 11:44:28 +08:00
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
|
|
|
#include <ldap.h>
|
2002-07-27 08:06:27 +08:00
|
|
|
#include "lutil.h"
|
2000-07-14 06:54:38 +08:00
|
|
|
#include "lutil_ldap.h"
|
1999-06-17 11:54:25 +08:00
|
|
|
#include "ldap_defaults.h"
|
1998-12-06 11:44:28 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
#include "common.h"
|
1998-12-06 11:44:28 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
static char *newpw = NULL;
|
|
|
|
static char *oldpw = NULL;
|
|
|
|
static int want_newpw = 0;
|
|
|
|
static int want_oldpw = 0;
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
usage( void )
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1999-12-08 12:37:59 +08:00
|
|
|
fprintf(stderr,
|
2000-07-16 08:28:57 +08:00
|
|
|
"Change password of an LDAP user\n\n"
|
2000-09-07 02:45:12 +08:00
|
|
|
"usage: %s [options] [user]\n"
|
|
|
|
" user: the autentication identity, commonly a DN\n"
|
2000-07-16 07:25:46 +08:00
|
|
|
"Password change options:\n"
|
2000-07-18 05:54:20 +08:00
|
|
|
" -a secret old password\n"
|
|
|
|
" -A prompt for old password\n"
|
|
|
|
" -s secret new password\n"
|
|
|
|
" -S prompt for new password\n"
|
2002-12-13 00:32:26 +08:00
|
|
|
, prog );
|
|
|
|
tool_common_usage();
|
1999-08-04 02:14:24 +08:00
|
|
|
exit( EXIT_FAILURE );
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
2000-07-16 08:14:08 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
const char options[] = "a:As:S" "Cd:D:e:h:H:InO:p:QR:U:vw:WxX:Y:Z";
|
2000-07-16 07:25:46 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
int
|
|
|
|
handle_private_option( int i )
|
|
|
|
{
|
|
|
|
switch ( i ) {
|
|
|
|
#if 0
|
|
|
|
int crit;
|
|
|
|
char *control, *cvalue;
|
2002-08-30 05:36:36 +08:00
|
|
|
case 'E': /* passwd controls */
|
|
|
|
if( version == LDAP_VERSION2 ) {
|
|
|
|
fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
|
2002-12-13 00:32:26 +08:00
|
|
|
prog, version );
|
|
|
|
exit( EXIT_FAILURE );
|
2002-08-30 05:36:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* should be extended to support comma separated list of
|
|
|
|
* [!]key[=value] parameters, e.g. -E !foo,bar=567
|
|
|
|
*/
|
|
|
|
|
|
|
|
crit = 0;
|
|
|
|
cvalue = NULL;
|
|
|
|
if( optarg[0] == '!' ) {
|
|
|
|
crit = 1;
|
|
|
|
optarg++;
|
|
|
|
}
|
|
|
|
|
|
|
|
control = strdup( optarg );
|
|
|
|
if ( (cvalue = strchr( control, '=' )) != NULL ) {
|
|
|
|
*cvalue++ = '\0';
|
|
|
|
}
|
|
|
|
fprintf( stderr, "Invalid passwd control name: %s\n", control );
|
2002-12-13 00:32:26 +08:00
|
|
|
usage();
|
2000-07-18 05:23:59 +08:00
|
|
|
#endif
|
1998-12-06 11:44:28 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
case 'a': /* old password (secret) */
|
|
|
|
oldpw = strdup (optarg);
|
1998-12-06 11:44:28 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
{
|
|
|
|
char* p;
|
|
|
|
for( p = optarg; *p != '\0'; p++ ) {
|
|
|
|
*p = '\0';
|
1999-06-30 02:59:11 +08:00
|
|
|
}
|
2000-07-17 08:56:29 +08:00
|
|
|
}
|
2000-07-18 05:23:59 +08:00
|
|
|
break;
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
case 'A': /* prompt for old password */
|
|
|
|
want_oldpw++;
|
2000-07-17 08:56:29 +08:00
|
|
|
break;
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
case 's': /* new password (secret) */
|
|
|
|
newpw = strdup (optarg);
|
2000-07-17 08:56:29 +08:00
|
|
|
{
|
|
|
|
char* p;
|
2000-11-06 23:53:02 +08:00
|
|
|
for( p = optarg; *p != '\0'; p++ ) {
|
2000-07-17 08:56:29 +08:00
|
|
|
*p = '\0';
|
2000-07-16 08:21:41 +08:00
|
|
|
}
|
2000-07-17 08:56:29 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
case 'S': /* prompt for user password */
|
|
|
|
want_newpw++;
|
|
|
|
break;
|
1998-12-06 11:44:28 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
default:
|
|
|
|
return 0;
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
2002-12-13 00:32:26 +08:00
|
|
|
return 1;
|
|
|
|
}
|
1998-12-06 11:44:28 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *user = NULL;
|
|
|
|
|
|
|
|
LDAP *ld = NULL;
|
2002-12-19 03:16:31 +08:00
|
|
|
struct berval bv = {0};
|
|
|
|
BerElement *ber = NULL;
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
int id, code = LDAP_OTHER;
|
|
|
|
LDAPMessage *res;
|
|
|
|
char *matcheddn = NULL, *text = NULL, **refs = NULL;
|
|
|
|
char *retoid = NULL;
|
|
|
|
struct berval *retdata = NULL;
|
|
|
|
|
2002-12-19 03:16:31 +08:00
|
|
|
prog = lutil_progname( "ldappasswd", argc, argv );
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
/* LDAPv3 only */
|
|
|
|
version = LDAP_VERSION3;
|
|
|
|
|
|
|
|
tool_args( argc, argv );
|
2000-07-16 08:23:41 +08:00
|
|
|
|
2000-09-07 02:45:12 +08:00
|
|
|
if( argc - optind > 1 ) {
|
2002-12-13 00:32:26 +08:00
|
|
|
usage();
|
2000-09-07 02:45:12 +08:00
|
|
|
} else if ( argc - optind == 1 ) {
|
|
|
|
user = strdup( argv[optind] );
|
|
|
|
} else {
|
|
|
|
user = NULL;
|
|
|
|
}
|
1999-12-10 05:30:32 +08:00
|
|
|
|
1999-12-10 12:52:32 +08:00
|
|
|
if( want_oldpw && oldpw == NULL ) {
|
|
|
|
/* prompt for old password */
|
|
|
|
char *ckoldpw;
|
2000-09-07 02:28:15 +08:00
|
|
|
oldpw = strdup(getpassphrase("Old password: "));
|
2000-05-20 02:41:26 +08:00
|
|
|
ckoldpw = getpassphrase("Re-enter old password: ");
|
1999-12-10 12:52:32 +08:00
|
|
|
|
2000-09-07 02:28:15 +08:00
|
|
|
if( oldpw== NULL || ckoldpw == NULL ||
|
2001-12-18 05:56:24 +08:00
|
|
|
strcmp( oldpw, ckoldpw ))
|
2000-06-24 09:40:39 +08:00
|
|
|
{
|
1999-12-10 12:52:32 +08:00
|
|
|
fprintf( stderr, "passwords do not match\n" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( want_newpw && newpw == NULL ) {
|
1999-12-08 12:37:59 +08:00
|
|
|
/* prompt for new password */
|
1998-12-07 04:07:29 +08:00
|
|
|
char *cknewpw;
|
2000-05-20 02:41:26 +08:00
|
|
|
newpw = strdup(getpassphrase("New password: "));
|
|
|
|
cknewpw = getpassphrase("Re-enter new password: ");
|
1998-12-07 04:07:29 +08:00
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
if( newpw== NULL || cknewpw == NULL ||
|
2001-12-18 05:56:24 +08:00
|
|
|
strcmp( newpw, cknewpw ))
|
2000-06-24 09:40:39 +08:00
|
|
|
{
|
1999-12-08 12:37:59 +08:00
|
|
|
fprintf( stderr, "passwords do not match\n" );
|
|
|
|
return EXIT_FAILURE;
|
1998-12-07 04:07:29 +08:00
|
|
|
}
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
2000-05-16 10:16:54 +08:00
|
|
|
if (want_bindpw && passwd.bv_val == NULL ) {
|
1999-12-10 12:52:32 +08:00
|
|
|
/* handle bind password */
|
2000-05-20 02:41:26 +08:00
|
|
|
passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
|
2000-06-24 09:40:39 +08:00
|
|
|
passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
|
1999-12-08 12:37:59 +08:00
|
|
|
}
|
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
ld = tool_conn_setup( 0, 0 );
|
2002-08-30 05:52:45 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
tool_bind( ld );
|
2002-11-28 10:26:55 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
if ( authzid || manageDSAit || noop )
|
|
|
|
tool_server_controls( ld, NULL, 0 );
|
2002-08-30 05:52:45 +08:00
|
|
|
|
2000-09-07 02:45:12 +08:00
|
|
|
if( user != NULL || oldpw != NULL || newpw != NULL ) {
|
1999-12-10 12:52:32 +08:00
|
|
|
/* build change password control */
|
2002-12-19 03:16:31 +08:00
|
|
|
ber = ber_alloc_t( LBER_USE_DER );
|
1999-12-08 12:37:59 +08:00
|
|
|
|
1999-12-10 12:52:32 +08:00
|
|
|
if( ber == NULL ) {
|
|
|
|
perror( "ber_alloc_t" );
|
|
|
|
ldap_unbind( ld );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1999-12-12 03:33:45 +08:00
|
|
|
ber_printf( ber, "{" /*}*/ );
|
|
|
|
|
2000-09-07 02:45:12 +08:00
|
|
|
if( user != NULL ) {
|
1999-12-10 12:52:32 +08:00
|
|
|
ber_printf( ber, "ts",
|
2002-01-29 04:25:30 +08:00
|
|
|
LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
|
2000-09-07 02:45:12 +08:00
|
|
|
free(user);
|
1999-12-10 12:52:32 +08:00
|
|
|
}
|
1999-12-10 05:30:32 +08:00
|
|
|
|
1999-12-10 12:52:32 +08:00
|
|
|
if( oldpw != NULL ) {
|
|
|
|
ber_printf( ber, "ts",
|
2002-01-29 04:25:30 +08:00
|
|
|
LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
|
1999-12-10 12:52:32 +08:00
|
|
|
free(oldpw);
|
|
|
|
}
|
1999-12-10 05:30:32 +08:00
|
|
|
|
1999-12-10 12:52:32 +08:00
|
|
|
if( newpw != NULL ) {
|
|
|
|
ber_printf( ber, "ts",
|
2002-01-29 04:25:30 +08:00
|
|
|
LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
|
1999-12-10 12:52:32 +08:00
|
|
|
free(newpw);
|
|
|
|
}
|
1999-12-10 05:30:32 +08:00
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
ber_printf( ber, /*{*/ "N}" );
|
1999-12-12 03:33:45 +08:00
|
|
|
|
2002-12-19 03:16:31 +08:00
|
|
|
rc = ber_flatten2( ber, &bv, 0 );
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1999-12-10 12:52:32 +08:00
|
|
|
if( rc < 0 ) {
|
2002-12-19 03:16:31 +08:00
|
|
|
perror( "ber_flatten2" );
|
1999-12-10 12:52:32 +08:00
|
|
|
ldap_unbind( ld );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
2000-07-17 08:56:29 +08:00
|
|
|
if ( not ) {
|
2000-07-04 03:03:22 +08:00
|
|
|
rc = LDAP_SUCCESS;
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ldap_extended_operation( ld,
|
2002-12-19 03:16:31 +08:00
|
|
|
LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL,
|
2000-07-04 03:03:22 +08:00
|
|
|
NULL, NULL, &id );
|
1999-12-08 12:37:59 +08:00
|
|
|
|
2002-12-19 03:16:31 +08:00
|
|
|
ber_free( ber, 1 );
|
1999-12-08 12:37:59 +08:00
|
|
|
|
2000-07-04 03:03:22 +08:00
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
ldap_perror( ld, "ldap_extended_operation" );
|
|
|
|
ldap_unbind( ld );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2000-07-05 05:49:00 +08:00
|
|
|
rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
|
2000-07-05 05:59:41 +08:00
|
|
|
if ( rc < 0 ) {
|
|
|
|
ldap_perror( ld, "ldappasswd: ldap_result" );
|
2000-07-04 03:03:22 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
|
|
|
|
|
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
ldap_perror( ld, "ldap_parse_result" );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
|
|
|
|
|
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
ldap_perror( ld, "ldap_parse_result" );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-12-10 12:52:32 +08:00
|
|
|
if( retdata != NULL ) {
|
|
|
|
ber_tag_t tag;
|
|
|
|
char *s;
|
2002-12-19 03:16:31 +08:00
|
|
|
ber = ber_init( retdata );
|
1999-12-10 12:52:32 +08:00
|
|
|
|
|
|
|
if( ber == NULL ) {
|
|
|
|
perror( "ber_init" );
|
|
|
|
ldap_unbind( ld );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we should check the tag */
|
1999-12-12 03:33:45 +08:00
|
|
|
tag = ber_scanf( ber, "{a}", &s);
|
1999-12-10 12:52:32 +08:00
|
|
|
|
|
|
|
if( tag == LBER_ERROR ) {
|
|
|
|
perror( "ber_scanf" );
|
|
|
|
} else {
|
|
|
|
printf("New password: %s\n", s);
|
|
|
|
free( s );
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
}
|
|
|
|
|
2000-07-04 03:03:22 +08:00
|
|
|
if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
|
|
|
|
printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
|
|
|
|
|
|
|
|
if( text && *text ) {
|
|
|
|
printf( "Additional info: %s\n", text );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( matcheddn && *matcheddn ) {
|
|
|
|
printf( "Matched DN: %s\n", matcheddn );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( refs ) {
|
|
|
|
int i;
|
|
|
|
for( i=0; refs[i]; i++ ) {
|
|
|
|
printf("Referral: %s\n", refs[i] );
|
|
|
|
}
|
|
|
|
}
|
1999-12-08 12:37:59 +08:00
|
|
|
}
|
|
|
|
|
2000-07-04 03:03:22 +08:00
|
|
|
ber_memfree( text );
|
|
|
|
ber_memfree( matcheddn );
|
2000-07-05 05:59:41 +08:00
|
|
|
ber_memvfree( (void **) refs );
|
2000-07-04 03:03:22 +08:00
|
|
|
ber_memfree( retoid );
|
1999-12-08 12:37:59 +08:00
|
|
|
ber_bvfree( retdata );
|
|
|
|
|
2000-07-04 03:03:22 +08:00
|
|
|
skip:
|
1998-12-06 11:44:28 +08:00
|
|
|
/* disconnect from server */
|
1998-12-15 02:36:51 +08:00
|
|
|
ldap_unbind (ld);
|
|
|
|
|
2002-03-11 08:25:20 +08:00
|
|
|
return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|