totemconfig.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2008 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 <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <errno.h>
  39. #include <assert.h>
  40. #include <unistd.h>
  41. #include <sys/socket.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <sys/param.h>
  48. #include "swab.h"
  49. #include "list.h"
  50. #include "util.h"
  51. #include "totem.h"
  52. #include "totemconfig.h"
  53. #include "logsys.h"
  54. #include "objdb.h"
  55. #include "tlist.h" /* for HZ */
  56. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  57. #define TOKEN_TIMEOUT 1000
  58. #define TOKEN_RETRANSMIT_TIMEOUT (int)(TOKEN_TIMEOUT / (TOKEN_RETRANSMITS_BEFORE_LOSS_CONST + 0.2))
  59. #define TOKEN_HOLD_TIMEOUT (int)(TOKEN_RETRANSMIT_TIMEOUT * 0.8 - (1000/(int)HZ))
  60. #define JOIN_TIMEOUT 50
  61. #define CONSENSUS_TIMEOUT 800
  62. #define MERGE_TIMEOUT 200
  63. #define DOWNCHECK_TIMEOUT 1000
  64. #define FAIL_TO_RECV_CONST 50
  65. #define SEQNO_UNCHANGED_CONST 30
  66. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  67. #define MAX_NETWORK_DELAY 50
  68. #define WINDOW_SIZE 50
  69. #define MAX_MESSAGES 17
  70. #define RRP_PROBLEM_COUNT_TIMEOUT 2000
  71. #define RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT 10
  72. #define RRP_PROBLEM_COUNT_THRESHOLD_MIN 5
  73. static char error_string_response[512];
  74. /* These just makes the code below a little neater */
  75. static inline int objdb_get_string (
  76. struct objdb_iface_ver0 *objdb,
  77. unsigned int object_service_handle,
  78. char *key, char **value)
  79. {
  80. int res;
  81. *value = NULL;
  82. if ( !(res = objdb->object_key_get (object_service_handle,
  83. key,
  84. strlen (key),
  85. (void *)value,
  86. NULL))) {
  87. if (*value) {
  88. return 0;
  89. }
  90. }
  91. return -1;
  92. }
  93. static inline void objdb_get_int (
  94. struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
  95. char *key, unsigned int *intvalue)
  96. {
  97. char *value = NULL;
  98. if (!objdb->object_key_get (object_service_handle,
  99. key,
  100. strlen (key),
  101. (void *)&value,
  102. NULL)) {
  103. if (value) {
  104. *intvalue = atoi(value);
  105. }
  106. }
  107. }
  108. static unsigned int totem_handle_find (
  109. struct objdb_iface_ver0 *objdb,
  110. unsigned int *totem_find_handle) {
  111. unsigned int object_find_handle;
  112. unsigned int res;
  113. /*
  114. * Find a network section
  115. */
  116. objdb->object_find_create (
  117. OBJECT_PARENT_HANDLE,
  118. "network",
  119. strlen ("network"),
  120. &object_find_handle);
  121. res = objdb->object_find_next (
  122. object_find_handle,
  123. totem_find_handle);
  124. objdb->object_find_destroy (object_find_handle);
  125. /*
  126. * Network section not found in configuration, checking for totem
  127. */
  128. if (res == -1) {
  129. objdb->object_find_create (
  130. OBJECT_PARENT_HANDLE,
  131. "totem",
  132. strlen ("totem"),
  133. &object_find_handle);
  134. res = objdb->object_find_next (
  135. object_find_handle,
  136. totem_find_handle);
  137. objdb->object_find_destroy (object_find_handle);
  138. }
  139. if (res == -1) {
  140. return (-1);
  141. }
  142. return (0);
  143. }
  144. extern int totem_config_read (
  145. struct objdb_iface_ver0 *objdb,
  146. struct totem_config *totem_config,
  147. char **error_string)
  148. {
  149. int res = 0;
  150. unsigned int object_totem_handle;
  151. unsigned int object_interface_handle;
  152. char *str;
  153. unsigned int ringnumber = 0;
  154. unsigned int object_find_interface_handle;
  155. res = totem_handle_find (objdb, &object_totem_handle);
  156. if (res == -1) {
  157. printf ("couldn't find totem handle\n");
  158. return (-1);
  159. }
  160. memset (totem_config, 0, sizeof (struct totem_config));
  161. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  162. if (totem_config->interfaces == 0) {
  163. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  164. return -1;
  165. }
  166. memset (totem_config->interfaces, 0,
  167. sizeof (struct totem_interface) * INTERFACE_MAX);
  168. totem_config->secauth = 1;
  169. strcpy (totem_config->rrp_mode, "none");
  170. if (!objdb_get_string (objdb, object_totem_handle, "version", &str)) {
  171. if (strcmp (str, "2") == 0) {
  172. totem_config->version = 2;
  173. }
  174. }
  175. if (!objdb_get_string (objdb, object_totem_handle, "secauth", &str)) {
  176. if (strcmp (str, "on") == 0) {
  177. totem_config->secauth = 1;
  178. }
  179. if (strcmp (str, "off") == 0) {
  180. totem_config->secauth = 0;
  181. }
  182. }
  183. if (!objdb_get_string (objdb, object_totem_handle, "rrp_mode", &str)) {
  184. strcpy (totem_config->rrp_mode, str);
  185. }
  186. /*
  187. * Get interface node id
  188. */
  189. objdb_get_int (objdb, object_totem_handle, "nodeid", &totem_config->node_id);
  190. objdb_get_int (objdb,object_totem_handle, "threads", &totem_config->threads);
  191. objdb_get_int (objdb,object_totem_handle, "netmtu", &totem_config->net_mtu);
  192. objdb_get_int (objdb,object_totem_handle, "token", &totem_config->token_timeout);
  193. objdb_get_int (objdb,object_totem_handle, "token_retransmit", &totem_config->token_retransmit_timeout);
  194. objdb_get_int (objdb,object_totem_handle, "hold", &totem_config->token_hold_timeout);
  195. objdb_get_int (objdb,object_totem_handle, "token_retransmits_before_loss_const", &totem_config->token_retransmits_before_loss_const);
  196. objdb_get_int (objdb,object_totem_handle, "join", &totem_config->join_timeout);
  197. objdb_get_int (objdb,object_totem_handle, "send_join", &totem_config->send_join_timeout);
  198. objdb_get_int (objdb,object_totem_handle, "consensus", &totem_config->consensus_timeout);
  199. objdb_get_int (objdb,object_totem_handle, "merge", &totem_config->merge_timeout);
  200. objdb_get_int (objdb,object_totem_handle, "downcheck", &totem_config->downcheck_timeout);
  201. objdb_get_int (objdb,object_totem_handle, "fail_recv_const", &totem_config->fail_to_recv_const);
  202. objdb_get_int (objdb,object_totem_handle, "seqno_unchanged_const", &totem_config->seqno_unchanged_const);
  203. objdb_get_int (objdb,object_totem_handle, "rrp_token_expired_timeout", &totem_config->rrp_token_expired_timeout);
  204. objdb_get_int (objdb,object_totem_handle, "rrp_problem_count_timeout", &totem_config->rrp_problem_count_timeout);
  205. objdb_get_int (objdb,object_totem_handle, "rrp_problem_count_threshold", &totem_config->rrp_problem_count_threshold);
  206. objdb_get_int (objdb,object_totem_handle, "heartbeat_failures_allowed", &totem_config->heartbeat_failures_allowed);
  207. objdb_get_int (objdb,object_totem_handle, "max_network_delay", &totem_config->max_network_delay);
  208. objdb_get_int (objdb,object_totem_handle, "window_size", &totem_config->window_size);
  209. objdb_get_string (objdb, object_totem_handle, "vsftype", &totem_config->vsf_type);
  210. objdb_get_int (objdb,object_totem_handle, "max_messages", &totem_config->max_messages);
  211. objdb->object_find_create (
  212. object_totem_handle,
  213. "interface",
  214. strlen ("interface"),
  215. &object_find_interface_handle);
  216. while (objdb->object_find_next (
  217. object_find_interface_handle,
  218. &object_interface_handle) == 0) {
  219. objdb_get_int (objdb, object_interface_handle, "ringnumber", &ringnumber);
  220. /*
  221. * Get interface multicast address
  222. */
  223. if (!objdb_get_string (objdb, object_interface_handle, "mcastaddr", &str)) {
  224. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  225. }
  226. /*
  227. * Get mcast port
  228. */
  229. if (!objdb_get_string (objdb, object_interface_handle, "mcastport", &str)) {
  230. totem_config->interfaces[ringnumber].ip_port = atoi (str);
  231. }
  232. /*
  233. * Get the bind net address
  234. */
  235. if (!objdb_get_string (objdb, object_interface_handle, "bindnetaddr", &str)) {
  236. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  237. totem_config->interfaces[ringnumber].mcast_addr.family);
  238. }
  239. totem_config->interface_count++;
  240. }
  241. objdb->object_find_destroy (object_find_interface_handle);
  242. return 0;
  243. }
  244. int totem_config_validate (
  245. struct totem_config *totem_config,
  246. char **error_string)
  247. {
  248. static char local_error_reason[512];
  249. char parse_error[512];
  250. char *error_reason = local_error_reason;
  251. int i;
  252. unsigned int interface_max = INTERFACE_MAX;
  253. if (totem_config->interface_count == 0) {
  254. error_reason = "No interfaces defined";
  255. goto parse_error;
  256. }
  257. for (i = 0; i < totem_config->interface_count; i++) {
  258. /*
  259. * Some error checking of parsed data to make sure its valid
  260. */
  261. if ((int *)&totem_config->interfaces[i].mcast_addr.addr == 0) {
  262. error_reason = "No multicast address specified";
  263. goto parse_error;
  264. }
  265. if (totem_config->interfaces[i].ip_port == 0) {
  266. error_reason = "No multicast port specified";
  267. goto parse_error;
  268. }
  269. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  270. totem_config->node_id == 0) {
  271. error_reason = "An IPV6 network requires that a node ID be specified.";
  272. goto parse_error;
  273. }
  274. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  275. error_reason = "Multicast address family does not match bind address family";
  276. goto parse_error;
  277. }
  278. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  279. error_reason = "Not all bind address belong to the same IP family";
  280. goto parse_error;
  281. }
  282. }
  283. if (totem_config->version != 2) {
  284. error_reason = "This totem parser can only parse version 2 configurations.";
  285. goto parse_error;
  286. }
  287. if (totem_config->token_retransmits_before_loss_const == 0) {
  288. totem_config->token_retransmits_before_loss_const =
  289. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  290. }
  291. /*
  292. * Setup timeout values that are not setup by user
  293. */
  294. if (totem_config->token_timeout == 0) {
  295. totem_config->token_timeout = TOKEN_TIMEOUT;
  296. if (totem_config->token_retransmits_before_loss_const == 0) {
  297. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  298. }
  299. if (totem_config->token_retransmit_timeout == 0) {
  300. totem_config->token_retransmit_timeout =
  301. (int)(totem_config->token_timeout /
  302. (totem_config->token_retransmits_before_loss_const + 0.2));
  303. }
  304. if (totem_config->token_hold_timeout == 0) {
  305. totem_config->token_hold_timeout =
  306. (int)(totem_config->token_retransmit_timeout * 0.8 -
  307. (1000/HZ));
  308. }
  309. }
  310. if (totem_config->max_network_delay == 0) {
  311. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  312. }
  313. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  314. sprintf (local_error_reason, "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  315. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  316. goto parse_error;
  317. }
  318. if (totem_config->window_size == 0) {
  319. totem_config->window_size = WINDOW_SIZE;
  320. }
  321. if (totem_config->max_messages == 0) {
  322. totem_config->max_messages = MAX_MESSAGES;
  323. }
  324. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  325. sprintf (local_error_reason, "The token timeout parameter (%d ms) may not be less then (%d ms).",
  326. totem_config->token_timeout, MINIMUM_TIMEOUT);
  327. goto parse_error;
  328. }
  329. if (totem_config->token_retransmit_timeout == 0) {
  330. totem_config->token_retransmit_timeout =
  331. (int)(totem_config->token_timeout /
  332. (totem_config->token_retransmits_before_loss_const + 0.2));
  333. }
  334. if (totem_config->token_hold_timeout == 0) {
  335. totem_config->token_hold_timeout =
  336. (int)(totem_config->token_retransmit_timeout * 0.8 -
  337. (1000/HZ));
  338. }
  339. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  340. sprintf (local_error_reason, "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  341. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  342. goto parse_error;
  343. }
  344. if (totem_config->token_hold_timeout == 0) {
  345. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  346. }
  347. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  348. sprintf (local_error_reason, "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  349. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  350. goto parse_error;
  351. }
  352. if (totem_config->join_timeout == 0) {
  353. totem_config->join_timeout = JOIN_TIMEOUT;
  354. }
  355. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  356. sprintf (local_error_reason, "The join timeout parameter (%d ms) may not be less then (%d ms).",
  357. totem_config->join_timeout, MINIMUM_TIMEOUT);
  358. goto parse_error;
  359. }
  360. if (totem_config->consensus_timeout == 0) {
  361. totem_config->consensus_timeout = CONSENSUS_TIMEOUT;
  362. }
  363. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  364. sprintf (local_error_reason, "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  365. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  366. goto parse_error;
  367. }
  368. if (totem_config->merge_timeout == 0) {
  369. totem_config->merge_timeout = MERGE_TIMEOUT;
  370. }
  371. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  372. sprintf (local_error_reason, "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  373. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  374. goto parse_error;
  375. }
  376. if (totem_config->downcheck_timeout == 0) {
  377. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  378. }
  379. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  380. sprintf (local_error_reason, "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  381. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  382. goto parse_error;
  383. }
  384. /*
  385. * RRP values validation
  386. */
  387. if (strcmp (totem_config->rrp_mode, "none") &&
  388. strcmp (totem_config->rrp_mode, "active") &&
  389. strcmp (totem_config->rrp_mode, "passive")) {
  390. sprintf (local_error_reason, "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  391. goto parse_error;
  392. }
  393. if (totem_config->rrp_problem_count_timeout == 0) {
  394. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  395. }
  396. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  397. sprintf (local_error_reason, "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  398. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  399. goto parse_error;
  400. }
  401. if (totem_config->rrp_problem_count_threshold == 0) {
  402. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  403. }
  404. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  405. sprintf (local_error_reason, "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  406. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  407. goto parse_error;
  408. }
  409. if (totem_config->rrp_token_expired_timeout == 0) {
  410. totem_config->rrp_token_expired_timeout =
  411. totem_config->token_retransmit_timeout;
  412. }
  413. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  414. sprintf (local_error_reason, "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  415. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  416. goto parse_error;
  417. }
  418. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  419. interface_max = 1;
  420. }
  421. if (interface_max < totem_config->interface_count) {
  422. sprintf (parse_error,
  423. "%d is too many configured interfaces for the rrp_mode setting %s.",
  424. totem_config->interface_count,
  425. totem_config->rrp_mode);
  426. error_reason = parse_error;
  427. goto parse_error;
  428. }
  429. if (totem_config->fail_to_recv_const == 0) {
  430. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  431. }
  432. if (totem_config->seqno_unchanged_const == 0) {
  433. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  434. }
  435. if (totem_config->net_mtu == 0) {
  436. totem_config->net_mtu = 1500;
  437. }
  438. if ((MESSAGE_SIZE_MAX / totem_config->net_mtu) < totem_config->max_messages) {
  439. sprintf (local_error_reason, "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  440. totem_config->max_messages, MESSAGE_SIZE_MAX / totem_config->net_mtu);
  441. goto parse_error;
  442. }
  443. if (totem_config->threads > SEND_THREADS_MAX) {
  444. totem_config->threads = SEND_THREADS_MAX;
  445. }
  446. if (totem_config->secauth == 0) {
  447. totem_config->threads = 0;
  448. }
  449. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  450. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  451. goto parse_error;
  452. }
  453. if (totem_config->vsf_type == NULL) {
  454. totem_config->vsf_type = "none";
  455. }
  456. return (0);
  457. parse_error:
  458. sprintf (error_string_response,
  459. "parse error in config: %s\n", error_reason);
  460. *error_string = error_string_response;
  461. return (-1);
  462. }
  463. static int read_keyfile (
  464. char *key_location,
  465. struct totem_config *totem_config,
  466. char **error_string)
  467. {
  468. int fd;
  469. int res;
  470. fd = open (key_location, O_RDONLY);
  471. if (fd == -1) {
  472. sprintf (error_string_response, "Could not open %s: %s\n",
  473. key_location, strerror (errno));
  474. goto parse_error;
  475. }
  476. res = read (fd, totem_config->private_key, 128);
  477. if (res == -1) {
  478. close (fd);
  479. sprintf (error_string_response, "Could not read %s: %s\n",
  480. key_location, strerror (errno));
  481. goto parse_error;
  482. }
  483. totem_config->private_key_len = 128;
  484. if (res != 128) {
  485. close (fd);
  486. sprintf (error_string_response, "Could only read %d bits of 1024 bits from %s.\n",
  487. res * 8, key_location);
  488. goto parse_error;
  489. }
  490. return 0;
  491. parse_error:
  492. *error_string = error_string_response;
  493. return (-1);
  494. }
  495. int totem_config_keyread (
  496. struct objdb_iface_ver0 *objdb,
  497. struct totem_config *totem_config,
  498. char **error_string)
  499. {
  500. int got_key = 0;
  501. char *key_location = NULL;
  502. unsigned int object_totem_handle;
  503. int res;
  504. memset (totem_config->private_key, 0, 128);
  505. totem_config->private_key_len = 128;
  506. if (totem_config->secauth == 0) {
  507. return (0);
  508. }
  509. res = totem_handle_find (objdb, &object_totem_handle);
  510. if (res == -1) {
  511. return (-1);
  512. }
  513. /* objdb may store the location of the key file */
  514. if (!objdb_get_string (objdb,object_totem_handle, "keyfile", &key_location)
  515. && key_location) {
  516. res = read_keyfile(key_location, totem_config, error_string);
  517. if (res) {
  518. goto key_error;
  519. }
  520. got_key = 1;
  521. } else { /* Or the key itself may be in the objdb */
  522. char *key = NULL;
  523. int key_len;
  524. res = objdb->object_key_get (object_totem_handle,
  525. "key",
  526. strlen ("key"),
  527. (void *)&key,
  528. &key_len);
  529. if (res == 0 && key) {
  530. memcpy(totem_config->private_key, key, key_len);
  531. totem_config->private_key_len = key_len;
  532. got_key = 1;
  533. }
  534. }
  535. /* In desperation we read the default filename */
  536. if (!got_key) {
  537. char *filename = getenv("OPENAIS_TOTEM_AUTHKEY_FILE");
  538. if (!filename)
  539. filename = "/etc/ais/authkey";
  540. res = read_keyfile(filename, totem_config, error_string);
  541. if (res)
  542. goto key_error;
  543. }
  544. return (0);
  545. *error_string = error_string_response;
  546. key_error:
  547. return (-1);
  548. }