openldap/servers/slapd/tools/slappasswd.c

136 lines
2.5 KiB
C
Raw Normal View History

1999-12-15 21:18:50 -05:00
/* $OpenLDAP$ */
/*
2003-01-03 15:20:47 -05:00
* Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
1999-12-15 21:18:50 -05: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-15 12:09:36 -04:00
"Usage: %s [options]\n"
" -h hash\tpassword scheme\n"
1999-12-15 21:18:50 -05:00
" -s secret\tnew password\n"
2001-06-13 01:40:24 -04:00
" -c format\tcrypt(3) salt format\n"
2001-01-19 20:15:44 -05:00
" -u\t\tgenerate RFC2307 values (default)\n"
1999-12-15 21:18:50 -05:00
" -v\t\tincrease verbosity\n"
2003-06-02 18:11:54 -04:00
" -T file\tread file for new password\n"
1999-12-15 21:18:50 -05:00
, s );
exit( EXIT_FAILURE );
}
int
main( int argc, char *argv[] )
{
char *scheme = "{SSHA}";
char *newpw = NULL;
2003-03-31 01:29:59 -05:00
char *pwfile = NULL;
const char *text;
1999-12-15 21:18:50 -05:00
int i;
struct berval passwd;
struct berval *hash = NULL;
while( (i = getopt( argc, argv,
2003-03-31 01:29:59 -05:00
"c:d:h:s:T:vu" )) != EOF )
1999-12-15 21:18:50 -05:00
{
switch (i) {
2001-06-13 01:40:24 -04:00
case 'c': /* crypt salt format */
scheme = "{CRYPT}";
lutil_salt_format( optarg );
break;
case 'h': /* scheme */
2001-06-13 01:40:24 -04:00
scheme = strdup( optarg );
2000-11-06 10:53:02 -05:00
break;
1999-12-15 21:18:50 -05:00
case 's': /* new password (secret) */
{
char* p;
2001-06-13 01:40:24 -04:00
newpw = strdup( optarg );
1999-12-15 21:18:50 -05:00
2000-11-06 10:53:02 -05:00
for( p = optarg; *p != '\0'; p++ ) {
*p = '\0';
1999-12-15 21:18:50 -05:00
}
2001-06-13 01:40:24 -04:00
} break;
1999-12-15 21:18:50 -05:00
2003-03-31 01:29:59 -05:00
case 'T': /* password file */
pwfile = optarg;
break;
case 'u': /* RFC2307 userPassword */
break;
1999-12-15 21:18:50 -05:00
case 'v': /* verbose */
verbose++;
break;
default:
usage (argv[0]);
}
}
if( argc - optind != 0 ) {
usage( argv[0] );
}
2003-03-31 01:29:59 -05:00
if( pwfile != NULL ) {
if( lutil_get_filed_password( pwfile, &passwd )) {
1999-12-15 21:18:50 -05:00
return EXIT_FAILURE;
}
2003-03-31 01:29:59 -05:00
} 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;
}
}
1999-12-15 21:18:50 -05:00
2003-03-31 01:29:59 -05:00
passwd.bv_val = newpw;
passwd.bv_len = strlen(passwd.bv_val);
}
1999-12-15 21:18:50 -05:00
hash = lutil_passwd_hash( &passwd, scheme, &text );
1999-12-15 21:18:50 -05:00
if( hash == NULL || hash->bv_val == NULL ) {
fprintf( stderr, "Password generation failed. %s\n",
text ? text : "" );
1999-12-15 21:18:50 -05:00
return EXIT_FAILURE;
}
if( lutil_passwd( hash, &passwd, NULL, &text ) ) {
fprintf( stderr, "Password verification failed. %s\n",
text ? text : "" );
1999-12-15 21:18:50 -05:00
return EXIT_FAILURE;
}
printf( "%s\n" , hash->bv_val );
return EXIT_SUCCESS;
}