totemparse.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <assert.h>
  39. #include <unistd.h>
  40. #include <sys/socket.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <fcntl.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include "../include/list.h"
  47. #include "util.h"
  48. #include "totem.h"
  49. #include "totemparse.h"
  50. #include "print.h"
  51. #define LOG_SERVICE LOG_SERVICE_GMI
  52. static char error_string_response[512];
  53. typedef enum {
  54. MAIN_HEAD,
  55. MAIN_TOTEM,
  56. MAIN_TIMEOUT,
  57. } main_parse_t;
  58. static inline char *
  59. strstr_rs (const char *haystack, const char *needle)
  60. {
  61. char *end_address;
  62. char *new_needle;
  63. new_needle = (char *)strdup (needle);
  64. new_needle[strlen(new_needle) - 1] = '\0';
  65. end_address = strstr (haystack, new_needle);
  66. if (end_address) {
  67. end_address += strlen (new_needle);
  68. end_address = strstr (end_address, needle + strlen (new_needle));
  69. }
  70. if (end_address) {
  71. end_address += 1; /* skip past { or = */
  72. do {
  73. if (*end_address == '\t' || *end_address == ' ') {
  74. end_address++;
  75. } else {
  76. break;
  77. }
  78. } while (*end_address != '\0');
  79. }
  80. free (new_needle);
  81. return (end_address);
  82. }
  83. extern int totem_config_read (
  84. struct totem_config *totem_config,
  85. char **error_string,
  86. int interface_max)
  87. {
  88. FILE *fp;
  89. int res = 0;
  90. int line_number = 0;
  91. main_parse_t parse = MAIN_HEAD;
  92. int totem_parsed = 0;
  93. int timeout_parsed = 0;
  94. char *loc;
  95. int i;
  96. int parse_done = 0;
  97. char line[512];
  98. char *error_reason = error_string_response;
  99. memset (totem_config, 0, sizeof (struct totem_config));
  100. totem_config->interfaces = malloc (sizeof (struct totem_interface) * interface_max);
  101. if (totem_config->interfaces == 0) {
  102. parse_done = 1;
  103. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  104. return -1;
  105. }
  106. memset (totem_config->interfaces, 0,
  107. sizeof (struct totem_interface) * interface_max);
  108. totem_config->mcast_addr.sin_family = AF_INET;
  109. fp = fopen ("/etc/ais/openais.conf", "r");
  110. if (fp == 0) {
  111. parse_done = 1;
  112. sprintf (error_reason, "Can't read file reason = (%s)\n", strerror (errno));
  113. *error_string = error_reason;
  114. return -1;
  115. }
  116. while (fgets (line, 255, fp)) {
  117. line_number += 1;
  118. line[strlen(line) - 1] = '\0';
  119. /*
  120. * Clear out white space and tabs
  121. */
  122. for (i = strlen (line) - 1; i > -1; i--) {
  123. if (line[i] == '\t' || line[i] == ' ') {
  124. line[i] = '\0';
  125. } else {
  126. break;
  127. }
  128. }
  129. /*
  130. * Clear out comments and empty lines
  131. */
  132. if (line[0] == '#' || line[0] == '\0') {
  133. continue;
  134. }
  135. line_number += 1;
  136. switch (parse) {
  137. case MAIN_HEAD:
  138. if (totem_parsed == 0 && strstr_rs (line, "network{")) {
  139. totem_parsed = 1;
  140. parse = MAIN_TOTEM;
  141. } else
  142. if (totem_parsed == 0 && strstr_rs (line, "totem{")) {
  143. totem_parsed = 1;
  144. parse = MAIN_TOTEM;
  145. } else
  146. if (timeout_parsed == 0 && strstr_rs (line, "timeout{")) {
  147. timeout_parsed = 1;
  148. parse = MAIN_TIMEOUT;
  149. } else {
  150. continue;
  151. }
  152. break;
  153. case MAIN_TOTEM:
  154. if ((loc = strstr_rs (line, "mcastaddr:"))) {
  155. res = inet_aton (loc, &totem_config->mcast_addr.sin_addr);
  156. } else
  157. if ((loc = strstr_rs (line, "mcastport:"))) {
  158. res = totem_config->mcast_addr.sin_port = htons (atoi (loc));
  159. } else
  160. if ((loc = strstr_rs (line, "bindnetaddr:"))) {
  161. if (interface_max == totem_config->interface_count) {
  162. sprintf (error_reason,
  163. "%d is too many interfaces in /etc/ais/network.conf",
  164. totem_config->interface_count);
  165. goto parse_error;
  166. }
  167. res = inet_aton (loc,
  168. &totem_config->interfaces[totem_config->interface_count].bindnet.sin_addr);
  169. totem_config->interface_count += 1;
  170. } else
  171. if ((loc = strstr_rs (line, "}"))) {
  172. parse = MAIN_HEAD;
  173. res = 1; /* any nonzero is ok */
  174. } else {
  175. goto parse_error;
  176. }
  177. if (res == 0) {
  178. sprintf (error_reason, "invalid network address or port number\n");
  179. goto parse_error;
  180. }
  181. break;
  182. case MAIN_TIMEOUT:
  183. if ((loc = strstr_rs (line, "token:"))) {
  184. totem_config->timeouts[TOTEM_TOKEN]= atoi(loc);
  185. } else if ((loc = strstr_rs (line, "token_retransmit:"))) {
  186. totem_config->timeouts[TOTEM_RETRANSMIT_TOKEN] = atoi(loc);
  187. } else if ((loc = strstr_rs (line, "hold:"))) {
  188. totem_config->timeouts[TOTEM_HOLD_TOKEN] = atoi(loc);
  189. } else if ((loc = strstr_rs (line, "retransmits_before_loss:"))) {
  190. totem_config->timeouts[TOTEM_RETRANSMITS_BEFORE_LOSS] = atoi(loc);
  191. } else if ((loc = strstr_rs (line, "join:"))) {
  192. totem_config->timeouts[TOTEM_JOIN] = atoi(loc);
  193. } else if ((loc = strstr_rs (line, "consensus:"))) {
  194. totem_config->timeouts[TOTEM_CONSENSUS] = atoi(loc);
  195. } else if ((loc = strstr_rs (line, "merge:"))) {
  196. totem_config->timeouts[TOTEM_MERGE] = atoi(loc);
  197. } else if ((loc = strstr_rs (line, "downcheck:"))) {
  198. totem_config->timeouts[TOTEM_DOWNCHECK] = atoi(loc);
  199. } else if ((loc = strstr_rs (line, "fail_recv_const:"))) {
  200. totem_config->timeouts[TOTEM_FAIL_RECV_CONST] = atoi(loc);
  201. } else if ((loc = strstr_rs (line, "}"))) {
  202. parse = MAIN_HEAD;
  203. } else {
  204. goto parse_error;
  205. }
  206. break;
  207. default:
  208. assert (0 == 1); /* SHOULDN'T HAPPEN */
  209. break;
  210. }
  211. }
  212. /*
  213. * Some error checking of parsed data to make sure its valid
  214. */
  215. parse_done = 1;
  216. if (totem_config->mcast_addr.sin_addr.s_addr == 0) {
  217. error_reason = "No multicast address specified";
  218. goto parse_error;
  219. }
  220. if (totem_config->mcast_addr.sin_port == 0) {
  221. error_reason = "No multicast port specified";
  222. goto parse_error;
  223. }
  224. if (totem_config->interface_count == 0) {
  225. error_reason = "No bindnet specified";
  226. goto parse_error;
  227. }
  228. if (parse == MAIN_HEAD) {
  229. fclose (fp);
  230. return (0);
  231. } else {
  232. error_reason = "Missing closing brace";
  233. goto parse_error;
  234. }
  235. parse_error:
  236. if (parse_done) {
  237. sprintf (error_string_response,
  238. "parse error in /etc/ais/openais.conf: %s.\n", error_reason);
  239. } else {
  240. sprintf (error_string_response,
  241. "parse error in /etc/ais/openais.conf: %s (line %d).\n",
  242. error_reason, line_number);
  243. }
  244. *error_string = error_string_response;
  245. fclose (fp);
  246. return (-1);
  247. }
  248. int totemparse_keyread (struct totem_config *totem_config)
  249. {
  250. int fd;
  251. int res;
  252. fd = open ("/etc/ais/authkey", O_RDONLY);
  253. if (fd == -1) {
  254. log_printf (LOG_LEVEL_ERROR, "Could not open /etc/ais/authkey: %s\n", strerror (errno));
  255. return (-1);
  256. }
  257. res = read (fd, totem_config->private_key, 128);
  258. if (res == -1) {
  259. log_printf (LOG_LEVEL_ERROR, "Could not read /etc/ais/authkey: %s\n", strerror (errno));
  260. return (-1);
  261. }
  262. totem_config->private_key_len = 128;
  263. if (res != 128) {
  264. log_printf (LOG_LEVEL_ERROR, "Could only read %d bits of 1024 bits from /etc/ais/authkey.\n", res * 8);
  265. return (-1);
  266. }
  267. close (fd);
  268. return (0);
  269. }