From 8a74086b307f70c10e4d7898ea349cf09e00efa0 Mon Sep 17 00:00:00 2001 From: Roker Date: Wed, 18 Jan 2017 16:08:44 +0100 Subject: [PATCH] JSON-18 #comment Add function getGpgEnvironment() that returns an object with 3 optional strings (which each might be null). --- server/Makefile | 1 + server/gpg_environment.cc | 45 +++++++++++++++++++++++++++++++++++++++ server/gpg_environment.hh | 16 ++++++++++++++ server/json-adapter.cc | 4 +++- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 server/gpg_environment.cc create mode 100644 server/gpg_environment.hh diff --git a/server/Makefile b/server/Makefile index 7971aa8..ba02719 100644 --- a/server/Makefile +++ b/server/Makefile @@ -32,6 +32,7 @@ libjson-adapter.a: \ json_spirit/json_spirit_reader.o json_spirit/json_spirit_value.o json_spirit/json_spirit_writer.o \ json-adapter.o registry.o nfc.o json_rpc.o \ function_map.o pep-types.o \ + gpg_environment.o \ security-token.o \ nfc_sets.o base64.o \ nulllogger.o \ diff --git a/server/gpg_environment.cc b/server/gpg_environment.cc new file mode 100644 index 0000000..21985df --- /dev/null +++ b/server/gpg_environment.cc @@ -0,0 +1,45 @@ +#include "gpg_environment.hh" +#include "function_map.hh" + +#include +#include // for get_binary_path() + +GpgEnvironment getGpgEnvironment() +{ + GpgEnvironment ge{}; + + const char* gpg_path = nullptr; + const auto status = get_binary_path( PEP_crypt_OpenPGP, &gpg_path); + if(status == PEP_STATUS_OK && gpg_path) + { + ge.gnupg_path = std::string(gpg_path); + } + + const char* home = std::getenv("GNUPGHOME"); + if(home) + { + ge.gnupg_home = std::string(home); + } + + const char* ai = std::getenv("GPG_AGENT_INFO"); + if(home) + { + ge.gpg_agent_info = std::string(ai); + } + + return ge; +} + + +template<> +js::Value to_json(const GpgEnvironment& ge) +{ + js::Object obj; + obj.emplace_back("gnupg_path", (ge.gnupg_path ? ge.gnupg_path.get() : js::Value{}) ); + obj.emplace_back("gnupg_home", (ge.gnupg_home ? ge.gnupg_home.get() : js::Value{}) ); + obj.emplace_back("gpg_agent_info", (ge.gpg_agent_info ? ge.gpg_agent_info.get() : js::Value{}) ); + return obj; +} + +template<> +js::Value Type2String::get() { return "GpgEnvironment"; } diff --git a/server/gpg_environment.hh b/server/gpg_environment.hh new file mode 100644 index 0000000..58ac177 --- /dev/null +++ b/server/gpg_environment.hh @@ -0,0 +1,16 @@ +#ifndef JSON_GPG_ENVIRONMENT_HH +#define JSON_GPG_ENVIRONMENT_HH + +#include +#include + +struct GpgEnvironment +{ + boost::optional gnupg_path; // filled by pEpEngine's gnu_gpg_path() + boost::optional gnupg_home; // filled by getenv("GNUPGHOME") + boost::optional gpg_agent_info; // filled by getenv("GPG_AGENT_INFO") +}; + +GpgEnvironment getGpgEnvironment(); + +#endif diff --git a/server/json-adapter.cc b/server/json-adapter.cc index 9eeebdc..c6ef2ab 100644 --- a/server/json-adapter.cc +++ b/server/json-adapter.cc @@ -20,6 +20,7 @@ #include "nulllogger.hh" #include "security-token.hh" #include "pep-utils.hh" +#include "gpg_environment.hh" #include #include @@ -205,7 +206,8 @@ const FunctionMap functions = { // my own example function that does something useful. :-) FP( "—— Other ——", new Separator ), FP( "version", new Func( &JsonAdapter::version ) ), - FP( "apiVersion", new Func ( &JsonAdapter::apiVersion ) ), + FP( "apiVersion", new Func ( &JsonAdapter::apiVersion ) ), + FP( "getGpgEnvironment", new Func( &getGpgEnvironment ) ), };