dhclient - Change config 'ignore' directive behaviour.
authorAntonio Huete Jimenez <tuxillo@quantumachine.net>
Sat, 9 Aug 2014 16:24:54 +0000 (18:24 +0200)
committerAntonio Huete Jimenez <tuxillo@quantumachine.net>
Sat, 9 Aug 2014 16:30:02 +0000 (18:30 +0200)
- Change dhclient.conf directive 'ignore' to take a list of option names
  rather than list of option declarations. e.g. 'ignore routers;'
  instead of 'ignore routers 1.2.3.4;' The value in the declaration
  was being ignored anyway.
- While there clean up the related code a bit.

Taken-from: OpenBSD

sbin/dhclient/clparse.c
sbin/dhclient/dhclient.c
sbin/dhclient/dhclient.conf.5
sbin/dhclient/dhcpd.h
sbin/dhclient/options.c

index 36e958e..a04d9b5 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: src/sbin/dhclient/clparse.c,v 1.40 2012/08/26 23:33:29 krw Exp $      */
+/*     $OpenBSD: src/sbin/dhclient/clparse.c,v 1.41 2012/10/27 23:08:53 krw Exp $      */
 
 /* Parser for dhclient config and lease files... */
 
@@ -154,7 +154,8 @@ read_client_leases(void)
 void
 parse_client_statement(FILE *cfile)
 {
-       int token, code;
+       u_int8_t ignorelist[256];
+       int token, code, count, i;
 
        switch (next_token(NULL, cfile)) {
        case TOK_SEND:
@@ -171,9 +172,9 @@ parse_client_statement(FILE *cfile)
                        config->default_actions[code] = ACTION_SUPERSEDE;
                return;
        case TOK_IGNORE:
-               code = parse_option_decl(cfile, &config->defaults[0]);
-               if (code != -1)
-                       config->default_actions[code] = ACTION_IGNORE;
+               count = parse_option_list(cfile, ignorelist);
+               for (i = 0; i < count; i++)
+                       config->default_actions[ignorelist[i]] = ACTION_IGNORE;
                return;
        case TOK_APPEND:
                code = parse_option_decl(cfile, &config->defaults[0]);
index b45a45f..f9c3a4d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: src/sbin/dhclient/dhclient.c,v 1.157 2012/10/10 17:44:43 krw Exp $    */
+/*     $OpenBSD: src/sbin/dhclient/dhclient.c,v 1.158 2012/10/27 23:08:53 krw Exp $    */
 
 /*
  * Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -1417,8 +1417,7 @@ write_client_lease(struct client_lease *lease)
                if (lease->options[i].len)
                        fprintf(leaseFile, "  option %s %s;\n",
                            dhcp_options[i].name,
-                           pretty_print_option(i, lease->options[i].data,
-                           lease->options[i].len, 1, 1));
+                           pretty_print_option(i, &lease->options[i], 1));
 
        t = gmtime(&lease->renewal);
        fprintf(leaseFile, "  renew %d %d/%d/%d %02d:%02d:%02d;\n",
@@ -1479,9 +1478,9 @@ priv_script_init(char *reason)
 void
 priv_script_write_params(char *prefix, struct client_lease *lease)
 {
-       u_int8_t dbuf[1500];
-       int i, len = 0;
-       char tbuf[128];
+       char buf[256];
+       struct option_data o;
+       int i;
 
        script_set_env(prefix, "ip_address", piaddr(lease->address));
 
@@ -1513,84 +1512,75 @@ priv_script_write_params(char *prefix, struct client_lease *lease)
        if (lease->server_name)
                script_set_env(prefix, "server_name",
                    lease->server_name);
+
        for (i = 0; i < 256; i++) {
-               u_int8_t *dp = NULL;
-
-               if (config->defaults[i].len) {
-                       if (lease->options[i].len) {
-                               switch (config->default_actions[i]) {
-                               case ACTION_IGNORE:
-                                       /* handled below */
-                                       break;
-                               case ACTION_DEFAULT:
-                                       dp = lease->options[i].data;
-                                       len = lease->options[i].len;
-                                       break;
-                               case ACTION_SUPERSEDE:
-supersede:
-                                       dp = config->defaults[i].data;
-                                       len = config->defaults[i].len;
-                                       break;
-                               case ACTION_PREPEND:
-                                       len = config->defaults[i].len +
-                                           lease->options[i].len;
-                                       if (len >= sizeof(dbuf)) {
-                                               warning("no space to %s %s",
-                                                   "prepend option",
-                                                   dhcp_options[i].name);
-                                               goto supersede;
-                                       }
-                                       dp = dbuf;
-                                       memcpy(dp,
-                                           config->defaults[i].data,
-                                           config->defaults[i].len);
-                                       memcpy(dp +
-                                           config->defaults[i].len,
-                                           lease->options[i].data,
-                                           lease->options[i].len);
-                                       dp[len] = '\0';
-                                       break;
-                               case ACTION_APPEND:
-                                       len = config->defaults[i].len +
-                                           lease->options[i].len;
-                                       if (len >= sizeof(dbuf)) {
-                                               warning("no space to %s %s",
-                                                   "append option",
-                                                   dhcp_options[i].name);
-                                               goto supersede;
-                                       }
-                                       dp = dbuf;
-                                       memcpy(dp, lease->options[i].data,
-                                           lease->options[i].len);
-                                       memcpy(dp + lease->options[i].len,
+               if (!dhcp_option_ev_name(buf, sizeof(buf), &dhcp_options[i]))
+                       continue;
+
+               switch (config->default_actions[i]) {
+               case ACTION_IGNORE:
+                        break;
+
+               case ACTION_DEFAULT:
+                       if (lease->options[i].len)
+                               script_set_env(prefix, buf,
+                                   pretty_print_option(i, &lease->options[i],
+                                       0));
+                       else if (config->defaults[i].len)
+                               script_set_env(prefix, buf,
+                                   pretty_print_option(i, &config->defaults[i],
+                                       0));
+                       break;
+
+               case ACTION_SUPERSEDE:
+                       if (config->defaults[i].len)
+                               script_set_env(prefix, buf,
+                                   pretty_print_option(i, &config->defaults[i],
+                                       0));
+                       break;
+
+               case ACTION_PREPEND:
+                       o.len = config->defaults[i].len + lease->options[i].len;
+                       if (o.len > 0) {
+                               o.data = calloc(1, o.len);
+                               if (o.data == NULL)
+                                       error("no space to prepend '%s' to %s",
                                            config->defaults[i].data,
-                                           config->defaults[i].len);
-                                       dp[len] = '\0';
-                               }
-                       } else {
-                               dp = config->defaults[i].data;
-                               len = config->defaults[i].len;
+                                           dhcp_options[i].name);
+                               memcpy(o.data, config->defaults[i].data,
+                                   config->defaults[i].len);
+                               memcpy(o.data + config->defaults[i].len,
+                                   lease->options[i].data,
+                                   lease->options[i].len);
+                               script_set_env(prefix, buf,
+                                   pretty_print_option(i, &o, 0));
+                               free(o.data);
                        }
-               } else if (lease->options[i].len) {
-                       len = lease->options[i].len;
-                       dp = lease->options[i].data;
-               } else {
-                       len = 0;
-               }
-               if (len && config->default_actions[i] == ACTION_IGNORE) {
-                       len = 0;
-               }
-               if (len) {
-                       char name[256];
+                       break;
 
-                       if (dhcp_option_ev_name(name, sizeof(name),
-                           &dhcp_options[i]))
-                               script_set_env(prefix, name,
-                                   pretty_print_option(i, dp, len, 0, 0));
+               case ACTION_APPEND:
+                       o.len = config->defaults[i].len + lease->options[i].len;
+                       if (o.len > 0) {
+                               o.data = calloc(1, o.len);
+                               if (o.data == NULL)
+                                       error("no space to append '%s' to %s",
+                                           config->defaults[i].data,
+                                           dhcp_options[i].name);
+                               memcpy(o.data, lease->options[i].data,
+                                   lease->options[i].len);
+                               memcpy(o.data + lease->options[i].len,
+                                   config->defaults[i].data,
+                                   config->defaults[i].len);
+                               script_set_env(prefix, buf,
+                                   pretty_print_option(i, &o, 0));
+                               free(o.data);
+                       }
+                       break;
                }
        }
-       snprintf(tbuf, sizeof(tbuf), "%d", (int)lease->expiry);
-       script_set_env(prefix, "expiry", tbuf);
+
+       snprintf(buf, sizeof(buf), "%d", (int)lease->expiry);
+       script_set_env(prefix, "expiry", buf);
 }
 
 void
@@ -1822,8 +1812,7 @@ check_option(struct client_lease *l, int option)
 
        /* we use this, since this is what gets passed to dhclient-script */
 
-       opbuf = pretty_print_option(option, l->options[option].data,
-           l->options[option].len, 0, 0);
+       opbuf = pretty_print_option(option, &l->options[option], 0);
 
        sbuf = option_as_string(option, l->options[option].data,
            l->options[option].len);
index f81dabb..cd40968 100644 (file)
@@ -1,4 +1,4 @@
-.\"    $OpenBSD: src/sbin/dhclient/dhclient.conf.5,v 1.23 2012/09/22 20:09:43 jmc Exp $
+.\"    $OpenBSD: src/sbin/dhclient/dhclient.conf.5,v 1.24 2012/10/27 23:08:53 krw Exp $
 .\"
 .\" Copyright (c) 1997 The Internet Software Consortium.
 .\" All rights reserved.
@@ -36,7 +36,7 @@
 .\" see ``http://www.isc.org/isc''.  To learn more about Vixie
 .\" Enterprises, see ``http://www.vix.com''.
 .\"
-.Dd August 7, 2014
+.Dd August 9, 2014
 .Dt DHCLIENT.CONF 5
 .Os
 .Sh NAME
@@ -177,6 +177,13 @@ The data that can be specifically requested is what are called
 DHCP Options are defined in
 .Xr dhcp-options 5 .
 .Bl -tag -width Ds
+.It Ic ignore Op Ar option
+The
+.Ic ignore
+statement causes the client to discard values provided by the server for
+the specified options.
+Only the option names should be specified in the ignore statement \- not
+option parameters.
 .It Ic request Op Ar option , ... ;
 The
 .Ic request
@@ -238,14 +245,6 @@ Use
 for the given
 .Ar option ,
 regardless of the value supplied by the server.
-.It Xo
-.Ic ignore No { Op Ar option declaration
-.Oo , Ar ... option declaration Oc }
-.Xc
-If for some set of options the client should always ignore the
-value supplied by the server, these values can be defined in the
-.Ic ignore
-statement.
 .It Ic prepend Ar option option-value ;
 Use
 .Ar option-value
index 5a77e8d..dbc2ff8 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: src/sbin/dhclient/dhcpd.h,v 1.81 2012/09/18 09:34:09 krw Exp $        */
+/*     $OpenBSD: src/sbin/dhclient/dhcpd.h,v 1.82 2012/10/27 23:08:53 krw Exp $        */
 
 /*
  * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -206,7 +206,7 @@ extern struct client_config *config;
 
 /* options.c */
 int cons_options(struct option_data *);
-char *pretty_print_option(unsigned int, unsigned char *, int, int, int);
+char *pretty_print_option(unsigned int, struct option_data *, int);
 void do_packet(int, unsigned int, struct iaddr, struct hardware *);
 
 /* errwarn.c */
index b6edda6..e582b4d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: src/sbin/dhclient/options.c,v 1.41 2012/06/26 14:46:42 krw Exp $      */
+/*     $OpenBSD: src/sbin/dhclient/options.c,v 1.42 2012/10/27 23:08:53 krw Exp $      */
 
 /* DHCP options parsing and reassembly. */
 
@@ -198,14 +198,16 @@ cons_options(struct option_data *options)
  * Format the specified option so that a human can easily read it.
  */
 char *
-pretty_print_option(unsigned int code, unsigned char *data, int len,
-    int emit_commas, int emit_quotes)
+pretty_print_option(unsigned int code, struct option_data *option,
+    int emit_punct)
 {
        static char optbuf[32768]; /* XXX */
        int hunksize = 0, numhunk = -1, numelem = 0;
        char fmtbuf[32], *op = optbuf;
        int i, j, k, opleft = sizeof(optbuf);
+       unsigned char *data = option->data;
        unsigned char *dp = data;
+       int len = option->len;
        struct in_addr foo;
        char comma;
 
@@ -213,7 +215,7 @@ pretty_print_option(unsigned int code, unsigned char *data, int len,
        if (code > 255)
                error("pretty_print_option: bad code %d", code);
 
-       if (emit_commas)
+       if (emit_punct)
                comma = ',';
        else
                comma = ' ';
@@ -316,7 +318,7 @@ pretty_print_option(unsigned int code, unsigned char *data, int len,
                        size_t oplen;
                        switch (fmtbuf[j]) {
                        case 't':
-                               if (emit_quotes) {
+                               if (emit_punct) {
                                        *op++ = '"';
                                        opleft--;
                                }
@@ -345,7 +347,7 @@ pretty_print_option(unsigned int code, unsigned char *data, int len,
                                                opleft--;
                                        }
                                }
-                               if (emit_quotes) {
+                               if (emit_punct) {
                                        *op++ = '"';
                                        opleft--;
                                }