From: Klas Lindfors Date: Fri, 12 Apr 2013 12:11:27 +0000 (+0200) Subject: implement both export formats in the cli X-Git-Tag: v1.13.0~46 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d2cfb0c38ecb0850be91aa717ed08bbe8192aca;p=yubikey-personalization implement both export formats in the cli --- diff --git a/ykpers-args.c b/ykpers-args.c index da63739..98e7db1 100644 --- a/ykpers-args.c +++ b/ykpers-args.c @@ -65,6 +65,8 @@ const char *usage = " (if FILE is -, send to stdout)\n" "-iFILE read configuration from FILE.\n" " (if FILE is -, read from stdin)\n" +"-fformat set the data format for -s and -i (and for standard write to console\n" +" valid values are json or legacy.\n" "-aXXX.. The AES secret key as a 32 (or 40 for OATH-HOTP/HMAC CHAL-RESP)\n" " char hex value (not modhex)\n" "-cXXX.. A 12 char hex value (not modhex) to use as access code for programming\n" @@ -156,7 +158,7 @@ const char *usage = "-v verbose\n" "-h help (this text)\n" ; -const char *optstring = "u12xza:c:n:t:hi:o:s:vym:S::"; +const char *optstring = "u12xza:c:n:t:hi:o:s:f:vym:S::"; static int _set_fixed(char *opt, YKP_CONFIG *cfg); static int _format_decimal_as_hex(uint8_t *dst, size_t dst_len, uint8_t *src); @@ -227,6 +229,7 @@ void report_yk_error(void) */ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk, const char **infname, const char **outfname, + int *data_format, bool *autocommit, char *salt, YK_STATUS *st, bool *verbose, unsigned char *access_code, bool *use_access_code, @@ -370,6 +373,17 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk, case 's': *outfname = optarg; break; + case 'f': + if(strcmp(optarg, "json") == 0) { + *data_format = YKP_FORMAT_JSON; + } else if(strcmp(optarg, "legacy") == 0) { + *data_format = YKP_FORMAT_LEGACY; + } else { + fprintf(stderr, "The only valid formats to -f is json and legacy.\n"); + *exit_code = 1; + return 0; + } + break; case 'a': *aesviahash = true; aeshash = optarg; diff --git a/ykpers-args.h b/ykpers-args.h index 8ee82eb..a0aadee 100644 --- a/ykpers-args.h +++ b/ykpers-args.h @@ -35,6 +35,7 @@ const char *usage; int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk, const char **infname, const char **outfname, + int *data_format, bool *autocommit, char *salt, YK_STATUS *st, bool *verbose, unsigned char *access_code, bool *use_access_code, diff --git a/ykpersonalize.c b/ykpersonalize.c index c275500..ca25f9e 100644 --- a/ykpersonalize.c +++ b/ykpersonalize.c @@ -45,15 +45,12 @@ static int reader(char *buf, size_t count, void *stream) { return (int)fread(buf, 1, count, (FILE *)stream); } -static int writer(const char *buf, size_t count, void *stream) -{ - return (int)fwrite(buf, 1, count, (FILE *)stream); -} int main(int argc, char **argv) { FILE *inf = NULL; const char *infname = NULL; FILE *outf = NULL; const char *outfname = NULL; + int data_format = YKP_FORMAT_LEGACY; bool verbose = false; bool aesviahash = false; bool use_access_code = false; @@ -63,6 +60,7 @@ int main(int argc, char **argv) YKP_CONFIG *cfg = ykp_alloc(); YK_STATUS *st = ykds_alloc(); bool autocommit = false; + char exported_data[1024]; /* Options */ char *salt = NULL; @@ -126,6 +124,7 @@ int main(int argc, char **argv) /* Parse all arguments in a testable way */ if (! args_to_config(argc, argv, cfg, yk, &infname, &outfname, + &data_format, &autocommit, salt, st, &verbose, access_code, &use_access_code, @@ -201,9 +200,11 @@ int main(int argc, char **argv) goto err; } + ykp_export_config(cfg, exported_data, 1024, data_format); if (outf) { - if (!ykp_write_config(cfg, writer, outf)) + if(!(fwrite(exported_data, 1, strlen(exported_data), outf))) { goto err; + } } else { char commitbuf[256]; size_t commitlen; @@ -223,7 +224,7 @@ int main(int argc, char **argv) } else { fprintf(stderr, "Configuration data to be updated in key configuration %d:\n\n", ykp_command(cfg) == SLOT_UPDATE1 ? 1 : 2); } - ykp_write_config(cfg, writer, stderr); + fwrite(exported_data, 1, strlen(exported_data), stderr); } fprintf(stderr, "\nCommit? (y/n) [n]: "); if (autocommit) {