diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/ChangeLog | 17 | ||||
-rw-r--r-- | common/Makefile.am | 8 | ||||
-rw-r--r-- | common/README | 5 | ||||
-rw-r--r-- | common/errors.h | 85 | ||||
-rw-r--r-- | common/maperror.c | 234 | ||||
-rw-r--r-- | common/membuf.c | 89 | ||||
-rw-r--r-- | common/membuf.h | 41 | ||||
-rw-r--r-- | common/util.h | 7 |
8 files changed, 212 insertions, 274 deletions
diff --git a/common/ChangeLog b/common/ChangeLog index da4bf9644..030147820 100644 --- a/common/ChangeLog +++ b/common/ChangeLog @@ -1,3 +1,20 @@ +2003-06-04 Werner Koch <[email protected]> + + + * errors.h: Removed all error codes. We keep the status codes for + now. + * Makefile.am: Do not create errors.c anymore; remove it from the + sources. + + * maperror.c: Don't include error.h. Change all error codes to + libgpg-error style. + (map_assuan_err): Changed to new Assuan error code convention. + (map_to_assuan_status): Likewise. + (map_gcry_err,map_kbx_err): Not needed. For now dummy functions. + + * membuf.c, membuf.h: New. Code taken from ../sm/call-agent.h. + * Makefile.am: Added above. + 2003-04-29 Werner Koch <[email protected]> * util.h (fopencokokie): Removed prototype and struct. diff --git a/common/Makefile.am b/common/Makefile.am index 8f8209afd..8e3dc68d3 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -1,5 +1,5 @@ # Makefile for common gnupg modules -# Copyright (C) 2001 Free Software Foundation, Inc. +# Copyright (C) 2001, 2003 Free Software Foundation, Inc. # # This file is part of GnuPG. # @@ -29,19 +29,17 @@ AM_CPPFLAGS = $(LIBGCRYPT_CFLAGS) $(KSBA_CFLAGS) libcommon_a_SOURCES = \ util.h i18n.h \ - errors.c errors.h \ + errors.h \ maperror.c \ sysutils.c sysutils.h \ cryptmiss.c \ gettime.c \ + membuf.c membuf.h \ signal.c libcommon_a_LIBADD = @LIBOBJS@ -errors.c : errors.h mkerrors mkerrtok - $(srcdir)/mkerrors < $(srcdir)/errors.h > errors.c - $(srcdir)/mkerrtok < $(srcdir)/errors.h >> errors.c diff --git a/common/README b/common/README index d3927d869..a90224bab 100644 --- a/common/README +++ b/common/README @@ -1,6 +1,4 @@ -Stuff used by several modules of GnuPG. This way we can share error -codes and serveral other things. - +Stuff used by several modules of GnuPG. These directories use it: @@ -10,5 +8,4 @@ agent These directories don't use it: -assuan kbx
\ No newline at end of file diff --git a/common/errors.h b/common/errors.h index 46cb6c10a..a5643f08a 100644 --- a/common/errors.h +++ b/common/errors.h @@ -23,91 +23,6 @@ #include "util.h" -#ifndef GPG_ERR_SOURCE_DEFAULT -/* Error numbers. Note, that they are onkly used for old code not yet - converted to libgpg-error. */ -enum { - GNUPG_EOF = -1, - GNUPG_No_Error = 0, - GNUPG_General_Error = 1, - GNUPG_Out_Of_Core = 2, - GNUPG_Invalid_Value = 3, - GNUPG_IO_Error = 4, - GNUPG_Resource_Limit = 5, - GNUPG_Internal_Error = 6, - GNUPG_Bad_Certificate = 7, - GNUPG_Bad_Certificate_Chain = 8, - GNUPG_Missing_Certificate = 9, - GNUPG_No_Data = 10, - GNUPG_Bad_Signature = 11, - GNUPG_Not_Implemented = 12, - GNUPG_Conflict = 13, - GNUPG_Bug = 14, - GNUPG_Read_Error = 15, - GNUPG_Write_Error = 16, - GNUPG_Incomplete_Line = 17, - GNUPG_Invalid_Response = 18, - GNUPG_No_Agent = 19, - GNUPG_Agent_Error = 20, - GNUPG_No_Public_Key = 21, - GNUPG_No_Secret_Key = 22, - GNUPG_File_Open_Error = 23, - GNUPG_File_Create_Error = 24, - GNUPG_File_Error = 25, - GNUPG_Not_Supported = 26, - GNUPG_Invalid_Data = 27, - GNUPG_Assuan_Server_Fault = 28, - GNUPG_Assuan_Error = 29, /* catch all assuan error */ - GNUPG_Invalid_Session_Key = 30, - GNUPG_Invalid_Sexp = 31, - GNUPG_Unsupported_Algorithm = 32, - GNUPG_No_PIN_Entry = 33, - GNUPG_PIN_Entry_Error = 34, - GNUPG_Bad_PIN = 35, - GNUPG_Bad_Passphrase = 36, - GNUPG_Invalid_Name = 37, - GNUPG_Bad_Public_Key = 38, - GNUPG_Bad_Secret_Key = 39, - GNUPG_Bad_Data = 40, - GNUPG_Invalid_Parameter = 41, - GNUPG_Tribute_to_D_A = 42, - GNUPG_No_Dirmngr = 43, - GNUPG_Dirmngr_Error = 44, - GNUPG_Certificate_Revoked = 45, - GNUPG_No_CRL_Known = 46, - GNUPG_CRL_Too_Old = 47, - GNUPG_Line_Too_Long = 48, - GNUPG_Not_Trusted = 49, - GNUPG_Canceled = 50, - GNUPG_Bad_CA_Certificate = 51, - GNUPG_Certificate_Expired = 52, - GNUPG_Certificate_Too_Young = 53, - GNUPG_Unsupported_Certificate = 54, - GNUPG_Unknown_Sexp = 55, - GNUPG_Unsupported_Protection = 56, - GNUPG_Corrupted_Protection = 57, - GNUPG_Ambiguous_Name = 58, - GNUPG_Card_Error = 59, - GNUPG_Card_Reset = 60, - GNUPG_Card_Removed = 61, - GNUPG_Invalid_Card = 62, - GNUPG_Card_Not_Present = 63, - GNUPG_No_PKCS15_App = 64, - GNUPG_Not_Confirmed = 65, - GNUPG_Configuration_Error = 66, - GNUPG_No_Policy_Match = 67, - GNUPG_Invalid_Index = 68, - GNUPG_Invalid_Id = 69, - GNUPG_No_Scdaemon = 70, - GNUPG_Scdaemon_Error = 71, - GNUPG_Unsupported_Protocol = 72, - GNUPG_Bad_PIN_Method = 73, - GNUPG_Card_Not_Initialized = 74, - GNUPG_Unsupported_Operation = 75, - GNUPG_Wrong_Key_Usage = 76, -}; -#endif /* !GPG_ERR_SOURCE_DEFAULT */ - /* Status codes - fixme: should go into another file */ enum { STATUS_ENTER, diff --git a/common/maperror.c b/common/maperror.c index e3e18e4a1..c6a1135c7 100644 --- a/common/maperror.c +++ b/common/maperror.c @@ -43,21 +43,21 @@ map_ksba_err (int err) case 0: break; - case KSBA_Out_Of_Core: err = GNUPG_Out_Of_Core; break; - case KSBA_Invalid_Value: err = GNUPG_Invalid_Value; break; - case KSBA_Not_Implemented: err = GNUPG_Not_Implemented; break; - case KSBA_Conflict: err = GNUPG_Conflict; break; - case KSBA_Read_Error: err = GNUPG_Read_Error; break; - case KSBA_Write_Error: err = GNUPG_Write_Error; break; - case KSBA_No_Data: err = GNUPG_No_Data; break; - case KSBA_Bug: err = GNUPG_Bug; break; - case KSBA_Unsupported_Algorithm: err = GNUPG_Unsupported_Algorithm; break; - case KSBA_Invalid_Index: err = GNUPG_Invalid_Index; break; - case KSBA_Invalid_Sexp: err = GNUPG_Invalid_Sexp; break; - case KSBA_Unknown_Sexp: err = GNUPG_Unknown_Sexp; break; + case KSBA_Out_Of_Core: err = GPG_ERR_ENOMEM; break; + case KSBA_Invalid_Value: err = GPG_ERR_INV_VALUE; break; + case KSBA_Not_Implemented: err = GPG_ERR_NOT_IMPLEMENTED; break; + case KSBA_Conflict: err = GPG_ERR_CONFLICT; break; + case KSBA_Read_Error: err = GPG_ERR_EIO; break; + case KSBA_Write_Error: err = GPG_ERR_EIO; break; + case KSBA_No_Data: err = GPG_ERR_NO_DATA; break; + case KSBA_Bug: err = GPG_ERR_BUG; break; + case KSBA_Unsupported_Algorithm: err = GPG_ERR_UNSUPPORTED_ALGORITHM; break; + case KSBA_Invalid_Index: err = GPG_ERR_INV_INDEX; break; + case KSBA_Invalid_Sexp: err = GPG_ERR_INV_SEXP; break; + case KSBA_Unknown_Sexp: err = GPG_ERR_UNKNOWN_SEXP; break; default: - err = seterr (General_Error); + err = GPG_ERR_GENERAL; break; } return err; @@ -67,200 +67,80 @@ map_ksba_err (int err) int map_gcry_err (int err) { - switch (err) - { - case GCRYERR_EOF: - case -1: - err = -1; - break; - - case 0: - break; - - case GCRYERR_WRONG_PK_ALGO: - case GCRYERR_INV_PK_ALGO: - case GCRYERR_INV_MD_ALGO: - case GCRYERR_INV_CIPHER_ALGO: - err = GNUPG_Unsupported_Algorithm; - break; - case GCRYERR_INV_KEYLEN: - case GCRYERR_WEAK_KEY: - case GCRYERR_BAD_PUBLIC_KEY: err = GNUPG_Bad_Public_Key; break; - case GCRYERR_BAD_SECRET_KEY: err = GNUPG_Bad_Secret_Key; break; - case GCRYERR_BAD_SIGNATURE: err = GNUPG_Bad_Signature; break; - - case GCRYERR_BAD_MPI: - err = GNUPG_Bad_Data; - break; - - case GCRYERR_INV_ARG: - case GCRYERR_INV_OP: - case GCRYERR_INTERNAL: - case GCRYERR_INV_CIPHER_MODE: - err = GNUPG_Invalid_Value; - break; - - case GCRYERR_SELFTEST: - err = GNUPG_Bug; - break; - - case GCRYERR_SEXP_INV_LEN_SPEC : - case GCRYERR_SEXP_STRING_TOO_LONG : - case GCRYERR_SEXP_UNMATCHED_PAREN : - case GCRYERR_SEXP_NOT_CANONICAL : - case GCRYERR_SEXP_BAD_CHARACTER : - case GCRYERR_SEXP_BAD_QUOTATION : - case GCRYERR_SEXP_ZERO_PREFIX : - case GCRYERR_SEXP_NESTED_DH : - case GCRYERR_SEXP_UNMATCHED_DH : - case GCRYERR_SEXP_UNEXPECTED_PUNC : - case GCRYERR_SEXP_BAD_HEX_CHAR : - case GCRYERR_SEXP_ODD_HEX_NUMBERS : - case GCRYERR_SEXP_BAD_OCT_CHAR : - err = GNUPG_Invalid_Sexp; - break; - - case GCRYERR_NO_MEM: err = GNUPG_Out_Of_Core; break; - - case GCRYERR_NOT_IMPL: err = GNUPG_Not_Implemented; break; - case GCRYERR_CONFLICT: err = GNUPG_Conflict; break; - - case GCRYERR_INV_OBJ: /* an object is not valid */ - case GCRYERR_TOO_SHORT: /* provided buffer too short */ - case GCRYERR_TOO_LARGE: /* object is too large */ - case GCRYERR_NO_OBJ: /* Missing item in an object */ - default: - err = seterr (General_Error); - break; - } return err; } int map_kbx_err (int err) { - switch (err) - { - case -1: - case 0: - break; - - default: - err = seterr (General_Error); - break; - } return err; } - -int +/* Map Assuan error code ERR to an GPG_ERR_ code. We need to + distinguish between genuine (and legacy) Assuan error codes and + application error codes shared with all GnuPG modules. The rule is + simple: All errors with a gpg_err_source of UNKNOWN are genuine + Assuan codes all others are passed verbatim through. */ +gpg_error_t map_assuan_err (int err) { + gpg_err_code_t ec; + + if (gpg_err_source (err)) + return err; + switch (err) { - case -1: - case 0: - break; + case -1: ec = GPG_ERR_EOF; break; + case 0: ec = 0; break; - case ASSUAN_Canceled: err = GNUPG_Canceled; break; - case ASSUAN_Invalid_Index: err = GNUPG_Invalid_Index; break; + case ASSUAN_Canceled: ec = GPG_ERR_CANCELED; break; + case ASSUAN_Invalid_Index: ec = GPG_ERR_INV_INDEX; break; - case ASSUAN_Not_Implemented: err = GNUPG_Not_Implemented; break; - case ASSUAN_Server_Fault: err = GNUPG_Assuan_Server_Fault; break; - case ASSUAN_No_Public_Key: err = GNUPG_No_Public_Key; break; - case ASSUAN_No_Secret_Key: err = GNUPG_No_Secret_Key; break; + case ASSUAN_Not_Implemented: ec = GPG_ERR_NOT_IMPLEMENTED; break; + case ASSUAN_Server_Fault: ec = GPG_ERR_ASSUAN_SERVER_FAULT; break; + case ASSUAN_No_Public_Key: ec = GPG_ERR_NO_PUBKEY; break; + case ASSUAN_No_Secret_Key: ec = GPG_ERR_NO_SECKEY; break; - case ASSUAN_Cert_Revoked: err = GNUPG_Certificate_Revoked; break; - case ASSUAN_No_CRL_For_Cert: err = GNUPG_No_CRL_Known; break; - case ASSUAN_CRL_Too_Old: err = GNUPG_CRL_Too_Old; break; + case ASSUAN_Cert_Revoked: ec = GPG_ERR_CERT_REVOKED; break; + case ASSUAN_No_CRL_For_Cert: ec = GPG_ERR_NO_CRL_KNOWN; break; + case ASSUAN_CRL_Too_Old: ec = GPG_ERR_CRL_TOO_OLD; break; - case ASSUAN_Not_Trusted: err = GNUPG_Not_Trusted; break; + case ASSUAN_Not_Trusted: ec = GPG_ERR_NOT_TRUSTED; break; - case ASSUAN_Card_Error: err = GNUPG_Card_Error; break; - case ASSUAN_Invalid_Card: err = GNUPG_Invalid_Card; break; - case ASSUAN_No_PKCS15_App: err = GNUPG_No_PKCS15_App; break; - case ASSUAN_Card_Not_Present: err= GNUPG_Card_Not_Present; break; - case ASSUAN_Not_Confirmed: err = GNUPG_Not_Confirmed; break; - case ASSUAN_Invalid_Id: err = GNUPG_Invalid_Id; break; + case ASSUAN_Card_Error: ec = GPG_ERR_CARD; break; + case ASSUAN_Invalid_Card: ec = GPG_ERR_INV_CARD; break; + case ASSUAN_No_PKCS15_App: ec = GPG_ERR_NO_PKCS15_APP; break; + case ASSUAN_Card_Not_Present: ec= GPG_ERR_CARD_NOT_PRESENT; break; + case ASSUAN_Not_Confirmed: ec = GPG_ERR_NOT_CONFIRMED; break; + case ASSUAN_Invalid_Id: ec = GPG_ERR_INV_ID; break; default: - err = err < 100? GNUPG_Assuan_Server_Fault : GNUPG_Assuan_Error; + ec = err < 100? GPG_ERR_ASSUAN_SERVER_FAULT : GPG_ERR_ASSUAN; break; } - return err; + return gpg_err_make (GPG_ERR_SOURCE_UNKNOWN, ec); } -/* Map GNUPG_xxx error codes to Assuan status codes */ +/* Map GPG_xERR_xx error codes to Assuan status codes */ int map_to_assuan_status (int rc) { - switch (rc) - { - case -1: - rc = ASSUAN_No_Data_Available; - break; - case 0: break; - case GNUPG_Bad_CA_Certificate: - case GNUPG_Bad_Certificate: - case GNUPG_Wrong_Key_Usage: - case GNUPG_Certificate_Revoked: - case GNUPG_No_CRL_Known: - case GNUPG_CRL_Too_Old: - case GNUPG_No_Policy_Match: - case GNUPG_Certificate_Expired: - rc = ASSUAN_Bad_Certificate; - break; - case GNUPG_Bad_Certificate_Chain: rc = ASSUAN_Bad_Certificate_Chain; break; - case GNUPG_Missing_Certificate: rc = ASSUAN_Missing_Certificate; break; - case GNUPG_No_Data: rc = ASSUAN_No_Data_Available; break; - case GNUPG_Bad_Signature: rc = ASSUAN_Bad_Signature; break; - case GNUPG_Not_Implemented: rc = ASSUAN_Not_Implemented; break; - case GNUPG_No_Agent: rc = ASSUAN_No_Agent; break; - case GNUPG_Agent_Error: rc = ASSUAN_Agent_Error; break; - case GNUPG_No_Public_Key: rc = ASSUAN_No_Public_Key; break; - case GNUPG_No_Secret_Key: rc = ASSUAN_No_Secret_Key; break; - case GNUPG_Invalid_Data: rc = ASSUAN_Invalid_Data; break; - case GNUPG_Invalid_Name: rc = ASSUAN_Invalid_Name; break; - case GNUPG_Not_Trusted: rc = ASSUAN_Not_Trusted; break; - case GNUPG_Canceled: rc = ASSUAN_Canceled; break; - case GNUPG_Invalid_Index: rc = ASSUAN_Invalid_Index; break; + gpg_err_code_t ec = gpg_err_code (rc); + gpg_err_source_t es = gpg_err_source (rc); - case GNUPG_Card_Error: - case GNUPG_Card_Reset: - rc = ASSUAN_Card_Error; - break; - case GNUPG_Card_Removed: - case GNUPG_Card_Not_Present: - rc = ASSUAN_Card_Not_Present; - break; - case GNUPG_Invalid_Card: rc = ASSUAN_Invalid_Card; break; - case GNUPG_No_PKCS15_App: rc = ASSUAN_No_PKCS15_App; break; - case GNUPG_Not_Confirmed: rc = ASSUAN_Not_Confirmed; break; - case GNUPG_Invalid_Id: rc = ASSUAN_Invalid_Id; break; + if (!es) + { + es = GPG_ERR_SOURCE_USER_4; /* This should not happen, but we + need to make sure to pass a new + Assuan errorcode along. */ + log_debug ("map_to_assuan_status called with no error source\n"); + } - case GNUPG_Bad_PIN: - case GNUPG_Bad_Passphrase: - rc = ASSUAN_No_Secret_Key; - break; + if (ec == -1) + ec = GPG_ERR_NO_DATA; /* That used to be ASSUAN_No_Data_Available. */ - case GNUPG_Read_Error: - case GNUPG_Write_Error: - case GNUPG_IO_Error: - rc = ASSUAN_Server_IO_Error; - break; - case GNUPG_Out_Of_Core: - case GNUPG_Resource_Limit: - rc = ASSUAN_Server_Resource_Problem; - break; - case GNUPG_Bug: - case GNUPG_Internal_Error: - rc = ASSUAN_Server_Bug; - break; - default: - rc = ASSUAN_Server_Fault; - break; - } - return rc; + return gpg_err_make (es, ec); } diff --git a/common/membuf.c b/common/membuf.c new file mode 100644 index 000000000..69e4ab908 --- /dev/null +++ b/common/membuf.c @@ -0,0 +1,89 @@ +/* membuf.c - A simple implementation of a dynamic buffer + * Copyright (C) 2001, 2003 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * GnuPG is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include <config.h> +#include <stdlib.h> +#include <errno.h> + +#include "membuf.h" + +#include "util.h" + + +/* A simple implementation of a dynamic buffer. Use init_membuf() to + create a buffer, put_membuf to append bytes and get_membuf to + release and return the buffer. Allocation errors are detected but + only returned at the final get_membuf(), this helps not to clutter + the code with out of core checks. */ + +void +init_membuf (membuf_t *mb, int initiallen) +{ + mb->len = 0; + mb->size = initiallen; + mb->out_of_core = 0; + mb->buf = xtrymalloc (initiallen); + if (!mb->buf) + mb->out_of_core = errno; +} + + +void +put_membuf (membuf_t *mb, const void *buf, size_t len) +{ + if (mb->out_of_core) + return; + + if (mb->len + len >= mb->size) + { + char *p; + + mb->size += len + 1024; + p = xtryrealloc (mb->buf, mb->size); + if (!p) + { + mb->out_of_core = errno; + return; + } + mb->buf = p; + } + memcpy (mb->buf + mb->len, buf, len); + mb->len += len; +} + + +void * +get_membuf (membuf_t *mb, size_t *len) +{ + char *p; + + if (mb->out_of_core) + { + xfree (mb->buf); + mb->buf = NULL; + return NULL; + } + + p = mb->buf; + *len = mb->len; + mb->buf = NULL; + mb->out_of_core = ENOMEM; /* hack to make sure it won't get reused. */ + return p; +} diff --git a/common/membuf.h b/common/membuf.h new file mode 100644 index 000000000..c199363cc --- /dev/null +++ b/common/membuf.h @@ -0,0 +1,41 @@ +/* membuf.h - A simple implementation of a dynamic buffer + * Copyright (C) 2001, 2003 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * GnuPG is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifndef GNUPG_COMMON_MEMBUF_H +#define GNUPG_COMMON_MEMBUF_H + +/* The definition of the structure is private, we only need it here, + so it can be allocated on the stack. */ +struct private_membuf_s { + size_t len; + size_t size; + char *buf; + int out_of_core; +}; + +typedef struct private_membuf_s membuf_t; + + +void init_membuf (membuf_t *mb, int initiallen); +void put_membuf (membuf_t *mb, const void *buf, size_t len); +void *get_membuf (membuf_t *mb, size_t *len); + + +#endif /*GNUPG_COMMON_MEMBUF_H*/ diff --git a/common/util.h b/common/util.h index ef988f672..c6d2ea7b8 100644 --- a/common/util.h +++ b/common/util.h @@ -21,8 +21,9 @@ #ifndef GNUPG_COMMON_UTIL_H #define GNUPG_COMMON_UTIL_H -#include <gcrypt.h> /* we need this for the memory function protos */ -#include <time.h> /* we need time_t */ +#include <gcrypt.h> /* We need this for the memory function protos. */ +#include <time.h> /* We need time_t. */ +#include <gpg-error.h> /* we need gpg-error_t. */ /* to pass hash functions to libksba we need to cast it */ #define HASH_FNC ((void (*)(void *, const void*,size_t))gcry_md_write) @@ -53,7 +54,7 @@ int map_ksba_err (int err); int map_gcry_err (int err); int map_kbx_err (int err); -int map_assuan_err (int err); +gpg_error_t map_assuan_err (int err); int map_to_assuan_status (int rc); /*-- gettime.c --*/ |