2015-08-01 20:54:10 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of dicepwgen
|
|
|
|
|
*
|
2016-08-23 17:34:57 +02:00
|
|
|
* Copyright (C) 2015-2016 T.v.Dein.
|
2015-08-01 20:54:10 +02:00
|
|
|
*
|
|
|
|
|
* This program is 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 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
* You can contact me by mail: <tom AT vondein DOT org>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "dicepwgen.h"
|
|
|
|
|
|
|
|
|
|
int usage() {
|
|
|
|
|
fprintf(stderr,
|
2016-08-23 17:34:57 +02:00
|
|
|
"Generate a random diceware passphrase\n"
|
|
|
|
|
"Usage: dice [-tcfvhd]\n"
|
|
|
|
|
"Options: \n"
|
|
|
|
|
"-t --humantoss Asks interactively for rolled dices\n"
|
|
|
|
|
"-c --wordcount <count> Number of words (default: 4)\n"
|
|
|
|
|
"-f --dictfile <dictfile> Dictionary file to use (default:\n"
|
|
|
|
|
" /usr/share/dict/american-english)\n"
|
|
|
|
|
"-l --minlen <count> Minimum word len (default: 5)\n"
|
|
|
|
|
"-m --maxlen <count> Maximum word len (default: 10)\n"
|
|
|
|
|
"-n --dontjump Use all words in the dict file, e.g.\n"
|
|
|
|
|
" if it is an original diceware list\n"
|
|
|
|
|
"-y --symbols Replace space with -, add non-letters\n"
|
|
|
|
|
"-d --debug Enable debug output\n"
|
|
|
|
|
"-v --version Print program version\n"
|
2025-11-05 22:31:56 +01:00
|
|
|
"-h -? --help Print this help screen\n");
|
2015-08-01 20:54:10 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
2022-07-17 19:22:54 +02:00
|
|
|
|
|
|
|
|
int WMIN, WMAX, humantoss, verbose, dontjump, symbols;
|
|
|
|
|
|
2025-11-05 22:31:56 +01:00
|
|
|
int main(int argc, char **argv) {
|
2015-08-01 20:54:10 +02:00
|
|
|
int count = 4;
|
|
|
|
|
char *dictfile = NULL;
|
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
|
|
WMIN = 6;
|
|
|
|
|
WMAX = 10;
|
2016-08-23 17:34:57 +02:00
|
|
|
humantoss = verbose = dontjump = symbols = 0;
|
2025-11-05 22:31:56 +01:00
|
|
|
|
2015-08-01 20:54:10 +02:00
|
|
|
static struct option longopts[] = {
|
2025-11-05 22:31:56 +01:00
|
|
|
{"wordcount", required_argument, NULL, 'c'},
|
|
|
|
|
{"minlen", required_argument, NULL, 'l'},
|
|
|
|
|
{"maxlen", required_argument, NULL, 'm'},
|
|
|
|
|
{"humantoss", required_argument, NULL, 't'},
|
|
|
|
|
{"dictfile", required_argument, NULL, 'f'},
|
|
|
|
|
{"dontjump", no_argument, NULL, 'n'},
|
|
|
|
|
{"symbols", no_argument, NULL, 'y'},
|
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
|
{"debug", no_argument, NULL, 'd'},
|
2015-08-01 20:54:10 +02:00
|
|
|
};
|
|
|
|
|
|
2025-11-05 22:31:56 +01:00
|
|
|
while ((opt = getopt_long(argc, argv, "l:m:tf:c:vh?dny", longopts, NULL)) !=
|
|
|
|
|
-1) {
|
|
|
|
|
switch (opt) {
|
|
|
|
|
case 'v':
|
|
|
|
|
fprintf(stderr, "This is %s version %s\n", argv[0], VERSION);
|
|
|
|
|
return 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'h':
|
|
|
|
|
case '?':
|
|
|
|
|
return usage();
|
|
|
|
|
break;
|
|
|
|
|
case 'c':
|
|
|
|
|
count = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'l':
|
|
|
|
|
WMIN = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'm':
|
|
|
|
|
WMAX = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
humantoss = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'y':
|
|
|
|
|
symbols = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
|
|
|
|
verbose++;
|
|
|
|
|
break;
|
|
|
|
|
case 'n':
|
|
|
|
|
dontjump = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'f':
|
|
|
|
|
dictfile = malloc(strlen(optarg));
|
|
|
|
|
strncpy(dictfile, optarg, strlen(optarg));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2015-08-01 20:54:10 +02:00
|
|
|
return usage();
|
|
|
|
|
break;
|
2025-11-05 22:31:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dictfile == NULL) {
|
|
|
|
|
dictfile = STRINGIZE_VALUE_OF(DICTFILE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dontjump) {
|
|
|
|
|
WMIN = 0;
|
|
|
|
|
WMAX = 128;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug(" using dictfile: %s", dictfile);
|
|
|
|
|
debug("minimum word length: %d", WMIN);
|
|
|
|
|
debug("maximum word length: %d", WMAX);
|
|
|
|
|
if (humantoss)
|
|
|
|
|
debug("user rolls dices");
|
|
|
|
|
else
|
|
|
|
|
debug("program rolls dices");
|
|
|
|
|
|
|
|
|
|
getwords(dictfile, count);
|
|
|
|
|
|
|
|
|
|
return 0;
|
2015-08-01 20:54:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void getwords(char *dictfile, int count) {
|
|
|
|
|
/*
|
|
|
|
|
initiate dice tossing, extract matching number of words
|
|
|
|
|
froom the wordlist and print it.
|
|
|
|
|
*/
|
|
|
|
|
char **words;
|
|
|
|
|
int i, pos, one, two, three, four, five;
|
|
|
|
|
int *tossed;
|
2016-08-23 17:34:57 +02:00
|
|
|
char sep = ' ';
|
2015-08-01 20:54:10 +02:00
|
|
|
unsigned char *tosses;
|
2025-11-05 22:31:56 +01:00
|
|
|
|
2015-08-01 20:54:10 +02:00
|
|
|
words = fetch_dict(dictfile);
|
|
|
|
|
|
|
|
|
|
tossed = malloc(count * sizeof(int));
|
2025-11-05 22:31:56 +01:00
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
2015-08-02 15:22:12 +02:00
|
|
|
tosses = toss(5, i);
|
2015-08-01 20:54:10 +02:00
|
|
|
|
2025-11-05 22:31:56 +01:00
|
|
|
one = tosses[0] * 10000;
|
|
|
|
|
two = tosses[1] * 1000;
|
|
|
|
|
three = tosses[2] * 100;
|
|
|
|
|
four = tosses[3] * 10;
|
|
|
|
|
five = tosses[4];
|
|
|
|
|
|
2015-08-01 20:54:10 +02:00
|
|
|
pos = one + two + three + four + five;
|
|
|
|
|
|
|
|
|
|
tossed[i] = pos;
|
|
|
|
|
|
|
|
|
|
free(tosses);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 22:31:56 +01:00
|
|
|
if (symbols)
|
2016-08-23 17:34:57 +02:00
|
|
|
sep = '-';
|
2025-11-05 22:31:56 +01:00
|
|
|
|
|
|
|
|
for (i = 0; i < count - 1; i++) {
|
2016-08-23 17:34:57 +02:00
|
|
|
fprintf(stdout, "%s%c", words[tossed[i]], sep);
|
2015-08-01 20:54:10 +02:00
|
|
|
}
|
2025-11-05 22:31:56 +01:00
|
|
|
fprintf(stdout, "%s", words[tossed[count - 1]]);
|
2015-08-01 20:54:10 +02:00
|
|
|
|
2025-11-05 22:31:56 +01:00
|
|
|
if (symbols)
|
2016-08-23 17:34:57 +02:00
|
|
|
fprintf(stdout, "%%8");
|
2025-11-05 22:31:56 +01:00
|
|
|
|
2015-08-01 20:54:10 +02:00
|
|
|
fprintf(stdout, "\n");
|
2025-11-05 22:31:56 +01:00
|
|
|
|
2015-08-01 20:54:10 +02:00
|
|
|
free(tossed);
|
|
|
|
|
|
2025-11-05 22:31:56 +01:00
|
|
|
for (i = 0; i < 66667; i++)
|
|
|
|
|
if (words[i] != NULL)
|
2015-08-01 20:54:10 +02:00
|
|
|
free(words[i]);
|
|
|
|
|
|
|
|
|
|
free(words);
|
|
|
|
|
}
|