2004-02-27 03:03:41 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2014-01-25 21:21:25 +08:00
|
|
|
* Copyright 1998-2014 The OpenLDAP Foundation.
|
2004-02-27 03:03:41 +08:00
|
|
|
* Portions Copyright 1998-2003 Kurt D. Zeilenga.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was initially developed by Kurt Zeilenga for inclusion
|
|
|
|
* in OpenLDAP Software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
|
|
|
|
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/signal.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
|
|
|
#include <ldap.h>
|
2006-05-11 08:12:54 +08:00
|
|
|
#include <lber_pvt.h>
|
2004-02-27 03:03:41 +08:00
|
|
|
#include <lutil.h>
|
2005-02-28 00:46:45 +08:00
|
|
|
#include <lutil_sha1.h>
|
2004-02-27 03:03:41 +08:00
|
|
|
|
|
|
|
#include "ldap_defaults.h"
|
2006-08-31 06:45:26 +08:00
|
|
|
#include "slap.h"
|
2004-02-27 03:03:41 +08:00
|
|
|
|
|
|
|
static int verbose = 0;
|
2012-05-31 12:13:41 +08:00
|
|
|
static char *modulepath = NULL;
|
|
|
|
static char *moduleload = NULL;
|
2004-02-27 03:03:41 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(const char *s)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: %s [options]\n"
|
2006-05-11 08:12:54 +08:00
|
|
|
" -c format\tcrypt(3) salt format\n"
|
2006-05-11 11:09:40 +08:00
|
|
|
" -g\t\tgenerate random password\n"
|
2004-02-27 03:03:41 +08:00
|
|
|
" -h hash\tpassword scheme\n"
|
2006-05-11 22:36:20 +08:00
|
|
|
" -n\t\tomit trailing newline\n"
|
2012-05-31 12:13:41 +08:00
|
|
|
" -o <opt>[=val] specify an option with a(n optional) value\n"
|
|
|
|
" \tmodule-path=<pathspec>\n"
|
|
|
|
" \tmodule-load=<filename>\n"
|
2004-02-27 03:03:41 +08:00
|
|
|
" -s secret\tnew password\n"
|
|
|
|
" -u\t\tgenerate RFC2307 values (default)\n"
|
|
|
|
" -v\t\tincrease verbosity\n"
|
|
|
|
" -T file\tread file for new password\n"
|
|
|
|
, s );
|
|
|
|
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2012-05-31 12:13:41 +08:00
|
|
|
static int
|
|
|
|
parse_slappasswdopt( void )
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = strchr( optarg, '=' );
|
|
|
|
if ( p != NULL ) {
|
|
|
|
len = p - optarg;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strncasecmp( optarg, "module-path", len ) == 0 ) {
|
|
|
|
if ( modulepath )
|
|
|
|
ch_free( modulepath );
|
|
|
|
modulepath = ch_strdup( p );
|
|
|
|
|
|
|
|
} else if ( strncasecmp( optarg, "module-load", len ) == 0 ) {
|
|
|
|
if ( moduleload )
|
|
|
|
ch_free( moduleload );
|
|
|
|
moduleload = ch_strdup( p );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-02-27 03:03:41 +08:00
|
|
|
int
|
|
|
|
slappasswd( int argc, char *argv[] )
|
|
|
|
{
|
2012-05-31 12:13:41 +08:00
|
|
|
int rc = EXIT_SUCCESS;
|
2005-02-28 00:46:45 +08:00
|
|
|
#ifdef LUTIL_SHA1_BYTES
|
2006-05-11 08:12:54 +08:00
|
|
|
char *default_scheme = "{SSHA}";
|
2005-02-28 00:46:45 +08:00
|
|
|
#else
|
2006-05-11 08:12:54 +08:00
|
|
|
char *default_scheme = "{SMD5}";
|
2005-02-28 00:46:45 +08:00
|
|
|
#endif
|
2006-05-11 08:12:54 +08:00
|
|
|
char *scheme = default_scheme;
|
2005-02-28 00:46:45 +08:00
|
|
|
|
2004-02-27 03:03:41 +08:00
|
|
|
char *newpw = NULL;
|
|
|
|
char *pwfile = NULL;
|
|
|
|
const char *text;
|
|
|
|
const char *progname = "slappasswd";
|
|
|
|
|
|
|
|
int i;
|
2006-05-11 22:36:20 +08:00
|
|
|
char *newline = "\n";
|
2006-05-11 11:09:40 +08:00
|
|
|
struct berval passwd = BER_BVNULL;
|
2004-03-03 06:11:31 +08:00
|
|
|
struct berval hash;
|
2004-02-27 03:03:41 +08:00
|
|
|
|
2012-05-31 12:13:41 +08:00
|
|
|
#ifdef LDAP_DEBUG
|
|
|
|
/* tools default to "none", so that at least LDAP_DEBUG_ANY
|
|
|
|
* messages show up; use -d 0 to reset */
|
|
|
|
slap_debug = LDAP_DEBUG_NONE;
|
|
|
|
#endif
|
|
|
|
ldap_syslog = 0;
|
|
|
|
|
2004-02-27 03:03:41 +08:00
|
|
|
while( (i = getopt( argc, argv,
|
2012-05-31 12:13:41 +08:00
|
|
|
"c:d:gh:no:s:T:vu" )) != EOF )
|
2004-02-27 03:03:41 +08:00
|
|
|
{
|
|
|
|
switch (i) {
|
|
|
|
case 'c': /* crypt salt format */
|
|
|
|
scheme = "{CRYPT}";
|
|
|
|
lutil_salt_format( optarg );
|
|
|
|
break;
|
|
|
|
|
2006-05-11 08:12:54 +08:00
|
|
|
case 'g': /* new password (generate) */
|
|
|
|
if ( pwfile != NULL ) {
|
2006-05-11 11:09:40 +08:00
|
|
|
fprintf( stderr, "Option -g incompatible with -T\n" );
|
2006-05-11 08:12:54 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
} else if ( newpw != NULL ) {
|
|
|
|
fprintf( stderr, "New password already provided\n" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2006-05-11 11:09:40 +08:00
|
|
|
} else if ( lutil_passwd_generate( &passwd, 8 )) {
|
|
|
|
fprintf( stderr, "Password generation failed\n" );
|
2006-05-11 08:12:54 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-02-27 03:03:41 +08:00
|
|
|
case 'h': /* scheme */
|
2006-05-11 11:09:40 +08:00
|
|
|
if ( scheme != default_scheme ) {
|
2006-05-11 08:12:54 +08:00
|
|
|
fprintf( stderr, "Scheme already provided\n" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
} else {
|
2006-08-29 09:36:52 +08:00
|
|
|
scheme = ch_strdup( optarg );
|
2006-05-11 08:12:54 +08:00
|
|
|
}
|
2004-02-27 03:03:41 +08:00
|
|
|
break;
|
|
|
|
|
2006-05-11 22:36:20 +08:00
|
|
|
case 'n':
|
|
|
|
newline = "";
|
|
|
|
break;
|
|
|
|
|
2012-05-31 12:13:41 +08:00
|
|
|
case 'o':
|
|
|
|
if ( parse_slappasswdopt() ) {
|
|
|
|
usage ( progname );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-02-27 03:03:41 +08:00
|
|
|
case 's': /* new password (secret) */
|
2006-05-11 08:12:54 +08:00
|
|
|
if ( pwfile != NULL ) {
|
|
|
|
fprintf( stderr, "Option -s incompatible with -T\n" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
} else if ( newpw != NULL ) {
|
|
|
|
fprintf( stderr, "New password already provided\n" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
} else {
|
2004-02-27 03:03:41 +08:00
|
|
|
char* p;
|
2006-08-29 09:36:52 +08:00
|
|
|
newpw = ch_strdup( optarg );
|
2004-02-27 03:03:41 +08:00
|
|
|
|
|
|
|
for( p = optarg; *p != '\0'; p++ ) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2006-05-11 08:12:54 +08:00
|
|
|
}
|
|
|
|
break;
|
2004-02-27 03:03:41 +08:00
|
|
|
|
|
|
|
case 'T': /* password file */
|
2006-05-11 08:12:54 +08:00
|
|
|
if ( pwfile != NULL ) {
|
|
|
|
fprintf( stderr, "Password file already provided\n" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
} else if ( newpw != NULL ) {
|
|
|
|
fprintf( stderr, "Option -T incompatible with -s/-g\n" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
}
|
2004-02-27 03:03:41 +08:00
|
|
|
pwfile = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u': /* RFC2307 userPassword */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v': /* verbose */
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage ( progname );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( argc - optind != 0 ) {
|
|
|
|
usage( progname );
|
2012-05-31 12:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SLAPD_MODULES
|
|
|
|
if ( module_init() != 0 ) {
|
|
|
|
fprintf( stderr, "%s: module_init failed\n", progname );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( modulepath && module_path(modulepath) ) {
|
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto destroy;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( moduleload && module_load(moduleload, 0, NULL) ) {
|
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto destroy;
|
|
|
|
}
|
|
|
|
#endif
|
2004-02-27 03:03:41 +08:00
|
|
|
|
|
|
|
if( pwfile != NULL ) {
|
|
|
|
if( lutil_get_filed_password( pwfile, &passwd )) {
|
2012-05-31 12:13:41 +08:00
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto destroy;
|
2004-02-27 03:03:41 +08:00
|
|
|
}
|
2006-05-11 11:09:40 +08:00
|
|
|
} else if ( BER_BVISEMPTY( &passwd )) {
|
2004-02-27 03:03:41 +08:00
|
|
|
if( newpw == NULL ) {
|
|
|
|
/* prompt for new password */
|
|
|
|
char *cknewpw;
|
2006-08-29 09:36:52 +08:00
|
|
|
newpw = ch_strdup(getpassphrase("New password: "));
|
2004-02-27 03:03:41 +08:00
|
|
|
cknewpw = getpassphrase("Re-enter new password: ");
|
|
|
|
|
|
|
|
if( strcmp( newpw, cknewpw )) {
|
|
|
|
fprintf( stderr, "Password values do not match\n" );
|
2012-05-31 12:13:41 +08:00
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto destroy;
|
2004-02-27 03:03:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
passwd.bv_val = newpw;
|
|
|
|
passwd.bv_len = strlen(passwd.bv_val);
|
2006-05-11 11:09:40 +08:00
|
|
|
} else {
|
2006-05-11 22:36:20 +08:00
|
|
|
hash = passwd;
|
|
|
|
goto print_pw;
|
2004-02-27 03:03:41 +08:00
|
|
|
}
|
|
|
|
|
2004-03-03 06:11:31 +08:00
|
|
|
lutil_passwd_hash( &passwd, scheme, &hash, &text );
|
|
|
|
if( hash.bv_val == NULL ) {
|
2004-02-27 03:03:41 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
"Password generation failed for scheme %s: %s\n",
|
|
|
|
scheme, text ? text : "" );
|
2012-05-31 12:13:41 +08:00
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto destroy;
|
2004-02-27 03:03:41 +08:00
|
|
|
}
|
|
|
|
|
2004-03-03 06:11:31 +08:00
|
|
|
if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
|
2004-02-27 03:03:41 +08:00
|
|
|
fprintf( stderr, "Password verification failed. %s\n",
|
|
|
|
text ? text : "" );
|
2012-05-31 12:13:41 +08:00
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto destroy;
|
2004-02-27 03:03:41 +08:00
|
|
|
}
|
|
|
|
|
2006-05-11 22:36:20 +08:00
|
|
|
print_pw:;
|
|
|
|
printf( "%s%s" , hash.bv_val, newline );
|
2012-05-31 12:13:41 +08:00
|
|
|
|
|
|
|
destroy:;
|
|
|
|
#ifdef SLAPD_MODULES
|
|
|
|
module_kill();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return rc;
|
2004-02-27 03:03:41 +08:00
|
|
|
}
|