static const char *title = \
-"Generate Broadcom CFE seeds and passwords for many popular modem/router devices\n"
+"Generate Broadcom CFE seeds and passwords for many popular modem/router devices"
;
+static const float VERSION = 1.3f;
static const char *copyright = \
"Copyright 2015 TJ <hacker@iam.tj>\n"
#include <string.h>
#include <time.h>
-static const float VERSION = 1.3f;
static const size_t TIMESTAMP_SIZE = 8;
static const size_t SEED_SIZE = 12;
static const size_t PASSWORD_SIZE = 8;
static const size_t DATESTRING_SIZE = 20;
static void
-pr_usage()
+pr_usage(int verbose)
{
- fprintf(stderr, "%s\n",
+ fprintf(stderr,
"Usage:\n"
" -v show version\n"
" -s 00:01:02:03:04:05 create seed from MAC address\n"
" -t [00000000] seconds since 1970-01-01 (defaults to NOW) \n"
- " -p [SEED] generate password (with optional seed)\n\n"
- " E.g. -s 01:02:03:04:05 \n"
- " -s 01:02:03:04:05 -p\n"
- " -p 000000030405\n"
+ " -p [SEED] generate password (with optional seed)\n"
+ " -h show additional help\n"
+ "\n"
+ "%s",
+ verbose ? help : ""
);
}
va_end(args);
fprintf(stderr, "Error: %s\n", error_message);
- if (usage) pr_usage();
+ if (usage) pr_usage(usage);
exit(EXIT_FAILURE);
}
main(int argc, char **argv, char **env)
{
int result = 0;
-
- if (argc == 1) {
- pr_usage();
- }
- else {
- unsigned int arg;
- char *MAC_ADDR = NULL;
- char timestamp[TIMESTAMP_SIZE + 1];
- char seed[SEED_SIZE + 1];
- char password[PASSWORD_SIZE + 1];
- char date_string[DATESTRING_SIZE + 1];
- unsigned int opt_seed, opt_pass, opt_ts;
- time_t ts = 0;
- struct tm *t = NULL;
- seed[0] = password[0] = timestamp[0] = 0;
- seed[SEED_SIZE] = password[PASSWORD_SIZE] = 0;
- opt_seed = opt_pass = opt_ts = 0;
- strncpy(timestamp, "00000000", TIMESTAMP_SIZE + 1);
-
- for (arg = 1; arg < (unsigned) argc; ++arg) {
- size_t arg_len = strlen(argv[arg]);
-
- if (argv[arg][0] == '-') {
- switch (argv[arg][1]) {
- case 's':
- opt_seed = 1;
- break;
- case 'p':
- opt_pass = 1;
- break;
- case 't':
- opt_ts = 1;
- break;
- case 'v':
- fprintf(stderr, "Version: %0.2f\n", VERSION);
- }
- } else if (opt_seed == 1) {
- MAC_ADDR = argv[arg];
- ++opt_seed;
- } else if (opt_pass == 1 && opt_seed == 0) {
- if (arg_len != SEED_SIZE)
- pr_error_exit(1, "seed length must be %d characters", SEED_SIZE);
-
- strncpy(seed, argv[arg], SEED_SIZE);
- ++opt_pass;
- } else if (opt_ts == 1) {
- if (arg_len != TIMESTAMP_SIZE)
- pr_error_exit(1, "timestamp length must be %d hexadecimal characters", TIMESTAMP_SIZE);
-
- strncpy(timestamp, argv[arg], TIMESTAMP_SIZE);
- ++opt_ts;
+ unsigned int arg;
+ char *MAC_ADDR = NULL;
+ char timestamp[TIMESTAMP_SIZE + 1];
+ char seed[SEED_SIZE + 1];
+ char password[PASSWORD_SIZE + 1];
+ char date_string[DATESTRING_SIZE + 1];
+ unsigned int opt_seed, opt_pass, opt_ts;
+ time_t ts = 0;
+ struct tm *t = NULL;
+ seed[0] = password[0] = timestamp[0] = 0;
+ seed[SEED_SIZE] = password[PASSWORD_SIZE] = 0;
+ opt_seed = opt_pass = opt_ts = 0;
+ strncpy(timestamp, "00000000", TIMESTAMP_SIZE + 1);
+
+ fprintf(stderr, "%s\nVersion: %0.2f\n%s\n", title, VERSION, copyright);
+
+ for (arg = 1; arg < (unsigned) argc; ++arg) {
+ size_t arg_len = strlen(argv[arg]);
+
+ if (argv[arg][0] == '-') {
+ switch (argv[arg][1]) {
+ case 's':
+ opt_seed = 1;
+ break;
+ case 'p':
+ opt_pass = 1;
+ break;
+ case 't':
+ opt_ts = 1;
+ break;
+ case 'h':
+ pr_usage(1);
+ exit(0);
+ case 'v':
+ fprintf(stderr, "Version: %0.2f\n", VERSION);
}
+ } else if (opt_seed == 1) {
+ MAC_ADDR = argv[arg];
+ ++opt_seed;
+ } else if (opt_pass == 1 && opt_seed == 0) {
+ if (arg_len != SEED_SIZE)
+ pr_error_exit(1, "seed length must be %d characters", SEED_SIZE);
+
+ strncpy(seed, argv[arg], SEED_SIZE);
+ ++opt_pass;
+ } else if (opt_ts == 1) {
+ if (arg_len != TIMESTAMP_SIZE)
+ pr_error_exit(1, "timestamp length must be %d hexadecimal characters", TIMESTAMP_SIZE);
+
+ strncpy(timestamp, argv[arg], TIMESTAMP_SIZE);
+ ++opt_ts;
}
- if (! opt_seed && ! opt_pass)
- pr_usage();
- else if (opt_seed && opt_seed != 2)
- pr_error_exit(1, "seed requires MAC-ADDRESS");
- else if (! opt_seed && opt_pass && opt_pass != 2)
- pr_error_exit(1, "password on its own requires a pre-generated seed");
- else if (opt_seed && opt_pass && opt_pass != 1)
- pr_error_exit(1, "generating seed and password; cannot also accept pre-generated seed");
- else if (opt_pass == 2 && opt_ts)
- pr_error_exit(1, "seed already contains a timestamp; cannot over-ride it");
- else if (opt_ts == 1 || opt_pass == 2) { // no timestamp provided; use NOW
- ts = time(NULL);
- if (ts)
- snprintf(timestamp, TIMESTAMP_SIZE + 1, "%08lX", ts);
- }
+ }
+ if (! opt_seed && ! opt_pass) {
+ pr_usage(0);
+ exit(0);
+ }
+ else if (opt_seed && opt_seed != 2)
+ pr_error_exit(1, "seed requires MAC-ADDRESS");
+ else if (! opt_seed && opt_pass && opt_pass != 2)
+ pr_error_exit(1, "password on its own requires a pre-generated seed");
+ else if (opt_seed && opt_pass && opt_pass != 1)
+ pr_error_exit(1, "generating seed and password; cannot also accept pre-generated seed");
+ else if (opt_pass == 2 && opt_ts)
+ pr_error_exit(1, "seed already contains a timestamp; cannot over-ride it");
+ else if (opt_ts == 1 || opt_pass == 2) { // no timestamp provided; use NOW
+ ts = time(NULL);
+ if (ts)
+ snprintf(timestamp, TIMESTAMP_SIZE + 1, "%08lX", ts);
+ }
- if (opt_pass == 2) { // try to figure out the correct date-time from the seed
- // inherits the most significant 2 characters from the NOW time
- strncpy(timestamp+2, seed, 6);
- time_t tmp;
- if (sscanf(timestamp, "%08lx", &tmp))
- if (tmp > ts-3600 && tmp < ts+3600) // timestamps are so close they must be for the same date
- ts = tmp;
- }
+ if (opt_pass == 2) { // try to figure out the correct date-time from the seed
+ // inherits the most significant 2 characters from the NOW time
+ strncpy(timestamp+2, seed, 6);
+ time_t tmp;
+ if (sscanf(timestamp, "%08lx", &tmp))
+ if (tmp > ts-3600 && tmp < ts+3600) // timestamps are so close they must be for the same date
+ ts = tmp;
+ }
- if(opt_ts) { // ts needs to be valid to be converted to a time string
- if(! sscanf(timestamp, "%08lx", &ts))
- pr_error_exit(1, "converting timestamp string ('%s') to number", timestamp);
- }
- t = gmtime(&ts);
- strftime(date_string, DATESTRING_SIZE, "%F %T", t);
-
- if (opt_seed)
- if (! generate_seed(MAC_ADDR, timestamp, seed))
- pr_error_exit(1, "unable to generate seed; aborting");
- if (opt_pass)
- if (! generate_pass(seed, password))
- pr_error_exit(0, "unable to generate password");
-
- if (opt_seed || opt_pass)
- printf("MAC address: %s Timestamp: %s (%s) Seed: %s Password: %s\n", MAC_ADDR, timestamp, date_string, seed, password);
+ if(opt_ts) { // ts needs to be valid to be converted to a time string
+ if(! sscanf(timestamp, "%08lx", &ts))
+ pr_error_exit(1, "converting timestamp string ('%s') to number", timestamp);
}
+ t = gmtime(&ts);
+ strftime(date_string, DATESTRING_SIZE, "%F %T", t);
+
+ if (opt_seed)
+ if (! generate_seed(MAC_ADDR, timestamp, seed))
+ pr_error_exit(1, "unable to generate seed; aborting");
+ if (opt_pass)
+ if (! generate_pass(seed, password))
+ pr_error_exit(0, "unable to generate password");
+
+ if (opt_seed || opt_pass)
+ printf("MAC address: %s Timestamp: %s (%s) Seed: %s Password: %s\n", MAC_ADDR, timestamp, date_string, seed, password);
return result;
}