*/
AP_DECLARE(const char *) ap_document_root(request_rec *r);
+/**
+ * Lookup the remote user agent's DNS name or IP address
+ * @ingroup get_remote_hostname
+ * @param req The current request
+ * @param type The type of lookup to perform. One of:
+ * <pre>
+ * REMOTE_HOST returns the hostname, or NULL if the hostname
+ * lookup fails. It will force a DNS lookup according to the
+ * HostnameLookups setting.
+ * REMOTE_NAME returns the hostname, or the dotted quad if the
+ * hostname lookup fails. It will force a DNS lookup according
+ * to the HostnameLookups setting.
+ * REMOTE_NOLOOKUP is like REMOTE_NAME except that a DNS lookup is
+ * never forced.
+ * REMOTE_DOUBLE_REV will always force a DNS lookup, and also force
+ * a double reverse lookup, regardless of the HostnameLookups
+ * setting. The result is the (double reverse checked)
+ * hostname, or NULL if any of the lookups fail.
+ * </pre>
+ * @param str_is_ip unless NULL is passed, this will be set to non-zero on
+ * output when an IP address string is returned
+ * @return The remote hostname (based on the request useragent_ip)
+ */
+AP_DECLARE(const char *) ap_get_useragent_host(request_rec *req, int type,
+ int *str_is_ip);
+
/**
* Lookup the remote client's DNS name or IP address
* @ingroup get_remote_host
* </pre>
* @param str_is_ip unless NULL is passed, this will be set to non-zero on output when an IP address
* string is returned
- * @return The remote hostname
+ * @return The remote hostname (based on the connection client_ip)
*/
AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config, int type, int *str_is_ip);
/* Code from Harald Hanche-Olsen <hanche@imf.unit.no> */
-static APR_INLINE void do_double_reverse (conn_rec *conn)
+static APR_INLINE void do_double_reverse (int *double_reverse,
+ const char *remote_host,
+ apr_sockaddr_t *client_addr,
+ apr_pool_t *pool)
{
apr_sockaddr_t *sa;
apr_status_t rv;
- if (conn->double_reverse) {
+ if (*double_reverse) {
/* already done */
return;
}
- if (conn->remote_host == NULL || conn->remote_host[0] == '\0') {
+ if (remote_host == NULL || remote_host[0] == '\0') {
/* single reverse failed, so don't bother */
- conn->double_reverse = -1;
+ *double_reverse = -1;
return;
}
- rv = apr_sockaddr_info_get(&sa, conn->remote_host, APR_UNSPEC, 0, 0, conn->pool);
+ rv = apr_sockaddr_info_get(&sa, remote_host, APR_UNSPEC, 0, 0, pool);
if (rv == APR_SUCCESS) {
while (sa) {
- if (apr_sockaddr_equal(sa, conn->client_addr)) {
- conn->double_reverse = 1;
+ if (apr_sockaddr_equal(sa, client_addr)) {
+ *double_reverse = 1;
return;
}
}
}
- conn->double_reverse = -1;
+ *double_reverse = -1;
}
AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config,
ap_str_tolower(conn->remote_host);
if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
- do_double_reverse(conn);
+ do_double_reverse(&conn->double_reverse, conn->remote_host,
+ conn->client_addr, conn->pool)
if (conn->double_reverse != 1) {
conn->remote_host = NULL;
}
}
if (type == REMOTE_DOUBLE_REV) {
- do_double_reverse(conn);
+ do_double_reverse(&conn->double_reverse, conn->remote_host,
+ conn->client_addr, conn->pool)
if (conn->double_reverse == -1) {
return NULL;
}
}
}
+AP_DECLARE(const char *) ap_get_useragent_host(request_rec *r,
+ int type, int *str_is_ip)
+{
+ conn_rec *conn = r->connection;
+ int hostname_lookups;
+ int ignored_str_is_ip;
+
+ if (req->useragent_addr == conn->client_addr) {
+ return ap_get_remote_host(conn, r->per_dir_config, type, str_is_ip);
+ }
+
+ if (!str_is_ip) { /* caller doesn't want to know */
+ str_is_ip = &ignored_str_is_ip;
+ }
+ *str_is_ip = 0;
+
+ hostname_lookups = ((core_dir_config *)
+ ap_get_core_module_config(r->per_dir_config))
+ ->hostname_lookups;
+ if (hostname_lookups == HOSTNAME_LOOKUP_UNSET) {
+ hostname_lookups = HOSTNAME_LOOKUP_OFF;
+ }
+
+ if (type != REMOTE_NOLOOKUP
+ && r->useragent_host == NULL
+ && (type == REMOTE_DOUBLE_REV
+ || hostname_lookups != HOSTNAME_LOOKUP_OFF)) {
+
+ if (apr_getnameinfo(&r->useragent_host, r->useragent_addr, 0)
+ == APR_SUCCESS) {
+ ap_str_tolower(r->useragent_host);
+
+ if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
+ do_double_reverse(&r->double_reverse, r->useragent_host,
+ r->useragent_addr, r->pool)
+ if (r->double_reverse != 1) {
+ r->useragent_host = NULL;
+ }
+ }
+ }
+
+ /* if failed, set it to the NULL string to indicate error */
+ if (r->useragent_host == NULL) {
+ r->useragent_host = "";
+ }
+ }
+
+ if (type == REMOTE_DOUBLE_REV) {
+ do_double_reverse(&r->double_reverse, r->useragent_host,
+ r->useragent_addr, r->pool)
+ if (r->double_reverse == -1) {
+ return NULL;
+ }
+ }
+
+ /*
+ * Return the desired information; either the remote DNS name, if found,
+ * or either NULL (if the hostname was requested) or the IP address
+ * (if any identifier was requested).
+ */
+ if (r->useragent_host != NULL && r->useragent_host[0] != '\0') {
+ return r->useragent_host;
+ }
+ else {
+ if (type == REMOTE_HOST || type == REMOTE_DOUBLE_REV) {
+ return NULL;
+ }
+ else {
+ *str_is_ip = 1;
+ return r->useragent_ip;
+ }
+ }
+}
+
/*
* Optional function coming from mod_ident, used for looking up ident user
*/