totemconfig.c 19 KB

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