1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-11 21:48:50 +01:00

gpg: Print a new FAILURE status after most commands.

* 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.
* g10/call-agent.c (start_agent): Print an STATUS_ERROR if we can't
set the pinentry mode.
--

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.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2015-08-25 09:03:31 +02:00
parent 348a6ebb63
commit 9cdff09743
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
6 changed files with 117 additions and 28 deletions

View File

@ -126,9 +126,10 @@ enum
STATUS_ERROR, STATUS_ERROR,
STATUS_SUCCESS, STATUS_SUCCESS,
STATUS_FAILURE,
STATUS_INQUIRE_MAXLEN, STATUS_INQUIRE_MAXLEN
}; };
const char *get_status_string (int code); const char *get_status_string (int code);

View File

@ -823,9 +823,17 @@ pkd:0:1024:B665B1435F4C2 .... FF26ABB:
numerical error code and an underscore; e.g.: "151011327_EOF". numerical error code and an underscore; e.g.: "151011327_EOF".
*** SUCCESS [<location>] *** SUCCESS [<location>]
Postive confirimation that an operation succeeded. <location> is Postive confirmation that an operation succeeded. It is used
optional but if given should not contain spaces. Used only with a similar to ISO-C's EXIT_SUCCESS. <location> is optional but if
few commands. given should not contain spaces. Used only with a few commands.
*** FAILURE <location> <error_code>
This is the counterpart to SUCCESS and used to indicate a program
failure. It is used similar to ISO-C's EXIT_FAILURE but allows to
convey more information, in particular an gpg-error error code.
That numerical error code may optionally have a suffix made of an
underscore and a string with an error symbol like "151011327_EOF".
A dash may be used instead of <location>.
*** BADARMOR *** BADARMOR
The ASCII armor is corrupted. No arguments yet. The ASCII armor is corrupted. No arguments yet.

View File

@ -318,9 +318,12 @@ start_agent (ctrl_t ctrl, int for_card)
NULL, NULL, NULL, NULL, NULL, NULL); NULL, NULL, NULL, NULL, NULL, NULL);
xfree (tmp); xfree (tmp);
if (rc) if (rc)
log_error ("setting pinentry mode '%s' failed: %s\n", {
str_pinentry_mode (opt.pinentry_mode), log_error ("setting pinentry mode '%s' failed: %s\n",
gpg_strerror (rc)); str_pinentry_mode (opt.pinentry_mode),
gpg_strerror (rc));
write_status_error ("set_pinentry_mode", rc);
}
} }
check_hijacking (agent_ctx); check_hijacking (agent_ctx);

View File

@ -183,7 +183,7 @@ write_status_text (int no, const char *text)
write_status_strings (no, text, NULL); write_status_strings (no, text, NULL);
} }
/* Wrte an ERROR status line using a full gpg-error error value. */ /* Write an ERROR status line using a full gpg-error error value. */
void void
write_status_error (const char *where, gpg_error_t err) write_status_error (const char *where, gpg_error_t err)
{ {
@ -211,6 +211,20 @@ write_status_errcode (const char *where, int errcode)
} }
/* 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. */
es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
get_status_string (STATUS_FAILURE), where, err);
if (es_fflush (statusfp) && opt.exit_on_status_write_error)
g10_exit (0);
}
/* /*
* Write a status line with a buffer using %XX escapes. If WRAP is > * Write a status line with a buffer using %XX escapes. If WRAP is >
* 0 wrap the line after this length. If STRING is not NULL it will * 0 wrap the line after this length. If STRING is not NULL it will

100
g10/gpg.c
View File

@ -3661,15 +3661,21 @@ main (int argc, char **argv)
if( argc > 1 ) if( argc > 1 )
wrong_args(_("--store [filename]")); wrong_args(_("--store [filename]"));
if( (rc = encrypt_store(fname)) ) if( (rc = encrypt_store(fname)) )
{
write_status_failure ("store", rc);
log_error ("storing '%s' failed: %s\n", log_error ("storing '%s' failed: %s\n",
print_fname_stdin(fname),gpg_strerror (rc) ); print_fname_stdin(fname),gpg_strerror (rc) );
}
break; break;
case aSym: /* encrypt the given file only with the symmetric cipher */ case aSym: /* encrypt the given file only with the symmetric cipher */
if( argc > 1 ) if( argc > 1 )
wrong_args(_("--symmetric [filename]")); wrong_args(_("--symmetric [filename]"));
if( (rc = encrypt_symmetric(fname)) ) if( (rc = encrypt_symmetric(fname)) )
{
write_status_failure ("symencrypt", rc);
log_error (_("symmetric encryption of '%s' failed: %s\n"), log_error (_("symmetric encryption of '%s' failed: %s\n"),
print_fname_stdin(fname),gpg_strerror (rc) ); print_fname_stdin(fname),gpg_strerror (rc) );
}
break; break;
case aEncr: /* encrypt the given file */ case aEncr: /* encrypt the given file */
@ -3680,8 +3686,11 @@ main (int argc, char **argv)
if( argc > 1 ) if( argc > 1 )
wrong_args(_("--encrypt [filename]")); wrong_args(_("--encrypt [filename]"));
if( (rc = encrypt_crypt (ctrl, -1, fname, remusr, 0, NULL, -1)) ) if( (rc = encrypt_crypt (ctrl, -1, fname, remusr, 0, NULL, -1)) )
log_error("%s: encryption failed: %s\n", {
print_fname_stdin(fname), gpg_strerror (rc) ); write_status_failure ("encrypt", rc);
log_error("%s: encryption failed: %s\n",
print_fname_stdin(fname), gpg_strerror (rc) );
}
} }
break; break;
@ -3701,8 +3710,11 @@ main (int argc, char **argv)
else else
{ {
if( (rc = encrypt_crypt (ctrl, -1, fname, remusr, 1, NULL, -1)) ) if( (rc = encrypt_crypt (ctrl, -1, fname, remusr, 1, NULL, -1)) )
log_error("%s: encryption failed: %s\n", {
print_fname_stdin(fname), gpg_strerror (rc) ); write_status_failure ("encrypt", rc);
log_error ("%s: encryption failed: %s\n",
print_fname_stdin(fname), gpg_strerror (rc) );
}
} }
break; break;
@ -3720,8 +3732,11 @@ main (int argc, char **argv)
strcpy(sl->d, fname); strcpy(sl->d, fname);
} }
} }
if( (rc = sign_file (ctrl, sl, detached_sig, locusr, 0, NULL, NULL)) ) if ((rc = sign_file (ctrl, sl, detached_sig, locusr, 0, NULL, NULL)))
log_error("signing failed: %s\n", gpg_strerror (rc) ); {
write_status_failure ("sign", rc);
log_error ("signing failed: %s\n", gpg_strerror (rc) );
}
free_strlist(sl); free_strlist(sl);
break; break;
@ -3735,8 +3750,11 @@ main (int argc, char **argv)
else else
sl = NULL; sl = NULL;
if ((rc = sign_file (ctrl, sl, detached_sig, locusr, 1, remusr, NULL))) if ((rc = sign_file (ctrl, sl, detached_sig, locusr, 1, remusr, NULL)))
{
write_status_failure ("sign-encrypt", rc);
log_error("%s: sign+encrypt failed: %s\n", log_error("%s: sign+encrypt failed: %s\n",
print_fname_stdin(fname), gpg_strerror (rc) ); print_fname_stdin(fname), gpg_strerror (rc) );
}
free_strlist(sl); free_strlist(sl);
break; break;
@ -3760,8 +3778,11 @@ main (int argc, char **argv)
sl = NULL; sl = NULL;
if ((rc = sign_file (ctrl, sl, detached_sig, locusr, if ((rc = sign_file (ctrl, sl, detached_sig, locusr,
2, remusr, NULL))) 2, remusr, NULL)))
log_error("%s: symmetric+sign+encrypt failed: %s\n", {
print_fname_stdin(fname), gpg_strerror (rc) ); write_status_failure ("sign-encrypt", rc);
log_error("%s: symmetric+sign+encrypt failed: %s\n",
print_fname_stdin(fname), gpg_strerror (rc) );
}
free_strlist(sl); free_strlist(sl);
} }
break; break;
@ -3771,19 +3792,26 @@ main (int argc, char **argv)
wrong_args(_("--sign --symmetric [filename]")); wrong_args(_("--sign --symmetric [filename]"));
rc = sign_symencrypt_file (fname, locusr); rc = sign_symencrypt_file (fname, locusr);
if (rc) if (rc)
{
write_status_failure ("sign-symencrypt", rc);
log_error("%s: sign+symmetric failed: %s\n", log_error("%s: sign+symmetric failed: %s\n",
print_fname_stdin(fname), gpg_strerror (rc) ); print_fname_stdin(fname), gpg_strerror (rc) );
}
break; break;
case aClearsign: /* make a clearsig */ case aClearsign: /* make a clearsig */
if( argc > 1 ) if( argc > 1 )
wrong_args(_("--clearsign [filename]")); wrong_args(_("--clearsign [filename]"));
if( (rc = clearsign_file(fname, locusr, NULL)) ) if( (rc = clearsign_file(fname, locusr, NULL)) )
{
write_status_failure ("sign", rc);
log_error("%s: clearsign failed: %s\n", log_error("%s: clearsign failed: %s\n",
print_fname_stdin(fname), gpg_strerror (rc) ); print_fname_stdin(fname), gpg_strerror (rc) );
}
break; break;
case aVerify: case aVerify:
rc = 0;
if (multifile) if (multifile)
{ {
if ((rc = verify_files (ctrl, argc, argv))) if ((rc = verify_files (ctrl, argc, argv)))
@ -3794,6 +3822,8 @@ main (int argc, char **argv)
if ((rc = verify_signatures (ctrl, argc, argv))) if ((rc = verify_signatures (ctrl, argc, argv)))
log_error("verify signatures failed: %s\n", gpg_strerror (rc) ); log_error("verify signatures failed: %s\n", gpg_strerror (rc) );
} }
if (rc)
write_status_failure ("verify", rc);
break; break;
case aDecrypt: case aDecrypt:
@ -3804,7 +3834,10 @@ main (int argc, char **argv)
if( argc > 1 ) if( argc > 1 )
wrong_args(_("--decrypt [filename]")); wrong_args(_("--decrypt [filename]"));
if( (rc = decrypt_message (ctrl, fname) )) if( (rc = decrypt_message (ctrl, fname) ))
log_error("decrypt_message failed: %s\n", gpg_strerror (rc) ); {
write_status_failure ("decrypt", rc);
log_error("decrypt_message failed: %s\n", gpg_strerror (rc) );
}
} }
break; break;
@ -3998,11 +4031,21 @@ main (int argc, char **argv)
if(rc) if(rc)
{ {
if(cmd==aSendKeys) if(cmd==aSendKeys)
log_error(_("keyserver send failed: %s\n"),gpg_strerror (rc)); {
write_status_failure ("send-keys", rc);
log_error(_("keyserver send failed: %s\n"),gpg_strerror (rc));
}
else if(cmd==aRecvKeys) else if(cmd==aRecvKeys)
log_error(_("keyserver receive failed: %s\n"),gpg_strerror (rc)); {
write_status_failure ("recv-keys", rc);
log_error (_("keyserver receive failed: %s\n"),
gpg_strerror (rc));
}
else else
log_error(_("key export failed: %s\n"),gpg_strerror (rc)); {
write_status_failure ("export", rc);
log_error (_("key export failed: %s\n"), gpg_strerror (rc));
}
} }
free_strlist(sl); free_strlist(sl);
break; break;
@ -4013,7 +4056,10 @@ main (int argc, char **argv)
append_to_strlist2 (&sl, *argv, utf8_strings); append_to_strlist2 (&sl, *argv, utf8_strings);
rc = keyserver_search (ctrl, sl); rc = keyserver_search (ctrl, sl);
if (rc) if (rc)
log_error (_("keyserver search failed: %s\n"), gpg_strerror (rc)); {
write_status_failure ("search-keys", rc);
log_error (_("keyserver search failed: %s\n"), gpg_strerror (rc));
}
free_strlist (sl); free_strlist (sl);
break; break;
@ -4023,7 +4069,10 @@ main (int argc, char **argv)
append_to_strlist2( &sl, *argv, utf8_strings ); append_to_strlist2( &sl, *argv, utf8_strings );
rc = keyserver_refresh (ctrl, sl); rc = keyserver_refresh (ctrl, sl);
if(rc) if(rc)
log_error(_("keyserver refresh failed: %s\n"),gpg_strerror (rc)); {
write_status_failure ("refresh-keys", rc);
log_error (_("keyserver refresh failed: %s\n"),gpg_strerror (rc));
}
free_strlist(sl); free_strlist(sl);
break; break;
@ -4033,7 +4082,10 @@ main (int argc, char **argv)
append_to_strlist2( &sl, *argv, utf8_strings ); append_to_strlist2( &sl, *argv, utf8_strings );
rc = keyserver_fetch (ctrl, sl); rc = keyserver_fetch (ctrl, sl);
if(rc) if(rc)
log_error("key fetch failed: %s\n",gpg_strerror (rc)); {
write_status_failure ("fetch-keys", rc);
log_error ("key fetch failed: %s\n",gpg_strerror (rc));
}
free_strlist(sl); free_strlist(sl);
break; break;
@ -4074,7 +4126,10 @@ main (int argc, char **argv)
wrong_args("--dearmor [file]"); wrong_args("--dearmor [file]");
rc = dearmor_file( argc? *argv: NULL ); rc = dearmor_file( argc? *argv: NULL );
if( rc ) if( rc )
log_error(_("dearmoring failed: %s\n"), gpg_strerror (rc)); {
write_status_failure ("dearmor", rc);
log_error (_("dearmoring failed: %s\n"), gpg_strerror (rc));
}
break; break;
case aEnArmor: case aEnArmor:
@ -4082,7 +4137,10 @@ main (int argc, char **argv)
wrong_args("--enarmor [file]"); wrong_args("--enarmor [file]");
rc = enarmor_file( argc? *argv: NULL ); rc = enarmor_file( argc? *argv: NULL );
if( rc ) if( rc )
log_error(_("enarmoring failed: %s\n"), gpg_strerror (rc)); {
write_status_failure ("enarmor", rc);
log_error (_("enarmoring failed: %s\n"), gpg_strerror (rc));
}
break; break;
@ -4274,7 +4332,7 @@ main (int argc, char **argv)
else if (argc == 1) else if (argc == 1)
change_pin (atoi (*argv),1); change_pin (atoi (*argv),1);
else else
wrong_args ("--change-pin [no]"); wrong_args ("--change-pin [no]");
break; break;
#endif /* ENABLE_CARD_SUPPORT*/ #endif /* ENABLE_CARD_SUPPORT*/
@ -4328,7 +4386,11 @@ main (int argc, char **argv)
} }
rc = proc_packets (ctrl, NULL, a ); rc = proc_packets (ctrl, NULL, a );
if( rc ) if( rc )
log_error("processing message failed: %s\n", gpg_strerror (rc)); {
write_status_failure ("-", rc);
log_error ("processing message failed: %s\n",
gpg_strerror (rc));
}
iobuf_close(a); iobuf_close(a);
} }
break; break;

View File

@ -180,6 +180,7 @@ int is_status_enabled ( void );
void write_status ( int no ); void write_status ( int no );
void write_status_error (const char *where, gpg_error_t err); void write_status_error (const char *where, gpg_error_t err);
void write_status_errcode (const char *where, int errcode); void write_status_errcode (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_text ( int no, const char *text );
void write_status_strings (int no, const char *text, void write_status_strings (int no, const char *text,
...) GPGRT_ATTR_SENTINEL(0); ...) GPGRT_ATTR_SENTINEL(0);