[Git][NTPsec/ntpsec][master] Address GitLab Issue #98: includefile fails to include non-local config files

Eric S. Raymond gitlab at mg.gitlab.com
Thu Oct 6 11:54:34 UTC 2016


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
d86878cd by Eric S. Raymond at 2016-10-06T07:54:22-04:00
Address GitLab Issue #98: includefile fails to include non-local config files

- - - - -


3 changed files:

- docs/includes/misc-options.txt
- docs/index.txt
- ntpd/ntp_scanner.c


Changes:

=====================================
docs/includes/misc-options.txt
=====================================
--- a/docs/includes/misc-options.txt
+++ b/docs/includes/misc-options.txt
@@ -69,11 +69,14 @@ and that file system links, symbolic or otherwise, should be avoided.
 
 +includefile+ _includefile_::
   This command allows additional configuration commands to be included
-  from a separate file. Include files may be nested to a depth of five;
-  upon reaching the end of any include file, command processing resumes
-  in the previous configuration file. This option is useful for sites
-  that run {ntpdman} on multiple hosts, with (mostly) common
-  options (e.g., a restriction list).
+  from a separate file. Include files may be nested to a depth of
+  five; upon reaching the end of any include file, command processing
+  resumes in the previous configuration file. Relative pathnames are
+  evaluated not with respect to the current working directory but with
+  respect to the directory name of the last pushed file in the
+  stack. This option is useful for sites that run {ntpdman} on
+  multiple hosts, with (mostly) common options (e.g., a restriction
+  list).
 
 +interface+ [+listen+ | +ignore+ | +drop+] [+all+ | +ipv4+ | +ipv6+ | +wildcard+ | 'name' | 'address'[/'prefixlen']]::
   This command controls which network addresses +{ntpd}+ opens, and


=====================================
docs/index.txt
=====================================
--- a/docs/index.txt
+++ b/docs/index.txt
@@ -143,6 +143,13 @@ few will be user-visible.
   each refclock page.  One major feature of the new syntax is that
   refclock drivers are referred to by names, not numbers.
 
+* The includefile directive now evaluates relative pathnames not with
+  respect to the current working directory but with respect to the
+  directory name of the last pushed file in the stack.  This means
+  that you can run ntpd from any directory with "includefile foo"
+  in /etc/ntp.conf finding /etc/foo rather than looking for foo in
+  ypur current directory.
+
 * For the generic (parse) driver only: Using the new refclock syntax,
   the maximum number of units that can be set up changes from 4
   (numbers 0-3) to unlimited.  However, the old magic-address syntax


=====================================
ntpd/ntp_scanner.c
=====================================
--- a/ntpd/ntp_scanner.c
+++ b/ntpd/ntp_scanner.c
@@ -18,6 +18,8 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
+#include <limits.h>
+#include <libgen.h>
 
 #include "ntpd.h"
 #include "ntp_config.h"
@@ -341,6 +343,11 @@ lex_flush_stack()
  * FILE_INFO that is bound to a local/disc file. Note that 'path' must
  * not be NULL, or the function will fail.
  *
+ * Relative pathnames are interpreted relative to the directory
+ * of the previous entry on the stack, not the current directory.
+ * This is so "include foo" from within /etc/conf will reliably
+ * pick up /etc/foo.
+ *
  * Returns true if a new info record was pushed onto the stack.
  */
 bool lex_push_file(
@@ -351,7 +358,19 @@ bool lex_push_file(
 	struct FILE_INFO * next = NULL;
 
 	if (NULL != path) {
-		next = lex_open(path, mode);
+		char fullpath[PATH_MAX];
+		fullpath[0] = '\0';
+		if (path[0] != DIR_SEP && lex_stack != NULL) {
+			char *end;
+			strlcpy(fullpath,
+				dirname(lex_stack->fname),sizeof(fullpath)-2);
+			end = fullpath + strlen(fullpath);
+			*end++ = DIR_SEP;
+			*end++ = '\0';
+		}
+		strlcat(fullpath, path, sizeof(fullpath));
+		fprintf(stderr, "Opening %s\n", fullpath);
+		next = lex_open(fullpath, mode);
 		if (NULL != next) {
 			next->st_next = lex_stack;
 			lex_stack = next;



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/d86878cdc3868858917a3cceaf6993c03907d3e9
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161006/2c7a6abe/attachment.html>


More information about the vc mailing list