2004-09-14 04:21:11 +04:00
# include <memory.h>
# include <string.h>
# include <stdlib.h>
# include <stdio.h>
# include <ctype.h>
# include <crack.h>
2017-12-14 23:40:22 +03:00
# include <unistd.h>
2004-09-14 04:21:11 +04:00
void usage ( char * command ) {
char * c , * comm ;
comm = command ;
while ( ( c = strrchr ( comm , ' / ' ) ) ! = NULL ) {
comm = c + 1 ;
}
2005-12-26 20:36:33 +03:00
fprintf ( stderr , " Usage: %s [-c] [-s] [-d <dictionary>] \n \n " , comm ) ;
fprintf ( stderr , " -c enables NT like complexity checks \n " ) ;
fprintf ( stderr , " -d <dictionary file> for cracklib \n " ) ;
fprintf ( stderr , " -s simple check use NT like checks ONLY \n \n " ) ;
fprintf ( stderr , " The password is read via stdin. \n \n " ) ;
2004-09-14 04:21:11 +04:00
exit ( - 1 ) ;
}
2005-12-26 20:22:46 +03:00
int complexity ( char * passwd )
{
/* TG 26.10.2005
* check password for complexity like MS Windows NT
*/
int c_upper = 0 ;
int c_lower = 0 ;
int c_digit = 0 ;
int c_punct = 0 ;
int c_tot = 0 ;
int i , len ;
if ( ! passwd ) goto fail ;
len = strlen ( passwd ) ;
for ( i = 0 ; i < len ; i + + ) {
if ( c_tot > = 3 ) break ;
if ( isupper ( passwd [ i ] ) ) {
if ( ! c_upper ) {
c_upper = 1 ;
c_tot + = 1 ;
}
continue ;
}
if ( islower ( passwd [ i ] ) ) {
if ( ! c_lower ) {
c_lower = 1 ;
c_tot + = 1 ;
}
continue ;
}
if ( isdigit ( passwd [ i ] ) ) {
if ( ! c_digit ) {
c_digit = 1 ;
c_tot + = 1 ;
}
continue ;
}
if ( ispunct ( passwd [ i ] ) ) {
if ( ! c_punct ) {
c_punct = 1 ;
c_tot + = 1 ;
}
continue ;
}
}
if ( ( c_tot ) < 3 ) goto fail ;
return 0 ;
fail :
fprintf ( stderr , " ERR Complexity check failed \n \n " ) ;
return - 4 ;
}
2004-09-14 04:21:11 +04:00
int main ( int argc , char * * argv ) {
extern char * optarg ;
2005-12-26 20:36:33 +03:00
int c , ret , complex_check = 0 , simplex_check = 0 ;
2004-09-14 04:21:11 +04:00
char f [ 256 ] ;
char * dictionary = NULL ;
char * password ;
2016-04-26 03:07:10 +03:00
const char * reply ;
2004-09-14 04:21:11 +04:00
2005-12-26 20:36:33 +03:00
while ( ( c = getopt ( argc , argv , " d:cs " ) ) ! = EOF ) {
2004-09-14 04:21:11 +04:00
switch ( c ) {
case ' d ' :
dictionary = strdup ( optarg ) ;
break ;
2005-12-26 20:22:46 +03:00
case ' c ' :
complex_check = 1 ;
break ;
2005-12-26 20:36:33 +03:00
case ' s ' :
complex_check = 1 ;
simplex_check = 1 ;
break ;
2004-09-14 04:21:11 +04:00
default :
usage ( argv [ 0 ] ) ;
}
}
2005-12-26 20:36:33 +03:00
if ( ! simplex_check & & dictionary = = NULL ) {
fprintf ( stderr , " ERR - Missing cracklib dictionary \n \n " ) ;
2004-09-14 04:21:11 +04:00
usage ( argv [ 0 ] ) ;
}
2005-12-26 20:22:46 +03:00
fflush ( stdin ) ;
2004-09-14 04:21:11 +04:00
password = fgets ( f , sizeof ( f ) , stdin ) ;
if ( password = = NULL ) {
fprintf ( stderr , " ERR - Failed to read password \n \n " ) ;
exit ( - 2 ) ;
}
2005-12-26 20:22:46 +03:00
if ( complex_check ) {
ret = complexity ( password ) ;
if ( ret ) {
exit ( ret ) ;
}
}
2005-12-26 20:36:33 +03:00
if ( simplex_check ) {
exit ( 0 ) ;
}
2004-09-14 04:21:11 +04:00
reply = FascistCheck ( password , dictionary ) ;
if ( reply ! = NULL ) {
fprintf ( stderr , " ERR - %s \n \n " , reply ) ;
exit ( - 3 ) ;
}
exit ( 0 ) ;
}