coroparse.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/un.h>
  39. #include <netinet/in.h>
  40. #include <arpa/inet.h>
  41. #include <unistd.h>
  42. #include <fcntl.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <errno.h>
  46. #include <string.h>
  47. #include <dirent.h>
  48. #include <corosync/lcr/lcr_comp.h>
  49. #include <corosync/engine/objdb.h>
  50. #include <corosync/engine/config.h>
  51. #include "util.h"
  52. static int read_config_file_into_objdb(
  53. struct objdb_iface_ver0 *objdb,
  54. const char **error_string);
  55. static char error_string_response[512];
  56. static int aisparser_readconfig (struct objdb_iface_ver0 *objdb,
  57. const char **error_string)
  58. {
  59. if (read_config_file_into_objdb(objdb, error_string)) {
  60. return -1;
  61. }
  62. return 0;
  63. }
  64. static char *remove_whitespace(char *string)
  65. {
  66. char *start = string+strspn(string, " \t");
  67. char *end = start+(strlen(start))-1;
  68. while ((*end == ' ' || *end == '\t' || *end == ':' || *end == '{') && end > start)
  69. end--;
  70. if (end != start)
  71. *(end+1) = '\0';
  72. return start;
  73. }
  74. #define PCHECK_ADD_SUBSECTION 1
  75. #define PCHECK_ADD_ITEM 2
  76. typedef int (*parser_check_item_f)(struct objdb_iface_ver0 *objdb,
  77. hdb_handle_t parent_handle,
  78. int type,
  79. const char *name,
  80. const char **error_string);
  81. static int parse_section(FILE *fp,
  82. struct objdb_iface_ver0 *objdb,
  83. hdb_handle_t parent_handle,
  84. const char **error_string,
  85. parser_check_item_f parser_check_item_call)
  86. {
  87. char line[512];
  88. int i;
  89. char *loc;
  90. while (fgets (line, sizeof (line), fp)) {
  91. if (strlen(line) > 0) {
  92. if (line[strlen(line) - 1] == '\n')
  93. line[strlen(line) - 1] = '\0';
  94. if (strlen (line) > 0 && line[strlen(line) - 1] == '\r')
  95. line[strlen(line) - 1] = '\0';
  96. }
  97. /*
  98. * Clear out white space and tabs
  99. */
  100. for (i = strlen (line) - 1; i > -1; i--) {
  101. if (line[i] == '\t' || line[i] == ' ') {
  102. line[i] = '\0';
  103. } else {
  104. break;
  105. }
  106. }
  107. /*
  108. * Clear out comments and empty lines
  109. */
  110. if (line[0] == '#' || line[0] == '\0') {
  111. continue;
  112. }
  113. /* New section ? */
  114. if ((loc = strchr_rs (line, '{'))) {
  115. hdb_handle_t new_parent;
  116. char *section = remove_whitespace(line);
  117. loc--;
  118. *loc = '\0';
  119. if (parser_check_item_call) {
  120. if (!parser_check_item_call(objdb, parent_handle, PCHECK_ADD_SUBSECTION,
  121. section, error_string))
  122. return -1;
  123. }
  124. objdb->object_create (parent_handle, &new_parent,
  125. section, strlen (section));
  126. if (parse_section(fp, objdb, new_parent, error_string, parser_check_item_call))
  127. return -1;
  128. }
  129. /* New key/value */
  130. if ((loc = strchr_rs (line, ':'))) {
  131. char *key;
  132. char *value;
  133. *(loc-1) = '\0';
  134. key = remove_whitespace(line);
  135. value = remove_whitespace(loc);
  136. if (parser_check_item_call) {
  137. if (!parser_check_item_call(objdb, parent_handle, PCHECK_ADD_ITEM,
  138. key, error_string))
  139. return -1;
  140. }
  141. objdb->object_key_create (parent_handle, key,
  142. strlen (key),
  143. value, strlen (value) + 1);
  144. }
  145. if ((loc = strchr_rs (line, '}'))) {
  146. return 0;
  147. }
  148. }
  149. if (parent_handle != OBJECT_PARENT_HANDLE) {
  150. *error_string = "Missing closing brace";
  151. return -1;
  152. }
  153. return 0;
  154. }
  155. static int parser_check_item_uidgid(struct objdb_iface_ver0 *objdb,
  156. hdb_handle_t parent_handle,
  157. int type,
  158. const char *name,
  159. const char **error_string)
  160. {
  161. if (type == PCHECK_ADD_SUBSECTION) {
  162. if (parent_handle != OBJECT_PARENT_HANDLE) {
  163. *error_string = "uidgid: Can't add second level subsection";
  164. return 0;
  165. }
  166. if (strcmp (name, "uidgid") != 0) {
  167. *error_string = "uidgid: Can't add subsection different then uidgid";
  168. return 0;
  169. }
  170. }
  171. if (type == PCHECK_ADD_ITEM) {
  172. if (!(strcmp (name, "uid") == 0 || strcmp (name, "gid") == 0)) {
  173. *error_string = "uidgid: Only uid and gid are allowed items";
  174. return 0;
  175. }
  176. }
  177. return 1;
  178. }
  179. static int read_uidgid_files_into_objdb(
  180. struct objdb_iface_ver0 *objdb,
  181. const char **error_string)
  182. {
  183. FILE *fp;
  184. const char *dirname;
  185. DIR *dp;
  186. struct dirent *dirent;
  187. char filename[PATH_MAX + NAME_MAX + 1];
  188. int res = 0;
  189. dirname = SYSCONFDIR "/corosync/uidgid.d";
  190. dp = opendir (dirname);
  191. if (dp == NULL)
  192. return 0;
  193. while ((dirent = readdir (dp))) {
  194. if (dirent->d_type == DT_REG) {
  195. snprintf(filename, sizeof (filename), "%s/%s", dirname, dirent->d_name);
  196. fp = fopen (filename, "r");
  197. if (fp == NULL) continue;
  198. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, parser_check_item_uidgid);
  199. fclose (fp);
  200. if (res != 0) {
  201. goto error_exit;
  202. }
  203. }
  204. }
  205. error_exit:
  206. closedir(dp);
  207. return res;
  208. }
  209. /* Read config file and load into objdb */
  210. static int read_config_file_into_objdb(
  211. struct objdb_iface_ver0 *objdb,
  212. const char **error_string)
  213. {
  214. FILE *fp;
  215. const char *filename;
  216. char *error_reason = error_string_response;
  217. int res;
  218. filename = getenv ("COROSYNC_MAIN_CONFIG_FILE");
  219. if (!filename)
  220. filename = SYSCONFDIR "/corosync.conf";
  221. fp = fopen (filename, "r");
  222. if (fp == NULL) {
  223. snprintf (error_reason, sizeof(error_string_response),
  224. "Can't read file %s reason = (%s)\n",
  225. filename, strerror (errno));
  226. *error_string = error_reason;
  227. return -1;
  228. }
  229. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, NULL);
  230. fclose(fp);
  231. if (res == 0) {
  232. res = read_uidgid_files_into_objdb(objdb, error_string);
  233. }
  234. if (res == 0) {
  235. snprintf (error_reason, sizeof(error_string_response),
  236. "Successfully read main configuration file '%s'.\n", filename);
  237. *error_string = error_reason;
  238. }
  239. return res;
  240. }
  241. /*
  242. * Dynamic Loader definition
  243. */
  244. struct config_iface_ver0 aisparser_iface_ver0 = {
  245. .config_readconfig = aisparser_readconfig
  246. };
  247. struct lcr_iface corosync_aisparser_ver0[1] = {
  248. {
  249. .name = "corosync_parser",
  250. .version = 0,
  251. .versions_replace = 0,
  252. .versions_replace_count = 0,
  253. .dependencies = 0,
  254. .dependency_count = 0,
  255. .constructor = NULL,
  256. .destructor = NULL,
  257. .interfaces = NULL,
  258. }
  259. };
  260. struct corosync_service_handler *aisparser_get_handler_ver0 (void);
  261. struct lcr_comp aisparser_comp_ver0 = {
  262. .iface_count = 1,
  263. .ifaces = corosync_aisparser_ver0
  264. };
  265. __attribute__ ((constructor)) static void aisparser_comp_register (void) {
  266. lcr_interfaces_set (&corosync_aisparser_ver0[0], &aisparser_iface_ver0);
  267. lcr_component_register (&aisparser_comp_ver0);
  268. }