"Fossies" - the Fresh Open Source Software Archive

Member "apg-2.2.3/randpass.c" (7 Aug 2003, 4845 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 ** randpass.c - Random password generation module of PWGEN program
   32 */
   33 
   34 #include <stdio.h>
   35 #include <stdlib.h>
   36 #include <time.h>
   37 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
   38 #include <pwd.h>
   39 #endif
   40 #include <unistd.h>
   41 #include "randpass.h"
   42 
   43 #include "owntypes.h"
   44 #include "smbl.h"
   45 
   46 /*
   47 ** gen_rand_pass - generates random password of specified type
   48 ** INPUT:
   49 **   char * - password string.
   50 **   int    - minimum password length.
   51 **   int    - maximum password length.
   52 **   unsigned int - password generation mode.
   53 ** OUTPUT:
   54 **   int - password length or -1 on error.
   55 ** NOTES:
   56 **   none.
   57 */
   58 int
   59 gen_rand_pass (char *password_string, int minl, int maxl, unsigned int pass_mode)
   60 {
   61   int i = 0;
   62   int j = 0;
   63   int length = 0;
   64   char *str_pointer;
   65   int random_weight[94];
   66   int max_weight = 0;
   67   int max_weight_element_number = 0;
   68 
   69   if (minl > APG_MAX_PASSWORD_LENGTH || maxl > APG_MAX_PASSWORD_LENGTH ||
   70       minl < 1 || maxl < 1 || minl > maxl)
   71       return (-1);
   72   for (i = 0; i <= 93; i++) random_weight[i] = 0; 
   73   length = minl + randint(maxl-minl+1);
   74   str_pointer = password_string;
   75 
   76   for (i = 0; i < length; i++)
   77     {
   78 /* Asign random weight in weight array if mode is present*/
   79       for (j = 0; j <= 93 ; j++)
   80          if ( ( (pass_mode & smbl[j].type) > 0) &&
   81          !( (S_RS & smbl[j].type) > 0))
   82         random_weight[j] = 1 + randint(20000);
   83       j = 0;
   84 /* Find an element with maximum weight */
   85       for (j = 0; j <= 93; j++)
   86     if (random_weight[j] > max_weight)
   87       {
   88         max_weight = random_weight[j];
   89         max_weight_element_number = j;
   90       }
   91 /* Get password symbol */
   92       *str_pointer = smbl[max_weight_element_number].ch;
   93       str_pointer++;
   94       max_weight = 0;
   95       max_weight_element_number = 0;
   96       for (j = 0; j <= 93; j++) random_weight[j] = 0;
   97     }
   98   *str_pointer = 0;
   99   return (length);
  100 }
  101 
  102 /*
  103 ** gen_rand_symbol - generates random password of specified type
  104 ** INPUT:
  105 **   char * - symbol.
  106 **   unsigned int - symbol type.
  107 ** OUTPUT:
  108 **   int - password length or -1 on error.
  109 ** NOTES:
  110 **   none.
  111 */
  112 int
  113 gen_rand_symbol (char *symbol, unsigned int mode)
  114 {
  115   int j = 0;
  116   char *str_pointer;
  117   int random_weight[94];
  118   int max_weight = 0;
  119   int max_weight_element_number = 0;
  120 
  121   for (j = 0; j <= 93; j++) random_weight[j] = 0; 
  122   str_pointer = symbol;
  123   j = 0;
  124 /* Asign random weight in weight array if mode is present*/
  125   for (j = 0; j <= 93 ; j++)
  126      if ( ( (mode & smbl[j].type) > 0) &&
  127          !( (S_RS & smbl[j].type) > 0))
  128        random_weight[j] = 1 + randint(20000);
  129   j = 0;
  130 /* Find an element with maximum weight */
  131   for (j = 0; j <= 93; j++)
  132      if (random_weight[j] > max_weight)
  133        {
  134         max_weight = random_weight[j];
  135         max_weight_element_number = j;
  136        }
  137 /* Get password symbol */
  138   *str_pointer = smbl[max_weight_element_number].ch;
  139   max_weight = 0;
  140   max_weight_element_number = 0;
  141   return (0);
  142 }
  143 
  144 /*
  145 ** is_restricted_symbol - detcts if symbol is restricted rigt now
  146 ** INPUT:
  147 **   char - symbol.
  148 ** OUTPUT:
  149 **   int - 0 - not restricted
  150 **         1 - restricted
  151 ** NOTES:
  152 **   none.
  153 */
  154 int
  155 is_restricted_symbol (char symbol)
  156 {
  157   int j = 0;
  158   for (j = 0; j <= 93 ; j++)
  159     if (symbol == smbl[j].ch)
  160       if ((S_RS & smbl[j].type) > 0)
  161         return(1);
  162   return(0);
  163 }