[patch] adds include statement in dhcpd.conf

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[patch] adds include statement in dhcpd.conf

Julien Dhaille
Hi,
this diff implements the “include” statement, like other daemons.
Also the config file can be split between different files (in my case, a big list of client is generated from a script, and I don’t want to modify dhcpd.conf).

Although, I am not even sure if this diff is decent and if it’s a good idea.
Have a good day :)


Index: conflex.c
===================================================================
RCS file: /cvs/src/usr.sbin/dhcpd/conflex.c,v
retrieving revision 1.19
diff -u -p -u -p -r1.19 conflex.c
--- conflex.c 24 Apr 2017 14:58:36 -0000 1.19
+++ conflex.c 10 May 2018 23:30:56 -0000
@@ -321,6 +321,7 @@ static const struct keywords {
  { "hardware", TOK_HARDWARE },
  { "host", TOK_HOST },
  { "hostname", TOK_HOSTNAME },
+ { "include", TOK_INCLUDE },
  { "ipsec-tunnel", TOK_IPSEC_TUNNEL },
  { "lease", TOK_LEASE },
  { "max-lease-time", TOK_MAX_LEASE_TIME },
Index: confpars.c
===================================================================
RCS file: /cvs/src/usr.sbin/dhcpd/confpars.c,v
retrieving revision 1.33
diff -u -p -u -p -r1.33 confpars.c
--- confpars.c 24 Apr 2017 14:58:36 -0000 1.33
+++ confpars.c 10 May 2018 23:30:56 -0000
@@ -329,6 +329,23 @@ parse_statement(FILE *cfile, struct grou
  parse_warn("use-host-decl-names not allowed here.");
  group->use_host_decl_names = parse_boolean(cfile);
  break;
+
+ case TOK_INCLUDE:
+ group->include = parse_string(cfile);
+ if ((cfile = fopen(group->include, "r")) == NULL)
+ fatal("Can't open %s", group->include);
+ do {
+ token = peek_token(&val, cfile);
+ if (token == EOF)
+ break;
+ declaration = parse_statement(cfile, &root_group,
+ ROOT_GROUP,
+ NULL,
+ declaration);
+ } while (1);
+ token = next_token(&val, cfile); /* Clear the peek buffer */
+ fclose(cfile);
+ break;

  case TOK_USE_LEASE_ADDR_FOR_DEFAULT_ROUTE:
  group->use_lease_addr_for_default_route =
Index: dhcpd.conf.5
===================================================================
RCS file: /cvs/src/usr.sbin/dhcpd/dhcpd.conf.5,v
retrieving revision 1.23
diff -u -p -u -p -r1.23 dhcpd.conf.5
--- dhcpd.conf.5 1 Mar 2018 20:48:11 -0000 1.23
+++ dhcpd.conf.5 10 May 2018 23:30:56 -0000
@@ -873,6 +873,25 @@ into its response (DHCP ACK or NAK) per
 In other words if the client sends the option it will receive it back.
 By default, this flag is on
 and client identifiers will be echoed back to the client.
+.Pp
+The
+.Ic include
+statement allows additional configuration files to be included:
+.Pp
+.D1 Ic include Qq Ar filename ;
+.Pp
+For example:
+.Bd -literal -offset indent
+include "/etc/dhcpd.conf.hosts";
+include "/etc/dhcpd.conf.office1";
+include "/etc/dhcpd.conf.office2";
+.Ed
+.Pp
+You can split the client declarations into different files.
+It could be use in order to keep
+.Nm
+small and easy to read, and if you want to generate clients declaration
+from an external script.
 .Sh REFERENCE: OPTION STATEMENTS
 DHCP option statements are documented in the
 .Xr dhcp-options 5
Index: dhcpd.h
===================================================================
RCS file: /cvs/src/usr.sbin/dhcpd/dhcpd.h,v
retrieving revision 1.66
diff -u -p -u -p -r1.66 dhcpd.h
--- dhcpd.h 4 Aug 2017 02:01:46 -0000 1.66
+++ dhcpd.h 10 May 2018 23:30:56 -0000
@@ -196,6 +196,7 @@ struct group {
  int allow_booting;
  int get_lease_hostnames;
  int use_host_decl_names;
+ char *include;
  int use_lease_addr_for_default_route;
  int authoritative;
  int always_reply_rfc1048;
Index: dhctoken.h
===================================================================
RCS file: /cvs/src/usr.sbin/dhcpd/dhctoken.h,v
retrieving revision 1.8
diff -u -p -u -p -r1.8 dhctoken.h
--- dhctoken.h 24 Apr 2017 14:58:36 -0000 1.8
+++ dhctoken.h 10 May 2018 23:30:56 -0000
@@ -92,6 +92,7 @@
 #define TOK_ALWAYS_REPLY_RFC1048 335
 #define TOK_IPSEC_TUNNEL 336
 #define TOK_ECHO_CLIENT_ID 337
+#define TOK_INCLUDE 338

 #define is_identifier(x) ((x) >= TOK_FIRST_TOKEN && \
  (x) != TOK_STRING && \

Reply | Threaded
Open this post in threaded view
|

Re: [patch] adds include statement in dhcpd.conf

Denis Fondras
Hi Julien,

I like the idea but implementation seems a bit naive.
Do you have a working example ?

While testing, I can notice a bug with line count (my latest hobby :D).

$ dhcpd -dn -c /etc/examples/dhcpd.conf
/etc/examples/dhcpd.conf line 21:
    filenameee
    ^
/etc/examples/dhcpd.conf line 24: /etc/examples/dhcpd.conf line 21:

^
fatal in dhcpd: Configuration file errors encountered

My test file :

* /etc/examples/dhcpd.conf :
option  domain-name "my.domain";
option  domain-name-servers 192.168.1.3, 192.168.1.5;

subnet 192.168.1.0 netmask 255.255.255.0 {
        option routers 192.168.1.1;
       
        range 192.168.1.32 192.168.1.127;

        host static-client {
                hardware ethernet 22:33:44:55:66:77;
                fixed-address 192.168.1.200;
        }

        host pxe-client {
                hardware ethernet 02:03:04:05:06:07;
                filename "pxeboot";
                next-server 192.168.1.1;
        }
        include "/usr/src/usr.sbin/dhcpd/host.conf";
}

#include "/usr/src/usr.sbin/dhcpd/subnet.conf";

* /usr/src/usr.sbin/dhcpd/host.conf :
host pxe-client2 {
        hardware ethernet 01:03:04:05:06:07;
        filenameee "pxeboot";
        next-server 192.168.1.10;
}



dhcpd.conf isn't 24 lines long and the problem is in the included file
host.conf.

Thanks.
Denis


On Fri, May 11, 2018 at 02:00:11AM +0200, Julien Dhaille wrote:

> Hi,
> this diff implements the “include” statement, like other daemons.
> Also the config file can be split between different files (in my case, a big list of client is generated from a script, and I don’t want to modify dhcpd.conf).
>
> Although, I am not even sure if this diff is decent and if it’s a good idea.
> Have a good day :)
>
>
> Index: conflex.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/dhcpd/conflex.c,v
> retrieving revision 1.19
> diff -u -p -u -p -r1.19 conflex.c
> --- conflex.c 24 Apr 2017 14:58:36 -0000 1.19
> +++ conflex.c 10 May 2018 23:30:56 -0000
> @@ -321,6 +321,7 @@ static const struct keywords {
>   { "hardware", TOK_HARDWARE },
>   { "host", TOK_HOST },
>   { "hostname", TOK_HOSTNAME },
> + { "include", TOK_INCLUDE },
>   { "ipsec-tunnel", TOK_IPSEC_TUNNEL },
>   { "lease", TOK_LEASE },
>   { "max-lease-time", TOK_MAX_LEASE_TIME },
> Index: confpars.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/dhcpd/confpars.c,v
> retrieving revision 1.33
> diff -u -p -u -p -r1.33 confpars.c
> --- confpars.c 24 Apr 2017 14:58:36 -0000 1.33
> +++ confpars.c 10 May 2018 23:30:56 -0000
> @@ -329,6 +329,23 @@ parse_statement(FILE *cfile, struct grou
>   parse_warn("use-host-decl-names not allowed here.");
>   group->use_host_decl_names = parse_boolean(cfile);
>   break;
> +
> + case TOK_INCLUDE:
> + group->include = parse_string(cfile);
> + if ((cfile = fopen(group->include, "r")) == NULL)
> + fatal("Can't open %s", group->include);
> + do {
> + token = peek_token(&val, cfile);
> + if (token == EOF)
> + break;
> + declaration = parse_statement(cfile, &root_group,
> + ROOT_GROUP,
> + NULL,
> + declaration);
> + } while (1);
> + token = next_token(&val, cfile); /* Clear the peek buffer */
> + fclose(cfile);
> + break;
>
>   case TOK_USE_LEASE_ADDR_FOR_DEFAULT_ROUTE:
>   group->use_lease_addr_for_default_route =
> Index: dhcpd.conf.5
> ===================================================================
> RCS file: /cvs/src/usr.sbin/dhcpd/dhcpd.conf.5,v
> retrieving revision 1.23
> diff -u -p -u -p -r1.23 dhcpd.conf.5
> --- dhcpd.conf.5 1 Mar 2018 20:48:11 -0000 1.23
> +++ dhcpd.conf.5 10 May 2018 23:30:56 -0000
> @@ -873,6 +873,25 @@ into its response (DHCP ACK or NAK) per
>  In other words if the client sends the option it will receive it back.
>  By default, this flag is on
>  and client identifiers will be echoed back to the client.
> +.Pp
> +The
> +.Ic include
> +statement allows additional configuration files to be included:
> +.Pp
> +.D1 Ic include Qq Ar filename ;
> +.Pp
> +For example:
> +.Bd -literal -offset indent
> +include "/etc/dhcpd.conf.hosts";
> +include "/etc/dhcpd.conf.office1";
> +include "/etc/dhcpd.conf.office2";
> +.Ed
> +.Pp
> +You can split the client declarations into different files.
> +It could be use in order to keep
> +.Nm
> +small and easy to read, and if you want to generate clients declaration
> +from an external script.
>  .Sh REFERENCE: OPTION STATEMENTS
>  DHCP option statements are documented in the
>  .Xr dhcp-options 5
> Index: dhcpd.h
> ===================================================================
> RCS file: /cvs/src/usr.sbin/dhcpd/dhcpd.h,v
> retrieving revision 1.66
> diff -u -p -u -p -r1.66 dhcpd.h
> --- dhcpd.h 4 Aug 2017 02:01:46 -0000 1.66
> +++ dhcpd.h 10 May 2018 23:30:56 -0000
> @@ -196,6 +196,7 @@ struct group {
>   int allow_booting;
>   int get_lease_hostnames;
>   int use_host_decl_names;
> + char *include;
>   int use_lease_addr_for_default_route;
>   int authoritative;
>   int always_reply_rfc1048;
> Index: dhctoken.h
> ===================================================================
> RCS file: /cvs/src/usr.sbin/dhcpd/dhctoken.h,v
> retrieving revision 1.8
> diff -u -p -u -p -r1.8 dhctoken.h
> --- dhctoken.h 24 Apr 2017 14:58:36 -0000 1.8
> +++ dhctoken.h 10 May 2018 23:30:56 -0000
> @@ -92,6 +92,7 @@
>  #define TOK_ALWAYS_REPLY_RFC1048 335
>  #define TOK_IPSEC_TUNNEL 336
>  #define TOK_ECHO_CLIENT_ID 337
> +#define TOK_INCLUDE 338
>
>  #define is_identifier(x) ((x) >= TOK_FIRST_TOKEN && \
>   (x) != TOK_STRING && \
>