Print this page
    
OS-5073 lx-branded zones should request options when using DHCP
Reviewed by: Patrick Mooney <patrick.mooney@joyent.com>
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/cmd/cmd-inet/sbin/dhcpagent/defaults.c
          +++ new/usr/src/cmd/cmd-inet/sbin/dhcpagent/defaults.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  
    | 
      ↓ open down ↓ | 
    23 lines elided | 
    
      ↑ open up ↑ | 
  
  24   24   */
  25   25  
  26   26  #include <sys/types.h>
  27   27  #include <stdlib.h>
  28   28  #include <string.h>
  29   29  #include <ctype.h>
  30   30  #include <dhcpmsg.h>
  31   31  #include <stdio.h>
  32   32  #include <sys/stat.h>
  33   33  #include <libnvpair.h>
       34 +#include <zone.h>
  34   35  
  35   36  #include "common.h"
  36   37  #include "defaults.h"
  37   38  
  38   39  struct dhcp_default {
  39   40  
  40   41          const char      *df_name;       /* parameter name */
  41   42          const char      *df_default;    /* default value */
  42   43          int             df_min;         /* min value if type DF_INTEGER */
  43   44          int             df_max;         /* max value if type DF_INTEGER */
  44   45  };
  45   46  
  46   47  /*
  47   48   * note: keep in the same order as tunable parameter constants in defaults.h
  48   49   */
  49   50  
  50   51  static struct dhcp_default defaults[] = {
  51   52  
  52   53          { "RELEASE_ON_SIGTERM",  "0",    0,   0   },
  53   54          { "IGNORE_FAILED_ARP",   "1",    0,   -1  },
  54   55          { "OFFER_WAIT",          "3",    1,   20  },
  
    | 
      ↓ open down ↓ | 
    11 lines elided | 
    
      ↑ open up ↑ | 
  
  55   56          { "ARP_WAIT",            "1000", 0,   -1  },
  56   57          { "CLIENT_ID",           NULL,   0,   0   },
  57   58          { "PARAM_REQUEST_LIST",  NULL,   0,   0   },
  58   59          { "REQUEST_HOSTNAME",    "1",    0,   0   },
  59   60          { "DEBUG_LEVEL",         "0",    0,   3   },
  60   61          { "VERBOSE",             "0",    0,   0   },
  61   62          { "VERIFIED_LEASE_ONLY", "0",    0,   0   },
  62   63          { "PARAM_IGNORE_LIST",   NULL,   0,   0   }
  63   64  };
  64   65  
       66 +
  65   67  /*
       68 + * df_find_defaults(): builds the path to the default configuration file
       69 + *
       70 + *   input: void
       71 + *  output: void
       72 + */
       73 +
       74 +static const char *
       75 +df_find_defaults(void)
       76 +{
       77 +        static char agent_defaults_path[MAXPATHLEN] = { 0 };
       78 +        const char      *zroot = NULL;
       79 +
       80 +        if (agent_defaults_path[0] != '\0') {
       81 +                return agent_defaults_path;
       82 +        }
       83 +
       84 +        zroot = zone_get_nroot();
       85 +
       86 +        (void) snprintf(agent_defaults_path, MAXPATHLEN, "%s%s",
       87 +            zroot != NULL ?  zroot : "", DHCP_AGENT_DEFAULTS);
       88 +
       89 +        return agent_defaults_path;
       90 +}
       91 +
       92 +/*
  66   93   * df_build_cache(): builds the defaults nvlist cache
  67   94   *
  68   95   *   input: void
  69   96   *  output: a pointer to an nvlist of the current defaults, or NULL on failure
  70   97   */
  71   98  
  72   99  static nvlist_t *
  73  100  df_build_cache(void)
  74  101  {
      102 +        const char      *agent_defaults_path = df_find_defaults();
  75  103          char            entry[1024];
  76  104          int             i;
  77  105          char            *param, *pastv6, *value, *end;
  78  106          FILE            *fp;
  79  107          nvlist_t        *nvlist;
  80  108          struct dhcp_default *defp;
  81  109  
  82      -        if ((fp = fopen(DHCP_AGENT_DEFAULTS, "r")) == NULL)
      110 +        if ((fp = fopen(agent_defaults_path, "r")) == NULL)
  83  111                  return (NULL);
  84  112  
  85  113          if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
  86  114                  dhcpmsg(MSG_WARNING, "cannot build default value cache; "
  87  115                      "using built-in defaults");
  88  116                  (void) fclose(fp);
  89  117                  return (NULL);
  90  118          }
  91  119  
  92  120          while (fgets(entry, sizeof (entry), fp) != NULL) {
  93  121                  for (i = 0; entry[i] == ' '; i++)
  94  122                          ;
  95  123  
  96  124                  end = strrchr(entry, '\n');
  97  125                  value = strchr(entry, '=');
  98  126                  if (end == NULL || value == NULL || entry[i] == '#')
  99  127                          continue;
 100  128  
 101  129                  *end = '\0';
 102  130                  *value++ = '\0';
 103  131  
 104  132                  /*
 105  133                   * to be compatible with the old defread()-based code
 106  134                   * which ignored case, store the parameters (except for the
 107  135                   * leading interface name) in upper case.
 108  136                   */
 109  137  
 110  138                  if ((param = strchr(entry, '.')) == NULL) {
 111  139                          pastv6 = param = entry;
 112  140                  } else {
 113  141                          pastv6 = ++param;
 114  142                          if (strncasecmp(param, "v6.", 3) == 0)
 115  143                                  pastv6 += 3;
 116  144                  }
 117  145  
 118  146                  for (defp = defaults;
 119  147                      (char *)defp < (char *)defaults + sizeof (defaults);
 120  148                      defp++) {
 121  149                          if (strcasecmp(pastv6, defp->df_name) == 0) {
 122  150                                  if (defp->df_max == -1) {
 123  151                                          dhcpmsg(MSG_WARNING, "parameter %s is "
 124  152                                              "obsolete; ignored", defp->df_name);
 125  153                                  }
 126  154                                  break;
 127  155                          }
 128  156                  }
 129  157  
 130  158                  for (; *param != '\0'; param++)
 131  159                          *param = toupper(*param);
 132  160  
 133  161                  if (nvlist_add_string(nvlist, &entry[i], value) != 0) {
 134  162                          dhcpmsg(MSG_WARNING, "cannot build default value cache;"
 135  163                              " using built-in defaults");
 136  164                          nvlist_free(nvlist);
 137  165                          nvlist = NULL;
 138  166                          break;
 139  167                  }
 140  168          }
 141  169  
 142  170          (void) fclose(fp);
 143  171          return (nvlist);
 144  172  }
 145  173  
 146  174  /*
 147  175   * df_get_string(): gets the string value of a given user-tunable parameter
 148  176   *
 149  177   *   input: const char *: the interface the parameter applies to
 150  178   *          boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
 151  179   *          uint_t: the parameter number to look up
  
    | 
      ↓ open down ↓ | 
    59 lines elided | 
    
      ↑ open up ↑ | 
  
 152  180   *  output: const char *: the parameter's value, or default if not set
 153  181   *                        (must be copied by caller to be kept)
 154  182   *    NOTE: df_get_string() is both used by functions outside this source
 155  183   *          file to retrieve strings from the defaults file, *and*
 156  184   *          internally by other df_get_*() functions.
 157  185   */
 158  186  
 159  187  const char *
 160  188  df_get_string(const char *if_name, boolean_t isv6, uint_t param)
 161  189  {
      190 +        const char              *agent_defaults_path = df_find_defaults();
 162  191          char                    *value;
 163  192          char                    paramstr[256];
 164  193          char                    name[256];
 165  194          struct stat             statbuf;
 166  195          static struct stat      df_statbuf;
 167  196          static boolean_t        df_unavail_msg = B_FALSE;
 168  197          static nvlist_t         *df_nvlist = NULL;
 169  198  
 170  199          if (param >= (sizeof (defaults) / sizeof (*defaults)))
 171  200                  return (NULL);
 172  201  
 173      -        if (stat(DHCP_AGENT_DEFAULTS, &statbuf) != 0) {
      202 +
      203 +        if (stat(agent_defaults_path, &statbuf) != 0) {
 174  204                  if (!df_unavail_msg) {
 175  205                          dhcpmsg(MSG_WARNING, "cannot access %s; using "
 176      -                            "built-in defaults", DHCP_AGENT_DEFAULTS);
      206 +                            "built-in defaults", agent_defaults_path);
 177  207                          df_unavail_msg = B_TRUE;
 178  208                  }
 179  209                  return (defaults[param].df_default);
 180  210          }
 181  211  
 182  212          /*
 183  213           * if our cached parameters are stale, rebuild.
 184  214           */
 185  215  
 186  216          if (statbuf.st_mtime != df_statbuf.st_mtime ||
 187  217              statbuf.st_size != df_statbuf.st_size) {
 188  218                  df_statbuf = statbuf;
 189  219                  nvlist_free(df_nvlist);
 190  220                  df_nvlist = df_build_cache();
 191  221          }
 192  222  
 193  223          if (isv6) {
 194  224                  (void) snprintf(name, sizeof (name), ".V6.%s",
 195  225                      defaults[param].df_name);
 196  226                  (void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name,
 197  227                      name);
 198  228          } else {
 199  229                  (void) strlcpy(name, defaults[param].df_name, sizeof (name));
 200  230                  (void) snprintf(paramstr, sizeof (paramstr), "%s.%s", if_name,
 201  231                      name);
 202  232          }
 203  233  
 204  234          /*
 205  235           * first look for `if_name.[v6.]param', then `[v6.]param'.  if neither
 206  236           * has been set, use the built-in default.
 207  237           */
 208  238  
 209  239          if (nvlist_lookup_string(df_nvlist, paramstr, &value) == 0 ||
 210  240              nvlist_lookup_string(df_nvlist, name, &value) == 0)
 211  241                  return (value);
 212  242  
 213  243          return (defaults[param].df_default);
 214  244  }
 215  245  
 216  246  /*
 217  247   * df_get_int(): gets the integer value of a given user-tunable parameter
 218  248   *
 219  249   *   input: const char *: the interface the parameter applies to
 220  250   *          boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
 221  251   *          uint_t: the parameter number to look up
 222  252   *  output: int: the parameter's value, or default if not set
 223  253   */
 224  254  
 225  255  int
 226  256  df_get_int(const char *if_name, boolean_t isv6, uint_t param)
 227  257  {
 228  258          const char      *value;
 229  259          int             value_int;
 230  260  
 231  261          if (param >= (sizeof (defaults) / sizeof (*defaults)))
 232  262                  return (0);
 233  263  
 234  264          value = df_get_string(if_name, isv6, param);
 235  265          if (value == NULL || !isdigit(*value))
 236  266                  goto failure;
 237  267  
 238  268          value_int = atoi(value);
 239  269          if (value_int > defaults[param].df_max ||
 240  270              value_int < defaults[param].df_min)
 241  271                  goto failure;
 242  272  
 243  273          return (value_int);
 244  274  
 245  275  failure:
 246  276          dhcpmsg(MSG_WARNING, "df_get_int: parameter `%s' is not between %d and "
 247  277              "%d, defaulting to `%s'", defaults[param].df_name,
 248  278              defaults[param].df_min, defaults[param].df_max,
 249  279              defaults[param].df_default);
 250  280          return (atoi(defaults[param].df_default));
 251  281  }
 252  282  
 253  283  /*
 254  284   * df_get_bool(): gets the boolean value of a given user-tunable parameter
 255  285   *
 256  286   *   input: const char *: the interface the parameter applies to
 257  287   *          boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
 258  288   *          uint_t: the parameter number to look up
 259  289   *  output: boolean_t: B_TRUE if true, B_FALSE if false, default if not set
 260  290   */
 261  291  
 262  292  boolean_t
 263  293  df_get_bool(const char *if_name, boolean_t isv6, uint_t param)
 264  294  {
 265  295          const char      *value;
 266  296  
 267  297          if (param >= (sizeof (defaults) / sizeof (*defaults)))
 268  298                  return (0);
 269  299  
 270  300          value = df_get_string(if_name, isv6, param);
 271  301          if (value != NULL) {
 272  302  
 273  303                  if (strcasecmp(value, "true") == 0 ||
 274  304                      strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0)
 275  305                          return (B_TRUE);
 276  306  
 277  307                  if (strcasecmp(value, "false") == 0 ||
 278  308                      strcasecmp(value, "no") == 0 || strcmp(value, "0") == 0)
 279  309                          return (B_FALSE);
 280  310          }
 281  311  
 282  312          dhcpmsg(MSG_WARNING, "df_get_bool: parameter `%s' has invalid value "
 283  313              "`%s', defaulting to `%s'", defaults[param].df_name,
 284  314              value != NULL ? value : "NULL", defaults[param].df_default);
 285  315  
 286  316          return ((atoi(defaults[param].df_default) == 0) ? B_FALSE : B_TRUE);
 287  317  }
  
    | 
      ↓ open down ↓ | 
    101 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX