totemconfig.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <errno.h>
  40. #include <assert.h>
  41. #include <unistd.h>
  42. #include <sys/socket.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <sys/param.h>
  49. #include <corosync/swab.h>
  50. #include <corosync/list.h>
  51. #include <corosync/totem/totem.h>
  52. #include <corosync/engine/objdb.h>
  53. #include <corosync/engine/config.h>
  54. #include <corosync/engine/logsys.h>
  55. #ifdef HAVE_LIBNSS
  56. #include <nss.h>
  57. #include <pk11pub.h>
  58. #include <pkcs11.h>
  59. #include <prerror.h>
  60. #endif
  61. #include "util.h"
  62. #include "totemconfig.h"
  63. #include "tlist.h" /* for HZ */
  64. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  65. #define TOKEN_TIMEOUT 1000
  66. #define TOKEN_RETRANSMIT_TIMEOUT (int)(TOKEN_TIMEOUT / (TOKEN_RETRANSMITS_BEFORE_LOSS_CONST + 0.2))
  67. #define TOKEN_HOLD_TIMEOUT (int)(TOKEN_RETRANSMIT_TIMEOUT * 0.8 - (1000/(int)HZ))
  68. #define JOIN_TIMEOUT 50
  69. #define CONSENSUS_TIMEOUT 800
  70. #define MERGE_TIMEOUT 200
  71. #define DOWNCHECK_TIMEOUT 1000
  72. #define FAIL_TO_RECV_CONST 50
  73. #define SEQNO_UNCHANGED_CONST 30
  74. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  75. #define MAX_NETWORK_DELAY 50
  76. #define WINDOW_SIZE 50
  77. #define MAX_MESSAGES 17
  78. #define RRP_PROBLEM_COUNT_TIMEOUT 2000
  79. #define RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT 10
  80. #define RRP_PROBLEM_COUNT_THRESHOLD_MIN 5
  81. static char error_string_response[512];
  82. static struct objdb_iface_ver0 *global_objdb;
  83. static void add_totem_config_notification(
  84. struct objdb_iface_ver0 *objdb,
  85. struct totem_config *totem_config,
  86. hdb_handle_t totem_object_handle);
  87. /* These just makes the code below a little neater */
  88. static inline int objdb_get_string (
  89. const struct objdb_iface_ver0 *objdb,
  90. hdb_handle_t object_service_handle,
  91. const char *key, const char **value)
  92. {
  93. int res;
  94. *value = NULL;
  95. if ( !(res = objdb->object_key_get (object_service_handle,
  96. key,
  97. strlen (key),
  98. (void *)value,
  99. NULL))) {
  100. if (*value) {
  101. return 0;
  102. }
  103. }
  104. return -1;
  105. }
  106. static inline void objdb_get_int (
  107. const struct objdb_iface_ver0 *objdb,
  108. hdb_handle_t object_service_handle,
  109. const char *key, unsigned int *intvalue)
  110. {
  111. char *value = NULL;
  112. if (!objdb->object_key_get (object_service_handle,
  113. key,
  114. strlen (key),
  115. (void *)&value,
  116. NULL)) {
  117. if (value) {
  118. *intvalue = atoi(value);
  119. }
  120. }
  121. }
  122. static unsigned int totem_handle_find (
  123. struct objdb_iface_ver0 *objdb,
  124. hdb_handle_t *totem_find_handle) {
  125. hdb_handle_t object_find_handle;
  126. unsigned int res;
  127. /*
  128. * Find a network section
  129. */
  130. objdb->object_find_create (
  131. OBJECT_PARENT_HANDLE,
  132. "network",
  133. strlen ("network"),
  134. &object_find_handle);
  135. res = objdb->object_find_next (
  136. object_find_handle,
  137. totem_find_handle);
  138. objdb->object_find_destroy (object_find_handle);
  139. /*
  140. * Network section not found in configuration, checking for totem
  141. */
  142. if (res == -1) {
  143. objdb->object_find_create (
  144. OBJECT_PARENT_HANDLE,
  145. "totem",
  146. strlen ("totem"),
  147. &object_find_handle);
  148. res = objdb->object_find_next (
  149. object_find_handle,
  150. totem_find_handle);
  151. objdb->object_find_destroy (object_find_handle);
  152. }
  153. if (res == -1) {
  154. return (-1);
  155. }
  156. return (0);
  157. }
  158. static void totem_volatile_config_read (
  159. struct objdb_iface_ver0 *objdb,
  160. struct totem_config *totem_config,
  161. hdb_handle_t object_totem_handle)
  162. {
  163. objdb_get_int (objdb,object_totem_handle, "token", &totem_config->token_timeout);
  164. objdb_get_int (objdb,object_totem_handle, "token_retransmit", &totem_config->token_retransmit_timeout);
  165. objdb_get_int (objdb,object_totem_handle, "hold", &totem_config->token_hold_timeout);
  166. objdb_get_int (objdb,object_totem_handle, "token_retransmits_before_loss_const", &totem_config->token_retransmits_before_loss_const);
  167. objdb_get_int (objdb,object_totem_handle, "join", &totem_config->join_timeout);
  168. objdb_get_int (objdb,object_totem_handle, "send_join", &totem_config->send_join_timeout);
  169. objdb_get_int (objdb,object_totem_handle, "consensus", &totem_config->consensus_timeout);
  170. objdb_get_int (objdb,object_totem_handle, "merge", &totem_config->merge_timeout);
  171. objdb_get_int (objdb,object_totem_handle, "downcheck", &totem_config->downcheck_timeout);
  172. objdb_get_int (objdb,object_totem_handle, "fail_recv_const", &totem_config->fail_to_recv_const);
  173. objdb_get_int (objdb,object_totem_handle, "seqno_unchanged_const", &totem_config->seqno_unchanged_const);
  174. objdb_get_int (objdb,object_totem_handle, "rrp_token_expired_timeout", &totem_config->rrp_token_expired_timeout);
  175. objdb_get_int (objdb,object_totem_handle, "rrp_problem_count_timeout", &totem_config->rrp_problem_count_timeout);
  176. objdb_get_int (objdb,object_totem_handle, "rrp_problem_count_threshold", &totem_config->rrp_problem_count_threshold);
  177. objdb_get_int (objdb,object_totem_handle, "heartbeat_failures_allowed", &totem_config->heartbeat_failures_allowed);
  178. objdb_get_int (objdb,object_totem_handle, "max_network_delay", &totem_config->max_network_delay);
  179. objdb_get_int (objdb,object_totem_handle, "window_size", &totem_config->window_size);
  180. objdb_get_string (objdb, object_totem_handle, "vsftype", &totem_config->vsf_type);
  181. objdb_get_int (objdb,object_totem_handle, "max_messages", &totem_config->max_messages);
  182. }
  183. static void totem_get_crypto_type(
  184. const struct objdb_iface_ver0 *objdb,
  185. hdb_handle_t object_totem_handle,
  186. struct totem_config *totem_config)
  187. {
  188. const char *str;
  189. totem_config->crypto_accept = TOTEM_CRYPTO_ACCEPT_OLD;
  190. if (!objdb_get_string (objdb, object_totem_handle, "crypto_accept", &str)) {
  191. if (strcmp(str, "new") == 0) {
  192. totem_config->crypto_accept = TOTEM_CRYPTO_ACCEPT_NEW;
  193. }
  194. }
  195. totem_config->crypto_type = TOTEM_CRYPTO_SOBER;
  196. #ifdef HAVE_LIBNSS
  197. /*
  198. * We must set these even if the key does not exist.
  199. * Encryption type can be set on-the-fly using CFG
  200. */
  201. totem_config->crypto_crypt_type = CKM_AES_CBC_PAD;
  202. totem_config->crypto_sign_type = CKM_SHA256_RSA_PKCS;
  203. #endif
  204. if (!objdb_get_string (objdb, object_totem_handle, "crypto_type", &str)) {
  205. if (strcmp(str, "sober") == 0) {
  206. return;
  207. }
  208. #ifdef HAVE_LIBNSS
  209. if (strcmp(str, "nss") == 0) {
  210. totem_config->crypto_type = TOTEM_CRYPTO_NSS;
  211. }
  212. #endif
  213. }
  214. }
  215. extern int totem_config_read (
  216. struct objdb_iface_ver0 *objdb,
  217. struct totem_config *totem_config,
  218. const char **error_string)
  219. {
  220. int res = 0;
  221. hdb_handle_t object_totem_handle;
  222. hdb_handle_t object_interface_handle;
  223. const char *str;
  224. unsigned int ringnumber = 0;
  225. hdb_handle_t object_find_interface_handle;
  226. res = totem_handle_find (objdb, &object_totem_handle);
  227. if (res == -1) {
  228. printf ("couldn't find totem handle\n");
  229. return (-1);
  230. }
  231. memset (totem_config, 0, sizeof (struct totem_config));
  232. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  233. if (totem_config->interfaces == 0) {
  234. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  235. return -1;
  236. }
  237. memset (totem_config->interfaces, 0,
  238. sizeof (struct totem_interface) * INTERFACE_MAX);
  239. totem_config->secauth = 1;
  240. strcpy (totem_config->rrp_mode, "none");
  241. if (!objdb_get_string (objdb, object_totem_handle, "version", &str)) {
  242. if (strcmp (str, "2") == 0) {
  243. totem_config->version = 2;
  244. }
  245. }
  246. if (!objdb_get_string (objdb, object_totem_handle, "secauth", &str)) {
  247. if (strcmp (str, "on") == 0) {
  248. totem_config->secauth = 1;
  249. }
  250. if (strcmp (str, "off") == 0) {
  251. totem_config->secauth = 0;
  252. }
  253. }
  254. if (totem_config->secauth == 1) {
  255. totem_get_crypto_type(objdb, object_totem_handle, totem_config);
  256. }
  257. if (!objdb_get_string (objdb, object_totem_handle, "rrp_mode", &str)) {
  258. strcpy (totem_config->rrp_mode, str);
  259. }
  260. /*
  261. * Get interface node id
  262. */
  263. objdb_get_int (objdb, object_totem_handle, "nodeid", &totem_config->node_id);
  264. objdb_get_int (objdb,object_totem_handle, "threads", &totem_config->threads);
  265. objdb_get_int (objdb,object_totem_handle, "netmtu", &totem_config->net_mtu);
  266. /*
  267. * Get things that might change in the future
  268. */
  269. totem_volatile_config_read (objdb, totem_config, object_totem_handle);
  270. objdb->object_find_create (
  271. object_totem_handle,
  272. "interface",
  273. strlen ("interface"),
  274. &object_find_interface_handle);
  275. while (objdb->object_find_next (
  276. object_find_interface_handle,
  277. &object_interface_handle) == 0) {
  278. objdb_get_int (objdb, object_interface_handle, "ringnumber", &ringnumber);
  279. /*
  280. * Get interface multicast address
  281. */
  282. if (!objdb_get_string (objdb, object_interface_handle, "mcastaddr", &str)) {
  283. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  284. }
  285. /*
  286. * Get mcast port
  287. */
  288. if (!objdb_get_string (objdb, object_interface_handle, "mcastport", &str)) {
  289. totem_config->interfaces[ringnumber].ip_port = atoi (str);
  290. }
  291. /*
  292. * Get the bind net address
  293. */
  294. if (!objdb_get_string (objdb, object_interface_handle, "bindnetaddr", &str)) {
  295. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  296. totem_config->interfaces[ringnumber].mcast_addr.family);
  297. }
  298. totem_config->interface_count++;
  299. }
  300. objdb->object_find_destroy (object_find_interface_handle);
  301. add_totem_config_notification(objdb, totem_config, object_totem_handle);
  302. return 0;
  303. }
  304. int totem_config_validate (
  305. struct totem_config *totem_config,
  306. const char **error_string)
  307. {
  308. static char local_error_reason[512];
  309. char parse_error[512];
  310. const char *error_reason = local_error_reason;
  311. int i;
  312. unsigned int interface_max = INTERFACE_MAX;
  313. if (totem_config->interface_count == 0) {
  314. error_reason = "No interfaces defined";
  315. goto parse_error;
  316. }
  317. for (i = 0; i < totem_config->interface_count; i++) {
  318. /*
  319. * Some error checking of parsed data to make sure its valid
  320. */
  321. if ((int *)&totem_config->interfaces[i].mcast_addr.addr == 0) {
  322. error_reason = "No multicast address specified";
  323. goto parse_error;
  324. }
  325. if (totem_config->interfaces[i].ip_port == 0) {
  326. error_reason = "No multicast port specified";
  327. goto parse_error;
  328. }
  329. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  330. totem_config->node_id == 0) {
  331. error_reason = "An IPV6 network requires that a node ID be specified.";
  332. goto parse_error;
  333. }
  334. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  335. error_reason = "Multicast address family does not match bind address family";
  336. goto parse_error;
  337. }
  338. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  339. error_reason = "Not all bind address belong to the same IP family";
  340. goto parse_error;
  341. }
  342. }
  343. if (totem_config->version != 2) {
  344. error_reason = "This totem parser can only parse version 2 configurations.";
  345. goto parse_error;
  346. }
  347. if (totem_config->token_retransmits_before_loss_const == 0) {
  348. totem_config->token_retransmits_before_loss_const =
  349. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  350. }
  351. /*
  352. * Setup timeout values that are not setup by user
  353. */
  354. if (totem_config->token_timeout == 0) {
  355. totem_config->token_timeout = TOKEN_TIMEOUT;
  356. if (totem_config->token_retransmits_before_loss_const == 0) {
  357. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  358. }
  359. if (totem_config->token_retransmit_timeout == 0) {
  360. totem_config->token_retransmit_timeout =
  361. (int)(totem_config->token_timeout /
  362. (totem_config->token_retransmits_before_loss_const + 0.2));
  363. }
  364. if (totem_config->token_hold_timeout == 0) {
  365. totem_config->token_hold_timeout =
  366. (int)(totem_config->token_retransmit_timeout * 0.8 -
  367. (1000/HZ));
  368. }
  369. }
  370. if (totem_config->max_network_delay == 0) {
  371. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  372. }
  373. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  374. snprintf (local_error_reason, sizeof(local_error_reason),
  375. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  376. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  377. goto parse_error;
  378. }
  379. if (totem_config->window_size == 0) {
  380. totem_config->window_size = WINDOW_SIZE;
  381. }
  382. if (totem_config->max_messages == 0) {
  383. totem_config->max_messages = MAX_MESSAGES;
  384. }
  385. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  386. snprintf (local_error_reason, sizeof(local_error_reason),
  387. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  388. totem_config->token_timeout, MINIMUM_TIMEOUT);
  389. goto parse_error;
  390. }
  391. if (totem_config->token_retransmit_timeout == 0) {
  392. totem_config->token_retransmit_timeout =
  393. (int)(totem_config->token_timeout /
  394. (totem_config->token_retransmits_before_loss_const + 0.2));
  395. }
  396. if (totem_config->token_hold_timeout == 0) {
  397. totem_config->token_hold_timeout =
  398. (int)(totem_config->token_retransmit_timeout * 0.8 -
  399. (1000/HZ));
  400. }
  401. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  402. snprintf (local_error_reason, sizeof(local_error_reason),
  403. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  404. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  405. goto parse_error;
  406. }
  407. if (totem_config->token_hold_timeout == 0) {
  408. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  409. }
  410. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  411. snprintf (local_error_reason, sizeof(local_error_reason),
  412. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  413. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  414. goto parse_error;
  415. }
  416. if (totem_config->join_timeout == 0) {
  417. totem_config->join_timeout = JOIN_TIMEOUT;
  418. }
  419. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  420. snprintf (local_error_reason, sizeof(local_error_reason),
  421. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  422. totem_config->join_timeout, MINIMUM_TIMEOUT);
  423. goto parse_error;
  424. }
  425. if (totem_config->consensus_timeout == 0) {
  426. totem_config->consensus_timeout = CONSENSUS_TIMEOUT;
  427. }
  428. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  429. snprintf (local_error_reason, sizeof(local_error_reason),
  430. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  431. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  432. goto parse_error;
  433. }
  434. if (totem_config->merge_timeout == 0) {
  435. totem_config->merge_timeout = MERGE_TIMEOUT;
  436. }
  437. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  438. snprintf (local_error_reason, sizeof(local_error_reason),
  439. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  440. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  441. goto parse_error;
  442. }
  443. if (totem_config->downcheck_timeout == 0) {
  444. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  445. }
  446. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  447. snprintf (local_error_reason, sizeof(local_error_reason),
  448. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  449. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  450. goto parse_error;
  451. }
  452. /*
  453. * RRP values validation
  454. */
  455. if (strcmp (totem_config->rrp_mode, "none") &&
  456. strcmp (totem_config->rrp_mode, "active") &&
  457. strcmp (totem_config->rrp_mode, "passive")) {
  458. snprintf (local_error_reason, sizeof(local_error_reason),
  459. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  460. goto parse_error;
  461. }
  462. if (totem_config->rrp_problem_count_timeout == 0) {
  463. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  464. }
  465. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  466. snprintf (local_error_reason, sizeof(local_error_reason),
  467. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  468. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  469. goto parse_error;
  470. }
  471. if (totem_config->rrp_problem_count_threshold == 0) {
  472. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  473. }
  474. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  475. snprintf (local_error_reason, sizeof(local_error_reason),
  476. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  477. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  478. goto parse_error;
  479. }
  480. if (totem_config->rrp_token_expired_timeout == 0) {
  481. totem_config->rrp_token_expired_timeout =
  482. totem_config->token_retransmit_timeout;
  483. }
  484. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  485. snprintf (local_error_reason, sizeof(local_error_reason),
  486. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  487. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  488. goto parse_error;
  489. }
  490. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  491. interface_max = 1;
  492. }
  493. if (interface_max < totem_config->interface_count) {
  494. snprintf (parse_error, sizeof(parse_error),
  495. "%d is too many configured interfaces for the rrp_mode setting %s.",
  496. totem_config->interface_count,
  497. totem_config->rrp_mode);
  498. error_reason = parse_error;
  499. goto parse_error;
  500. }
  501. if (totem_config->fail_to_recv_const == 0) {
  502. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  503. }
  504. if (totem_config->seqno_unchanged_const == 0) {
  505. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  506. }
  507. if (totem_config->net_mtu == 0) {
  508. totem_config->net_mtu = 1500;
  509. }
  510. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  511. snprintf (local_error_reason, sizeof(local_error_reason),
  512. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  513. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  514. goto parse_error;
  515. }
  516. if (totem_config->threads > SEND_THREADS_MAX) {
  517. totem_config->threads = SEND_THREADS_MAX;
  518. }
  519. if (totem_config->secauth == 0) {
  520. totem_config->threads = 0;
  521. }
  522. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  523. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  524. goto parse_error;
  525. }
  526. if (totem_config->vsf_type == NULL) {
  527. totem_config->vsf_type = "none";
  528. }
  529. return (0);
  530. parse_error:
  531. snprintf (error_string_response, sizeof(error_string_response),
  532. "parse error in config: %s\n", error_reason);
  533. *error_string = error_string_response;
  534. return (-1);
  535. }
  536. static int read_keyfile (
  537. const char *key_location,
  538. struct totem_config *totem_config,
  539. const char **error_string)
  540. {
  541. int fd;
  542. int res;
  543. ssize_t expected_key_len = sizeof (totem_config->private_key);
  544. int saved_errno;
  545. fd = open (key_location, O_RDONLY);
  546. if (fd == -1) {
  547. snprintf (error_string_response, sizeof(error_string_response),
  548. "Could not open %s: %s\n",
  549. key_location, strerror (errno));
  550. goto parse_error;
  551. }
  552. res = read (fd, totem_config->private_key, expected_key_len);
  553. saved_errno = errno;
  554. close (fd);
  555. if (res == -1) {
  556. snprintf (error_string_response, sizeof(error_string_response),
  557. "Could not read %s: %s\n",
  558. key_location, strerror (saved_errno));
  559. goto parse_error;
  560. }
  561. totem_config->private_key_len = expected_key_len;
  562. if (res != expected_key_len) {
  563. snprintf (error_string_response, sizeof(error_string_response),
  564. "Could only read %d bits of 1024 bits from %s.\n",
  565. res * 8, key_location);
  566. goto parse_error;
  567. }
  568. return 0;
  569. parse_error:
  570. *error_string = error_string_response;
  571. return (-1);
  572. }
  573. int totem_config_keyread (
  574. struct objdb_iface_ver0 *objdb,
  575. struct totem_config *totem_config,
  576. const char **error_string)
  577. {
  578. int got_key = 0;
  579. const char *key_location = NULL;
  580. hdb_handle_t object_totem_handle;
  581. int res;
  582. memset (totem_config->private_key, 0, 128);
  583. totem_config->private_key_len = 128;
  584. if (totem_config->secauth == 0) {
  585. return (0);
  586. }
  587. res = totem_handle_find (objdb, &object_totem_handle);
  588. if (res == -1) {
  589. return (-1);
  590. }
  591. /* objdb may store the location of the key file */
  592. if (!objdb_get_string (objdb,object_totem_handle, "keyfile", &key_location)
  593. && key_location) {
  594. res = read_keyfile(key_location, totem_config, error_string);
  595. if (res) {
  596. goto key_error;
  597. }
  598. got_key = 1;
  599. } else { /* Or the key itself may be in the objdb */
  600. char *key = NULL;
  601. size_t key_len;
  602. res = objdb->object_key_get (object_totem_handle,
  603. "key",
  604. strlen ("key"),
  605. (void *)&key,
  606. &key_len);
  607. if (res == 0 && key) {
  608. if (key_len > sizeof (totem_config->private_key)) {
  609. goto key_error;
  610. }
  611. memcpy(totem_config->private_key, key, key_len);
  612. totem_config->private_key_len = key_len;
  613. got_key = 1;
  614. }
  615. }
  616. /* In desperation we read the default filename */
  617. if (!got_key) {
  618. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  619. if (!filename)
  620. filename = SYSCONFDIR "/ais/authkey";
  621. res = read_keyfile(filename, totem_config, error_string);
  622. if (res)
  623. goto key_error;
  624. }
  625. return (0);
  626. *error_string = error_string_response;
  627. key_error:
  628. return (-1);
  629. }
  630. static void totem_key_change_notify(object_change_type_t change_type,
  631. hdb_handle_t parent_object_handle,
  632. hdb_handle_t object_handle,
  633. const void *object_name_pt, size_t object_name_len,
  634. const void *key_name_pt, size_t key_len,
  635. const void *key_value_pt, size_t key_value_len,
  636. void *priv_data_pt)
  637. {
  638. struct totem_config *totem_config = priv_data_pt;
  639. if (memcmp(object_name_pt, "totem", object_name_len) == 0)
  640. totem_volatile_config_read(global_objdb,
  641. totem_config,
  642. object_handle); // CHECK
  643. }
  644. static void totem_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  645. void *priv_data_pt)
  646. {
  647. struct totem_config *totem_config = priv_data_pt;
  648. hdb_handle_t totem_object_handle;
  649. /*
  650. * A new totem {} key might exist, cancel the
  651. * existing notification at the start of reload,
  652. * and start a new one on the new object when
  653. * it's all settled.
  654. */
  655. if (type == OBJDB_RELOAD_NOTIFY_START) {
  656. global_objdb->object_track_stop(
  657. totem_key_change_notify,
  658. NULL,
  659. NULL,
  660. NULL,
  661. NULL);
  662. }
  663. if (type == OBJDB_RELOAD_NOTIFY_END ||
  664. type == OBJDB_RELOAD_NOTIFY_FAILED) {
  665. if (!totem_handle_find(global_objdb,
  666. &totem_object_handle)) {
  667. add_totem_config_notification(global_objdb, totem_config, totem_object_handle);
  668. /*
  669. * Reload the configuration
  670. */
  671. totem_volatile_config_read(global_objdb,
  672. totem_config,
  673. totem_object_handle);
  674. }
  675. else {
  676. log_printf(LOGSYS_LEVEL_ERROR, "totem objdb tracking stopped, cannot find totem{} handle on objdb\n");
  677. }
  678. }
  679. }
  680. static void add_totem_config_notification(
  681. struct objdb_iface_ver0 *objdb,
  682. struct totem_config *totem_config,
  683. hdb_handle_t totem_object_handle)
  684. {
  685. global_objdb = objdb;
  686. objdb->object_track_start(totem_object_handle,
  687. 1,
  688. totem_key_change_notify,
  689. NULL, // object_create_notify,
  690. NULL, // object_destroy_notify,
  691. NULL, // object_reload_notify
  692. totem_config); // priv_data
  693. /*
  694. * Reload notify must be on the parent object
  695. */
  696. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  697. 1,
  698. NULL, // key_change_notify,
  699. NULL, // object_create_notify,
  700. NULL, // object_destroy_notify,
  701. totem_objdb_reload_notify, // object_reload_notify
  702. totem_config); // priv_data
  703. }