p≡p JSON adapter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
16 KiB

2 years ago
2 years ago
4 years ago
2 years ago
2 years ago
4 years ago
4 years ago
4 years ago
  1. #include <pEp/webserver.hh>
  2. #include "ev_server.hh"
  3. #include "c_string.hh"
  4. #include "prefix-config.hh"
  5. #include "json-adapter.hh"
  6. #include "function_map.hh"
  7. #include "pEp-types.hh"
  8. #include "json_rpc.hh"
  9. #include "pEp-utils.hh"
  10. #include "logger.hh"
  11. #include "server_version.hh"
  12. #include <pEp/message_api.h>
  13. #include <pEp/blacklist.h>
  14. #include <pEp/key_reset.h>
  15. #include <pEp/openpgp_compat.h>
  16. #include <pEp/message_api.h> // for get_binary_path()
  17. #include <pEp/mime.h>
  18. // libpEpAdapter:
  19. #include <pEp/Adapter.hh>
  20. #include <pEp/status_to_string.hh>
  21. #include <pEp/slurp.hh>
  22. #include <boost/filesystem.hpp>
  23. #include "json_spirit/json_spirit_reader.h"
  24. // HACK:
  25. #ifndef ADAPTER_LIBRARY
  26. #include "mini-adapter-impl.hh"
  27. #else // ADAPTER_LIBRARY
  28. void startSync();
  29. void stopSync();
  30. #endif
  31. namespace fs = boost::filesystem;
  32. // compile-time default. might be overwritten in main() or before any ev_server function is called.
  33. fs::path ev_server::path_to_html = fs::path(html_directory);
  34. namespace {
  35. std::string version_as_a_string()
  36. {
  37. std::stringstream ss;
  38. ss << server_version();
  39. return ss.str();
  40. }
  41. std::string getBinaryPath()
  42. {
  43. const char* gpg_path = nullptr;
  44. const auto status = get_binary_path( PEP_crypt_OpenPGP, &gpg_path);
  45. if(status == PEP_STATUS_OK && gpg_path)
  46. {
  47. return std::string(gpg_path);
  48. }
  49. throw std::runtime_error("getBinaryPath returns error: " + ::pEp::status_to_string(status) );
  50. }
  51. // these are the pEp functions that are callable by the client
  52. const FunctionMap functions = {
  53. // from message_api.h
  54. FP( "Message API", new Separator ),
  55. FP( "encrypt_message", new Func<PEP_STATUS, In_Pep_Session, InOut<message*>, In<stringlist_t*>, Out<message*>, In<PEP_enc_format>, In<PEP_encrypt_flags_t>>( &encrypt_message ) ),
  56. FP( "encrypt_message_and_add_priv_key", new Func<PEP_STATUS, In_Pep_Session,
  57. In<message*>, Out<message*>, In<c_string>, In<PEP_enc_format>, In<PEP_encrypt_flags_t>>( &encrypt_message_and_add_priv_key) ),
  58. FP( "encrypt_message_for_self", new Func<PEP_STATUS, In_Pep_Session,
  59. In<pEp_identity*>, In<message*>, In<stringlist_t*>, Out<message*>, In<PEP_enc_format>, In<PEP_encrypt_flags_t>>( &encrypt_message_for_self ) ),
  60. FP( "decrypt_message", new Func<PEP_STATUS, In_Pep_Session, InOut<message*>, Out<message*>, InOutP<stringlist_t*>, Out<PEP_rating>, InOutP<PEP_decrypt_flags_t>>( &decrypt_message ) ),
  61. FP( "get_key_rating_for_user", new Func<PEP_STATUS, In_Pep_Session, In<c_string>, In<c_string>, Out<PEP_rating>>( &get_key_rating_for_user) ),
  62. // from mime.h
  63. FP( "MIME handling API", new Separator),
  64. FP( "mime_encode_message", new Func<PEP_STATUS, In<const message*>, In<bool>, Out<char*>, In<bool, ParamFlag::NoInput>>( &mime_encode_message )),
  65. FP( "mime_decode_message", new Func<PEP_STATUS, In<c_string>, InLength<>, Out<message*>, In<bool*, ParamFlag::NoInput>>( &mime_decode_message )),
  66. // from pEpEngine.h
  67. FP( "pEp Engine Core API", new Separator),
  68. // FP( "log_event", new Func<PEP_STATUS, In_Pep_Session, In<c_string>, In<c_string>, In<c_string>, In<c_string>>( &log_event) ),
  69. FP( "get_trustwords", new Func<PEP_STATUS, In_Pep_Session, In<const pEp_identity*>, In<const pEp_identity*>, In<Language>, Out<char*>, Out<size_t>, In<bool>>( &get_trustwords) ),
  70. FP( "re_evaluate_message_rating", new Func<PEP_STATUS, In_Pep_Session, In<message *>, In<stringlist_t *>, In<PEP_rating>, Out<PEP_rating>>( &re_evaluate_message_rating ) ),
  71. FP( "get_languagelist", new Func<PEP_STATUS, In_Pep_Session, Out<char*>>( &get_languagelist) ),
  72. // FP( "get_phrase" , new Func<PEP_STATUS, In_Pep_Session, In<Language>, In<int>, Out<char*>> ( &get_phrase) ),
  73. // FP( "get_engine_version", new Func<const char*> ( &get_engine_version) ),
  74. FP( "is_pEp_user" , new Func<PEP_STATUS, In_Pep_Session, In<pEp_identity*>, Out<bool>>( &is_pEp_user) ),
  75. FP( "config_passive_mode", new Func<void, In_Pep_Session, In<bool>>( &config_passive_mode) ),
  76. FP( "config_unencrypted_subject", new Func<void, In_Pep_Session, In<bool>>( &config_unencrypted_subject) ),
  77. FP( "Identity Management API", new Separator),
  78. FP( "get_identity" , new Func<PEP_STATUS, In_Pep_Session, In<c_string>, In<c_string>, Out<pEp_identity*>>( &get_identity) ),
  79. FP( "set_identity" , new Func<PEP_STATUS, In_Pep_Session, In<const pEp_identity*>> ( &set_identity) ),
  80. FP( "mark_as_comprimized", new Func<PEP_STATUS, In_Pep_Session, In<c_string>> ( &mark_as_compromized) ),
  81. FP( "identity_rating" , new Func<PEP_STATUS, In_Pep_Session, In<pEp_identity*>, Out<PEP_rating>>( &identity_rating) ),
  82. FP( "outgoing_message_rating", new Func<PEP_STATUS, In_Pep_Session, In<message*>, Out<PEP_rating>>( &outgoing_message_rating) ),
  83. FP( "outgoing_message_rating_preview", new Func<PEP_STATUS, In_Pep_Session, In<message*>, Out<PEP_rating>>( &outgoing_message_rating_preview) ),
  84. FP( "set_identity_flags" , new Func<PEP_STATUS, In_Pep_Session, InOut<pEp_identity*>, In<identity_flags_t>>( &set_identity_flags) ),
  85. FP( "unset_identity_flags" , new Func<PEP_STATUS, In_Pep_Session, InOut<pEp_identity*>, In<identity_flags_t>>( &unset_identity_flags) ),
  86. FP( "Low level Key Management API", new Separator),
  87. FP( "generate_keypair", new Func<PEP_STATUS, In_Pep_Session, InOut<pEp_identity*>> ( &generate_keypair) ),
  88. FP( "delete_keypair", new Func<PEP_STATUS, In_Pep_Session, In<c_string>> ( &delete_keypair) ),
  89. FP( "import_key" , new Func<PEP_STATUS, In_Pep_Session, In<c_string>, In<std::size_t>, Out<identity_list*>> ( &import_key) ),
  90. FP( "export_key" , new Func<PEP_STATUS, In_Pep_Session, In<c_string>, Out<char*>, Out<std::size_t>> ( &export_key) ),
  91. FP( "find_keys" , new Func<PEP_STATUS, In_Pep_Session, In<c_string>, Out<stringlist_t*>> ( &find_keys) ),
  92. FP( "get_trust" , new Func<PEP_STATUS, In_Pep_Session, InOut<pEp_identity*>> ( &get_trust) ),
  93. FP( "own_key_is_listed", new Func<PEP_STATUS, In_Pep_Session, In<c_string>, Out<bool>> ( &own_key_is_listed) ),
  94. FP( "own_identities_retrieve", new Func<PEP_STATUS, In_Pep_Session, Out<identity_list*>>( &own_identities_retrieve ) ),
  95. FP( "set_own_key", new Func<PEP_STATUS, In_Pep_Session, InOut<pEp_identity*>, In<c_string>>( &set_own_key ) ),
  96. FP( "key_reset_identity", new Func<PEP_STATUS, In_Pep_Session, In<pEp_identity*>, In<c_string>>( &key_reset_identity) ),
  97. FP( "key_reset_user", new Func<PEP_STATUS, In_Pep_Session, In<c_string> , In<c_string, ParamFlag::NullOkay>>( &key_reset_user) ),
  98. FP( "key_reset_all_own_keys", new Func<PEP_STATUS, In_Pep_Session>( &key_reset_all_own_keys) ),
  99. FP( "myself" , new Func<PEP_STATUS, In_Pep_Session, InOut<pEp_identity*>> ( &myself) ),
  100. FP( "update_identity", new Func<PEP_STATUS, In_Pep_Session, InOut<pEp_identity*>> ( &update_identity) ),
  101. FP( "trust_personal_key", new Func<PEP_STATUS, In_Pep_Session, In<pEp_identity*>>( &trust_personal_key) ),
  102. FP( "trust_own_key", new Func<PEP_STATUS, In_Pep_Session, In<pEp_identity*>>( &trust_own_key) ),
  103. FP( "key_mistrusted", new Func<PEP_STATUS, In_Pep_Session, In<pEp_identity*>>( &key_mistrusted) ),
  104. FP( "key_reset_trust", new Func<PEP_STATUS, In_Pep_Session, In<pEp_identity*>>( &key_reset_trust) ),
  105. FP( "least_trust" , new Func<PEP_STATUS, In_Pep_Session, In<c_string>, Out<PEP_comm_type>> ( &least_trust) ),
  106. FP( "get_key_rating", new Func<PEP_STATUS, In_Pep_Session, In<c_string>, Out<PEP_comm_type>> ( &get_key_rating) ),
  107. FP( "renew_key" , new Func<PEP_STATUS, In_Pep_Session, In<c_string>, In<const timestamp*>> ( &renew_key) ),
  108. FP( "revoke" , new Func<PEP_STATUS, In_Pep_Session, In<c_string>, In<c_string>> ( &revoke_key) ),
  109. FP( "key_expired" , new Func<PEP_STATUS, In_Pep_Session, In<c_string>, In<time_t>, Out<bool>> ( &key_expired) ),
  110. FP( "from blacklist.h & OpenPGP_compat.h", new Separator),
  111. FP( "blacklist_add" , new Func<PEP_STATUS, In_Pep_Session, In<c_string>> ( &blacklist_add) ),
  112. FP( "blacklist_delete", new Func<PEP_STATUS, In_Pep_Session, In<c_string>> ( &blacklist_delete) ),
  113. FP( "blacklist_is_listed", new Func<PEP_STATUS, In_Pep_Session, In<c_string>, Out<bool>> ( &blacklist_is_listed) ),
  114. FP( "blacklist_retrieve" , new Func<PEP_STATUS, In_Pep_Session, Out<stringlist_t*>> ( &blacklist_retrieve) ),
  115. FP( "OpenPGP_list_keyinfo", new Func<PEP_STATUS, In_Pep_Session, In<c_string>, Out<stringpair_list_t*>> ( &OpenPGP_list_keyinfo) ),
  116. FP( "Event Listener & Results", new Separator ),
  117. FP( "deliverHandshakeResult" , new Func<PEP_STATUS, In_Pep_Session, In<sync_handshake_result>, In<const identity_list*> > (&deliverHandshakeResult) ),
  118. // TODO: session_id shall be removed as soon as we find a way to make it automatic again.
  119. // 'std::this_thread::id'' as ID did not work as expected. :-(
  120. // FP( "pollForEvents" , new Func<js::Array, In<JsonAdapter*,ParamFlag::NoInput>, In<unsigned>> (&JsonAdapter::pollForEvents) ),
  121. FP( "pollForEvents" , new Func<js::Array, In<JsonAdapter*,ParamFlag::NoInput>, In<std::string>, In<unsigned>> (&JsonAdapter::pollForEvents2) ),
  122. FP( "create_session" , new Func<std::string>(&JsonAdapter::create_session)),
  123. FP( "close_session" , new Func<void, In<JsonAdapter*,ParamFlag::NoInput>, In<std::string>> (&JsonAdapter::close_session) ),
  124. FP( "Sync", new Separator ),
  125. FP( "leave_device_group" , new Func<PEP_STATUS, In_Pep_Session> (&leave_device_group) ),
  126. FP( "enable_identity_for_sync" , new Func<PEP_STATUS, In_Pep_Session, InOut<pEp_identity*>> (&enable_identity_for_sync)),
  127. FP( "disable_identity_for_sync", new Func<PEP_STATUS, In_Pep_Session, InOut<pEp_identity*>> (&disable_identity_for_sync)),
  128. #ifndef ADAPTER_LIBRARY
  129. FP( "startSync", new Func<void> (&pEp::mini::startSync) ),
  130. FP( "stopSync" , new Func<void> (&pEp::mini::stopSync) ),
  131. #else
  132. FP( "startSync", new Func<void> (&startSync) ),
  133. FP( "stopSync" , new Func<void> (&stopSync) ),
  134. #endif
  135. // my own example function that does something useful. :-)
  136. FP( "Other", new Separator ),
  137. FP( "serverVersion", new Func<ServerVersion>( &server_version ) ),
  138. FP( "version", new Func<std::string>( &version_as_a_string ) ),
  139. FP( "getBinaryPath", new Func<std::string>( &getBinaryPath ) ),
  140. FP( "testMessageToSend", new Func<PEP_STATUS, In<message*>> (&JsonAdapter::messageToSend) ),
  141. FP( "shutdown", new Func<void, In<JsonAdapter*,ParamFlag::NoInput>>( &JsonAdapter::shutdown_now ) ),
  142. };
  143. bool add_sharks = false;
  144. } // end of anonymous namespace
  145. ev_server::ev_server(const std::string& address, unsigned short port, bool deliver_html, const std::string& base_url)
  146. : pEp::Webserver(pEp::net::ip::address::from_string(address), port)
  147. {
  148. this->add_url_handler(base_url + "callFunction", ev_server::OnApiRequest);
  149. if(deliver_html)
  150. {
  151. this->add_url_handler("/pEp_functions.js", ev_server::OnGetFunctions);
  152. this->add_generic_url_handler(ev_server::OnOtherRequest);
  153. }
  154. }
  155. pEp::Webserver::response ev_server::sendReplyString(const pEp::Webserver::request& req, const char* contentType, std::string&& outputText)
  156. {
  157. Log() << Logger::Debug << "sendReplyString(): "
  158. << ", contentType=" << (contentType ? "«" + std::string(contentType)+ "»" : "NULL")
  159. << ", output.size()=«" << outputText.size() << "»"
  160. << ", keep_alive=" << req.keep_alive() << ".";
  161. DEBUG_LOG(Log()) << "outputText=«" << outputText << "»";
  162. pEp::Webserver::response res{pEp::http::status::ok, req.version()};
  163. res.set(pEp::http::field::content_type, contentType);
  164. res.keep_alive(req.keep_alive());
  165. res.content_length(outputText.size());
  166. res.body() = std::move(outputText);
  167. return res;
  168. }
  169. pEp::Webserver::response ev_server::sendFile(const pEp::Webserver::request& req, const char* mimeType, const fs::path& fileName)
  170. {
  171. // not the best for big files, but this server does not send big files. :-)
  172. std::string fileContent = pEp::slurp(fileName.string());
  173. return sendReplyString(req, mimeType, std::move(fileContent));
  174. }
  175. struct FileRequest
  176. {
  177. const char* mimeType;
  178. fs::path fileName;
  179. };
  180. // catch-all callback
  181. pEp::Webserver::response ev_server::OnOtherRequest(boost::cmatch match, const pEp::Webserver::request& req)
  182. {
  183. static const std::map<std::string, FileRequest > files =
  184. {
  185. { "/" , {"text/html" , path_to_html / "index.html" } },
  186. { "/jquery.js" , {"text/javascript", path_to_html / "jquery-2.2.0.min.js" } },
  187. { "/interactive.js" , {"text/javascript", path_to_html / "interactive.js" } },
  188. { "/favicon.ico" , {"image/vnd.microsoft.icon", path_to_html / "json-test.ico"} },
  189. };
  190. const std::string path = req.target().to_string(); // NB: is percent-encoded! does not relevant for the supported paths above.
  191. Log() << Logger::Debug << "** Request: [" << req.method_string().to_string() << "] " << "Path: [" + path + "]";
  192. try{
  193. const auto q = files.find(path);
  194. if(q != files.end()) // found in "files" map
  195. {
  196. Log() << Logger::Debug << "\t found file \"" << q->second.fileName.string() << "\", type=" << q->second.mimeType << ".\n";
  197. return sendFile( req, q->second.mimeType, q->second.fileName);
  198. }
  199. return pEp::Webserver::create_status_response(req, pEp::http::status::not_found);
  200. }
  201. catch(const std::runtime_error& e)
  202. {
  203. const std::string error_msg = "Internal error caused by path \"" + path + "\"";
  204. // Log e.what() to log file, but DON'T send it in HTTP error message
  205. // because it might contain sensitive information, e.g. local file paths etc.!
  206. Log() << Logger::Error << "OnOtherRequest: " << error_msg << ". what:" << e.what();
  207. return pEp::Webserver::create_status_response(req, pEp::http::status::internal_server_error );
  208. }
  209. };
  210. // generate a JavaScript file containing the definition of all registered callable functions, see above.
  211. pEp::Webserver::response ev_server::OnGetFunctions(boost::cmatch match, const pEp::Webserver::request& req)
  212. {
  213. static const auto& version = server_version();
  214. static const std::string preamble =
  215. "var Direction = { In:1, Out:2, InOut:3 };\n"
  216. "var Type = {\n"
  217. " Blob : 10, // binary strings or 'array of octet'\n"
  218. " String : 20, // human-readable, NUL-terminated utf8-encoded strings\n"
  219. " StringList: 25,\n"
  220. " Message : 30,\n"
  221. " PEP_color : 50,\n"
  222. " PEP_enc_format: 51,\n"
  223. " PEP_STATUS: 59,\n"
  224. " Uint16 : 80,\n"
  225. " Session : 90 // opaque type. only a special encoded 'handle' is used in JavaScript code\n"
  226. " };\n"
  227. "\n"
  228. "var server_version_name = \"" + version.name + "\";\n"
  229. "var server_version = \"" + version.major_minor_patch() + "\";\n"
  230. "var add_sharks = " + (add_sharks?"true":"false") + ";\n"
  231. "var pEp_functions = ";
  232. js::Array jsonfunctions;
  233. for(const auto& f : functions)
  234. {
  235. js::Object o;
  236. o.emplace_back( "name", f.first );
  237. f.second->setJavaScriptSignature( o );
  238. jsonfunctions.push_back( o );
  239. }
  240. std::string output = preamble + js::write( jsonfunctions, js::pretty_print | js::raw_utf8 | js::single_line_arrays )
  241. + ";\n"
  242. "\n"
  243. "// End of generated file.\n";
  244. return sendReplyString(req, "text/javascript", std::move(output));
  245. }
  246. pEp::Webserver::response ev_server::OnApiRequest(boost::cmatch match, const pEp::Webserver::request& req)
  247. {
  248. Logger L( Log(), "OnApiReq");
  249. int request_id = -42;
  250. js::Object answer;
  251. js::Value p;
  252. try
  253. {
  254. JsonAdapter& ja = JsonAdapter::getInstance();
  255. const std::string data_string = req.body();
  256. if(!data_string.empty())
  257. {
  258. #ifdef DEBUG_ENABLED
  259. L << Logger::Debug << "Data: «" << data_string << "» (" << data_string.size() << " bytes).";
  260. #else
  261. L << Logger::Debug << "Data.size=" << data_string.size() << ".";
  262. #endif
  263. bool b = js::read( data_string, p);
  264. if(p.type() == js::obj_type)
  265. {
  266. const js::Object& request = p.get_obj();
  267. answer = call( functions, request, &ja );
  268. }else{
  269. const std::string error_msg = "request body is not a JSON object. js::read() returned" + std::to_string(b);
  270. L << Logger::Error << error_msg;
  271. answer = make_error( JSON_RPC::PARSE_ERROR, error_msg, js::Value{data_string}, 42 );
  272. }
  273. }else{
  274. answer = make_error( JSON_RPC::INTERNAL_ERROR, "ZERO size request", p, request_id );
  275. }
  276. }
  277. catch(const std::exception& e)
  278. {
  279. L << Logger::Error << "Exception: \"" << e.what() << "\"";
  280. answer = make_error( JSON_RPC::INTERNAL_ERROR, "Got a std::exception: \"" + std::string(e.what()) + "\"", p, request_id );
  281. }
  282. return sendReplyString(req, "text/plain", js::write(answer, js::raw_utf8));
  283. };
  284. Logger& ev_server::Log()
  285. {
  286. static Logger L("evs");
  287. return L;
  288. }
  289. void ev_server::addSharks()
  290. {
  291. add_sharks = true;
  292. }
  293. void ev_server::thread_init()
  294. {
  295. // nothing to do, yet.
  296. }
  297. void ev_server::thread_done()
  298. {
  299. JsonAdapter::getInstance().connection_close_cb();
  300. pEp::Adapter::session(pEp::Adapter::release);
  301. }