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