]> err.no Git - yubikey-personalization/commitdiff
start on import code
authorKlas Lindfors <klas@yubico.com>
Fri, 12 Apr 2013 14:03:49 +0000 (16:03 +0200)
committerKlas Lindfors <klas@yubico.com>
Fri, 12 Apr 2013 14:03:49 +0000 (16:03 +0200)
ykpers-json.c
ykpersonalize.c

index aed86e36db6dd7a6e8e213017cbc0d32c23bde66..c3538576d85402cc3f61423d60d44a809d9180a0 100644 (file)
@@ -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;
 }
 
index ca25f9e9f9180d7e164ceab51dfd8f27bbf72df0..d8b629f6b72df486373179141bda8afe379c87cc 100644 (file)
 
 #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;
                        }
                }