1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2021 The OpenLDAP Foundation.
5  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Kurt Zeilenga for inclusion
18  * in OpenLDAP Software.
19  */
20 
21 #include "portable.h"
22 
23 #include <stdio.h>
24 
25 #include <ac/stdlib.h>
26 
27 #include <ac/ctype.h>
28 #include <ac/signal.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
31 #include <ac/time.h>
32 #include <ac/unistd.h>
33 
34 #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 #include "slap-config.h"
43 #include "slapcommon.h"
44 
45 static char	*modulepath = NULL;
46 static char	*moduleload = NULL;
47 static int	moduleargc = 0;
48 static char	**moduleargv = NULL;
49 
50 static void
usage(const char * s)51 usage(const char *s)
52 {
53 	fprintf(stderr,
54 		"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 		"  -o <opt>[=val] specify an option with a(n optional) value\n"
60 		"  \tmodule-path=<pathspec>\n"
61 		"  \tmodule-load=<filename>\n"
62 		"  -s secret\tnew password\n"
63 		"  -u\t\tgenerate RFC2307 values (default)\n"
64 		"  -v\t\tincrease verbosity\n"
65 		"  -T file\tread file for new password\n"
66 		, s );
67 
68 	exit( EXIT_FAILURE );
69 }
70 
71 static int
parse_slappasswdopt(void)72 parse_slappasswdopt( void )
73 {
74 	size_t	len = 0;
75 	char	*p;
76 
77 	p = strchr( optarg, '=' );
78 	if ( p != NULL ) {
79 		len = p - optarg;
80 		p++;
81 	}
82 
83 	if ( strncasecmp( optarg, "module-path", len ) == 0 ) {
84 		modulepath = p;
85 
86 	} else if ( strncasecmp( optarg, "module-load", len ) == 0 ) {
87 		ConfigArgs c = { .line = p };
88 
89 		if ( config_fp_parse_line( &c ) ) {
90 			return -1;
91 		}
92 		moduleload = c.argv[0];
93 
94 		moduleargc = c.argc - 1;
95 		if ( moduleargc ) {
96 			moduleargv = c.argv+1;
97 		}
98 
99 	} else {
100 		return -1;
101 	}
102 
103 	return 0;
104 }
105 
106 int
slappasswd(int argc,char * argv[])107 slappasswd( int argc, char *argv[] )
108 {
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 	char	*newpw = NULL;
118 	char	*pwfile = NULL;
119 	const char *text;
120 	const char *progname = "slappasswd";
121 
122 	int		i;
123 	char		*newline = "\n";
124 	struct berval passwd = BER_BVNULL;
125 	struct berval hash = BER_BVNULL;
126 
127 #ifdef LDAP_DEBUG
128 	/* tools default to "none", so that at least LDAP_DEBUG_ANY
129 	 * messages show up; use -d 0 to reset */
130 	slap_debug = LDAP_DEBUG_NONE;
131 #endif
132 	ldap_syslog = 0;
133 
134 	while( (i = getopt( argc, argv,
135 		"c:d:gh:no:s:T:vu" )) != EOF )
136 	{
137 		switch (i) {
138 		case 'c':	/* crypt salt format */
139 			scheme = "{CRYPT}";
140 			lutil_salt_format( optarg );
141 			break;
142 
143 		case 'g':	/* new password (generate) */
144 			if ( pwfile != NULL ) {
145 				fprintf( stderr, "Option -g incompatible with -T\n" );
146 				return EXIT_FAILURE;
147 
148 			} else if ( newpw != NULL ) {
149 				fprintf( stderr, "New password already provided\n" );
150 				return EXIT_FAILURE;
151 
152 			} else if ( lutil_passwd_generate( &passwd, 8 )) {
153 				fprintf( stderr, "Password generation failed\n" );
154 				return EXIT_FAILURE;
155 			}
156 			break;
157 
158 		case 'h':	/* scheme */
159 			if ( scheme != default_scheme ) {
160 				fprintf( stderr, "Scheme already provided\n" );
161 				return EXIT_FAILURE;
162 
163 			} else {
164 				scheme = optarg;
165 			}
166 			break;
167 
168 		case 'n':
169 			newline = "";
170 			break;
171 
172 		case 'o':
173 			if ( parse_slappasswdopt() ) {
174 				usage ( progname );
175 			}
176 			break;
177 
178 		case 's':	/* new password (secret) */
179 			if ( pwfile != NULL ) {
180 				fprintf( stderr, "Option -s incompatible with -T\n" );
181 				return EXIT_FAILURE;
182 
183 			} else if ( newpw != NULL ) {
184 				fprintf( stderr, "New password already provided\n" );
185 				return EXIT_FAILURE;
186 
187 			} else {
188 				char* p;
189 				newpw = ch_strdup( optarg );
190 
191 				for( p = optarg; *p != '\0'; p++ ) {
192 					*p = '\0';
193 				}
194 			}
195 			break;
196 
197 		case 'T':	/* password file */
198 			if ( pwfile != NULL ) {
199 				fprintf( stderr, "Password file already provided\n" );
200 				return EXIT_FAILURE;
201 
202 			} else if ( newpw != NULL ) {
203 				fprintf( stderr, "Option -T incompatible with -s/-g\n" );
204 				return EXIT_FAILURE;
205 
206 			}
207 			pwfile = optarg;
208 			break;
209 
210 		case 'u':	/* RFC2307 userPassword */
211 			break;
212 
213 		case 'v':	/* verbose */
214 			verbose++;
215 			break;
216 
217 		default:
218 			usage ( progname );
219 		}
220 	}
221 	slapTool = SLAPPASSWD;
222 
223 	if( argc - optind != 0 ) {
224 		usage( progname );
225 	}
226 
227 #ifdef SLAPD_MODULES
228 	if ( module_init() != 0 ) {
229 		fprintf( stderr, "%s: module_init failed\n", progname );
230 		return EXIT_FAILURE;
231 	}
232 
233 	if ( modulepath && module_path(modulepath) ) {
234 		rc = EXIT_FAILURE;
235 		goto destroy;
236 	}
237 
238 	if ( moduleload && module_load(moduleload, moduleargc, moduleargv) ) {
239 		rc = EXIT_FAILURE;
240 		goto destroy;
241 	}
242 #endif
243 
244 	if( pwfile != NULL ) {
245 		if( lutil_get_filed_password( pwfile, &passwd )) {
246 			rc = EXIT_FAILURE;
247 			goto destroy;
248 		}
249 	} else if ( BER_BVISEMPTY( &passwd )) {
250 		if( newpw == NULL ) {
251 			/* prompt for new password */
252 			char *cknewpw;
253 			newpw = ch_strdup(getpassphrase("New password: "));
254 			cknewpw = getpassphrase("Re-enter new password: ");
255 
256 			if( strcmp( newpw, cknewpw )) {
257 				fprintf( stderr, "Password values do not match\n" );
258 				rc = EXIT_FAILURE;
259 				goto destroy;
260 			}
261 		}
262 
263 		passwd.bv_val = newpw;
264 		passwd.bv_len = strlen(passwd.bv_val);
265 	} else {
266 		hash = passwd;
267 		goto print_pw;
268 	}
269 
270 	lutil_passwd_hash( &passwd, scheme, &hash, &text );
271 	if ( BER_BVISNULL( &hash ) ) {
272 		fprintf( stderr,
273 			"Password generation failed for scheme %s: %s\n",
274 			scheme, text ? text : "" );
275 		rc = EXIT_FAILURE;
276 		goto destroy;
277 	}
278 
279 	if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
280 		fprintf( stderr, "Password verification failed. %s\n",
281 			text ? text : "" );
282 		rc = EXIT_FAILURE;
283 		goto destroy;
284 	}
285 
286 print_pw:;
287 	printf( "%s%s" , hash.bv_val, newline );
288 
289 destroy:;
290 #ifdef SLAPD_MODULES
291 	module_kill();
292 #endif
293 	if ( !BER_BVISNULL( &hash ) ) {
294 		ber_memfree( hash.bv_val );
295 	}
296 	if ( passwd.bv_val != hash.bv_val && !BER_BVISNULL( &passwd ) ) {
297 		ber_memfree( passwd.bv_val );
298 	}
299 
300 	return rc;
301 }
302