"Fossies" - the Fresh Open Source Software Archive

Member "passgen-1.2.0/src/passgen_conf.c" (18 Jun 2011, 4209 Bytes) of package /linux/privat/old/passgen-1.2.0.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. For more information about "passgen_conf.c" see the Fossies "Dox" file reference documentation.

    1 /*  Author:     V. Alex Brennen (vab@mit.edu)   */
    2 /*  Copyright:      None                */
    3 /*  License:        Public Domain           */
    4 /*  Created:        2006.12.26          */
    5 /*  Last Updated:   2011.06.18          */
    6 
    7 #include "passgen_conf.h"
    8 
    9 
   10 int init_passgen_config(struct passgen_config **config)
   11 {
   12     /* We default to 14 chars because that is thought to still 
   13      * relatively secure.
   14      */
   15     (*config)->num_chars = 14;
   16     (*config)->num_passwds = 5;
   17     (*config)->use_urandom = 0;
   18     (*config)->no_space = 0;
   19     (*config)->passwd_type = 0;
   20     (*config)->homoglyph_suppression_level = 0;
   21     (*config)->lowercase_only = 0;
   22     (*config)->uppercase_only = 0;
   23     (*config)->suppress_config_warning = 0;
   24 
   25     return 0;
   26 }
   27 
   28 
   29 int read_passgen_config(struct passgen_config **config)
   30 {
   31     FILE *conf_file = NULL;
   32     char line[513];
   33     char file_loc[1024];
   34     char *p = NULL;
   35 
   36 
   37     memset(line,0x00,513);
   38     memset(file_loc,0x00,1024);
   39 
   40     if(file_exists(CONF))
   41     {
   42         strncpy(file_loc,CONF,1023);
   43     }
   44     else if(file_exists("/etc/passgen.conf"))
   45     {
   46         strncpy(file_loc,"/etc/passgen.conf",63);
   47     }
   48     else if(file_exists("/usr/local/etc/passgen.conf"))
   49     {
   50         strncpy(file_loc,"/usr/local/etc/passgen.conf",63);
   51     }
   52     else
   53     {
   54         if((*config)->suppress_config_warning == 0)
   55         {
   56             fprintf(stderr,"Warning: Unable to find configuration file.\n");
   57         }
   58         
   59         return -1;
   60     }
   61 
   62     if((conf_file = fopen(file_loc,"r")) == NULL)
   63     {
   64         if((*config)->suppress_config_warning == 0)
   65         {
   66             fprintf(stderr,"Failed to open configuration file: %s\n",file_loc);
   67         }
   68         
   69         return -1;
   70     }
   71 
   72     while(fgets(line,512,conf_file) != NULL)
   73     {
   74         if( (!(memcmp(line,"#",1) == 0)) && (!(isspace(line[0]))) )
   75         {
   76             char *name = NULL;
   77             char *value = NULL;
   78 
   79             name = &line[0];
   80             p = memchr(name,' ',512);
   81             if(p == NULL)
   82                 continue;
   83             value = ++p;
   84             if(value == NULL)
   85                 continue;
   86             p--;
   87             *p='\0';
   88             value[strlen(value)-1] = '\0';
   89 
   90             if(memcmp(name,"num_chars",9) == 0)
   91             {
   92                 if(!isdigit(value[0]))
   93                     config_reject(name);
   94                 else
   95                     (*config)->num_chars = atoi(value);
   96                 if((*config)->num_chars < 1)
   97                     config_reject(name);
   98             }
   99             else if(memcmp(name,"num_passwds",11) == 0)
  100             {
  101                 if(!isdigit(value[0]))
  102                     config_reject(name);
  103                 else
  104                     (*config)->num_passwds = atoi(value);
  105                 if((*config)->num_passwds < 1)
  106                     config_reject(name);
  107             }
  108             else if(memcmp(name,"use_urandom",11) == 0)
  109             {
  110                 if(!isdigit(value[0]))
  111                     config_reject(name);
  112                 else
  113                     (*config)->use_urandom = atoi(value);
  114                 if(((*config)->use_urandom != 0) && ((*config)->use_urandom != 1))
  115                     config_reject(name);
  116             }
  117             else if(memcmp(name,"no_space",8) == 0)
  118             {
  119                 if(!isdigit(value[0]))
  120                     config_reject(name);
  121                 else
  122                     (*config)->no_space = atoi(value);
  123                 if(((*config)->no_space != 0) && ((*config)->no_space != 1))
  124                     config_reject(name);
  125             }
  126             else if(memcmp(name,"passwd_type",11) == 0)
  127             {
  128                 if(!isdigit(value[0]))
  129                     config_reject(name);
  130                 else
  131                     (*config)->passwd_type = atoi(value);
  132                 if(((*config)->passwd_type > 3) || ((*config)->passwd_type < 0))
  133                     config_reject(name);
  134             }
  135             else if(memcmp(name,"homoglyph_suppression_level",26) == 0)
  136             {
  137                 if(!(isdigit(value[0])))
  138                     config_reject(name);
  139                 else
  140                     (*config)->homoglyph_suppression_level = atoi(value);
  141                 if(((*config)->homoglyph_suppression_level > 2) || ((*config)->homoglyph_suppression_level < 0))
  142                     config_reject(name);
  143             }
  144             else if(memcmp(name,"forced_case",14) == 0)
  145             {
  146                 int forced_case = 0;
  147                 
  148                 if(!isdigit(value[0]))
  149                     config_reject(name);
  150                 else
  151                     forced_case = atoi(value);
  152                 if((forced_case < 0) || (forced_case > 2))
  153                     config_reject(name);
  154                 else
  155                 {
  156                     if(forced_case == 1)
  157                     {
  158                         (*config)->lowercase_only = 1;
  159                     }
  160                     else if(forced_case == 2)
  161                     {
  162                         (*config)->uppercase_only = 1;  
  163                     }
  164                     else
  165                     {
  166                         (*config)->lowercase_only = 0;
  167                         (*config)->uppercase_only = 0;
  168                     }
  169                 }
  170             }
  171             else
  172             {
  173                 config_reject(name);
  174             }
  175         }
  176     }
  177     fclose(conf_file);
  178 
  179     return 0;
  180 }
  181 
  182 
  183 void config_reject(char *name)
  184 {
  185     fprintf(stderr,"passgen.conf:  Bad value, rejecting config var: %s\n",name);
  186 }
  187 
  188 
  189 int file_exists(char *path)
  190 {
  191     int rslt = 0;
  192     struct stat tmp_stats;
  193 
  194 
  195     rslt = lstat(path, &tmp_stats);
  196     if(rslt != -1)
  197     {
  198         return 1;
  199     }
  200 
  201     return 0;
  202 }