"Fossies" - the Fresh Open Source Software Archive

Member "apg-2.2.3/restrict.c" (7 Aug 2003, 7738 Bytes) of package /linux/privat/old/apg-2.2.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2 ** Copyright (c) 1999, 2000, 2001, 2002, 2003
    3 ** Adel I. Mirzazhanov. All rights reserved
    4 **
    5 ** Redistribution and use in source and binary forms, with or without
    6 ** modification, are permitted provided that the following conditions
    7 ** are met:
    8 ** 
    9 **     1.Redistributions of source code must retain the above copyright notice,
   10 **       this list of conditions and the following disclaimer. 
   11 **     2.Redistributions in binary form must reproduce the above copyright
   12 **       notice, this list of conditions and the following disclaimer in the
   13 **       documentation and/or other materials provided with the distribution. 
   14 **     3.The name of the author may not be used to endorse or promote products
   15 **       derived from this software without specific prior written permission. 
   16 **        
   17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND ANY EXPRESS
   18 ** OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO, THE IMPLIED
   19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20 ** ARE DISCLAIMED.  IN  NO  EVENT  SHALL THE AUTHOR BE LIABLE FOR ANY
   21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE
   23 ** GOODS OR SERVICES;  LOSS OF USE,  DATA,  OR  PROFITS;  OR BUSINESS
   24 ** INTERRUPTION)  HOWEVER  CAUSED  AND  ON  ANY  THEORY OF LIABILITY,
   25 ** WHETHER  IN  CONTRACT,   STRICT   LIABILITY,  OR  TORT  (INCLUDING
   26 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   27 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   28 */
   29 
   30 /*
   31 ** restrict.c
   32 */
   33 #include <stdio.h>
   34 #include <stdlib.h>
   35 #include <string.h>
   36 #include "restrict.h"
   37 extern struct sym smbl[94];
   38 /*
   39 ** check_pass() - routine that checks if password exist in dictionary
   40 ** INPUT:
   41 **   char * - password to check.
   42 **   char * - dictionary filename.
   43 ** OUTPUT:
   44 **   int
   45 **    -1 - error 
   46 **     1 - password exist in dictionary
   47 **     0 - password does not exist in dictionary
   48 ** NOTES:
   49 **   none.
   50 */
   51 int
   52 check_pass(char *pass, char *dict)
   53 {
   54  FILE *dct;
   55  char *string;
   56  char *tmp;
   57  if( (string = (char *) calloc(1,MAX_DICT_STRING_SIZE)) == NULL)
   58    return(-1);
   59  
   60 #ifdef APG_DEBUG
   61  fprintf (stdout, "DEBUG> check_pass: ck pass: %s\n", pass);
   62  fflush (stdout);
   63 #endif /* APG_DEBUG */
   64  /*
   65  ** Open dict file an report of error
   66  */
   67  if ( (dct = fopen(dict,"r")) == NULL)
   68    return(-1);
   69 
   70  while ((fgets(string, MAX_DICT_STRING_SIZE, dct) != NULL))
   71   {
   72    tmp = strtok (string," \t\n\0");
   73    if( tmp != NULL)
   74      string = tmp;
   75    else
   76      continue;
   77    if(strlen(string) != strlen(pass)) continue;
   78    else if (strncmp(string, pass, strlen(pass)) == 0)
   79     {
   80      free ( (void *)string);
   81      fclose (dct);
   82 #ifdef APG_DEBUG
   83      fprintf (stdout, "DEBUG> check_pass: password found in dictionary: %s\n", pass);
   84      fflush (stdout);
   85 #endif /* APG_DEBUG */
   86      return (1);
   87     }
   88   }
   89  free ( (void *)string);
   90  fclose (dct);
   91  return (0); 
   92 }
   93 
   94 /*
   95 ** bloom_check_pass() - routine that checks if password exist in dictionary
   96 ** using Bloom filter.
   97 ** INPUT:
   98 **   char * - password to check.
   99 **   char * - bloom-filter filename.
  100 ** OUTPUT:
  101 **   int
  102 **    -1 - error 
  103 **     1 - password exist in dictionary
  104 **     0 - password does not exist in dictionary
  105 ** NOTES:
  106 **   none.
  107 */
  108 int
  109 bloom_check_pass (char *word, char *filter)
  110 {
  111  int ret = 0;
  112  FILE *f_filter;
  113  h_val filter_size = 0L;
  114  f_mode flt_mode = 0x00;
  115  if ( (f_filter = open_filter(filter,"r")) == NULL)
  116     return(-1);
  117  filter_size = get_filtersize(f_filter);
  118  flt_mode =    get_filtermode(f_filter);
  119  ret = check_word (word, f_filter, filter_size, flt_mode);
  120  close_filter(f_filter);
  121  return(ret);
  122 }
  123 
  124 /*
  125 ** paranoid_bloom_check_pass() - routine that checks if password or any
  126 ** substring of the password exist in dictionary using Bloom filter.
  127 ** INPUT:
  128 **   char * - password to check.
  129 **   char * - bloom-filter filename.
  130 **   USHORT - minimum substring length
  131 ** OUTPUT:
  132 **   int
  133 **    -1 - error 
  134 **     1 - password exist in dictionary
  135 **     0 - password does not exist in dictionary
  136 ** NOTES:
  137 **   none.
  138 */
  139 int
  140 paranoid_bloom_check_pass (char * password, char *filter, USHORT s_len)
  141 {
  142  char * substring;
  143  int len = strlen(password); /* string length                      */
  144  int c_substr_start_pos = 0; /* current start position             */
  145  int substr_len = 0;         /* substring length (LEN-I >= substr_len >= 2) */
  146  int k = 0;                  /* counter                            */
  147  int c = 0;                  /* counter                            */
  148  int ret = 0;
  149  if (s_len < 2) s_len = 2;
  150  if (s_len > len) return (bloom_check_pass(password, filter));
  151 
  152 #ifdef APG_DEBUG
  153  fprintf (stdout, "DEBUG> paranoid_bloom_check_pass: ck pass: %s\n", password);
  154  fflush (stdout);
  155 #endif /* APG_DEBUG */
  156 
  157  if ((substring = (char *)calloc(1, (size_t)len))==NULL)
  158    return (-1);
  159  
  160  for (c_substr_start_pos = 0; c_substr_start_pos <= len-s_len; c_substr_start_pos++)
  161   for (substr_len = s_len; substr_len <= len-c_substr_start_pos; substr_len++)
  162    {
  163     c = 0;
  164     for (k = c_substr_start_pos; k <= c_substr_start_pos + substr_len-1; k++)
  165      {
  166       substring[c]=password[k];
  167       c++;
  168      }
  169 #ifdef APG_DEBUG
  170     fprintf (stdout, "DEBUG> paranoid_bloom_check_pass: ck substr: %s\n", substring);
  171     fflush (stdout);
  172 #endif /* APG_DEBUG */
  173     if((ret = bloom_check_pass(substring, filter)) == 1)
  174     {
  175 #ifdef APG_DEBUG
  176      fprintf (stdout, "DEBUG> paranoid_bloom_check_pass: substr found in filter: %s\n", substring);
  177      fflush (stdout);
  178 #endif /* APG_DEBUG */
  179      return(1);
  180     }
  181     else if (ret == -1) return(-1);
  182     (void)memset(substring,0,(size_t)len);
  183    }
  184  return(0);
  185 }
  186 
  187 /*
  188 ** filter_check_pass() - routine that checks password against filter string
  189 **
  190 ** INPUT:
  191 **   char * - password to check.
  192 **   char * - bloom-filter filename.
  193 ** OUTPUT:
  194 **   int
  195 **    -1 - error 
  196 **     1 - password do not pass the filter
  197 **     0 - password pass the filter
  198 ** NOTES:
  199 **   none.
  200 */
  201 
  202 int
  203 filter_check_pass(const char * word, unsigned int cond)
  204 {
  205  int i = 0;
  206  int sl_ret = 0;
  207  int cl_ret = 0;
  208  int nb_ret = 0;
  209  int ss_ret = 0;
  210 
  211 #ifdef APG_DEBUG
  212  fprintf (stdout, "DEBUG> filter_check_pass: ck pass: %s\n", word);
  213  fflush (stdout);
  214 #endif /* APG_DEBUG */
  215 
  216  if ((cond & S_SS) > 0)
  217     for (i=0; i < 94; i++)
  218        if ((smbl[i].type & S_SS) > 0)
  219           if ((strchr(word,smbl[i].ch)) != NULL)
  220          ss_ret = 1;
  221  i = 0;
  222  if ((cond & S_SL) > 0)
  223     for (i=0; i < 94; i++)
  224        if ((smbl[i].type & S_SL) > 0)
  225           if ((strchr(word,smbl[i].ch)) != NULL)
  226          sl_ret = 1;
  227  i = 0;
  228  if ((cond & S_CL) > 0)
  229     for (i=0; i < 94; i++)
  230        if ((smbl[i].type & S_CL) > 0)
  231           if ((strchr(word,smbl[i].ch)) != NULL)
  232          cl_ret = 1;
  233  i = 0;
  234  if ((cond & S_NB) > 0)
  235     for (i=0; i < 94; i++)
  236        if ((smbl[i].type & S_NB) > 0)
  237           if ((strchr(word,smbl[i].ch)) != NULL)
  238          nb_ret = 1;
  239  if (((cond & S_SS) > 0) &&(ss_ret != 1)) return (1);
  240  if (((cond & S_SL) > 0) &&(sl_ret != 1)) return (1);
  241  if (((cond & S_CL) > 0) &&(cl_ret != 1)) return (1);
  242  if (((cond & S_NB) > 0) &&(nb_ret != 1)) return (1);
  243 
  244 #ifdef APG_DEBUG
  245  fprintf (stdout, "DEBUG> filter_check_pass: password %s pass the filter\n", word);
  246  fflush (stdout);
  247 #endif /* APG_DEBUG */
  248  return(0);
  249 }
  250 
  251 /*
  252 ** set_exclude_list() - set up character list that should
  253 ** be excluded from password generation process
  254 **
  255 ** INPUT:
  256 **   char * - string of characters.
  257 ** OUTPUT:
  258 **   int - return code
  259 **    0 - OK
  260 **   -1 - char_string is too long (max 93) 
  261 ** NOTES:
  262 **   none.
  263 */
  264 int set_exclude_list(const char * char_string)
  265 {
  266  int i = 0;
  267  if (strlen(char_string) > 93)
  268   return(-1);
  269  for(i=0; i < 94; i++)
  270   if ((strchr(char_string, smbl[i].ch)) != NULL)
  271     smbl[i].type = smbl[i].type | S_RS;
  272  return(0);
  273 }