coroparse.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. /*
  2. * Copyright (c) 2006-2022 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Patrick Caulfield (pcaulfie@redhat.com)
  7. * Jan Friesse (jfriesse@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <sys/types.h>
  37. #include <sys/uio.h>
  38. #include <sys/socket.h>
  39. #include <sys/stat.h>
  40. #include <sys/un.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include <unistd.h>
  44. #include <fcntl.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <errno.h>
  48. #include <string.h>
  49. #include <dirent.h>
  50. #include <libgen.h>
  51. #include <limits.h>
  52. #include <stddef.h>
  53. #include <grp.h>
  54. #include <pwd.h>
  55. #include <qb/qblist.h>
  56. #include <qb/qbutil.h>
  57. #define LOGSYS_UTILS_ONLY 1
  58. #include <corosync/logsys.h>
  59. #include <corosync/icmap.h>
  60. #include "main.h"
  61. #include "util.h"
  62. enum parser_cb_type {
  63. PARSER_CB_START,
  64. PARSER_CB_END,
  65. PARSER_CB_SECTION_START,
  66. PARSER_CB_SECTION_END,
  67. PARSER_CB_ITEM,
  68. PARSER_CB_CLEANUP,
  69. };
  70. enum main_cp_cb_data_state {
  71. MAIN_CP_CB_DATA_STATE_NORMAL,
  72. MAIN_CP_CB_DATA_STATE_TOTEM,
  73. MAIN_CP_CB_DATA_STATE_INTERFACE,
  74. MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS,
  75. MAIN_CP_CB_DATA_STATE_UIDGID,
  76. MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON,
  77. MAIN_CP_CB_DATA_STATE_MEMBER,
  78. MAIN_CP_CB_DATA_STATE_QUORUM,
  79. MAIN_CP_CB_DATA_STATE_QDEVICE,
  80. MAIN_CP_CB_DATA_STATE_NODELIST,
  81. MAIN_CP_CB_DATA_STATE_NODELIST_NODE,
  82. MAIN_CP_CB_DATA_STATE_PLOAD,
  83. MAIN_CP_CB_DATA_STATE_SYSTEM,
  84. MAIN_CP_CB_DATA_STATE_RESOURCES,
  85. MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM,
  86. MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS,
  87. MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED,
  88. MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED
  89. };
  90. typedef int (*parser_cb_f)(const char *path,
  91. char *key,
  92. char *value,
  93. enum main_cp_cb_data_state *state,
  94. enum parser_cb_type type,
  95. const char **error_string,
  96. icmap_map_t config_map,
  97. void *user_data);
  98. struct key_value_list_item {
  99. char *key;
  100. char *value;
  101. struct qb_list_head list;
  102. };
  103. struct main_cp_cb_data {
  104. int linknumber;
  105. char *bindnetaddr;
  106. char *mcastaddr;
  107. char *broadcast;
  108. int mcastport;
  109. int ttl;
  110. int knet_link_priority;
  111. int knet_ping_interval;
  112. int knet_ping_timeout;
  113. int knet_ping_precision;
  114. int knet_pong_count;
  115. int knet_pmtud_interval;
  116. unsigned int knet_mtu;
  117. char *knet_transport;
  118. struct qb_list_head logger_subsys_items_head;
  119. char *subsys;
  120. char *logging_daemon_name;
  121. struct qb_list_head member_items_head;
  122. int node_number;
  123. };
  124. static int read_config_file_into_icmap(
  125. const char **error_string, icmap_map_t config_map);
  126. static char error_string_response[512];
  127. static int uid_determine (const char *req_user)
  128. {
  129. int pw_uid = 0;
  130. struct passwd passwd;
  131. struct passwd* pwdptr = &passwd;
  132. struct passwd* temp_pwd_pt;
  133. char *pwdbuffer;
  134. int pwdlinelen, rc;
  135. long int id;
  136. char *ep;
  137. id = strtol(req_user, &ep, 10);
  138. if (*req_user != '\0' && *ep == '\0' && id >= 0 && id <= UINT_MAX) {
  139. return (id);
  140. }
  141. pwdlinelen = sysconf (_SC_GETPW_R_SIZE_MAX);
  142. if (pwdlinelen == -1) {
  143. pwdlinelen = 256;
  144. }
  145. pwdbuffer = malloc (pwdlinelen);
  146. while ((rc = getpwnam_r (req_user, pwdptr, pwdbuffer, pwdlinelen, &temp_pwd_pt)) == ERANGE) {
  147. char *n;
  148. pwdlinelen *= 2;
  149. if (pwdlinelen <= 32678) {
  150. n = realloc (pwdbuffer, pwdlinelen);
  151. if (n != NULL) {
  152. pwdbuffer = n;
  153. continue;
  154. }
  155. }
  156. }
  157. if (rc != 0) {
  158. free (pwdbuffer);
  159. sprintf (error_string_response, "getpwnam_r(): %s", strerror(rc));
  160. return (-1);
  161. }
  162. if (temp_pwd_pt == NULL) {
  163. free (pwdbuffer);
  164. sprintf (error_string_response,
  165. "The '%s' user is not found in /etc/passwd, please read the documentation.",
  166. req_user);
  167. return (-1);
  168. }
  169. pw_uid = passwd.pw_uid;
  170. free (pwdbuffer);
  171. return pw_uid;
  172. }
  173. static int gid_determine (const char *req_group)
  174. {
  175. int corosync_gid = 0;
  176. struct group group;
  177. struct group * grpptr = &group;
  178. struct group * temp_grp_pt;
  179. char *grpbuffer;
  180. int grplinelen, rc;
  181. long int id;
  182. char *ep;
  183. id = strtol(req_group, &ep, 10);
  184. if (*req_group != '\0' && *ep == '\0' && id >= 0 && id <= UINT_MAX) {
  185. return (id);
  186. }
  187. grplinelen = sysconf (_SC_GETGR_R_SIZE_MAX);
  188. if (grplinelen == -1) {
  189. grplinelen = 256;
  190. }
  191. grpbuffer = malloc (grplinelen);
  192. while ((rc = getgrnam_r (req_group, grpptr, grpbuffer, grplinelen, &temp_grp_pt)) == ERANGE) {
  193. char *n;
  194. grplinelen *= 2;
  195. if (grplinelen <= 32678) {
  196. n = realloc (grpbuffer, grplinelen);
  197. if (n != NULL) {
  198. grpbuffer = n;
  199. continue;
  200. }
  201. }
  202. }
  203. if (rc != 0) {
  204. free (grpbuffer);
  205. sprintf (error_string_response, "getgrnam_r(): %s", strerror(rc));
  206. return (-1);
  207. }
  208. if (temp_grp_pt == NULL) {
  209. free (grpbuffer);
  210. sprintf (error_string_response,
  211. "The '%s' group is not found in /etc/group, please read the documentation.",
  212. req_group);
  213. return (-1);
  214. }
  215. corosync_gid = group.gr_gid;
  216. free (grpbuffer);
  217. return corosync_gid;
  218. }
  219. static char *strchr_rs (const char *haystack, int byte)
  220. {
  221. const char *end_address = strchr (haystack, byte);
  222. if (end_address) {
  223. end_address += 1; /* skip past { or = */
  224. while (*end_address == ' ' || *end_address == '\t' || (unsigned char)*end_address == 0xA0)
  225. end_address++;
  226. }
  227. return ((char *) end_address);
  228. }
  229. int coroparse_configparse (icmap_map_t config_map, const char **error_string)
  230. {
  231. if (read_config_file_into_icmap(error_string, config_map)) {
  232. return -1;
  233. }
  234. return 0;
  235. }
  236. static char *remove_whitespace(char *string, int remove_colon_and_brace)
  237. {
  238. char *start;
  239. char *end;
  240. start = string;
  241. if (*start == '\0')
  242. return start;
  243. while (*start == ' ' || *start == '\t' || (unsigned char)*start == 0xA0)
  244. start++;
  245. end = start+(strlen(start))-1;
  246. while ((*end == ' ' || *end == '\t' || (unsigned char)*end == 0xA0 || (remove_colon_and_brace && (*end == ':' || *end == '{'))) && end > start)
  247. end--;
  248. if (*end != '\0')
  249. *(end + 1) = '\0';
  250. return start;
  251. }
  252. static int parse_section(FILE *fp,
  253. const char *fname,
  254. int *line_no,
  255. const char *path,
  256. const char **error_string,
  257. int depth,
  258. enum main_cp_cb_data_state state,
  259. parser_cb_f parser_cb,
  260. icmap_map_t config_map,
  261. void *user_data)
  262. {
  263. char line[512];
  264. int i;
  265. char *loc;
  266. int ignore_line;
  267. char new_keyname[ICMAP_KEYNAME_MAXLEN];
  268. static char formated_err[384];
  269. const char *tmp_error_string;
  270. if (strcmp(path, "") == 0) {
  271. parser_cb("", NULL, NULL, &state, PARSER_CB_START, error_string, config_map, user_data);
  272. }
  273. tmp_error_string = NULL;
  274. while (fgets (line, sizeof (line), fp)) {
  275. (*line_no)++;
  276. if (strlen(line) > 0) {
  277. /*
  278. * Check if complete line was read. Use feof to handle files
  279. * without ending \n at the end of the file
  280. */
  281. if ((line[strlen(line) - 1] != '\n') && !feof(fp)) {
  282. tmp_error_string = "Line too long";
  283. goto parse_error;
  284. }
  285. if (line[strlen(line) - 1] == '\n')
  286. line[strlen(line) - 1] = '\0';
  287. if (strlen (line) > 0 && line[strlen(line) - 1] == '\r')
  288. line[strlen(line) - 1] = '\0';
  289. }
  290. /*
  291. * Clear out white space and tabs
  292. */
  293. for (i = strlen (line) - 1; i > -1; i--) {
  294. if (line[i] == '\t' || line[i] == ' ' || (unsigned char)line[i] == 0xA0) {
  295. line[i] = '\0';
  296. } else {
  297. break;
  298. }
  299. }
  300. ignore_line = 1;
  301. for (i = 0; i < strlen (line); i++) {
  302. if (line[i] != '\t' && line[i] != ' ' && (unsigned char)line[i] != 0xA0) {
  303. if (line[i] != '#')
  304. ignore_line = 0;
  305. break;
  306. }
  307. }
  308. /*
  309. * Clear out comments and empty lines
  310. */
  311. if (ignore_line) {
  312. continue;
  313. }
  314. /* New section ? */
  315. if ((loc = strchr_rs (line, '{'))) {
  316. char *section;
  317. char *after_section;
  318. enum main_cp_cb_data_state newstate;
  319. *(loc-1) = '\0';
  320. section = remove_whitespace(line, 1);
  321. after_section = remove_whitespace(loc, 0);
  322. if (strcmp(section, "") == 0) {
  323. tmp_error_string = "Missing section name before opening bracket '{'";
  324. goto parse_error;
  325. }
  326. if (strcmp(after_section, "") != 0) {
  327. tmp_error_string = "Extra characters after opening bracket '{'";
  328. goto parse_error;
  329. }
  330. if (strlen(path) + strlen(section) + 1 >= ICMAP_KEYNAME_MAXLEN) {
  331. tmp_error_string = "Start of section makes total cmap path too long";
  332. goto parse_error;
  333. }
  334. strcpy(new_keyname, path);
  335. if (strcmp(path, "") != 0) {
  336. strcat(new_keyname, ".");
  337. }
  338. strcat(new_keyname, section);
  339. /* Only use the new state for items further down the stack */
  340. newstate = state;
  341. if (!parser_cb(new_keyname, NULL, NULL, &newstate, PARSER_CB_SECTION_START,
  342. &tmp_error_string, config_map, user_data)) {
  343. goto parse_error;
  344. }
  345. if (parse_section(fp, fname, line_no, new_keyname, error_string, depth + 1, newstate,
  346. parser_cb, config_map, user_data))
  347. return -1;
  348. continue ;
  349. }
  350. /* New key/value */
  351. if ((loc = strchr_rs (line, ':'))) {
  352. char *key;
  353. char *value;
  354. *(loc-1) = '\0';
  355. key = remove_whitespace(line, 1);
  356. value = remove_whitespace(loc, 0);
  357. if (strlen(key) == 0) {
  358. tmp_error_string = "Key name can't be empty";
  359. goto parse_error;
  360. }
  361. if (strlen(path) + strlen(key) + 1 >= ICMAP_KEYNAME_MAXLEN) {
  362. tmp_error_string = "New key makes total cmap path too long";
  363. goto parse_error;
  364. }
  365. strcpy(new_keyname, path);
  366. if (strcmp(path, "") != 0) {
  367. strcat(new_keyname, ".");
  368. }
  369. strcat(new_keyname, key);
  370. if (!parser_cb(new_keyname, key, value, &state, PARSER_CB_ITEM, &tmp_error_string,
  371. config_map, user_data)) {
  372. goto parse_error;
  373. }
  374. continue ;
  375. }
  376. if (strchr_rs (line, '}')) {
  377. char *trimmed_line;
  378. trimmed_line = remove_whitespace(line, 0);
  379. if (strcmp(trimmed_line, "}") != 0) {
  380. tmp_error_string = "Extra characters before or after closing bracket '}'";
  381. goto parse_error;
  382. }
  383. if (depth == 0) {
  384. tmp_error_string = "Unexpected closing brace";
  385. goto parse_error;
  386. }
  387. if (!parser_cb(path, NULL, NULL, &state, PARSER_CB_SECTION_END, &tmp_error_string,
  388. config_map, user_data)) {
  389. goto parse_error;
  390. }
  391. return 0;
  392. }
  393. /*
  394. * Line is not opening section, ending section or value -> error
  395. */
  396. tmp_error_string = "Line is not opening or closing section or key value";
  397. goto parse_error;
  398. }
  399. if (strcmp(path, "") != 0) {
  400. tmp_error_string = "Missing closing brace";
  401. goto parse_error;
  402. }
  403. if (strcmp(path, "") == 0) {
  404. parser_cb("", NULL, NULL, &state, PARSER_CB_END, error_string, config_map, user_data);
  405. parser_cb("", NULL, NULL, &state, PARSER_CB_CLEANUP, error_string, config_map, user_data);
  406. }
  407. return 0;
  408. parse_error:
  409. if (snprintf(formated_err, sizeof(formated_err), "parser error: %s:%u: %s", fname, *line_no,
  410. tmp_error_string) >= sizeof(formated_err)) {
  411. *error_string = "Can't format parser error message";
  412. } else {
  413. *error_string = formated_err;
  414. }
  415. parser_cb("", NULL, NULL, &state, PARSER_CB_CLEANUP, error_string, config_map, user_data);
  416. return -1;
  417. }
  418. static int safe_atoq_range(icmap_value_types_t value_type, long long int *min_val, long long int *max_val)
  419. {
  420. switch (value_type) {
  421. case ICMAP_VALUETYPE_INT8: *min_val = INT8_MIN; *max_val = INT8_MAX; break;
  422. case ICMAP_VALUETYPE_UINT8: *min_val = 0; *max_val = UINT8_MAX; break;
  423. case ICMAP_VALUETYPE_INT16: *min_val = INT16_MIN; *max_val = INT16_MAX; break;
  424. case ICMAP_VALUETYPE_UINT16: *min_val = 0; *max_val = UINT16_MAX; break;
  425. case ICMAP_VALUETYPE_INT32: *min_val = INT32_MIN; *max_val = INT32_MAX; break;
  426. case ICMAP_VALUETYPE_UINT32: *min_val = 0; *max_val = UINT32_MAX; break;
  427. default:
  428. return (-1);
  429. }
  430. return (0);
  431. }
  432. /*
  433. * Convert string str to long long int res. Type of result is target_type and currently only
  434. * ICMAP_VALUETYPE_[U]INT[8|16|32] is supported.
  435. * Return 0 on success, -1 on failure.
  436. */
  437. static int safe_atoq(const char *str, long long int *res, icmap_value_types_t target_type)
  438. {
  439. long long int val;
  440. long long int min_val, max_val;
  441. char *endptr;
  442. errno = 0;
  443. val = strtoll(str, &endptr, 10);
  444. if (errno == ERANGE) {
  445. return (-1);
  446. }
  447. if (endptr == str) {
  448. return (-1);
  449. }
  450. if (*endptr != '\0') {
  451. return (-1);
  452. }
  453. if (safe_atoq_range(target_type, &min_val, &max_val) != 0) {
  454. return (-1);
  455. }
  456. if (val < min_val || val > max_val) {
  457. return (-1);
  458. }
  459. *res = val;
  460. return (0);
  461. }
  462. static int str_to_ull(const char *str, unsigned long long int *res)
  463. {
  464. unsigned long long int val;
  465. char *endptr;
  466. errno = 0;
  467. val = strtoull(str, &endptr, 10);
  468. if (errno == ERANGE) {
  469. return (-1);
  470. }
  471. if (endptr == str) {
  472. return (-1);
  473. }
  474. if (*endptr != '\0') {
  475. return (-1);
  476. }
  477. *res = val;
  478. return (0);
  479. }
  480. static int get_dscp_value(char *str, int *dscp) {
  481. struct dscp_name {const char *name; int dscp;} names[] = {
  482. {"cs0", 0}, {"cs1", 8}, {"cs2", 16}, {"cs3", 24},
  483. {"cs4", 32}, {"cs5", 40}, {"cs6", 48}, {"cs7", 56},
  484. {"af11", 10}, {"af12", 12}, {"af13", 14}, {"af21", 18},
  485. {"af22", 20}, {"af23", 22}, {"af31", 26}, {"af32", 28},
  486. {"af33", 30}, {"af41", 34}, {"af42", 36}, {"af43", 38},
  487. {"ef", 46}, {NULL, 0}
  488. };
  489. struct dscp_name *n;
  490. long val;
  491. char *end;
  492. /*
  493. * allow dscp symbolical names according to
  494. * https://www.iana.org/assignments/dscp-registry/dscp-registry.xhtml
  495. */
  496. for (n = names; n->name; n++) {
  497. if (strcmp(str, n->name) == 0) {
  498. *dscp = n->dscp;
  499. return 0;
  500. }
  501. }
  502. /* allow dscp as number (decimal, hex, octal) */
  503. errno = 0;
  504. val = strtol(str, &end, 0);
  505. if (errno == 0 && *end == '\0' && val >= 0 && val <= 63) {
  506. *dscp = val;
  507. return 0;
  508. }
  509. return -1;
  510. }
  511. static int handle_crypto_model(const char *val, const char **error_string)
  512. {
  513. if (util_is_valid_knet_crypto_model(val, NULL, 0,
  514. "Invalid crypto model. Should be ", error_string) == 1) {
  515. return (0);
  516. } else {
  517. return (-1);
  518. }
  519. }
  520. static int handle_crypto_cipher(const char *val, const char **error_string)
  521. {
  522. if (util_is_valid_knet_crypto_cipher(val, NULL, 0,
  523. "Invalid cipher type. Should be ", error_string) == 1) {
  524. return (0);
  525. } else {
  526. return (-1);
  527. }
  528. }
  529. static int handle_crypto_hash(const char *val, const char **error_string)
  530. {
  531. if (util_is_valid_knet_crypto_hash(val, NULL, 0,
  532. "Invalid hash type. Should be ", error_string) == 1) {
  533. return (0);
  534. } else {
  535. return (-1);
  536. }
  537. }
  538. static int handle_compress_model(const char *val, const char **error_string)
  539. {
  540. if (util_is_valid_knet_compress_model(val, NULL, 0,
  541. "Invalid compression model. Should be ", error_string) == 1) {
  542. return (0);
  543. } else {
  544. return (-1);
  545. }
  546. }
  547. static int main_config_parser_cb(const char *path,
  548. char *key,
  549. char *value,
  550. enum main_cp_cb_data_state *state,
  551. enum parser_cb_type type,
  552. const char **error_string,
  553. icmap_map_t config_map,
  554. void *user_data)
  555. {
  556. int ii;
  557. long long int val;
  558. long long int min_val, max_val;
  559. icmap_value_types_t val_type = ICMAP_VALUETYPE_BINARY;
  560. unsigned long long int ull;
  561. int add_as_string;
  562. char key_name[ICMAP_KEYNAME_MAXLEN + 1];
  563. static char formated_err[256];
  564. struct main_cp_cb_data *data = (struct main_cp_cb_data *)user_data;
  565. struct key_value_list_item *kv_item;
  566. struct qb_list_head *iter, *tmp_iter;
  567. int uid, gid, dscp;
  568. cs_error_t cs_err;
  569. const char *path_prefix;
  570. cs_err = CS_OK;
  571. /*
  572. * Formally this check is not needed because length is checked by parse_section
  573. */
  574. if (strlen(path) >= sizeof(key_name)) {
  575. *error_string = "main_config_parser_cb path is longer than key_name";
  576. return (0);
  577. }
  578. /*
  579. * Key_name is used in atoi_error/icmap_set_error, but many of icmap_set*
  580. * are using path, so initialize key_name to valid value
  581. */
  582. strncpy(key_name, path, sizeof(key_name) - 1);
  583. switch (type) {
  584. case PARSER_CB_START:
  585. memset(data, 0, sizeof(struct main_cp_cb_data));
  586. qb_list_init(&data->logger_subsys_items_head);
  587. qb_list_init(&data->member_items_head);
  588. *state = MAIN_CP_CB_DATA_STATE_NORMAL;
  589. break;
  590. case PARSER_CB_END:
  591. break;
  592. case PARSER_CB_CLEANUP:
  593. free(data->bindnetaddr);
  594. free(data->mcastaddr);
  595. free(data->broadcast);
  596. free(data->knet_transport);
  597. qb_list_for_each_safe(iter, tmp_iter, &(data->logger_subsys_items_head)) {
  598. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  599. qb_list_del(&kv_item->list);
  600. free(kv_item->value);
  601. free(kv_item->key);
  602. free(kv_item);
  603. }
  604. free(data->subsys);
  605. free(data->logging_daemon_name);
  606. qb_list_for_each_safe(iter, tmp_iter, &(data->member_items_head)) {
  607. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  608. qb_list_del(&kv_item->list);
  609. free(kv_item->value);
  610. free(kv_item->key);
  611. free(kv_item);
  612. }
  613. break;
  614. case PARSER_CB_ITEM:
  615. add_as_string = 1;
  616. switch (*state) {
  617. case MAIN_CP_CB_DATA_STATE_NORMAL:
  618. break;
  619. case MAIN_CP_CB_DATA_STATE_PLOAD:
  620. if ((strcmp(path, "pload.count") == 0) ||
  621. (strcmp(path, "pload.size") == 0)) {
  622. val_type = ICMAP_VALUETYPE_UINT32;
  623. if (safe_atoq(value, &val, val_type) != 0) {
  624. goto safe_atoq_error;
  625. }
  626. if ((cs_err = icmap_set_uint32_r(config_map, path, val)) != CS_OK) {
  627. goto icmap_set_error;
  628. }
  629. add_as_string = 0;
  630. }
  631. break;
  632. case MAIN_CP_CB_DATA_STATE_QUORUM:
  633. if ((strcmp(path, "quorum.expected_votes") == 0) ||
  634. (strcmp(path, "quorum.votes") == 0) ||
  635. (strcmp(path, "quorum.last_man_standing_window") == 0) ||
  636. (strcmp(path, "quorum.leaving_timeout") == 0)) {
  637. val_type = ICMAP_VALUETYPE_UINT32;
  638. if (safe_atoq(value, &val, val_type) != 0) {
  639. goto safe_atoq_error;
  640. }
  641. if ((cs_err = icmap_set_uint32_r(config_map, path, val)) != CS_OK) {
  642. goto icmap_set_error;
  643. }
  644. add_as_string = 0;
  645. }
  646. if ((strcmp(path, "quorum.two_node") == 0) ||
  647. (strcmp(path, "quorum.expected_votes_tracking") == 0) ||
  648. (strcmp(path, "quorum.allow_downscale") == 0) ||
  649. (strcmp(path, "quorum.wait_for_all") == 0) ||
  650. (strcmp(path, "quorum.auto_tie_breaker") == 0) ||
  651. (strcmp(path, "quorum.last_man_standing") == 0)) {
  652. val_type = ICMAP_VALUETYPE_UINT8;
  653. if (safe_atoq(value, &val, val_type) != 0) {
  654. goto safe_atoq_error;
  655. }
  656. if ((cs_err = icmap_set_uint8_r(config_map, path, val)) != CS_OK) {
  657. goto icmap_set_error;
  658. }
  659. add_as_string = 0;
  660. }
  661. break;
  662. case MAIN_CP_CB_DATA_STATE_QDEVICE:
  663. if ((strcmp(path, "quorum.device.timeout") == 0) ||
  664. (strcmp(path, "quorum.device.sync_timeout") == 0) ||
  665. (strcmp(path, "quorum.device.votes") == 0)) {
  666. val_type = ICMAP_VALUETYPE_UINT32;
  667. if (safe_atoq(value, &val, val_type) != 0) {
  668. goto safe_atoq_error;
  669. }
  670. if ((cs_err = icmap_set_uint32_r(config_map, path, val)) != CS_OK) {
  671. goto icmap_set_error;
  672. }
  673. add_as_string = 0;
  674. }
  675. if ((strcmp(path, "quorum.device.master_wins") == 0)) {
  676. val_type = ICMAP_VALUETYPE_UINT8;
  677. if (safe_atoq(value, &val, val_type) != 0) {
  678. goto safe_atoq_error;
  679. }
  680. if ((cs_err = icmap_set_uint8_r(config_map, path, val)) != CS_OK) {
  681. goto icmap_set_error;
  682. }
  683. add_as_string = 0;
  684. }
  685. break;
  686. case MAIN_CP_CB_DATA_STATE_TOTEM:
  687. if ((strcmp(path, "totem.version") == 0) ||
  688. (strcmp(path, "totem.nodeid") == 0) ||
  689. (strcmp(path, "totem.threads") == 0) ||
  690. (strcmp(path, "totem.token") == 0) ||
  691. (strcmp(path, "totem.token_coefficient") == 0) ||
  692. (strcmp(path, "totem.token_retransmit") == 0) ||
  693. (strcmp(path, "totem.token_warning") == 0) ||
  694. (strcmp(path, "totem.hold") == 0) ||
  695. (strcmp(path, "totem.token_retransmits_before_loss_const") == 0) ||
  696. (strcmp(path, "totem.join") == 0) ||
  697. (strcmp(path, "totem.send_join") == 0) ||
  698. (strcmp(path, "totem.consensus") == 0) ||
  699. (strcmp(path, "totem.merge") == 0) ||
  700. (strcmp(path, "totem.downcheck") == 0) ||
  701. (strcmp(path, "totem.fail_recv_const") == 0) ||
  702. (strcmp(path, "totem.seqno_unchanged_const") == 0) ||
  703. (strcmp(path, "totem.heartbeat_failures_allowed") == 0) ||
  704. (strcmp(path, "totem.max_network_delay") == 0) ||
  705. (strcmp(path, "totem.window_size") == 0) ||
  706. (strcmp(path, "totem.max_messages") == 0) ||
  707. (strcmp(path, "totem.miss_count_const") == 0) ||
  708. (strcmp(path, "totem.knet_pmtud_interval") == 0) ||
  709. (strcmp(path, "totem.knet_mtu") == 0) ||
  710. (strcmp(path, "totem.knet_compression_threshold") == 0) ||
  711. (strcmp(path, "totem.netmtu") == 0)) {
  712. val_type = ICMAP_VALUETYPE_UINT32;
  713. if (safe_atoq(value, &val, val_type) != 0) {
  714. goto safe_atoq_error;
  715. }
  716. if ((cs_err = icmap_set_uint32_r(config_map,path, val)) != CS_OK) {
  717. goto icmap_set_error;
  718. }
  719. add_as_string = 0;
  720. }
  721. if (strcmp(path, "totem.knet_compression_level") == 0) {
  722. val_type = ICMAP_VALUETYPE_INT32;
  723. if (safe_atoq(value, &val, val_type) != 0) {
  724. goto safe_atoq_error;
  725. }
  726. if ((cs_err = icmap_set_int32_r(config_map, path, val)) != CS_OK) {
  727. goto icmap_set_error;
  728. }
  729. add_as_string = 0;
  730. }
  731. if (strcmp(path, "totem.config_version") == 0) {
  732. if (str_to_ull(value, &ull) != 0) {
  733. goto str_to_ull_error;
  734. }
  735. if ((cs_err = icmap_set_uint64_r(config_map, path, ull)) != CS_OK) {
  736. goto icmap_set_error;
  737. }
  738. add_as_string = 0;
  739. }
  740. if (strcmp(path, "totem.ip_version") == 0) {
  741. if ((strcmp(value, "ipv4") != 0) &&
  742. (strcmp(value, "ipv6") != 0) &&
  743. (strcmp(value, "ipv6-4") != 0) &&
  744. (strcmp(value, "ipv4-6") != 0)) {
  745. *error_string = "Invalid ip_version type";
  746. return (0);
  747. }
  748. }
  749. if (strcmp(path, "totem.crypto_model") == 0) {
  750. if (handle_crypto_model(value, error_string) != 0) {
  751. return (0);
  752. }
  753. }
  754. if (strcmp(path, "totem.crypto_cipher") == 0) {
  755. if (handle_crypto_cipher(value, error_string) != 0) {
  756. return (0);
  757. }
  758. }
  759. if (strcmp(path, "totem.crypto_hash") == 0) {
  760. if (handle_crypto_hash(value, error_string) != 0) {
  761. return (0);
  762. }
  763. }
  764. if (strcmp(path, "totem.knet_compression_model") == 0) {
  765. if (handle_compress_model(value, error_string) != 0) {
  766. return (0);
  767. }
  768. }
  769. if (strcmp(path, "totem.ip_dscp") == 0) {
  770. if (get_dscp_value(value, &dscp) != 0) {
  771. goto str_to_dscp_error;
  772. }
  773. if ((cs_err = icmap_set_uint8_r(config_map, path, dscp)) != CS_OK) {
  774. goto icmap_set_error;
  775. }
  776. add_as_string = 0;
  777. }
  778. break;
  779. case MAIN_CP_CB_DATA_STATE_SYSTEM:
  780. if (strcmp(path, "system.qb_ipc_type") == 0) {
  781. if ((strcmp(value, "native") != 0) &&
  782. (strcmp(value, "shm") != 0) &&
  783. (strcmp(value, "socket") != 0)) {
  784. *error_string = "Invalid system.qb_ipc_type";
  785. return (0);
  786. }
  787. }
  788. if (strcmp(path, "system.sched_rr") == 0) {
  789. if ((strcmp(value, "yes") != 0) &&
  790. (strcmp(value, "no") != 0)) {
  791. *error_string = "Invalid system.sched_rr value";
  792. return (0);
  793. }
  794. }
  795. if (strcmp(path, "system.move_to_root_cgroup") == 0) {
  796. if ((strcmp(value, "yes") != 0) &&
  797. (strcmp(value, "no") != 0) &&
  798. (strcmp(value, "auto") != 0)) {
  799. *error_string = "Invalid system.move_to_root_cgroup";
  800. return (0);
  801. }
  802. }
  803. if (strcmp(path, "system.allow_knet_handle_fallback") == 0) {
  804. if ((strcmp(value, "yes") != 0) &&
  805. (strcmp(value, "no") != 0)) {
  806. *error_string = "Invalid system.allow_knet_handle_fallback";
  807. return (0);
  808. }
  809. }
  810. break;
  811. case MAIN_CP_CB_DATA_STATE_INTERFACE:
  812. if (strcmp(path, "totem.interface.linknumber") == 0) {
  813. val_type = ICMAP_VALUETYPE_UINT8;
  814. if (safe_atoq(value, &val, val_type) != 0) {
  815. goto safe_atoq_error;
  816. }
  817. data->linknumber = val;
  818. add_as_string = 0;
  819. }
  820. if (strcmp(path, "totem.interface.bindnetaddr") == 0) {
  821. free(data->bindnetaddr);
  822. data->bindnetaddr = strdup(value);
  823. add_as_string = 0;
  824. }
  825. if (strcmp(path, "totem.interface.mcastaddr") == 0) {
  826. free(data->mcastaddr);
  827. data->mcastaddr = strdup(value);
  828. add_as_string = 0;
  829. }
  830. if (strcmp(path, "totem.interface.broadcast") == 0) {
  831. free(data->broadcast);
  832. data->broadcast = strdup(value);
  833. add_as_string = 0;
  834. }
  835. if (strcmp(path, "totem.interface.mcastport") == 0) {
  836. val_type = ICMAP_VALUETYPE_UINT16;
  837. if (safe_atoq(value, &val, val_type) != 0) {
  838. goto safe_atoq_error;
  839. }
  840. data->mcastport = val;
  841. add_as_string = 0;
  842. }
  843. if (strcmp(path, "totem.interface.ttl") == 0) {
  844. val_type = ICMAP_VALUETYPE_UINT8;
  845. if (safe_atoq(value, &val, val_type) != 0) {
  846. goto safe_atoq_error;
  847. }
  848. data->ttl = val;
  849. add_as_string = 0;
  850. }
  851. if (strcmp(path, "totem.interface.knet_link_priority") == 0) {
  852. val_type = ICMAP_VALUETYPE_UINT8;
  853. if (safe_atoq(value, &val, val_type) != 0) {
  854. goto safe_atoq_error;
  855. }
  856. data->knet_link_priority = val;
  857. add_as_string = 0;
  858. }
  859. if (strcmp(path, "totem.interface.knet_ping_interval") == 0) {
  860. val_type = ICMAP_VALUETYPE_UINT32;
  861. if (safe_atoq(value, &val, val_type) != 0) {
  862. goto safe_atoq_error;
  863. }
  864. data->knet_ping_interval = val;
  865. add_as_string = 0;
  866. }
  867. if (strcmp(path, "totem.interface.knet_ping_timeout") == 0) {
  868. val_type = ICMAP_VALUETYPE_UINT32;
  869. if (safe_atoq(value, &val, val_type) != 0) {
  870. goto safe_atoq_error;
  871. }
  872. data->knet_ping_timeout = val;
  873. add_as_string = 0;
  874. }
  875. if (strcmp(path, "totem.interface.knet_ping_precision") == 0) {
  876. val_type = ICMAP_VALUETYPE_UINT32;
  877. if (safe_atoq(value, &val, val_type) != 0) {
  878. goto safe_atoq_error;
  879. }
  880. data->knet_ping_precision = val;
  881. add_as_string = 0;
  882. }
  883. if (strcmp(path, "totem.interface.knet_pong_count") == 0) {
  884. val_type = ICMAP_VALUETYPE_UINT32;
  885. if (safe_atoq(value, &val, val_type) != 0) {
  886. goto safe_atoq_error;
  887. }
  888. data->knet_pong_count = val;
  889. add_as_string = 0;
  890. }
  891. if (strcmp(path, "totem.interface.knet_transport") == 0) {
  892. free(data->knet_transport);
  893. data->knet_transport = strdup(value);
  894. add_as_string = 0;
  895. }
  896. break;
  897. case MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS:
  898. if (strcmp(path, "logging.logger_subsys.subsys") == 0) {
  899. free(data->subsys);
  900. data->subsys = strdup(value);
  901. if (data->subsys == NULL) {
  902. *error_string = "Can't alloc memory";
  903. return (0);
  904. }
  905. } else {
  906. path_prefix = "logging.logger_subsys.";
  907. if (strlen(path) < strlen(path_prefix) ||
  908. strncmp(path, path_prefix, strlen(path_prefix)) != 0) {
  909. *error_string = "Internal error - incorrect path prefix for logger subsys state";
  910. return (0);
  911. }
  912. kv_item = malloc(sizeof(*kv_item));
  913. if (kv_item == NULL) {
  914. *error_string = "Can't alloc memory";
  915. return (0);
  916. }
  917. memset(kv_item, 0, sizeof(*kv_item));
  918. kv_item->key = strdup(path + strlen(path_prefix));
  919. kv_item->value = strdup(value);
  920. if (kv_item->key == NULL || kv_item->value == NULL) {
  921. free(kv_item->key);
  922. free(kv_item->value);
  923. free(kv_item);
  924. *error_string = "Can't alloc memory";
  925. return (0);
  926. }
  927. qb_list_init(&kv_item->list);
  928. qb_list_add(&kv_item->list, &data->logger_subsys_items_head);
  929. }
  930. add_as_string = 0;
  931. break;
  932. case MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON:
  933. if (strcmp(path, "logging.logging_daemon.subsys") == 0) {
  934. free(data->subsys);
  935. data->subsys = strdup(value);
  936. if (data->subsys == NULL) {
  937. *error_string = "Can't alloc memory";
  938. return (0);
  939. }
  940. } else if (strcmp(path, "logging.logging_daemon.name") == 0) {
  941. free(data->logging_daemon_name);
  942. data->logging_daemon_name = strdup(value);
  943. if (data->logging_daemon_name == NULL) {
  944. *error_string = "Can't alloc memory";
  945. return (0);
  946. }
  947. } else {
  948. path_prefix = "logging.logging_daemon.";
  949. if (strlen(path) < strlen(path_prefix) ||
  950. strncmp(path, path_prefix, strlen(path_prefix)) != 0) {
  951. *error_string = "Internal error - incorrect path prefix for logging daemon state";
  952. return (0);
  953. }
  954. kv_item = malloc(sizeof(*kv_item));
  955. if (kv_item == NULL) {
  956. *error_string = "Can't alloc memory";
  957. return (0);
  958. }
  959. memset(kv_item, 0, sizeof(*kv_item));
  960. kv_item->key = strdup(path + strlen(path_prefix));
  961. kv_item->value = strdup(value);
  962. if (kv_item->key == NULL || kv_item->value == NULL) {
  963. free(kv_item->key);
  964. free(kv_item->value);
  965. free(kv_item);
  966. *error_string = "Can't alloc memory";
  967. return (0);
  968. }
  969. qb_list_init(&kv_item->list);
  970. qb_list_add(&kv_item->list, &data->logger_subsys_items_head);
  971. }
  972. add_as_string = 0;
  973. break;
  974. case MAIN_CP_CB_DATA_STATE_UIDGID:
  975. if (strcmp(path, "uidgid.uid") == 0) {
  976. uid = uid_determine(value);
  977. if (uid == -1) {
  978. *error_string = error_string_response;
  979. return (0);
  980. }
  981. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.uid.%u",
  982. uid);
  983. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  984. goto icmap_set_error;
  985. }
  986. add_as_string = 0;
  987. } else if (strcmp(path, "uidgid.gid") == 0) {
  988. gid = gid_determine(value);
  989. if (gid == -1) {
  990. *error_string = error_string_response;
  991. return (0);
  992. }
  993. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.gid.%u",
  994. gid);
  995. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  996. goto icmap_set_error;
  997. }
  998. add_as_string = 0;
  999. } else {
  1000. *error_string = "uidgid: Only uid and gid are allowed items";
  1001. return (0);
  1002. }
  1003. break;
  1004. case MAIN_CP_CB_DATA_STATE_MEMBER:
  1005. if (strcmp(path, "totem.interface.member.memberaddr") != 0) {
  1006. *error_string = "Only memberaddr is allowed in member section";
  1007. return (0);
  1008. }
  1009. kv_item = malloc(sizeof(*kv_item));
  1010. if (kv_item == NULL) {
  1011. *error_string = "Can't alloc memory";
  1012. return (0);
  1013. }
  1014. memset(kv_item, 0, sizeof(*kv_item));
  1015. kv_item->key = strdup(key);
  1016. kv_item->value = strdup(value);
  1017. if (kv_item->key == NULL || kv_item->value == NULL) {
  1018. free(kv_item->key);
  1019. free(kv_item->value);
  1020. free(kv_item);
  1021. *error_string = "Can't alloc memory";
  1022. return (0);
  1023. }
  1024. qb_list_init(&kv_item->list);
  1025. qb_list_add(&kv_item->list, &data->member_items_head);
  1026. add_as_string = 0;
  1027. break;
  1028. case MAIN_CP_CB_DATA_STATE_NODELIST:
  1029. break;
  1030. case MAIN_CP_CB_DATA_STATE_NODELIST_NODE:
  1031. path_prefix = "nodelist.node.";
  1032. if (strlen(path) < strlen(path_prefix) ||
  1033. strncmp(path, path_prefix, strlen(path_prefix)) != 0) {
  1034. *error_string = "Internal error - incorrect path prefix for nodelist node state";
  1035. return (0);
  1036. }
  1037. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.%s", data->node_number,
  1038. path + strlen(path_prefix));
  1039. if ((strcmp(path, "nodelist.node.nodeid") == 0) ||
  1040. (strcmp(path, "nodelist.node.quorum_votes") == 0)) {
  1041. val_type = ICMAP_VALUETYPE_UINT32;
  1042. if (safe_atoq(value, &val, val_type) != 0) {
  1043. goto safe_atoq_error;
  1044. }
  1045. if ((cs_err = icmap_set_uint32_r(config_map, key_name, val)) != CS_OK) {
  1046. goto icmap_set_error;
  1047. }
  1048. add_as_string = 0;
  1049. }
  1050. if (add_as_string) {
  1051. if ((cs_err = icmap_set_string_r(config_map, key_name, value)) != CS_OK) {
  1052. goto icmap_set_error;
  1053. };
  1054. add_as_string = 0;
  1055. }
  1056. break;
  1057. case MAIN_CP_CB_DATA_STATE_RESOURCES:
  1058. if (strcmp(key, "watchdog_timeout") == 0) {
  1059. val_type = ICMAP_VALUETYPE_UINT32;
  1060. if (safe_atoq(value, &val, val_type) != 0) {
  1061. goto safe_atoq_error;
  1062. }
  1063. if ((cs_err = icmap_set_uint32_r(config_map,path, val)) != CS_OK) {
  1064. goto icmap_set_error;
  1065. }
  1066. add_as_string = 0;
  1067. }
  1068. break;
  1069. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM:
  1070. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED:
  1071. if (strcmp(key, "poll_period") == 0) {
  1072. if (str_to_ull(value, &ull) != 0) {
  1073. goto str_to_ull_error;
  1074. }
  1075. if ((cs_err = icmap_set_uint64_r(config_map,path, ull)) != CS_OK) {
  1076. goto icmap_set_error;
  1077. }
  1078. add_as_string = 0;
  1079. }
  1080. break;
  1081. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS:
  1082. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED:
  1083. if (strcmp(key, "poll_period") == 0) {
  1084. if (str_to_ull(value, &ull) != 0) {
  1085. goto str_to_ull_error;
  1086. }
  1087. if ((cs_err = icmap_set_uint64_r(config_map,path, ull)) != CS_OK) {
  1088. goto icmap_set_error;
  1089. }
  1090. add_as_string = 0;
  1091. }
  1092. break;
  1093. }
  1094. if (add_as_string) {
  1095. if ((cs_err = icmap_set_string_r(config_map, path, value)) != CS_OK) {
  1096. goto icmap_set_error;
  1097. }
  1098. }
  1099. break;
  1100. case PARSER_CB_SECTION_START:
  1101. if (strcmp(path, "totem.interface") == 0) {
  1102. *state = MAIN_CP_CB_DATA_STATE_INTERFACE;
  1103. data->linknumber = 0;
  1104. data->mcastport = -1;
  1105. data->ttl = -1;
  1106. data->knet_link_priority = -1;
  1107. data->knet_ping_interval = -1;
  1108. data->knet_ping_timeout = -1;
  1109. data->knet_ping_precision = -1;
  1110. data->knet_pong_count = -1;
  1111. data->knet_transport = NULL;
  1112. qb_list_init(&data->member_items_head);
  1113. };
  1114. if (strcmp(path, "totem") == 0) {
  1115. *state = MAIN_CP_CB_DATA_STATE_TOTEM;
  1116. };
  1117. if (strcmp(path, "system") == 0) {
  1118. *state = MAIN_CP_CB_DATA_STATE_SYSTEM;
  1119. }
  1120. if (strcmp(path, "logging.logger_subsys") == 0) {
  1121. *state = MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS;
  1122. qb_list_init(&data->logger_subsys_items_head);
  1123. data->subsys = NULL;
  1124. }
  1125. if (strcmp(path, "logging.logging_daemon") == 0) {
  1126. *state = MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON;
  1127. qb_list_init(&data->logger_subsys_items_head);
  1128. data->subsys = NULL;
  1129. data->logging_daemon_name = NULL;
  1130. }
  1131. if (strcmp(path, "uidgid") == 0) {
  1132. *state = MAIN_CP_CB_DATA_STATE_UIDGID;
  1133. }
  1134. if (strcmp(path, "totem.interface.member") == 0) {
  1135. *state = MAIN_CP_CB_DATA_STATE_MEMBER;
  1136. }
  1137. if (strcmp(path, "quorum") == 0) {
  1138. *state = MAIN_CP_CB_DATA_STATE_QUORUM;
  1139. }
  1140. if (strcmp(path, "quorum.device") == 0) {
  1141. *state = MAIN_CP_CB_DATA_STATE_QDEVICE;
  1142. }
  1143. if (strcmp(path, "nodelist") == 0) {
  1144. *state = MAIN_CP_CB_DATA_STATE_NODELIST;
  1145. data->node_number = 0;
  1146. }
  1147. if (strcmp(path, "nodelist.node") == 0) {
  1148. *state = MAIN_CP_CB_DATA_STATE_NODELIST_NODE;
  1149. }
  1150. if (strcmp(path, "resources") == 0) {
  1151. *state = MAIN_CP_CB_DATA_STATE_RESOURCES;
  1152. }
  1153. if (strcmp(path, "resources.system") == 0) {
  1154. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM;
  1155. }
  1156. if (strcmp(path, "resources.system.memory_used") == 0) {
  1157. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED;
  1158. }
  1159. if (strcmp(path, "resources.process") == 0) {
  1160. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS;
  1161. }
  1162. if (strcmp(path, "resources.process.memory_used") == 0) {
  1163. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED;
  1164. }
  1165. if (*state == MAIN_CP_CB_DATA_STATE_UIDGID && strcmp(path, "uidgid") != 0) {
  1166. *error_string = "Subsections are not allowed within uidgid section";
  1167. return (0);
  1168. };
  1169. if (*state == MAIN_CP_CB_DATA_STATE_MEMBER && strcmp(path, "totem.interface.member") != 0) {
  1170. *error_string = "Subsections are not allowed within totem.interface.member section";
  1171. return (0);
  1172. };
  1173. break;
  1174. case PARSER_CB_SECTION_END:
  1175. switch (*state) {
  1176. case MAIN_CP_CB_DATA_STATE_INTERFACE:
  1177. if (strcmp(path, "totem.interface") != 0) {
  1178. /*
  1179. * Process only end of totem.interface section, not subsections
  1180. */
  1181. break;
  1182. }
  1183. /*
  1184. * Create new interface section
  1185. */
  1186. if (data->bindnetaddr != NULL) {
  1187. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr",
  1188. data->linknumber);
  1189. cs_err = icmap_set_string_r(config_map, key_name, data->bindnetaddr);
  1190. free(data->bindnetaddr);
  1191. data->bindnetaddr = NULL;
  1192. if (cs_err != CS_OK) {
  1193. goto icmap_set_error;
  1194. }
  1195. }
  1196. if (data->mcastaddr != NULL) {
  1197. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr",
  1198. data->linknumber);
  1199. cs_err = icmap_set_string_r(config_map, key_name, data->mcastaddr);
  1200. free(data->mcastaddr);
  1201. data->mcastaddr = NULL;
  1202. if (cs_err != CS_OK) {
  1203. goto icmap_set_error;
  1204. }
  1205. }
  1206. if (data->broadcast != NULL) {
  1207. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast",
  1208. data->linknumber);
  1209. cs_err = icmap_set_string_r(config_map, key_name, data->broadcast);
  1210. free(data->broadcast);
  1211. data->broadcast = NULL;
  1212. if (cs_err != CS_OK) {
  1213. goto icmap_set_error;
  1214. }
  1215. }
  1216. if (data->mcastport > -1) {
  1217. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport",
  1218. data->linknumber);
  1219. if ((cs_err = icmap_set_uint16_r(config_map, key_name,
  1220. data->mcastport)) != CS_OK) {
  1221. goto icmap_set_error;
  1222. }
  1223. }
  1224. if (data->ttl > -1) {
  1225. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl",
  1226. data->linknumber);
  1227. if ((cs_err = icmap_set_uint8_r(config_map, key_name, data->ttl)) != CS_OK) {
  1228. goto icmap_set_error;
  1229. }
  1230. }
  1231. if (data->knet_link_priority > -1) {
  1232. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_link_priority",
  1233. data->linknumber);
  1234. if ((cs_err = icmap_set_uint8_r(config_map, key_name,
  1235. data->knet_link_priority)) != CS_OK) {
  1236. goto icmap_set_error;
  1237. }
  1238. }
  1239. if (data->knet_ping_interval > -1) {
  1240. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_interval",
  1241. data->linknumber);
  1242. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1243. data->knet_ping_interval)) != CS_OK) {
  1244. goto icmap_set_error;
  1245. }
  1246. }
  1247. if (data->knet_ping_timeout > -1) {
  1248. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_timeout",
  1249. data->linknumber);
  1250. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1251. data->knet_ping_timeout)) != CS_OK) {
  1252. goto icmap_set_error;
  1253. }
  1254. }
  1255. if (data->knet_ping_precision > -1) {
  1256. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_precision",
  1257. data->linknumber);
  1258. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1259. data->knet_ping_precision)) != CS_OK) {
  1260. goto icmap_set_error;
  1261. }
  1262. }
  1263. if (data->knet_pong_count > -1) {
  1264. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_pong_count",
  1265. data->linknumber);
  1266. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1267. data->knet_pong_count)) != CS_OK) {
  1268. goto icmap_set_error;
  1269. }
  1270. }
  1271. if (data->knet_transport) {
  1272. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_transport",
  1273. data->linknumber);
  1274. cs_err = icmap_set_string_r(config_map, key_name, data->knet_transport);
  1275. free(data->knet_transport);
  1276. data->knet_transport = NULL;
  1277. if (cs_err != CS_OK) {
  1278. goto icmap_set_error;
  1279. }
  1280. }
  1281. ii = 0;
  1282. qb_list_for_each_safe(iter, tmp_iter, &(data->member_items_head)) {
  1283. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  1284. qb_list_del(&kv_item->list);
  1285. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.%u",
  1286. data->linknumber, ii);
  1287. cs_err = icmap_set_string_r(config_map, key_name, kv_item->value);
  1288. free(kv_item->value);
  1289. free(kv_item->key);
  1290. free(kv_item);
  1291. ii++;
  1292. if (cs_err != CS_OK) {
  1293. goto icmap_set_error;
  1294. }
  1295. }
  1296. qb_list_init(&data->member_items_head);
  1297. break;
  1298. case MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS:
  1299. if (strcmp(path, "logging.logger_subsys") != 0) {
  1300. /*
  1301. * Process only end of logging.logger_subsys section, not subsections
  1302. */
  1303. break;
  1304. }
  1305. if (data->subsys == NULL) {
  1306. *error_string = "No subsys key in logger_subsys directive";
  1307. return (0);
  1308. }
  1309. qb_list_for_each_safe(iter, tmp_iter, &(data->logger_subsys_items_head)) {
  1310. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  1311. qb_list_del(&kv_item->list);
  1312. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.%s",
  1313. data->subsys, kv_item->key);
  1314. cs_err = icmap_set_string_r(config_map, key_name, kv_item->value);
  1315. free(kv_item->value);
  1316. free(kv_item->key);
  1317. free(kv_item);
  1318. if (cs_err != CS_OK) {
  1319. goto icmap_set_error;
  1320. }
  1321. }
  1322. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.subsys",
  1323. data->subsys);
  1324. cs_err = icmap_set_string_r(config_map, key_name, data->subsys);
  1325. qb_list_init(&data->logger_subsys_items_head);
  1326. free(data->subsys);
  1327. data->subsys = NULL;
  1328. if (cs_err != CS_OK) {
  1329. goto icmap_set_error;
  1330. }
  1331. break;
  1332. case MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON:
  1333. if (strcmp(path, "logging.logging_daemon") != 0) {
  1334. /*
  1335. * Process only end of logging.logging_daemon section, not subsections
  1336. */
  1337. break;
  1338. }
  1339. if (data->logging_daemon_name == NULL) {
  1340. *error_string = "No name key in logging_daemon directive";
  1341. return (0);
  1342. }
  1343. qb_list_for_each_safe(iter, tmp_iter, &(data->logger_subsys_items_head)) {
  1344. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  1345. qb_list_del(&kv_item->list);
  1346. if (data->subsys == NULL) {
  1347. if (strcmp(data->logging_daemon_name, "corosync") == 0) {
  1348. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1349. "logging.%s",
  1350. kv_item->key);
  1351. } else {
  1352. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1353. "logging.logging_daemon.%s.%s",
  1354. data->logging_daemon_name, kv_item->key);
  1355. }
  1356. } else {
  1357. if (strcmp(data->logging_daemon_name, "corosync") == 0) {
  1358. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1359. "logging.logger_subsys.%s.%s",
  1360. data->subsys,
  1361. kv_item->key);
  1362. } else {
  1363. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1364. "logging.logging_daemon.%s.%s.%s",
  1365. data->logging_daemon_name, data->subsys,
  1366. kv_item->key);
  1367. }
  1368. }
  1369. cs_err = icmap_set_string_r(config_map, key_name, kv_item->value);
  1370. free(kv_item->value);
  1371. free(kv_item->key);
  1372. free(kv_item);
  1373. if (cs_err != CS_OK) {
  1374. goto icmap_set_error;
  1375. }
  1376. }
  1377. if (data->subsys == NULL) {
  1378. if (strcmp(data->logging_daemon_name, "corosync") != 0) {
  1379. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.name",
  1380. data->logging_daemon_name);
  1381. cs_err = icmap_set_string_r(config_map, key_name, data->logging_daemon_name);
  1382. }
  1383. } else {
  1384. if (strcmp(data->logging_daemon_name, "corosync") == 0) {
  1385. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.subsys",
  1386. data->subsys);
  1387. cs_err = icmap_set_string_r(config_map, key_name, data->subsys);
  1388. } else {
  1389. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.%s.subsys",
  1390. data->logging_daemon_name, data->subsys);
  1391. cs_err = icmap_set_string_r(config_map, key_name, data->subsys);
  1392. if (cs_err != CS_OK) {
  1393. goto icmap_set_error;
  1394. }
  1395. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.%s.name",
  1396. data->logging_daemon_name, data->subsys);
  1397. cs_err = icmap_set_string_r(config_map, key_name, data->logging_daemon_name);
  1398. }
  1399. }
  1400. qb_list_init(&data->logger_subsys_items_head);
  1401. free(data->subsys);
  1402. data->subsys = NULL;
  1403. free(data->logging_daemon_name);
  1404. data->logging_daemon_name = NULL;
  1405. if (cs_err != CS_OK) {
  1406. goto icmap_set_error;
  1407. }
  1408. break;
  1409. case MAIN_CP_CB_DATA_STATE_NODELIST_NODE:
  1410. if (strcmp(path, "nodelist.node") != 0) {
  1411. /*
  1412. * Process only end of nodelist.node section, not subsections
  1413. */
  1414. break;
  1415. }
  1416. data->node_number++;
  1417. break;
  1418. case MAIN_CP_CB_DATA_STATE_NORMAL:
  1419. case MAIN_CP_CB_DATA_STATE_PLOAD:
  1420. case MAIN_CP_CB_DATA_STATE_UIDGID:
  1421. case MAIN_CP_CB_DATA_STATE_MEMBER:
  1422. case MAIN_CP_CB_DATA_STATE_QUORUM:
  1423. case MAIN_CP_CB_DATA_STATE_QDEVICE:
  1424. case MAIN_CP_CB_DATA_STATE_NODELIST:
  1425. case MAIN_CP_CB_DATA_STATE_TOTEM:
  1426. case MAIN_CP_CB_DATA_STATE_SYSTEM:
  1427. case MAIN_CP_CB_DATA_STATE_RESOURCES:
  1428. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM:
  1429. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED:
  1430. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS:
  1431. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED:
  1432. break;
  1433. }
  1434. break;
  1435. }
  1436. return (1);
  1437. safe_atoq_error:
  1438. /*
  1439. * For integers supported by safe_atoq display range
  1440. */
  1441. min_val = max_val = 0;
  1442. /*
  1443. * This is really assert, because developer ether doesn't set val_type correctly or
  1444. * we've got here after some nasty memory overwrite
  1445. */
  1446. assert(safe_atoq_range(val_type, &min_val, &max_val) == 0);
  1447. if (snprintf(formated_err, sizeof(formated_err),
  1448. "Value of key \"%s\" is expected to be integer in range (%lld..%lld), but \"%s\" was given",
  1449. key_name, min_val, max_val, value) >= sizeof(formated_err)) {
  1450. *error_string = "Can't format parser error message";
  1451. } else {
  1452. *error_string = formated_err;
  1453. }
  1454. return (0);
  1455. str_to_ull_error:
  1456. /*
  1457. * For integers not supported by safe_atoq (64-bit int)
  1458. */
  1459. if (snprintf(formated_err, sizeof(formated_err),
  1460. "Value of key \"%s\" is expected to be unsigned integer, but \"%s\" was given",
  1461. key_name, value) >= sizeof(formated_err)) {
  1462. *error_string = "Can't format parser error message";
  1463. } else {
  1464. *error_string = formated_err;
  1465. }
  1466. return (0);
  1467. str_to_dscp_error:
  1468. if (snprintf(formated_err, sizeof(formated_err),
  1469. "Value of key \"%s\" is expected to be number (0..63) or symbolical dscp value, "
  1470. "but \"%s\" was given", key_name, value) >= sizeof(formated_err)) {
  1471. *error_string = "Can't format parser error message";
  1472. } else {
  1473. *error_string = formated_err;
  1474. }
  1475. return (0);
  1476. icmap_set_error:
  1477. if (snprintf(formated_err, sizeof(formated_err),
  1478. "Can't store key \"%s\" into icmap, returned error is %s",
  1479. key_name, cs_strerror(cs_err)) >= sizeof(formated_err)) {
  1480. *error_string = "Can't format parser error message";
  1481. } else {
  1482. *error_string = formated_err;
  1483. }
  1484. return (0);
  1485. }
  1486. static int uidgid_config_parser_cb(const char *path,
  1487. char *key,
  1488. char *value,
  1489. enum main_cp_cb_data_state *state,
  1490. enum parser_cb_type type,
  1491. const char **error_string,
  1492. icmap_map_t config_map,
  1493. void *user_data)
  1494. {
  1495. char key_name[ICMAP_KEYNAME_MAXLEN];
  1496. int uid, gid;
  1497. static char formated_err[256];
  1498. cs_error_t cs_err;
  1499. switch (type) {
  1500. case PARSER_CB_START:
  1501. break;
  1502. case PARSER_CB_END:
  1503. break;
  1504. case PARSER_CB_CLEANUP:
  1505. break;
  1506. case PARSER_CB_ITEM:
  1507. if (strcmp(path, "uidgid.uid") == 0) {
  1508. uid = uid_determine(value);
  1509. if (uid == -1) {
  1510. *error_string = error_string_response;
  1511. return (0);
  1512. }
  1513. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.uid.%u",
  1514. uid);
  1515. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  1516. goto icmap_set_error;
  1517. }
  1518. } else if (strcmp(path, "uidgid.gid") == 0) {
  1519. gid = gid_determine(value);
  1520. if (gid == -1) {
  1521. *error_string = error_string_response;
  1522. return (0);
  1523. }
  1524. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.gid.%u",
  1525. gid);
  1526. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  1527. goto icmap_set_error;
  1528. }
  1529. } else {
  1530. *error_string = "uidgid: Only uid and gid are allowed items";
  1531. return (0);
  1532. }
  1533. break;
  1534. case PARSER_CB_SECTION_START:
  1535. if (strcmp(path, "uidgid") != 0) {
  1536. *error_string = "uidgid: Can't add subsection different than uidgid";
  1537. return (0);
  1538. };
  1539. break;
  1540. case PARSER_CB_SECTION_END:
  1541. break;
  1542. }
  1543. return (1);
  1544. icmap_set_error:
  1545. if (snprintf(formated_err, sizeof(formated_err),
  1546. "Can't store key \"%s\" into icmap, returned error is %s",
  1547. key_name, cs_strerror(cs_err)) >= sizeof(formated_err)) {
  1548. *error_string = "Can't format parser error message";
  1549. } else {
  1550. *error_string = formated_err;
  1551. }
  1552. return (0);
  1553. }
  1554. static int read_uidgid_files_into_icmap(
  1555. const char **error_string,
  1556. icmap_map_t config_map)
  1557. {
  1558. FILE *fp;
  1559. char *dirname_res;
  1560. DIR *dp;
  1561. struct dirent *dirent;
  1562. char filename[PATH_MAX + FILENAME_MAX + 1];
  1563. char uidgid_dirname[PATH_MAX + FILENAME_MAX + 1];
  1564. int res = 0;
  1565. struct stat stat_buf;
  1566. enum main_cp_cb_data_state state = MAIN_CP_CB_DATA_STATE_NORMAL;
  1567. char key_name[ICMAP_KEYNAME_MAXLEN];
  1568. int line_no;
  1569. /*
  1570. * Build uidgid directory based on corosync.conf file location
  1571. */
  1572. res = snprintf(filename, sizeof(filename), "%s",
  1573. corosync_get_config_file());
  1574. if (res >= sizeof(filename)) {
  1575. *error_string = "uidgid.d path too long";
  1576. return (-1);
  1577. }
  1578. dirname_res = dirname(filename);
  1579. res = snprintf(uidgid_dirname, sizeof(uidgid_dirname), "%s/%s",
  1580. dirname_res, "uidgid.d");
  1581. if (res >= sizeof(uidgid_dirname)) {
  1582. *error_string = "uidgid.d path too long";
  1583. return (-1);
  1584. }
  1585. dp = opendir (uidgid_dirname);
  1586. if (dp == NULL)
  1587. return 0;
  1588. for (dirent = readdir(dp);
  1589. dirent != NULL;
  1590. dirent = readdir(dp)) {
  1591. res = snprintf(filename, sizeof (filename), "%s/%s", uidgid_dirname, dirent->d_name);
  1592. if (res >= sizeof(filename)) {
  1593. res = -1;
  1594. *error_string = "uidgid.d dirname path too long";
  1595. goto error_exit;
  1596. }
  1597. // coverity[TOCTOU:SUPPRESS] not really problem
  1598. res = stat (filename, &stat_buf);
  1599. if (res == 0 && S_ISREG(stat_buf.st_mode)) {
  1600. fp = fopen (filename, "r");
  1601. if (fp == NULL) continue;
  1602. key_name[0] = 0;
  1603. line_no = 0;
  1604. res = parse_section(fp, filename, &line_no, key_name, error_string, 0, state,
  1605. uidgid_config_parser_cb, config_map, NULL);
  1606. fclose (fp);
  1607. if (res != 0) {
  1608. goto error_exit;
  1609. }
  1610. }
  1611. }
  1612. error_exit:
  1613. closedir(dp);
  1614. return res;
  1615. }
  1616. /* Read config file and load into icmap */
  1617. static int read_config_file_into_icmap(
  1618. const char **error_string,
  1619. icmap_map_t config_map)
  1620. {
  1621. FILE *fp;
  1622. const char *filename;
  1623. char *error_reason = error_string_response;
  1624. int res;
  1625. char key_name[ICMAP_KEYNAME_MAXLEN];
  1626. struct main_cp_cb_data data;
  1627. enum main_cp_cb_data_state state = MAIN_CP_CB_DATA_STATE_NORMAL;
  1628. int line_no;
  1629. filename = corosync_get_config_file();
  1630. fp = fopen (filename, "r");
  1631. if (fp == NULL) {
  1632. char error_str[100];
  1633. const char *error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  1634. snprintf (error_reason, sizeof(error_string_response),
  1635. "Can't read file %s: %s",
  1636. filename, error_ptr);
  1637. *error_string = error_reason;
  1638. return -1;
  1639. }
  1640. key_name[0] = 0;
  1641. line_no = 0;
  1642. res = parse_section(fp, filename, &line_no, key_name, error_string, 0, state,
  1643. main_config_parser_cb, config_map, &data);
  1644. fclose(fp);
  1645. if (res == 0) {
  1646. res = read_uidgid_files_into_icmap(error_string, config_map);
  1647. }
  1648. if (res == 0) {
  1649. snprintf (error_reason, sizeof(error_string_response),
  1650. "Successfully read main configuration file '%s'.", filename);
  1651. *error_string = error_reason;
  1652. }
  1653. return res;
  1654. }