2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 The OpenLDAP Foundation.
5 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Kurt Zeilenga for inclusion
18 * in OpenLDAP Software.
25 #include <ac/stdlib.h>
28 #include <ac/signal.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
32 #include <ac/unistd.h>
37 #include <lutil_sha1.h>
39 #include "ldap_defaults.h"
41 static int verbose = 0;
47 "Usage: %s [options]\n"
48 " -c format\tcrypt(3) salt format\n"
50 " -h hash\tpassword scheme\n"
51 " -s secret\tnew password\n"
52 " -u\t\tgenerate RFC2307 values (default)\n"
53 " -v\t\tincrease verbosity\n"
54 " -T file\tread file for new password\n"
61 slappasswd( int argc, char *argv[] )
63 char *cleartext_scheme = "{CLEARTEXT}";
64 #ifdef LUTIL_SHA1_BYTES
65 char *default_scheme = "{SSHA}";
67 char *default_scheme = "{SMD5}";
69 char *scheme = default_scheme;
74 const char *progname = "slappasswd";
80 while( (i = getopt( argc, argv,
81 "c:d:gh:s:T:vu" )) != EOF )
84 case 'c': /* crypt salt format */
86 lutil_salt_format( optarg );
89 case 'g': /* new password (generate) */
90 if ( pwfile != NULL ) {
91 fprintf( stderr, "Option -s incompatible with -T\n" );
94 } else if ( newpw != NULL ) {
95 fprintf( stderr, "New password already provided\n" );
98 } else if ( scheme != default_scheme && strcmp( scheme, cleartext_scheme ) != 0 ) {
99 fprintf( stderr, "Option -g incompatible with scheme \"%s\"\n", scheme );
103 struct berval p = BER_BVNULL;
105 lutil_passwd_generate( &p, 8 );
109 scheme = cleartext_scheme;
113 case 'h': /* scheme */
114 if ( scheme == cleartext_scheme ) {
115 if ( strcmp( optarg, cleartext_scheme ) != 0 ) {
116 fprintf( stderr, "Option -h incompatible with -g\n" );
120 } else if ( scheme != default_scheme ) {
121 fprintf( stderr, "Scheme already provided\n" );
125 scheme = strdup( optarg );
129 case 's': /* new password (secret) */
130 if ( pwfile != NULL ) {
131 fprintf( stderr, "Option -s incompatible with -T\n" );
134 } else if ( newpw != NULL ) {
135 fprintf( stderr, "New password already provided\n" );
140 newpw = strdup( optarg );
142 for( p = optarg; *p != '\0'; p++ ) {
148 case 'T': /* password file */
149 if ( pwfile != NULL ) {
150 fprintf( stderr, "Password file already provided\n" );
153 } else if ( newpw != NULL ) {
154 fprintf( stderr, "Option -T incompatible with -s/-g\n" );
161 case 'u': /* RFC2307 userPassword */
164 case 'v': /* verbose */
173 if( argc - optind != 0 ) {
177 if( pwfile != NULL ) {
178 if( lutil_get_filed_password( pwfile, &passwd )) {
182 if( newpw == NULL ) {
183 /* prompt for new password */
185 newpw = strdup(getpassphrase("New password: "));
186 cknewpw = getpassphrase("Re-enter new password: ");
188 if( strcmp( newpw, cknewpw )) {
189 fprintf( stderr, "Password values do not match\n" );
194 passwd.bv_val = newpw;
195 passwd.bv_len = strlen(passwd.bv_val);
198 lutil_passwd_hash( &passwd, scheme, &hash, &text );
199 if( hash.bv_val == NULL ) {
201 "Password generation failed for scheme %s: %s\n",
202 scheme, text ? text : "" );
206 if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
207 fprintf( stderr, "Password verification failed. %s\n",
212 printf( "%s\n" , hash.bv_val );