Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an old bug and add options to include and enforce additional characters #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pw_phonemes.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct pw_element elements[] = {

#define NUM_ELEMENTS (sizeof(elements) / sizeof (struct pw_element))

void pw_phonemes(char *buf, int size, int pw_flags, char *remove)
void pw_phonemes(char *buf, int size, int pw_flags, char *add, char *remove, char *force)
{
int c, i, len, flags, feature_flags;
int prev, should_be, first;
Expand Down
42 changes: 40 additions & 2 deletions pw_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ static void remove_chars(char *buf, const char *remove)
}
}

static void add_chars(char *buf, const char *add)
{
const char *cp;
char *end;

if (!add)
return;
end = buf + strlen(buf);
for (cp = add; *cp; cp++) {
char *r = strchr(buf, *cp);

if (r != NULL) // skip if already there
continue;
*end++ = *cp;
*end = '\0';
}
}

static int find_chars(char *buf, const char *set)
{
const char *cp;
Expand All @@ -45,7 +63,17 @@ static int find_chars(char *buf, const char *set)
return 0;
}

void pw_rand(char *buf, int size, int pw_flags, char *remove)
static int find_all_chars(char *buf, const char *set)
{
const char *cp;

for (cp = set; *cp; cp++)
if (!strchr(buf, *cp))
return 0;
return 1;
}

void pw_rand(char *buf, int size, int pw_flags, char *add, char *remove, char *force)
{
char ch, *chars, *wchars;
int i, len, feature_flags;
Expand All @@ -61,6 +89,12 @@ void pw_rand(char *buf, int size, int pw_flags, char *remove)
if (pw_flags & PW_SYMBOLS) {
len += strlen(pw_symbols);
}
if (add) {
len += strlen(add);
}
if (force) {
len += strlen(force);
}
chars = malloc(len+1);
if (!chars) {
fprintf(stderr, "Couldn't malloc pw_rand buffer.\n");
Expand All @@ -80,12 +114,14 @@ void pw_rand(char *buf, int size, int pw_flags, char *remove)
if (pw_flags & PW_SYMBOLS) {
strcpy(wchars, pw_symbols);
}
if (remove) {
if (add || remove || force) {
if (pw_flags & PW_AMBIGUOUS)
remove_chars(chars, pw_ambiguous);
if (pw_flags & PW_NO_VOWELS)
remove_chars(chars, pw_vowels);
remove_chars(chars, remove);
add_chars(chars, add); // Override preceding removals
add_chars(chars, force);
if ((pw_flags & PW_DIGITS) &&
!find_chars(chars, pw_digits)) {
fprintf(stderr,
Expand Down Expand Up @@ -135,6 +171,8 @@ void pw_rand(char *buf, int size, int pw_flags, char *remove)
if (feature_flags & (PW_UPPERS | PW_DIGITS | PW_SYMBOLS))
goto try_again;
buf[size] = 0;
if (force && !find_all_chars(buf, force))
goto try_again;
free(chars);
return;
}
27 changes: 24 additions & 3 deletions pwgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ struct option pwgen_options[] = {
{ "numerals", no_argument, 0, 'n'},
{ "symbols", no_argument, 0, 'y'},
{ "num-passwords", required_argument, 0, 'N'},
{ "add-chars", required_argument, 0, 'j' },
{ "remove-chars", required_argument, 0, 'r' },
{ "force-chars", required_argument, 0, 'f' },
{ "secure", no_argument, 0, 's' },
{ "help", no_argument, 0, 'h'},
{ "no-numerals", no_argument, 0, '0' },
Expand All @@ -47,7 +49,7 @@ struct option pwgen_options[] = {
};
#endif

const char *pw_options = "01AaBCcnN:sr:hH:vy";
const char *pw_options = "01AaBCcnN:sj:r:f:hH:vy";

static void usage(void)
{
Expand All @@ -67,9 +69,15 @@ static void usage(void)
fputs(" -y or --symbols\n", stderr);
fputs("\tInclude at least one special symbol in the password\n",
stderr);
fputs(" -j <chars> or --add-chars=<chars>\n", stderr);
fputs("\tAdds/joins characters to the set of characters to "
"generate passwords\n", stderr);
fputs(" -r <chars> or --remove-chars=<chars>\n", stderr);
fputs("\tRemove characters from the set of characters to "
"generate passwords\n", stderr);
fputs(" -f <chars> or --force-chars=<chars>\n", stderr);
fputs("\tForce the usage of specified characters in generated passwords\n",
stderr);
fputs(" -s or --secure\n", stderr);
fputs("\tGenerate completely random passwords\n", stderr);
fputs(" -B or --ambiguous\n", stderr);
Expand All @@ -96,8 +104,10 @@ int main(int argc, char **argv)
int c, i;
int num_cols = -1;
char *buf, *tmp;
char *add=NULL;
char *remove=NULL;
void (*pwgen)(char *inbuf, int size, int pw_flags, char *remove);
char *force=NULL;
void (*pwgen)(char *inbuf, int size, int pw_flags, char *add, char *remove, char *force);

pwgen = pw_phonemes;
pw_number = pw_random_number;
Expand Down Expand Up @@ -160,10 +170,18 @@ int main(int argc, char **argv)
pwgen = pw_rand;
pwgen_flags |= PW_NO_VOWELS;
break;
case 'j':
add = strdup(optarg);
pwgen = pw_rand;
break;
case 'r':
remove = strdup(optarg);
pwgen = pw_rand;
break;
case 'f':
force = strdup(optarg);
pwgen = pw_rand;
break;
case 'h':
case '?':
usage();
Expand Down Expand Up @@ -211,13 +229,16 @@ int main(int argc, char **argv)
exit(1);
}
for (i=0; i < num_pw; i++) {
pwgen(buf, pw_length, pwgen_flags, remove);
pwgen(buf, pw_length, pwgen_flags, add, remove, force);
if (!do_columns || ((i % num_cols) == (num_cols-1)) ||
(i == (num_pw - 1)))
printf("%s\n", buf);
else
printf("%s ", buf);
}
free(buf);
free(add);
free(remove);
free(force);
return 0;
}
4 changes: 2 additions & 2 deletions pwgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ extern const char *pw_ambiguous;
/* Function prototypes */

/* pw_phonemes.c */
extern void pw_phonemes(char *buf, int size, int pw_flags, char *remove);
extern void pw_phonemes(char *buf, int size, int pw_flags, char *add, char *remove, char *force);

/* pw_rand.c */
extern void pw_rand(char *buf, int size, int pw_flags, char *remove);
extern void pw_rand(char *buf, int size, int pw_flags, char *add, char *remove, char *force);

/* randnum.c */
extern int pw_random_number(int max_num);
Expand Down