slappasswd.c 4.48 KB
Newer Older
1
2
3
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2007 The OpenLDAP Foundation.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Kurt Zeilenga for inclusion
 * in OpenLDAP Software.
 */

#include "portable.h"

#include <stdio.h>

#include <ac/stdlib.h>

#include <ac/ctype.h>
#include <ac/signal.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
#include <ac/unistd.h>

#include <ldap.h>
35
#include <lber_pvt.h>
36
#include <lutil.h>
37
#include <lutil_sha1.h>
38
39

#include "ldap_defaults.h"
40
#include "slap.h"
41
42
43
44
45
46
47
48

static int	verbose = 0;

static void
usage(const char *s)
{
	fprintf(stderr,
		"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
54
55
56
57
58
59
60
61
62
63
64
		"  -s secret\tnew password\n"
		"  -u\t\tgenerate RFC2307 values (default)\n"
		"  -v\t\tincrease verbosity\n"
		"  -T file\tread file for new password\n"
		, s );

	exit( EXIT_FAILURE );
}

int
slappasswd( int argc, char *argv[] )
{
65
#ifdef LUTIL_SHA1_BYTES
66
	char	*default_scheme = "{SSHA}";
67
#else
68
	char	*default_scheme = "{SMD5}";
69
#endif
70
	char	*scheme = default_scheme;
71

72
73
74
75
76
77
	char	*newpw = NULL;
	char	*pwfile = NULL;
	const char *text;
	const char *progname = "slappasswd";

	int		i;
78
	char		*newline = "\n";
79
	struct berval passwd = BER_BVNULL;
80
	struct berval hash;
81
82

	while( (i = getopt( argc, argv,
83
		"c:d:gh:ns:T:vu" )) != EOF )
84
85
86
87
88
89
90
	{
		switch (i) {
		case 'c':	/* crypt salt format */
			scheme = "{CRYPT}";
			lutil_salt_format( optarg );
			break;

91
92
		case 'g':	/* new password (generate) */
			if ( pwfile != NULL ) {
93
				fprintf( stderr, "Option -g incompatible with -T\n" );
94
95
96
97
98
99
				return EXIT_FAILURE;

			} else if ( newpw != NULL ) {
				fprintf( stderr, "New password already provided\n" );
				return EXIT_FAILURE;

100
101
			} else if ( lutil_passwd_generate( &passwd, 8 )) {
				fprintf( stderr, "Password generation failed\n" );
102
103
104
105
				return EXIT_FAILURE;
			}
			break;

106
		case 'h':	/* scheme */
107
			if ( scheme != default_scheme ) {
108
109
110
111
				fprintf( stderr, "Scheme already provided\n" );
				return EXIT_FAILURE;

			} else {
Howard Chu's avatar
Howard Chu committed
112
				scheme = ch_strdup( optarg );
113
			}
114
115
			break;

116
117
118
119
		case 'n':
			newline = "";
			break;

120
		case 's':	/* new password (secret) */
121
122
123
124
125
126
127
128
129
			if ( pwfile != NULL ) {
				fprintf( stderr, "Option -s incompatible with -T\n" );
				return EXIT_FAILURE;

			} else if ( newpw != NULL ) {
				fprintf( stderr, "New password already provided\n" );
				return EXIT_FAILURE;

			} else {
130
				char* p;
Howard Chu's avatar
Howard Chu committed
131
				newpw = ch_strdup( optarg );
132
133
134
135

				for( p = optarg; *p != '\0'; p++ ) {
					*p = '\0';
				}
136
137
			}
			break;
138
139

		case 'T':	/* password file */
140
141
142
143
144
145
146
147
148
			if ( pwfile != NULL ) {
				fprintf( stderr, "Password file already provided\n" );
				return EXIT_FAILURE;

			} else if ( newpw != NULL ) {
				fprintf( stderr, "Option -T incompatible with -s/-g\n" );
				return EXIT_FAILURE;

			}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
			pwfile = optarg;
			break;

		case 'u':	/* RFC2307 userPassword */
			break;

		case 'v':	/* verbose */
			verbose++;
			break;

		default:
			usage ( progname );
		}
	}

	if( argc - optind != 0 ) {
		usage( progname );
	} 

	if( pwfile != NULL ) {
		if( lutil_get_filed_password( pwfile, &passwd )) {
			return EXIT_FAILURE;
		}
172
	} else if ( BER_BVISEMPTY( &passwd )) {
173
174
175
		if( newpw == NULL ) {
			/* prompt for new password */
			char *cknewpw;
Howard Chu's avatar
Howard Chu committed
176
			newpw = ch_strdup(getpassphrase("New password: "));
177
178
179
180
181
182
183
184
185
186
			cknewpw = getpassphrase("Re-enter new password: ");
	
			if( strcmp( newpw, cknewpw )) {
				fprintf( stderr, "Password values do not match\n" );
				return EXIT_FAILURE;
			}
		}

		passwd.bv_val = newpw;
		passwd.bv_len = strlen(passwd.bv_val);
187
	} else {
188
189
		hash = passwd;
		goto print_pw;
190
191
	}

192
193
	lutil_passwd_hash( &passwd, scheme, &hash, &text );
	if( hash.bv_val == NULL ) {
194
195
196
197
198
199
		fprintf( stderr,
			"Password generation failed for scheme %s: %s\n",
			scheme, text ? text : "" );
		return EXIT_FAILURE;
	}

200
	if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
201
202
203
204
205
		fprintf( stderr, "Password verification failed. %s\n",
			text ? text : "" );
		return EXIT_FAILURE;
	}

206
207
print_pw:;
	printf( "%s%s" , hash.bv_val, newline );
208
209
	return EXIT_SUCCESS;
}