int main( int argc, char *argv[] ) { int i, j, rc; struct berval *passwd; #ifdef SLAP_AUTHPASSWD struct berval *salt; #endif struct berval bad; bad.bv_val = "bad password"; bad.bv_len = sizeof("bad password")-1; for( i= 0; hash[i]; i++ ) { for( j = 0; pw[j].bv_len; j++ ) { #ifdef SLAP_AUTHPASSWD rc = lutil_authpasswd_hash( &pw[j], &passwd, &salt, hash[i] ); if( rc ) #else passwd = lutil_passwd_hash( &pw[j], hash[i] ); if( passwd == NULL ) #endif { printf("%s generate fail: %s (%d)\n", hash[i], pw[j].bv_val, pw[j].bv_len ); continue; } #ifdef SLAP_AUTHPASSWD rc = lutil_authpasswd( &pw[j], passwd, salt, NULL ); #else rc = lutil_passwd( passwd, &pw[j], NULL ); #endif printf("%s (%d): %s (%d)\t(%d) %s\n", pw[j].bv_val, pw[j].bv_len, passwd->bv_val, passwd->bv_len, rc, rc == 0 ? "OKAY" : "BAD" ); #ifdef SLAP_AUTHPASSWD rc = lutil_authpasswd( passwd, salt, &bad, NULL ); #else rc = lutil_passwd( passwd, &bad, NULL ); #endif printf("%s (%d): %s (%d)\t(%d) %s\n", bad.bv_val, bad.bv_len, passwd->bv_val, passwd->bv_len, rc, rc != 0 ? "OKAY" : "BAD" ); } printf("\n"); } return EXIT_SUCCESS; }
int slappasswd( int argc, char *argv[] ) { int rc = EXIT_SUCCESS; #ifdef LUTIL_SHA1_BYTES char *default_scheme = "{SSHA}"; #else char *default_scheme = "{SMD5}"; #endif char *scheme = default_scheme; char *newpw = NULL; char *pwfile = NULL; const char *text; const char *progname = "slappasswd"; int i; char *newline = "\n"; struct berval passwd = BER_BVNULL; struct berval hash; #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; while( (i = getopt( argc, argv, "c:d:gh:no:s:T:vu" )) != EOF ) { switch (i) { case 'c': /* crypt salt format */ scheme = "{CRYPT}"; lutil_salt_format( optarg ); break; case 'g': /* new password (generate) */ if ( pwfile != NULL ) { fprintf( stderr, "Option -g incompatible with -T\n" ); return EXIT_FAILURE; } else if ( newpw != NULL ) { fprintf( stderr, "New password already provided\n" ); return EXIT_FAILURE; } else if ( lutil_passwd_generate( &passwd, 8 )) { fprintf( stderr, "Password generation failed\n" ); return EXIT_FAILURE; } break; case 'h': /* scheme */ if ( scheme != default_scheme ) { fprintf( stderr, "Scheme already provided\n" ); return EXIT_FAILURE; } else { scheme = ch_strdup( optarg ); } break; case 'n': newline = ""; break; case 'o': if ( parse_slappasswdopt() ) { usage ( progname ); } break; case 's': /* new password (secret) */ 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 { char* p; newpw = ch_strdup( optarg ); for( p = optarg; *p != '\0'; p++ ) { *p = '\0'; } } break; case 'T': /* password file */ 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; } pwfile = optarg; break; case 'u': /* RFC2307 userPassword */ break; case 'v': /* verbose */ verbose++; break; default: usage ( progname ); } } if( argc - optind != 0 ) { usage( progname ); } #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; } if ( moduleload && module_load(moduleload, 0, NULL) ) { rc = EXIT_FAILURE; goto destroy; } #endif if( pwfile != NULL ) { if( lutil_get_filed_password( pwfile, &passwd )) { rc = EXIT_FAILURE; goto destroy; } } else if ( BER_BVISEMPTY( &passwd )) { if( newpw == NULL ) { /* prompt for new password */ char *cknewpw; newpw = ch_strdup(getpassphrase("New password: "******"Re-enter new password: "******"Password values do not match\n" ); rc = EXIT_FAILURE; goto destroy; } } passwd.bv_val = newpw; passwd.bv_len = strlen(passwd.bv_val); } else { hash = passwd; goto print_pw; } lutil_passwd_hash( &passwd, scheme, &hash, &text ); if( hash.bv_val == NULL ) { fprintf( stderr, "Password generation failed for scheme %s: %s\n", scheme, text ? text : "" ); rc = EXIT_FAILURE; goto destroy; } if( lutil_passwd( &hash, &passwd, NULL, &text ) ) { fprintf( stderr, "Password verification failed. %s\n", text ? text : "" ); rc = EXIT_FAILURE; goto destroy; } print_pw:; printf( "%s%s" , hash.bv_val, newline ); destroy:; #ifdef SLAPD_MODULES module_kill(); #endif return rc; }
int slappasswd( int argc, char *argv[] ) { #ifdef LUTIL_SHA1_BYTES char *default_scheme = "{SSHA}"; #else char *default_scheme = "{SMD5}"; #endif char *scheme = default_scheme; char *newpw = NULL; char *pwfile = NULL; const char *text; const char *progname = "slappasswd"; int i; char *newline = "\n"; struct berval passwd = BER_BVNULL; struct berval hash; while( (i = getopt( argc, argv, "c:d:gh:ns:T:vu" )) != EOF ) { switch (i) { case 'c': /* crypt salt format */ scheme = "{CRYPT}"; lutil_salt_format( optarg ); break; case 'g': /* new password (generate) */ if ( pwfile != NULL ) { fprintf( stderr, "Option -g incompatible with -T\n" ); return EXIT_FAILURE; } else if ( newpw != NULL ) { fprintf( stderr, "New password already provided\n" ); return EXIT_FAILURE; } else if ( lutil_passwd_generate( &passwd, 8 )) { fprintf( stderr, "Password generation failed\n" ); return EXIT_FAILURE; } break; case 'h': /* scheme */ if ( scheme != default_scheme ) { fprintf( stderr, "Scheme already provided\n" ); return EXIT_FAILURE; } else { scheme = ch_strdup( optarg ); } break; case 'n': newline = ""; break; case 's': /* new password (secret) */ 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 { char* p; newpw = ch_strdup( optarg ); for( p = optarg; *p != '\0'; p++ ) { *p = '\0'; } } break; case 'T': /* password file */ 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; } 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; } } else if ( BER_BVISEMPTY( &passwd )) { if( newpw == NULL ) { /* prompt for new password */ char *cknewpw; newpw = ch_strdup(getpassphrase("New password: "******"Re-enter new password: "******"Password values do not match\n" ); return EXIT_FAILURE; } } passwd.bv_val = newpw; passwd.bv_len = strlen(passwd.bv_val); } else { hash = passwd; goto print_pw; } lutil_passwd_hash( &passwd, scheme, &hash, &text ); if( hash.bv_val == NULL ) { fprintf( stderr, "Password generation failed for scheme %s: %s\n", scheme, text ? text : "" ); return EXIT_FAILURE; } if( lutil_passwd( &hash, &passwd, NULL, &text ) ) { fprintf( stderr, "Password verification failed. %s\n", text ? text : "" ); return EXIT_FAILURE; } print_pw:; printf( "%s%s" , hash.bv_val, newline ); return EXIT_SUCCESS; }
struct berval * cred, struct berval * new ) { struct berval *tmp; #ifdef LUTIL_SHA1_BYTES char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}"; #else char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}"; #endif #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD ) ldap_pvt_thread_mutex_lock( &passwd_mutex ); #endif tmp = lutil_passwd_hash( cred , hash ); #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD ) ldap_pvt_thread_mutex_unlock( &passwd_mutex ); #endif if( tmp == NULL ) { new->bv_len = 0; new->bv_val = NULL; } *new = *tmp; free( tmp ); return; }