]> err.no Git - yubikey-personalization/commitdiff
implement dry-run for the cli
authorKlas Lindfors <klas@yubico.com>
Thu, 25 Apr 2013 09:21:05 +0000 (11:21 +0200)
committerKlas Lindfors <klas@yubico.com>
Thu, 25 Apr 2013 09:21:05 +0000 (11:21 +0200)
tests/test_args_to_config.c
ykpers-args.c
ykpers-args.h
ykpersonalize.1
ykpersonalize.c

index 62a5ba621dcffdd78edb6be8f273a8e6956ef9b0..97169b4b4040cf8c95bfe2e57e5a5f7c27b1ec54 100644 (file)
@@ -104,6 +104,7 @@ int _test_config (YKP_CONFIG *cfg, YK_STATUS *st, int argc, char **argv)
        const char *infname = NULL;
        const char *outfname = NULL;
        bool verbose = false;
+       bool dry_run = false;
        bool aesviahash = false;
        bool use_access_code = false;
        unsigned char access_code[256];
@@ -140,7 +141,7 @@ int _test_config (YKP_CONFIG *cfg, YK_STATUS *st, int argc, char **argv)
                            &infname, &outfname,
                            &data_format,
                            &autocommit, salt,
-                           st, &verbose,
+                           st, &verbose, &dry_run,
                            access_code, &use_access_code,
                            &aesviahash, &ndef_type, ndef, &usb_mode, &zap,
                            scan_map, &exit_code);
@@ -292,6 +293,7 @@ int _test_non_config_args(void)
        const char *infname = NULL;
        const char *outfname = NULL;
        bool verbose = false;
+       bool dry_run = false;
        bool aesviahash = false;
        bool use_access_code = false;
        unsigned char access_code[256];
@@ -334,7 +336,7 @@ int _test_non_config_args(void)
                            &infname, &outfname,
                            &data_format,
                            &autocommit, salt,
-                           st, &verbose,
+                           st, &verbose, &dry_run,
                            access_code, &use_access_code,
                            &aesviahash, &ndef_type, ndef, &usb_mode, &zap,
                            scan_map, &exit_code);
index 7ed0a4919d3eb3b5a18244f3cdd71c1ca6623147..4c5d4e0592e49527fb21da61d96b7013887a5b7c 100644 (file)
@@ -154,10 +154,12 @@ const char *usage =
 "\n"
 "-y        always commit (do not prompt)\n"
 "\n"
+"-d        dry-run (don't write anything to key)\n"
+"\n"
 "-v        verbose\n"
 "-h        help (this text)\n"
 ;
-const char *optstring = "u12xza:c:n:t:hi:o:s:f:vym:S::";
+const char *optstring = "u12xza:c:n:t:hi:o:s:f:dvym: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);
@@ -230,7 +232,7 @@ 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,
+                  YK_STATUS *st, bool *verbose, bool *dry_run,
                   unsigned char *access_code, bool *use_access_code,
                   bool *aesviahash, char *ndef_type, char *ndef,
                   unsigned char *usb_mode, bool *zap,
@@ -666,6 +668,9 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
                                return 0;
                        }
                        break;
+               case 'd':
+                       *dry_run = true;
+                       break;
                case 'v':
                        *verbose = true;
                        break;
index a0aadeeaafaa7b8d8a22d3fe45e962cc38c2ccd2..61e8c70f88e21a7f42a08c6591b760ae538ede5f 100644 (file)
@@ -37,7 +37,7 @@ 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,
+                  YK_STATUS *st, bool *verbose, bool *dry_run,
                   unsigned char *access_code, bool *use_access_code,
                   bool *aesviahash, char *ndef_type, char *ndef, unsigned char *usb_mode, 
                   bool *zap, unsigned char *scan_bin, int *exit_code);
index 8b7fcedd62980b7a28fa10e0ef1c92b2c072177b..e643b08ccb0643820cea38eb7b572ecdd6e9bfde 100644 (file)
@@ -37,7 +37,7 @@ ykpersonalize - personalize YubiKey OTP tokens
 .SH SYNOPSIS
 .B ykpersonalize
 [\fI-1\fR | \fI-2\fR] [\fI-sfile\fR] [\fI-ifile\fR] [\fI-fformat\fR] [\fI-axxx\fR] [\fI-cxxx\fR]
-[\fI-ooption\fR] [\fI-y\fR] [\fI-v\fR] [\fI-h\fR] [\fI-n\fR] [\fI-t\fR] [\fI-u\fR] [\fI-x\fR]
+[\fI-ooption\fR] [\fI-y\fR] [\fI-v\fR] [\fI-d\fR] [\fI-h\fR] [\fI-n\fR] [\fI-t\fR] [\fI-u\fR] [\fI-x\fR]
 [\fI-z\fR] [\fI-m\fR] [\fI-S\fR]
 .\".SH DESCRIPTION
 .\" Add any additional description here
@@ -124,6 +124,9 @@ Set/clear ticket flag, see the section `Configuration flags\&'
 \fB-y\fR
 always commit without prompting
 .TP
+\fB-d\fR
+dry-run, run without writing a YubiKey
+.TP
 \fB-v\fR
 Be more verbose
 .TP
index 16f2733bf995e7c1ac3005a85891fa7db1e8fb06..e4c3fb2618031486f998c00934e0ea6dce7871e8 100644 (file)
@@ -56,6 +56,7 @@ int main(int argc, char **argv)
        YK_STATUS *st = ykds_alloc();
        bool autocommit = false;
        char data[1024];
+       bool dry_run = false;
 
        /* Options */
        char *salt = NULL;
@@ -121,7 +122,7 @@ int main(int argc, char **argv)
                             &infname, &outfname,
                             &data_format,
                             &autocommit, salt,
-                            st, &verbose,
+                            st, &verbose, &dry_run,
                             access_code, &use_access_code,
                             &aesviahash, &ndef_type, ndef_string,
                             &usb_mode, &zap, scan_codes, &exit_code)) {
@@ -246,7 +247,10 @@ int main(int argc, char **argv)
 
                        if (verbose)
                                printf("Attempting to write configuration to the yubikey...");
-                       if(ykp_command(cfg) == SLOT_NDEF || ykp_command(cfg) == SLOT_NDEF2) {
+                       if (dry_run) {
+                               printf("Not writing anything to key due to dry_run requested.\n");
+                       }
+                       else if(ykp_command(cfg) == SLOT_NDEF || ykp_command(cfg) == SLOT_NDEF2) {
                                YK_NDEF *ndef = ykp_alloc_ndef();
                                int confnum = 1;
                                if(ndef_type == 'U') {
@@ -298,7 +302,7 @@ int main(int argc, char **argv)
                                }
                        }
 
-                       if (verbose)
+                       if (verbose && !dry_run)
                                printf(" success\n");
                }
        }