]> err.no Git - yubikey-personalization/commitdiff
add -z switch for deleting a configuration
authorKlas Lindfors <klas@yubico.com>
Fri, 19 Oct 2012 07:45:27 +0000 (09:45 +0200)
committerKlas Lindfors <klas@yubico.com>
Fri, 19 Oct 2012 07:45:27 +0000 (09:45 +0200)
tests/test_args_to_config.c
ykpers-args.c
ykpers-args.h
ykpersonalize.c

index 33b8966c30fc8b6185da92d86d7cf165fa6d5c0e..3fcf24823b3b779001cebca9164f9a6a26b8cd08 100644 (file)
@@ -115,6 +115,7 @@ int _test_config (YKP_CONFIG *cfg, YK_STATUS *st, int argc, char **argv)
        char *salt = NULL;
        char ndef[128];
        char ndef_type = NULL;
+       bool zap = false;
 
        int rc;
 
@@ -130,7 +131,7 @@ int _test_config (YKP_CONFIG *cfg, YK_STATUS *st, int argc, char **argv)
                            &autocommit, salt,
                            st, &verbose,
                            access_code, &use_access_code,
-                           &aesviahash, &ndef_type, ndef,
+                           &aesviahash, &ndef_type, ndef, &zap,
                            &exit_code);
 
        return rc;
@@ -292,6 +293,7 @@ int _test_non_config_args(void)
        char *salt = NULL;
        char ndef[128];
        char ndef_type = NULL;
+       bool zap = false;
 
        char *argv[] = {
                "unittest", "-1", "-sout", "-iin", "-c313233343536", "-y", "-v",
@@ -312,7 +314,7 @@ int _test_non_config_args(void)
                            &autocommit, salt,
                            st, &verbose,
                            access_code, &use_access_code,
-                           &aesviahash, &ndef_type, ndef,
+                           &aesviahash, &ndef_type, ndef, &zap,
                            &exit_code);
        assert(rc == 1);
        i = strcmp(infname, "in"); assert(i == 0);
index f577cbc6d291e7c8d992f3b21e600810ff66fb9c..d17ba23b6c5df75728a06b05a5741fd8417d76ab 100644 (file)
@@ -60,6 +60,7 @@ const char *usage =
 "          are set by default.\n"
 "-x        swap the configuration in slot 1 and 2.  This is for YubiKey 2.3\n"
 "          and newer only\n"
+"-z        delete the configuration in slot 1 or 2.\n"
 "-sFILE    save configuration to FILE instead of key.\n"
 "          (if FILE is -, send to stdout)\n"
 "-iFILE    read configuration from FILE.\n"
@@ -144,7 +145,7 @@ const char *usage =
 "-v        verbose\n"
 "-h        help (this text)\n"
 ;
-const char *optstring = "u12xa:c:n:t:hi:o:s:vy";
+const char *optstring = "u12xza:c:n:t:hi:o:s:vy";
 
 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);
@@ -218,7 +219,7 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
                   bool *autocommit, char *salt,
                   YK_STATUS *st, bool *verbose,
                   unsigned char *access_code, bool *use_access_code,
-                  bool *aesviahash, char *ndef_type, char *ndef,
+                  bool *aesviahash, char *ndef_type, char *ndef, bool *zap,
                   int *exit_code)
 {
        int c;
@@ -360,6 +361,24 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
                        }
                        swap_seen = true;
                        break;
+               case 'z':
+                       if (swap_seen) {
+                               fprintf(stderr, "Swap (-x) and zap (-z) can't be combined.\n");
+                               *exit_code = 1;
+                               return 0;
+                       }
+                       if (update_seen) {
+                               fprintf(stderr, "Update (-u) and zap (-z) can't be combined.\n");
+                               *exit_code = 1;
+                               return 0;
+                       }
+                       if (!slot_chosen) {
+                               fprintf(stderr, "A slot must be chosen (-1 / -2) before adding zap (-z)\n");
+                               *exit_code = 1;
+                               return 0;
+                       }
+                       *zap = true;
+                       break;
                case 'i':
                        *infname = optarg;
                        break;
@@ -395,7 +414,7 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
                        if(!*ndef_type) {
                                *ndef_type = 'U';
                        }
-                       if (slot_chosen || swap_seen || update_seen || option_seen) {
+                       if (slot_chosen || swap_seen || update_seen || option_seen || *zap) {
                                fprintf(stderr, "Ndef (-n/-t) must be used on it's own.\n");
                                *exit_code = 1;
                                return 0;
@@ -407,6 +426,11 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
                        ndef_seen = true;
                        break;
                case 'o':
+                       if (*zap) {
+                               fprintf(stderr, "No options can be given with zap (-z).\n");
+                               *exit_code = 1;
+                               return 0;
+                       }
                        if (strncmp(optarg, "salt=", 5) == 0)
                                salt = strdup(optarg+5);
                        else if (strncmp(optarg, "fixed=", 6) == 0) {
index 02c3bb326cf0963fb2e3305b0c215d948d77e634..6611a9a26188e66cc769ff1e71028095511ce39e 100644 (file)
@@ -38,7 +38,7 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
                   bool *autocommit, char *salt,
                   YK_STATUS *st, bool *verbose,
                   unsigned char *access_code, bool *use_access_code,
-                  bool *aesviahash, char *ndef_type, char *ndef,
+                  bool *aesviahash, char *ndef_type, char *ndef, bool *zap,
                   int *exit_code);
 
 void report_yk_error(void);
index 9f2b7a9911de548822980120c410947672c96ca9..6da344e718d979da55bb8e5916043b225c2ee59e 100644 (file)
@@ -67,6 +67,7 @@ int main(int argc, char **argv)
        char *salt = NULL;
        char ndef_string[128] = {0};
        char ndef_type;
+       bool zap = false;
 
        bool error = false;
        int exit_code = 0;
@@ -126,7 +127,7 @@ int main(int argc, char **argv)
                             &autocommit, salt,
                             st, &verbose,
                             access_code, &use_access_code,
-                            &aesviahash, &ndef_type, ndef_string,
+                            &aesviahash, &ndef_type, ndef_string, &zap,
                             &exit_code)) {
                goto err;
        }
@@ -180,7 +181,7 @@ int main(int argc, char **argv)
        if (inf) {
                if (!ykp_read_config(cfg, reader, inf))
                        goto err;
-       } else if (! aesviahash && (ykp_command(cfg) == SLOT_CONFIG || ykp_command(cfg) == SLOT_CONFIG2)) {
+       } else if (! aesviahash && ! zap && (ykp_command(cfg) == SLOT_CONFIG || ykp_command(cfg) == SLOT_CONFIG2)) {
                char passphrasebuf[256]; size_t passphraselen;
                fprintf(stderr, "Passphrase to create AES key: ");
                fflush(stderr);
@@ -203,6 +204,8 @@ int main(int argc, char **argv)
                        fprintf(stderr, "Configuration in slot 1 and 2 will be swapped\n");
                } else if(ykp_command(cfg) == SLOT_NDEF) {
                        fprintf(stderr, "New NDEF URI will be written\n");
+               } else if(zap) {
+                       fprintf(stderr, "Configuration in slot %d will be deleted\n", ykp_config_num(cfg));
                } else {
                        if (ykp_command(cfg) == SLOT_CONFIG || ykp_command(cfg) == SLOT_CONFIG2) {
                                fprintf(stderr, "Configuration data to be written to key configuration %d:\n\n", ykp_config_num(cfg));
@@ -244,8 +247,13 @@ int main(int argc, char **argv)
                                }
                                ykp_free_ndef(ndef);
                        } else {
+                               YK_CONFIG *ycfg = NULL;
+                               /* if we're deleting a slot we send the configuration as NULL */
+                               if (!zap) {
+                                       ycfg = ykp_core_config(cfg);
+                               }
                                if (!yk_write_command(yk,
-                                                       ykp_core_config(cfg), ykp_command(cfg),
+                                                       ycfg, ykp_command(cfg),
                                                        use_access_code ? access_code : NULL)) {
                                        if (verbose)
                                                printf(" failure\n");