Exemplo n.º 1
0
static void
idnsAddNameserver(const char *buf)
{
    struct in_addr A;
    if (!safe_inet_addr(buf, &A)) {
	debug(78, 0) ("WARNING: rejecting '%s' as a name server, because it is not a numeric IP address\n", buf);
	return;
    }
    if (A.s_addr == 0) {
	debug(78, 0) ("WARNING: Squid does not accept 0.0.0.0 in DNS server specifications.\n");
	debug(78, 0) ("Will be using 127.0.0.1 instead, assuming you meant that DNS is running on the same machine\n");
	safe_inet_addr("127.0.0.1", &A);
    }
    if (nns == nns_alloc) {
	int oldalloc = nns_alloc;
	ns *oldptr = nameservers;
	if (nns_alloc == 0)
	    nns_alloc = 2;
	else
	    nns_alloc <<= 1;
	nameservers = xcalloc(nns_alloc, sizeof(*nameservers));
	if (oldptr && oldalloc)
	    xmemcpy(nameservers, oldptr, oldalloc * sizeof(*nameservers));
	if (oldptr)
	    safe_free(oldptr);
    }
    assert(nns < nns_alloc);
    nameservers[nns].S.sin_family = AF_INET;
    nameservers[nns].S.sin_port = htons(NS_DEFAULTPORT);
    nameservers[nns].S.sin_addr.s_addr = A.s_addr;
    debug(78, 3) ("idnsAddNameserver: Added nameserver #%d: %s\n",
	nns, inet_ntoa(nameservers[nns].S.sin_addr));
    nns++;
}
Exemplo n.º 2
0
int
main(int argc, char *argv[])
{
    char *s;
    cachemgr_request *req;
    safe_inet_addr("255.255.255.255", &no_addr);
    now = time(NULL);
#ifdef _SQUID_MSWIN_
    Win32SockInit();
    atexit(Win32SockCleanup);
    _setmode(_fileno(stdin), _O_BINARY);
    _setmode(_fileno(stdout), _O_BINARY);
    _fmode = _O_BINARY;
    if ((s = strrchr(argv[0], '\\')))
#else
    if ((s = strrchr(argv[0], '/')))
#endif
	progname = xstrdup(s + 1);
    else
	progname = xstrdup(argv[0]);
    if ((s = getenv("SCRIPT_NAME")) != NULL)
	script_name = xstrdup(s);
    req = read_request();
    return process_request(req);
}
Exemplo n.º 3
0
static void
lookup(const char *buf)
{
    const struct hostent *result = NULL;
    int reverse = 0;
    int ttl = 0;
    int retry = 0;
    int i;
    struct in_addr addr;
    if (0 == strcmp(buf, "$shutdown"))
	exit(0);
    if (0 == strcmp(buf, "$hello")) {
	printf("$alive\n");
	return;
    }
    /* check if it's already an IP address in text form. */
    for (;;) {
	if (safe_inet_addr(buf, &addr)) {
	    reverse = 1;
	    result = gethostbyaddr((char *) &addr.s_addr, 4, AF_INET);
	} else {
	    result = gethostbyname(buf);
	}
	if (NULL != result)
	    break;
	if (h_errno != TRY_AGAIN)
	    break;
	if (++retry == 3)
	    break;
	sleep(1);
    }
    if (NULL == result) {
	if (h_errno == TRY_AGAIN) {
	    printf("$fail Name Server for domain '%s' is unavailable.\n", buf);
	} else {
	    printf("$fail DNS Domain '%s' is invalid: %s.\n",
		buf, my_h_msgs(h_errno));
	}
	return;
    }
#if LIBRESOLV_DNS_TTL_HACK
    /* DNS TTL handling - [email protected]
     * for first try it's a dirty hack, by hacking getanswer
     * to place the ttl in a global variable */
    if (_dns_ttl_ > -1)
	ttl = _dns_ttl_;
#endif
    if (reverse) {
	printf("$name %d %s\n", ttl, result->h_name);
	return;
    }
    printf("$addr %d", ttl);
    for (i = 0; NULL != result->h_addr_list[i]; i++) {
	if (32 == i)
	    break;
	xmemcpy(&addr, result->h_addr_list[i], sizeof(addr));
	printf(" %s", inet_ntoa(addr));
    }
    printf("\n");
}
Exemplo n.º 4
0
void
netdbBinaryExchange(StoreEntry * s)
{
    http_reply *reply = s->mem_obj->reply;
#if USE_ICMP
    netdbEntry *n;
    int i;
    int j;
    int rec_sz;
    char *buf;
    struct in_addr addr;
    storeBuffer(s);
    httpReplyReset(reply);
    httpReplySetHeaders(reply, HTTP_OK, "OK", NULL, -1, squid_curtime, -1);
    httpReplySwapOut(reply, s);
    rec_sz = 0;
    rec_sz += 1 + sizeof(addr.s_addr);
    rec_sz += 1 + sizeof(int);
    rec_sz += 1 + sizeof(int);
    buf = memAllocate(MEM_4K_BUF);
    i = 0;
    hash_first(addr_table);
    while ((n = (netdbEntry *) hash_next(addr_table))) {
        if (0.0 == n->rtt)
            continue;
        if (n->rtt > 60000)	/* RTT > 1 MIN probably bogus */
            continue;
        if (!safe_inet_addr(n->network, &addr))
            continue;
        buf[i++] = (char) NETDB_EX_NETWORK;
        xmemcpy(&buf[i], &addr.s_addr, sizeof(addr.s_addr));
        i += sizeof(addr.s_addr);
        buf[i++] = (char) NETDB_EX_RTT;
        j = htonl((int) (n->rtt * 1000));
        xmemcpy(&buf[i], &j, sizeof(int));
        i += sizeof(int);
        buf[i++] = (char) NETDB_EX_HOPS;
        j = htonl((int) (n->hops * 1000));
        xmemcpy(&buf[i], &j, sizeof(int));
        i += sizeof(int);
        if (i + rec_sz > 4096) {
            storeAppend(s, buf, i);
            i = 0;
        }
    }
    if (i > 0) {
        storeAppend(s, buf, i);
        i = 0;
    }
    assert(0 == i);
    storeBufferFlush(s);
    memFree(buf, MEM_4K_BUF);
#else
    httpReplyReset(reply);
    httpReplySetHeaders(reply, HTTP_BAD_REQUEST, "Bad Request", NULL, -1, -1, -1);
    httpReplySwapOut(reply, s);
    storeAppendPrintf(s, "NETDB support not compiled into this Squid cache.\n");
#endif
    storeComplete(s);
}
Exemplo n.º 5
0
static void
idnsAddNameserver(const char *buf)
{
    struct in_addr A;
    if (!safe_inet_addr(buf, &A)) {
	debug(78, 0) ("WARNING: rejecting '%s' as a name server, because it is not a numeric IP address\n", buf);
	return;
    }
    if (nns == nns_alloc) {
	int oldalloc = nns_alloc;
	ns *oldptr = nameservers;
	if (nns_alloc == 0)
	    nns_alloc = 2;
	else
	    nns_alloc <<= 1;
	nameservers = xcalloc(nns_alloc, sizeof(*nameservers));
	if (oldptr && oldalloc)
	    xmemcpy(nameservers, oldptr, oldalloc * sizeof(*nameservers));
	if (oldptr)
	    safe_free(oldptr);
    }
    assert(nns < nns_alloc);
    nameservers[nns].S.sin_family = AF_INET;
    nameservers[nns].S.sin_port = htons(DOMAIN_PORT);
    nameservers[nns].S.sin_addr.s_addr = A.s_addr;
    debug(78, 3) ("idnsAddNameserver: Added nameserver #%d: %s\n",
	nns, inet_ntoa(nameservers[nns].S.sin_addr));
    nns++;
}
Exemplo n.º 6
0
static int
process_request(cachemgr_request * req)
{
    const struct hostent *hp;
    static struct sockaddr_in S;
    int s;
    int l;
    static char buf[2 * 1024];
    if (req == NULL) {
	auth_html(CACHEMGR_HOSTNAME, CACHE_HTTP_PORT, "");
	return 1;
    }
    if (req->hostname == NULL) {
	req->hostname = xstrdup(CACHEMGR_HOSTNAME);
    }
    if (req->port == 0) {
	req->port = CACHE_HTTP_PORT;
    }
    if (req->action == NULL) {
	req->action = xstrdup("");
    }
    if (!strcmp(req->action, "authenticate")) {
	auth_html(req->hostname, req->port, req->user_name);
	return 0;
    }
    if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
	snprintf(buf, 1024, "socket: %s\n", xstrerror());
	error_html(buf);
	return 1;
    }
    memset(&S, '\0', sizeof(struct sockaddr_in));
    S.sin_family = AF_INET;
    if ((hp = gethostbyname(req->hostname)) != NULL) {
	assert(hp->h_length <= sizeof(S.sin_addr.s_addr));
	xmemcpy(&S.sin_addr.s_addr, hp->h_addr, hp->h_length);
    } else if (safe_inet_addr(req->hostname, &S.sin_addr))
	(void) 0;
    else {
	snprintf(buf, 1024, "Unknown host: %s\n", req->hostname);
	error_html(buf);
	return 1;
    }
    S.sin_port = htons(req->port);
    if (connect(s, (struct sockaddr *) &S, sizeof(struct sockaddr_in)) < 0) {
	snprintf(buf, 1024, "connect: %s\n", xstrerror());
	error_html(buf);
	return 1;
    }
    l = snprintf(buf, sizeof(buf),
	"GET cache_object://%s/%s HTTP/1.0\r\n"
	"Accept: */*\r\n"
	"%s"			/* Authentication info or nothing */
	"\r\n",
	req->hostname,
	req->action,
	make_auth_header(req));
    write(s, buf, l);
    debug(1) fprintf(stderr, "wrote request: '%s'\n", buf);
    return read_reply(s, req);
}
Exemplo n.º 7
0
int
main(int argc, char *argv[])
{
    char *s;
    cachemgr_request *req;
    safe_inet_addr("255.255.255.255", &no_addr);
    now = time(NULL);
    if ((s = strrchr(argv[0], '/')))
	progname = xstrdup(s + 1);
    else
	progname = xstrdup(argv[0]);
    if ((s = getenv("SCRIPT_NAME")) != NULL)
	script_name = xstrdup(s);
    req = read_request();
    return process_request(req);
}
Exemplo n.º 8
0
int
main(int argc, char *argv[])
{
    char request[512];
    char *t = NULL;
    int c;
    int opt_s = 0;
    extern char *optarg;

    safe_inet_addr("255.255.255.255", &no_addr);

#if HAVE_RES_INIT
    res_init();
#ifdef RES_DEFAULT
    _res.options = RES_DEFAULT;
#endif
#ifdef RES_DEFNAMES
    _res.options &= ~RES_DEFNAMES;
#endif
#ifdef RES_DNSRCH
    _res.options &= ~RES_DNSRCH;
#endif
#endif

    while ((c = getopt(argc, argv, "Dhs:v")) != -1) {
	switch (c) {
	case 'D':
#ifdef RES_DEFNAMES
	    _res.options |= RES_DEFNAMES;
#endif
#ifdef RES_DNSRCH
	    _res.options |= RES_DNSRCH;
#endif
	    break;
	case 's':
#if HAVE_RES_INIT
	    if (opt_s == 0) {
		_res.nscount = 0;
		/*
		 * Setting RES_INIT here causes coredumps when -s is
		 * used with -D option.  It looks to me like setting
		 * RES_INIT is wrong.  The resolver code sets RES_INIT
		 * after calling res_init().  When we change the _res
		 * structure and set RES_INIT, some internal resolver
		 * structures get confused.             -DW 2.1.p1
		 */
#if SEEMS_WRONG
		_res.options |= RES_INIT;
#endif
		opt_s = 1;
	    } else if (_res.nscount == MAXNS) {
		fprintf(stderr, "Too many -s options, only %d are allowed\n",
		    MAXNS);
		break;
	    }
#if HAVE_RES_NSADDR_LIST
	    _res.nsaddr_list[_res.nscount] = _res.nsaddr_list[0];
	    safe_inet_addr(optarg, &_res.nsaddr_list[_res.nscount++].sin_addr);
#elif HAVE_RES_NS_LIST
	    _res.ns_list[_res.nscount] = _res.ns_list[0];
	    safe_inet_addr(optarg, &_res.ns_list[_res.nscount++].addr.sin_addr);
#else /* Unknown NS list format */
	    fprintf(stderr, "-s is not supported on this resolver\n");
#endif
#else /* !HAVE_RES_INIT */
	    fprintf(stderr, "-s is not supported on this resolver\n");
#endif /* HAVE_RES_INIT */
	    break;
	case 'v':
	    printf("dnsserver version %s\n", SQUID_VERSION);
	    exit(0);
	    break;
	case 'h':
	default:
	    usage();
	    exit(1);
	    break;
	}
    }

    for (;;) {
	memset(request, '\0', REQ_SZ);
	if (fgets(request, REQ_SZ, stdin) == NULL)
	    exit(1);
	t = strrchr(request, '\n');
	if (t == NULL)		/* Ignore if no newline */
	    continue;
	*t = '\0';		/* strip NL */
	if ((t = strrchr(request, '\r')) != NULL)
	    *t = '\0';		/* strip CR */
	lookup(request);
	fflush(stdout);
    }
    /* NOTREACHED */
    return 0;
}
Exemplo n.º 9
0
static void
netdbReloadState(void)
{
    char *buf;
    char *t;
    char *s;
    int fd;
    int l;
    struct stat sb;
    netdbEntry *n;
    netdbEntry N;
    struct in_addr addr;
    int count = 0;
    struct timeval start = current_time;

    if (strcmp(Config.netdbFilename, "none") == 0)
        return;

    /*
     * This was nicer when we were using stdio, but thanks to
     * Solaris bugs, its a bad idea.  fopen can fail if more than
     * 256 FDs are open.
     */
    fd = file_open(Config.netdbFilename, O_RDONLY | O_BINARY);
    if (fd < 0)
        return;
    if (fstat(fd, &sb) < 0) {
        file_close(fd);
        return;
    }
    t = buf = xcalloc(1, (size_t) sb.st_size + 1);
    l = FD_READ_METHOD(fd, buf, (int) sb.st_size);
    file_close(fd);
    if (l <= 0)
        return;
    while ((s = strchr(t, '\n'))) {
        char *q;
        assert(s - buf < l);
        *s = '\0';
        memset(&N, '\0', sizeof(netdbEntry));
        q = strtok(t, w_space);
        t = s + 1;
        if (NULL == q)
            continue;
        if (!safe_inet_addr(q, &addr))
            continue;
        if (netdbLookupAddr(addr) != NULL)	/* no dups! */
            continue;
        if ((q = strtok(NULL, w_space)) == NULL)
            continue;
        N.pings_sent = atoi(q);
        if ((q = strtok(NULL, w_space)) == NULL)
            continue;
        N.pings_recv = atoi(q);
        if (N.pings_recv == 0)
            continue;
        /* give this measurement low weight */
        N.pings_sent = 1;
        N.pings_recv = 1;
        if ((q = strtok(NULL, w_space)) == NULL)
            continue;
        N.hops = atof(q);
        if ((q = strtok(NULL, w_space)) == NULL)
            continue;
        N.rtt = atof(q);
        if ((q = strtok(NULL, w_space)) == NULL)
            continue;
        N.next_ping_time = (time_t) atoi(q);
        if ((q = strtok(NULL, w_space)) == NULL)
            continue;
        N.last_use_time = (time_t) atoi(q);
        n = memAllocate(MEM_NETDBENTRY);
        xmemcpy(n, &N, sizeof(netdbEntry));
        netdbHashInsert(n, addr);
        while ((q = strtok(NULL, w_space)) != NULL) {
            if (netdbLookupHost(q) != NULL)	/* no dups! */
                continue;
            netdbHostInsert(n, q);
        }
        count++;
    }
    xfree(buf);
    getCurrentTime();
    debug(38, 1) ("NETDB state reloaded; %d entries, %d msec\n",
                  count, tvSubMsec(start, current_time));
}