Пример #1
0
static void
idnsParseResolvConf(void)
{
    FILE *fp;
    char buf[512];
    char *t;
    fp = fopen(_PATH_RESOLV_CONF, "r");
    if (fp == NULL) {
	debug(78, 1) ("%s: %s\n", _PATH_RESOLV_CONF, xstrerror());
	return;
    }
    while (fgets(buf, 512, fp)) {
	t = strtok(buf, w_space);
	if (t == NULL)
	    continue;;
	if (strcasecmp(t, "nameserver"))
	    continue;
	t = strtok(NULL, w_space);
	if (t == NULL)
	    continue;;
	debug(78, 1) ("Adding nameserver %s from %s\n", t, _PATH_RESOLV_CONF);
	idnsAddNameserver(t);
    }
    fclose(fp);
}
Пример #2
0
void
idnsInit(void)
{
    static int init = 0;
    CBDATA_INIT_TYPE(idns_query);
    if (DnsSocket < 0) {
	int port;
	struct in_addr addr;
	if (Config.Addrs.udp_outgoing.s_addr != no_addr.s_addr)
	    addr = Config.Addrs.udp_outgoing;
	else
	    addr = Config.Addrs.udp_incoming;
	DnsSocket = comm_open(SOCK_DGRAM,
	    IPPROTO_UDP,
	    addr,
	    0,
	    COMM_NONBLOCKING,
	    "DNS Socket");
	if (DnsSocket < 0)
	    fatal("Could not create a DNS socket");
	/* Ouch... we can't call functions using debug from a debug
	 * statement. Doing so messes up the internal _db_level
	 */
	port = comm_local_port(DnsSocket);
	debug(78, 1) ("DNS Socket created at %s, port %d, FD %d\n",
	    inet_ntoa(addr),
	    port, DnsSocket);
    }
    assert(0 == nns);
    idnsParseNameservers();
#ifndef _SQUID_MSWIN_
    if (0 == nns)
	idnsParseResolvConf();
#endif
#ifdef _SQUID_WIN32_
    if (0 == nns)
	idnsParseWIN32Registry();
#endif
    if (0 == nns) {
	debug(78, 1) ("Warning: Could not find any nameservers. Trying to use localhost\n");
#ifdef _SQUID_WIN32_
	debug(78, 1) ("Please check your TCP-IP settings or /etc/resolv.conf file\n");
#else
	debug(78, 1) ("Please check your /etc/resolv.conf file\n");
#endif
	debug(78, 1) ("or use the 'dns_nameservers' option in squid.conf.\n");
	idnsAddNameserver("127.0.0.1");
    }
    if (!init) {
	memDataInit(MEM_IDNS_QUERY, "idns_query", sizeof(idns_query), 0);
	cachemgrRegister("idns",
	    "Internal DNS Statistics",
	    idnsStats, 0, 1);
	memset(RcodeMatrix, '\0', sizeof(RcodeMatrix));
	idns_lookup_hash = hash_create((HASHCMP *) strcmp, 103, hash_string);
	init++;
    }
}
Пример #3
0
static void
idnsParseNameservers(void)
{
    wordlist *w;
    for (w = Config.dns_nameservers; w; w = w->next) {
	debug(78, 1) ("Adding nameserver %s from squid.conf\n", w->key);
	idnsAddNameserver(w->key);
    }
}
Пример #4
0
static void
idnsParseWIN32Registry(void)
{
    char *t;
    char *token;
    HKEY hndKey, hndKey2;

    switch (WIN32_OS_version) {
    case _WIN_OS_WINNT:
	/* get nameservers from the Windows NT registry */
	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);
	    if (Result == ERROR_SUCCESS && Size) {
		t = (char *) xmalloc(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);
		    token = strtok(NULL, ", ");
		}
		xfree(t);
	    }
	    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);
		    idnsAddNameserver(token);
		    token = strtok(NULL, ", ");
		}
		xfree(t);
	    }
	    RegCloseKey(hndKey);
	}
	idnsParseWIN32SearchList(" ");
	break;
    case _WIN_OS_WIN2K:
    case _WIN_OS_WINXP:
    case _WIN_OS_WINNET:
    case _WIN_OS_WINLON:
    case _WIN_OS_WIN7:
	/* get nameservers from the Windows 2000 registry */
	/* search all interfaces for DNS server addresses */
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_TCPIP_PARA_INTERFACES, 0, KEY_READ, &hndKey) == ERROR_SUCCESS) {
	    int i;
	    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);
			    }
			    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);
			    }
			    RegCloseKey(hndKey2);
			}
			xfree(newkeyname);
		    }
		}
		xfree(keyname);
	    }
	    RegCloseKey(hndKey);
	}
	idnsParseWIN32SearchList(", ");
	break;
    case _WIN_OS_WIN95:
    case _WIN_OS_WIN98:
    case _WIN_OS_WINME:
	/* get nameservers from the Windows 9X registry */
	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);
	    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);
		    idnsAddNameserver(token);
		    token = strtok(NULL, ", ");
		}
		xfree(t);
	    }
	    RegCloseKey(hndKey);
	}
	break;
    default:
	debug(78, 1)
	    ("Failed to read nameserver from Registry: Unknown System Type.\n");
	return;
    }
}
Пример #5
0
static void
idnsParseResolvConf(void)
{
    FILE *fp;
    char buf[RESOLV_BUFSZ];
    const char *t;
    fp = fopen(_PATH_RESCONF, "r");
    if (fp == NULL) {
	debug(78, 1) ("%s: %s\n", _PATH_RESCONF, xstrerror());
	return;
    }
#if defined(_SQUID_CYGWIN_)
    setmode(fileno(fp), O_TEXT);
#endif
    while (fgets(buf, RESOLV_BUFSZ, fp)) {
	t = strtok(buf, w_space);
	if (NULL == t) {
	    continue;
	} else if (strcasecmp(t, "nameserver") == 0) {
	    t = strtok(NULL, w_space);
	    if (NULL == t)
		continue;
	    debug(78, 1) ("Adding nameserver %s from %s\n", t, _PATH_RESCONF);
	    idnsAddNameserver(t);
	} else if (strcasecmp(t, "domain") == 0) {
	    idnsFreeSearchpath();
	    t = strtok(NULL, w_space);
	    if (NULL == t)
		continue;
	    debug(78, 1) ("Adding domain %s from %s\n", t, _PATH_RESCONF);
	    idnsAddPathComponent(t);
	} else if (strcasecmp(t, "search") == 0) {
	    idnsFreeSearchpath();
	    while (NULL != t) {
		t = strtok(NULL, w_space);
		if (NULL == t)
		    continue;
		debug(78, 1) ("Adding domain %s from %s\n", t, _PATH_RESCONF);
		idnsAddPathComponent(t);
	    }
	} else if (strcasecmp(t, "options") == 0) {
	    while (NULL != t) {
		t = strtok(NULL, w_space);
		if (NULL == t)
		    continue;
		if (strncmp(t, "ndots:", 6) == 0) {
		    ndots = atoi(t + 6);
		    if (ndots < 1)
			ndots = 1;
		    if (ndots > RES_MAXNDOTS)
			ndots = RES_MAXNDOTS;
		    debug(78, 1) ("Adding ndots %d from %s\n", ndots, _PATH_RESCONF);
		}
	    }
	}
    }
    fclose(fp);

    if (npc == 0 && (t = getMyHostname())) {
	t = strchr(t, '.');
	if (t)
	    idnsAddPathComponent(t + 1);
    }
}
Пример #6
0
static void
idnsParseWIN32Registry(void)
{
	char *t;
	char *token;
	HKEY hndKey, hndKey2;

	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)
		{
			DWORD Type = 0;
			DWORD Size = 0;
			LONG Result;
			Result =
				RegQueryValueEx(hndKey, "DhcpNameServer", NULL, &Type, NULL,
								&Size);
			if (Result == ERROR_SUCCESS && Size)
			{
				t = (char *) xmalloc(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);
					token = strtok(NULL, ", ");
				}
				xfree(t);
			}
			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);
					idnsAddNameserver(token);
					token = strtok(NULL, ", ");
				}
				xfree(t);
			}
			RegCloseKey(hndKey);
		}
		idnsParseWIN32SearchList(" ");
		break;
	case _WIN_OS_WIN2K:
	case _WIN_OS_WINXP:
	case _WIN_OS_WINNET:
	case _WIN_OS_WINLON:
		/* 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)
		{
			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, ", ");
							}
							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, ", ");
							}
							xfree(t);
						}
						RegCloseKey(hndKey2);
					}
				}
			}
			RegCloseKey(hndKey);
		}
		idnsParseWIN32SearchList(", ");
		break;
	case _WIN_OS_WIN95:
	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)
		{
			DWORD Type = 0;
			DWORD Size = 0;
			LONG Result;
			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);
					idnsAddNameserver(token);
					token = strtok(NULL, ", ");
				}
				xfree(t);
			}
			RegCloseKey(hndKey);
		}
		break;
	default:
		debug(78, 1)
		("Failed to read nameserver from Registry: Unknown System Type.\n");
		return;
	}
}