protocol.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. /**
  38. * @file
  39. * @brief Gear Protocol Definitions
  40. */
  41. #include "gear_config.h"
  42. #include "configmake.h"
  43. #include <libgearman-server/common.h>
  44. #include <libgearman/strcommand.h>
  45. #include <libgearman-server/packet.h>
  46. #include "libgearman/strcommand.h"
  47. #include <cstdio>
  48. #include <cstdlib>
  49. #include <cerrno>
  50. #include "libgearman/ssl.h"
  51. #include <libgearman-server/plugins/protocol/gear/protocol.h>
  52. #include "libgearman/command.h"
  53. static gearmand_error_t gearmand_packet_unpack_header(gearmand_packet_st *packet)
  54. {
  55. uint32_t tmp;
  56. if (memcmp(packet->args, "\0REQ", 4) == 0)
  57. {
  58. packet->magic= GEARMAN_MAGIC_REQUEST;
  59. }
  60. else if (memcmp(packet->args, "\0RES", 4) == 0)
  61. {
  62. packet->magic= GEARMAN_MAGIC_RESPONSE;
  63. }
  64. else
  65. {
  66. gearmand_warning("invalid magic value");
  67. return GEARMAND_INVALID_MAGIC;
  68. }
  69. memcpy(&tmp, packet->args + 4, 4);
  70. packet->command= static_cast<gearman_command_t>(ntohl(tmp));
  71. if (packet->command == GEARMAN_COMMAND_TEXT ||
  72. packet->command >= GEARMAN_COMMAND_MAX)
  73. {
  74. gearmand_error("invalid command value");
  75. return GEARMAND_INVALID_COMMAND;
  76. }
  77. memcpy(&tmp, packet->args + 8, 4);
  78. packet->data_size= ntohl(tmp);
  79. return GEARMAND_SUCCESS;
  80. }
  81. class Geartext : public gearmand::protocol::Context {
  82. public:
  83. ~Geartext()
  84. { }
  85. bool is_owner()
  86. {
  87. return false;
  88. }
  89. void notify(gearman_server_con_st* connection)
  90. {
  91. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "Gear connection disconnected: %s:%s", connection->host(), connection->port());
  92. }
  93. size_t unpack(gearmand_packet_st *packet,
  94. gearman_server_con_st *,
  95. const void *data, const size_t data_size,
  96. gearmand_error_t& ret_ptr)
  97. {
  98. size_t used_size;
  99. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "Gear unpack");
  100. if (packet->args_size == 0)
  101. {
  102. if (data_size > 0 && ((uint8_t *)data)[0] != 0)
  103. {
  104. /* Try to parse a text-based command. */
  105. uint8_t* ptr= (uint8_t *)memchr(data, '\n', data_size);
  106. if (ptr == NULL)
  107. {
  108. ret_ptr= GEARMAND_IO_WAIT;
  109. return 0;
  110. }
  111. packet->magic= GEARMAN_MAGIC_TEXT;
  112. packet->command= GEARMAN_COMMAND_TEXT;
  113. used_size= size_t(ptr - ((uint8_t *)data)) +1;
  114. *ptr= 0;
  115. if (used_size > 1 && *(ptr - 1) == '\r')
  116. {
  117. *(ptr - 1)= 0;
  118. }
  119. size_t arg_size;
  120. for (arg_size= used_size, ptr= (uint8_t *)data; ptr != NULL; data= ptr)
  121. {
  122. ptr= (uint8_t *)memchr(data, ' ', arg_size);
  123. if (ptr != NULL)
  124. {
  125. *ptr= 0;
  126. ptr++;
  127. while (*ptr == ' ')
  128. {
  129. ptr++;
  130. }
  131. arg_size-= size_t(ptr - ((uint8_t *)data));
  132. }
  133. ret_ptr= gearmand_packet_create(packet, data, ptr == NULL ? arg_size :
  134. size_t(ptr - ((uint8_t *)data)));
  135. if (ret_ptr != GEARMAND_SUCCESS)
  136. {
  137. return used_size;
  138. }
  139. }
  140. return used_size;
  141. }
  142. else if (data_size < GEARMAND_PACKET_HEADER_SIZE)
  143. {
  144. ret_ptr= GEARMAND_IO_WAIT;
  145. return 0;
  146. }
  147. packet->args= packet->args_buffer;
  148. packet->args_size= GEARMAND_PACKET_HEADER_SIZE;
  149. memcpy(packet->args, data, GEARMAND_PACKET_HEADER_SIZE);
  150. if (gearmand_failed(ret_ptr= gearmand_packet_unpack_header(packet)))
  151. {
  152. return 0;
  153. }
  154. used_size= GEARMAND_PACKET_HEADER_SIZE;
  155. }
  156. else
  157. {
  158. used_size= 0;
  159. }
  160. while (packet->argc != gearman_command_info(packet->command)->argc)
  161. {
  162. if (packet->argc != (gearman_command_info(packet->command)->argc - 1) or
  163. gearman_command_info(packet->command)->data)
  164. {
  165. uint8_t* ptr= (uint8_t *)memchr(((uint8_t *)data) +used_size, 0,
  166. data_size -used_size);
  167. if (ptr == NULL)
  168. {
  169. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  170. "Possible protocol error for %s, received only %u args",
  171. gearman_command_info(packet->command)->name, packet->argc);
  172. ret_ptr= GEARMAND_IO_WAIT;
  173. return used_size;
  174. }
  175. size_t arg_size= size_t(ptr - (((uint8_t *)data) + used_size)) +1;
  176. if (gearmand_failed((ret_ptr= gearmand_packet_create(packet, ((uint8_t *)data) + used_size, arg_size))))
  177. {
  178. return used_size;
  179. }
  180. packet->data_size-= arg_size;
  181. used_size+= arg_size;
  182. }
  183. else
  184. {
  185. if ((data_size - used_size) < packet->data_size)
  186. {
  187. ret_ptr= GEARMAND_IO_WAIT;
  188. return used_size;
  189. }
  190. ret_ptr= gearmand_packet_create(packet, ((uint8_t *)data) + used_size, packet->data_size);
  191. if (gearmand_failed(ret_ptr))
  192. {
  193. return used_size;
  194. }
  195. used_size+= packet->data_size;
  196. packet->data_size= 0;
  197. }
  198. }
  199. #if defined(VCS_CHECKOUT) && VCS_CHECKOUT
  200. if (packet->command == GEARMAN_COMMAND_ECHO_REQ and packet->data_size)
  201. {
  202. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  203. "GEAR %s length: %" PRIu64,
  204. gearman_strcommand(packet->command),
  205. uint64_t(packet->data_size));
  206. }
  207. else if (packet->command == GEARMAN_COMMAND_TEXT and packet->data_size)
  208. {
  209. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  210. "GEAR %s text: %.*s",
  211. gearman_strcommand(packet->command),
  212. int(packet->data_size),
  213. packet->data);
  214. }
  215. else if (packet->command == GEARMAN_COMMAND_OPTION_REQ and packet->arg_size[0])
  216. {
  217. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  218. "GEAR %s option: %.*s",
  219. gearman_strcommand(packet->command),
  220. int(packet->arg_size[0]),
  221. packet->arg[0]);
  222. }
  223. else if (packet->command == GEARMAN_COMMAND_WORK_EXCEPTION and packet->data_size)
  224. {
  225. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  226. "GEAR %s handle: %.*s exception: %.*s",
  227. gearman_strcommand(packet->command),
  228. int(packet->arg_size[0]),
  229. packet->arg[0],
  230. int(packet->data_size),
  231. packet->data);
  232. }
  233. else if (packet->command == GEARMAN_COMMAND_WORK_FAIL and packet->arg_size[0])
  234. {
  235. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  236. "GEAR %s handle: %.*s",
  237. gearman_strcommand(packet->command),
  238. int(packet->arg_size[0]),
  239. packet->arg[0]);
  240. }
  241. else if (packet->command == GEARMAN_COMMAND_SET_CLIENT_ID and packet->arg_size[0])
  242. {
  243. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  244. "GEAR %s identifier: %.*s",
  245. gearman_strcommand(packet->command),
  246. int(packet->arg_size[0]),
  247. packet->arg[0]);
  248. }
  249. else
  250. {
  251. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  252. "GEAR %s",
  253. gearman_strcommand(packet->command));
  254. }
  255. #endif
  256. ret_ptr= GEARMAND_SUCCESS;
  257. return used_size;
  258. }
  259. size_t pack(const gearmand_packet_st *packet,
  260. gearman_server_con_st*,
  261. void *data, const size_t data_size,
  262. gearmand_error_t& ret_ptr)
  263. {
  264. #if defined(VCS_CHECKOUT) && VCS_CHECKOUT
  265. if (packet->command == GEARMAN_COMMAND_ECHO_RES and packet->data_size)
  266. {
  267. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  268. "GEAR %s length: %" PRIu64,
  269. gearman_strcommand(packet->command),
  270. uint64_t(packet->data_size));
  271. }
  272. else if (packet->command == GEARMAN_COMMAND_OPTION_RES and packet->arg_size[0])
  273. {
  274. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  275. "GEAR %s option: %.*s",
  276. gearman_strcommand(packet->command),
  277. int(packet->arg_size[0]),
  278. packet->arg[0]);
  279. }
  280. else
  281. {
  282. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  283. "GEAR %s",
  284. gearman_strcommand(packet->command));
  285. }
  286. #endif
  287. if (packet->args_size == 0)
  288. {
  289. ret_ptr= GEARMAND_SUCCESS;
  290. return 0;
  291. }
  292. if (packet->args_size > data_size)
  293. {
  294. ret_ptr= GEARMAND_FLUSH_DATA;
  295. return 0;
  296. }
  297. memcpy(data, packet->args, packet->args_size);
  298. ret_ptr= GEARMAND_SUCCESS;
  299. return packet->args_size;
  300. }
  301. private:
  302. };
  303. static Geartext gear_context;
  304. static gearmand_error_t _gear_con_remove(gearman_server_con_st* connection)
  305. {
  306. #if defined(HAVE_SSL) && HAVE_SSL
  307. if (connection->_ssl)
  308. {
  309. SSL_shutdown(connection->_ssl);
  310. SSL_free(connection->_ssl);
  311. connection->_ssl= NULL;
  312. }
  313. #else
  314. (void)connection;
  315. #endif
  316. return GEARMAND_SUCCESS;
  317. }
  318. static gearmand_error_t _gear_con_add(gearman_server_con_st *connection)
  319. {
  320. #if defined(HAVE_SSL) && HAVE_SSL
  321. if (Gearmand()->ctx_ssl())
  322. {
  323. if ((connection->_ssl= SSL_new(Gearmand()->ctx_ssl())) == NULL)
  324. {
  325. return gearmand_log_gerror(GEARMAN_DEFAULT_LOG_PARAM, GEARMAND_MEMORY_ALLOCATION_FAILURE, "SSL_new() failed to return a valid object");
  326. }
  327. SSL_set_fd(connection->_ssl, connection->con.fd());
  328. int accept_error;
  329. while ((accept_error= SSL_accept(connection->_ssl)) != SSL_SUCCESS)
  330. {
  331. int ssl_error;
  332. switch (ssl_error= SSL_get_error(connection->_ssl, accept_error))
  333. {
  334. case SSL_ERROR_NONE:
  335. break;
  336. case SSL_ERROR_WANT_READ:
  337. case SSL_ERROR_WANT_WRITE:
  338. case SSL_ERROR_WANT_ACCEPT:
  339. case SSL_ERROR_WANT_CONNECT:
  340. case SSL_ERROR_WANT_X509_LOOKUP:
  341. continue;
  342. case SSL_ERROR_SYSCALL:
  343. return gearmand_log_perror(GEARMAN_DEFAULT_LOG_PARAM, errno, "Error occurred on SSL_accept()");
  344. case SSL_ERROR_SSL:
  345. case SSL_ERROR_ZERO_RETURN:
  346. default:
  347. {
  348. char ssl_error_buffer[SSL_ERROR_SIZE]= { 0 };
  349. ERR_error_string_n(ssl_error, ssl_error_buffer, sizeof(ssl_error_buffer));
  350. return gearmand_log_gerror(GEARMAN_DEFAULT_LOG_PARAM, GEARMAND_LOST_CONNECTION, "%s(%d)",
  351. ssl_error_buffer, ssl_error);
  352. }
  353. }
  354. }
  355. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "GearSSL connection made: %s:%s", connection->host(), connection->port());
  356. }
  357. else
  358. #endif
  359. {
  360. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "Gear connection made: %s:%s", connection->host(), connection->port());
  361. }
  362. connection->set_protocol(&gear_context);
  363. return GEARMAND_SUCCESS;
  364. }
  365. namespace gearmand {
  366. namespace protocol {
  367. Gear::Gear() :
  368. Plugin("Gear"),
  369. _port(GEARMAN_DEFAULT_TCP_PORT_STRING),
  370. _ssl_ca_file(GEARMAND_CA_CERTIFICATE),
  371. _ssl_certificate(GEARMAND_SERVER_PEM),
  372. _ssl_key(GEARMAND_SERVER_KEY),
  373. opt_ssl(false)
  374. {
  375. command_line_options().add_options()
  376. ("port,p", boost::program_options::value(&_port)->default_value(GEARMAN_DEFAULT_TCP_PORT_STRING),
  377. "Port the server should listen on.")
  378. ("ssl", boost::program_options::bool_switch(&opt_ssl)->default_value(false),
  379. "Enable ssl connections.")
  380. ("ssl-ca-file", boost::program_options::value(&_ssl_ca_file),
  381. "CA file.")
  382. ("ssl-certificate", boost::program_options::value(&_ssl_certificate),
  383. "SSL certificate.")
  384. ("ssl-key", boost::program_options::value(&_ssl_key),
  385. "SSL key for certificate.")
  386. ;
  387. }
  388. Gear::~Gear()
  389. {
  390. }
  391. gearmand_error_t Gear::start(gearmand_st *gearmand)
  392. {
  393. gearmand_error_t rc;
  394. if (_port.compare(GEARMAN_DEFAULT_TCP_PORT_STRING) == 0)
  395. {
  396. char* service;
  397. if ((service= getenv("GEARMAND_PORT")) and service[0])
  398. {
  399. _port.clear();
  400. _port.append(service);
  401. }
  402. }
  403. if (_port.empty())
  404. {
  405. const char* service= GEARMAN_DEFAULT_TCP_PORT_STRING;
  406. struct servent *gearman_servent;
  407. if ((gearman_servent= getservbyname(GEARMAN_DEFAULT_TCP_SERVICE, NULL)))
  408. {
  409. if (gearman_servent and gearman_servent->s_name)
  410. {
  411. service= gearman_servent->s_name;
  412. }
  413. }
  414. _port.clear();
  415. _port.append(service);
  416. }
  417. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "Initializing Gear on port %s with SSL: %s", _port.c_str(), opt_ssl ? "true" : "false");
  418. #if defined(HAVE_SSL) && HAVE_SSL
  419. if (opt_ssl)
  420. {
  421. if (getenv("GEARMAND_CA_CERTIFICATE"))
  422. {
  423. _ssl_ca_file= getenv("GEARMAND_CA_CERTIFICATE");
  424. }
  425. if (getenv("GEARMAND_SERVER_PEM"))
  426. {
  427. _ssl_certificate= getenv("GEARMAND_SERVER_PEM");
  428. }
  429. if (getenv("GEARMAND_SERVER_KEY"))
  430. {
  431. _ssl_key= getenv("GEARMAND_SERVER_KEY");
  432. }
  433. gearmand->init_ssl();
  434. if (SSL_CTX_load_verify_locations(gearmand->ctx_ssl(), _ssl_ca_file.c_str(), 0) != SSL_SUCCESS)
  435. {
  436. gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "SSL_CTX_load_verify_locations() cannot local the ca certificate %s", _ssl_ca_file.c_str());
  437. }
  438. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "Loading CA certificate : %s", _ssl_ca_file.c_str());
  439. if (SSL_CTX_use_certificate_file(gearmand->ctx_ssl(), _ssl_certificate.c_str(), SSL_FILETYPE_PEM) != SSL_SUCCESS)
  440. {
  441. gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "SSL_CTX_use_certificate_file() cannot obtain certificate %s", _ssl_certificate.c_str());
  442. }
  443. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "Loading certificate : %s", _ssl_certificate.c_str());
  444. if (SSL_CTX_use_PrivateKey_file(gearmand->ctx_ssl(), _ssl_key.c_str(), SSL_FILETYPE_PEM) != SSL_SUCCESS)
  445. {
  446. gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "SSL_CTX_use_PrivateKey_file() cannot obtain certificate %s", _ssl_key.c_str());
  447. }
  448. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "Loading certificate key : %s", _ssl_key.c_str());
  449. if (SSL_CTX_check_private_key(gearmand->ctx_ssl()) != SSL_SUCCESS)
  450. {
  451. gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "SSL_CTX_check_private_key() cannot check certificate %s", _ssl_key.c_str());
  452. }
  453. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "Checking certificate key : %s", _ssl_key.c_str());
  454. assert(gearmand->ctx_ssl());
  455. }
  456. #endif
  457. rc= gearmand_port_add(gearmand, _port.c_str(), _gear_con_add, _gear_con_remove);
  458. return rc;
  459. }
  460. } // namespace protocol
  461. } // namespace gearmand