From: Klas Lindfors Date: Fri, 12 Apr 2013 14:03:49 +0000 (+0200) Subject: start on import code X-Git-Tag: v1.13.0~43 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f6d472a36ed33174a82b50008bb9393e31257aef;p=yubikey-personalization start on import code --- diff --git a/ykpers-json.c b/ykpers-json.c index aed86e3..c353857 100644 --- a/ykpers-json.c +++ b/ykpers-json.c @@ -165,7 +165,38 @@ int ykp_json_export_cfg(const YKP_CONFIG *cfg, char *json, size_t len) { } int ykp_json_import_cfg(const char *json, size_t len, YKP_CONFIG *cfg) { - ykp_errno = YKP_ENOTYETIMPL; + if(cfg) { + json_object *jobj = json_tokener_parse(json); + json_object *yprod_json = json_object_object_get(jobj, "yubiProdConfig"); + json_object *jmode = json_object_object_get(yprod_json, "mode"); + const char *raw_mode = json_object_get_string(jmode); + int mode = MODE_OTP_YUBICO; + + struct map_st *p; + + for(p = _modes_map; p->flag; p++) { + if(strcmp(raw_mode, p->json_text) == 0) { + mode = p->flag; + break; + } + } + + if(mode == MODE_OATH_HOTP) { + ykp_set_tktflag_OATH_HOTP(cfg, true); + } else if(mode == MODE_CHAL_HMAC) { + ykp_set_tktflag_CHAL_RESP(cfg, true); + ykp_set_cfgflag_CHAL_HMAC(cfg, true); + } else if(mode == MODE_CHAL_YUBICO) { + ykp_set_tktflag_CHAL_RESP(cfg, true); + ykp_set_cfgflag_CHAL_YUBICO(cfg, true); + } else if(mode == MODE_STATIC_TICKET) { + ykp_set_cfgflag_STATIC_TICKET(cfg, true); + } + + json_object_put(jobj); + return 1; + } + ykp_errno = YKP_EINVAL; return 0; } diff --git a/ykpersonalize.c b/ykpersonalize.c index ca25f9e..d8b629f 100644 --- a/ykpersonalize.c +++ b/ykpersonalize.c @@ -41,11 +41,6 @@ #include "ykpers-args.h" -static int reader(char *buf, size_t count, void *stream) -{ - return (int)fread(buf, 1, count, (FILE *)stream); -} - int main(int argc, char **argv) { FILE *inf = NULL; const char *infname = NULL; @@ -60,7 +55,7 @@ int main(int argc, char **argv) YKP_CONFIG *cfg = ykp_alloc(); YK_STATUS *st = ykds_alloc(); bool autocommit = false; - char exported_data[1024]; + char data[1024]; /* Options */ char *salt = NULL; @@ -180,7 +175,9 @@ int main(int argc, char **argv) } if (inf) { - if (!ykp_read_config(cfg, reader, inf)) + if(!fread(data, 1, 1024, inf)) + goto err; + if (!ykp_import_config(data, strlen(data), cfg, data_format)) goto err; } else if (! aesviahash && ! zap && (ykp_command(cfg) == SLOT_CONFIG || ykp_command(cfg) == SLOT_CONFIG2)) { char passphrasebuf[256]; size_t passphraselen; @@ -200,9 +197,9 @@ int main(int argc, char **argv) goto err; } - ykp_export_config(cfg, exported_data, 1024, data_format); + ykp_export_config(cfg, data, 1024, data_format); if (outf) { - if(!(fwrite(exported_data, 1, strlen(exported_data), outf))) { + if(!(fwrite(data, 1, strlen(data), outf))) { goto err; } } else { @@ -224,7 +221,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); } - fwrite(exported_data, 1, strlen(exported_data), stderr); + fwrite(data, 1, strlen(data), stderr); } fprintf(stderr, "\nCommit? (y/n) [n]: "); if (autocommit) { @@ -234,7 +231,6 @@ int main(int argc, char **argv) if (!fgets(commitbuf, sizeof(commitbuf), stdin)) { perror ("fgets"); - exit_code; goto err; } }