diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-04-15 15:00:26 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-04-15 15:00:26 +0200 |
commit | 74e237164ce7958c19467ad440e45576c4425570 (patch) | |
tree | e5156b7197cbb76ad0b0d617d9a04eeb2c08ea48 /src/util | |
parent | ebf049a8c2d982e723fe67dbff64e1eea14d8247 (diff) | |
parent | 3098c0a9e0b18a436e484ef693cdebeb16d4c131 (diff) | |
download | exchange-74e237164ce7958c19467ad440e45576c4425570.tar.gz exchange-74e237164ce7958c19467ad440e45576c4425570.tar.bz2 exchange-74e237164ce7958c19467ad440e45576c4425570.zip |
Merge branch 'master' of ssh://taler.net:/var/git/exchange
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/Makefile.am | 44 | ||||
-rw-r--r-- | src/util/amount.c | 24 | ||||
-rw-r--r-- | src/util/json.c | 400 | ||||
-rw-r--r-- | src/util/os_installation.c | 687 | ||||
-rw-r--r-- | src/util/paths.conf | 29 | ||||
-rw-r--r-- | src/util/plugin.c | 88 | ||||
-rw-r--r-- | src/util/taler-config.in | 10 | ||||
-rw-r--r-- | src/util/test_amount.c | 13 | ||||
-rw-r--r-- | src/util/test_json.c | 182 | ||||
-rw-r--r-- | src/util/util.c | 177 | ||||
-rw-r--r-- | src/util/wireformats.c | 441 |
11 files changed, 128 insertions, 1967 deletions
diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 8efc3987a..22bc788b8 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -1,11 +1,32 @@ # This Makefile.am is in the public domain -AM_CPPFLAGS = -I$(top_srcdir)/src/include $(LIBGCRYPT_CFLAGS) $(POSTGRESQL_CPPFLAGS) +AM_CPPFLAGS = -I$(top_srcdir)/src/include $(LIBGCRYPT_CFLAGS) if USE_COVERAGE AM_CFLAGS = --coverage -O0 XLIB = -lgcov endif + +pkgcfgdir = $(prefix)/share/taler/config.d/ + +pkgcfg_DATA = \ + paths.conf + +EXTRA_DIST = \ + paths.conf + +dist_bin_SCRIPTS = taler-config + +# See https://www.gnu.org/software/autoconf/manual/autoconf-2.63/html_node/Installation-Directory-Variables.html +# for and explanation and why this ugliness is necessary. +edit = sed -e 's|@libdir[@]|$(libdir)|g' +taler-config: Makefile $(srcdir)/taler-config.in + rm -f $@ $@.tmp + $(edit) '$(srcdir)/$@.in' >$@.tmp + chmod +x $@.tmp + chmod a-w $@.tmp + mv $@.tmp $@ + if WALLET_ONLY lib_LTLIBRARIES = \ libtalerutil_wallet.la @@ -31,15 +52,11 @@ libtalerutil_la_SOURCES = \ amount.c \ crypto.c \ util.c \ - json.c \ - os_installation.c \ - plugin.c \ - wireformats.c + os_installation.c libtalerutil_la_LIBADD = \ -lgnunetutil \ $(LIBGCRYPT_LIBS) \ - -ljansson \ -lmicrohttpd $(XLIB) libtalerutil_la_LDFLAGS = \ @@ -48,13 +65,11 @@ libtalerutil_la_LDFLAGS = \ TESTS = \ test_amount \ - test_crypto \ - test_json + test_crypto -check_PROGRAMS= \ +check_PROGRAMS = \ test_amount \ - test_crypto \ - test_json + test_crypto test_amount_SOURCES = \ @@ -68,10 +83,3 @@ test_crypto_SOURCES = \ test_crypto_LDADD = \ -lgnunetutil \ libtalerutil.la - -test_json_SOURCES = \ - test_json.c -test_json_LDADD = \ - -lgnunetutil \ - -ljansson \ - libtalerutil.la diff --git a/src/util/amount.c b/src/util/amount.c index dc2d2e400..4ac7d30ab 100644 --- a/src/util/amount.c +++ b/src/util/amount.c @@ -151,6 +151,30 @@ TALER_string_to_amount (const char *str, /** + * Parse denomination description, in the format "T:V.F". + * + * @param str denomination description + * @param denom denomination to write the result to, in NBO + * @return #GNUNET_OK if the string is a valid denomination specification, + * #GNUNET_SYSERR if it is invalid. + */ +int +TALER_string_to_amount_nbo (const char *str, + struct TALER_AmountNBO *denom) +{ + struct TALER_Amount amount; + + if (GNUNET_OK != + TALER_string_to_amount (str, + &amount)) + return GNUNET_SYSERR; + TALER_amount_hton (denom, + &amount); + return GNUNET_OK; +} + + +/** * Convert amount from host to network representation. * * @param res where to store amount in network representation diff --git a/src/util/json.c b/src/util/json.c deleted file mode 100644 index 6aca7548c..000000000 --- a/src/util/json.c +++ /dev/null @@ -1,400 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014, 2015 GNUnet e.V. - - TALER 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 3, or (at your option) any later version. - - TALER 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 - TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file util/json.c - * @brief helper functions for JSON processing using libjansson - * @author Sree Harsha Totakura <sreeharsha@totakura.in> - */ -#include "platform.h" -#if HAVE_GNUNET_GNUNET_UTIL_TALER_WALLET_LIB_H -#include <gnunet/gnunet_util_taler_wallet_lib.h> -#endif -#if HAVE_GNUNET_GNUNET_UTIL_LIB_H -#include <gnunet/gnunet_util_lib.h> -#endif -#include "taler_util.h" - -/** - * Shorthand for exit jumps. - */ -#define EXITIF(cond) \ - do { \ - if (cond) { GNUNET_break (0); goto EXITIF_exit; } \ - } while (0) - -/** - * Shorthand for JSON parsing related exit jumps. - */ -#define UNPACK_EXITIF(cond) \ - do { \ - if (cond) { TALER_json_warn (error); goto EXITIF_exit; } \ - } while (0) - - -/** - * Convert a TALER amount to a JSON - * object. - * - * @param amount the amount - * @return a json object describing the amount - */ -json_t * -TALER_json_from_amount (const struct TALER_Amount *amount) -{ - json_t *j; - - if ( (amount->value != (uint64_t) ((json_int_t) amount->value)) || - (0 > ((json_int_t) amount->value)) ) - { - /* Theoretically, json_int_t can be a 32-bit "long", or we might - have a 64-bit value which converted to a 63-bit signed long - long causes problems here. So we check. Note that depending - on the platform, the compiler may be able to statically tell - that at least the first check is always false. */ - GNUNET_break (0); - return NULL; - } - j = json_pack ("{s:s, s:I, s:I}", - "currency", amount->currency, - "value", (json_int_t) amount->value, - "fraction", (json_int_t) amount->fraction); - GNUNET_assert (NULL != j); - return j; -} - - -/** - * Convert absolute timestamp to a json string. - * - * @param stamp the time stamp - * @return a json string with the timestamp in @a stamp - */ -json_t * -TALER_json_from_abs (struct GNUNET_TIME_Absolute stamp) -{ - json_t *j; - char *mystr; - int ret; - - GNUNET_assert (GNUNET_OK == - TALER_round_abs_time (&stamp)); - if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) - return json_string ("/never/"); - ret = GNUNET_asprintf (&mystr, - "/Date(%llu)/", - (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL))); - GNUNET_assert (ret > 0); - j = json_string (mystr); - GNUNET_free (mystr); - return j; -} - - -/** - * Convert RSA public key to JSON. - * - * @param pk public key to convert - * @return corresponding JSON encoding - */ -json_t * -TALER_json_from_rsa_public_key (struct GNUNET_CRYPTO_rsa_PublicKey *pk) -{ - char *buf; - size_t buf_len; - json_t *ret; - - buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk, - &buf); - ret = TALER_json_from_data (buf, - buf_len); - GNUNET_free (buf); - return ret; -} - - -/** - * Convert JSON to RSA public key. - * - * @param json JSON encoding to convert - * @return corresponding public key - */ -struct GNUNET_CRYPTO_rsa_PublicKey * -TALER_json_to_rsa_public_key (json_t *json) -{ - const char *enc; - char *buf; - size_t len; - size_t buf_len; - struct GNUNET_CRYPTO_rsa_PublicKey *pk; - - buf = NULL; - EXITIF (NULL == (enc = json_string_value (json))); - len = strlen (enc); - buf_len = (len * 5) / 8; - buf = GNUNET_malloc (buf_len); - EXITIF (GNUNET_OK != - GNUNET_STRINGS_string_to_data (enc, - len, - buf, - buf_len)); - EXITIF (NULL == (pk = GNUNET_CRYPTO_rsa_public_key_decode (buf, - buf_len))); - GNUNET_free (buf); - return pk; - EXITIF_exit: - GNUNET_free_non_null (buf); - return NULL; -} - - -/** - * Convert JSON to RSA signature. - * - * @param json JSON encoding to convert - * @return corresponding signature - */ -struct GNUNET_CRYPTO_rsa_Signature * -TALER_json_to_rsa_signature (json_t *json) -{ - const char *enc; - char *buf; - size_t len; - size_t buf_len; - struct GNUNET_CRYPTO_rsa_Signature *sig; - - buf = NULL; - EXITIF (NULL == (enc = json_string_value (json))); - len = strlen (enc); - buf_len = (len * 5) / 8; - buf = GNUNET_malloc (buf_len); - EXITIF (GNUNET_OK != - GNUNET_STRINGS_string_to_data (enc, - len, - buf, - buf_len)); - EXITIF (NULL == (sig = GNUNET_CRYPTO_rsa_signature_decode (buf, - buf_len))); - GNUNET_free (buf); - return sig; - EXITIF_exit: - GNUNET_free_non_null (buf); - return NULL; -} - - -/** - * Convert RSA signature to JSON. - * - * @param sig signature to convert - * @return corresponding JSON encoding - */ -json_t * -TALER_json_from_rsa_signature (struct GNUNET_CRYPTO_rsa_Signature *sig) -{ - char *buf; - size_t buf_len; - json_t *ret; - - buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig, - &buf); - ret = TALER_json_from_data (buf, - buf_len); - GNUNET_free (buf); - return ret; -} - - -/** - * Convert binary data to a JSON string - * with the base32crockford encoding. - * - * @param data binary data - * @param size size of @a data in bytes - * @return json string that encodes @a data - */ -json_t * -TALER_json_from_data (const void *data, - size_t size) -{ - char *buf; - json_t *json; - - buf = GNUNET_STRINGS_data_to_string_alloc (data, size); - json = json_string (buf); - GNUNET_free (buf); - return json; -} - - -/** - * Parse given JSON object to Amount - * - * @param json the json object representing Amount - * @param[out] r_amount where the amount has to be written - * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error - */ -int -TALER_json_to_amount (json_t *json, - struct TALER_Amount *r_amount) -{ - json_int_t value; - json_int_t fraction; - const char *currency; - - memset (r_amount, - 0, - sizeof (struct TALER_Amount)); - if (0 != json_unpack (json, - "{s:I, s:I, s:s}", - "value", &value, - "fraction", &fraction, - "currency", ¤cy)) - { - char *json_enc; - - if (NULL == (json_enc = json_dumps (json, - JSON_COMPACT | JSON_ENCODE_ANY))) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Malformed JSON amount: %s\n", - json_enc); - free (json_enc); - return GNUNET_SYSERR; - } - if ( (value < 0) || - (fraction < 0) || - (value > UINT64_MAX) || - (fraction > UINT32_MAX) ) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - if (strlen (currency) >= TALER_CURRENCY_LEN) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - r_amount->value = (uint64_t) value; - r_amount->fraction = (uint32_t) fraction; - strcpy (r_amount->currency, currency); - (void) TALER_amount_normalize (r_amount); - return GNUNET_OK; -} - - -/** - * Parse given JSON object to absolute time. - * - * @param json the json object representing Amount - * @param[out] abs where the amount has to be written - * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error - */ -int -TALER_json_to_abs (json_t *json, - struct GNUNET_TIME_Absolute *abs) -{ - const char *val; - unsigned long long int tval; - - val = json_string_value (json); - if (NULL == val) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - if ( (0 == strcasecmp (val, - "/forever/")) || - (0 == strcasecmp (val, - "/never/")) ) - { - *abs = GNUNET_TIME_UNIT_FOREVER_ABS; - return GNUNET_OK; - } - if (1 != sscanf (val, - "/Date(%llu)/", - &tval)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ - abs->abs_value_us = tval * 1000LL * 1000LL; - if ( (abs->abs_value_us) / 1000LL / 1000LL != tval) - { - /* Integer overflow */ - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - return GNUNET_OK; -} - - -/** - * Parse given JSON object to data - * - * @param json the json object representing data - * @param out the pointer to hold the parsed data. - * @param out_size the size of @a out - * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error - */ -int -TALER_json_to_data (json_t *json, - void *out, - size_t out_size) -{ - const char *enc; - unsigned int len; - - EXITIF (NULL == (enc = json_string_value (json))); - len = strlen (enc); - EXITIF (((len * 5) / 8) != out_size); - EXITIF (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, len, out, out_size)); - return GNUNET_OK; - EXITIF_exit: - return GNUNET_SYSERR; -} - - -/** - * Hash a JSON for binary signing. - * - * @param[in] json some JSON value - * @param[out] hc resulting hash code - * @return #GNUNET_OK on success, #GNUNET_SYSERR on error - */ -int -TALER_hash_json (json_t *json, - struct GNUNET_HashCode *hc) -{ - char *wire_enc; - size_t len; - - if (NULL == (wire_enc = json_dumps (json, - JSON_COMPACT | JSON_SORT_KEYS))) - return GNUNET_SYSERR; - len = strlen (wire_enc) + 1; - GNUNET_CRYPTO_hash (wire_enc, - len, - hc); - free (wire_enc); - return GNUNET_OK; -} - - -/* End of util/json.c */ diff --git a/src/util/os_installation.c b/src/util/os_installation.c index 0eab118fe..5aa34f1b9 100644 --- a/src/util/os_installation.c +++ b/src/util/os_installation.c @@ -1,701 +1,64 @@ /* - This file is part of GNUnet. - Copyright (C) 2006-2014 GNUnet e.V. + This file is part of GNU Taler. + Copyright (C) 2016 Inria - GNUnet is free software; you can redistribute it and/or modify + Taler 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 3, or (at your option) any later version. - GNUnet is distributed in the hope that it will be useful, but + Taler 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 GNUnet; see the file COPYING. If not, write to the + along with Taler; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** * @file os_installation.c - * @brief get paths used by the program; based heavily on the - * corresponding GNUnet file, just adapted for Taler. - * @author Milan + * @brief initialize libgnunet OS subsystem for Taler. + * @author Christian Grothoff */ #include "platform.h" #include <gnunet/gnunet_util_lib.h> -#if DARWIN -#include <mach-o/ldsyms.h> -#include <mach-o/dyld.h> -#elif WINDOWS -#include <windows.h> -#endif -#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) - -#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename) - - -#if LINUX /** - * Try to determine path by reading /proc/PID/exe - * - * @return NULL on error + * Default project data used for installation path detection + * for GNU Taler. */ -static char * -get_path_from_proc_maps () -{ - char fn[64]; - char line[1024]; - char dir[1024]; - FILE *f; - char *lgu; - - GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ()); - if (NULL == (f = FOPEN (fn, "r"))) - return NULL; - while (NULL != fgets (line, sizeof (line), f)) - { - if ((1 == - SSCANF (line, "%*x-%*x %*c%*c%*c%*c %*x %*2x:%*2x %*u%*[ ]%1023s", dir)) && - (NULL != (lgu = strstr (dir, "libtalerutil")))) - { - lgu[0] = '\0'; - FCLOSE (f); - return GNUNET_strdup (dir); - } - } - FCLOSE (f); - return NULL; -} +static const struct GNUNET_OS_ProjectData taler_pd = { + .libname = "libtalerutil", + .project_dirname = "taler", + .binary_name = "taler-exchange-httpd", + .env_varname = "TALER_PREFIX", + .bug_email = "taler@gnu.org", + .homepage = "http://www.gnu.org/s/taler/", +}; /** - * Try to determine path by reading /proc/PID/exe - * - * @return NULL on error + * Return default project data used by Taler. */ -static char * -get_path_from_proc_exe () +const struct GNUNET_OS_ProjectData * +TALER_project_data_default (void) { - char fn[64]; - char lnk[1024]; - ssize_t size; - - GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ()); - size = readlink (fn, lnk, sizeof (lnk) - 1); - if (size <= 0) - { - LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn); - return NULL; - } - GNUNET_assert (size < sizeof (lnk)); - lnk[size] = '\0'; - while ((lnk[size] != '/') && (size > 0)) - size--; - /* test for being in lib/taler/libexec/ or lib/MULTIARCH/taler/libexec */ - if ( (size > strlen ("/taler/libexec/")) && - (0 == strcmp ("/taler/libexec/", - &lnk[size - strlen ("/taler/libexec/")])) ) - size -= strlen ("taler/libexec/"); - if ((size < 4) || (lnk[size - 4] != '/')) - { - /* not installed in "/bin/" -- binary path probably useless */ - return NULL; - } - lnk[size] = '\0'; - return GNUNET_strdup (lnk); + return &taler_pd; } -#endif - - -#if WINDOWS -static HINSTANCE dll_instance; /** - * GNUNET_util_cl_init() in common_logging.c is preferred. - * This function is only for thread-local storage (not used in GNUnet) - * and hInstance saving. + * Initialize libtalerutil. */ -BOOL WINAPI -DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) +void __attribute__ ((constructor)) +TALER_OS_init () { - switch (fdwReason) - { - case DLL_PROCESS_ATTACH: - dll_instance = hinstDLL; - break; - case DLL_THREAD_ATTACH: - break; - case DLL_THREAD_DETACH: - break; - case DLL_PROCESS_DETACH: - break; - } - return TRUE; + GNUNET_OS_init (&taler_pd); } -/** - * Try to determine path with win32-specific function - * - * @return NULL on error - */ -static char * -get_path_from_module_filename () -{ - size_t pathlen = 512; - DWORD real_pathlen; - wchar_t *idx; - wchar_t *modulepath = NULL; - char *upath; - uint8_t *u8_string; - size_t u8_string_length; - - /* This braindead function won't tell us how much space it needs, so - * we start at 1024 and double the space up if it doesn't fit, until - * it fits, or we exceed the threshold. - */ - do - { - pathlen = pathlen * 2; - modulepath = GNUNET_realloc (modulepath, pathlen * sizeof (wchar_t)); - SetLastError (0); - real_pathlen = GetModuleFileNameW (dll_instance, modulepath, pathlen * sizeof (wchar_t)); - } while (real_pathlen >= pathlen && pathlen < 16*1024); - if (real_pathlen >= pathlen) - GNUNET_assert (0); - /* To be safe */ - modulepath[real_pathlen] = '\0'; - - idx = modulepath + real_pathlen; - while ((idx > modulepath) && (*idx != L'\\') && (*idx != L'/')) - idx--; - *idx = L'\0'; - - /* Now modulepath holds full path to the directory where libtalerutil is. - * This directory should look like <TALER_PREFIX>/bin or <TALER_PREFIX>. - */ - if (wcschr (modulepath, L'/') || wcschr (modulepath, L'\\')) - { - /* At least one directory component (i.e. we're not in a root directory) */ - wchar_t *dirname = idx; - while ((dirname > modulepath) && (*dirname != L'\\') && (*dirname != L'/')) - dirname--; - *dirname = L'\0'; - if (dirname > modulepath) - { - dirname++; - /* Now modulepath holds full path to the parent directory of the directory - * where libtalerutil is. - * dirname holds the name of the directory where libtalerutil is. - */ - if (wcsicmp (dirname, L"bin") == 0) - { - /* pass */ - } - else - { - /* Roll back our changes to modulepath */ - dirname--; - *dirname = L'/'; - } - } - } - - /* modulepath is TALER_PREFIX */ - u8_string = u16_to_u8 (modulepath, wcslen (modulepath), NULL, &u8_string_length); - if (NULL == u8_string) - GNUNET_assert (0); - - upath = GNUNET_malloc (u8_string_length + 1); - memcpy (upath, u8_string, u8_string_length); - upath[u8_string_length] = '\0'; - - free (u8_string); - GNUNET_free (modulepath); - - return upath; -} -#endif - - -#if DARWIN -/** - * Signature of the '_NSGetExecutablePath" function. - * - * @param buf where to write the path - * @param number of bytes available in 'buf' - * @return 0 on success, otherwise desired number of bytes is stored in 'bufsize' - */ -typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize); - - -/** - * Try to obtain the path of our executable using '_NSGetExecutablePath'. - * - * @return NULL on error - */ -static char * -get_path_from_NSGetExecutablePath () -{ - static char zero = '\0'; - char *path; - size_t len; - MyNSGetExecutablePathProto func; - - path = NULL; - if (NULL == (func = - (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"))) - return NULL; - path = &zero; - len = 0; - /* get the path len, including the trailing \0 */ - (void) func (path, &len); - if (0 == len) - return NULL; - path = GNUNET_malloc (len); - if (0 != func (path, &len)) - { - GNUNET_free (path); - return NULL; - } - len = strlen (path); - while ((path[len] != '/') && (len > 0)) - len--; - path[len] = '\0'; - return path; -} - - -/** - * Try to obtain the path of our executable using '_dyld_image' API. - * - * @return NULL on error - */ -static char * -get_path_from_dyld_image () -{ - const char *path; - char *p; - char *s; - unsigned int i; - int c; - - c = _dyld_image_count (); - for (i = 0; i < c; i++) - { - if (((const void *) _dyld_get_image_header (i)) != (const void *)&_mh_dylib_header) - continue; - path = _dyld_get_image_name (i); - if ( (NULL == path) || (0 == strlen (path)) ) - continue; - p = GNUNET_strdup (path); - s = p + strlen (p); - while ((s > p) && ('/' != *s)) - s--; - s++; - *s = '\0'; - return p; - } - return NULL; -} -#endif - - -/** - * Return the actual path to a file found in the current - * PATH environment variable. - * - * @param binary the name of the file to find - * @return path to binary, NULL if not found - */ -static char * -get_path_from_PATH (const char *binary) -{ - char *path; - char *pos; - char *end; - char *buf; - const char *p; - - if (NULL == (p = getenv ("PATH"))) - return NULL; -#if WINDOWS - /* On W32 look in CWD first. */ - GNUNET_asprintf (&path, ".%c%s", PATH_SEPARATOR, p); -#else - path = GNUNET_strdup (p); /* because we write on it */ -#endif - buf = GNUNET_malloc (strlen (path) + strlen (binary) + 1 + 1); - pos = path; - while (NULL != (end = strchr (pos, PATH_SEPARATOR))) - { - *end = '\0'; - sprintf (buf, "%s/%s", pos, binary); - if (GNUNET_DISK_file_test (buf) == GNUNET_YES) - { - pos = GNUNET_strdup (pos); - GNUNET_free (buf); - GNUNET_free (path); - return pos; - } - pos = end + 1; - } - sprintf (buf, "%s/%s", pos, binary); - if (GNUNET_YES == GNUNET_DISK_file_test (buf)) - { - pos = GNUNET_strdup (pos); - GNUNET_free (buf); - GNUNET_free (path); - return pos; - } - GNUNET_free (buf); - GNUNET_free (path); - return NULL; -} - - -/** - * Try to obtain the installation path using the "TALER_PREFIX" environment - * variable. - * - * @return NULL on error (environment variable not set) - */ -static char * -get_path_from_TALER_PREFIX () -{ - const char *p; - - if (NULL != (p = getenv ("TALER_PREFIX"))) - return GNUNET_strdup (p); - return NULL; -} - - -/** - * @brief get the path to Taler bin/ or lib/, prefering the lib/ path - * @author Milan - * - * @return a pointer to the executable path, or NULL on error - */ -static char * -os_get_taler_path () -{ - char *ret; - - if (NULL != (ret = get_path_from_TALER_PREFIX ())) - return ret; -#if LINUX - if (NULL != (ret = get_path_from_proc_maps ())) - return ret; - /* try path *first*, before /proc/exe, as /proc/exe can be wrong */ - if (NULL != (ret = get_path_from_PATH ("taler-mint-httpd"))) - return ret; - if (NULL != (ret = get_path_from_proc_exe ())) - return ret; -#endif -#if WINDOWS - if (NULL != (ret = get_path_from_module_filename ())) - return ret; -#endif -#if DARWIN - if (NULL != (ret = get_path_from_dyld_image ())) - return ret; - if (NULL != (ret = get_path_from_NSGetExecutablePath ())) - return ret; -#endif - if (NULL != (ret = get_path_from_PATH ("taler-mint-httpd"))) - return ret; - /* other attempts here */ - LOG (GNUNET_ERROR_TYPE_ERROR, - _("Could not determine installation path for %s. Set `%s' environment variable.\n"), - "Taler", "TALER_PREFIX"); - return NULL; -} - - -/** - * @brief get the path to current app's bin/ - * @author Milan - * - * @return a pointer to the executable path, or NULL on error - */ -static char * -os_get_exec_path () -{ - char *ret = NULL; - -#if LINUX - if (NULL != (ret = get_path_from_proc_exe ())) - return ret; -#endif -#if WINDOWS - if (NULL != (ret = get_path_from_module_filename ())) - return ret; -#endif -#if DARWIN - if (NULL != (ret = get_path_from_NSGetExecutablePath ())) - return ret; -#endif - /* other attempts here */ - return ret; -} - - -/** - * @brief get the path to a specific Taler installation directory or, - * with #GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation directory - * @author Milan - * @return a pointer to the dir path (to be freed by the caller) - */ -char * -TALER_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind) -{ - size_t n; - const char *dirname; - char *execpath = NULL; - char *tmp; - char *multiarch; - char *libdir; - int isbasedir; - - /* if wanted, try to get the current app's bin/ */ - if (dirkind == GNUNET_OS_IPK_SELF_PREFIX) - execpath = os_get_exec_path (); - - /* try to get Taler's bin/ or lib/, or if previous was unsuccessful some - * guess for the current app */ - if (NULL == execpath) - execpath = os_get_taler_path (); - - if (NULL == execpath) - return NULL; - - n = strlen (execpath); - if (0 == n) - { - /* should never happen, but better safe than sorry */ - GNUNET_free (execpath); - return NULL; - } - /* remove filename itself */ - while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1])) - execpath[--n] = '\0'; - - isbasedir = 1; - if ((n > 6) && - ((0 == strcasecmp (&execpath[n - 6], "/lib32")) || - (0 == strcasecmp (&execpath[n - 6], "/lib64")))) - { - if ( (GNUNET_OS_IPK_LIBDIR != dirkind) && - (GNUNET_OS_IPK_LIBEXECDIR != dirkind) ) - { - /* strip '/lib32' or '/lib64' */ - execpath[n - 6] = '\0'; - n -= 6; - } - else - isbasedir = 0; - } - else if ((n > 4) && - ((0 == strcasecmp (&execpath[n - 4], "/bin")) || - (0 == strcasecmp (&execpath[n - 4], "/lib")))) - { - /* strip '/bin' or '/lib' */ - execpath[n - 4] = '\0'; - n -= 4; - } - multiarch = NULL; - if (NULL != (libdir = strstr (execpath, "/lib/"))) - { - /* test for multi-arch path of the form "PREFIX/lib/MULTIARCH/"; - here we need to re-add 'multiarch' to lib and libexec paths later! */ - multiarch = &libdir[5]; - if (NULL == strchr (multiarch, '/')) - libdir[0] = '\0'; /* Debian multiarch format, cut of from 'execpath' but preserve in multicarch */ - else - multiarch = NULL; /* maybe not, multiarch still has a '/', which is not OK */ - } - /* in case this was a directory named foo-bin, remove "foo-" */ - while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) - execpath[--n] = '\0'; - switch (dirkind) - { - case GNUNET_OS_IPK_PREFIX: - case GNUNET_OS_IPK_SELF_PREFIX: - dirname = DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_BINDIR: - dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_LIBDIR: - if (isbasedir) - { - GNUNET_asprintf (&tmp, - "%s%s%s%s%s", - execpath, - DIR_SEPARATOR_STR "lib", - (NULL != multiarch) ? DIR_SEPARATOR_STR : "", - (NULL != multiarch) ? multiarch : "", - DIR_SEPARATOR_STR "taler" DIR_SEPARATOR_STR); - if (GNUNET_YES == - GNUNET_DISK_directory_test (tmp, GNUNET_YES)) - { - GNUNET_free (execpath); - return tmp; - } - GNUNET_free (tmp); - tmp = NULL; - if (4 == sizeof (void *)) - { - dirname = - DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR "taler" DIR_SEPARATOR_STR; - GNUNET_asprintf (&tmp, - "%s%s", - execpath, - dirname); - } - if (8 == sizeof (void *)) - { - dirname = - DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR "taler" DIR_SEPARATOR_STR; - GNUNET_asprintf (&tmp, - "%s%s", - execpath, - dirname); - } - - if ( (NULL != tmp) && - (GNUNET_YES == - GNUNET_DISK_directory_test (tmp, GNUNET_YES)) ) - { - GNUNET_free (execpath); - return tmp; - } - GNUNET_free (tmp); - } - dirname = DIR_SEPARATOR_STR "taler" DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_DATADIR: - dirname = - DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "taler" DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_LOCALEDIR: - dirname = - DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_ICONDIR: - dirname = - DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_DOCDIR: - dirname = - DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "doc" DIR_SEPARATOR_STR \ - "gnunet" DIR_SEPARATOR_STR; - break; - case GNUNET_OS_IPK_LIBEXECDIR: - if (isbasedir) - { - dirname = - DIR_SEPARATOR_STR "taler" DIR_SEPARATOR_STR "libexec" DIR_SEPARATOR_STR; - GNUNET_asprintf (&tmp, - "%s%s%s%s", - execpath, - DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR, - (NULL != multiarch) ? multiarch : "", - dirname); - if (GNUNET_YES == - GNUNET_DISK_directory_test (tmp, GNUNET_YES)) - { - GNUNET_free (execpath); - return tmp; - } - GNUNET_free (tmp); - tmp = NULL; - if (4 == sizeof (void *)) - { - dirname = - DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR "taler" DIR_SEPARATOR_STR \ - "libexec" DIR_SEPARATOR_STR; - GNUNET_asprintf (&tmp, - "%s%s", - execpath, - dirname); - } - if (8 == sizeof (void *)) - { - dirname = - DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR "taler" DIR_SEPARATOR_STR \ - "libexec" DIR_SEPARATOR_STR; - GNUNET_asprintf (&tmp, - "%s%s", - execpath, - dirname); - } - if ( (NULL != tmp) && - (GNUNET_YES == - GNUNET_DISK_directory_test (tmp, GNUNET_YES)) ) - { - GNUNET_free (execpath); - return tmp; - } - - GNUNET_free (tmp); - } - dirname = - DIR_SEPARATOR_STR "taler" DIR_SEPARATOR_STR \ - "libexec" DIR_SEPARATOR_STR; - break; - default: - GNUNET_free (execpath); - return NULL; - } - GNUNET_asprintf (&tmp, - "%s%s", - execpath, - dirname); - GNUNET_free (execpath); - return tmp; -} - - -/** - * Given the name of a taler-helper, taler-service or taler-daemon - * binary, try to prefix it with the libexec/-directory to get the - * full path. - * - * @param progname name of the binary - * @return full path to the binary, if possible, otherwise copy of 'progname' - */ -char * -TALER_OS_get_libexec_binary_path (const char *progname) -{ - static char *cache; - char *libexecdir; - char *binary; - - if ( (DIR_SEPARATOR == progname[0]) || - (GNUNET_YES == GNUNET_STRINGS_path_is_absolute (progname, GNUNET_NO, NULL, NULL)) ) - return GNUNET_strdup (progname); - if (NULL != cache) - libexecdir = cache; - else - libexecdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBEXECDIR); - if (NULL == libexecdir) - return GNUNET_strdup (progname); - GNUNET_asprintf (&binary, - "%s%s", - libexecdir, - progname); - cache = libexecdir; - return binary; -} - - - /* end of os_installation.c */ diff --git a/src/util/paths.conf b/src/util/paths.conf new file mode 100644 index 000000000..03febb0e4 --- /dev/null +++ b/src/util/paths.conf @@ -0,0 +1,29 @@ +# This file is in the public domain. +# +[PATHS] +# The PATHS section is special, as filenames including $-expression are +# expanded using the values from PATHS or the system environment (PATHS +# is checked first). Taler also supports expanding $-expressions using +# defaults with the syntax "${VAR:-default}". Here, "default" can again +# be a $-expression. +# +# We usually want $HOME for $TALER_HOME, but we allow testcases to +# easily override this by setting $TALER_TEST_HOME. +# +TALER_HOME = ${TALER_TEST_HOME:-${HOME:-${USERPROFILE}}} + +# see XDG Base Directory Specification at +# http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html +# for how these should be used. + +# Persistant data storage +TALER_DATA_HOME = ${XDG_DATA_HOME:-$TALER_HOME/.local/share}/taler/ + +# Configuration files +TALER_CONFIG_HOME = ${XDG_CONFIG_HOME:-$TALER_HOME/.config}/taler/ + +# Cached data, no big deal if lost +TALER_CACHE_HOME = ${XDG_CACHE_HOME:-$TALER_HOME/.cache}/taler/ + +# Runtime data (always lost on system boot) +TALER_RUNTIME_DIR = ${TMPDIR:-${TMP:-/tmp}}/taler-system-runtime/ diff --git a/src/util/plugin.c b/src/util/plugin.c deleted file mode 100644 index 6f8e03df6..000000000 --- a/src/util/plugin.c +++ /dev/null @@ -1,88 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2015 GNUnet e.V. - - TALER 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 3, or (at your option) any later version. - - TALER 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 - TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file util/plugin.c - * @brief Setup paths so that we can load Taler plugins - * @author Christian Grothoff - * @author Sree Harsha Totakura <sreeharsha@totakura.in> - */ -#include "platform.h" -#include "taler_util.h" -#include <ltdl.h> - -/** - * Libtool search path before we started. - */ -static char *old_dlsearchpath; - - -/** - * Setup libtool paths. - */ -void __attribute__ ((constructor)) -plugin_init () -{ - int err; - const char *opath; - char *path; - char *cpath; - - err = lt_dlinit (); - if (err > 0) - { - FPRINTF (stderr, - _("Initialization of plugin mechanism failed: %s!\n"), - lt_dlerror ()); - return; - } - opath = lt_dlgetsearchpath (); - if (NULL != opath) - old_dlsearchpath = GNUNET_strdup (opath); - path = TALER_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); - if (NULL != path) - { - if (NULL != opath) - { - GNUNET_asprintf (&cpath, "%s:%s", opath, path); - lt_dlsetsearchpath (cpath); - GNUNET_free (path); - GNUNET_free (cpath); - } - else - { - lt_dlsetsearchpath (path); - GNUNET_free (path); - } - } -} - - -/** - * Shutdown libtool. - */ -void __attribute__ ((destructor)) -plugin_fini () -{ - lt_dlsetsearchpath (old_dlsearchpath); - if (NULL != old_dlsearchpath) - { - GNUNET_free (old_dlsearchpath); - old_dlsearchpath = NULL; - } - lt_dlexit (); -} - -/* end of plugin.c */ diff --git a/src/util/taler-config.in b/src/util/taler-config.in new file mode 100644 index 000000000..eb4114c82 --- /dev/null +++ b/src/util/taler-config.in @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +if ! type gnunet-config >/dev/null; then + echo "$0 needs gnunet-config to be installed" + exit 1 +fi + +# FIXME: not very portable ... +export LD_PRELOAD=@libdir@/libtalerutil.so +exec gnunet-config "$@" diff --git a/src/util/test_amount.c b/src/util/test_amount.c index 3f33334bc..640477156 100644 --- a/src/util/test_amount.c +++ b/src/util/test_amount.c @@ -73,6 +73,19 @@ main(int argc, GNUNET_assert (4 == a1.value); GNUNET_assert (0 == a1.fraction); + /* test conversion with leading zero in fraction */ + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount ("eur:0.02", + &a2)); + GNUNET_assert (0 == strcasecmp ("eur", + a2.currency)); + GNUNET_assert (0 == a2.value); + GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 2 == a2.fraction); + c = TALER_amount_to_string (&a2); + GNUNET_assert (0 == strcmp ("eur:0.02", + c)); + GNUNET_free (c); + /* test conversion with leading space and with fraction */ GNUNET_assert (GNUNET_OK == TALER_string_to_amount (" eur:4.12", diff --git a/src/util/test_json.c b/src/util/test_json.c deleted file mode 100644 index 5e2f50fe8..000000000 --- a/src/util/test_json.c +++ /dev/null @@ -1,182 +0,0 @@ -/* - This file is part of TALER - (C) 2015 GNUnet e.V. - - TALER 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 3, or (at your option) any later version. - - TALER 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 - TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> -*/ - -/** - * @file util/test_json.c - * @brief Tests for Taler-specific crypto logic - * @author Christian Grothoff <christian@grothoff.org> - */ -#include "platform.h" -#include "taler_util.h" -#include "taler_json_lib.h" - - -/** - * Test amount conversion from/to JSON. - * - * @return 0 on success - */ -static int -test_amount () -{ - json_t *j; - struct TALER_Amount a1; - struct TALER_Amount a2; - - GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:4.3", - &a1)); - j = TALER_json_from_amount (&a1); - GNUNET_assert (NULL != j); - GNUNET_assert (GNUNET_OK == - TALER_json_to_amount (j, - &a2)); - GNUNET_assert (0 == - TALER_amount_cmp (&a1, - &a2)); - json_decref (j); - return 0; -} - - -/** - * Test time conversion from/to JSON. - * - * @return 0 on success - */ -static int -test_time () -{ - json_t *j; - struct GNUNET_TIME_Absolute a1; - struct GNUNET_TIME_Absolute a2; - - a1 = GNUNET_TIME_absolute_get (); - TALER_round_abs_time (&a1); - j = TALER_json_from_abs (a1); - GNUNET_assert (NULL != j); - GNUNET_assert (GNUNET_OK == - TALER_json_to_abs (j, - &a2)); - GNUNET_assert (a1.abs_value_us == - a2.abs_value_us); - json_decref (j); - - a1 = GNUNET_TIME_UNIT_FOREVER_ABS; - j = TALER_json_from_abs (a1); - GNUNET_assert (NULL != j); - GNUNET_assert (GNUNET_OK == - TALER_json_to_abs (j, - &a2)); - GNUNET_assert (a1.abs_value_us == - a2.abs_value_us); - json_decref (j); - return 0; -} - - -/** - * Test raw (binary) conversion from/to JSON. - * - * @return 0 on success - */ -static int -test_raw () -{ - char blob[256]; - char blob2[256]; - unsigned int i; - json_t *j; - - for (i=0;i<=256;i++) - { - memset (blob, i, i); - j = TALER_json_from_data (blob, i); - GNUNET_assert (NULL != j); - GNUNET_assert (GNUNET_OK == - TALER_json_to_data (j, - blob2, - i)); - GNUNET_assert (0 == - memcmp (blob, - blob2, - i)); - } - return 0; -} - - -/** - * Test rsa conversions from/to JSON. - * - * @return 0 on success - */ -static int -test_rsa () -{ - struct GNUNET_CRYPTO_rsa_PublicKey *pub; - struct GNUNET_CRYPTO_rsa_PublicKey *pub2; - struct GNUNET_CRYPTO_rsa_Signature *sig; - struct GNUNET_CRYPTO_rsa_Signature *sig2; - struct GNUNET_CRYPTO_rsa_PrivateKey *priv; - char msg[] = "Hello"; - json_t *jp; - json_t *js; - - priv = GNUNET_CRYPTO_rsa_private_key_create (1024); - pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); - sig = GNUNET_CRYPTO_rsa_sign (priv, - msg, - sizeof (msg)); - GNUNET_assert (NULL != (jp = TALER_json_from_rsa_public_key (pub))); - GNUNET_assert (NULL != (js = TALER_json_from_rsa_signature (sig))); - GNUNET_assert (NULL != (pub2 = TALER_json_to_rsa_public_key (jp))); - GNUNET_assert (NULL != (sig2 = TALER_json_to_rsa_signature (js))); - GNUNET_break (0 == - GNUNET_CRYPTO_rsa_signature_cmp (sig, - sig2)); - GNUNET_break (0 == - GNUNET_CRYPTO_rsa_public_key_cmp (pub, - pub2)); - GNUNET_CRYPTO_rsa_signature_free (sig); - GNUNET_CRYPTO_rsa_signature_free (sig2); - GNUNET_CRYPTO_rsa_private_key_free (priv); - GNUNET_CRYPTO_rsa_public_key_free (pub); - GNUNET_CRYPTO_rsa_public_key_free (pub2); - return 0; -} - - -int -main(int argc, - const char *const argv[]) -{ - GNUNET_log_setup ("test-json", - "WARNING", - NULL); - if (0 != test_amount ()) - return 1; - if (0 != test_time ()) - return 1; - if (0 != test_raw ()) - return 1; - if (0 != test_rsa ()) - return 1; - /* FIXME: test EdDSA signature conversion... */ - return 0; -} - -/* end of test_json.c */ diff --git a/src/util/util.c b/src/util/util.c index addafacf7..d5fa8c05c 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -56,7 +56,7 @@ TALER_b2s (const void *buf, GNUNET_free (tmp); ret[8] = '\0'; return ret; -} +} /** @@ -89,179 +89,4 @@ TALER_config_get_denom (struct GNUNET_CONFIGURATION_Handle *cfg, } -/** - * Round a time value so that it is suitable for transmission - * via JSON encodings. - * - * @param at time to round - * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if - * it was just now rounded - */ -int -TALER_round_abs_time (struct GNUNET_TIME_Absolute *at) -{ - if (at->abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) - return GNUNET_OK; - if (0 == at->abs_value_us % 1000000) - return GNUNET_OK; - at->abs_value_us -= at->abs_value_us % 1000000; - return GNUNET_NO; -} - - -/** - * Round a time value so that it is suitable for transmission - * via JSON encodings. - * - * @param rt time to round - * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if - * it was just now rounded - */ -int -TALER_round_rel_time (struct GNUNET_TIME_Relative *rt) -{ - if (rt->rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) - return GNUNET_OK; - if (0 == rt->rel_value_us % 1000000) - return GNUNET_OK; - rt->rel_value_us -= rt->rel_value_us % 1000000; - return GNUNET_NO; -} - - -/** - * Load configuration by parsing all configuration - * files in the given directory. - * - * @param base_dir directory with the configuration files - * @return NULL on error, otherwise configuration - */ -struct GNUNET_CONFIGURATION_Handle * -TALER_config_load (const char *base_dir) -{ - struct GNUNET_CONFIGURATION_Handle *cfg; - char *cfg_dir; - int res; - - res = GNUNET_asprintf (&cfg_dir, - "%s" DIR_SEPARATOR_STR "config", - base_dir); - GNUNET_assert (res > 0); - cfg = GNUNET_CONFIGURATION_create (); - res = GNUNET_CONFIGURATION_load_from (cfg, cfg_dir); - GNUNET_free (cfg_dir); - if (GNUNET_OK != res) - return NULL; - return cfg; -} - - - -/** - * At what offset does the help text start? - */ -#define BORDER 29 - -/** - * Print out details on command line options (implements --help). - * - * @param ctx command line processing context - * @param scls additional closure (points to about text) - * @param option name of the option - * @param value not used (NULL) - * @return #GNUNET_NO (do not continue, not an error) - */ -int -TALER_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, - void *scls, - const char *option, - const char *value) -{ - const char *about = scls; - size_t slen; - unsigned int i; - int j; - size_t ml; - size_t p; - char *scp; - const char *trans; - const struct GNUNET_GETOPT_CommandLineOption *opt; - - if (NULL != about) - { - printf ("%s\n%s\n", - ctx->binaryOptions, - gettext (about)); - printf (_("Arguments mandatory for long options are also mandatory for short options.\n")); - } - opt = ctx->allOptions; - for (i=0;NULL != opt[i].description;i++) - { - if (opt[i].shortName == '\0') - printf (" "); - else - printf (" -%c, ", opt[i].shortName); - printf ("--%s", opt[i].name); - slen = 8 + strlen (opt[i].name); - if (opt[i].argumentHelp != NULL) - { - printf ("=%s", opt[i].argumentHelp); - slen += 1 + strlen (opt[i].argumentHelp); - } - if (slen > BORDER) - { - printf ("\n%*s", BORDER, ""); - slen = BORDER; - } - if (slen < BORDER) - { - printf ("%*s", (int) (BORDER - slen), ""); - slen = BORDER; - } - if (0 < strlen (opt[i].description)) - trans = gettext (opt[i].description); - else - trans = ""; - ml = strlen (trans); - p = 0; -OUTER: - while (ml - p > 78 - slen) - { - for (j = p + 78 - slen; j > p; j--) - { - if (isspace ((unsigned char) trans[j])) - { - scp = GNUNET_malloc (j - p + 1); - memcpy (scp, &trans[p], j - p); - scp[j - p] = '\0'; - printf ("%s\n%*s", scp, BORDER + 2, ""); - GNUNET_free (scp); - p = j + 1; - slen = BORDER + 2; - goto OUTER; - } - } - /* could not find space to break line */ - scp = GNUNET_malloc (78 - slen + 1); - memcpy (scp, &trans[p], 78 - slen); - scp[78 - slen] = '\0'; - printf ("%s\n%*s", scp, BORDER + 2, ""); - GNUNET_free (scp); - slen = BORDER + 2; - p = p + 78 - slen; - } - /* print rest */ - if (p < ml) - printf ("%s\n", &trans[p]); - if (strlen (trans) == 0) - printf ("\n"); - } - printf ("Report bugs to taler@gnu.org.\n" - "Taler home page: http://www.gnu.org/software/taler/\n" - "General help using GNU software: http://www.gnu.org/gethelp/\n"); - return GNUNET_NO; -} - - - /* end of util.c */ diff --git a/src/util/wireformats.c b/src/util/wireformats.c deleted file mode 100644 index cd5a9c3d4..000000000 --- a/src/util/wireformats.c +++ /dev/null @@ -1,441 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014, 2015 GNUnet e.V. - - TALER 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 3, or (at your option) any later version. - - TALER 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 - TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file util/wireformats.c - * @brief helper functions for JSON processing using libjansson - * @author Sree Harsha Totakura <sreeharsha@totakura.in> - * @author Christian Grothoff - */ -#include "platform.h" -#include <gnunet/gnunet_util_lib.h> -#include "taler_util.h" - -/** - * Shorthand for exit jumps. - */ -#define EXITIF(cond) \ - do { \ - if (cond) { GNUNET_break (0); goto EXITIF_exit; } \ - } while (0) - -/** - * Shorthand for JSON parsing related exit jumps. - */ -#define UNPACK_EXITIF(cond) \ - do { \ - if (cond) { TALER_json_warn (error); goto EXITIF_exit; } \ - } while (0) - - -/* Taken from GNU gettext */ - -/** - * Entry in the country table. - */ -struct table_entry -{ - /** - * 2-Character international country code. - */ - const char *code; - - /** - * Long English name of the country. - */ - const char *english; -}; - -/* Keep the following table in sync with gettext. - WARNING: the entries should stay sorted according to the code */ -/** - * List of country codes. - */ -static const struct table_entry country_table[] = - { - { "AE", "U.A.E." }, - { "AF", "Afghanistan" }, - { "AL", "Albania" }, - { "AM", "Armenia" }, - { "AN", "Netherlands Antilles" }, - { "AR", "Argentina" }, - { "AT", "Austria" }, - { "AU", "Australia" }, - { "AZ", "Azerbaijan" }, - { "BA", "Bosnia and Herzegovina" }, - { "BD", "Bangladesh" }, - { "BE", "Belgium" }, - { "BG", "Bulgaria" }, - { "BH", "Bahrain" }, - { "BN", "Brunei Darussalam" }, - { "BO", "Bolivia" }, - { "BR", "Brazil" }, - { "BT", "Bhutan" }, - { "BY", "Belarus" }, - { "BZ", "Belize" }, - { "CA", "Canada" }, - { "CG", "Congo" }, - { "CH", "Switzerland" }, - { "CI", "Cote d'Ivoire" }, - { "CL", "Chile" }, - { "CM", "Cameroon" }, - { "CN", "People's Republic of China" }, - { "CO", "Colombia" }, - { "CR", "Costa Rica" }, - { "CS", "Serbia and Montenegro" }, - { "CZ", "Czech Republic" }, - { "DE", "Germany" }, - { "DK", "Denmark" }, - { "DO", "Dominican Republic" }, - { "DZ", "Algeria" }, - { "EC", "Ecuador" }, - { "EE", "Estonia" }, - { "EG", "Egypt" }, - { "ER", "Eritrea" }, - { "ES", "Spain" }, - { "ET", "Ethiopia" }, - { "FI", "Finland" }, - { "FO", "Faroe Islands" }, - { "FR", "France" }, - { "GB", "United Kingdom" }, - { "GD", "Caribbean" }, - { "GE", "Georgia" }, - { "GL", "Greenland" }, - { "GR", "Greece" }, - { "GT", "Guatemala" }, - { "HK", "Hong Kong" }, - { "HK", "Hong Kong S.A.R." }, - { "HN", "Honduras" }, - { "HR", "Croatia" }, - { "HT", "Haiti" }, - { "HU", "Hungary" }, - { "ID", "Indonesia" }, - { "IE", "Ireland" }, - { "IL", "Israel" }, - { "IN", "India" }, - { "IQ", "Iraq" }, - { "IR", "Iran" }, - { "IS", "Iceland" }, - { "IT", "Italy" }, - { "JM", "Jamaica" }, - { "JO", "Jordan" }, - { "JP", "Japan" }, - { "KE", "Kenya" }, - { "KG", "Kyrgyzstan" }, - { "KH", "Cambodia" }, - { "KR", "South Korea" }, - { "KW", "Kuwait" }, - { "KZ", "Kazakhstan" }, - { "LA", "Laos" }, - { "LB", "Lebanon" }, - { "LI", "Liechtenstein" }, - { "LK", "Sri Lanka" }, - { "LT", "Lithuania" }, - { "LU", "Luxembourg" }, - { "LV", "Latvia" }, - { "LY", "Libya" }, - { "MA", "Morocco" }, - { "MC", "Principality of Monaco" }, - { "MD", "Moldava" }, - { "MD", "Moldova" }, - { "ME", "Montenegro" }, - { "MK", "Former Yugoslav Republic of Macedonia" }, - { "ML", "Mali" }, - { "MM", "Myanmar" }, - { "MN", "Mongolia" }, - { "MO", "Macau S.A.R." }, - { "MT", "Malta" }, - { "MV", "Maldives" }, - { "MX", "Mexico" }, - { "MY", "Malaysia" }, - { "NG", "Nigeria" }, - { "NI", "Nicaragua" }, - { "NL", "Netherlands" }, - { "NO", "Norway" }, - { "NP", "Nepal" }, - { "NZ", "New Zealand" }, - { "OM", "Oman" }, - { "PA", "Panama" }, - { "PE", "Peru" }, - { "PH", "Philippines" }, - { "PK", "Islamic Republic of Pakistan" }, - { "PL", "Poland" }, - { "PR", "Puerto Rico" }, - { "PT", "Portugal" }, - { "PY", "Paraguay" }, - { "QA", "Qatar" }, - { "RE", "Reunion" }, - { "RO", "Romania" }, - { "RS", "Serbia" }, - { "RU", "Russia" }, - { "RW", "Rwanda" }, - { "SA", "Saudi Arabia" }, - { "SE", "Sweden" }, - { "SG", "Singapore" }, - { "SI", "Slovenia" }, - { "SK", "Slovak" }, - { "SN", "Senegal" }, - { "SO", "Somalia" }, - { "SR", "Suriname" }, - { "SV", "El Salvador" }, - { "SY", "Syria" }, - { "TH", "Thailand" }, - { "TJ", "Tajikistan" }, - { "TM", "Turkmenistan" }, - { "TN", "Tunisia" }, - { "TR", "Turkey" }, - { "TT", "Trinidad and Tobago" }, - { "TW", "Taiwan" }, - { "TZ", "Tanzania" }, - { "UA", "Ukraine" }, - { "US", "United States" }, - { "UY", "Uruguay" }, - { "VA", "Vatican" }, - { "VE", "Venezuela" }, - { "VN", "Viet Nam" }, - { "YE", "Yemen" }, - { "ZA", "South Africa" }, - { "ZW", "Zimbabwe" } - }; - - -/** - * Country code comparator function, for binary search with bsearch(). - * - * @param ptr1 pointer to a `struct table_entry` - * @param ptr2 pointer to a `struct table_entry` - * @return result of strncmp()'ing the 2-digit country codes of the entries - */ -static int -cmp_country_code (const void *ptr1, - const void *ptr2) -{ - const struct table_entry *cc1 = ptr1; - const struct table_entry *cc2 = ptr2; - - return strncmp (cc1->code, cc2->code, 2); -} - - -/** - * Validates given IBAN according to the European Banking Standards. See: - * http://www.europeanpaymentscouncil.eu/documents/ECBS%20IBAN%20standard%20EBS204_V3.2.pdf - * - * @param iban the IBAN number to validate - * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not - */ -static int -validate_iban (const char *iban) -{ - char cc[2]; - char ibancpy[35]; - struct table_entry cc_entry; - unsigned int len; - char *nbuf; - unsigned int i; - unsigned int j; - unsigned long long dividend; - unsigned long long remainder; - int nread; - int ret; - - len = strlen (iban); - if (len > 34) - return GNUNET_NO; - strncpy (cc, iban, 2); - strncpy (ibancpy, iban + 4, len - 4); - strncpy (ibancpy + len - 4, iban, 4); - ibancpy[len] = '\0'; - cc_entry.code = cc; - cc_entry.english = NULL; - if (NULL == - bsearch (&cc_entry, - country_table, - sizeof (country_table) / sizeof (struct table_entry), - sizeof (struct table_entry), - &cmp_country_code)) - return GNUNET_NO; - nbuf = GNUNET_malloc ((len * 2) + 1); - for (i=0, j=0; i < len; i++) - { - if (isalpha ((int) ibancpy[i])) - { - EXITIF(2 != snprintf(&nbuf[j], - 3, - "%2u", - (ibancpy[i] - 'A' + 10))); - j += 2; - continue; - } - nbuf[j] = ibancpy[i]; - j++; - } - for (j=0;'\0' != nbuf[j];j++) - GNUNET_assert (isdigit(nbuf[j])); - GNUNET_assert (sizeof(dividend) >= 8); - remainder = 0; - for (i=0; i<j; i+=16) - { - EXITIF (1 != - (ret = sscanf (&nbuf[i], - "%16llu %n", - ÷nd, - &nread))); - if (0 != remainder) - dividend += remainder * (pow (10, nread)); - remainder = dividend % 97; - } - if (1 == remainder) - { - GNUNET_free (nbuf); - return GNUNET_YES; - } - EXITIF_exit: - GNUNET_free (nbuf); - return GNUNET_NO; -} - - -/** - * Validate SEPA account details. - * - * @param wire JSON with the SEPA details - * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not - */ -static int -validate_sepa (const json_t *wire) -{ - json_error_t error; - const char *type; - const char *iban; - const char *name; - const char *bic; - uint64_t r; - const char *address; - - UNPACK_EXITIF (0 != json_unpack_ex - ((json_t *) wire, - &error, JSON_STRICT, - "{" - "s:s," /* TYPE: sepa */ - "s:s," /* IBAN: iban */ - "s:s," /* name: beneficiary name */ - "s:s," /* BIC: beneficiary bank's BIC */ - "s:i," /* r: random 64-bit integer nounce */ - "s:s" /* address: address of the beneficiary */ - "}", - "type", &type, - "IBAN", &iban, - "name", &name, - "bic", &bic, - "r", &r, - "address", &address)); - if (1 != validate_iban (iban)) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "IBAN `%s' invalid\n", - iban); - return GNUNET_NO; - } - return GNUNET_YES; - EXITIF_exit: - return GNUNET_NO; -} - - -/** - * Validate TEST account details. The "test" format is used - * for testing, so this validator does nothing. - * - * @param wire JSON with the TEST details - * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not - */ -static int -validate_test (const json_t *wire) -{ - return GNUNET_YES; -} - - -/** - * Handler for a wire format. - */ -struct FormatHandler -{ - /** - * Type handled by this format handler. - */ - const char *type; - - /** - * Function to call to evaluate the format. - * - * @param wire the JSON to evaluate - * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not - */ - int (*handler)(const json_t *wire); -}; - - -/** - * Check if the given wire format JSON object is correctly formatted - * - * @param allowed NULL-terminated array of allowed wire format types - * @param wire the JSON wire format object - * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not - */ -int -TALER_json_validate_wireformat (const char **allowed, - const json_t *wire) -{ - static const struct FormatHandler format_handlers[] = { - { "SEPA", &validate_sepa }, - { "TEST", &validate_test }, - { NULL, NULL} - }; - unsigned int i; - const char *stype; - json_error_t error; - - UNPACK_EXITIF (0 != json_unpack_ex - ((json_t *) wire, - &error, 0, - "{s:s}", - "type", &stype)); - for (i=0;NULL != allowed[i];i++) - if (0 == strcasecmp (allowed[i], - stype)) - break; - if (NULL == allowed[i]) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Wireformat `%s' does not match mint's allowed formats\n", - stype); - return GNUNET_NO; - } - - for (i=0;NULL != format_handlers[i].type;i++) - if (0 == strcasecmp (format_handlers[i].type, - stype)) - return format_handlers[i].handler (wire); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Wireformat `%s' not supported\n", - stype); - return GNUNET_NO; - EXITIF_exit: - return GNUNET_NO; -} - -/* end of wireformats.c */ |