Beispiel #1
0
static const char *expr_var_fn(ap_expr_eval_ctx_t *ctx, const void *data)
{
    char *var = (char *)data;
    SSLConnRec *sslconn = ssl_get_effective_config(ctx->c);

    return sslconn ? ssl_var_lookup_ssl(ctx->p, sslconn, ctx->r, var) : NULL;
}
static KGL_RESULT WINAPI  GetServerVariable (
	kgl_filter_context * pfc,
	LPSTR                         name,
	LPVOID                        buffer,
	LPDWORD                       size
	)
{
	KHttpRequest *rq = (KHttpRequest *)pfc->ServerContext;
	if (strcasecmp(name, "SERVER_PROTOCOL")==0) {
/////////[242]
		return ADD_VAR(buffer,size,"HTTP/1.1");
	}
	if (strcasecmp(name, "SERVER_NAME") == 0) {
		return add_api_var(buffer, size, rq->url->host);
	}
	if (strcasecmp(name, "REQUEST_METHOD") == 0) {
		return add_api_var(buffer, size, rq->getMethod());
	}
	if (strcasecmp(name, "REQUEST_URI") == 0) {
		if (rq->raw_url.param == NULL) {
			return add_api_var(buffer, size, rq->raw_url.path);
		} 
		return var_printf(buffer,size,"%s?%s",rq->raw_url.path,rq->raw_url.param);
	}
	if (strcasecmp(name, "SCRIPT_NAME") == 0) {
		return add_api_var(buffer, size, rq->url->path);
	}
	if (strcasecmp(name, "QUERY_STRING") == 0) {
		if (rq->url->param==NULL || !*rq->url->param) {
			*size = 0;
			return KGL_ENO_DATA;
		}
		if (!TEST(rq->url->flags,KGL_URL_VARIED)) {
			return add_api_var(buffer, size, rq->url->param);
		}
		char *orig_param = rq->url->getVariedOrigParam();
		KGL_RESULT ret = KGL_ENO_DATA;
		if (*orig_param) {
			ret = add_api_var(buffer, size, orig_param);
		}
		free(orig_param);
		return ret;			
	}
	if (strcasecmp(name, "SERVER_ADDR") == 0) {
		sockaddr_i self_addr;
		rq->c->socket->get_self_addr(&self_addr);
		char ips[MAXIPLEN];
		KSocket::make_ip(&self_addr,ips,sizeof(ips));
		return add_api_var(buffer, size, ips);
	}
	if (strcasecmp(name, "SERVER_PORT") == 0) {
		return var_printf(buffer,size,"%d",rq->raw_url.port);
	}
	if (strcasecmp(name, "REMOTE_ADDR") == 0) {
		return add_api_var(buffer, size, rq->getClientIp());
	}	
	if (strcasecmp(name, "REMOTE_PORT") == 0) {
		return var_printf(buffer,size,"%d",rq->c->socket->get_remote_port());
	}
	if (strcasecmp(name, "PEER_ADDR") == 0) {
		char ips[MAXIPLEN];
		rq->c->socket->get_remote_ip(ips, MAXIPLEN);
		return add_api_var(buffer, size,ips);
	}
	if (strcasecmp(name, "DOCUMENT_ROOT") == 0) {
		if (!rq->svh) {
			return KGL_ENO_DATA;
		}
		return add_api_var(buffer, size, rq->svh->doc_root);
	}
	if (strcasecmp(name, "CONTENT_LENGTH") == 0) {
		return var_printf(buffer,size,INT64_FORMAT,rq->content_length);
	}
	if (strcasecmp(name, "CONTENT_TYPE") == 0) {
		return add_header_var(buffer,size,rq->parser.headers,"Content-Type");
	}
	if (strcasecmp(name, "HTTPS") == 0) {
		if (TEST(rq->workModel, WORK_MODEL_SSL)) {
			return ADD_VAR(buffer, size, "ON");
		} else {
			return ADD_VAR(buffer, size, "OFF");
		}
	}
	if (strncasecmp(name,"HTTP_",5) == 0) {
		return add_header_var(buffer,size,rq->parser.headers,name+5);
	}
#ifdef KSOCKET_SSL
	if (TEST(rq->workModel, WORK_MODEL_SSL)) {
		KSSLSocket *sslSocket = static_cast<KSSLSocket *>(rq->c->getSocket());
		char *result = ssl_var_lookup_ssl(sslSocket->getSSL(),name);
		if (result) {
			KGL_RESULT ret = add_api_var(buffer, size, result);
			OPENSSL_free(result);
			return ret;
		}
	}
#endif
	return KGL_EUNKNOW;
}
Beispiel #3
0
/* This function must remain safe to use for a non-SSL connection. */
char *ssl_var_lookup(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, char *var)
{
    SSLModConfigRec *mc = myModConfig(s);
    const char *result;
    BOOL resdup;
    apr_time_exp_t tm;

    result = NULL;
    resdup = TRUE;

    /*
     * When no pool is given try to find one
     */
    if (p == NULL) {
        if (r != NULL)
            p = r->pool;
        else if (c != NULL)
            p = c->pool;
        else
            p = mc->pPool;
    }

    /*
     * Request dependent stuff
     */
    if (r != NULL) {
        switch (var[0]) {
        case 'H':
        case 'h':
            if (strcEQ(var, "HTTP_USER_AGENT"))
                result = apr_table_get(r->headers_in, "User-Agent");
            else if (strcEQ(var, "HTTP_REFERER"))
                result = apr_table_get(r->headers_in, "Referer");
            else if (strcEQ(var, "HTTP_COOKIE"))
                result = apr_table_get(r->headers_in, "Cookie");
            else if (strcEQ(var, "HTTP_FORWARDED"))
                result = apr_table_get(r->headers_in, "Forwarded");
            else if (strcEQ(var, "HTTP_HOST"))
                result = apr_table_get(r->headers_in, "Host");
            else if (strcEQ(var, "HTTP_PROXY_CONNECTION"))
                result = apr_table_get(r->headers_in, "Proxy-Connection");
            else if (strcEQ(var, "HTTP_ACCEPT"))
                result = apr_table_get(r->headers_in, "Accept");
            else if (strlen(var) > 5 && strcEQn(var, "HTTP:", 5))
                /* all other headers from which we are still not know about */
                result = apr_table_get(r->headers_in, var+5);
            break;

        case 'R':
        case 'r':
            if (strcEQ(var, "REQUEST_METHOD"))
                result = r->method;
            else if (strcEQ(var, "REQUEST_SCHEME"))
                result = ap_http_scheme(r);
            else if (strcEQ(var, "REQUEST_URI"))
                result = r->uri;
            else if (strcEQ(var, "REQUEST_FILENAME"))
                result = r->filename;
            else if (strcEQ(var, "REMOTE_ADDR"))
                result = r->useragent_ip;
            else if (strcEQ(var, "REMOTE_HOST"))
                result = ap_get_useragent_host(r, REMOTE_NAME, NULL);
            else if (strcEQ(var, "REMOTE_IDENT"))
                result = ap_get_remote_logname(r);
            else if (strcEQ(var, "REMOTE_USER"))
                result = r->user;
            break;

        case 'S':
        case 's':
            if (strcEQn(var, "SSL", 3)) break; /* shortcut common case */

            if (strcEQ(var, "SERVER_ADMIN"))
                result = r->server->server_admin;
            else if (strcEQ(var, "SERVER_NAME"))
                result = ap_get_server_name_for_url(r);
            else if (strcEQ(var, "SERVER_PORT"))
                result = apr_psprintf(p, "%u", ap_get_server_port(r));
            else if (strcEQ(var, "SERVER_PROTOCOL"))
                result = r->protocol;
            else if (strcEQ(var, "SCRIPT_FILENAME"))
                result = r->filename;
            break;

        default:
            if (strcEQ(var, "PATH_INFO"))
                result = r->path_info;
            else if (strcEQ(var, "QUERY_STRING"))
                result = r->args;
            else if (strcEQ(var, "IS_SUBREQ"))
                result = (r->main != NULL ? "true" : "false");
            else if (strcEQ(var, "DOCUMENT_ROOT"))
                result = ap_document_root(r);
            else if (strcEQ(var, "AUTH_TYPE"))
                result = r->ap_auth_type;
            else if (strcEQ(var, "THE_REQUEST"))
                result = r->the_request;
            else if (strlen(var) > 4 && strcEQn(var, "ENV:", 4)) {
                result = apr_table_get(r->notes, var+4);
                if (result == NULL)
                    result = apr_table_get(r->subprocess_env, var+4);
            }
            break;
        }
    }

    /*
     * Connection stuff
     */
    if (result == NULL && c != NULL) {
        SSLConnRec *sslconn = ssl_get_effective_config(c);
        if (strlen(var) > 4 && strcEQn(var, "SSL_", 4)
            && sslconn && sslconn->ssl)
            result = ssl_var_lookup_ssl(p, sslconn, r, var+4);
        else if (strcEQ(var, "HTTPS")) {
            if (sslconn && sslconn->ssl)
                result = "on";
            else
                result = "off";
        }
    }

    /*
     * Totally independent stuff
     */
    if (result == NULL) {
        if (strlen(var) > 12 && strcEQn(var, "SSL_VERSION_", 12))
            result = ssl_var_lookup_ssl_version(p, var+12);
        else if (strcEQ(var, "SERVER_SOFTWARE"))
            result = ap_get_server_banner();
        else if (strcEQ(var, "API_VERSION")) {
            result = apr_itoa(p, MODULE_MAGIC_NUMBER_MAJOR);
            resdup = FALSE;
        }
        else if (strcEQ(var, "TIME_YEAR")) {
            apr_time_exp_lt(&tm, apr_time_now());
            result = apr_psprintf(p, "%02d%02d",
                                 (tm.tm_year / 100) + 19, tm.tm_year % 100);
            resdup = FALSE;
        }
#define MKTIMESTR(format, tmfield) \
            apr_time_exp_lt(&tm, apr_time_now()); \
            result = apr_psprintf(p, format, tm.tmfield); \
            resdup = FALSE;
        else if (strcEQ(var, "TIME_MON")) {
            MKTIMESTR("%02d", tm_mon+1)
        }
        else if (strcEQ(var, "TIME_DAY")) {
            MKTIMESTR("%02d", tm_mday)
        }
        else if (strcEQ(var, "TIME_HOUR")) {
            MKTIMESTR("%02d", tm_hour)
        }
        else if (strcEQ(var, "TIME_MIN")) {
            MKTIMESTR("%02d", tm_min)
        }
        else if (strcEQ(var, "TIME_SEC")) {
            MKTIMESTR("%02d", tm_sec)
        }
        else if (strcEQ(var, "TIME_WDAY")) {
            MKTIMESTR("%d", tm_wday)
        }
        else if (strcEQ(var, "TIME")) {
            apr_time_exp_lt(&tm, apr_time_now());
            result = apr_psprintf(p,
                        "%02d%02d%02d%02d%02d%02d%02d", (tm.tm_year / 100) + 19,
                        (tm.tm_year % 100), tm.tm_mon+1, tm.tm_mday,
                        tm.tm_hour, tm.tm_min, tm.tm_sec);
            resdup = FALSE;
        }
        /* all other env-variables from the parent Apache process */
        else if (strlen(var) > 4 && strcEQn(var, "ENV:", 4)) {
            result = getenv(var+4);
        }
    }

    if (result != NULL && resdup)
        result = apr_pstrdup(p, result);
    if (result == NULL)
        result = "";
    return (char *)result;
}