2 ** Copyright (c) 1999, 2000, 2001, 2002, 2003
3 ** Adel I. Mirzazhanov. All rights reserved
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
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.
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.
31 ** randpass.c - Random password generation module of PWGEN program
46 ** gen_rand_pass - generates random password of specified type
48 ** char * - password string.
49 ** int - minimum password length.
50 ** int - maximum password length.
51 ** unsigned int - password generation mode.
53 ** int - password length or -1 on error.
58 gen_rand_pass (char *password_string, int minl, int maxl, unsigned int pass_mode)
64 int random_weight[94];
66 int max_weight_element_number = 0;
68 if (minl > APG_MAX_PASSWORD_LENGTH || maxl > APG_MAX_PASSWORD_LENGTH ||
69 minl < 1 || maxl < 1 || minl > maxl)
71 for (i = 0; i <= 93; i++) random_weight[i] = 0;
72 length = minl + randint(maxl-minl+1);
73 str_pointer = password_string;
75 for (i = 0; i < length; i++)
77 /* Asign random weight in weight array if mode is present*/
78 for (j = 0; j <= 93 ; j++)
79 if ( ( (pass_mode & smbl[j].type) > 0) &&
80 !( (S_RS & smbl[j].type) > 0))
81 random_weight[j] = 1 + randint(20000);
83 /* Find an element with maximum weight */
84 for (j = 0; j <= 93; j++)
85 if (random_weight[j] > max_weight)
87 max_weight = random_weight[j];
88 max_weight_element_number = j;
90 /* Get password symbol */
91 *str_pointer = smbl[max_weight_element_number].ch;
94 max_weight_element_number = 0;
95 for (j = 0; j <= 93; j++) random_weight[j] = 0;
102 ** gen_rand_symbol - generates random password of specified type
105 ** unsigned int - symbol type.
107 ** int - password length or -1 on error.
112 gen_rand_symbol (char *symbol, unsigned int mode)
116 int random_weight[94];
118 int max_weight_element_number = 0;
120 for (j = 0; j <= 93; j++) random_weight[j] = 0;
121 str_pointer = symbol;
123 /* Asign random weight in weight array if mode is present*/
124 for (j = 0; j <= 93 ; j++)
125 if ( ( (mode & smbl[j].type) > 0) &&
126 !( (S_RS & smbl[j].type) > 0))
127 random_weight[j] = 1 + randint(20000);
129 /* Find an element with maximum weight */
130 for (j = 0; j <= 93; j++)
131 if (random_weight[j] > max_weight)
133 max_weight = random_weight[j];
134 max_weight_element_number = j;
136 /* Get password symbol */
137 *str_pointer = smbl[max_weight_element_number].ch;
139 max_weight_element_number = 0;
144 ** is_restricted_symbol - detcts if symbol is restricted rigt now
148 ** int - 0 - not restricted
154 is_restricted_symbol (char symbol)
157 for (j = 0; j <= 93 ; j++)
158 if (symbol == smbl[j].ch)
159 if ((S_RS & smbl[j].type) > 0)