Attached is a patch that allows you to use the registry to store variables that are normally defined as environment variables.


A new function called netsnmp_getenv has been created in tools.c. For *nix it simply returns what getenv() would return.

For Windows it:

-returns a pointer to the environment variable if it is defined

-otherwise looks up the variable in HKCU\Software\Net-SNMP. If it is defined, it copies it to the environment variable and then returns a pointer to the environment variable

-otherwise looks up the variable in HKLM\Software\Net-SNMP. If it is defined, it copies it to the environment variable and then returns a pointer to the environment variable

-otherwise returns a NULL

The real environment variables are looked at first because when specifying MIBDIRS on the command line, it actually stores the passed values in the environment variable which is then used when reading the MIB files during initialization. This means if you have the env vars defined, the registry values will be ignored.

The following variables can be defined in the registry under either HKCU\Software\Net-SNMP or HKLM\Software\Net-SNMP:

-MIBDIRS
-MIBFILES
-MIBS
-PREFIX
-SNMP_PERSISTENT_FILE
-SNMPCONFPATH
-HOME

For example:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Net-SNMP]
"SNMPCONFPATH"="d:/Program Files/Net-SNMP/etc/snmp"
"SNMP_PERSISTENT_FILE"="c:/temp/persist/snmpd.conf"
"MIBDIRS"="$HOME;c:/usr/share/snmp/mibs"
"HOME"="c:/mymibs"

(HOME would not normally be used, but I used the above as a test and it worked. Use -Dfixup_mib_directory to see it. Use -Dread_config and -Dinit_mib to see the others).

By using this patch and modifying the NSIS installer script, the problem of having to reboot the machine after registering the snmpd and snmptrapd service will go away (tested).

I think this is the simplest way to implement registry access for Windows. It will work with any environment variable as long as netsnmp_getenv is used instead of getenv so if any are added in the future, we don't have to worry about porting it to the registry.

What does everyone think?

Alex

--- ../../net-snmp-main-10-7-04b.fresh/net-snmp/snmplib/tools.c Sat Jul  3 13:48:56 
2004
+++ snmplib/tools.c     Thu Oct  7 19:33:26 2004
@@ -906,3 +906,104 @@
 {
     return marker_tticks((marker_t) tv);
 }
+
+/**
+ * Non Windows:  Returns a pointer to the desired environment variable  
+ *               or NULL if the environment variable does not exist.  
+ *               
+ * Windows:      Returns a pointer to the desired environment variable  
+ *               if it exists.  If it does not, the variable is looked up
+ *               in the registry in HKCU\Net-SNMP or HKLM\Net-SNMP
+ *               (whichever it finds first) and stores the result in the 
+ *               environment variable.  It then returns a pointer to 
+ *               environment variable.
+ */
+
+char *netsnmp_getenv(const char *name)
+{
+  char *temp = NULL;  
+#ifndef WIN32
+  return (getenv(name));
+#else
+  HKEY hKey;
+  unsigned char key_value[512] = "";
+  DWORD key_value_size = sizeof(key_value);
+  DWORD key_value_type = 0;
+
+  DEBUGMSGTL(("read_config", "netsnmp_getenv called with name: %s\n",name));
+
+  if (!(name))
+    return NULL;
+  
+  /* Try environment variable first */ 
+  temp = getenv(name);
+  if (temp)
+    DEBUGMSGTL(("read_config", "netsnmp_getenv will return from ENV: %s\n",temp));
+  
+  /* Next try HKCU */
+  if (temp == NULL)
+  {
+    if (RegOpenKeyEx(
+          HKEY_CURRENT_USER, 
+          "SOFTWARE\\Net-SNMP", 
+          0, 
+          KEY_QUERY_VALUE, 
+          &hKey) == ERROR_SUCCESS) {   
+      
+      if (RegQueryValueEx(
+            hKey, 
+            name, 
+            NULL, 
+            &key_value_type, 
+            key_value, 
+            &key_value_size) == ERROR_SUCCESS) {
+        
+        /* Make sure last value is a NULL */
+        key_value[sizeof(key_value)-1] = 0;
+        
+        temp = key_value;
+      }
+      RegCloseKey(hKey);
+      if (temp)
+        DEBUGMSGTL(("read_config", "netsnmp_getenv will return from HKCU: 
%s\n",temp));
+    }
+  }
+
+  /* Next try HKLM */
+  if (temp == NULL)
+  {
+    if (RegOpenKeyEx(
+          HKEY_LOCAL_MACHINE, 
+          "SOFTWARE\\Net-SNMP", 
+          0, 
+          KEY_QUERY_VALUE, 
+          &hKey) == ERROR_SUCCESS) {   
+      
+      if (RegQueryValueEx(
+            hKey, 
+            name, 
+            NULL, 
+            &key_value_type, 
+            key_value, 
+            &key_value_size) == ERROR_SUCCESS) {
+
+        /* Make sure last value is a NULL */
+        key_value[sizeof(key_value)-1] = 0;
+        
+        temp = key_value;
+      }
+      RegCloseKey(hKey);
+      if (temp)
+        DEBUGMSGTL(("read_config", "netsnmp_getenv will return from HKLM: 
%s\n",temp));
+    }
+  }
+  
+  if (temp)
+    setenv(name, temp, 1);
+
+  DEBUGMSGTL(("read_config", "netsnmp_getenv returning: %s\n",getenv(name)));
+
+  return(getenv(name));
+#endif
+}
+
--- ../../net-snmp-main-10-7-04b.fresh/net-snmp/snmplib/read_config.c   Tue Oct  5 
19:47:00 2004
+++ snmplib/read_config.c       Thu Oct  7 19:22:06 2004
@@ -897,7 +897,7 @@
 
     if (NULL == netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
                                      NETSNMP_DS_LIB_CONFIGURATION_DIR)) {
-        homepath = getenv("HOME");
+        homepath = netsnmp_getenv("HOME");
         snprintf(defaultPath, sizeof(defaultPath), "%s%c%s%c%s%s%s%s",
                 SNMPCONFPATH, ENV_SEPARATOR_CHAR,
                 SNMPSHAREPATH, ENV_SEPARATOR_CHAR, SNMPLIBPATH,
@@ -1054,7 +1054,7 @@
         /*
          * read the config files 
          */
-        if ((envconfpath = getenv("SNMPCONFPATH")) == NULL) {
+        if ((envconfpath = netsnmp_getenv("SNMPCONFPATH")) == NULL) {
             snprintf(defaultPath, sizeof(defaultPath), "%s%s%s",
                     ((confpath == NULL) ? "" : confpath),
                     ((perspath == NULL) ? "" : ENV_SEPARATOR),
@@ -1082,7 +1082,7 @@
              * then we read all the configuration files we can, starting with
              * the oldest first.
              */
-           persfile = getenv("SNMP_PERSISTENT_FILE");
+           persfile = netsnmp_getenv("SNMP_PERSISTENT_FILE");
             if (strncmp(cptr2, perspath, strlen(perspath)) == 0 ||
                 (persfile != NULL &&
                  strncmp(cptr2, persfile, strlen(persfile)) == 0)) {
@@ -1195,7 +1195,7 @@
      * 1. ENV variable SNMP_PERSISTENT_FILE
      * 2. configured <PERSISTENT_DIRECTORY>/<type>.conf
      */
-    if ((filep = getenv("SNMP_PERSISTENT_FILE")) == NULL) {
+    if ((filep = netsnmp_getenv("SNMP_PERSISTENT_FILE")) == NULL) {
         snprintf(file, sizeof(file),
                  "%s/%s.conf", get_persistent_directory(), type);
         file[ sizeof(file)-1 ] = 0;
--- ../../net-snmp-main-10-7-04b.fresh/net-snmp/snmplib/mib.c   Tue Oct  5 19:47:00 
2004
+++ snmplib/mib.c       Thu Oct  7 19:20:38 2004
@@ -2450,7 +2450,7 @@
         DEBUGMSGTL(("get_mib_directory", "no mib directories set\n"));
 
         /** Check if the environment variable is set */
-        dir = getenv("MIBDIRS");
+        dir = netsnmp_getenv("MIBDIRS");
         if (dir == NULL) {
             DEBUGMSGTL(("get_mib_directory", "no mib directories set by 
environment\n"));
             /** Not set use hard coded path */
@@ -2489,7 +2489,7 @@
 void
 netsnmp_fixup_mib_directory(void)
 {
-    char *homepath = getenv("HOME");
+    char *homepath = netsnmp_getenv("HOME");
     char *mibpath = netsnmp_get_mib_directory();
     char *oldmibpath = NULL;
     char *ptr_home;
@@ -2562,7 +2562,7 @@
     }
     SNMP_FREE(env_var);
 
-    env_var = getenv("MIBFILES");
+    env_var = netsnmp_getenv("MIBFILES");
     if (env_var != NULL) {
         if (*env_var == '+')
             entry = strtok_r(env_var+1, ENV_SEPARATOR, &st);
@@ -2580,7 +2580,7 @@
      * Read in any modules or mibs requested 
      */
 
-    env_var = getenv("MIBS");
+    env_var = netsnmp_getenv("MIBS");
     if (env_var == NULL) {
         if (confmibs != NULL)
             env_var = strdup(confmibs);
@@ -2619,7 +2619,7 @@
     adopt_orphans();
     SNMP_FREE(env_var);
 
-    env_var = getenv("MIBFILES");
+    env_var = netsnmp_getenv("MIBFILES");
     if (env_var != NULL) {
         if (*env_var == '+') {
 #ifdef DEFAULT_MIBFILES
@@ -2657,7 +2657,7 @@
         SNMP_FREE(env_var);
     }
 
-    prefix = getenv("PREFIX");
+    prefix = netsnmp_getenv("PREFIX");
 
     if (!prefix)
         prefix = Standard_Prefix;
--- ../../net-snmp-main-10-7-04b.fresh/net-snmp/include/net-snmp/library/tools.h       
 Sat Jul  3 13:48:56 2004
+++ include/net-snmp/library/tools.h    Thu Oct  7 19:17:28 2004
@@ -197,7 +197,8 @@
 
     int             marker_tticks(marker_t pm);
     int             timeval_tticks(struct timeval *tv);
-
+    char            *netsnmp_getenv(const char *name);
+    
 #ifdef __cplusplus
 }
 #endif

Reply via email to