]> git.ipfire.org Git - thirdparty/dhcp.git/commitdiff
- Added a configuration function, 'gethostname()', which calls the system
authorDavid Hankins <dhankins@isc.org>
Mon, 6 Jul 2009 23:29:52 +0000 (23:29 +0000)
committerDavid Hankins <dhankins@isc.org>
Mon, 6 Jul 2009 23:29:52 +0000 (23:29 +0000)
  function of the same name and presents the results as a data expression.
  This function can be used to incorporate the system level hostname of
  the system the DHCP software is operating on in responses or queries (such
  as including a failover partner's hostname in a dhcp message or binding
  scope, or having a DHCP client send any system hostname in the host-name or
  FQDN options by default).  [ISC-Bugs #17351]

RELNOTES
client/dhclient.conf
common/conflex.c
common/dhcp-eval.5
common/parse.c
common/print.c
common/tree.c
includes/dhctoken.h
includes/tree.h

index aa1fa1bf8737227d9b96830de3a04ca6e1b8b5b3..0a6237f9c6a91647f2f8c132c72ca068f2df3ed5 100644 (file)
--- a/RELNOTES
+++ b/RELNOTES
@@ -58,6 +58,14 @@ work on other platforms. Please report any problems and suggested fixes to
   please carefully review the documentation of this parameter in the
   dhcpd.conf(5) manpage before determining to use it yourself.
 
+- Added a configuration function, 'gethostname()', which calls the system
+  function of the same name and presents the results as a data expression.
+  This function can be used to incorporate the system level hostname of
+  the system the DHCP software is operating on in responses or queries (such
+  as including a failover partner's hostname in a dhcp message or binding
+  scope, or having a DHCP client send any system hostname in the host-name or
+  FQDN options by default).
+
                        Changes since 4.1.0 (bug fixes)
 
 - Remove infinite loop in token_print_indent_concat().
index 147e0045a5d8fdff845e4b3951b92f356bff6acd..64c6ce110357387fbcbb6f2aa61ed7873ecdcab4 100644 (file)
@@ -1,4 +1,4 @@
-send host-name "andare.fugue.com";
+send host-name = pick-first-value(gethostname(), "ISC-dhclient");
 send dhcp-client-identifier 1:0:a0:24:ab:fb:9c;
 send dhcp-lease-time 3600;
 supersede domain-name "fugue.com home.vix.com";
index 2ce948401363197fee869b68f954ab2a5f121c81..41edb89aa76a681f59dd8600c38750f5cc121fd5 100644 (file)
@@ -984,12 +984,17 @@ intern(char *atom, enum dhcp_token dfv) {
                        return TOKEN_FREE;
                break;
              case 'g':
+               if (!strncasecmp(atom + 1, "et", 2)) {
+                       if (!strcasecmp(atom + 3, "-lease-hostnames"))
+                               return GET_LEASE_HOSTNAMES;
+                       if (!strcasecmp(atom + 3, "hostname"))
+                               return GETHOSTNAME;
+                       break;
+               }
                if (!strcasecmp (atom + 1, "iaddr"))
                        return GIADDR;
                if (!strcasecmp (atom + 1, "roup"))
                        return GROUP;
-               if (!strcasecmp (atom + 1, "et-lease-hostnames"))
-                       return GET_LEASE_HOSTNAMES;
                break;
              case 'h':
                if (!strcasecmp(atom + 1, "ash"))
index 1bde0b2b453513a17e5323e7c452abe5b2aeba63..d497da9d66dd384f852f43af57e1656dbdd9f83f 100644 (file)
@@ -1,4 +1,4 @@
-.\"    $Id: dhcp-eval.5,v 1.27 2007/06/07 15:52:29 dhankins Exp $
+.\"    $Id: dhcp-eval.5,v 1.28 2009/07/06 23:29:51 dhankins Exp $
 .\"
 .\" Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
 .\" Copyright (c) 1996-2003 by Internet Software Consortium
@@ -247,6 +247,16 @@ The \fBconfig-option\fR operator returns the value for the specified option
 that the DHCP client or server has been configured to send.
 .RE
 .PP
+.B gethostname()
+.PP
+.RS 0.25i
+The \fBgethostname()\fR function returns a data string whose contents are a
+character string, the results of calling gethostname() on the local
+system with a size limit of 255 bytes (not including NULL terminator).  This
+can be used for example to configure dhclient to send the local hostname
+without knowing the local hostname at the time dhclient.conf is written.
+.RE
+.PP
 .B hardware
 .PP
 .RS 0.25i
index d2842b484ae29cb78df62459282bf1bbc2094980..feaec7df2cdde679af0c684a999cf2bef4571503 100644 (file)
@@ -4503,6 +4503,22 @@ int parse_non_binary (expr, cfile, lose, context)
                        goto norparen;
                break;
 
+               /* This parses 'gethostname()'. */
+             case GETHOSTNAME:
+               token = next_token(&val, NULL, cfile);
+               if (!expression_allocate(expr, MDL))
+                       log_fatal("can't allocate expression");
+               (*expr)->op = expr_gethostname;
+
+               token = next_token(NULL, NULL, cfile);
+               if (token != LPAREN)
+                       goto nolparen;
+
+               token = next_token(NULL, NULL, cfile);
+               if (token != RPAREN)
+                       goto norparen;
+               break;
+
                /* Not a valid start to an expression... */
              default:
                if (token != NAME && token != NUMBER_OR_NAME)
index a95198d7d90f14859d6945b9943265506a8c08d9..6592b889aa872a1b7b9bfb523692e5dbd67efb54 100644 (file)
@@ -1062,6 +1062,13 @@ static unsigned print_subexpression (expr, buf, len)
                        return rv;
                }
 
+             case expr_gethostname:
+               if (len > 13) {
+                       strcpy(buf, "(gethostname)");
+                       return 13;
+               }
+               break;
+
              default:
                log_fatal("Impossible case at %s:%d (undefined expression "
                          "%d).", MDL, expr->op);
index 5244a1965c31b18dfdd98eee2624f094584e0e7a..2e4616f3cb32a8b0f98f7b9ca0a7958f1e2accb3 100644 (file)
@@ -951,6 +951,7 @@ int evaluate_dns_expression (result, packet, lease, client_state, in_options,
              case expr_config_option:
              case expr_leased_address:
              case expr_null:
+             case expr_gethostname:
                log_error ("Data opcode in evaluate_dns_expression: %d",
                      expr -> op);
                return 0;
@@ -1376,6 +1377,7 @@ int evaluate_boolean_expression (result, packet, lease, client_state,
              case expr_null:
              case expr_filename:
              case expr_sname:
+             case expr_gethostname:
                log_error ("Data opcode in evaluate_boolean_expression: %d",
                      expr -> op);
                return 0;
@@ -2299,6 +2301,39 @@ int evaluate_data_expression (result, packet, lease, client_state,
 #endif
                return s0;
 
+               /* Provide the system's local hostname as a return value. */
+             case expr_gethostname:
+               /*
+                * Allocate a buffer to return.
+                *
+                * The largest valid hostname is maybe 64 octets at a single
+                * label, or 255 octets if you think a hostname is allowed
+                * to contain labels (plus termination).
+                */
+               memset(result, 0, sizeof(*result));
+               if (!buffer_allocate(&result->buffer, 255, file, line)) {
+                       log_error("data: gethostname(): no memory for buffer");
+                       return 0;
+               }
+               result->data = result->buffer->data;
+
+               /*
+                * On successful completion, gethostname() resturns 0.  It may
+                * not null-terminate the string if there was insufficient
+                * space.
+                */
+               if (!gethostname((char *)result->buffer->data, 255)) {
+                       if (result->buffer->data[255] == '\0')
+                               result->len =
+                                       strlen((char *)result->buffer->data);
+                       else
+                               result->len = 255;
+                       return 1;
+               }
+
+               data_string_forget(result, MDL);
+               return 0;
+
              case expr_check:
              case expr_equal:
              case expr_not_equal:
@@ -2420,6 +2455,7 @@ int evaluate_numeric_expression (result, packet, lease, client_state,
              case expr_config_option:
              case expr_leased_address:
              case expr_null:
+             case expr_gethostname:
                log_error ("Data opcode in evaluate_numeric_expression: %d",
                      expr -> op);
                return 0;
@@ -3202,6 +3238,7 @@ void expression_dereference (eptr, file, line)
              case expr_exists:
              case expr_known:
              case expr_null:
+             case expr_gethostname:
                break;
 
              default:
@@ -3261,7 +3298,8 @@ int is_data_expression (expr)
                expr->op == expr_host_decl_name ||
                expr->op == expr_leased_address ||
                expr->op == expr_config_option ||
-               expr->op == expr_null);
+               expr->op == expr_null ||
+               expr->op == expr_gethostname);
 }
 
 int is_numeric_expression (expr)
@@ -3364,6 +3402,7 @@ static int op_val (op)
              case expr_binary_or:
              case expr_binary_xor:
              case expr_client_state:
+             case expr_gethostname:
                return 100;
 
              case expr_equal:
@@ -3457,6 +3496,7 @@ enum expression_context op_context (op)
              case expr_arg:
              case expr_funcall:
              case expr_function:
+             case expr_gethostname:
                return context_any;
 
              case expr_equal:
@@ -3988,6 +4028,11 @@ int write_expression (file, expr, col, indent, firstp)
                col = token_print_indent (file, col, indent, "", "", ")");
                break;
 
+             case expr_gethostname:
+               col = token_print_indent(file, col, indent, "", "",
+                                        "gethostname()");
+               break;
+
              default:
                log_fatal ("invalid expression type in print_expression: %d",
                           expr -> op);
@@ -4241,6 +4286,7 @@ int data_subexpression_length (int *rv,
              case expr_binary_or:
              case expr_binary_xor:
              case expr_client_state:
+             case expr_gethostname:
                return 0;
        }
        return 0;
index 01ed130372484ca924e60f8bfdf564bd5a563f37..d47506a0a2e34077ef2a1f6ff25f3dda23bdcf86 100644 (file)
@@ -355,7 +355,8 @@ enum dhcp_token {
        FIXED_PREFIX6 = 658,
        ANYCAST_MAC = 659,
        CONFLICT_DONE = 660,
-       AUTO_PARTNER_DOWN = 661
+       AUTO_PARTNER_DOWN = 661,
+       GETHOSTNAME = 662
 };
 
 #define is_identifier(x)       ((x) >= FIRST_TOKEN &&  \
index e7bde430854e436e6ffe3bd331a2a15ffd338e9f..9c37bcc508da15233f24d3ff6e50d447334f8df2 100644 (file)
@@ -197,7 +197,8 @@ enum expr_op {
        expr_ucase,
        expr_lcase,
        expr_regex_match,
-       expr_iregex_match
+       expr_iregex_match,
+       expr_gethostname
 };
 
 struct expression {