File 12492.patch of Package squid

---------------------
PatchSet 12492 
Date: 2009/06/25 22:56:47
Author: hno
Branch: SQUID_2_7
Tag: (none) 
Log:
Author: serassio
MFC: Windows port: Fix improper access permissions to registry and DNS parsing from registry

- RegOpenKey() always try to open registry keys in full control mode, even if not needed.
  This could make Squid to fail when running as a non privileged user. RegOpenKeyEx() allow to
  specify only the needed priviledge and now is used instead.

- When parsing DNS setting into registry, a fixed size loop was used. Now the loop count is
  dynamic.

- Also ran indent.

Members: 
	src/dns_internal.c:1.63.2.8->1.63.2.9 
	src/win32.c:1.18.2.7->1.18.2.8 

Index: squid/src/dns_internal.c
===================================================================
RCS file: /cvsroot/squid/squid/src/dns_internal.c,v
retrieving revision 1.63.2.8
retrieving revision 1.63.2.9
diff -u -r1.63.2.8 -r1.63.2.9
--- squid/src/dns_internal.c	25 Jun 2009 22:48:37 -0000	1.63.2.8
+++ squid/src/dns_internal.c	25 Jun 2009 22:56:47 -0000	1.63.2.9
@@ -1,6 +1,6 @@
 
 /*
- * $Id: dns_internal.c,v 1.63.2.8 2009/06/25 22:48:37 hno Exp $
+ * $Id: dns_internal.c,v 1.63.2.9 2009/06/25 22:56:47 hno Exp $
  *
  * DEBUG: section 78    DNS lookups; interacts with lib/rfc1035.c
  * AUTHOR: Duane Wessels
@@ -50,6 +50,9 @@
 
 #ifdef _SQUID_WIN32_
 #include <windows.h>
+#define REG_TCPIP_PARA_INTERFACES "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces"
+#define REG_TCPIP_PARA "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"
+#define REG_VXD_MSTCP "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP"
 #endif
 #ifndef _PATH_RESCONF
 #define _PATH_RESCONF "/etc/resolv.conf"
@@ -318,32 +321,24 @@
     char *token;
     HKEY hndKey;
 
-    if (RegOpenKey(HKEY_LOCAL_MACHINE,
-	    "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
-	    &hndKey) == ERROR_SUCCESS) {
+    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_TCPIP_PARA, 0, KEY_QUERY_VALUE, &hndKey) == ERROR_SUCCESS) {
 	DWORD Type = 0;
 	DWORD Size = 0;
 	LONG Result;
-	Result =
-	    RegQueryValueEx(hndKey, "Domain", NULL, &Type, NULL,
-	    &Size);
+	Result = RegQueryValueEx(hndKey, "Domain", NULL, &Type, NULL, &Size);
 
 	if (Result == ERROR_SUCCESS && Size) {
 	    t = (char *) xmalloc(Size);
-	    RegQueryValueEx(hndKey, "Domain", NULL, &Type, (LPBYTE) t,
-		&Size);
+	    RegQueryValueEx(hndKey, "Domain", NULL, &Type, (LPBYTE) t, &Size);
 	    debug(78, 1) ("Adding domain %s from Registry\n", t);
 	    idnsAddPathComponent(t);
 	    xfree(t);
 	}
-	Result =
-	    RegQueryValueEx(hndKey, "SearchList", NULL, &Type, NULL,
-	    &Size);
+	Result = RegQueryValueEx(hndKey, "SearchList", NULL, &Type, NULL, &Size);
 
 	if (Result == ERROR_SUCCESS && Size) {
 	    t = (char *) xmalloc(Size);
-	    RegQueryValueEx(hndKey, "SearchList", NULL, &Type, (LPBYTE) t,
-		&Size);
+	    RegQueryValueEx(hndKey, "SearchList", NULL, &Type, (LPBYTE) t, &Size);
 	    token = strtok(t, Separator);
 	    idnsFreeSearchpath();
 
@@ -373,24 +368,18 @@
     switch (WIN32_OS_version) {
     case _WIN_OS_WINNT:
 	/* get nameservers from the Windows NT registry */
-	if (RegOpenKey(HKEY_LOCAL_MACHINE,
-		"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
-		&hndKey) == ERROR_SUCCESS) {
+	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_TCPIP_PARA, 0, KEY_QUERY_VALUE, &hndKey) == ERROR_SUCCESS) {
 	    DWORD Type = 0;
 	    DWORD Size = 0;
 	    LONG Result;
-	    Result =
-		RegQueryValueEx(hndKey, "DhcpNameServer", NULL, &Type, NULL,
-		&Size);
+	    Result = RegQueryValueEx(hndKey, "DhcpNameServer", NULL, &Type, NULL, &Size);
 	    if (Result == ERROR_SUCCESS && Size) {
 		t = (char *) xmalloc(Size);
-		RegQueryValueEx(hndKey, "DhcpNameServer", NULL, &Type, t,
-		    &Size);
+		RegQueryValueEx(hndKey, "DhcpNameServer", NULL, &Type, t, &Size);
 		token = strtok(t, ", ");
 		while (token) {
 		    idnsAddNameserver(token);
-		    debug(78, 1) ("Adding DHCP nameserver %s from Registry\n",
-			token);
+		    debug(78, 1) ("Adding DHCP nameserver %s from Registry\n", token);
 		    token = strtok(NULL, ", ");
 		}
 		xfree(t);
@@ -402,8 +391,7 @@
 		RegQueryValueEx(hndKey, "NameServer", NULL, &Type, t, &Size);
 		token = strtok(t, ", ");
 		while (token) {
-		    debug(78, 1) ("Adding nameserver %s from Registry\n",
-			token);
+		    debug(78, 1) ("Adding nameserver %s from Registry\n", token);
 		    idnsAddNameserver(token);
 		    token = strtok(NULL, ", ");
 		}
@@ -420,60 +408,58 @@
     case _WIN_OS_WIN7:
 	/* get nameservers from the Windows 2000 registry */
 	/* search all interfaces for DNS server addresses */
-	if (RegOpenKey(HKEY_LOCAL_MACHINE,
-		"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces",
-		&hndKey) == ERROR_SUCCESS) {
+	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_TCPIP_PARA_INTERFACES, 0, KEY_READ, &hndKey) == ERROR_SUCCESS) {
 	    int i;
-	    char keyname[255];
-
-	    for (i = 0; i < 10; i++) {
-		if (RegEnumKey(hndKey, i, (char *) &keyname,
-			255) == ERROR_SUCCESS) {
-		    char newkeyname[255];
-		    strcpy(newkeyname,
-			"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\");
-		    strcat(newkeyname, keyname);
-		    if (RegOpenKey(HKEY_LOCAL_MACHINE, newkeyname,
-			    &hndKey2) == ERROR_SUCCESS) {
-			DWORD Type = 0;
-			DWORD Size = 0;
-			LONG Result;
-			Result =
-			    RegQueryValueEx(hndKey2, "DhcpNameServer", NULL,
-			    &Type, NULL, &Size);
-			if (Result == ERROR_SUCCESS && Size) {
-			    t = (char *) xmalloc(Size);
-			    RegQueryValueEx(hndKey2, "DhcpNameServer", NULL,
-				&Type, t, &Size);
-			    token = strtok(t, ", ");
-			    while (token) {
-				debug(78, 1)
-				    ("Adding DHCP nameserver %s from Registry\n",
-				    token);
-				idnsAddNameserver(token);
-				token = strtok(NULL, ", ");
+	    int MaxSubkeyLen;
+	    DWORD InterfacesCount;
+	    char *keyname;
+	    FILETIME ftLastWriteTime;
+
+	    if (RegQueryInfoKey(hndKey, NULL, NULL, NULL, &InterfacesCount, &MaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
+		keyname = (char *) xmalloc(++MaxSubkeyLen);
+		for (i = 0; i < (int) InterfacesCount; i++) {
+		    int j;
+		    j = MaxSubkeyLen;
+		    if (RegEnumKeyEx(hndKey, i, keyname, &j, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS) {
+			char *newkeyname;
+			newkeyname = (char *) xmalloc(sizeof(REG_TCPIP_PARA_INTERFACES) + j + 2);
+			strcpy(newkeyname, REG_TCPIP_PARA_INTERFACES);
+			strcat(newkeyname, "\\");
+			strcat(newkeyname, keyname);
+			if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, newkeyname, 0, KEY_QUERY_VALUE, &hndKey2) == ERROR_SUCCESS) {
+			    DWORD Type = 0;
+			    DWORD Size = 0;
+			    LONG Result;
+			    Result = RegQueryValueEx(hndKey2, "DhcpNameServer", NULL, &Type, NULL, &Size);
+			    if (Result == ERROR_SUCCESS && Size) {
+				t = (char *) xmalloc(Size);
+				RegQueryValueEx(hndKey2, "DhcpNameServer", NULL, &Type, t, &Size);
+				token = strtok(t, ", ");
+				while (token) {
+				    debug(78, 1) ("Adding DHCP nameserver %s from Registry\n", token);
+				    idnsAddNameserver(token);
+				    token = strtok(NULL, ", ");
+				}
+				xfree(t);
 			    }
-			    xfree(t);
-			}
-			Result =
-			    RegQueryValueEx(hndKey2, "NameServer", NULL, &Type,
-			    NULL, &Size);
-			if (Result == ERROR_SUCCESS && Size) {
-			    t = (char *) xmalloc(Size);
-			    RegQueryValueEx(hndKey2, "NameServer", NULL, &Type,
-				t, &Size);
-			    token = strtok(t, ", ");
-			    while (token) {
-				debug(78, 1) ("Adding nameserver %s from Registry\n",
-				    token);
-				idnsAddNameserver(token);
-				token = strtok(NULL, ", ");
+			    Result = RegQueryValueEx(hndKey2, "NameServer", NULL, &Type, NULL, &Size);
+			    if (Result == ERROR_SUCCESS && Size) {
+				t = (char *) xmalloc(Size);
+				RegQueryValueEx(hndKey2, "NameServer", NULL, &Type, t, &Size);
+				token = strtok(t, ", ");
+				while (token) {
+				    debug(78, 1) ("Adding nameserver %s from Registry\n", token);
+				    idnsAddNameserver(token);
+				    token = strtok(NULL, ", ");
+				}
+				xfree(t);
 			    }
-			    xfree(t);
+			    RegCloseKey(hndKey2);
 			}
-			RegCloseKey(hndKey2);
+			xfree(newkeyname);
 		    }
 		}
+		xfree(keyname);
 	    }
 	    RegCloseKey(hndKey);
 	}
@@ -483,21 +469,17 @@
     case _WIN_OS_WIN98:
     case _WIN_OS_WINME:
 	/* get nameservers from the Windows 9X registry */
-	if (RegOpenKey(HKEY_LOCAL_MACHINE,
-		"SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP",
-		&hndKey) == ERROR_SUCCESS) {
+	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_VXD_MSTCP, 0, KEY_QUERY_VALUE, &hndKey) == ERROR_SUCCESS) {
 	    DWORD Type = 0;
 	    DWORD Size = 0;
 	    LONG Result;
-	    Result =
-		RegQueryValueEx(hndKey, "NameServer", NULL, &Type, NULL, &Size);
+	    Result = RegQueryValueEx(hndKey, "NameServer", NULL, &Type, NULL, &Size);
 	    if (Result == ERROR_SUCCESS && Size) {
 		t = (char *) xmalloc(Size);
 		RegQueryValueEx(hndKey, "NameServer", NULL, &Type, t, &Size);
 		token = strtok(t, ", ");
 		while (token) {
-		    debug(78, 1) ("Adding nameserver %s from Registry\n",
-			token);
+		    debug(78, 1) ("Adding nameserver %s from Registry\n", token);
 		    idnsAddNameserver(token);
 		    token = strtok(NULL, ", ");
 		}
Index: squid/src/win32.c
===================================================================
RCS file: /cvsroot/squid/squid/src/win32.c,v
retrieving revision 1.18.2.7
retrieving revision 1.18.2.8
diff -u -r1.18.2.7 -r1.18.2.8
--- squid/src/win32.c	25 Jun 2009 22:48:37 -0000	1.18.2.7
+++ squid/src/win32.c	25 Jun 2009 22:56:47 -0000	1.18.2.8
@@ -1,6 +1,6 @@
 
 /*
- * $Id: win32.c,v 1.18.2.7 2009/06/25 22:48:37 hno Exp $
+ * $Id: win32.c,v 1.18.2.8 2009/06/25 22:56:47 hno Exp $
  *
  * Windows support
  * AUTHOR: Guido Serassio <serassio@squid-cache.org>
@@ -471,9 +471,7 @@
 	if (signal(SIGABRT, WIN32_Abort) == SIG_ERR)
 	    return 1;
 	/* Register the service Handler function */
-	svcHandle =
-	    RegisterServiceCtrlHandler(WIN32_Service_name,
-	    WIN32_svcHandler);
+	svcHandle = RegisterServiceCtrlHandler(WIN32_Service_name, WIN32_svcHandler);
 	if (svcHandle == 0)
 	    return 1;
 	/* Set Process work dir to directory cointaining squid.exe */
@@ -484,26 +482,22 @@
 	    return 1;
 	safe_free(ConfigFile);
 	/* get config file from Windows Registry */
-	if (RegOpenKey(HKEY_LOCAL_MACHINE, REGKEY, &hndKey) == ERROR_SUCCESS) {
+	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY, 0, KEY_QUERY_VALUE, &hndKey) == ERROR_SUCCESS) {
 	    DWORD Type = 0;
 	    DWORD Size = 0;
 	    LONG Result;
-	    Result =
-		RegQueryValueEx(hndKey, CONFIGFILE, NULL, &Type, NULL, &Size);
+	    Result = RegQueryValueEx(hndKey, CONFIGFILE, NULL, &Type, NULL, &Size);
 	    if (Result == ERROR_SUCCESS && Size) {
 		ConfigFile = xmalloc(Size);
-		RegQueryValueEx(hndKey, CONFIGFILE, NULL, &Type, ConfigFile,
-		    &Size);
+		RegQueryValueEx(hndKey, CONFIGFILE, NULL, &Type, ConfigFile, &Size);
 	    } else
 		ConfigFile = xstrdup(DefaultConfigFile);
 	    Size = 0;
 	    Type = 0;
-	    Result =
-		RegQueryValueEx(hndKey, COMMANDLINE, NULL, &Type, NULL, &Size);
+	    Result = RegQueryValueEx(hndKey, COMMANDLINE, NULL, &Type, NULL, &Size);
 	    if (Result == ERROR_SUCCESS && Size) {
 		WIN32_Service_Command_Line = xmalloc(Size);
-		RegQueryValueEx(hndKey, COMMANDLINE, NULL, &Type, WIN32_Service_Command_Line,
-		    &Size);
+		RegQueryValueEx(hndKey, COMMANDLINE, NULL, &Type, WIN32_Service_Command_Line, &Size);
 	    } else
 		WIN32_Service_Command_Line = xstrdup("");
 	    RegCloseKey(hndKey);
@@ -517,8 +511,7 @@
 	/* Set Service Status to SERVICE_START_PENDING */
 	svcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
 	svcStatus.dwCurrentState = SERVICE_START_PENDING;
-	svcStatus.dwControlsAccepted =
-	    SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
+	svcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
 	svcStatus.dwWin32ExitCode = 0;
 	svcStatus.dwServiceSpecificExitCode = 0;
 	svcStatus.dwCheckPoint = 0;
openSUSE Build Service is sponsored by