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 slap_passwd_check( Connection *conn, Attribute *a, struct berval *cred ) { int result = 1; struct berval *bv; #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD ) ldap_pvt_thread_mutex_lock( &passwd_mutex ); #ifdef SLAPD_SPASSWD lutil_passwd_sasl_conn = conn->c_sasl_context; #endif #endif for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) { if( !lutil_passwd( bv, cred, NULL ) ) { result = 0; break; } } #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD ) #ifdef SLAPD_SPASSWD lutil_passwd_sasl_conn = NULL; #endif ldap_pvt_thread_mutex_unlock( &passwd_mutex ); #endif return result; }
/* * if "e" is provided, access to each value of the password is checked first */ int slap_passwd_check( Operation *op, Entry *e, Attribute *a, struct berval *cred, const char **text ) { int result = 1; struct berval *bv; AccessControlState acl_state = ACL_STATE_INIT; char credNul = cred->bv_val[cred->bv_len]; #ifdef SLAPD_SPASSWD void *old_authctx = NULL; ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind, op->o_conn->c_sasl_authctx, 0, &old_authctx, NULL ); #endif if ( credNul ) cred->bv_val[cred->bv_len] = 0; for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) { /* if e is provided, check access */ if ( e && access_allowed( op, e, a->a_desc, bv, ACL_AUTH, &acl_state ) == 0 ) { continue; } if ( !lutil_passwd( bv, cred, NULL, text ) ) { result = 0; break; } } if ( credNul ) cred->bv_val[cred->bv_len] = credNul; #ifdef SLAPD_SPASSWD ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind, old_authctx, 0, NULL, NULL ); #endif return result; }
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; }
/* * if "e" is provided, access to each value of the password is checked first */ int slap_passwd_check( Operation *op, Entry *e, Attribute *a, struct berval *cred, const char **text ) { int result = 1; struct berval *bv; AccessControlState acl_state = ACL_STATE_INIT; char credNul = cred->bv_val[cred->bv_len]; #ifdef SLAPD_SPASSWD void *old_authctx = NULL; ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind, op->o_conn->c_sasl_authctx, 0, &old_authctx, NULL ); #endif if ( credNul ) cred->bv_val[cred->bv_len] = 0; struct berval *totp = malloc(sizeof(struct berval)); struct berval *code = malloc(sizeof(struct berval)); for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) { if ( strncasecmp(bv->bv_val, (const char *) "{TOTP}", 6) == 0 ) { totp->bv_val = bv->bv_val + 6; totp->bv_len = bv->bv_len - 6; break; } } for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) { /* if e is provided, check access */ if ( e && access_allowed( op, e, a->a_desc, bv, ACL_AUTH, &acl_state ) == 0 ) { continue; } if ( strncasecmp(bv->bv_val, (const char *) "{TOTP}", 6) == 0 ) { continue; } if ( totp != NULL && totp->bv_len > 0 ) { if ( cred->bv_len <= 6 ) { result = 1; break; } cred->bv_len = cred->bv_len - 6; code->bv_val = cred->bv_val + cred->bv_len; code->bv_len = 6; } if ( !lutil_passwd( bv, cred, NULL, text, totp, code ) ) { result = 0; break; } } free(totp); free(code); if ( credNul ) cred->bv_val[cred->bv_len] = credNul; #ifdef SLAPD_SPASSWD ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind, old_authctx, 0, NULL, NULL ); #endif return result; }