corosync  2.3.5
coroparse.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006-2013 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 
36 #include <config.h>
37 
38 #include <sys/types.h>
39 #include <sys/uio.h>
40 #include <sys/socket.h>
41 #include <sys/stat.h>
42 #include <sys/un.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <errno.h>
50 #include <string.h>
51 #include <dirent.h>
52 #include <limits.h>
53 #include <stddef.h>
54 #include <grp.h>
55 #include <pwd.h>
56 
57 #include <corosync/list.h>
58 #include <qb/qbutil.h>
59 #define LOGSYS_UTILS_ONLY 1
60 #include <corosync/logsys.h>
61 #include <corosync/icmap.h>
62 
63 #include "main.h"
64 #include "util.h"
65 
72 };
73 
74 typedef int (*parser_cb_f)(const char *path,
75  char *key,
76  char *value,
77  enum parser_cb_type type,
78  const char **error_string,
79  icmap_map_t config_map,
80  void *user_data);
81 
96 };
97 
99  char *key;
100  char *value;
101  struct list_head list;
102 };
103 
106 
108  char *bindnetaddr;
109  char *mcastaddr;
110  char *broadcast;
112  int ttl;
113 
115  char *subsys;
118 
121 };
122 
123 static int read_config_file_into_icmap(
124  const char **error_string, icmap_map_t config_map);
125 static char error_string_response[512];
126 
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 
138  id = strtol(req_user, &ep, 10);
139  if (*ep == '\0' && id >= 0 && id <= UINT_MAX) {
140  return (id);
141  }
142 
143  pwdlinelen = sysconf (_SC_GETPW_R_SIZE_MAX);
144 
145  if (pwdlinelen == -1) {
146  pwdlinelen = 256;
147  }
148 
149  pwdbuffer = malloc (pwdlinelen);
150 
151  while ((rc = getpwnam_r (req_user, pwdptr, pwdbuffer, pwdlinelen, &temp_pwd_pt)) == ERANGE) {
152  char *n;
153 
154  pwdlinelen *= 2;
155  if (pwdlinelen <= 32678) {
156  n = realloc (pwdbuffer, pwdlinelen);
157  if (n != NULL) {
158  pwdbuffer = n;
159  continue;
160  }
161  }
162  }
163  if (rc != 0) {
164  free (pwdbuffer);
165  sprintf (error_string_response, "getpwnam_r(): %s", strerror(rc));
166  return (-1);
167  }
168  if (temp_pwd_pt == NULL) {
169  free (pwdbuffer);
170  sprintf (error_string_response,
171  "The '%s' user is not found in /etc/passwd, please read the documentation.",
172  req_user);
173  return (-1);
174  }
175  pw_uid = passwd.pw_uid;
176  free (pwdbuffer);
177 
178  return pw_uid;
179 }
180 
181 static int gid_determine (const char *req_group)
182 {
183  int corosync_gid = 0;
184  struct group group;
185  struct group * grpptr = &group;
186  struct group * temp_grp_pt;
187  char *grpbuffer;
188  int grplinelen, rc;
189  long int id;
190  char *ep;
191 
192  id = strtol(req_group, &ep, 10);
193  if (*ep == '\0' && id >= 0 && id <= UINT_MAX) {
194  return (id);
195  }
196 
197  grplinelen = sysconf (_SC_GETGR_R_SIZE_MAX);
198 
199  if (grplinelen == -1) {
200  grplinelen = 256;
201  }
202 
203  grpbuffer = malloc (grplinelen);
204 
205  while ((rc = getgrnam_r (req_group, grpptr, grpbuffer, grplinelen, &temp_grp_pt)) == ERANGE) {
206  char *n;
207 
208  grplinelen *= 2;
209  if (grplinelen <= 32678) {
210  n = realloc (grpbuffer, grplinelen);
211  if (n != NULL) {
212  grpbuffer = n;
213  continue;
214  }
215  }
216  }
217  if (rc != 0) {
218  free (grpbuffer);
219  sprintf (error_string_response, "getgrnam_r(): %s", strerror(rc));
220  return (-1);
221  }
222  if (temp_grp_pt == NULL) {
223  free (grpbuffer);
224  sprintf (error_string_response,
225  "The '%s' group is not found in /etc/group, please read the documentation.",
226  req_group);
227  return (-1);
228  }
229  corosync_gid = group.gr_gid;
230  free (grpbuffer);
231 
232  return corosync_gid;
233 }
234 static char *strchr_rs (const char *haystack, int byte)
235 {
236  const char *end_address = strchr (haystack, byte);
237  if (end_address) {
238  end_address += 1; /* skip past { or = */
239 
240  while (*end_address == ' ' || *end_address == '\t')
241  end_address++;
242  }
243 
244  return ((char *) end_address);
245 }
246 
247 int coroparse_configparse (icmap_map_t config_map, const char **error_string)
248 {
249  if (read_config_file_into_icmap(error_string, config_map)) {
250  return -1;
251  }
252 
253  return 0;
254 }
255 
256 static char *remove_whitespace(char *string, int remove_colon_and_brace)
257 {
258  char *start;
259  char *end;
260 
261  start = string;
262  while (*start == ' ' || *start == '\t')
263  start++;
264 
265  end = start+(strlen(start))-1;
266  while ((*end == ' ' || *end == '\t' || (remove_colon_and_brace && (*end == ':' || *end == '{'))) && end > start)
267  end--;
268  if (end != start)
269  *(end+1) = '\0';
270 
271  return start;
272 }
273 
274 
275 
276 static int parse_section(FILE *fp,
277  char *path,
278  const char **error_string,
279  int depth,
280  parser_cb_f parser_cb,
281  icmap_map_t config_map,
282  void *user_data)
283 {
284  char line[512];
285  int i;
286  char *loc;
287  int ignore_line;
288  char new_keyname[ICMAP_KEYNAME_MAXLEN];
289 
290  if (strcmp(path, "") == 0) {
291  parser_cb("", NULL, NULL, PARSER_CB_START, error_string, config_map, user_data);
292  }
293 
294  while (fgets (line, sizeof (line), fp)) {
295  if (strlen(line) > 0) {
296  if (line[strlen(line) - 1] == '\n')
297  line[strlen(line) - 1] = '\0';
298  if (strlen (line) > 0 && line[strlen(line) - 1] == '\r')
299  line[strlen(line) - 1] = '\0';
300  }
301  /*
302  * Clear out white space and tabs
303  */
304  for (i = strlen (line) - 1; i > -1; i--) {
305  if (line[i] == '\t' || line[i] == ' ') {
306  line[i] = '\0';
307  } else {
308  break;
309  }
310  }
311 
312  ignore_line = 1;
313  for (i = 0; i < strlen (line); i++) {
314  if (line[i] != '\t' && line[i] != ' ') {
315  if (line[i] != '#')
316  ignore_line = 0;
317 
318  break;
319  }
320  }
321  /*
322  * Clear out comments and empty lines
323  */
324  if (ignore_line) {
325  continue;
326  }
327 
328  /* New section ? */
329  if ((loc = strchr_rs (line, '{'))) {
330  char *section = remove_whitespace(line, 1);
331 
332  loc--;
333  *loc = '\0';
334 
335  if (strlen(path) + strlen(section) + 1 >= ICMAP_KEYNAME_MAXLEN) {
336  *error_string = "parser error: Start of section makes total cmap path too long";
337  return -1;
338  }
339  strcpy(new_keyname, path);
340  if (strcmp(path, "") != 0) {
341  strcat(new_keyname, ".");
342  }
343  strcat(new_keyname, section);
344 
345  if (!parser_cb(new_keyname, NULL, NULL, PARSER_CB_SECTION_START, error_string, config_map, user_data)) {
346  return -1;
347  }
348 
349  if (parse_section(fp, new_keyname, error_string, depth + 1, parser_cb, config_map, user_data))
350  return -1;
351 
352  continue ;
353  }
354 
355  /* New key/value */
356  if ((loc = strchr_rs (line, ':'))) {
357  char *key;
358  char *value;
359 
360  *(loc-1) = '\0';
361  key = remove_whitespace(line, 1);
362  value = remove_whitespace(loc, 0);
363 
364  if (strlen(path) + strlen(key) + 1 >= ICMAP_KEYNAME_MAXLEN) {
365  *error_string = "parser error: New key makes total cmap path too long";
366  return -1;
367  }
368  strcpy(new_keyname, path);
369  if (strcmp(path, "") != 0) {
370  strcat(new_keyname, ".");
371  }
372  strcat(new_keyname, key);
373 
374  if (!parser_cb(new_keyname, key, value, PARSER_CB_ITEM, error_string, config_map, user_data)) {
375  return -1;
376  }
377 
378  continue ;
379  }
380 
381  if (strchr_rs (line, '}')) {
382  if (depth == 0) {
383  *error_string = "parser error: Unexpected closing brace";
384 
385  return -1;
386  }
387 
388  if (!parser_cb(path, NULL, NULL, PARSER_CB_SECTION_END, error_string, config_map, user_data)) {
389  return -1;
390  }
391 
392  return 0;
393  }
394  }
395 
396  if (strcmp(path, "") != 0) {
397  *error_string = "parser error: Missing closing brace";
398  return -1;
399  }
400 
401  if (strcmp(path, "") == 0) {
402  parser_cb("", NULL, NULL, PARSER_CB_END, error_string, config_map, user_data);
403  }
404 
405  return 0;
406 }
407 
408 static int safe_atoq_range(icmap_value_types_t value_type, long long int *min_val, long long int *max_val)
409 {
410  switch (value_type) {
411  case ICMAP_VALUETYPE_INT8: *min_val = INT8_MIN; *max_val = INT8_MAX; break;
412  case ICMAP_VALUETYPE_UINT8: *min_val = 0; *max_val = UINT8_MAX; break;
413  case ICMAP_VALUETYPE_INT16: *min_val = INT16_MIN; *max_val = INT16_MAX; break;
414  case ICMAP_VALUETYPE_UINT16: *min_val = 0; *max_val = UINT16_MAX; break;
415  case ICMAP_VALUETYPE_INT32: *min_val = INT32_MIN; *max_val = INT32_MAX; break;
416  case ICMAP_VALUETYPE_UINT32: *min_val = 0; *max_val = UINT32_MAX; break;
417  default:
418  return (-1);
419  }
420 
421  return (0);
422 }
423 
424 /*
425  * Convert string str to long long int res. Type of result is target_type and currently only
426  * ICMAP_VALUETYPE_[U]INT[8|16|32] is supported.
427  * Return 0 on success, -1 on failure.
428  */
429 static int safe_atoq(const char *str, long long int *res, icmap_value_types_t target_type)
430 {
431  long long int val;
432  long long int min_val, max_val;
433  char *endptr;
434 
435  errno = 0;
436 
437  val = strtoll(str, &endptr, 10);
438  if (errno == ERANGE) {
439  return (-1);
440  }
441 
442  if (endptr == str) {
443  return (-1);
444  }
445 
446  if (*endptr != '\0') {
447  return (-1);
448  }
449 
450  if (safe_atoq_range(target_type, &min_val, &max_val) != 0) {
451  return (-1);
452  }
453 
454  if (val < min_val || val > max_val) {
455  return (-1);
456  }
457 
458  *res = val;
459  return (0);
460 }
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 
467  errno = 0;
468 
469  val = strtoull(str, &endptr, 10);
470  if (errno == ERANGE) {
471  return (-1);
472  }
473 
474  if (endptr == str) {
475  return (-1);
476  }
477 
478  if (*endptr != '\0') {
479  return (-1);
480  }
481 
482  *res = val;
483  return (0);
484 }
485 
486 static int main_config_parser_cb(const char *path,
487  char *key,
488  char *value,
489  enum parser_cb_type type,
490  const char **error_string,
491  icmap_map_t config_map,
492  void *user_data)
493 {
494  int ii;
495  long long int val;
496  long long int min_val, max_val;
498  unsigned long long int ull;
499  int add_as_string;
500  char key_name[ICMAP_KEYNAME_MAXLEN];
501  static char formated_err[256];
502  struct main_cp_cb_data *data = (struct main_cp_cb_data *)user_data;
503  struct key_value_list_item *kv_item;
504  struct list_head *iter, *iter_next;
505  int uid, gid;
506 
507  switch (type) {
508  case PARSER_CB_START:
509  memset(data, 0, sizeof(struct main_cp_cb_data));
511  break;
512  case PARSER_CB_END:
513  break;
514  case PARSER_CB_ITEM:
515  add_as_string = 1;
516 
517  switch (data->state) {
519  break;
521  if ((strcmp(path, "pload.count") == 0) ||
522  (strcmp(path, "pload.size") == 0)) {
523  val_type = ICMAP_VALUETYPE_UINT32;
524  if (safe_atoq(value, &val, val_type) != 0) {
525  goto atoi_error;
526  }
527  icmap_set_uint32_r(config_map, path, val);
528  add_as_string = 0;
529  }
530  break;
532  if ((strcmp(path, "quorum.expected_votes") == 0) ||
533  (strcmp(path, "quorum.votes") == 0) ||
534  (strcmp(path, "quorum.last_man_standing_window") == 0) ||
535  (strcmp(path, "quorum.leaving_timeout") == 0)) {
536  val_type = ICMAP_VALUETYPE_UINT32;
537  if (safe_atoq(value, &val, val_type) != 0) {
538  goto atoi_error;
539  }
540  icmap_set_uint32_r(config_map, path, val);
541  add_as_string = 0;
542  }
543 
544  if ((strcmp(path, "quorum.two_node") == 0) ||
545  (strcmp(path, "quorum.expected_votes_tracking") == 0) ||
546  (strcmp(path, "quorum.allow_downscale") == 0) ||
547  (strcmp(path, "quorum.wait_for_all") == 0) ||
548  (strcmp(path, "quorum.auto_tie_breaker") == 0) ||
549  (strcmp(path, "quorum.last_man_standing") == 0)) {
550  val_type = ICMAP_VALUETYPE_UINT8;
551  if (safe_atoq(value, &val, val_type) != 0) {
552  goto atoi_error;
553  }
554  icmap_set_uint8_r(config_map, path, val);
555  add_as_string = 0;
556  }
557  break;
559  if ((strcmp(path, "quorum.device.timeout") == 0) ||
560  (strcmp(path, "quorum.device.sync_timeout") == 0) ||
561  (strcmp(path, "quorum.device.votes") == 0)) {
562  val_type = ICMAP_VALUETYPE_UINT32;
563  if (safe_atoq(value, &val, val_type) != 0) {
564  goto atoi_error;
565  }
566  icmap_set_uint32_r(config_map, path, val);
567  add_as_string = 0;
568  }
569  if ((strcmp(path, "quorum.device.master_wins") == 0)) {
570  val_type = ICMAP_VALUETYPE_UINT8;
571  if (safe_atoq(value, &val, val_type) != 0) {
572  goto atoi_error;
573  }
574  icmap_set_uint8_r(config_map, path, val);
575  add_as_string = 0;
576  }
577  break;
579  if ((strcmp(path, "totem.version") == 0) ||
580  (strcmp(path, "totem.nodeid") == 0) ||
581  (strcmp(path, "totem.threads") == 0) ||
582  (strcmp(path, "totem.token") == 0) ||
583  (strcmp(path, "totem.token_coefficient") == 0) ||
584  (strcmp(path, "totem.token_retransmit") == 0) ||
585  (strcmp(path, "totem.hold") == 0) ||
586  (strcmp(path, "totem.token_retransmits_before_loss_const") == 0) ||
587  (strcmp(path, "totem.join") == 0) ||
588  (strcmp(path, "totem.send_join") == 0) ||
589  (strcmp(path, "totem.consensus") == 0) ||
590  (strcmp(path, "totem.merge") == 0) ||
591  (strcmp(path, "totem.downcheck") == 0) ||
592  (strcmp(path, "totem.fail_recv_const") == 0) ||
593  (strcmp(path, "totem.seqno_unchanged_const") == 0) ||
594  (strcmp(path, "totem.rrp_token_expired_timeout") == 0) ||
595  (strcmp(path, "totem.rrp_problem_count_timeout") == 0) ||
596  (strcmp(path, "totem.rrp_problem_count_threshold") == 0) ||
597  (strcmp(path, "totem.rrp_problem_count_mcast_threshold") == 0) ||
598  (strcmp(path, "totem.rrp_autorecovery_check_timeout") == 0) ||
599  (strcmp(path, "totem.heartbeat_failures_allowed") == 0) ||
600  (strcmp(path, "totem.max_network_delay") == 0) ||
601  (strcmp(path, "totem.window_size") == 0) ||
602  (strcmp(path, "totem.max_messages") == 0) ||
603  (strcmp(path, "totem.miss_count_const") == 0) ||
604  (strcmp(path, "totem.netmtu") == 0)) {
605  val_type = ICMAP_VALUETYPE_UINT32;
606  if (safe_atoq(value, &val, val_type) != 0) {
607  goto atoi_error;
608  }
609  icmap_set_uint32_r(config_map,path, val);
610  add_as_string = 0;
611  }
612  if (strcmp(path, "totem.config_version") == 0) {
613  if (str_to_ull(value, &ull) != 0) {
614  goto atoi_error;
615  }
616  icmap_set_uint64_r(config_map, path, ull);
617  add_as_string = 0;
618  }
619  if (strcmp(path, "totem.ip_version") == 0) {
620  if ((strcmp(value, "ipv4") != 0) &&
621  (strcmp(value, "ipv6") != 0)) {
622  *error_string = "Invalid ip_version type";
623 
624  return (0);
625  }
626  }
627  if (strcmp(path, "totem.crypto_type") == 0) {
628  if ((strcmp(value, "nss") != 0) &&
629  (strcmp(value, "aes256") != 0) &&
630  (strcmp(value, "aes192") != 0) &&
631  (strcmp(value, "aes128") != 0) &&
632  (strcmp(value, "3des") != 0)) {
633  *error_string = "Invalid crypto type";
634 
635  return (0);
636  }
637  }
638  if (strcmp(path, "totem.crypto_cipher") == 0) {
639  if ((strcmp(value, "none") != 0) &&
640  (strcmp(value, "aes256") != 0) &&
641  (strcmp(value, "aes192") != 0) &&
642  (strcmp(value, "aes128") != 0) &&
643  (strcmp(value, "3des") != 0)) {
644  *error_string = "Invalid cipher type";
645 
646  return (0);
647  }
648  }
649  if (strcmp(path, "totem.crypto_hash") == 0) {
650  if ((strcmp(value, "none") != 0) &&
651  (strcmp(value, "md5") != 0) &&
652  (strcmp(value, "sha1") != 0) &&
653  (strcmp(value, "sha256") != 0) &&
654  (strcmp(value, "sha384") != 0) &&
655  (strcmp(value, "sha512") != 0)) {
656  *error_string = "Invalid hash type";
657 
658  return (0);
659  }
660  }
661  break;
662 
664  if (strcmp(path, "qb.ipc_type") == 0) {
665  if ((strcmp(value, "native") != 0) &&
666  (strcmp(value, "shm") != 0) &&
667  (strcmp(value, "socket") != 0)) {
668  *error_string = "Invalid qb ipc_type";
669 
670  return (0);
671  }
672  }
673  break;
674 
676  if (strcmp(path, "totem.interface.ringnumber") == 0) {
677  val_type = ICMAP_VALUETYPE_UINT8;
678  if (safe_atoq(value, &val, val_type) != 0) {
679  goto atoi_error;
680  }
681 
682  data->ringnumber = val;
683  add_as_string = 0;
684  }
685  if (strcmp(path, "totem.interface.bindnetaddr") == 0) {
686  data->bindnetaddr = strdup(value);
687  add_as_string = 0;
688  }
689  if (strcmp(path, "totem.interface.mcastaddr") == 0) {
690  data->mcastaddr = strdup(value);
691  add_as_string = 0;
692  }
693  if (strcmp(path, "totem.interface.broadcast") == 0) {
694  data->broadcast = strdup(value);
695  add_as_string = 0;
696  }
697  if (strcmp(path, "totem.interface.mcastport") == 0) {
698  val_type = ICMAP_VALUETYPE_UINT16;
699  if (safe_atoq(value, &val, val_type) != 0) {
700  goto atoi_error;
701  }
702  data->mcastport = val;
703  add_as_string = 0;
704  }
705  if (strcmp(path, "totem.interface.ttl") == 0) {
706  val_type = ICMAP_VALUETYPE_UINT8;
707  if (safe_atoq(value, &val, val_type) != 0) {
708  goto atoi_error;
709  }
710  data->ttl = val;
711  add_as_string = 0;
712  }
713  break;
715  if (strcmp(key, "subsys") == 0) {
716  data->subsys = strdup(value);
717  if (data->subsys == NULL) {
718  *error_string = "Can't alloc memory";
719 
720  return (0);
721  }
722  } else {
723  kv_item = malloc(sizeof(*kv_item));
724  if (kv_item == NULL) {
725  *error_string = "Can't alloc memory";
726 
727  return (0);
728  }
729  memset(kv_item, 0, sizeof(*kv_item));
730 
731  kv_item->key = strdup(key);
732  kv_item->value = strdup(value);
733  if (kv_item->key == NULL || kv_item->value == NULL) {
734  free(kv_item);
735  *error_string = "Can't alloc memory";
736 
737  return (0);
738  }
739  list_init(&kv_item->list);
740  list_add(&kv_item->list, &data->logger_subsys_items_head);
741  }
742  add_as_string = 0;
743  break;
745  if (strcmp(key, "subsys") == 0) {
746  data->subsys = strdup(value);
747  if (data->subsys == NULL) {
748  *error_string = "Can't alloc memory";
749 
750  return (0);
751  }
752  } else if (strcmp(key, "name") == 0) {
753  data->logging_daemon_name = strdup(value);
754  if (data->logging_daemon_name == NULL) {
755  *error_string = "Can't alloc memory";
756 
757  return (0);
758  }
759  } else {
760  kv_item = malloc(sizeof(*kv_item));
761  if (kv_item == NULL) {
762  *error_string = "Can't alloc memory";
763 
764  return (0);
765  }
766  memset(kv_item, 0, sizeof(*kv_item));
767 
768  kv_item->key = strdup(key);
769  kv_item->value = strdup(value);
770  if (kv_item->key == NULL || kv_item->value == NULL) {
771  free(kv_item);
772  *error_string = "Can't alloc memory";
773 
774  return (0);
775  }
776  list_init(&kv_item->list);
777  list_add(&kv_item->list, &data->logger_subsys_items_head);
778  }
779  add_as_string = 0;
780  break;
782  if (strcmp(key, "uid") == 0) {
783  uid = uid_determine(value);
784  if (uid == -1) {
785  *error_string = error_string_response;
786  return (0);
787  }
788  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.uid.%u",
789  uid);
790  icmap_set_uint8_r(config_map, key_name, 1);
791  add_as_string = 0;
792  } else if (strcmp(key, "gid") == 0) {
793  gid = gid_determine(value);
794  if (gid == -1) {
795  *error_string = error_string_response;
796  return (0);
797  }
798  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.gid.%u",
799  gid);
800  icmap_set_uint8_r(config_map, key_name, 1);
801  add_as_string = 0;
802  } else {
803  *error_string = "uidgid: Only uid and gid are allowed items";
804  return (0);
805  }
806  break;
808  if (strcmp(key, "memberaddr") != 0) {
809  *error_string = "Only memberaddr is allowed in member section";
810 
811  return (0);
812  }
813 
814  kv_item = malloc(sizeof(*kv_item));
815  if (kv_item == NULL) {
816  *error_string = "Can't alloc memory";
817 
818  return (0);
819  }
820  memset(kv_item, 0, sizeof(*kv_item));
821 
822  kv_item->key = strdup(key);
823  kv_item->value = strdup(value);
824  if (kv_item->key == NULL || kv_item->value == NULL) {
825  free(kv_item);
826  *error_string = "Can't alloc memory";
827 
828  return (0);
829  }
830  list_init(&kv_item->list);
831  list_add(&kv_item->list, &data->member_items_head);
832  add_as_string = 0;
833  break;
835  break;
837  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.%s", data->node_number, key);
838  if ((strcmp(key, "nodeid") == 0) ||
839  (strcmp(key, "quorum_votes") == 0)) {
840  val_type = ICMAP_VALUETYPE_UINT32;
841  if (safe_atoq(value, &val, val_type) != 0) {
842  goto atoi_error;
843  }
844 
845  icmap_set_uint32_r(config_map, key_name, val);
846  add_as_string = 0;
847  }
848 
849  if (strcmp(key, "ring0_addr") == 0) {
850  data->ring0_addr_added = 1;
851  }
852 
853  if (add_as_string) {
854  icmap_set_string_r(config_map, key_name, value);
855  add_as_string = 0;
856  }
857  break;
858  }
859 
860  if (add_as_string) {
861  icmap_set_string_r(config_map, path, value);
862  }
863  break;
865  if (strcmp(path, "totem.interface") == 0) {
867  data->ringnumber = 0;
868  data->mcastport = -1;
869  data->ttl = -1;
870  list_init(&data->member_items_head);
871  };
872  if (strcmp(path, "totem") == 0) {
874  };
875  if (strcmp(path, "qb") == 0) {
877  }
878  if (strcmp(path, "logging.logger_subsys") == 0) {
880  list_init(&data->logger_subsys_items_head);
881  data->subsys = NULL;
882  }
883  if (strcmp(path, "logging.logging_daemon") == 0) {
885  list_init(&data->logger_subsys_items_head);
886  data->subsys = NULL;
887  data->logging_daemon_name = NULL;
888  }
889  if (strcmp(path, "uidgid") == 0) {
891  }
892  if (strcmp(path, "totem.interface.member") == 0) {
894  }
895  if (strcmp(path, "quorum") == 0) {
897  }
898  if (strcmp(path, "quorum.device") == 0) {
900  }
901  if (strcmp(path, "nodelist") == 0) {
903  data->node_number = 0;
904  }
905  if (strcmp(path, "nodelist.node") == 0) {
907  data->ring0_addr_added = 0;
908  }
909  break;
911  switch (data->state) {
913  break;
916  break;
918  /*
919  * Create new interface section
920  */
921  if (data->bindnetaddr != NULL) {
922  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr",
923  data->ringnumber);
924  icmap_set_string_r(config_map, key_name, data->bindnetaddr);
925 
926  free(data->bindnetaddr);
927  data->bindnetaddr = NULL;
928  }
929 
930  if (data->mcastaddr != NULL) {
931  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr",
932  data->ringnumber);
933  icmap_set_string_r(config_map, key_name, data->mcastaddr);
934 
935  free(data->mcastaddr);
936  data->mcastaddr = NULL;
937  }
938 
939  if (data->broadcast != NULL) {
940  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast",
941  data->ringnumber);
942  icmap_set_string_r(config_map, key_name, data->broadcast);
943 
944  free(data->broadcast);
945  data->broadcast = NULL;
946  }
947 
948  if (data->mcastport > -1) {
949  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport",
950  data->ringnumber);
951  icmap_set_uint16_r(config_map, key_name, data->mcastport);
952  }
953 
954  if (data->ttl > -1) {
955  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl",
956  data->ringnumber);
957  icmap_set_uint8_r(config_map, key_name, data->ttl);
958  }
959 
960  ii = 0;
961  for (iter = data->member_items_head.next;
962  iter != &data->member_items_head; iter = iter_next) {
963  kv_item = list_entry(iter, struct key_value_list_item, list);
964 
965  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.%u",
966  data->ringnumber, ii);
967  icmap_set_string_r(config_map, key_name, kv_item->value);
968 
969  iter_next = iter->next;
970 
971  free(kv_item->value);
972  free(kv_item->key);
973  free(kv_item);
974  ii++;
975  }
976 
978  break;
981  break;
984  break;
986  if (data->subsys == NULL) {
987  *error_string = "No subsys key in logger_subsys directive";
988 
989  return (0);
990  }
991 
992  for (iter = data->logger_subsys_items_head.next;
993  iter != &data->logger_subsys_items_head; iter = iter_next) {
994  kv_item = list_entry(iter, struct key_value_list_item, list);
995 
996  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.%s",
997  data->subsys, kv_item->key);
998  icmap_set_string_r(config_map, key_name, kv_item->value);
999 
1000  iter_next = iter->next;
1001 
1002  free(kv_item->value);
1003  free(kv_item->key);
1004  free(kv_item);
1005  }
1006 
1007  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.subsys",
1008  data->subsys);
1009  icmap_set_string_r(config_map, key_name, data->subsys);
1010 
1011  free(data->subsys);
1012 
1014  break;
1016  if (data->logging_daemon_name == NULL) {
1017  *error_string = "No name key in logging_daemon directive";
1018 
1019  return (0);
1020  }
1021 
1022  for (iter = data->logger_subsys_items_head.next;
1023  iter != &data->logger_subsys_items_head; iter = iter_next) {
1024  kv_item = list_entry(iter, struct key_value_list_item, list);
1025 
1026  if (data->subsys == NULL) {
1027  if (strcmp(data->logging_daemon_name, "corosync") == 0) {
1028  snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
1029  "logging.%s",
1030  kv_item->key);
1031  } else {
1032  snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
1033  "logging.logging_daemon.%s.%s",
1034  data->logging_daemon_name, kv_item->key);
1035  }
1036  } else {
1037  if (strcmp(data->logging_daemon_name, "corosync") == 0) {
1038  snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
1039  "logging.logger_subsys.%s.%s",
1040  data->subsys,
1041  kv_item->key);
1042  } else {
1043  snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
1044  "logging.logging_daemon.%s.%s.%s",
1045  data->logging_daemon_name, data->subsys,
1046  kv_item->key);
1047  }
1048  }
1049  icmap_set_string_r(config_map, key_name, kv_item->value);
1050 
1051  iter_next = iter->next;
1052 
1053  free(kv_item->value);
1054  free(kv_item->key);
1055  free(kv_item);
1056  }
1057 
1058  if (data->subsys == NULL) {
1059  if (strcmp(data->logging_daemon_name, "corosync") != 0) {
1060  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.name",
1061  data->logging_daemon_name);
1062  icmap_set_string_r(config_map, key_name, data->logging_daemon_name);
1063  }
1064  } else {
1065  if (strcmp(data->logging_daemon_name, "corosync") == 0) {
1066  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.subsys",
1067  data->subsys);
1068  icmap_set_string_r(config_map, key_name, data->subsys);
1069 
1070  } else {
1071  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.%s.subsys",
1072  data->logging_daemon_name, data->subsys);
1073  icmap_set_string_r(config_map, key_name, data->subsys);
1074  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.%s.name",
1075  data->logging_daemon_name, data->subsys);
1076  icmap_set_string_r(config_map, key_name, data->logging_daemon_name);
1077  }
1078  }
1079 
1080  free(data->subsys);
1081  free(data->logging_daemon_name);
1082 
1084  break;
1087  break;
1090  break;
1093  break;
1096  break;
1099  break;
1101  if (!data->ring0_addr_added) {
1102  *error_string = "No ring0_addr specified for node";
1103 
1104  return (0);
1105  }
1106  data->node_number++;
1108  break;
1109  }
1110  break;
1111  }
1112 
1113  return (1);
1114 
1115 atoi_error:
1116  min_val = max_val = 0;
1117  /*
1118  * This is really assert, because developer ether doesn't set val_type correctly or
1119  * we've got here after some nasty memory overwrite
1120  */
1121  assert(safe_atoq_range(val_type, &min_val, &max_val) == 0);
1122 
1123  snprintf(formated_err, sizeof(formated_err),
1124  "Value of key \"%s\" is expected to be integer in range (%lld..%lld), but \"%s\" was given",
1125  key, min_val, max_val, value);
1126  *error_string = formated_err;
1127 
1128  return (0);
1129 }
1130 
1131 static int uidgid_config_parser_cb(const char *path,
1132  char *key,
1133  char *value,
1134  enum parser_cb_type type,
1135  const char **error_string,
1136  icmap_map_t config_map,
1137  void *user_data)
1138 {
1139  char key_name[ICMAP_KEYNAME_MAXLEN];
1140  int uid, gid;
1141 
1142  switch (type) {
1143  case PARSER_CB_START:
1144  break;
1145  case PARSER_CB_END:
1146  break;
1147  case PARSER_CB_ITEM:
1148  if (strcmp(path, "uidgid.uid") == 0) {
1149  uid = uid_determine(value);
1150  if (uid == -1) {
1151  *error_string = error_string_response;
1152  return (0);
1153  }
1154  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.uid.%u",
1155  uid);
1156  icmap_set_uint8_r(config_map, key_name, 1);
1157  } else if (strcmp(path, "uidgid.gid") == 0) {
1158  gid = gid_determine(value);
1159  if (gid == -1) {
1160  *error_string = error_string_response;
1161  return (0);
1162  }
1163  snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.gid.%u",
1164  gid);
1165  icmap_set_uint8_r(config_map, key_name, 1);
1166  } else {
1167  *error_string = "uidgid: Only uid and gid are allowed items";
1168  return (0);
1169  }
1170  break;
1172  if (strcmp(path, "uidgid") != 0) {
1173  *error_string = "uidgid: Can't add subsection different than uidgid";
1174  return (0);
1175  };
1176  break;
1177  case PARSER_CB_SECTION_END:
1178  break;
1179  }
1180 
1181  return (1);
1182 }
1183 
1184 static int read_uidgid_files_into_icmap(
1185  const char **error_string,
1186  icmap_map_t config_map)
1187 {
1188  FILE *fp;
1189  const char *dirname;
1190  DIR *dp;
1191  struct dirent *dirent;
1192  struct dirent *entry;
1193  char filename[PATH_MAX + FILENAME_MAX + 1];
1194  int res = 0;
1195  size_t len;
1196  int return_code;
1197  struct stat stat_buf;
1198  char key_name[ICMAP_KEYNAME_MAXLEN];
1199 
1200  dirname = COROSYSCONFDIR "/uidgid.d";
1201  dp = opendir (dirname);
1202 
1203  if (dp == NULL)
1204  return 0;
1205 
1206  len = offsetof(struct dirent, d_name) + FILENAME_MAX + 1;
1207 
1208  entry = malloc(len);
1209  if (entry == NULL) {
1210  res = 0;
1211  goto error_exit;
1212  }
1213 
1214  for (return_code = readdir_r(dp, entry, &dirent);
1215  dirent != NULL && return_code == 0;
1216  return_code = readdir_r(dp, entry, &dirent)) {
1217 
1218  snprintf(filename, sizeof (filename), "%s/%s", dirname, dirent->d_name);
1219  res = stat (filename, &stat_buf);
1220  if (res == 0 && S_ISREG(stat_buf.st_mode)) {
1221 
1222  fp = fopen (filename, "r");
1223  if (fp == NULL) continue;
1224 
1225  key_name[0] = 0;
1226 
1227  res = parse_section(fp, key_name, error_string, 0, uidgid_config_parser_cb, config_map, NULL);
1228 
1229  fclose (fp);
1230 
1231  if (res != 0) {
1232  goto error_exit;
1233  }
1234  }
1235  }
1236 
1237 error_exit:
1238  free (entry);
1239  closedir(dp);
1240 
1241  return res;
1242 }
1243 
1244 /* Read config file and load into icmap */
1245 static int read_config_file_into_icmap(
1246  const char **error_string,
1247  icmap_map_t config_map)
1248 {
1249  FILE *fp;
1250  const char *filename;
1251  char *error_reason = error_string_response;
1252  int res;
1253  char key_name[ICMAP_KEYNAME_MAXLEN];
1254  struct main_cp_cb_data data;
1255 
1256  filename = getenv ("COROSYNC_MAIN_CONFIG_FILE");
1257  if (!filename)
1258  filename = COROSYSCONFDIR "/corosync.conf";
1259 
1260  fp = fopen (filename, "r");
1261  if (fp == NULL) {
1262  char error_str[100];
1263  const char *error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
1264  snprintf (error_reason, sizeof(error_string_response),
1265  "Can't read file %s reason = (%s)",
1266  filename, error_ptr);
1267  *error_string = error_reason;
1268  return -1;
1269  }
1270 
1271  key_name[0] = 0;
1272 
1273  res = parse_section(fp, key_name, error_string, 0, main_config_parser_cb, config_map, &data);
1274 
1275  fclose(fp);
1276 
1277  if (res == 0) {
1278  res = read_uidgid_files_into_icmap(error_string, config_map);
1279  }
1280 
1281  if (res == 0) {
1282  snprintf (error_reason, sizeof(error_string_response),
1283  "Successfully read main configuration file '%s'.", filename);
1284  *error_string = error_reason;
1285  }
1286 
1287  return res;
1288 }
char * logging_daemon_name
Definition: coroparse.c:116
uint32_t value
struct list_head list
Definition: coroparse.c:101
struct list_head * next
Definition: list.h:47
struct list_head logger_subsys_items_head
Definition: coroparse.c:114
struct list_head member_items_head
Definition: coroparse.c:117
int coroparse_configparse(icmap_map_t config_map, const char **error_string)
Definition: coroparse.c:247
cs_error_t icmap_set_uint64_r(const icmap_map_t map, const char *key_name, uint64_t value)
Definition: icmap.c:553
char * mcastaddr
Definition: coroparse.c:109
char * bindnetaddr
Definition: coroparse.c:108
#define COROSYSCONFDIR
Definition: config.h:11
enum main_cp_cb_data_state state
Definition: coroparse.c:105
Definition: list.h:46
parser_cb_type
Definition: coroparse.c:66
cs_error_t icmap_set_string_r(const icmap_map_t map, const char *key_name, const char *value)
Definition: icmap.c:571
#define ICMAP_KEYNAME_MAXLEN
Definition: icmap.h:48
cs_error_t icmap_set_uint8_r(const icmap_map_t map, const char *key_name, uint8_t value)
Definition: icmap.c:517
void * user_data
Definition: sam.c:126
int(* parser_cb_f)(const char *path, char *key, char *value, enum parser_cb_type type, const char **error_string, icmap_map_t config_map, void *user_data)
Definition: coroparse.c:74
cs_error_t icmap_set_uint32_r(const icmap_map_t map, const char *key_name, uint32_t value)
Definition: icmap.c:541
char * broadcast
Definition: coroparse.c:110
Linked list API.
int ring0_addr_added
Definition: coroparse.c:120
cs_error_t icmap_set_uint16_r(const icmap_map_t map, const char *key_name, uint16_t value)
Definition: icmap.c:529
main_cp_cb_data_state
Definition: coroparse.c:82
#define list_entry(ptr, type, member)
Definition: list.h:84
char type
Definition: totemrrp.c:518
icmap_value_types_t
Definition: icmap.h:58