coroparse.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright (c) 2006, 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Patrick Caulfield (pcaulfie@redhat.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 <config.h>
  35. #include <sys/types.h>
  36. #include <sys/uio.h>
  37. #include <sys/socket.h>
  38. #include <sys/stat.h>
  39. #include <sys/un.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <string.h>
  48. #include <dirent.h>
  49. #include <limits.h>
  50. #include <stddef.h>
  51. #include <corosync/lcr/lcr_comp.h>
  52. #include <corosync/engine/objdb.h>
  53. #include <corosync/engine/config.h>
  54. #define LOGSYS_UTILS_ONLY 1
  55. #include <corosync/engine/logsys.h>
  56. #include "util.h"
  57. static int read_config_file_into_objdb(
  58. struct objdb_iface_ver0 *objdb,
  59. const char **error_string);
  60. static char error_string_response[512];
  61. static char *strchr_rs (const char *haystack, int byte)
  62. {
  63. const char *end_address = strchr (haystack, byte);
  64. if (end_address) {
  65. end_address += 1; /* skip past { or = */
  66. end_address += strspn (end_address, " \t");
  67. }
  68. return ((char *) end_address);
  69. }
  70. static int aisparser_readconfig (struct objdb_iface_ver0 *objdb,
  71. const char **error_string)
  72. {
  73. if (read_config_file_into_objdb(objdb, error_string)) {
  74. return -1;
  75. }
  76. return 0;
  77. }
  78. static char *remove_whitespace(char *string)
  79. {
  80. char *start = string+strspn(string, " \t");
  81. char *end = start+(strlen(start))-1;
  82. while ((*end == ' ' || *end == '\t' || *end == ':' || *end == '{') && end > start)
  83. end--;
  84. if (end != start)
  85. *(end+1) = '\0';
  86. return start;
  87. }
  88. #define PCHECK_ADD_SUBSECTION 1
  89. #define PCHECK_ADD_ITEM 2
  90. typedef int (*parser_check_item_f)(struct objdb_iface_ver0 *objdb,
  91. hdb_handle_t parent_handle,
  92. int type,
  93. const char *name,
  94. const char **error_string);
  95. static int parse_section(FILE *fp,
  96. struct objdb_iface_ver0 *objdb,
  97. hdb_handle_t parent_handle,
  98. const char **error_string,
  99. parser_check_item_f parser_check_item_call)
  100. {
  101. char line[512];
  102. int i;
  103. char *loc;
  104. int ignore_line;
  105. while (fgets (line, sizeof (line), fp)) {
  106. if (strlen(line) > 0) {
  107. if (line[strlen(line) - 1] == '\n')
  108. line[strlen(line) - 1] = '\0';
  109. if (strlen (line) > 0 && line[strlen(line) - 1] == '\r')
  110. line[strlen(line) - 1] = '\0';
  111. }
  112. /*
  113. * Clear out white space and tabs
  114. */
  115. for (i = strlen (line) - 1; i > -1; i--) {
  116. if (line[i] == '\t' || line[i] == ' ') {
  117. line[i] = '\0';
  118. } else {
  119. break;
  120. }
  121. }
  122. ignore_line = 1;
  123. for (i = 0; i < strlen (line); i++) {
  124. if (line[i] != '\t' && line[i] != ' ') {
  125. if (line[i] != '#')
  126. ignore_line = 0;
  127. break;
  128. }
  129. }
  130. /*
  131. * Clear out comments and empty lines
  132. */
  133. if (ignore_line) {
  134. continue;
  135. }
  136. /* New section ? */
  137. if ((loc = strchr_rs (line, '{'))) {
  138. hdb_handle_t new_parent;
  139. char *section = remove_whitespace(line);
  140. loc--;
  141. *loc = '\0';
  142. if (parser_check_item_call) {
  143. if (!parser_check_item_call(objdb, parent_handle, PCHECK_ADD_SUBSECTION,
  144. section, error_string))
  145. return -1;
  146. }
  147. objdb->object_create (parent_handle, &new_parent,
  148. section, strlen (section));
  149. if (parse_section(fp, objdb, new_parent, error_string, parser_check_item_call))
  150. return -1;
  151. }
  152. /* New key/value */
  153. if ((loc = strchr_rs (line, ':'))) {
  154. char *key;
  155. char *value;
  156. *(loc-1) = '\0';
  157. key = remove_whitespace(line);
  158. value = remove_whitespace(loc);
  159. if (parser_check_item_call) {
  160. if (!parser_check_item_call(objdb, parent_handle, PCHECK_ADD_ITEM,
  161. key, error_string))
  162. return -1;
  163. }
  164. objdb->object_key_create_typed (parent_handle, key,
  165. value, strlen (value) + 1, OBJDB_VALUETYPE_STRING);
  166. }
  167. if (strchr_rs (line, '}')) {
  168. return 0;
  169. }
  170. }
  171. if (parent_handle != OBJECT_PARENT_HANDLE) {
  172. *error_string = "Missing closing brace";
  173. return -1;
  174. }
  175. return 0;
  176. }
  177. static int parser_check_item_uidgid(struct objdb_iface_ver0 *objdb,
  178. hdb_handle_t parent_handle,
  179. int type,
  180. const char *name,
  181. const char **error_string)
  182. {
  183. if (type == PCHECK_ADD_SUBSECTION) {
  184. if (parent_handle != OBJECT_PARENT_HANDLE) {
  185. *error_string = "uidgid: Can't add second level subsection";
  186. return 0;
  187. }
  188. if (strcmp (name, "uidgid") != 0) {
  189. *error_string = "uidgid: Can't add subsection different then uidgid";
  190. return 0;
  191. }
  192. }
  193. if (type == PCHECK_ADD_ITEM) {
  194. if (!(strcmp (name, "uid") == 0 || strcmp (name, "gid") == 0)) {
  195. *error_string = "uidgid: Only uid and gid are allowed items";
  196. return 0;
  197. }
  198. }
  199. return 1;
  200. }
  201. static int read_uidgid_files_into_objdb(
  202. struct objdb_iface_ver0 *objdb,
  203. const char **error_string)
  204. {
  205. FILE *fp;
  206. const char *dirname;
  207. DIR *dp;
  208. struct dirent *dirent;
  209. struct dirent *entry;
  210. char filename[PATH_MAX + FILENAME_MAX + 1];
  211. int res = 0;
  212. size_t len;
  213. int return_code;
  214. struct stat stat_buf;
  215. dirname = COROSYSCONFDIR "/uidgid.d";
  216. dp = opendir (dirname);
  217. if (dp == NULL)
  218. return 0;
  219. len = offsetof(struct dirent, d_name) + NAME_MAX + 1;
  220. entry = malloc(len);
  221. if (entry == NULL) {
  222. res = 0;
  223. goto error_exit;
  224. }
  225. for (return_code = readdir_r(dp, entry, &dirent);
  226. dirent != NULL && return_code == 0;
  227. return_code = readdir_r(dp, entry, &dirent)) {
  228. snprintf(filename, sizeof (filename), "%s/%s", dirname, dirent->d_name);
  229. stat (filename, &stat_buf);
  230. if (S_ISREG(stat_buf.st_mode)) {
  231. fp = fopen (filename, "r");
  232. if (fp == NULL) continue;
  233. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, parser_check_item_uidgid);
  234. fclose (fp);
  235. if (res != 0) {
  236. goto error_exit;
  237. }
  238. }
  239. }
  240. error_exit:
  241. free (entry);
  242. closedir(dp);
  243. return res;
  244. }
  245. static int read_service_files_into_objdb(
  246. struct objdb_iface_ver0 *objdb,
  247. const char **error_string)
  248. {
  249. FILE *fp;
  250. const char *dirname;
  251. DIR *dp;
  252. struct dirent *dirent;
  253. struct dirent *entry;
  254. char filename[PATH_MAX + FILENAME_MAX + 1];
  255. int res = 0;
  256. struct stat stat_buf;
  257. size_t len;
  258. int return_code;
  259. dirname = COROSYSCONFDIR "/service.d";
  260. dp = opendir (dirname);
  261. if (dp == NULL)
  262. return 0;
  263. len = offsetof(struct dirent, d_name) + NAME_MAX + 1;
  264. entry = malloc(len);
  265. if (entry == NULL) {
  266. res = 0;
  267. goto error_exit;
  268. }
  269. for (return_code = readdir_r(dp, entry, &dirent);
  270. dirent != NULL && return_code == 0;
  271. return_code = readdir_r(dp, entry, &dirent)) {
  272. snprintf(filename, sizeof (filename), "%s/%s", dirname, dirent->d_name);
  273. stat (filename, &stat_buf);
  274. if (S_ISREG(stat_buf.st_mode)) {
  275. fp = fopen (filename, "r");
  276. if (fp == NULL) continue;
  277. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, NULL);
  278. fclose (fp);
  279. if (res != 0) {
  280. goto error_exit;
  281. }
  282. }
  283. }
  284. error_exit:
  285. free (entry);
  286. closedir(dp);
  287. return res;
  288. }
  289. /* Read config file and load into objdb */
  290. static int read_config_file_into_objdb(
  291. struct objdb_iface_ver0 *objdb,
  292. const char **error_string)
  293. {
  294. FILE *fp;
  295. const char *filename;
  296. char *error_reason = error_string_response;
  297. int res;
  298. filename = getenv ("COROSYNC_MAIN_CONFIG_FILE");
  299. if (!filename)
  300. filename = COROSYSCONFDIR "/corosync.conf";
  301. fp = fopen (filename, "r");
  302. if (fp == NULL) {
  303. char error_str[100];
  304. const char *error_ptr;
  305. LOGSYS_STRERROR_R (error_ptr, errno, error_str, sizeof(error_str));
  306. snprintf (error_reason, sizeof(error_string_response),
  307. "Can't read file %s reason = (%s)\n",
  308. filename, error_ptr);
  309. *error_string = error_reason;
  310. return -1;
  311. }
  312. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, NULL);
  313. fclose(fp);
  314. if (res == 0) {
  315. res = read_uidgid_files_into_objdb(objdb, error_string);
  316. }
  317. if (res == 0) {
  318. res = read_service_files_into_objdb(objdb, error_string);
  319. }
  320. if (res == 0) {
  321. snprintf (error_reason, sizeof(error_string_response),
  322. "Successfully read main configuration file '%s'.\n", filename);
  323. *error_string = error_reason;
  324. }
  325. return res;
  326. }
  327. /*
  328. * Dynamic Loader definition
  329. */
  330. struct config_iface_ver0 aisparser_iface_ver0 = {
  331. .config_readconfig = aisparser_readconfig
  332. };
  333. struct lcr_iface corosync_aisparser_ver0[1] = {
  334. {
  335. .name = "corosync_parser",
  336. .version = 0,
  337. .versions_replace = 0,
  338. .versions_replace_count = 0,
  339. .dependencies = 0,
  340. .dependency_count = 0,
  341. .constructor = NULL,
  342. .destructor = NULL,
  343. .interfaces = NULL,
  344. }
  345. };
  346. struct corosync_service_handler *aisparser_get_handler_ver0 (void);
  347. struct lcr_comp aisparser_comp_ver0 = {
  348. .iface_count = 1,
  349. .ifaces = corosync_aisparser_ver0
  350. };
  351. #ifdef COROSYNC_SOLARIS
  352. void corosync_lcr_component_register (void);
  353. void corosync_lcr_component_register (void) {
  354. #else
  355. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  356. #endif
  357. lcr_interfaces_set (&corosync_aisparser_ver0[0], &aisparser_iface_ver0);
  358. lcr_component_register (&aisparser_comp_ver0);
  359. }