예제 #1
0
static void
send_announce(const ipcache_addrs * ia, void *junk)
{
    LOCAL_ARRAY(char, tbuf, 256);
    LOCAL_ARRAY(char, sndbuf, BUFSIZ);
    struct sockaddr_in S;
    char *host = Config.Announce.host;
    char *file = NULL;
    u_short port = Config.Announce.port;
    int l;
    int n;
    int fd;
    int x;
    cbdataFree(junk);
    if (ia == NULL) {
	debug(27, 1) ("send_announce: Unknown host '%s'\n", host);
	return;
    }
    debug(27, 1) ("Sending Announcement to %s\n", host);
    sndbuf[0] = '\0';
    snprintf(tbuf, 256, "cache_version SQUID/%s\n", version_string);
    strcat(sndbuf, tbuf);
    assert(Config.Sockaddr.http);
    snprintf(tbuf, 256, "Running on %s %d %d\n",
	getMyHostname(),
	(int) ntohs(Config.Sockaddr.http->s.sin_port),
	(int) Config.Port.icp);
    strcat(sndbuf, tbuf);
    if (Config.adminEmail) {
	snprintf(tbuf, 256, "cache_admin: %s\n", Config.adminEmail);
	strcat(sndbuf, tbuf);
    }
    snprintf(tbuf, 256, "generated %d [%s]\n",
	(int) squid_curtime,
	mkhttpdlogtime(&squid_curtime));
    strcat(sndbuf, tbuf);
    l = strlen(sndbuf);
    if ((file = Config.Announce.file) != NULL) {
	fd = file_open(file, O_RDONLY);
	if (fd > -1 && (n = read(fd, sndbuf + l, BUFSIZ - l - 1)) > 0) {
	    fd_bytes(fd, n, FD_READ);
	    l += n;
	    sndbuf[l] = '\0';
	    file_close(fd);
	} else {
	    debug(50, 1) ("send_announce: %s: %s\n", file, xstrerror());
	}
    }
    memset(&S, '\0', sizeof(S));
    S.sin_family = AF_INET;
    S.sin_port = htons(port);
    S.sin_addr = ia->in_addrs[0];
    assert(theOutIcpConnection > 0);
    x = comm_udp_sendto(theOutIcpConnection,
	&S, sizeof(S),
	sndbuf, strlen(sndbuf) + 1);
    if (x < 0)
	debug(27, 1) ("send_announce: FD %d: %s\n", theOutIcpConnection,
	    xstrerror());
}
예제 #2
0
static void
idnsParseWIN32SearchList(const char *Separator)
{
	char *t;
	char *token;
	HKEY hndKey;

	if (RegOpenKey(HKEY_LOCAL_MACHINE,
				   "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
				   &hndKey) == ERROR_SUCCESS)
	{
		DWORD Type = 0;
		DWORD Size = 0;
		LONG Result;
		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);
			debug(78, 1) ("Adding domain %s from Registry\n", t);
			idnsAddPathComponent(t);
			xfree(t);
		}
		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);
			token = strtok(t, Separator);
			idnsFreeSearchpath();

			while (token)
			{
				idnsAddPathComponent(token);
				debug(78, 1) ("Adding domain %s from Registry\n", token);
				token = strtok(NULL, Separator);
			}
			xfree(t);
		}
		RegCloseKey(hndKey);
	}
	if (npc == 0 && ((const char *) t = getMyHostname()))
	{
		t = strchr(t, '.');
		if (t)
			idnsAddPathComponent(t + 1);
	}
}
static void
gopherHTMLFooter(StoreEntry * e)
{
    storeAppendPrintf(e, "<HR noshade size=\"1px\">\n");
    storeAppendPrintf(e, "<ADDRESS>\n");
    storeAppendPrintf(e, "Generated %s by %s (%s)\n",
	mkrfc1123(squid_curtime),
	getMyHostname(),
	visible_appname_string);
    storeAppendPrintf(e, "</ADDRESS></BODY></HTML>\n");
}
예제 #4
0
void
neighbors_init(void)
{
    struct sockaddr_in name;
    socklen_t len = sizeof(struct sockaddr_in);
    struct servent *sep = NULL;
    const char *me = getMyHostname();
    peer *this;
    peer *next;
    int fd = theInIcpConnection;
    if (fd >= 0) {
	memset(&name, '\0', sizeof(struct sockaddr_in));
	if (getsockname(fd, (struct sockaddr *) &name, &len) < 0)
	    debug(15, 1) ("getsockname(%d,%p,%p) failed.\n", fd, &name, &len);
	for (this = Config.peers; this; this = next) {
	    http_port_list *s;
	    next = this->next;
	    if (0 != strcmp(this->host, me))
		continue;
	    for (s = Config.Sockaddr.http; s; s = s->next) {
		if (this->http_port != ntohs(s->s.sin_port))
		    continue;
		debug(15, 1) ("WARNING: Peer looks like this host\n");
		debug(15, 1) ("         Ignoring %s %s/%d/%d\n",
		    neighborTypeStr(this), this->host, this->http_port,
		    this->icp.port);
		neighborRemove(this);
	    }
	}
    }
    peerRefreshDNS((void *) 1);
    if (0 == echo_hdr.opcode) {
	echo_hdr.opcode = ICP_SECHO;
	echo_hdr.version = ICP_VERSION_CURRENT;
	echo_hdr.length = 0;
	echo_hdr.reqnum = 0;
	echo_hdr.flags = 0;
	echo_hdr.pad = 0;
	echo_hdr.shostid = name.sin_addr.s_addr;
	sep = getservbyname("echo", "udp");
	echo_port = sep ? ntohs((u_short) sep->s_port) : 7;
    }
    first_ping = Config.peers;
    cachemgrRegister("server_list",
	"Peer Cache Statistics",
	neighborDumpPeers, 0, 1);
    if (theInIcpConnection >= 0) {
	cachemgrRegister("non_peers",
	    "List of Unknown sites sending ICP messages",
	    neighborDumpNonPeers, 0, 1);
    }
}
예제 #5
0
static void
idnsParseWIN32SearchList(const char *Separator)
{
    char *t;
    const char *token;
    HKEY hndKey;

    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);

	if (Result == ERROR_SUCCESS && Size) {
	    t = (char *) xmalloc(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);

	if (Result == ERROR_SUCCESS && Size) {
	    t = (char *) xmalloc(Size);
	    RegQueryValueEx(hndKey, "SearchList", NULL, &Type, (LPBYTE) t, &Size);
	    token = strtok(t, Separator);
	    idnsFreeSearchpath();

	    while (token) {
		idnsAddPathComponent(token);
		debug(78, 1) ("Adding domain %s from Registry\n", token);
		token = strtok(NULL, Separator);
	    }
	    xfree(t);
	}
	RegCloseKey(hndKey);
    }
    if (npc == 0 && (token = getMyHostname())) {
	token = strchr(token, '.');
	if (token)
	    idnsAddPathComponent(token + 1);
    }
}
예제 #6
0
static const char *
errorConvert(char token, ErrorState * err)
{
    request_t *r = err->request;
    static MemBuf mb = MemBufNULL;
    const char *p = NULL;	/* takes priority over mb if set */
    int do_quote = 1;

    memBufReset(&mb);
    switch (token) {
    case 'a':
	if (r && r->auth_user_request)
	    p = authenticateUserRequestUsername(r->auth_user_request);
	if (!p)
	    p = "-";
	break;
    case 'B':
	p = r ? ftpUrlWith2f(r) : "[no URL]";
	break;
    case 'c':
	p = errorPageName(err->type);
	break;
    case 'e':
	memBufPrintf(&mb, "%d", err->xerrno);
	break;
    case 'E':
	if (err->xerrno)
	    memBufPrintf(&mb, "(%d) %s", err->xerrno, strerror(err->xerrno));
	else
	    memBufPrintf(&mb, "[No Error]");
	break;
    case 'f':
	/* FTP REQUEST LINE */
	if (err->ftp.request)
	    p = err->ftp.request;
	else
	    p = "nothing";
	break;
    case 'F':
	/* FTP REPLY LINE */
	if (err->ftp.request)
	    p = err->ftp.reply;
	else
	    p = "nothing";
	break;
    case 'g':
	/* FTP SERVER MESSAGE */
	wordlistCat(err->ftp.server_msg, &mb);
	break;
    case 'h':
	memBufPrintf(&mb, "%s", getMyHostname());
	break;
    case 'H':
	if (r) {
	    if (r->hier.host)
		p = r->hier.host;
	    else
		p = r->host;
	} else
	    p = "[unknown host]";
	break;
    case 'i':
	memBufPrintf(&mb, "%s", inet_ntoa(err->src_addr));
	break;
    case 'I':
	if (r && r->hier.host) {
	    memBufPrintf(&mb, "%s", r->hier.host);
	} else
	    p = "[unknown]";
	break;
    case 'L':
	if (Config.errHtmlText) {
	    memBufPrintf(&mb, "%s", Config.errHtmlText);
	    do_quote = 0;
	} else
	    p = "[not available]";
	break;
    case 'm':
	p = authenticateAuthUserRequestMessage(err->auth_user_request) ? authenticateAuthUserRequestMessage(err->auth_user_request) : "[not available]";
	break;
    case 'M':
	p = r ? RequestMethods[r->method].str : "[unknown method]";
	break;
    case 'o':
	p = external_acl_message;
	if (!p)
	    p = "[not available]";
	break;
    case 'p':
	if (r) {
	    memBufPrintf(&mb, "%d", (int) r->port);
	} else {
	    p = "[unknown port]";
	}
	break;
    case 'P':
	p = r ? ProtocolStr[r->protocol] : "[unkown protocol]";
	break;
    case 'R':
	if (NULL != r) {
	    Packer p;
	    memBufPrintf(&mb, "%s %s HTTP/%d.%d\n",
		RequestMethods[r->method].str,
		strLen(r->urlpath) ? strBuf(r->urlpath) : "/",
		r->http_ver.major, r->http_ver.minor);
	    packerToMemInit(&p, &mb);
	    httpHeaderPackInto(&r->header, &p);
	    packerClean(&p);
	} else if (err->request_hdrs) {
	    p = err->request_hdrs;
	} else {
	    p = "[no request]";
	}
	break;
    case 's':
	p = visible_appname_string;
	break;
    case 'S':
	/* signature may contain %-escapes, recursion */
	if (err->page_id != ERR_SQUID_SIGNATURE) {
	    const int saved_id = err->page_id;
	    MemBuf sign_mb;
	    err->page_id = ERR_SQUID_SIGNATURE;
	    sign_mb = errorBuildContent(err);
	    memBufPrintf(&mb, "%s", sign_mb.buf);
	    memBufClean(&sign_mb);
	    err->page_id = saved_id;
	    do_quote = 0;
	} else {
	    /* wow, somebody put %S into ERR_SIGNATURE, stop recursion */
	    p = "[%S]";
	}
	break;
    case 't':
	memBufPrintf(&mb, "%s", mkhttpdlogtime(&squid_curtime));
	break;
    case 'T':
	memBufPrintf(&mb, "%s", mkrfc1123(squid_curtime));
	break;
    case 'U':
	p = r ? urlCanonicalClean(r) : err->url ? err->url : "[no URL]";
	break;
    case 'u':
	p = r ? urlCanonical(r) : err->url ? err->url : "[no URL]";
	break;
    case 'w':
	if (Config.adminEmail)
	    memBufPrintf(&mb, "%s", Config.adminEmail);
	else
	    p = "[unknown]";
	break;
    case 'z':
	if (err->dnsserver_msg)
	    p = err->dnsserver_msg;
	else
	    p = "[unknown]";
	break;
    case '%':
	p = "%";
	break;
    default:
	memBufPrintf(&mb, "%%%c", token);
	do_quote = 0;
	break;
    }
    if (!p)
	p = mb.buf;		/* do not use mb after this assignment! */
    assert(p);
    debug(4, 3) ("errorConvert: %%%c --> '%s'\n", token, p);
    if (do_quote)
	p = html_quote(p);
    return p;
}
예제 #7
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);
    }
}
예제 #8
0
static void
urnHandleReply(void *data, char *buf, ssize_t size)
{
    UrnState *urnState = data;
    StoreEntry *e = urnState->entry;
    StoreEntry *urlres_e = urnState->urlres_e;
    char *s = NULL;
    size_t k;
    HttpReply *rep;
    url_entry *urls;
    url_entry *u;
    url_entry *min_u;
    MemBuf mb;
    ErrorState *err;
    int i;
    int urlcnt = 0;
    http_version_t version;

    debug(52, 3) ("urnHandleReply: Called with size=%d.\n", (int) size);
    if (EBIT_TEST(urlres_e->flags, ENTRY_ABORTED)) {
	memFree(buf, MEM_4K_BUF);
	return;
    }
    if (size == 0) {
	memFree(buf, MEM_4K_BUF);
	return;
    } else if (size < 0) {
	memFree(buf, MEM_4K_BUF);
	return;
    }
    if (urlres_e->store_status == STORE_PENDING && size < SM_PAGE_SIZE) {
	storeClientCopy(urnState->sc, urlres_e,
	    size,
	    0,
	    SM_PAGE_SIZE,
	    buf,
	    urnHandleReply,
	    urnState);
	return;
    }
    /* we know its STORE_OK */
    k = headersEnd(buf, size);
    if (0 == k) {
	debug(52, 1) ("urnHandleReply: didn't find end-of-headers for %s\n",
	    storeUrl(e));
	return;
    }
    s = buf + k;
    assert(urlres_e->mem_obj->reply);
    httpReplyParse(urlres_e->mem_obj->reply, buf, k);
    debug(52, 3) ("mem->reply exists, code=%d.\n",
	urlres_e->mem_obj->reply->sline.status);
    if (urlres_e->mem_obj->reply->sline.status != HTTP_OK) {
	debug(52, 3) ("urnHandleReply: failed.\n");
	err = errorCon(ERR_URN_RESOLVE, HTTP_NOT_FOUND);
	err->request = requestLink(urnState->request);
	err->url = xstrdup(storeUrl(e));
	errorAppendEntry(e, err);
	return;
    }
    while (xisspace(*s))
	s++;
    urls = urnParseReply(s, urnState->request->method);
    for (i = 0; NULL != urls[i].url; i++)
	urlcnt++;
    debug(53, 3) ("urnFindMinRtt: Counted %d URLs\n", i);
    if (urls == NULL) {		/* unkown URN error */
	debug(52, 3) ("urnTranslateDone: unknown URN %s\n", storeUrl(e));
	err = errorCon(ERR_URN_RESOLVE, HTTP_NOT_FOUND);
	err->request = requestLink(urnState->request);
	err->url = xstrdup(storeUrl(e));
	errorAppendEntry(e, err);
	return;
    }
    min_u = urnFindMinRtt(urls, urnState->request->method, NULL);
    qsort(urls, urlcnt, sizeof(*urls), url_entry_sort);
    storeBuffer(e);
    memBufDefInit(&mb);
    memBufPrintf(&mb,
	"<TITLE>Select URL for %s</TITLE>\n"
	"<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n"
	"<H2>Select URL for %s</H2>\n"
	"<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", storeUrl(e), storeUrl(e));
    for (i = 0; i < urlcnt; i++) {
	u = &urls[i];
	debug(52, 3) ("URL {%s}\n", u->url);
	memBufPrintf(&mb,
	    "<TR><TD><A HREF=\"%s\">%s</A></TD>", u->url, u->url);
	if (urls[i].rtt > 0)
	    memBufPrintf(&mb,
		"<TD align=\"right\">%4d <it>ms</it></TD>", u->rtt);
	else
	    memBufPrintf(&mb, "<TD align=\"right\">Unknown</TD>");
	memBufPrintf(&mb,
	    "<TD>%s</TD></TR>\n", u->flags.cached ? "    [cached]" : " ");
    }
    memBufPrintf(&mb,
	"</TABLE>"
	"<HR noshade size=\"1px\">\n"
	"<ADDRESS>\n"
	"Generated by %s@%s\n"
	"</ADDRESS>\n",
	full_appname_string, getMyHostname());
    rep = e->mem_obj->reply;
    httpReplyReset(rep);
    httpBuildVersion(&version, 1, 0);
    httpReplySetHeaders(rep, version, HTTP_MOVED_TEMPORARILY, NULL,
	"text/html", mb.size, 0, squid_curtime);
    if (urnState->flags.force_menu) {
	debug(51, 3) ("urnHandleReply: forcing menu\n");
    } else if (min_u) {
	httpHeaderPutStr(&rep->header, HDR_LOCATION, min_u->url);
    }
    httpBodySet(&rep->body, &mb);
    httpReplySwapOut(rep, e);
    storeComplete(e);
    memFree(buf, MEM_4K_BUF);
    for (i = 0; i < urlcnt; i++) {
	safe_free(urls[i].url);
	safe_free(urls[i].host);
    }
    safe_free(urls);
    /* mb was absorbed in httpBodySet call, so we must not clean it */
    storeUnregister(urnState->sc, urlres_e, urnState);
    storeUnlockObject(urlres_e);
    storeUnlockObject(urnState->entry);
    requestUnlink(urnState->request);
    requestUnlink(urnState->urlres_r);
    cbdataFree(urnState);
}