--- embedtools/src/voucher.c 2012/07/04 13:49:02 1.1.2.1 +++ embedtools/src/voucher.c 2012/07/04 16:15:53 1.1.2.3 @@ -1,8 +1,116 @@ #include "global.h" +#include +#include +#include +io_enableDEBUG; +cfg_root_t cfg; +FILE *output; +char szConfig[MAXPATHLEN] = VOUCHER_CFG; +extern char compiled[], compiledby[], compilehost[]; + + +static void +Usage() +{ + printf( " -= VOUCHER =- management tool\n" + "=== %s === %s@%s ===\n\n" + "Syntax: voucher [options] -r [count]\n" + "\tvoucher [options] -t [voucher [voucher ...]]\n\n" + "\t-v\t\tVerbose (more -v more verbosity)\n" + "\t-r\t\tRequest new voucher(s) mode\n" + "\t-t\t\tTest voucher(s) mode\n" + "\t-g\t\tRequest new RSA pair mode\n" + "\t-c \tConfig file\n" + "\t-o \tOutput file [default=-]\n" + "\n", compiled, compiledby, compilehost); +} + +static void +AtExit() +{ + if (output != stdout) + fclose(output); +} + +static inline int +RedirOutput(const char *name) +{ + AtExit(); + + if (strcmp(name, "-")) { + output = fopen(name, "w+"); + if (!output) { + printf("Error:: can't redirect output #%d - %s\n", + errno, strerror(errno)); + return -1; + } + } else + output = stdout; + return 0; +} + +static int +NewRSA() +{ + RSA *k = RSA_generate_key(VOUCHER_MAX_RSA * 8, 65537, NULL, NULL); + if (!k) { + printf("Error:: can't generate RSA key\n"); + return 2; + } + + PEM_write_RSAPrivateKey(output, k, NULL, NULL, 0, NULL, NULL); + PEM_write_RSA_PUBKEY(output, k); + return 0; +} + + int main(int argc, char **argv) { + char ch, mode = 0; + int rid, cnt = 1; + + output = stdout; + atexit(AtExit); + + while ((ch = getopt(argc, argv, "hvrtgc:o:")) != -1) + switch (ch) { + case 'r': + mode = 1; + break; + case 't': + mode = 2; + break; + case 'g': + return NewRSA(); + case 'c': + strlcpy(szConfig, optarg, sizeof szConfig); + break; + case 'o': + RedirOutput(optarg); + break; + case 'v': + io_incDebug; + break; + case 'h': + default: + Usage(); + return 1; + } + argc -= optind; + argv += optind; + if (!argc || !mode) { + printf("Error:: not enough parameter or unspecified mode ...\n\n"); + Usage(); + return 1; + } + if (cfgLoadConfig(szConfig, &cfg)) { + printf("Error:: load config #%d - %s\n", cfg_GetErrno(), cfg_GetError()); + return 3; + } + + cfgUnloadConfig(&cfg); return 0; }