From: Klas Lindfors Date: Fri, 19 Oct 2012 07:45:27 +0000 (+0200) Subject: add -z switch for deleting a configuration X-Git-Tag: v1.9.0~15 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9df0acaf1152a5300325e4ebe90617bfbf66a71a;p=yubikey-personalization add -z switch for deleting a configuration --- diff --git a/tests/test_args_to_config.c b/tests/test_args_to_config.c index 33b8966..3fcf248 100644 --- a/tests/test_args_to_config.c +++ b/tests/test_args_to_config.c @@ -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); diff --git a/ykpers-args.c b/ykpers-args.c index f577cbc..d17ba23 100644 --- a/ykpers-args.c +++ b/ykpers-args.c @@ -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) { diff --git a/ykpers-args.h b/ykpers-args.h index 02c3bb3..6611a9a 100644 --- a/ykpers-args.h +++ b/ykpers-args.h @@ -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); diff --git a/ykpersonalize.c b/ykpersonalize.c index 9f2b7a9..6da344e 100644 --- a/ykpersonalize.c +++ b/ykpersonalize.c @@ -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");