coroparse.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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, int remove_colon_and_brace)
  79. {
  80. char *start = string+strspn(string, " \t");
  81. char *end = start+(strlen(start))-1;
  82. while ((*end == ' ' || *end == '\t' || (remove_colon_and_brace && (*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. int depth,
  100. parser_check_item_f parser_check_item_call)
  101. {
  102. char line[512];
  103. int i;
  104. char *loc;
  105. int ignore_line;
  106. while (fgets (line, sizeof (line), fp)) {
  107. if (strlen(line) > 0) {
  108. if (line[strlen(line) - 1] == '\n')
  109. line[strlen(line) - 1] = '\0';
  110. if (strlen (line) > 0 && line[strlen(line) - 1] == '\r')
  111. line[strlen(line) - 1] = '\0';
  112. }
  113. /*
  114. * Clear out white space and tabs
  115. */
  116. for (i = strlen (line) - 1; i > -1; i--) {
  117. if (line[i] == '\t' || line[i] == ' ') {
  118. line[i] = '\0';
  119. } else {
  120. break;
  121. }
  122. }
  123. ignore_line = 1;
  124. for (i = 0; i < strlen (line); i++) {
  125. if (line[i] != '\t' && line[i] != ' ') {
  126. if (line[i] != '#')
  127. ignore_line = 0;
  128. break;
  129. }
  130. }
  131. /*
  132. * Clear out comments and empty lines
  133. */
  134. if (ignore_line) {
  135. continue;
  136. }
  137. /* New section ? */
  138. if ((loc = strchr_rs (line, '{'))) {
  139. hdb_handle_t new_parent;
  140. char *section = remove_whitespace(line, 1);
  141. loc--;
  142. *loc = '\0';
  143. if (parser_check_item_call) {
  144. if (!parser_check_item_call(objdb, parent_handle, PCHECK_ADD_SUBSECTION,
  145. section, error_string))
  146. return -1;
  147. }
  148. objdb->object_create (parent_handle, &new_parent,
  149. section, strlen (section));
  150. if (parse_section(fp, objdb, new_parent, error_string, depth + 1, parser_check_item_call))
  151. return -1;
  152. continue ;
  153. }
  154. /* New key/value */
  155. if ((loc = strchr_rs (line, ':'))) {
  156. char *key;
  157. char *value;
  158. *(loc-1) = '\0';
  159. key = remove_whitespace(line, 1);
  160. value = remove_whitespace(loc, 0);
  161. if (parser_check_item_call) {
  162. if (!parser_check_item_call(objdb, parent_handle, PCHECK_ADD_ITEM,
  163. key, error_string))
  164. return -1;
  165. }
  166. objdb->object_key_create_typed (parent_handle, key,
  167. value, strlen (value) + 1, OBJDB_VALUETYPE_STRING);
  168. continue ;
  169. }
  170. if (strchr_rs (line, '}')) {
  171. if (depth == 0) {
  172. *error_string = "parser error: Unexpected closing brace";
  173. return -1;
  174. }
  175. return 0;
  176. }
  177. }
  178. if (parent_handle != OBJECT_PARENT_HANDLE) {
  179. *error_string = "parser error: Missing closing brace";
  180. return -1;
  181. }
  182. return 0;
  183. }
  184. static int parser_check_item_uidgid(struct objdb_iface_ver0 *objdb,
  185. hdb_handle_t parent_handle,
  186. int type,
  187. const char *name,
  188. const char **error_string)
  189. {
  190. if (type == PCHECK_ADD_SUBSECTION) {
  191. if (parent_handle != OBJECT_PARENT_HANDLE) {
  192. *error_string = "uidgid: Can't add second level subsection";
  193. return 0;
  194. }
  195. if (strcmp (name, "uidgid") != 0) {
  196. *error_string = "uidgid: Can't add subsection different then uidgid";
  197. return 0;
  198. }
  199. }
  200. if (type == PCHECK_ADD_ITEM) {
  201. if (!(strcmp (name, "uid") == 0 || strcmp (name, "gid") == 0)) {
  202. *error_string = "uidgid: Only uid and gid are allowed items";
  203. return 0;
  204. }
  205. }
  206. return 1;
  207. }
  208. static int read_uidgid_files_into_objdb(
  209. struct objdb_iface_ver0 *objdb,
  210. const char **error_string)
  211. {
  212. FILE *fp;
  213. const char *dirname;
  214. DIR *dp;
  215. struct dirent *dirent;
  216. struct dirent *entry;
  217. char filename[PATH_MAX + FILENAME_MAX + 1];
  218. int res = 0;
  219. size_t len;
  220. int return_code;
  221. struct stat stat_buf;
  222. dirname = COROSYSCONFDIR "/uidgid.d";
  223. dp = opendir (dirname);
  224. if (dp == NULL)
  225. return 0;
  226. len = offsetof(struct dirent, d_name) + NAME_MAX + 1;
  227. entry = malloc(len);
  228. if (entry == NULL) {
  229. res = 0;
  230. goto error_exit;
  231. }
  232. for (return_code = readdir_r(dp, entry, &dirent);
  233. dirent != NULL && return_code == 0;
  234. return_code = readdir_r(dp, entry, &dirent)) {
  235. snprintf(filename, sizeof (filename), "%s/%s", dirname, dirent->d_name);
  236. stat (filename, &stat_buf);
  237. if (S_ISREG(stat_buf.st_mode)) {
  238. fp = fopen (filename, "r");
  239. if (fp == NULL) continue;
  240. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, 0, parser_check_item_uidgid);
  241. fclose (fp);
  242. if (res != 0) {
  243. goto error_exit;
  244. }
  245. }
  246. }
  247. error_exit:
  248. free (entry);
  249. closedir(dp);
  250. return res;
  251. }
  252. static int read_service_files_into_objdb(
  253. struct objdb_iface_ver0 *objdb,
  254. const char **error_string)
  255. {
  256. FILE *fp;
  257. const char *dirname;
  258. DIR *dp;
  259. struct dirent *dirent;
  260. struct dirent *entry;
  261. char filename[PATH_MAX + FILENAME_MAX + 1];
  262. int res = 0;
  263. struct stat stat_buf;
  264. size_t len;
  265. int return_code;
  266. dirname = COROSYSCONFDIR "/service.d";
  267. dp = opendir (dirname);
  268. if (dp == NULL)
  269. return 0;
  270. len = offsetof(struct dirent, d_name) + NAME_MAX + 1;
  271. entry = malloc(len);
  272. if (entry == NULL) {
  273. res = 0;
  274. goto error_exit;
  275. }
  276. for (return_code = readdir_r(dp, entry, &dirent);
  277. dirent != NULL && return_code == 0;
  278. return_code = readdir_r(dp, entry, &dirent)) {
  279. snprintf(filename, sizeof (filename), "%s/%s", dirname, dirent->d_name);
  280. stat (filename, &stat_buf);
  281. if (S_ISREG(stat_buf.st_mode)) {
  282. fp = fopen (filename, "r");
  283. if (fp == NULL) continue;
  284. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, 0, NULL);
  285. fclose (fp);
  286. if (res != 0) {
  287. goto error_exit;
  288. }
  289. }
  290. }
  291. error_exit:
  292. free (entry);
  293. closedir(dp);
  294. return res;
  295. }
  296. /* Read config file and load into objdb */
  297. static int read_config_file_into_objdb(
  298. struct objdb_iface_ver0 *objdb,
  299. const char **error_string)
  300. {
  301. FILE *fp;
  302. const char *filename;
  303. char *error_reason = error_string_response;
  304. int res;
  305. filename = getenv ("COROSYNC_MAIN_CONFIG_FILE");
  306. if (!filename)
  307. filename = COROSYSCONFDIR "/corosync.conf";
  308. fp = fopen (filename, "r");
  309. if (fp == NULL) {
  310. char error_str[100];
  311. const char *error_ptr;
  312. LOGSYS_STRERROR_R (error_ptr, errno, error_str, sizeof(error_str));
  313. snprintf (error_reason, sizeof(error_string_response),
  314. "Can't read file %s reason = (%s)\n",
  315. filename, error_ptr);
  316. *error_string = error_reason;
  317. return -1;
  318. }
  319. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, 0, NULL);
  320. fclose(fp);
  321. if (res == 0) {
  322. res = read_uidgid_files_into_objdb(objdb, error_string);
  323. }
  324. if (res == 0) {
  325. res = read_service_files_into_objdb(objdb, error_string);
  326. }
  327. if (res == 0) {
  328. snprintf (error_reason, sizeof(error_string_response),
  329. "Successfully read main configuration file '%s'.\n", filename);
  330. *error_string = error_reason;
  331. }
  332. return res;
  333. }
  334. /*
  335. * Dynamic Loader definition
  336. */
  337. struct config_iface_ver0 aisparser_iface_ver0 = {
  338. .config_readconfig = aisparser_readconfig
  339. };
  340. struct lcr_iface corosync_aisparser_ver0[1] = {
  341. {
  342. .name = "corosync_parser",
  343. .version = 0,
  344. .versions_replace = 0,
  345. .versions_replace_count = 0,
  346. .dependencies = 0,
  347. .dependency_count = 0,
  348. .constructor = NULL,
  349. .destructor = NULL,
  350. .interfaces = NULL,
  351. }
  352. };
  353. struct corosync_service_handler *aisparser_get_handler_ver0 (void);
  354. struct lcr_comp aisparser_comp_ver0 = {
  355. .iface_count = 1,
  356. .ifaces = corosync_aisparser_ver0
  357. };
  358. #ifdef COROSYNC_SOLARIS
  359. void corosync_lcr_component_register (void);
  360. void corosync_lcr_component_register (void) {
  361. #else
  362. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  363. #endif
  364. lcr_interfaces_set (&corosync_aisparser_ver0[0], &aisparser_iface_ver0);
  365. lcr_component_register (&aisparser_comp_ver0);
  366. }