2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2013 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;
43 static char *modulepath = NULL;
44 static char *moduleload = NULL;
50 "Usage: %s [options]\n"
51 " -c format\tcrypt(3) salt format\n"
52 " -g\t\tgenerate random password\n"
53 " -h hash\tpassword scheme\n"
54 " -n\t\tomit trailing newline\n"
55 " -o <opt>[=val] specify an option with a(n optional) value\n"
56 " \tmodule-path=<pathspec>\n"
57 " \tmodule-load=<filename>\n"
58 " -s secret\tnew password\n"
59 " -u\t\tgenerate RFC2307 values (default)\n"
60 " -v\t\tincrease verbosity\n"
61 " -T file\tread file for new password\n"
68 parse_slappasswdopt( void )
73 p = strchr( optarg, '=' );
79 if ( strncasecmp( optarg, "module-path", len ) == 0 ) {
81 ch_free( modulepath );
82 modulepath = ch_strdup( p );
84 } else if ( strncasecmp( optarg, "module-load", len ) == 0 ) {
86 ch_free( moduleload );
87 moduleload = ch_strdup( p );
97 slappasswd( int argc, char *argv[] )
99 int rc = EXIT_SUCCESS;
100 #ifdef LUTIL_SHA1_BYTES
101 char *default_scheme = "{SSHA}";
103 char *default_scheme = "{SMD5}";
105 char *scheme = default_scheme;
110 const char *progname = "slappasswd";
113 char *newline = "\n";
114 struct berval passwd = BER_BVNULL;
118 /* tools default to "none", so that at least LDAP_DEBUG_ANY
119 * messages show up; use -d 0 to reset */
120 slap_debug = LDAP_DEBUG_NONE;
124 while( (i = getopt( argc, argv,
125 "c:d:gh:no:s:T:vu" )) != EOF )
128 case 'c': /* crypt salt format */
130 lutil_salt_format( optarg );
133 case 'g': /* new password (generate) */
134 if ( pwfile != NULL ) {
135 fprintf( stderr, "Option -g incompatible with -T\n" );
138 } else if ( newpw != NULL ) {
139 fprintf( stderr, "New password already provided\n" );
142 } else if ( lutil_passwd_generate( &passwd, 8 )) {
143 fprintf( stderr, "Password generation failed\n" );
148 case 'h': /* scheme */
149 if ( scheme != default_scheme ) {
150 fprintf( stderr, "Scheme already provided\n" );
154 scheme = ch_strdup( optarg );
163 if ( parse_slappasswdopt() ) {
168 case 's': /* new password (secret) */
169 if ( pwfile != NULL ) {
170 fprintf( stderr, "Option -s incompatible with -T\n" );
173 } else if ( newpw != NULL ) {
174 fprintf( stderr, "New password already provided\n" );
179 newpw = ch_strdup( optarg );
181 for( p = optarg; *p != '\0'; p++ ) {
187 case 'T': /* password file */
188 if ( pwfile != NULL ) {
189 fprintf( stderr, "Password file already provided\n" );
192 } else if ( newpw != NULL ) {
193 fprintf( stderr, "Option -T incompatible with -s/-g\n" );
200 case 'u': /* RFC2307 userPassword */
203 case 'v': /* verbose */
212 if( argc - optind != 0 ) {
217 if ( module_init() != 0 ) {
218 fprintf( stderr, "%s: module_init failed\n", progname );
222 if ( modulepath && module_path(modulepath) ) {
227 if ( moduleload && module_load(moduleload, 0, NULL) ) {
233 if( pwfile != NULL ) {
234 if( lutil_get_filed_password( pwfile, &passwd )) {
238 } else if ( BER_BVISEMPTY( &passwd )) {
239 if( newpw == NULL ) {
240 /* prompt for new password */
242 newpw = ch_strdup(getpassphrase("New password: "));
243 cknewpw = getpassphrase("Re-enter new password: ");
245 if( strcmp( newpw, cknewpw )) {
246 fprintf( stderr, "Password values do not match\n" );
252 passwd.bv_val = newpw;
253 passwd.bv_len = strlen(passwd.bv_val);
259 lutil_passwd_hash( &passwd, scheme, &hash, &text );
260 if( hash.bv_val == NULL ) {
262 "Password generation failed for scheme %s: %s\n",
263 scheme, text ? text : "" );
268 if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
269 fprintf( stderr, "Password verification failed. %s\n",
276 printf( "%s%s" , hash.bv_val, newline );