]> err.no Git - yubikey-personalization.old/commitdiff
Better error checking and reporting
authorRichard Levitte <levitte@lp.se>
Sun, 5 Oct 2008 07:58:56 +0000 (07:58 +0000)
committerRichard Levitte <levitte@lp.se>
Sun, 5 Oct 2008 07:58:56 +0000 (07:58 +0000)
ykpersonalize.c

index 61593ca66cce1a8079dae09665a0ae1792495419..e2a2886bfaa7ca8f3c420074518536717b4fa3de 100644 (file)
@@ -61,6 +61,22 @@ static int writer(const char *buf, size_t count, void *stream)
        return (int)fwrite(buf, 1, count, (FILE *)stream);
 }
 
+static void report_yk_error()
+{
+       if (ykp_errno)
+               fprintf(stderr, "Yubikey personalization error: %s\n",
+                       ykp_strerror(ykp_errno));
+       if (yk_errno) {
+               if (yk_errno == YK_EUSBERR) {
+                       fprintf(stderr, "USB error: %s\n",
+                               usb_strerror());
+               } else {
+                       fprintf(stderr, "Yubikey core error: %s\n",
+                               yk_strerror(yk_errno));
+               }
+       }
+}
+
 main(int argc, char **argv)
 {
        char c;
@@ -68,6 +84,7 @@ main(int argc, char **argv)
        FILE *outf = NULL; const char *outfname = NULL;
        bool verbose = false;
        CONFIG *cfg = ykp_create_config();
+       YUBIKEY *yk = NULL;
 
        bool error = false;
        int exit_code = 0;
@@ -167,8 +184,6 @@ main(int argc, char **argv)
                        if (!ykp_write_config(cfg, writer, outf))
                                break;
                } else {
-                       YUBIKEY *yk;
-
                        /* Assume the worst */
                        exit_code = 2;
 
@@ -180,47 +195,40 @@ main(int argc, char **argv)
                        if (!(yk = yk_open_first_key()))
                                break;
 
-                       if (yk_write_config(yk, cfg, NULL)) {
-                               if (verbose)
-                                       printf(" success\n");
-                               ykp_write_config(cfg, writer, stdout);
-                               exit_code = 0;
-                       } else {
+                       if (!yk_write_config(yk, cfg, NULL)) {
                                if (verbose)
                                        printf(" failure\n");
-                       }
-                       if (!yk_close_key(yk))
                                break;
+                       }
 
-                       if (!yk_release())
-                               break;
+                       if (verbose)
+                               printf(" success\n");
+                       ykp_write_config(cfg, writer, stdout);
                }
+
+               exit_code = 0;
                error = false;
        } while(false);
 
+       if (error) {
+               report_yk_error();
+       }
+
        if (salt)
                free(salt);
        if (inf)
                fclose(inf);
        if (outf)
                fclose(outf);
+       if (!yk_close_key(yk)) {
+               report_yk_error();
+               exit_code = 2;
+       }
 
-       if (error) {
-               if (ykp_errno)
-                       fprintf(stderr, "Yubikey personalization error: %s\n",
-                               ykp_strerror(ykp_errno));
-               if (yk_errno) {
-                       if (yk_errno == YK_EUSBERR) {
-                               fprintf(stderr, "USB error: %s\n",
-                                       usb_strerror());
-                       } else {
-                               fprintf(stderr, "Yubikey core error: %s\n",
-                                       yk_strerror(yk_errno));
-                       }
-               }
-               if (exit_code)
-                       exit(exit_code);
-               exit(1);
+       if (!yk_release()) {
+               report_yk_error();
+               exit_code = 2;
        }
-       exit(0);
+
+       exit(exit_code);
 }