File: fpm_gpw.c

package info (click to toggle)
fpm2 0.79-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,596 kB
  • ctags: 498
  • sloc: ansic: 7,199; sh: 1,032; makefile: 40
file content (212 lines) | stat: -rw-r--r-- 6,250 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* FIGARO'S PASSWORD MANAGER (FPM)
 * Copyright (C) 2000 John Conneely
 * 
 * FPM is open source / free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * FPM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 *
 * fpm-gpw.c -- Password generation routines
 */

#include <gtk/gtk.h>
#include <math.h>
#include <string.h>

#include "fpm_gpw.h"
#include "fpm.h"
#include "support.h"

#define GPW_CHAR_BUFFER 256

static gint gpw_len;
static gboolean gpw_use_lcase;
static gboolean gpw_use_ucase;
static gboolean gpw_use_num;
static gboolean gpw_use_sym;
static gboolean gpw_no_amb;

static gchar gpw_chars[GPW_CHAR_BUFFER];
static gint gpw_num_chars;




void
fpm_gpw_set_options(	gint 		pw_len,
			gboolean 	use_lcase,
			gboolean 	use_ucase,
			gboolean	use_num,
			gboolean	use_sym,
			gboolean	no_amb)
/* This routine sets what types of characters can be used in
 * generated passwords */
{
  const gchar chars_lcase1[] = "abcdefghijkmnpqrstuvwxyz";
  const gchar chars_lcase2[] = "lo";
  const gchar chars_ucase1[] = "ABCDEFHJKLMNPQRSTUVWXYZ";
  const gchar chars_ucase2[] = "IOG";
  const gchar chars_num1[] = "2345789";
  const gchar chars_num2[] = "016";
  const gchar chars_sym1[] = "!@#$%&*()+=/{}[]:;<>";
  const gchar chars_sym2[] = "_-|,.`'~^";


  gpw_len=pw_len;
  gpw_use_lcase=use_lcase;
  gpw_use_ucase=use_ucase;
  gpw_use_num=use_num;
  gpw_use_sym=use_sym;
  gpw_no_amb=no_amb;

  strncpy(gpw_chars, "", GPW_CHAR_BUFFER);
  if (gpw_use_lcase) strncat(gpw_chars, chars_lcase1, GPW_CHAR_BUFFER-1);
  if (gpw_use_ucase) strncat(gpw_chars, chars_ucase1, GPW_CHAR_BUFFER-1);
  if (gpw_use_num) strncat(gpw_chars, chars_num1, GPW_CHAR_BUFFER-1);
  if (gpw_use_sym) strncat(gpw_chars, chars_sym1, GPW_CHAR_BUFFER-1);
  if (!no_amb)
  {
    if (gpw_use_lcase) strncat(gpw_chars, chars_lcase2, GPW_CHAR_BUFFER-1);
    if (gpw_use_ucase) strncat(gpw_chars, chars_ucase2, GPW_CHAR_BUFFER-1);
    if (gpw_use_num) strncat(gpw_chars, chars_num2, GPW_CHAR_BUFFER-1);
    if (gpw_use_sym) strncat(gpw_chars, chars_sym2, GPW_CHAR_BUFFER-1);
  }

  gpw_num_chars = strlen(gpw_chars);
}

static gboolean
fpm_gpw_get_check_button_value(GtkWidget* win, gchar* check_name)
{
  GtkWidget* widget;
  widget = lookup_widget(win, check_name);
  return(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)));
}

static void
fpm_gpw_set_check_button_value(	GtkWidget* win,	
				gchar* check_name,
				gboolean value)
{
  GtkWidget* widget;
  widget = lookup_widget(win, check_name);
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), value);
}

static void
fpm_put_stats(GtkWidget* win)
/* Update the statistics on the password generator screen.  This consists
 * of the number of possible passwords that could be generated given the
 * specified criteria of length and character set.  The numbers are given
 * in log base 10, and log base 2.  Log base 10 is good because it allows
 * people to have a grasp of how big the number is (a 1 and so many zeros).
 * Log base 2 is good because it shows about how good the key is in terms
 * of bits.
 */
{
  GtkWidget* widget;
  gdouble base10;
  gdouble bits;
  gchar* tmp;

  bits = gpw_len * log(gpw_num_chars) / log(2);
  base10 = bits * log(2) / log(10);
  widget = lookup_widget(win, "entry_base_10");
  tmp = g_strdup_printf("%f", base10);
  gtk_entry_set_text(GTK_ENTRY(widget), tmp);
  g_free(tmp);
  widget = lookup_widget(win, "entry_bits");
  tmp = g_strdup_printf("%f", bits);
  gtk_entry_set_text(GTK_ENTRY(widget), tmp);
  g_free(tmp);
}

void
fpm_gpw_set_from_dialog(GtkWidget* win)
{
  GtkWidget* widget;
  
  widget = lookup_widget(win, "spinbutton_num_char");
  
  
  fpm_gpw_set_options(
	gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)), 
	fpm_gpw_get_check_button_value(win, "checkbutton_lcase"),
	fpm_gpw_get_check_button_value(win, "checkbutton_ucase"),
	fpm_gpw_get_check_button_value(win, "checkbutton_num"),
	fpm_gpw_get_check_button_value(win, "checkbutton_sym"),
	fpm_gpw_get_check_button_value(win, "checkbutton_amb"));
  fpm_put_stats(win);
}




void
fpm_gpw_start_screen(GtkWidget* win)
/* Set the screen to match current rules. */
{
  fpm_gpw_set_check_button_value(win, "checkbutton_lcase", gpw_use_lcase);
  fpm_gpw_set_check_button_value(win, "checkbutton_ucase", gpw_use_ucase);
  fpm_gpw_set_check_button_value(win, "checkbutton_num", gpw_use_num);
  fpm_gpw_set_check_button_value(win, "checkbutton_sym", gpw_use_sym);
  fpm_gpw_set_check_button_value(win, "checkbutton_amb", gpw_no_amb);
  fpm_put_stats(win);
}


static gchar* 
fpm_gpw_gen_password()
/* Actually generate the passwords.  Notice fpm_gpw_set_options must be called
 * before running this routine, because gpw_chars and gpw_num_chars need to
 * be populated.
 * This routine assumes the existance of /dev/random.  Password generation is
 * needs good entropy, which is why I use /dev/random here.  This makes the
 * code less portable, however.  Perhaps we should roll back to another
 * random number generator if /dev/random fails.
 */
{
  gchar* password;
  FILE* rnd;
  guint16 rnd_num;
  gint i, idx;
  size_t result;
  
  password = g_malloc0(gpw_len+1);

  rnd = fopen("/dev/random", "r");

  for (i=0;i<gpw_len;i++)
  {
    result = fread(&rnd_num, sizeof(guint16), 1, rnd);
    idx = floor(gpw_num_chars * rnd_num / pow(2, 16));
    password[i] = gpw_chars[idx];
  }
  fclose(rnd);

  return(password);
}

void
fpm_gpw_fillin_password(GtkWidget* win)
{
  gchar* password;
  GtkWidget* widget;

  password = fpm_gpw_gen_password();
  widget = lookup_widget(win, "entry_gen_password");
  gtk_entry_set_text(GTK_ENTRY(widget), password);
  memset(password, 0, strlen(password));
  g_free(password);
  
}