coroparse.c 8.1 KB

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