14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #include <sys/types.h>
  27 #include <stdlib.h>
  28 #include <string.h>
  29 #include <ctype.h>
  30 #include <dhcpmsg.h>
  31 #include <stdio.h>
  32 #include <sys/stat.h>
  33 #include <libnvpair.h>
  34 
  35 #include "common.h"
  36 #include "defaults.h"
  37 
  38 struct dhcp_default {
  39 
  40         const char      *df_name;       /* parameter name */
  41         const char      *df_default;    /* default value */
  42         int             df_min;         /* min value if type DF_INTEGER */
  43         int             df_max;         /* max value if type DF_INTEGER */
  44 };
  45 
  46 /*
  47  * note: keep in the same order as tunable parameter constants in defaults.h
  48  */
  49 
  50 static struct dhcp_default defaults[] = {
  51 
  52         { "RELEASE_ON_SIGTERM",  "0",    0,   0   },
  53         { "IGNORE_FAILED_ARP",   "1",    0,   -1  },
  54         { "OFFER_WAIT",          "3",    1,   20  },
  55         { "ARP_WAIT",            "1000", 0,   -1  },
  56         { "CLIENT_ID",           NULL,   0,   0   },
  57         { "PARAM_REQUEST_LIST",  NULL,   0,   0   },
  58         { "REQUEST_HOSTNAME",    "1",    0,   0   },
  59         { "DEBUG_LEVEL",         "0",    0,   3   },
  60         { "VERBOSE",             "0",    0,   0   },
  61         { "VERIFIED_LEASE_ONLY", "0",    0,   0   },
  62         { "PARAM_IGNORE_LIST",   NULL,   0,   0   }
  63 };
  64 
  65 /*
  66  * df_build_cache(): builds the defaults nvlist cache
  67  *
  68  *   input: void
  69  *  output: a pointer to an nvlist of the current defaults, or NULL on failure
  70  */
  71 
  72 static nvlist_t *
  73 df_build_cache(void)
  74 {
  75         char            entry[1024];
  76         int             i;
  77         char            *param, *pastv6, *value, *end;
  78         FILE            *fp;
  79         nvlist_t        *nvlist;
  80         struct dhcp_default *defp;
  81 
  82         if ((fp = fopen(DHCP_AGENT_DEFAULTS, "r")) == NULL)
  83                 return (NULL);
  84 
  85         if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
  86                 dhcpmsg(MSG_WARNING, "cannot build default value cache; "
  87                     "using built-in defaults");
  88                 (void) fclose(fp);
  89                 return (NULL);
  90         }
  91 
  92         while (fgets(entry, sizeof (entry), fp) != NULL) {
  93                 for (i = 0; entry[i] == ' '; i++)
  94                         ;
  95 
  96                 end = strrchr(entry, '\n');
  97                 value = strchr(entry, '=');
  98                 if (end == NULL || value == NULL || entry[i] == '#')
  99                         continue;
 100 
 101                 *end = '\0';
 102                 *value++ = '\0';
 
 142         (void) fclose(fp);
 143         return (nvlist);
 144 }
 145 
 146 /*
 147  * df_get_string(): gets the string value of a given user-tunable parameter
 148  *
 149  *   input: const char *: the interface the parameter applies to
 150  *          boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
 151  *          uint_t: the parameter number to look up
 152  *  output: const char *: the parameter's value, or default if not set
 153  *                        (must be copied by caller to be kept)
 154  *    NOTE: df_get_string() is both used by functions outside this source
 155  *          file to retrieve strings from the defaults file, *and*
 156  *          internally by other df_get_*() functions.
 157  */
 158 
 159 const char *
 160 df_get_string(const char *if_name, boolean_t isv6, uint_t param)
 161 {
 162         char                    *value;
 163         char                    paramstr[256];
 164         char                    name[256];
 165         struct stat             statbuf;
 166         static struct stat      df_statbuf;
 167         static boolean_t        df_unavail_msg = B_FALSE;
 168         static nvlist_t         *df_nvlist = NULL;
 169 
 170         if (param >= (sizeof (defaults) / sizeof (*defaults)))
 171                 return (NULL);
 172 
 173         if (stat(DHCP_AGENT_DEFAULTS, &statbuf) != 0) {
 174                 if (!df_unavail_msg) {
 175                         dhcpmsg(MSG_WARNING, "cannot access %s; using "
 176                             "built-in defaults", DHCP_AGENT_DEFAULTS);
 177                         df_unavail_msg = B_TRUE;
 178                 }
 179                 return (defaults[param].df_default);
 180         }
 181 
 182         /*
 183          * if our cached parameters are stale, rebuild.
 184          */
 185 
 186         if (statbuf.st_mtime != df_statbuf.st_mtime ||
 187             statbuf.st_size != df_statbuf.st_size) {
 188                 df_statbuf = statbuf;
 189                 nvlist_free(df_nvlist);
 190                 df_nvlist = df_build_cache();
 191         }
 192 
 193         if (isv6) {
 194                 (void) snprintf(name, sizeof (name), ".V6.%s",
 195                     defaults[param].df_name);
 196                 (void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name,
 
 | 
 
 
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #include <sys/types.h>
  27 #include <stdlib.h>
  28 #include <string.h>
  29 #include <ctype.h>
  30 #include <dhcpmsg.h>
  31 #include <stdio.h>
  32 #include <sys/stat.h>
  33 #include <libnvpair.h>
  34 #include <zone.h>
  35 
  36 #include "common.h"
  37 #include "defaults.h"
  38 
  39 struct dhcp_default {
  40 
  41         const char      *df_name;       /* parameter name */
  42         const char      *df_default;    /* default value */
  43         int             df_min;         /* min value if type DF_INTEGER */
  44         int             df_max;         /* max value if type DF_INTEGER */
  45 };
  46 
  47 /*
  48  * note: keep in the same order as tunable parameter constants in defaults.h
  49  */
  50 
  51 static struct dhcp_default defaults[] = {
  52 
  53         { "RELEASE_ON_SIGTERM",  "0",    0,   0   },
  54         { "IGNORE_FAILED_ARP",   "1",    0,   -1  },
  55         { "OFFER_WAIT",          "3",    1,   20  },
  56         { "ARP_WAIT",            "1000", 0,   -1  },
  57         { "CLIENT_ID",           NULL,   0,   0   },
  58         { "PARAM_REQUEST_LIST",  NULL,   0,   0   },
  59         { "REQUEST_HOSTNAME",    "1",    0,   0   },
  60         { "DEBUG_LEVEL",         "0",    0,   3   },
  61         { "VERBOSE",             "0",    0,   0   },
  62         { "VERIFIED_LEASE_ONLY", "0",    0,   0   },
  63         { "PARAM_IGNORE_LIST",   NULL,   0,   0   }
  64 };
  65 
  66 
  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 /*
  93  * df_build_cache(): builds the defaults nvlist cache
  94  *
  95  *   input: void
  96  *  output: a pointer to an nvlist of the current defaults, or NULL on failure
  97  */
  98 
  99 static nvlist_t *
 100 df_build_cache(void)
 101 {
 102         const char      *agent_defaults_path = df_find_defaults();
 103         char            entry[1024];
 104         int             i;
 105         char            *param, *pastv6, *value, *end;
 106         FILE            *fp;
 107         nvlist_t        *nvlist;
 108         struct dhcp_default *defp;
 109 
 110         if ((fp = fopen(agent_defaults_path, "r")) == NULL)
 111                 return (NULL);
 112 
 113         if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
 114                 dhcpmsg(MSG_WARNING, "cannot build default value cache; "
 115                     "using built-in defaults");
 116                 (void) fclose(fp);
 117                 return (NULL);
 118         }
 119 
 120         while (fgets(entry, sizeof (entry), fp) != NULL) {
 121                 for (i = 0; entry[i] == ' '; i++)
 122                         ;
 123 
 124                 end = strrchr(entry, '\n');
 125                 value = strchr(entry, '=');
 126                 if (end == NULL || value == NULL || entry[i] == '#')
 127                         continue;
 128 
 129                 *end = '\0';
 130                 *value++ = '\0';
 
 170         (void) fclose(fp);
 171         return (nvlist);
 172 }
 173 
 174 /*
 175  * df_get_string(): gets the string value of a given user-tunable parameter
 176  *
 177  *   input: const char *: the interface the parameter applies to
 178  *          boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
 179  *          uint_t: the parameter number to look up
 180  *  output: const char *: the parameter's value, or default if not set
 181  *                        (must be copied by caller to be kept)
 182  *    NOTE: df_get_string() is both used by functions outside this source
 183  *          file to retrieve strings from the defaults file, *and*
 184  *          internally by other df_get_*() functions.
 185  */
 186 
 187 const char *
 188 df_get_string(const char *if_name, boolean_t isv6, uint_t param)
 189 {
 190         const char              *agent_defaults_path = df_find_defaults();
 191         char                    *value;
 192         char                    paramstr[256];
 193         char                    name[256];
 194         struct stat             statbuf;
 195         static struct stat      df_statbuf;
 196         static boolean_t        df_unavail_msg = B_FALSE;
 197         static nvlist_t         *df_nvlist = NULL;
 198 
 199         if (param >= (sizeof (defaults) / sizeof (*defaults)))
 200                 return (NULL);
 201 
 202 
 203         if (stat(agent_defaults_path, &statbuf) != 0) {
 204                 if (!df_unavail_msg) {
 205                         dhcpmsg(MSG_WARNING, "cannot access %s; using "
 206                             "built-in defaults", agent_defaults_path);
 207                         df_unavail_msg = B_TRUE;
 208                 }
 209                 return (defaults[param].df_default);
 210         }
 211 
 212         /*
 213          * if our cached parameters are stale, rebuild.
 214          */
 215 
 216         if (statbuf.st_mtime != df_statbuf.st_mtime ||
 217             statbuf.st_size != df_statbuf.st_size) {
 218                 df_statbuf = statbuf;
 219                 nvlist_free(df_nvlist);
 220                 df_nvlist = df_build_cache();
 221         }
 222 
 223         if (isv6) {
 224                 (void) snprintf(name, sizeof (name), ".V6.%s",
 225                     defaults[param].df_name);
 226                 (void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name,
 
 |