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"
49 " -g\t\tgenerate random password\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 #ifdef LUTIL_SHA1_BYTES
64 char *default_scheme = "{SSHA}";
66 char *default_scheme = "{SMD5}";
68 char *scheme = default_scheme;
73 const char *progname = "slappasswd";
76 struct berval passwd = BER_BVNULL;
79 while( (i = getopt( argc, argv,
80 "c:d:gh:s:T:vu" )) != EOF )
83 case 'c': /* crypt salt format */
85 lutil_salt_format( optarg );
88 case 'g': /* new password (generate) */
89 if ( pwfile != NULL ) {
90 fprintf( stderr, "Option -g incompatible with -T\n" );
93 } else if ( newpw != NULL ) {
94 fprintf( stderr, "New password already provided\n" );
97 } else if ( lutil_passwd_generate( &passwd, 8 )) {
98 fprintf( stderr, "Password generation failed\n" );
103 case 'h': /* scheme */
104 if ( scheme != default_scheme ) {
105 fprintf( stderr, "Scheme already provided\n" );
109 scheme = strdup( optarg );
113 case 's': /* new password (secret) */
114 if ( pwfile != NULL ) {
115 fprintf( stderr, "Option -s incompatible with -T\n" );
118 } else if ( newpw != NULL ) {
119 fprintf( stderr, "New password already provided\n" );
124 newpw = strdup( optarg );
126 for( p = optarg; *p != '\0'; p++ ) {
132 case 'T': /* password file */
133 if ( pwfile != NULL ) {
134 fprintf( stderr, "Password file already provided\n" );
137 } else if ( newpw != NULL ) {
138 fprintf( stderr, "Option -T incompatible with -s/-g\n" );
145 case 'u': /* RFC2307 userPassword */
148 case 'v': /* verbose */
157 if( argc - optind != 0 ) {
161 if( pwfile != NULL ) {
162 if( lutil_get_filed_password( pwfile, &passwd )) {
165 } else if ( BER_BVISEMPTY( &passwd )) {
166 if( newpw == NULL ) {
167 /* prompt for new password */
169 newpw = strdup(getpassphrase("New password: "));
170 cknewpw = getpassphrase("Re-enter new password: ");
172 if( strcmp( newpw, cknewpw )) {
173 fprintf( stderr, "Password values do not match\n" );
178 passwd.bv_val = newpw;
179 passwd.bv_len = strlen(passwd.bv_val);
181 /* Omit trailing newline so it may be directed to a pwfile */
182 printf( "%s", passwd.bv_val );
186 lutil_passwd_hash( &passwd, scheme, &hash, &text );
187 if( hash.bv_val == NULL ) {
189 "Password generation failed for scheme %s: %s\n",
190 scheme, text ? text : "" );
194 if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
195 fprintf( stderr, "Password verification failed. %s\n",
200 printf( "%s\n" , hash.bv_val );