-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpwqgen.c
74 lines (62 loc) · 1.72 KB
/
pwqgen.c
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
/*
* Copyright (c) 2008,2009 by Dmitry V. Levin
* Copyright (c) 2016,2021 by Solar Designer
* See LICENSE
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "passwdqc.h"
static void
print_help(void)
{
puts("Generate quality controllable passphrase.\n"
"\nUsage: pwqgen [options]\n"
"\nValid options are:\n"
" random=N\n"
" set size of randomly-generated passphrase in bits;\n"
" config=FILE\n"
" load config FILE in passwdqc.conf format;\n"
" --version\n"
" print program version and exit;\n"
" -h or --help\n"
" print this help text and exit.");
}
int main(int argc, const char **argv)
{
passwdqc_params_t params;
char *reason, *pass;
int retval;
if (argc > 1 && argv[1][0] == '-') {
if (!strcmp("-h", argv[1]) || !strcmp("--help", argv[1])) {
print_help();
return 0;
}
if (!strcmp("--version", argv[1])) {
printf("pwqgen version %s\n", PASSWDQC_VERSION);
return 0;
}
}
passwdqc_params_reset(¶ms);
if (argc > 1 &&
passwdqc_params_parse(¶ms, &reason, argc - 1, argv + 1)) {
fprintf(stderr, "pwqgen: %s\n",
(reason ? reason : "Out of memory"));
free(reason);
return 1;
}
pass = passwdqc_random(¶ms.qc);
passwdqc_params_free(¶ms);
if (!pass) {
fprintf(stderr, "pwqgen: Failed to generate a passphrase.\n"
"This could happen for a number of reasons: you could have requested\n"
"an impossible passphrase length, or the access to kernel random number\n"
"pool could have failed.\n");
return 1;
}
setvbuf(stdout, NULL, _IONBF, 0);
retval = (puts(pass) >= 0 && fflush(stdout) == 0) ? 0 : 1;
_passwdqc_memzero(pass, strlen(pass));
free(pass);
return retval;
}