openldap/servers/slapd/slappasswd.c
2006-05-11 00:12:54 +00:00

215 lines
4.6 KiB
C

/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 1998-2006 The OpenLDAP Foundation.
* 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>
#include <lber_pvt.h>
#include <lutil.h>
#include <lutil_sha1.h>
#include "ldap_defaults.h"
static int verbose = 0;
static void
usage(const char *s)
{
fprintf(stderr,
"Usage: %s [options]\n"
" -c format\tcrypt(3) salt format\n"
" -g\n"
" -h hash\tpassword scheme\n"
" -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 );
}
int
slappasswd( int argc, char *argv[] )
{
char *cleartext_scheme = "{CLEARTEXT}";
#ifdef LUTIL_SHA1_BYTES
char *default_scheme = "{SSHA}";
#else
char *default_scheme = "{SMD5}";
#endif
char *scheme = default_scheme;
char *newpw = NULL;
char *pwfile = NULL;
const char *text;
const char *progname = "slappasswd";
int i;
struct berval passwd;
struct berval hash;
while( (i = getopt( argc, argv,
"c:d:gh:s:T:vu" )) != EOF )
{
switch (i) {
case 'c': /* crypt salt format */
scheme = "{CRYPT}";
lutil_salt_format( optarg );
break;
case 'g': /* new password (generate) */
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 if ( scheme != default_scheme && strcmp( scheme, cleartext_scheme ) != 0 ) {
fprintf( stderr, "Option -g incompatible with scheme \"%s\"\n", scheme );
return EXIT_FAILURE;
} else {
struct berval p = BER_BVNULL;
lutil_passwd_generate( &p, 8 );
newpw = p.bv_val;
scheme = cleartext_scheme;
}
break;
case 'h': /* scheme */
if ( scheme == cleartext_scheme ) {
if ( strcmp( optarg, cleartext_scheme ) != 0 ) {
fprintf( stderr, "Option -h incompatible with -g\n" );
return EXIT_FAILURE;
}
} else if ( scheme != default_scheme ) {
fprintf( stderr, "Scheme already provided\n" );
return EXIT_FAILURE;
} else {
scheme = strdup( optarg );
}
break;
case 's': /* new password (secret) */
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 {
char* p;
newpw = strdup( optarg );
for( p = optarg; *p != '\0'; p++ ) {
*p = '\0';
}
}
break;
case 'T': /* password file */
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;
}
pwfile = optarg;
break;
case 'u': /* RFC2307 userPassword */
break;
case 'v': /* verbose */
verbose++;
break;
default:
usage ( progname );
}
}
if( argc - optind != 0 ) {
usage( progname );
}
if( pwfile != NULL ) {
if( lutil_get_filed_password( pwfile, &passwd )) {
return EXIT_FAILURE;
}
} else {
if( newpw == NULL ) {
/* prompt for new password */
char *cknewpw;
newpw = strdup(getpassphrase("New password: "));
cknewpw = getpassphrase("Re-enter new password: ");
if( strcmp( newpw, cknewpw )) {
fprintf( stderr, "Password values do not match\n" );
return EXIT_FAILURE;
}
}
passwd.bv_val = newpw;
passwd.bv_len = strlen(passwd.bv_val);
}
lutil_passwd_hash( &passwd, scheme, &hash, &text );
if( hash.bv_val == NULL ) {
fprintf( stderr,
"Password generation failed for scheme %s: %s\n",
scheme, text ? text : "" );
return EXIT_FAILURE;
}
if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
fprintf( stderr, "Password verification failed. %s\n",
text ? text : "" );
return EXIT_FAILURE;
}
printf( "%s\n" , hash.bv_val );
return EXIT_SUCCESS;
}