/*
 * This license is stolen from Poul-Henning Kamp.
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE":
 * <proguy@proguy.dk> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return. Paul Fleischer
 * ----------------------------------------------------------------------------
 *
 *
 * Compilation instructions:
 *   gcc cracksh.c -o cracksh -lcrack
 * 
 * This should work for any UNIX system, but has only been tested on Linux.
 */

#include <crack.h>
#include <stdio.h>
#include <string.h>

#define MAX_PASSWORD_SIZE 100
char *default_dict_path = "/usr/lib/cracklib_dict";

void
printhelp(char *progname) {
  printf("Usage:\n");
  printf("%s [-h] [-d <dict path>]\n", progname);
  printf("\n");
  printf("The -d option is used to specify the dictionary to use.\n");
  printf("If nothing is specified /usr/lib/cracklib_dict will be used.\n");
}

int
main(int argc, char **argv) {
  char *msg;
  char password[MAX_PASSWORD_SIZE];
  char *path = NULL;
  int i;
  
  /* Check arguments */
  for(i = 1; i<argc; i++) {

    /* Option */
    if( argv[i][0] == '-' ) {
      
      /* Which option? */
      if( argv[i][1] == 'h' ) {
	printhelp(argv[0]);
	exit(0);
      } else
      if( argv[i][1] == 'd') {
	path = argv[++i];
      } else {
	printhelp(argv[0]);
	exit(0);
      }
    }
  }

  if( !path )
    path = default_dict_path;

  /* Use i as our are-we-still-running variable */
  i = 1;

  while( i ) {

    /* Read something from stdin and check send it to cracklib*/
    if( fgets(password, MAX_PASSWORD_SIZE, stdin) > 0 ) {
      msg = FascistCheck(password, path);
      
      if( msg ) {
	printf("%s\n", msg);
      } else {
	printf("OK\n");
      }

    } else { 
      i = 0; 
    }
  }
}

