23 #include <curl/curl.h>
30 const boost::property_tree::ptree &cfg,
33 , bus_sub_(ctx,
zmqpp::socket_type::sub)
38 if (
config_.get<
bool>(
"module_config.want_ssl",
true))
39 flags |= CURL_GLOBAL_SSL;
40 if ((ret = curl_global_init(flags)) != 0)
42 throw std::runtime_error(
"Failed to initialize curl: return code: " +
45 bus_sub_.connect(
"inproc://zmq-bus-pub");
52 curl_global_cleanup();
66 WARN(
"Unexpected message content.");
69 msg >> src >> type >> card >> bits;
72 INFO(
"WS-Notifier cannot handle this type of credential yet.");
81 for (
auto &&itr :
config_.get_child(
"module_config.sources"))
83 auto name = itr.second.get<std::string>(
"");
87 for (
auto &&itr :
config_.get_child(
"module_config.targets"))
90 target.
url_ = itr.second.get<std::string>(
"url");
93 target.
verify_host_ = itr.second.get<
bool>(
"verify_host",
true);
94 target.
verify_peer_ = itr.second.get<
bool>(
"verify_peer",
true);
95 target.
CA_info_file_ = itr.second.get<std::string>(
"ca_file",
"");
97 INFO(
"WS-Notifier remote target: "
104 targets_.push_back(std::move(target));
109 const std::string &card_hex,
113 card.card_id(card_hex);
114 card.nb_bits(nb_bits);
118 auto curl = curl_easy_init();
121 if (!target.CA_info_file_.empty())
122 curl_easy_setopt(curl, CURLOPT_CAINFO, target.CA_info_file_.c_str());
123 if (!target.verify_host_)
124 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
125 if (!target.verify_peer_)
126 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
128 curl_easy_cleanup(curl);
132 ERROR(
"Cannot initialize curl_easy.");
142 static size_t write_callback(
char * ,
size_t size,
size_t nmemb,
149 void *curl,
const std::string &auth_source,
const Cred::RFIDCard &card,
154 std::string post_fields =
155 fmt::format(
"card_id={}&auth_source={}&card_id_raw={}", card.to_int(),
156 auth_source, card.to_raw_int());
158 curl_easy_setopt(curl, CURLOPT_URL, target.url_.c_str());
159 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_fields.c_str());
162 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, target.connect_timeout_);
163 curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, target.request_timeout_);
165 curl_easy_setopt(curl, CURLOPT_WRITEDATA,
nullptr);
166 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_callback);
168 auto res = curl_easy_perform(curl);
171 WARN(
"curl_easy_perform() failed: " << curl_easy_strerror(res));