diff options
-rw-r--r-- | Dockerfile | 4 | ||||
-rw-r--r-- | README.md | 23 | ||||
-rw-r--r-- | contrib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | contrib/epee/CMakeLists.txt | 30 | ||||
-rw-r--r-- | contrib/epee/include/net/abstract_tcp_server2.inl | 7 | ||||
-rw-r--r-- | contrib/epee/include/net/http_auth.h | 81 | ||||
-rw-r--r-- | contrib/epee/include/net/http_protocol_handler.h | 15 | ||||
-rw-r--r-- | contrib/epee/include/net/http_server_impl_base.h | 6 | ||||
-rw-r--r-- | contrib/epee/src/CMakeLists.txt | 29 | ||||
-rw-r--r-- | contrib/epee/src/http_auth.cpp | 527 | ||||
-rw-r--r-- | src/ringct/rctTypes.h | 6 | ||||
-rw-r--r-- | src/rpc/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.cpp | 40 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.h | 1 | ||||
-rw-r--r-- | src/wallet/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/wallet/api/address_book.cpp | 2 | ||||
-rw-r--r-- | src/wallet/api/address_book.h | 2 | ||||
-rw-r--r-- | src/wallet/api/wallet.cpp | 67 | ||||
-rw-r--r-- | src/wallet/api/wallet.h | 11 | ||||
-rw-r--r-- | src/wallet/wallet2.cpp | 4 | ||||
-rw-r--r-- | src/wallet/wallet2.h | 2 | ||||
-rw-r--r-- | src/wallet/wallet2_api.h | 17 | ||||
-rw-r--r-- | tests/unit_tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/unit_tests/http_auth.cpp | 471 |
24 files changed, 1297 insertions, 54 deletions
diff --git a/Dockerfile b/Dockerfile index 158832f4a..8a40e9533 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,7 +17,7 @@ WORKDIR /src RUN set -e && \ apt-get update -q && \ apt-get install -q -y --no-install-recommends build-essential ca-certificates g++ gcc cmake \ - pkg-config libunbound2 libevent-2.0-5 libgtest-dev libboost-all-dev libdb5.3++-dev libdb5.3-dev libssl-dev && \ + pkg-config libunbound2 libevent-2.0-5 libgtest-dev libboost-all-dev libdb5.3++-dev libdb5.3-dev libssl1.0-dev && \ make -j 4 && \ apt-get purge -y g++ gcc cmake pkg-config && \ apt-get clean -q -y && \ @@ -46,4 +46,4 @@ ENV RPC_BIND_PORT 18081 EXPOSE 18080 EXPOSE 18081 -CMD ./monerod --log-level=$LOG_LEVEL --p2p-bind-ip=$P2P_BIND_IP --p2p-bind-port=$P2P_BIND_PORT --rpc-bind-ip=$RPC_BIND_IP --rpc-bind-port=$RPC_BIND_PORT
\ No newline at end of file +CMD ./monerod --log-level=$LOG_LEVEL --p2p-bind-ip=$P2P_BIND_IP --p2p-bind-port=$P2P_BIND_PORT --rpc-bind-ip=$RPC_BIND_IP --rpc-bind-port=$RPC_BIND_PORT @@ -81,7 +81,7 @@ See [LICENSE](LICENSE). Monero uses a hardforking mechanism to implement new features which requires that users of Monero software run current versions and update their software on a -regular schedule. Here is the current schedule, versions, and compatability. +regular schedule. Here is the current schedule, versions, and compatibility. Dates are provided in the format YYYYMMDD. @@ -132,17 +132,18 @@ library archives (`.a`). | GCC | 4.7.3 | NO | `build-essential` | `base-devel` | NO | | | CMake | 3.0.0 | NO | `cmake` | `cmake` | NO | | | pkg-config | any | NO | `pkg-config` | `base-devel` | NO | | -| Boost | 1.58 | NO | `libboost-all-dev` | `boost` | NO | | -| BerkeleyDB | 4.8 | NO | `libdb{,++}-dev` | `db` | NO | | -| libevent | 2.0 | NO | `libevent-dev` | `libevent` | NO | | -| libunbound | 1.4.16 | YES | `libunbound-dev` | `unbound` | NO | | +| Boost | 1.58 | NO | `libboost-all-dev` | `boost` | NO | C++ libraries | +| libevent | 2.0 | NO | `libevent-dev` | `libevent` | NO | Network IO | +| libunbound | 1.4.16 | YES | `libunbound-dev` | `unbound` | NO | DNS resolver | | libminiupnpc | 2.0 | YES | `libminiupnpc-dev` | `miniupnpc` | YES | NAT punching | -| libunwind | any | NO | `libunwind8-dev` | `libunwind` | YES | stack traces | -| ldns | 1.6.17 | NO | `libldns-dev` | `ldns` | YES | ? | -| expat | 1.1 | NO | `libexpat1-dev` | `expat` | YES | ? | -| GTest | 1.5 | YES | `libgtest-dev`^ | `gtest` | YES | test suite | -| Doxygen | any | NO | `doxygen` | `doxygen` | YES | documentation | -| Graphviz | any | NO | `graphviz` | `graphviz` | YES | documentation | +| libunwind | any | NO | `libunwind8-dev` | `libunwind` | YES | Stack traces | +| liblzma | any | NO | `liblzma-dev` | `xz` | YES | For libunwind | +| ldns | 1.6.17 | NO | `libldns-dev` | `ldns` | YES | SSL toolkit | +| expat | 1.1 | NO | `libexpat1-dev` | `expat` | YES | XML parsing | +| GTest | 1.5 | YES | `libgtest-dev`^ | `gtest` | YES | Test suite | +| BerkeleyDB | 4.8 | NO | `libdb{,++}-dev` | `db` | YES | Historic DB | +| Doxygen | any | NO | `doxygen` | `doxygen` | YES | Documentation | +| Graphviz | any | NO | `graphviz` | `graphviz` | YES | Documentation | [^] On Debian/Ubuntu `libgtest-dev` only includes sources and headers. You must build the library binary manually. This can be done with the following command ```sudo apt-get install libgtest-dev && cd /usr/src/gtest && sudo cmake . && sudo make && sudo mv libg* /usr/lib/ ``` diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt index c6e594e5a..92b44e0db 100644 --- a/contrib/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -26,5 +26,6 @@ # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +add_subdirectory(epee) add_subdirectory(otshell_utils) diff --git a/contrib/epee/CMakeLists.txt b/contrib/epee/CMakeLists.txt new file mode 100644 index 000000000..d466b87e0 --- /dev/null +++ b/contrib/epee/CMakeLists.txt @@ -0,0 +1,30 @@ +# Copyright (c) 2014-2016, The Monero Project +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are +# permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other +# materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be +# used to endorse or promote products derived from this software without specific +# prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +add_subdirectory(src) + diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl index 3dca30006..834b5a7a6 100644 --- a/contrib/epee/include/net/abstract_tcp_server2.inl +++ b/contrib/epee/include/net/abstract_tcp_server2.inl @@ -683,7 +683,7 @@ PRAGMA_WARNING_DISABLE_VS(4355) m_sock_count(0), m_sock_number(0), m_threads_count(0), m_pfilter(NULL), m_thread_index(0), m_connection_type( connection_type ), - new_connection_(new connection<t_protocol_handler>(io_service_, m_config, m_sock_count, m_sock_number, m_pfilter, m_connection_type)) + new_connection_() { create_server_type_map(); m_thread_name_prefix = "NET"; @@ -697,7 +697,7 @@ PRAGMA_WARNING_DISABLE_VS(4355) m_sock_count(0), m_sock_number(0), m_threads_count(0), m_pfilter(NULL), m_thread_index(0), m_connection_type(connection_type), - new_connection_(new connection<t_protocol_handler>(io_service_, m_config, m_sock_count, m_sock_number, m_pfilter, connection_type)) + new_connection_() { create_server_type_map(); m_thread_name_prefix = "NET"; @@ -736,6 +736,7 @@ PRAGMA_WARNING_DISABLE_VS(4355) boost::asio::ip::tcp::endpoint binded_endpoint = acceptor_.local_endpoint(); m_port = binded_endpoint.port(); _fact_c("net/RPClog", "start accept"); + new_connection_.reset(new connection<t_protocol_handler>(io_service_, m_config, m_sock_count, m_sock_number, m_pfilter, m_connection_type)); acceptor_.async_accept(new_connection_->socket(), boost::bind(&boosted_tcp_server<t_protocol_handler>::handle_accept, this, boost::asio::placeholders::error)); @@ -1051,7 +1052,7 @@ POP_WARNINGS } else { - _erro("[sock " << new_connection_->socket().native_handle() << "] Failed to start connection, connections_count = " << m_sock_count); + _erro("[sock " << new_connection_l->socket().native_handle() << "] Failed to start connection, connections_count = " << m_sock_count); } new_connection_l->save_dbg_log(); diff --git a/contrib/epee/include/net/http_auth.h b/contrib/epee/include/net/http_auth.h new file mode 100644 index 000000000..1931b6115 --- /dev/null +++ b/contrib/epee/include/net/http_auth.h @@ -0,0 +1,81 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#pragma once + +#include <boost/optional/optional.hpp> +#include <cstdint> +#include "http_base.h" +#include <string> +#include <utility> + +namespace epee +{ +namespace net_utils +{ + namespace http + { + //! Implements RFC 2617 digest auth. Digests from RFC 7616 can be added. + class http_auth + { + public: + struct login + { + login() = delete; + std::string username; + std::string password; + }; + + struct session + { + session() = delete; + const login credentials; + std::string nonce; + std::uint32_t counter; + }; + + http_auth() : user() {} + http_auth(login credentials); + + //! \return Auth response, or `boost::none` iff `request` had valid auth. + boost::optional<http_response_info> get_response(const http_request_info& request) + { + if (user) + { + return process(request); + } + return boost::none; + } + + private: + boost::optional<http_response_info> process(const http_request_info& request); + + boost::optional<session> user; + }; + } +} +} diff --git a/contrib/epee/include/net/http_protocol_handler.h b/contrib/epee/include/net/http_protocol_handler.h index 40e3392db..3813f9d7c 100644 --- a/contrib/epee/include/net/http_protocol_handler.h +++ b/contrib/epee/include/net/http_protocol_handler.h @@ -30,9 +30,11 @@ #ifndef _HTTP_SERVER_H_ #define _HTTP_SERVER_H_ +#include <boost/optional/optional.hpp> #include <string> #include "net_utils_base.h" #include "to_nonconst_iterator.h" +#include "http_auth.h" #include "http_base.h" namespace epee @@ -50,6 +52,7 @@ namespace net_utils { std::string m_folder; std::string m_required_user_agent; + boost::optional<http_auth::login> m_user; critical_section m_lock; }; @@ -169,11 +172,20 @@ namespace net_utils http_custom_handler(i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context) : simple_http_connection_handler<t_connection_context>(psnd_hndlr, config), m_config(config), - m_conn_context(conn_context) + m_conn_context(conn_context), + m_auth(m_config.m_user ? http_auth{*m_config.m_user} : http_auth{}) {} inline bool handle_request(const http_request_info& query_info, http_response_info& response) { CHECK_AND_ASSERT_MES(m_config.m_phandler, false, "m_config.m_phandler is NULL!!!!"); + + const auto auth_response = m_auth.get_response(query_info); + if (auth_response) + { + response = std::move(*auth_response); + return true; + } + //fill with default values response.m_mime_tipe = "text/plain"; response.m_response_code = 200; @@ -202,6 +214,7 @@ namespace net_utils //simple_http_connection_handler::config_type m_stub_config; config_type& m_config; t_connection_context& m_conn_context; + http_auth m_auth; }; } } diff --git a/contrib/epee/include/net/http_server_impl_base.h b/contrib/epee/include/net/http_server_impl_base.h index 65fe5eed6..f6b2d6941 100644 --- a/contrib/epee/include/net/http_server_impl_base.h +++ b/contrib/epee/include/net/http_server_impl_base.h @@ -52,7 +52,8 @@ namespace epee : m_net_server(external_io_service) {} - bool init(const std::string& bind_port = "0", const std::string& bind_ip = "0.0.0.0", const std::string &user_agent = "") + bool init(const std::string& bind_port = "0", const std::string& bind_ip = "0.0.0.0", + std::string user_agent = "", boost::optional<net_utils::http::http_auth::login> user = boost::none) { //set self as callback handler @@ -62,7 +63,8 @@ namespace epee m_net_server.get_config_object().m_folder = ""; // workaround till we get auth/encryption - m_net_server.get_config_object().m_required_user_agent = user_agent; + m_net_server.get_config_object().m_required_user_agent = std::move(user_agent); + m_net_server.get_config_object().m_user = std::move(user); LOG_PRINT_L0("Binding on " << bind_ip << ":" << bind_port); bool res = m_net_server.init_server(bind_port, bind_ip); diff --git a/contrib/epee/src/CMakeLists.txt b/contrib/epee/src/CMakeLists.txt new file mode 100644 index 000000000..8426cd45d --- /dev/null +++ b/contrib/epee/src/CMakeLists.txt @@ -0,0 +1,29 @@ +# Copyright (c) 2014-2016, The Monero Project +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are +# permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other +# materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be +# used to endorse or promote products derived from this software without specific +# prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +add_library(epee STATIC http_auth.cpp) diff --git a/contrib/epee/src/http_auth.cpp b/contrib/epee/src/http_auth.cpp new file mode 100644 index 000000000..98870a7a9 --- /dev/null +++ b/contrib/epee/src/http_auth.cpp @@ -0,0 +1,527 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include "net/http_auth.h" + +#include <array> +#include <boost/algorithm/string/predicate.hpp> +#include <boost/fusion/adapted/std_tuple.hpp> +#include <boost/fusion/algorithm/iteration/for_each.hpp> +#include <boost/fusion/algorithm/query/any.hpp> +#include <boost/range/algorithm/copy.hpp> +#include <boost/range/algorithm/find_if.hpp> +#include <boost/range/iterator_range_core.hpp> +#include <boost/range/join.hpp> +#include <boost/spirit/include/qi_alternative.hpp> +#include <boost/spirit/include/qi_and_predicate.hpp> +#include <boost/spirit/include/qi_char.hpp> +#include <boost/spirit/include/qi_char_class.hpp> +#include <boost/spirit/include/qi_difference.hpp> +#include <boost/spirit/include/qi_kleene.hpp> +#include <boost/spirit/include/qi_optional.hpp> +#include <boost/spirit/include/qi_parse.hpp> +#include <boost/spirit/include/qi_plus.hpp> +#include <boost/spirit/include/qi_no_case.hpp> +#include <boost/spirit/include/qi_raw.hpp> +#include <boost/spirit/include/qi_rule.hpp> +#include <boost/spirit/include/qi_sequence.hpp> +#include <boost/spirit/include/qi_string.hpp> +#include <boost/spirit/include/qi_symbols.hpp> +#include <boost/spirit/include/qi_uint.hpp> +#include <boost/utility/string_ref.hpp> +#include <cassert> +#include <functional> +#include <iterator> +#include <tuple> +#include <type_traits> + +#include "crypto/crypto.h" +#include "md5_l.h" +#include "string_coding.h" + +/* This file uses the `u8` prefix and specifies all chars by ASCII numeric +value. This is for maximum portability - C++ does not actually specify ASCII +as the encoding type for unprefixed string literals, etc. Although rare, the +effort required to support rare compiler encoding types is low. + +Also be careful of qi::ascii character classes (`qi::asci::alpha`, etc.) - +non-ASCII characters will cause undefined behavior in the table lookup until +boost 1.60. The expression `&qi::ascii::char_` will fail on non-ASCII +characters without "consuming" the input character. */ + +namespace +{ + // string_ref is only constexpr if length is given + template<std::size_t N> + constexpr boost::string_ref ceref(const char (&arg)[N]) + { + return boost::string_ref(arg, N - 1); + } + + constexpr const auto auth_realm = ceref(u8"monero-wallet-rpc"); + constexpr const char comma = 44; + constexpr const char equal_sign = 61; + constexpr const char quote = 34; + constexpr const auto sess_algo = ceref(u8"-sess"); + + //// Digest Algorithms + + template<std::size_t N> + std::array<char, N * 2> to_hex(const std::array<std::uint8_t, N>& digest) noexcept + { + static constexpr const char alphabet[] = u8"0123456789abcdef"; + static_assert(sizeof(alphabet) == 17, "bad alphabet size"); + + // TODO upgrade (improve performance) of to hex in epee string tools + std::array<char, N * 2> out{{}}; + auto current = out.begin(); + for (const std::uint8_t byte : digest) + { + *current = alphabet[byte >> 4]; + ++current; + *current = alphabet[byte & 0x0F]; + ++current; + } + return out; + } + + struct md5_ + { + static constexpr const boost::string_ref name = ceref(u8"MD5"); + + struct update + { + template<typename T> + void operator()(const T& arg) const + { + const boost::iterator_range<const char*> data(boost::as_literal(arg)); + md5::MD5Update( + std::addressof(ctx), + reinterpret_cast<const std::uint8_t*>(data.begin()), + data.size() + ); + } + void operator()(const std::string& arg) const + { + (*this)(boost::string_ref(arg)); + } + + md5::MD5_CTX& ctx; + }; + + template<typename... T> + std::array<char, 32> operator()(const T&... args) const + { + md5::MD5_CTX ctx{}; + md5::MD5Init(std::addressof(ctx)); + boost::fusion::for_each(std::tie(args...), update{ctx}); + + std::array<std::uint8_t, 16> digest{{}}; + md5::MD5Final(digest.data(), std::addressof(ctx)); + return to_hex(digest); + } + }; + constexpr const boost::string_ref md5_::name; + + //! Digest Algorithms available for HTTP Digest Auth. + constexpr const std::tuple<md5_> digest_algorithms{}; + + //// Various String Parsing Utilities + + struct ascii_tolower_ + { + template<typename Char> + Char operator()(Char value) const noexcept + { + static_assert(std::is_integral<Char>::value, "only integral types supported"); + return (65 <= value && value <= 90) ? (value + 32) : value; + } + }; + constexpr const ascii_tolower_ ascii_tolower{}; + + struct ascii_iequal_ + { + template<typename Char> + bool operator()(Char left, Char right) const noexcept + { + return ascii_tolower(left) == ascii_tolower(right); + } + }; + constexpr const ascii_iequal_ ascii_iequal{}; + + //// Digest Authentication + + struct auth_request + { + using iterator = const char*; + enum status{ kFail = 0, kStale, kPass }; + + static status verify(const std::string& method, const std::string& request, + const epee::net_utils::http::http_auth::session& user) + { + struct parser + { + using field_parser = std::function<bool(const parser&, iterator&, iterator, auth_request&)>; + + explicit parser() : field_table(), skip_whitespace(), header(), token(), fields() { + using namespace std::placeholders; + namespace qi = boost::spirit::qi; + + struct parse_nc + { + bool operator()(const parser&, iterator& current, const iterator end, auth_request& result) const + { + return qi::parse( + current, end, + (qi::raw[qi::uint_parser<std::uint32_t, 16, 8, 8>{}]), + result.nc + ); + } + }; + struct parse_token + { + bool operator()(const parser& parse, iterator& current, const iterator end, + boost::iterator_range<iterator>& result) const + { + return qi::parse(current, end, parse.token, result); + } + }; + struct parse_string + { + bool operator()(const parser&, iterator& current, const iterator end, + boost::iterator_range<iterator>& result) const + { + return qi::parse( + current, end, + (qi::lit(quote) >> qi::raw[+(u8"\\\"" | (qi::ascii::char_ - quote))] >> qi::lit(quote)), + result + ); + } + }; + struct parse_response + { + bool operator()(const parser&, iterator& current, const iterator end, auth_request& result) const + { + using byte = qi::uint_parser<std::uint8_t, 16, 2, 2>; + return qi::parse( + current, end, + (qi::lit(quote) >> qi::raw[+(byte{})] >> qi::lit(quote)), + result.response + ); + } + }; + + field_table.add + (u8"algorithm", std::bind(parse_token{}, _1, _2, _3, std::bind(&auth_request::algorithm, _4))) + (u8"cnonce", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_request::cnonce, _4))) + (u8"nc", parse_nc{}) + (u8"nonce", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_request::nonce, _4))) + (u8"qop", std::bind(parse_token{}, _1, _2, _3, std::bind(&auth_request::qop, _4))) + (u8"realm", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_request::realm, _4))) + (u8"response", parse_response{}) + (u8"uri", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_request::uri, _4))) + (u8"username", std::bind(parse_string{}, _1, _2, _3, std::bind(&auth_request::username, _4))); + + skip_whitespace = *(&qi::ascii::char_ >> qi::ascii::space); + header = skip_whitespace >> qi::ascii::no_case[u8"digest"] >> skip_whitespace; + token = + -qi::lit(quote) >> + qi::raw[+(&qi::ascii::char_ >> (qi::ascii::graph - qi::ascii::char_(u8"()<>@,;:\\\"/[]?={}")))] >> + -qi::lit(quote); + fields = field_table >> skip_whitespace >> equal_sign >> skip_whitespace; + } + + boost::optional<auth_request> operator()(const std::string& method, const std::string& request) const + { + namespace qi = boost::spirit::qi; + + iterator current = request.data(); + const iterator end = current + request.size(); + + if (!qi::parse(current, end, header)) + { + return boost::none; + } + + auth_request info(method); + field_parser null_parser{}; + std::reference_wrapper<const field_parser> field = null_parser; + + do // require at least one field; require field after `,` character + { + if (!qi::parse(current, end, fields, field) || !field(*this, current, end, info)) + { + return boost::none; + } + qi::parse(current, end, skip_whitespace); + } while (qi::parse(current, end, qi::char_(comma) >> skip_whitespace)); + return boost::make_optional(current == end, info); + } + + private: + boost::spirit::qi::symbols< + char, field_parser, boost::spirit::qi::tst<char, field_parser>, ascii_tolower_ + > field_table; + boost::spirit::qi::rule<iterator> skip_whitespace; + boost::spirit::qi::rule<iterator> header; + boost::spirit::qi::rule<iterator, boost::iterator_range<iterator>()> token; + boost::spirit::qi::rule<iterator, std::reference_wrapper<const field_parser>()> fields; + }; // parser + + static const parser parse; + return do_verify(parse(method, request), user); + } + + private: + explicit auth_request(const std::string& method_) + : algorithm() + , cnonce() + , method(method_) + , nc() + , nonce() + , qop() + , realm() + , response() + , uri() + , username() { + } + + struct has_valid_response + { + template<typename Digest, typename Result> + Result generate_old_response(Digest digest, const Result& key, const Result& auth) const + { + return digest(key, u8":", request.nonce, u8":", auth); + } + + template<typename Digest, typename Result> + Result generate_new_response(Digest digest, const Result& key, const Result& auth) const + { + return digest( + key, u8":", request.nonce, u8":", request.nc, u8":", request.cnonce, u8":", request.qop, u8":", auth + ); + } + + template<typename Digest> + typename std::result_of<Digest()>::type generate_auth(Digest digest) const + { + return digest(request.method, u8":", request.uri); + } + + template<typename Result> + bool check(const Result& result) const + { + return boost::equals(request.response, result, ascii_iequal); + } + + template<typename Digest> + bool operator()(const Digest& digest) const + { + if (boost::starts_with(request.algorithm, Digest::name, ascii_iequal) || + (request.algorithm.empty() && std::is_same<md5_, Digest>::value)) + { + auto key = digest(user.credentials.username, u8":", auth_realm, u8":", user.credentials.password); + + if (boost::ends_with(request.algorithm, sess_algo, ascii_iequal)) + { + key = digest(key, u8":", request.nonce, u8":", request.cnonce); + } + + if (request.qop.empty()) + { + return check(generate_old_response(digest, std::move(key), generate_auth(digest))); + } + else if (boost::equals(ceref(u8"auth"), request.qop, ascii_iequal)) + { + return check(generate_new_response(digest, std::move(key), generate_auth(digest))); + } + } + return false; + } + + const auth_request& request; + const epee::net_utils::http::http_auth::session& user; + }; + + static status do_verify(const boost::optional<auth_request>& request, + const epee::net_utils::http::http_auth::session& user) + { + if (request && + boost::equals(request->username, user.credentials.username) && + boost::fusion::any(digest_algorithms, has_valid_response{*request, user})) + { + if (boost::equals(request->nonce, user.nonce)) + { + // RFC 2069 format does not verify nc value - allow just once + if (user.counter == 1 || (!request->qop.empty() && request->counter() == user.counter)) + { + return kPass; + } + } + return kStale; + } + return kFail; + } + + boost::optional<std::uint32_t> counter() const + { + namespace qi = boost::spirit::qi; + using hex = qi::uint_parser<std::uint32_t, 16>; + std::uint32_t value = 0; + const bool converted = qi::parse(nc.begin(), nc.end(), hex{}, value); + return boost::make_optional(converted, value); + } + + boost::iterator_range<iterator> algorithm; + boost::iterator_range<iterator> cnonce; + boost::string_ref method; + boost::iterator_range<iterator> nc; + boost::iterator_range<iterator> nonce; + boost::iterator_range<iterator> qop; + boost::iterator_range<iterator> realm; + boost::iterator_range<iterator> response; + boost::iterator_range<iterator> uri; + boost::iterator_range<iterator> username; + }; // auth_request + + struct add_challenge + { + template<typename T> + static void add_field(std::string& str, const char* const name, const T& value) + { + str.push_back(comma); + str.append(name); + str.push_back(equal_sign); + boost::range::copy(value, std::back_inserter(str)); + } + + template<typename T> + using quoted_result = boost::joined_range< + const boost::joined_range<const boost::string_ref, const T>, const boost::string_ref + >; + + template<typename T> + static quoted_result<T> quoted(const T& arg) + { + return boost::range::join(boost::range::join(ceref(u8"\""), arg), ceref(u8"\"")); + } + + template<typename Digest> + void operator()(const Digest& digest) const + { + static constexpr const auto fname = ceref(u8"WWW-authenticate"); + static constexpr const auto fvalue = ceref(u8"Digest qop=\"auth\""); + + for (unsigned i = 0; i < 2; ++i) + { + std::string out(fvalue); + + const auto algorithm = boost::range::join( + Digest::name, (i == 0 ? boost::string_ref{} : sess_algo) + ); + add_field(out, u8"algorithm", algorithm); + add_field(out, u8"realm", quoted(auth_realm)); + add_field(out, u8"nonce", quoted(nonce)); + add_field(out, u8"stale", is_stale ? ceref("true") : ceref("false")); + + fields.push_back(std::make_pair(std::string(fname), std::move(out))); + } + } + + const std::string& nonce; + std::list<std::pair<std::string, std::string>>& fields; + const bool is_stale; + }; + + epee::net_utils::http::http_response_info create_digest_response( + const std::string& nonce, const bool is_stale) + { + epee::net_utils::http::http_response_info rc{}; + rc.m_response_code = 401; + rc.m_response_comment = u8"Unauthorized"; + rc.m_mime_tipe = u8"text/html"; + rc.m_body = + u8"<html><head><title>Unauthorized Access</title></head><body><h1>401 Unauthorized</h1></body></html>"; + + boost::fusion::for_each( + digest_algorithms, add_challenge{nonce, rc.m_additional_fields, is_stale} + ); + + return rc; + } +} + +namespace epee +{ + namespace net_utils + { + namespace http + { + http_auth::http_auth(login credentials) + : user(session{std::move(credentials)}) { + } + + boost::optional<http_response_info> http_auth::process(const http_request_info& request) + { + assert(user); + using field = std::pair<std::string, std::string>; + + const std::list<field>& fields = request.m_header_info.m_etc_fields; + const auto auth = boost::find_if(fields, [] (const field& value) { + return boost::equals(ceref(u8"authorization"), value.first, ascii_iequal); + }); + + bool is_stale = false; + if (auth != fields.end()) + { + ++(user->counter); + switch (auth_request::verify(request.m_http_method_str, auth->second, *user)) + { + case auth_request::kPass: + return boost::none; + + case auth_request::kStale: + is_stale = true; + break; + + default: + case auth_request::kFail: + break; + } + } + user->counter = 0; + { + std::array<std::uint8_t, 16> rand_128bit{{}}; + crypto::rand(rand_128bit.size(), rand_128bit.data()); + user->nonce = string_encoding::base64_encode(rand_128bit.data(), rand_128bit.size()); + } + return create_digest_response(user->nonce, is_stale); + } + } + } +} + diff --git a/src/ringct/rctTypes.h b/src/ringct/rctTypes.h index 71cc61ddc..6b24fc470 100644 --- a/src/ringct/rctTypes.h +++ b/src/ringct/rctTypes.h @@ -308,10 +308,10 @@ namespace rct { ar.tag("cc"); FIELDS(MGs[i].cc) // MGs[i].II not saved, it can be reconstructed - if (mg_elements - i > 1) - ar.delimit_array(); - ar.end_object(); + + if (mg_elements - i > 1) + ar.delimit_array(); } ar.end_array(); return true; diff --git a/src/rpc/CMakeLists.txt b/src/rpc/CMakeLists.txt index 050b5e569..6df93cde1 100644 --- a/src/rpc/CMakeLists.txt +++ b/src/rpc/CMakeLists.txt @@ -46,6 +46,7 @@ target_link_libraries(rpc PUBLIC cryptonote_core cryptonote_protocol + epee ${Boost_THREAD_LIBRARY} PRIVATE ${EXTRA_LIBRARIES}) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 85bdee16f..b46447975 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -663,6 +663,7 @@ simple_wallet::simple_wallet() m_cmd_binder.set_handler("locked_transfer", boost::bind(&simple_wallet::locked_transfer, this, _1), tr("locked_transfer [<mixin_count>] <addr> <amount> <lockblocks>(Number of blocks to lock the transaction for, max 1000000) [<payment_id>]")); m_cmd_binder.set_handler("sweep_unmixable", boost::bind(&simple_wallet::sweep_unmixable, this, _1), tr("Send all unmixable outputs to yourself with mixin 0")); m_cmd_binder.set_handler("sweep_all", boost::bind(&simple_wallet::sweep_all, this, _1), tr("sweep_all [mixin] address [payment_id] - Send all unlocked balance an address")); + m_cmd_binder.set_handler("donate", boost::bind(&simple_wallet::donate, this, _1), tr("donate [<mixin_count>] <amount> [payment_id] - Donate <amount> to the development team (donate.getmonero.org)")); m_cmd_binder.set_handler("sign_transfer", boost::bind(&simple_wallet::sign_transfer, this, _1), tr("Sign a transaction from a file")); m_cmd_binder.set_handler("submit_transfer", boost::bind(&simple_wallet::submit_transfer, this, _1), tr("Submit a signed transaction from a file")); m_cmd_binder.set_handler("set_log", boost::bind(&simple_wallet::set_log, this, _1), tr("set_log <level> - Change current log detail level, <0-4>")); @@ -2826,6 +2827,45 @@ bool simple_wallet::sweep_all(const std::vector<std::string> &args_) return true; } //---------------------------------------------------------------------------------------------------- +bool simple_wallet::donate(const std::vector<std::string> &args_) +{ + std::vector<std::string> local_args = args_; + if(local_args.empty() || local_args.size() > 3) + { + fail_msg_writer() << tr("wrong number of arguments"); + return true; + } + std::string mixin_str; + std::string address_str = "donate.getmonero.org"; + std::string amount_str; + std::string payment_id_str; + // check payment id + crypto::hash payment_id; + crypto::hash8 payment_id8; + if (tools::wallet2::parse_long_payment_id (local_args.back(), payment_id ) || + tools::wallet2::parse_short_payment_id(local_args.back(), payment_id8)) + { + payment_id_str = local_args.back(); + local_args.pop_back(); + } + // check mixin + if (local_args.size() > 1) + { + mixin_str = local_args[0]; + local_args.erase(local_args.begin()); + } + amount_str = local_args[0]; + // refill args as necessary + local_args.clear(); + if (!mixin_str.empty()) + local_args.push_back(mixin_str); + local_args.push_back(address_str); + local_args.push_back(amount_str); + if (!payment_id_str.empty()) + local_args.push_back(payment_id_str); + transfer_new(local_args); +} +//---------------------------------------------------------------------------------------------------- bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes, const std::function<const tools::wallet2::tx_construction_data&(size_t)> &get_tx, const std::string &extra_message) { // gather info to ask the user diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 0d83f429b..420597699 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -124,6 +124,7 @@ namespace cryptonote bool locked_transfer(const std::vector<std::string> &args); bool sweep_all(const std::vector<std::string> &args); bool sweep_unmixable(const std::vector<std::string> &args); + bool donate(const std::vector<std::string> &args); bool sign_transfer(const std::vector<std::string> &args); bool submit_transfer(const std::vector<std::string> &args); std::vector<std::vector<cryptonote::tx_destination_entry>> split_amounts( diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index ba5c6b5ac..056a1ca10 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -34,7 +34,6 @@ set(wallet_sources password_container.cpp wallet2.cpp wallet_args.cpp - wallet_rpc_server.cpp api/wallet.cpp api/wallet_manager.cpp api/transaction_info.cpp @@ -103,6 +102,7 @@ if (NOT BUILD_GUI_DEPS) target_link_libraries(wallet_rpc_server PRIVATE wallet + epee rpc cryptonote_core crypto diff --git a/src/wallet/api/address_book.cpp b/src/wallet/api/address_book.cpp index 1847e1496..bbf96c81a 100644 --- a/src/wallet/api/address_book.cpp +++ b/src/wallet/api/address_book.cpp @@ -94,7 +94,7 @@ void AddressBookImpl::refresh() } -bool AddressBookImpl::deleteRow(int rowId) +bool AddressBookImpl::deleteRow(std::size_t rowId) { LOG_PRINT_L2("Deleting address book row " << rowId); bool r = m_wallet->m_wallet->delete_address_book_row(rowId); diff --git a/src/wallet/api/address_book.h b/src/wallet/api/address_book.h index c3a24eff9..7f30e4387 100644 --- a/src/wallet/api/address_book.h +++ b/src/wallet/api/address_book.h @@ -46,7 +46,7 @@ public: void refresh(); std::vector<AddressBookRow*> getAll() const; bool addRow(const std::string &dst_addr , const std::string &payment_id, const std::string &description); - bool deleteRow(int rowId); + bool deleteRow(std::size_t rowId); // Error codes. See AddressBook:ErrorCode enum in wallet2_api.h std::string errorString() const {return m_errorString;} diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 6d8e48deb..3a4493ec3 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -204,6 +204,7 @@ WalletImpl::WalletImpl(bool testnet) , m_recoveringFromSeed(false) , m_synchronized(false) , m_rebuildWalletCache(false) + , m_is_connected(false) { m_wallet = new tools::wallet2(testnet); m_history = new TransactionHistoryImpl(this); @@ -224,11 +225,19 @@ WalletImpl::WalletImpl(bool testnet) WalletImpl::~WalletImpl() { + + LOG_PRINT_L1(__FUNCTION__); + // Pause refresh thread - prevents refresh from starting again + pauseRefresh(); + // Close wallet - stores cache and stops ongoing refresh operation + close(); + // Stop refresh thread stopRefresh(); + delete m_wallet2Callback; delete m_history; delete m_addressBook; delete m_wallet; - delete m_wallet2Callback; + LOG_PRINT_L1(__FUNCTION__ << " finished"); } bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language) @@ -329,7 +338,7 @@ bool WalletImpl::close() { bool result = false; - LOG_PRINT_L3("closing wallet..."); + LOG_PRINT_L1("closing wallet..."); try { // Do not store wallet with invalid status // Status Critical refers to errors on opening or creating wallets. @@ -337,10 +346,10 @@ bool WalletImpl::close() m_wallet->store(); else LOG_PRINT_L3("Status_Critical - not storing wallet"); - LOG_PRINT_L3("wallet::store done"); - LOG_PRINT_L3("Calling wallet::stop..."); + LOG_PRINT_L1("wallet::store done"); + LOG_PRINT_L1("Calling wallet::stop..."); m_wallet->stop(); - LOG_PRINT_L3("wallet::stop done"); + LOG_PRINT_L1("wallet::stop done"); result = true; clearStatus(); } catch (const std::exception &e) { @@ -487,6 +496,8 @@ uint64_t WalletImpl::approximateBlockChainHeight() const } uint64_t WalletImpl::daemonBlockChainHeight() const { + if (!m_is_connected) + return 0; std::string err; uint64_t result = m_wallet->get_daemon_blockchain_height(err); if (!err.empty()) { @@ -504,6 +515,8 @@ uint64_t WalletImpl::daemonBlockChainHeight() const uint64_t WalletImpl::daemonBlockChainTargetHeight() const { + if (!m_is_connected) + return 0; std::string err; uint64_t result = m_wallet->get_daemon_blockchain_target_height(err); if (!err.empty()) { @@ -516,9 +529,20 @@ uint64_t WalletImpl::daemonBlockChainTargetHeight() const m_status = Status_Ok; m_errorString = ""; } + // Target height can be 0 when daemon is synced. Use blockchain height instead. + if(result == 0) + result = daemonBlockChainHeight(); return result; } +bool WalletImpl::daemonSynced() const +{ + if(connected() == Wallet::ConnectionStatus_Disconnected) + return false; + uint64_t blockChainHeight = daemonBlockChainHeight(); + return (blockChainHeight >= daemonBlockChainTargetHeight() && blockChainHeight > 1); +} + bool WalletImpl::synchronized() const { return m_synchronized; @@ -924,8 +948,8 @@ bool WalletImpl::connectToDaemon() Wallet::ConnectionStatus WalletImpl::connected() const { uint32_t version = 0; - bool is_connected = m_wallet->check_connection(&version); - if (!is_connected) + m_is_connected = m_wallet->check_connection(&version); + if (!m_is_connected) return Wallet::ConnectionStatus_Disconnected; if ((version >> 16) != CORE_RPC_VERSION_MAJOR) return Wallet::ConnectionStatus_WrongVersion; @@ -970,7 +994,7 @@ void WalletImpl::refreshThreadFunc() LOG_PRINT_L3(__FUNCTION__ << ": refresh lock acquired..."); LOG_PRINT_L3(__FUNCTION__ << ": m_refreshEnabled: " << m_refreshEnabled); LOG_PRINT_L3(__FUNCTION__ << ": m_status: " << m_status); - if (m_refreshEnabled /*&& m_status == Status_Ok*/) { + if (m_refreshEnabled) { LOG_PRINT_L3(__FUNCTION__ << ": refreshing..."); doRefresh(); } @@ -983,16 +1007,23 @@ void WalletImpl::doRefresh() // synchronizing async and sync refresh calls boost::lock_guard<boost::mutex> guarg(m_refreshMutex2); try { - m_wallet->refresh(); - if (!m_synchronized) { - m_synchronized = true; - } - // assuming if we have empty history, it wasn't initialized yet - // for futher history changes client need to update history in - // "on_money_received" and "on_money_sent" callbacks - if (m_history->count() == 0) { - m_history->refresh(); - } + // Syncing daemon and refreshing wallet simultaneously is very resource intensive. + // Disable refresh if wallet is disconnected or daemon isn't synced. + if (daemonSynced()) { + // Use fast refresh for new wallets + if (isNewWallet()) + m_wallet->set_refresh_from_block_height(daemonBlockChainHeight()); + m_wallet->refresh(); + if (!m_synchronized) { + m_synchronized = true; + } + // assuming if we have empty history, it wasn't initialized yet + // for futher history changes client need to update history in + // "on_money_received" and "on_money_sent" callbacks + if (m_history->count() == 0) { + m_history->refresh(); + } + } } catch (const std::exception &e) { m_status = Status_Error; m_errorString = e.what(); diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 5046fbf0f..d245a78d0 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -107,14 +107,15 @@ public: virtual std::string getTxKey(const std::string &txid) const; virtual std::string signMessage(const std::string &message); virtual bool verifySignedMessage(const std::string &message, const std::string &address, const std::string &signature) const; + virtual void startRefresh(); + virtual void pauseRefresh(); private: void clearStatus(); void refreshThreadFunc(); void doRefresh(); - void startRefresh(); + bool daemonSynced() const; void stopRefresh(); - void pauseRefresh(); bool isNewWallet() const; void doInit(const std::string &daemon_address, uint64_t upper_transaction_size_limit); @@ -148,9 +149,11 @@ private: // flag indicating wallet is recovering from seed // so it shouldn't be considered as new and pull blocks (slow-refresh) // instead of pulling hashes (fast-refresh) - bool m_recoveringFromSeed; + std::atomic<bool> m_recoveringFromSeed; std::atomic<bool> m_synchronized; - bool m_rebuildWalletCache; + std::atomic<bool> m_rebuildWalletCache; + // cache connection status to avoid unnecessary RPC calls + mutable std::atomic<bool> m_is_connected; }; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 60fa1d266..db4fae557 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1569,14 +1569,14 @@ bool wallet2::add_address_book_row(const cryptonote::account_public_address &add a.m_payment_id = payment_id; a.m_description = description; - int old_size = m_address_book.size(); + auto old_size = m_address_book.size(); m_address_book.push_back(a); if(m_address_book.size() == old_size+1) return true; return false; } -bool wallet2::delete_address_book_row(int row_id) { +bool wallet2::delete_address_book_row(std::size_t row_id) { if(m_address_book.size() <= row_id) return false; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 36b9b3d1f..c3381730b 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -525,7 +525,7 @@ namespace tools */ std::vector<address_book_row> get_address_book() const { return m_address_book; } bool add_address_book_row(const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description); - bool delete_address_book_row(int row_id); + bool delete_address_book_row(std::size_t row_id); uint64_t get_num_rct_outputs(); const transfer_details &get_transfer_details(size_t idx) const; diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index cd2e7230a..6651b876d 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -137,14 +137,14 @@ struct TransactionHistory */ struct AddressBookRow { public: - AddressBookRow(int _rowId, const std::string &_address, const std::string &_paymentId, const std::string &_description): + AddressBookRow(std::size_t _rowId, const std::string &_address, const std::string &_paymentId, const std::string &_description): m_rowId(_rowId), m_address(_address), m_paymentId(_paymentId), m_description(_description) {} private: - int m_rowId; + std::size_t m_rowId; std::string m_address; std::string m_paymentId; std::string m_description; @@ -153,7 +153,7 @@ public: std::string getAddress() const {return m_address;} std::string getDescription() const {return m_description;} std::string getPaymentId() const {return m_paymentId;} - int getRowId() const {return m_rowId;} + std::size_t getRowId() const {return m_rowId;} }; /** @@ -171,7 +171,7 @@ struct AddressBook virtual ~AddressBook() = 0; virtual std::vector<AddressBookRow*> getAll() const = 0; virtual bool addRow(const std::string &dst_addr , const std::string &payment_id, const std::string &description) = 0; - virtual bool deleteRow(int rowId) = 0; + virtual bool deleteRow(std::size_t rowId) = 0; virtual void refresh() = 0; virtual std::string errorString() const = 0; virtual int errorCode() const = 0; @@ -364,6 +364,15 @@ struct Wallet static std::string paymentIdFromAddress(const std::string &str, bool testnet); static uint64_t maximumAllowedAmount(); + /** + * @brief StartRefresh - Start/resume refresh thread (refresh every 10 seconds) + */ + virtual void startRefresh() = 0; + /** + * @brief pauseRefresh - pause refresh thread + */ + virtual void pauseRefresh() = 0; + /** * @brief refresh - refreshes the wallet, updating transactions from daemon * @return - true if refreshed successfully; diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index f5c64dc94..08c8213e4 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -42,6 +42,7 @@ set(unit_tests_sources epee_levin_protocol_handler_async.cpp fee.cpp get_xtype_from_string.cpp + http_auth.cpp main.cpp mnemonics.cpp mul_div.cpp @@ -73,6 +74,7 @@ target_link_libraries(unit_tests rpc wallet p2p + epee ${GTEST_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${EXTRA_LIBRARIES}) diff --git a/tests/unit_tests/http_auth.cpp b/tests/unit_tests/http_auth.cpp new file mode 100644 index 000000000..fc647dfeb --- /dev/null +++ b/tests/unit_tests/http_auth.cpp @@ -0,0 +1,471 @@ +// Copyright (c) 2014-2016, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "gtest/gtest.h" +#include "net/http_auth.h" + +#include <boost/algorithm/string/predicate.hpp> +#include <boost/fusion/adapted/std_pair.hpp> +#include <boost/range/algorithm/find_if.hpp> +#include <boost/range/iterator_range_core.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_list.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_right_alignment.hpp> +#include <boost/spirit/include/karma_sequence.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_uint.hpp> +#include <boost/spirit/include/qi_alternative.hpp> +#include <boost/spirit/include/qi_char.hpp> +#include <boost/spirit/include/qi_char_class.hpp> +#include <boost/spirit/include/qi_difference.hpp> +#include <boost/spirit/include/qi_eoi.hpp> +#include <boost/spirit/include/qi_list.hpp> +#include <boost/spirit/include/qi_parse.hpp> +#include <boost/spirit/include/qi_plus.hpp> +#include <boost/spirit/include/qi_sequence.hpp> +#include <boost/spirit/include/qi_string.hpp> +#include <cstdint> +#include <iterator> +#include <string> +#include <unordered_map> +#include <utility> +#include <vector> + +#include "md5_l.h" +#include "string_tools.h" + +namespace { +using fields = std::unordered_map<std::string, std::string>; +using auth_responses = std::vector<fields>; + +std::string quoted(std::string str) +{ + str.insert(str.begin(), '"'); + str.push_back('"'); + return str; +} + +epee::net_utils::http::http_request_info make_request(const fields& args) +{ + namespace karma = boost::spirit::karma; + + std::string out{" DIGEST "}; + karma::generate( + std::back_inserter(out), + (karma::string << " = " << karma::string) % " , ", + args); + + epee::net_utils::http::http_request_info request{}; + request.m_http_method_str = "NOP"; + request.m_header_info.m_etc_fields.push_back( + std::make_pair(u8"authorization", std::move(out)) + ); + return request; +} + +bool has_same_fields(const auth_responses& in) +{ + const std::vector<std::string> check{u8"nonce", u8"qop", u8"realm", u8"stale"}; + + auto current = in.begin(); + const auto end = in.end(); + if (current == end) + return true; + + ++current; + for ( ; current != end; ++current ) + { + for (const auto& field : check) + { + const std::string& expected = in[0].at(field); + const std::string& actual = current->at(field); + EXPECT_EQ(expected, actual); + if (expected != actual) + return false; + } + } + return true; +} + +bool is_unauthorized(const epee::net_utils::http::http_response_info& response) +{ + EXPECT_EQ(401, response.m_response_code); + EXPECT_STREQ(u8"Unauthorized", response.m_response_comment.c_str()); + EXPECT_STREQ(u8"text/html", response.m_mime_tipe.c_str()); + return response.m_response_code == 401 && + response.m_response_comment == u8"Unauthorized" && + response.m_mime_tipe == u8"text/html"; +} + + +auth_responses parse_response(const epee::net_utils::http::http_response_info& response) +{ + namespace qi = boost::spirit::qi; + + auth_responses result{}; + + const auto end = response.m_additional_fields.end(); + for (auto current = response.m_additional_fields.begin(); current != end; ++current) + { + current = std::find_if(current, end, [] (const std::pair<std::string, std::string>& field) { + return boost::iequals(u8"www-authenticate", field.first); + }); + + if (current == end) + return result; + + std::unordered_map<std::string, std::string> fields{}; + const bool rc = qi::parse( + current->second.begin(), current->second.end(), + qi::lit(u8"Digest ") >> (( + +qi::ascii::alpha >> + qi::lit('=') >> ( + (qi::lit('"') >> +(qi::ascii::char_ - '"') >> qi::lit('"')) | + +(qi::ascii::graph - qi::ascii::char_(u8"()<>@,;:\\\"/[]?={}")) + ) + ) % ',' + ) >> qi::eoi, + fields + ); + + if (rc) + result.push_back(std::move(fields)); + } + return result; +} + +std::string md5_hex(const std::string& in) +{ + md5::MD5_CTX ctx{}; + md5::MD5Init(std::addressof(ctx)); + md5::MD5Update( + std::addressof(ctx), + reinterpret_cast<const std::uint8_t*>(in.data()), + in.size() + ); + + std::array<std::uint8_t, 16> digest{{}}; + md5::MD5Final(digest.data(), std::addressof(ctx)); + return epee::string_tools::pod_to_hex(digest); +} + +std::string get_a1(const epee::net_utils::http::http_auth::login& user, const auth_responses& responses) +{ + const std::string& realm = responses.at(0).at(u8"realm"); + return boost::join( + std::vector<std::string>{user.username, realm, user.password}, u8":" + ); +} + +std::string get_a1_sess(const epee::net_utils::http::http_auth::login& user, const std::string& cnonce, const auth_responses& responses) +{ + const std::string& nonce = responses.at(0).at(u8"nonce"); + return boost::join( + std::vector<std::string>{md5_hex(get_a1(user, responses)), nonce, cnonce}, u8":" + ); +} + +std::string get_a2(const std::string& uri) +{ + return boost::join(std::vector<std::string>{"NOP", uri}, u8":"); +} + +std::string get_nc(std::uint32_t count) +{ + namespace karma = boost::spirit::karma; + std::string out; + karma::generate( + std::back_inserter(out), + karma::right_align(8, '0')[karma::uint_generator<std::uint32_t, 16>{}], + count + ); + + return out; +} +} + +TEST(HTTP_Auth, NotRequired) +{ + epee::net_utils::http::http_auth auth{}; + EXPECT_FALSE(auth.get_response(epee::net_utils::http::http_request_info{})); +} + +TEST(HTTP_Auth, MissingAuth) +{ + epee::net_utils::http::http_auth auth{{"foo", "bar"}}; + EXPECT_TRUE(auth.get_response(epee::net_utils::http::http_request_info{})); + { + epee::net_utils::http::http_request_info request{}; + request.m_header_info.m_etc_fields.push_back({"\xFF", "\xFF"}); + EXPECT_TRUE(auth.get_response(request)); + } +} + +TEST(HTTP_Auth, BadSyntax) +{ + epee::net_utils::http::http_auth auth{{"foo", "bar"}}; + EXPECT_TRUE(auth.get_response(make_request({{u8"algorithm", "fo\xFF"}}))); + EXPECT_TRUE(auth.get_response(make_request({{u8"cnonce", "\"000\xFF\""}}))); + EXPECT_TRUE(auth.get_response(make_request({{u8"cnonce \xFF =", "\"000\xFF\""}}))); + EXPECT_TRUE(auth.get_response(make_request({{u8" \xFF cnonce", "\"000\xFF\""}}))); +} + +TEST(HTTP_Auth, MD5) +{ + epee::net_utils::http::http_auth::login user{"foo", "bar"}; + epee::net_utils::http::http_auth auth{user}; + + const auto response = auth.get_response(make_request({})); + ASSERT_TRUE(response); + EXPECT_TRUE(is_unauthorized(*response)); + + const auto fields = parse_response(*response); + ASSERT_LE(2u, fields.size()); + EXPECT_TRUE(has_same_fields(fields)); + + const std::string& nonce = fields[0].at(u8"nonce"); + EXPECT_EQ(24, nonce.size()); + + const std::string uri{"/some_foo_thing"}; + + const std::string a1 = get_a1(user, fields); + const std::string a2 = get_a2(uri); + + const std::string auth_code = md5_hex( + boost::join(std::vector<std::string>{md5_hex(a1), nonce, md5_hex(a2)}, u8":") + ); + + const auto request = make_request({ + {u8"nonce", quoted(nonce)}, + {u8"realm", quoted(fields[0].at(u8"realm"))}, + {u8"response", quoted(auth_code)}, + {u8"uri", quoted(uri)}, + {u8"username", quoted(user.username)} + }); + + EXPECT_FALSE(auth.get_response(request)); + + const auto response2 = auth.get_response(request); + ASSERT_TRUE(response2); + EXPECT_TRUE(is_unauthorized(*response2)); + + const auto fields2 = parse_response(*response2); + ASSERT_LE(2u, fields2.size()); + EXPECT_TRUE(has_same_fields(fields2)); + + EXPECT_NE(nonce, fields2[0].at(u8"nonce")); + EXPECT_STREQ(u8"true", fields2[0].at(u8"stale").c_str()); +} + +TEST(HTTP_Auth, MD5_sess) +{ + constexpr const char cnonce[] = "not a good cnonce"; + + epee::net_utils::http::http_auth::login user{"foo", "bar"}; + epee::net_utils::http::http_auth auth{user}; + + const auto response = auth.get_response(make_request({})); + ASSERT_TRUE(response); + EXPECT_TRUE(is_unauthorized(*response)); + + const auto fields = parse_response(*response); + ASSERT_LE(2u, fields.size()); + EXPECT_TRUE(has_same_fields(fields)); + + const std::string& nonce = fields[0].at(u8"nonce"); + EXPECT_EQ(24, nonce.size()); + + const std::string uri{"/some_foo_thing"}; + + const std::string a1 = get_a1_sess(user, cnonce, fields); + const std::string a2 = get_a2(uri); + + const std::string auth_code = md5_hex( + boost::join(std::vector<std::string>{md5_hex(a1), nonce, md5_hex(a2)}, u8":") + ); + + const auto request = make_request({ + {u8"algorithm", u8"md5-sess"}, + {u8"cnonce", quoted(cnonce)}, + {u8"nonce", quoted(nonce)}, + {u8"realm", quoted(fields[0].at(u8"realm"))}, + {u8"response", quoted(auth_code)}, + {u8"uri", quoted(uri)}, + {u8"username", quoted(user.username)} + }); + + EXPECT_FALSE(auth.get_response(request)); + + const auto response2 = auth.get_response(request); + ASSERT_TRUE(response2); + EXPECT_TRUE(is_unauthorized(*response2)); + + const auto fields2 = parse_response(*response2); + ASSERT_LE(2u, fields2.size()); + EXPECT_TRUE(has_same_fields(fields2)); + + EXPECT_NE(nonce, fields2[0].at(u8"nonce")); + EXPECT_STREQ(u8"true", fields2[0].at(u8"stale").c_str()); +} + +TEST(HTTP_Auth, MD5_auth) +{ + constexpr const char cnonce[] = "not a nonce"; + constexpr const char qop[] = "auth"; + + epee::net_utils::http::http_auth::login user{"foo", "bar"}; + epee::net_utils::http::http_auth auth{user}; + + const auto response = auth.get_response(make_request({})); + ASSERT_TRUE(response); + EXPECT_TRUE(is_unauthorized(*response)); + + const auto parsed = parse_response(*response); + ASSERT_LE(2u, parsed.size()); + EXPECT_TRUE(has_same_fields(parsed)); + + const std::string& nonce = parsed[0].at(u8"nonce"); + EXPECT_EQ(24, nonce.size()); + + const std::string uri{"/some_foo_thing"}; + + const std::string a1 = get_a1(user, parsed); + const std::string a2 = get_a2(uri); + std::string nc = get_nc(1); + + const auto generate_auth = [&] { + return md5_hex( + boost::join( + std::vector<std::string>{md5_hex(a1), nonce, nc, cnonce, qop, md5_hex(a2)}, u8":" + ) + ); + }; + + fields args{ + {u8"algorithm", quoted(u8"md5")}, + {u8"cnonce", quoted(cnonce)}, + {u8"nc", nc}, + {u8"nonce", quoted(nonce)}, + {u8"qop", quoted(qop)}, + {u8"realm", quoted(parsed[0].at(u8"realm"))}, + {u8"response", quoted(generate_auth())}, + {u8"uri", quoted(uri)}, + {u8"username", quoted(user.username)} + }; + + const auto request = make_request(args); + EXPECT_FALSE(auth.get_response(request)); + + for (unsigned i = 2; i < 20; ++i) + { + nc = get_nc(i); + args.at(u8"nc") = nc; + args.at(u8"response") = quoted(generate_auth()); + EXPECT_FALSE(auth.get_response(make_request(args))); + } + + const auto replay = auth.get_response(request); + ASSERT_TRUE(replay); + EXPECT_TRUE(is_unauthorized(*replay)); + + const auto parsed_replay = parse_response(*replay); + ASSERT_LE(2u, parsed_replay.size()); + EXPECT_TRUE(has_same_fields(parsed_replay)); + + EXPECT_NE(nonce, parsed_replay[0].at(u8"nonce")); + EXPECT_STREQ(u8"true", parsed_replay[0].at(u8"stale").c_str()); +} + +TEST(HTTP_Auth, MD5_sess_auth) +{ + constexpr const char cnonce[] = "not a nonce"; + constexpr const char qop[] = "auth"; + + epee::net_utils::http::http_auth::login user{"foo", "bar"}; + epee::net_utils::http::http_auth auth{user}; + + const auto response = auth.get_response(make_request({})); + ASSERT_TRUE(response); + EXPECT_TRUE(is_unauthorized(*response)); + + const auto parsed = parse_response(*response); + ASSERT_LE(2u, parsed.size()); + EXPECT_TRUE(has_same_fields(parsed)); + + const std::string& nonce = parsed[0].at(u8"nonce"); + EXPECT_EQ(24, nonce.size()); + + const std::string uri{"/some_foo_thing"}; + + const std::string a1 = get_a1_sess(user, cnonce, parsed); + const std::string a2 = get_a2(uri); + std::string nc = get_nc(1); + + const auto generate_auth = [&] { + return md5_hex( + boost::join( + std::vector<std::string>{md5_hex(a1), nonce, nc, cnonce, qop, md5_hex(a2)}, u8":" + ) + ); + }; + + fields args{ + {u8"algorithm", u8"md5-sess"}, + {u8"cnonce", quoted(cnonce)}, + {u8"nc", nc}, + {u8"nonce", quoted(nonce)}, + {u8"qop", qop}, + {u8"realm", quoted(parsed[0].at(u8"realm"))}, + {u8"response", quoted(generate_auth())}, + {u8"uri", quoted(uri)}, + {u8"username", quoted(user.username)} + }; + + const auto request = make_request(args); + EXPECT_FALSE(auth.get_response(request)); + + for (unsigned i = 2; i < 20; ++i) + { + nc = get_nc(i); + args.at(u8"nc") = nc; + args.at(u8"response") = quoted(generate_auth()); + EXPECT_FALSE(auth.get_response(make_request(args))); + } + + const auto replay = auth.get_response(request); + ASSERT_TRUE(replay); + EXPECT_TRUE(is_unauthorized(*replay)); + + const auto parsed_replay = parse_response(*replay); + ASSERT_LE(2u, parsed_replay.size()); + EXPECT_TRUE(has_same_fields(parsed_replay)); + + EXPECT_NE(nonce, parsed_replay[0].at(u8"nonce")); + EXPECT_STREQ(u8"true", parsed_replay[0].at(u8"stale").c_str()); +} |