openldap/servers/slapd/tools/slappasswd.c

118 lines
2.0 KiB
C
Raw Normal View History

1999-12-16 10:18:50 +08:00
/* $OpenLDAP$ */
/*
2000-05-13 10:47:56 +08:00
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
1999-12-16 10:18:50 +08:00
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#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>
#include <lutil.h>
#include "ldap_defaults.h"
static int verbose = 0;
static void
usage(const char *s)
{
fprintf(stderr,
2000-06-16 00:09:36 +08:00
"Usage: %s [options]\n"
" -h hash\tpassword scheme\n"
1999-12-16 10:18:50 +08:00
" -s secret\tnew password\n"
" -u\t\tgenerate RFC2307 values\n"
1999-12-16 10:18:50 +08:00
" -v\t\tincrease verbosity\n"
, s );
exit( EXIT_FAILURE );
}
int
main( int argc, char *argv[] )
{
int rc;
char *scheme = "{SSHA}";
char *newpw = NULL;
int i;
int version = -1;
struct berval passwd;
struct berval *hash = NULL;
while( (i = getopt( argc, argv,
"d:h:s:vu" )) != EOF )
1999-12-16 10:18:50 +08:00
{
switch (i) {
case 'h': /* scheme */
1999-12-16 10:18:50 +08:00
scheme = strdup (optarg);
case 's': /* new password (secret) */
newpw = strdup (optarg);
{
char* p;
for( p = optarg; *p == '\0'; p++ ) {
*p = '\0';
1999-12-16 10:18:50 +08:00
}
}
break;
case 'u': /* RFC2307 userPassword */
break;
1999-12-16 10:18:50 +08:00
case 'v': /* verbose */
verbose++;
break;
default:
usage (argv[0]);
}
}
if( argc - optind != 0 ) {
usage( argv[0] );
}
if( newpw == NULL ) {
/* prompt for new password */
char *cknewpw;
2000-06-16 00:09:36 +08:00
newpw = strdup(getpassphrase("New password: "));
cknewpw = getpassphrase("Re-enter new password: ");
1999-12-16 10:18:50 +08:00
if( strncmp( newpw, cknewpw, strlen(newpw) )) {
fprintf( stderr, "Password values do not match\n" );
1999-12-16 10:18:50 +08:00
return EXIT_FAILURE;
}
}
passwd.bv_val = newpw;
passwd.bv_len = strlen(passwd.bv_val);
hash = lutil_passwd_hash( &passwd, scheme );
if( hash == NULL || hash->bv_val == NULL ) {
fprintf( stderr, "Password generation failed.\n");
return EXIT_FAILURE;
}
if( lutil_passwd( hash, &passwd, NULL ) ) {
fprintf( stderr, "Password verification failed.\n");
1999-12-16 10:18:50 +08:00
return EXIT_FAILURE;
}
printf( "%s\n" , hash->bv_val );
return EXIT_SUCCESS;
}