Browse code

cfg_parser: allow relative paths for the cfg file

Allow relative parhs in the cfg_parser framework (used by the tls
module, ldap(s) and iptrtpproxy), by adding a new "basedir"
parameter to cfg_parser_init().
If basedir == 0 and the filename does not start with '/', the
filename path will be considered to be relative to the main ser
config file (e.g. ser -f /etc/ser/ser.cfg => relative to /etc/ser/ ).
This was the previous behaviour.
If basedir == "" the filename path will be considered to be
relative to the working directory (ser -w /tmp => relative to
/tmp).
For other basedir values, the filename path will be considered to
be relative to basedir.

Andrei Pelinescu-Onciul authored on 07/09/2010 15:03:53
Showing 2 changed files
... ...
@@ -612,19 +612,33 @@ static char* get_base_name(str* filename)
612 612
 }
613 613
 
614 614
 
615
-cfg_parser_t* cfg_parser_init(str* filename)
615
+
616
+/** intialize the config parser.
617
+ * @param basedir - path to the config file name. If 0 the path
618
+ *               (base directory) of the main ser.cfg file will be used, else
619
+ *               basedir will be concatenated to the filename. It will be
620
+ *               used only if filename is not an absolute path.
621
+ * @param filename - config filename (can include path elements).
622
+ * @return 0 on error, !=0 on success.
623
+ */
624
+cfg_parser_t* cfg_parser_init(str* basedir, str* filename)
616 625
 {
617 626
 	cfg_parser_t* st;
618
-	char* pathname, *base;
627
+	char* pathname, *base, *abs_pathname;
619 628
 
620
-	pathname = NULL;
629
+	abs_pathname = NULL;
630
+	pathname = filename->s;
621 631
 	st = NULL;
622 632
 	base = NULL;
623 633
 	
624
-	if ((pathname = get_abs_pathname(NULL, filename)) == NULL) {
625
-		ERR("cfg_parser: Error while converting %.*s to absolute pathname\n", 
626
-			STR_FMT(filename));
627
-		goto error;
634
+	/* if basedir == 0 or != "" get_abs_pathname */
635
+	if (basedir == 0  || basedir->len != 0) {
636
+		if ((abs_pathname = get_abs_pathname(basedir, filename)) == NULL) {
637
+			ERR("cfg_parser: Error while converting %.*s to absolute"
638
+					" pathname\n", STR_FMT(filename));
639
+			goto error;
640
+		}
641
+		pathname = abs_pathname;
628 642
 	}
629 643
 
630 644
 	if ((base = get_base_name(filename)) == NULL) goto error;
... ...
@@ -640,7 +654,7 @@ cfg_parser_t* cfg_parser_init(str* filename)
640 640
 		goto error;
641 641
 	}
642 642
 
643
-	pkg_free(pathname);
643
+	if (abs_pathname) pkg_free(abs_pathname);
644 644
 
645 645
 	st->file = base;
646 646
 	st->line = 1;
... ...
@@ -653,7 +667,7 @@ cfg_parser_t* cfg_parser_init(str* filename)
653 653
 		pkg_free(st);
654 654
 	}
655 655
 	if (base) pkg_free(base);
656
-	if (pathname) pkg_free(pathname);
656
+	if (abs_pathname) pkg_free(abs_pathname);
657 657
 	return NULL;
658 658
 }
659 659
 
... ...
@@ -151,7 +151,7 @@ typedef struct cfg_parser {
151 151
 
152 152
 extern struct cfg_option cfg_bool_values[];
153 153
 
154
-struct cfg_parser* cfg_parser_init(str* filename);
154
+struct cfg_parser* cfg_parser_init(str* basedir, str* filename);
155 155
 
156 156
 void cfg_section_parser(struct cfg_parser* st, cfg_func_f parser, void* param);
157 157