2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 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"
42 static int verbose = 0;
48 "Usage: %s [options]\n"
49 " -c format\tcrypt(3) salt format\n"
50 " -g\t\tgenerate random password\n"
51 " -h hash\tpassword scheme\n"
52 " -n\t\tomit trailing newline\n"
53 " -s secret\tnew password\n"
54 " -u\t\tgenerate RFC2307 values (default)\n"
55 " -v\t\tincrease verbosity\n"
56 " -T file\tread file for new password\n"
63 slappasswd( int argc, char *argv[] )
65 #ifdef LUTIL_SHA1_BYTES
66 char *default_scheme = "{SSHA}";
68 char *default_scheme = "{SMD5}";
70 char *scheme = default_scheme;
75 const char *progname = "slappasswd";
79 struct berval passwd = BER_BVNULL;
82 while( (i = getopt( argc, argv,
83 "c:d:gh:ns:T:vu" )) != EOF )
86 case 'c': /* crypt salt format */
88 lutil_salt_format( optarg );
91 case 'g': /* new password (generate) */
92 if ( pwfile != NULL ) {
93 fprintf( stderr, "Option -g incompatible with -T\n" );
96 } else if ( newpw != NULL ) {
97 fprintf( stderr, "New password already provided\n" );
100 } else if ( lutil_passwd_generate( &passwd, 8 )) {
101 fprintf( stderr, "Password generation failed\n" );
106 case 'h': /* scheme */
107 if ( scheme != default_scheme ) {
108 fprintf( stderr, "Scheme already provided\n" );
112 scheme = ch_strdup( optarg );
120 case 's': /* new password (secret) */
121 if ( pwfile != NULL ) {
122 fprintf( stderr, "Option -s incompatible with -T\n" );
125 } else if ( newpw != NULL ) {
126 fprintf( stderr, "New password already provided\n" );
131 newpw = ch_strdup( optarg );
133 for( p = optarg; *p != '\0'; p++ ) {
139 case 'T': /* password file */
140 if ( pwfile != NULL ) {
141 fprintf( stderr, "Password file already provided\n" );
144 } else if ( newpw != NULL ) {
145 fprintf( stderr, "Option -T incompatible with -s/-g\n" );
152 case 'u': /* RFC2307 userPassword */
155 case 'v': /* verbose */
164 if( argc - optind != 0 ) {
168 if( pwfile != NULL ) {
169 if( lutil_get_filed_password( pwfile, &passwd )) {
172 } else if ( BER_BVISEMPTY( &passwd )) {
173 if( newpw == NULL ) {
174 /* prompt for new password */
176 newpw = ch_strdup(getpassphrase("New password: "));
177 cknewpw = getpassphrase("Re-enter new password: ");
179 if( strcmp( newpw, cknewpw )) {
180 fprintf( stderr, "Password values do not match\n" );
185 passwd.bv_val = newpw;
186 passwd.bv_len = strlen(passwd.bv_val);
192 lutil_passwd_hash( &passwd, scheme, &hash, &text );
193 if( hash.bv_val == NULL ) {
195 "Password generation failed for scheme %s: %s\n",
196 scheme, text ? text : "" );
200 if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
201 fprintf( stderr, "Password verification failed. %s\n",
207 printf( "%s%s" , hash.bv_val, newline );