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 " -n\t\tomit trailing newline\n"
52 " -s secret\tnew password\n"
53 " -u\t\tgenerate RFC2307 values (default)\n"
54 " -v\t\tincrease verbosity\n"
55 " -T file\tread file for new password\n"
62 slappasswd( int argc, char *argv[] )
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";
78 struct berval passwd = BER_BVNULL;
81 while( (i = getopt( argc, argv,
82 "c:d:gh:ns:T:vu" )) != EOF )
85 case 'c': /* crypt salt format */
87 lutil_salt_format( optarg );
90 case 'g': /* new password (generate) */
91 if ( pwfile != NULL ) {
92 fprintf( stderr, "Option -g incompatible with -T\n" );
95 } else if ( newpw != NULL ) {
96 fprintf( stderr, "New password already provided\n" );
99 } else if ( lutil_passwd_generate( &passwd, 8 )) {
100 fprintf( stderr, "Password generation failed\n" );
105 case 'h': /* scheme */
106 if ( scheme != default_scheme ) {
107 fprintf( stderr, "Scheme already provided\n" );
111 scheme = strdup( optarg );
119 case 's': /* new password (secret) */
120 if ( pwfile != NULL ) {
121 fprintf( stderr, "Option -s incompatible with -T\n" );
124 } else if ( newpw != NULL ) {
125 fprintf( stderr, "New password already provided\n" );
130 newpw = strdup( optarg );
132 for( p = optarg; *p != '\0'; p++ ) {
138 case 'T': /* password file */
139 if ( pwfile != NULL ) {
140 fprintf( stderr, "Password file already provided\n" );
143 } else if ( newpw != NULL ) {
144 fprintf( stderr, "Option -T incompatible with -s/-g\n" );
151 case 'u': /* RFC2307 userPassword */
154 case 'v': /* verbose */
163 if( argc - optind != 0 ) {
167 if( pwfile != NULL ) {
168 if( lutil_get_filed_password( pwfile, &passwd )) {
171 } else if ( BER_BVISEMPTY( &passwd )) {
172 if( newpw == NULL ) {
173 /* prompt for new password */
175 newpw = strdup(getpassphrase("New password: "));
176 cknewpw = getpassphrase("Re-enter new password: ");
178 if( strcmp( newpw, cknewpw )) {
179 fprintf( stderr, "Password values do not match\n" );
184 passwd.bv_val = newpw;
185 passwd.bv_len = strlen(passwd.bv_val);
191 lutil_passwd_hash( &passwd, scheme, &hash, &text );
192 if( hash.bv_val == NULL ) {
194 "Password generation failed for scheme %s: %s\n",
195 scheme, text ? text : "" );
199 if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
200 fprintf( stderr, "Password verification failed. %s\n",
206 printf( "%s%s" , hash.bv_val, newline );