slappasswd.c 6.18 KB
Newer Older
1
2
3
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1998-2021 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

41
#include "slap.h"
42
43
#include "slap-config.h"
#include "slapcommon.h"
44

45
46
static char	*modulepath = NULL;
static char	*moduleload = NULL;
47
48
static int	moduleargc = 0;
static char	**moduleargv = NULL;
49
50
51
52
53
54

static void
usage(const char *s)
{
	fprintf(stderr,
		"Usage: %s [options]\n"
55
		"  -c format\tcrypt(3) salt format\n"
56
		"  -g\t\tgenerate random password\n"
57
		"  -h hash\tpassword scheme\n"
58
		"  -n\t\tomit trailing newline\n"
59
60
61
		"  -o <opt>[=val] specify an option with a(n optional) value\n"
		"  \tmodule-path=<pathspec>\n"
		"  \tmodule-load=<filename>\n"
62
63
64
65
66
67
68
69
70
		"  -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 );
}

71
72
73
74
75
76
77
78
79
80
81
82
83
static int
parse_slappasswdopt( void )
{
	size_t	len = 0;
	char	*p;

	p = strchr( optarg, '=' );
	if ( p != NULL ) {
		len = p - optarg;
		p++;
	}

	if ( strncasecmp( optarg, "module-path", len ) == 0 ) {
84
		modulepath = p;
85
86

	} else if ( strncasecmp( optarg, "module-load", len ) == 0 ) {
87
88
89
90
91
92
93
94
95
96
97
		ConfigArgs c = { .line = p };

		if ( config_fp_parse_line( &c ) ) {
			return -1;
		}
		moduleload = c.argv[0];

		moduleargc = c.argc - 1;
		if ( moduleargc ) {
			moduleargv = c.argv+1;
		}
98
99
100
101
102
103
104
105

	} else {
		return -1;
	}

	return 0;
}

106
107
108
int
slappasswd( int argc, char *argv[] )
{
109
	int rc = EXIT_SUCCESS;
110
#ifdef LUTIL_SHA1_BYTES
111
	char	*default_scheme = "{SSHA}";
112
#else
113
	char	*default_scheme = "{SMD5}";
114
#endif
115
	char	*scheme = default_scheme;
116

117
118
119
120
121
122
	char	*newpw = NULL;
	char	*pwfile = NULL;
	const char *text;
	const char *progname = "slappasswd";

	int		i;
123
	char		*newline = "\n";
124
	struct berval passwd = BER_BVNULL;
125
	struct berval hash = BER_BVNULL;
126

127
128
129
130
131
132
133
#ifdef LDAP_DEBUG
	/* tools default to "none", so that at least LDAP_DEBUG_ANY
	 * messages show up; use -d 0 to reset */
	slap_debug = LDAP_DEBUG_NONE;
#endif
	ldap_syslog = 0;

134
	while( (i = getopt( argc, argv,
135
		"c:d:gh:no:s:T:vu" )) != EOF )
136
137
138
139
140
141
142
	{
		switch (i) {
		case 'c':	/* crypt salt format */
			scheme = "{CRYPT}";
			lutil_salt_format( optarg );
			break;

143
144
		case 'g':	/* new password (generate) */
			if ( pwfile != NULL ) {
145
				fprintf( stderr, "Option -g incompatible with -T\n" );
146
147
148
149
150
151
				return EXIT_FAILURE;

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

152
153
			} else if ( lutil_passwd_generate( &passwd, 8 )) {
				fprintf( stderr, "Password generation failed\n" );
154
155
156
157
				return EXIT_FAILURE;
			}
			break;

158
		case 'h':	/* scheme */
159
			if ( scheme != default_scheme ) {
160
161
162
163
				fprintf( stderr, "Scheme already provided\n" );
				return EXIT_FAILURE;

			} else {
164
				scheme = optarg;
165
			}
166
167
			break;

168
169
170
171
		case 'n':
			newline = "";
			break;

172
173
174
175
176
177
		case 'o':
			if ( parse_slappasswdopt() ) {
				usage ( progname );
			}
			break;

178
		case 's':	/* new password (secret) */
179
180
181
182
183
184
185
186
187
			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 {
188
				char* p;
Howard Chu's avatar
Howard Chu committed
189
				newpw = ch_strdup( optarg );
190
191
192
193

				for( p = optarg; *p != '\0'; p++ ) {
					*p = '\0';
				}
194
195
			}
			break;
196
197

		case 'T':	/* password file */
198
199
200
201
202
203
204
205
206
			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;

			}
207
208
209
210
211
212
213
214
215
216
217
218
219
220
			pwfile = optarg;
			break;

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

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

		default:
			usage ( progname );
		}
	}
221
	slapTool = SLAPPASSWD;
222
223
224

	if( argc - optind != 0 ) {
		usage( progname );
225
226
227
228
229
230
231
232
233
234
235
236
237
	}

#ifdef SLAPD_MODULES
	if ( module_init() != 0 ) {
		fprintf( stderr, "%s: module_init failed\n", progname );
		return EXIT_FAILURE;
	}

	if ( modulepath && module_path(modulepath) ) {
		rc = EXIT_FAILURE;
		goto destroy;
	}

238
	if ( moduleload && module_load(moduleload, moduleargc, moduleargv) ) {
239
240
241
242
		rc = EXIT_FAILURE;
		goto destroy;
	}
#endif
243
244
245

	if( pwfile != NULL ) {
		if( lutil_get_filed_password( pwfile, &passwd )) {
246
247
			rc = EXIT_FAILURE;
			goto destroy;
248
		}
249
	} else if ( BER_BVISEMPTY( &passwd )) {
250
251
252
		if( newpw == NULL ) {
			/* prompt for new password */
			char *cknewpw;
Howard Chu's avatar
Howard Chu committed
253
			newpw = ch_strdup(getpassphrase("New password: "));
254
255
256
257
			cknewpw = getpassphrase("Re-enter new password: ");
	
			if( strcmp( newpw, cknewpw )) {
				fprintf( stderr, "Password values do not match\n" );
258
259
				rc = EXIT_FAILURE;
				goto destroy;
260
261
262
263
264
			}
		}

		passwd.bv_val = newpw;
		passwd.bv_len = strlen(passwd.bv_val);
265
	} else {
266
267
		hash = passwd;
		goto print_pw;
268
269
	}

270
	lutil_passwd_hash( &passwd, scheme, &hash, &text );
271
	if ( BER_BVISNULL( &hash ) ) {
272
273
274
		fprintf( stderr,
			"Password generation failed for scheme %s: %s\n",
			scheme, text ? text : "" );
275
276
		rc = EXIT_FAILURE;
		goto destroy;
277
278
	}

279
	if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
280
281
		fprintf( stderr, "Password verification failed. %s\n",
			text ? text : "" );
282
283
		rc = EXIT_FAILURE;
		goto destroy;
284
285
	}

286
287
print_pw:;
	printf( "%s%s" , hash.bv_val, newline );
288
289
290
291
292

destroy:;
#ifdef SLAPD_MODULES
	module_kill();
#endif
293
294
295
296
297
298
	if ( !BER_BVISNULL( &hash ) ) {
		ber_memfree( hash.bv_val );
	}
	if ( passwd.bv_val != hash.bv_val && !BER_BVISNULL( &passwd ) ) {
		ber_memfree( passwd.bv_val );
	}
299
300

	return rc;
301
}