gpg: Print a new FAILURE status after most commands.
authorWerner Koch <wk@gnupg.org>
Tue, 8 Sep 2015 13:29:59 +0000 (15:29 +0200)
committerWerner Koch <wk@gnupg.org>
Tue, 8 Sep 2015 13:31:51 +0000 (15:31 +0200)
* common/status.h (STATUS_FAILURE): New.
* g10/cpr.c (write_status_failure): New.
* g10/gpg.c (main): Call write_status_failure for all commands which
print an error message here.
--

This status line can be used similar to the error code returned by
commands send over the Assuan interface in gpgsm.  We don't emit them
in gpgsm because there we already have that Assuan interface to return
proper error code.  This change helps GPGME to return better error
codes.

(backported from master 9cdff09743c473a12359bfdb914578ede0e4e3e2)

Signed-off-by: Werner Koch <wk@gnupg.org>
common/status.h
g10/cpr.c
g10/gpg.c
g10/main.h

index cfb64ca..f101bf0 100644 (file)
@@ -20,7 +20,7 @@
 #ifndef GNUPG_COMMON_STATUS_H
 #define GNUPG_COMMON_STATUS_H
 
-enum 
+enum
   {
     STATUS_ENTER,
     STATUS_LEAVE,
@@ -39,7 +39,7 @@ enum
     STATUS_TRUST_MARGINAL,
     STATUS_TRUST_FULLY,
     STATUS_TRUST_ULTIMATE,
-  
+
     STATUS_NEED_PASSPHRASE,
     STATUS_VALIDSIG,
     STATUS_SIG_ID,
@@ -59,20 +59,20 @@ enum
     STATUS_ERRMDC,
     STATUS_IMPORTED,
     STATUS_IMPORT_OK,
-    STATUS_IMPORT_PROBLEM, 
+    STATUS_IMPORT_PROBLEM,
     STATUS_IMPORT_RES,
     STATUS_IMPORT_CHECK,
 
     STATUS_FILE_START,
     STATUS_FILE_DONE,
     STATUS_FILE_ERROR,
-  
+
     STATUS_BEGIN_DECRYPTION,
     STATUS_END_DECRYPTION,
     STATUS_BEGIN_ENCRYPTION,
     STATUS_END_ENCRYPTION,
     STATUS_BEGIN_SIGNING,
-  
+
     STATUS_DELETE_PROBLEM,
 
     STATUS_GET_BOOL,
@@ -126,6 +126,7 @@ enum
 
     STATUS_TRUNCATED,
     STATUS_ERROR,
+    STATUS_FAILURE,
     STATUS_SUCCESS
 };
 
index 0fc45b1..3142ac4 100644 (file)
--- a/g10/cpr.c
+++ b/g10/cpr.c
@@ -303,6 +303,20 @@ write_status_begin_signing (gcry_md_hd_t md)
 }
 
 
+/* Write a FAILURE status line.  */
+void
+write_status_failure (const char *where, gpg_error_t err)
+{
+  if (!statusfp || !status_currently_allowed (STATUS_FAILURE))
+    return;  /* Not enabled or allowed. */
+
+  fprintf (statusfp, "[GNUPG:] %s %s %u\n",
+           get_status_string (STATUS_FAILURE), where, err);
+  if (fflush (statusfp) && opt.exit_on_status_write_error)
+    g10_exit (0);
+}
+
+
 static int
 myread(int fd, void *buf, size_t count)
 {
index 3fb598c..4f71bf4 100644 (file)
--- a/g10/gpg.c
+++ b/g10/gpg.c
@@ -3504,15 +3504,21 @@ main (int argc, char **argv)
        if( argc > 1 )
            wrong_args(_("--store [filename]"));
        if( (rc = encode_store(fname)) )
+          {
+            write_status_failure ("store", rc);
            log_error ("storing `%s' failed: %s\n",
                        print_fname_stdin(fname),g10_errstr(rc) );
+          }
        break;
       case aSym: /* encrypt the given file only with the symmetric cipher */
        if( argc > 1 )
            wrong_args(_("--symmetric [filename]"));
        if( (rc = encode_symmetric(fname)) )
+          {
+            write_status_failure ("symencrypt", rc);
             log_error (_("symmetric encryption of `%s' failed: %s\n"),
                         print_fname_stdin(fname),g10_errstr(rc) );
+          }
        break;
 
       case aEncr: /* encrypt the given file */
@@ -3523,8 +3529,11 @@ main (int argc, char **argv)
            if( argc > 1 )
              wrong_args(_("--encrypt [filename]"));
            if( (rc = encode_crypt(fname,remusr,0)) )
-             log_error("%s: encryption failed: %s\n",
-                       print_fname_stdin(fname), g10_errstr(rc) );
+              {
+                write_status_failure ("encrypt", rc);
+                log_error("%s: encryption failed: %s\n",
+                          print_fname_stdin(fname), g10_errstr(rc) );
+              }
          }
        break;
 
@@ -3544,8 +3553,11 @@ main (int argc, char **argv)
        else
          {
            if( (rc = encode_crypt(fname,remusr,1)) )
-             log_error("%s: encryption failed: %s\n",
-                       print_fname_stdin(fname), g10_errstr(rc) );
+              {
+                write_status_failure ("encrypt", rc);
+                log_error("%s: encryption failed: %s\n",
+                          print_fname_stdin(fname), g10_errstr(rc) );
+              }
          }
        break;
 
@@ -3564,7 +3576,10 @@ main (int argc, char **argv)
            }
        }
        if( (rc = sign_file( sl, detached_sig, locusr, 0, NULL, NULL)) )
+          {
+            write_status_failure ("sign", rc);
            log_error("signing failed: %s\n", g10_errstr(rc) );
+          }
        free_strlist(sl);
        break;
 
@@ -3578,8 +3593,11 @@ main (int argc, char **argv)
        else
            sl = NULL;
        if( (rc = sign_file(sl, detached_sig, locusr, 1, remusr, NULL)) )
+          {
+            write_status_failure ("sign-encrypt", rc);
            log_error("%s: sign+encrypt failed: %s\n",
                      print_fname_stdin(fname), g10_errstr(rc) );
+          }
        free_strlist(sl);
        break;
 
@@ -3602,8 +3620,11 @@ main (int argc, char **argv)
            else
              sl = NULL;
            if( (rc = sign_file(sl, detached_sig, locusr, 2, remusr, NULL)) )
-             log_error("%s: symmetric+sign+encrypt failed: %s\n",
-                       print_fname_stdin(fname), g10_errstr(rc) );
+              {
+                write_status_failure ("sign-encrypt", rc);
+                log_error("%s: symmetric+sign+encrypt failed: %s\n",
+                          print_fname_stdin(fname), g10_errstr(rc) );
+              }
            free_strlist(sl);
          }
        break;
@@ -3613,16 +3634,22 @@ main (int argc, char **argv)
            wrong_args(_("--sign --symmetric [filename]"));
        rc = sign_symencrypt_file (fname, locusr);
         if (rc)
+          {
+            write_status_failure ("sign-symencrypt", rc);
            log_error("%s: sign+symmetric failed: %s\n",
                       print_fname_stdin(fname), g10_errstr(rc) );
+          }
        break;
 
       case aClearsign: /* make a clearsig */
        if( argc > 1 )
            wrong_args(_("--clearsign [filename]"));
        if( (rc = clearsign_file(fname, locusr, NULL)) )
+          {
+            write_status_failure ("sign", rc);
            log_error("%s: clearsign failed: %s\n",
                       print_fname_stdin(fname), g10_errstr(rc) );
+          }
        break;
 
       case aVerify:
@@ -3636,6 +3663,8 @@ main (int argc, char **argv)
            if( (rc = verify_signatures( argc, argv ) ))
              log_error("verify signatures failed: %s\n", g10_errstr(rc) );
          }
+        if (rc)
+          write_status_failure ("verify", rc);
        break;
 
       case aDecrypt:
@@ -3646,7 +3675,10 @@ main (int argc, char **argv)
            if( argc > 1 )
              wrong_args(_("--decrypt [filename]"));
            if( (rc = decrypt_message( fname ) ))
-             log_error("decrypt_message failed: %s\n", g10_errstr(rc) );
+              {
+                write_status_failure ("decrypt", rc);
+                log_error("decrypt_message failed: %s\n", g10_errstr(rc));
+              }
          }
        break;
 
@@ -3780,11 +3812,20 @@ main (int argc, char **argv)
        if(rc)
          {
            if(cmd==aSendKeys)
-             log_error(_("keyserver send failed: %s\n"),g10_errstr(rc));
+              {
+                write_status_failure ("send-keys", rc);
+                log_error(_("keyserver send failed: %s\n"),g10_errstr(rc));
+              }
            else if(cmd==aRecvKeys)
-             log_error(_("keyserver receive failed: %s\n"),g10_errstr(rc));
+              {
+                write_status_failure ("recv-keys", rc);
+                log_error(_("keyserver receive failed: %s\n"),g10_errstr(rc));
+              }
            else
-             log_error(_("key export failed: %s\n"),g10_errstr(rc));
+              {
+                write_status_failure ("export", rc);
+                log_error(_("key export failed: %s\n"),g10_errstr(rc));
+              }
          }
        free_strlist(sl);
        break;
@@ -3795,7 +3836,10 @@ main (int argc, char **argv)
          append_to_strlist2( &sl, *argv, utf8_strings );
        rc=keyserver_search( sl );
        if(rc)
-         log_error(_("keyserver search failed: %s\n"),g10_errstr(rc));
+          {
+            write_status_failure ("search-keys", rc);
+            log_error(_("keyserver search failed: %s\n"), g10_errstr(rc));
+          }
        free_strlist(sl);
        break;
 
@@ -3805,7 +3849,10 @@ main (int argc, char **argv)
            append_to_strlist2( &sl, *argv, utf8_strings );
        rc=keyserver_refresh(sl);
        if(rc)
-         log_error(_("keyserver refresh failed: %s\n"),g10_errstr(rc));
+          {
+            write_status_failure ("refresh-keys", rc);
+            log_error(_("keyserver refresh failed: %s\n"),g10_errstr(rc));
+          }
        free_strlist(sl);
        break;
 
@@ -3815,7 +3862,10 @@ main (int argc, char **argv)
            append_to_strlist2( &sl, *argv, utf8_strings );
        rc=keyserver_fetch(sl);
        if(rc)
-         log_error("key fetch failed: %s\n",g10_errstr(rc));
+          {
+            write_status_failure ("fetch-keys", rc);
+            log_error("key fetch failed: %s\n",g10_errstr(rc));
+          }
        free_strlist(sl);
        break;
 
@@ -3856,7 +3906,10 @@ main (int argc, char **argv)
            wrong_args("--dearmor [file]");
        rc = dearmor_file( argc? *argv: NULL );
        if( rc )
+          {
+            write_status_failure ("dearmor", rc);
            log_error(_("dearmoring failed: %s\n"), g10_errstr(rc));
+          }
        break;
 
       case aEnArmor:
@@ -3864,7 +3917,10 @@ main (int argc, char **argv)
            wrong_args("--enarmor [file]");
        rc = enarmor_file( argc? *argv: NULL );
        if( rc )
+          {
+            write_status_failure ("enarmor", rc);
            log_error(_("enarmoring failed: %s\n"), g10_errstr(rc));
+          }
        break;
 
 
@@ -4053,7 +4109,7 @@ main (int argc, char **argv)
         else if (argc == 1)
             change_pin (atoi (*argv),1);
         else
-        wrong_args ("--change-pin [no]");
+          wrong_args ("--change-pin [no]");
         break;
 #endif /* ENABLE_CARD_SUPPORT*/
 
@@ -4105,7 +4161,10 @@ main (int argc, char **argv)
            }
            rc = proc_packets(NULL, a );
            if( rc )
+              {
+                write_status_failure ("-", rc);
                log_error("processing message failed: %s\n", g10_errstr(rc) );
+              }
            iobuf_close(a);
        }
        break;
index 4ee1b73..86fbabe 100644 (file)
@@ -163,6 +163,7 @@ void set_status_fd ( int fd );
 int  is_status_enabled ( void );
 void write_status ( int no );
 void write_status_error (const char *where, int errcode);
+void write_status_failure (const char *where, gpg_error_t err);
 void write_status_text ( int no, const char *text );
 void write_status_strings (int no, const char *text,
                            ...) GNUPG_GCC_A_SENTINEL(0);