Problems with wshelper res_init.c

Douglas E. Engert deengert at anl.gov
Wed Jul 9 17:29:56 EDT 2003


A number of problems with the University of Michigan's KX509 when run
on W2K have been traced to problems in the MIT wshelper/res_init.c code.

The problems come form looking for the DNS servers in the registry, at least 
on W2K and XP. These problems include:


 o One machine had a long value for the for   
   HKLM\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Linkage\\Bind  It was 
   longer then BUFSIZ.  This caused the string to not be processed, and thus 
   no DNS servers where found.  

 o One machine never used DHCP, but has a null string for NameServer on
   the first interface. The null string was taken as the list of DNS servers,
   rather the looking at other interfaces. 
  
 o A user can specify a list of DNS servers to override the DHCP list, but the 
   code will take the DHCP defined name servers first. 

 o The code scans all the interfaces as listed in the Bind, even if they are
   not up, and finds the first set of DNS servers listed, even if these
   are not the ones wihc should be used. (I did not try and fix this. This is 
   an observation I have not seen this failure.)

 o If no domain was found the code would discard any DNS servers found and
   try and use the built in list. 

The attached patch attempts to circumvent some of these problems. Bind
can be as big as BUFSIZ*4, The NameServer key is checked before the DHCPNameServer,
A search will continue even if the NameServer or DHCPNameServer key is a null string.

The search is still not correct and may find a DHCP list before a NameServer list
if the interfaces are searched in a particular order, and there may be other situations 
where the BUFSIZ limit may cause problems. 

   
The following patch is against the KX509 version, but the changes also apply
to the MIT version. 


*** ,res_init.c	Thu Mar 28 15:45:19 2002
--- res_init.c	Tue Jul  8 09:14:17 2003
***************
*** 120,125 ****
--- 120,126 ----
  static int set_nameservers_using_iphlp();
  static FILE *find_config_file( LPSTR config_path );
  static int const getRegKey(const HKEY key, const char *subkey, const char *value, char
*buf);
+ static int const getRegKeyEx(const HKEY key, const char *subkey, const char *value, char
*buf, int size);
  
  int WINAPI wsh_getdomainname(char* name, int size);
  
***************
*** 431,436 ****
--- 432,439 ----
              "(or down if using DHCP).\n"
              "\tUsing built-in defaults.\n"
              );
+ 	}
+ 	if (!nserv) {
  
          // Let's use those hard coded defaults
          // Note: these must match the DEF file entries
***************
*** 1042,1048 ****
      char* buf
      )
  {
!     static char linkage[BUFSIZ];
      char* p;
      char* q;
      HKEY hAdapters;
--- 1045,1051 ----
      char* buf
      )
  {
!     static char linkage[BUFSIZ*4];
      char* p;
      char* q;
      HKEY hAdapters;
***************
*** 1053,1059 ****
  #define TCPIP_PATH_ADAPTERS
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces"
  #define TCPIP_PATH_LINKAGE "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Linkage"
  
!     if (!getRegKey(HKEY_LOCAL_MACHINE, TCPIP_PATH_LINKAGE, "Bind", linkage))
          return FALSE;
  
      p = linkage;
--- 1056,1062 ----
  #define TCPIP_PATH_ADAPTERS
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces"
  #define TCPIP_PATH_LINKAGE "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Linkage"
  
!     if (!getRegKeyEx(HKEY_LOCAL_MACHINE, TCPIP_PATH_LINKAGE, "Bind", linkage,
sizeof(linkage))) 
          return FALSE;
  
      p = linkage;
***************
*** 1069,1075 ****
          p = q;
          while (*p) p++;
          p++;
!         if (getRegKey(hAdapters, q, param, buf)) {
              if (!skip) {
                  RegCloseKey(hAdapters);
                  return TRUE;
--- 1072,1080 ----
          p = q;
          while (*p) p++;
          p++;
!         buf[0] = '\0';
! 		/* some systems have a null list, we need to look further */
!         if (getRegKey(hAdapters, q, param, buf) && buf[0]  ) {
              if (!skip) {
                  RegCloseKey(hAdapters);
                  return TRUE;
***************
*** 1511,1519 ****
          }
          break;
      case TCPIP_PARAMS_ALA_NT5:
!         ok = get_nt5_adapter_param(dhcp_param, 0, buf);
          if (!ok || !buf[0])
!             ok = get_nt5_adapter_param(param, 0, buf);
          break;
      }
  
--- 1516,1524 ----
          }
          break;
      case TCPIP_PARAMS_ALA_NT5:
!         ok = get_nt5_adapter_param(param, 0, buf);
          if (!ok || !buf[0])
!             ok = get_nt5_adapter_param(dhcp_param, 0, buf);
          break;
      }
  
***************
*** 1770,1775 ****
--- 1775,1793 ----
      char *buf
      )
  {
+ 	return (getRegKeyEx(key, subkey, value, buf, BUFSIZ));
+ }
+ 
+ static 
+ int const 
+ getRegKeyEx(
+     const HKEY key, 
+     const char *subkey, 
+     const char *value, 
+     char *buf,
+ 	int size
+     )
+ {
      HKEY hkTcpipParameters;
      LONG err;
      DWORD type, cb;
***************
*** 1778,1790 ****
  //  if (RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkTcpipParameters) ==
ERROR_SUCCESS) {
  
      if (RegOpenKey(key, subkey, &hkTcpipParameters) == ERROR_SUCCESS) {
!         cb = BUFSIZ;
          err = RegQueryValueEx(hkTcpipParameters, value, 0, &type, buf, &cb);
          RegCloseKey(hkTcpipParameters);
          if( err == ERROR_SUCCESS ){
              if( type == REG_EXPAND_SZ ){
                  if( env_buf = malloc( cb ) ){
!                     err = ExpandEnvironmentStrings( strcpy( env_buf, buf ), buf, BUFSIZ );
                      free( env_buf );
                      return err;
                  } else {
--- 1796,1808 ----
  //  if (RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkTcpipParameters) ==
ERROR_SUCCESS) {
  
      if (RegOpenKey(key, subkey, &hkTcpipParameters) == ERROR_SUCCESS) {
!         cb = size;
          err = RegQueryValueEx(hkTcpipParameters, value, 0, &type, buf, &cb);
          RegCloseKey(hkTcpipParameters);
          if( err == ERROR_SUCCESS ){
              if( type == REG_EXPAND_SZ ){
                  if( env_buf = malloc( cb ) ){
!                     err = ExpandEnvironmentStrings( strcpy( env_buf, buf ), buf, size );
                      free( env_buf );
                      return err;
                  } else {


More information about the krbdev mailing list