logresolve.c revision 76afc31650e2621ed0882c45bbb900e550f9c96a
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/* Licensed to the Apache Software Foundation (ASF) under one or more
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * contributor license agreements. See the NOTICE file distributed with
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * this work for additional information regarding copyright ownership.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * The ASF licenses this file to You under the Apache License, Version 2.0
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * (the "License"); you may not use this file except in compliance with
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * the License. You may obtain a copy of the License at
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * http://www.apache.org/licenses/LICENSE-2.0
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Unless required by applicable law or agreed to in writing, software
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * distributed under the License is distributed on an "AS IS" BASIS,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * See the License for the specific language governing permissions and
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * limitations under the License.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder */
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/*
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * logresolve 2.0
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Tom Rathborne - tomr uunet.ca - http://www.uunet.ca/~tomr/
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * UUNET Canada, April 16, 1995
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Rewritten by David Robinson. (drtr ast.cam.ac.uk)
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Rewritten again, and ported to APR by Colm MacCarthaigh
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Usage: logresolve [-s filename] [-c] < access_log > new_log
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Arguments:
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * -s filename name of a file to record statistics
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * -c check the DNS for a matching A record for the host.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Notes: (For historical interest)
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * To generate meaningful statistics from an HTTPD log file, it's good
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * to have the domain name of each machine that accessed your site, but
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * doing this on the fly can slow HTTPD down.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Compiling NCSA HTTPD with the -DMINIMAL_DNS flag turns IP#->hostname
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * resolution off. Before running your stats program, just run your log
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * file through this program (logresolve) and all of your IP numbers will
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * be resolved into hostnames (where possible).
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * logresolve takes an HTTPD access log (in the COMMON log file format,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * or any other format that has the IP number/domain name as the first
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * field for that matter), and outputs the same file with all of the
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * domain names looked up. Where no domain name can be found, the IP
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * number is left in.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * To minimize impact on your nameserver, logresolve has its very own
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * internal hash-table cache. This means that each IP number will only
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * be looked up the first time it is found in the log file.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder *
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * The -c option causes logresolve to apply the same check as httpd
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * compiled with -DMAXIMUM_DNS; after finding the hostname from the IP
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * address, it looks up the IP addresses for the hostname and checks
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * that one of these matches the original address.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder */
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#include "apr.h"
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#include "apr_lib.h"
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#include "apr_hash.h"
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#include "apr_getopt.h"
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#include "apr_strings.h"
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#include "apr_file_io.h"
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#include "apr_network_io.h"
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#if APR_HAVE_STDLIB_H
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#include <stdlib.h>
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#endif
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic apr_file_t *errfile;
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic const char *shortname = "logresolve";
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic apr_hash_t *cache;
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/* Statistics */
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic int cachehits = 0;
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic int cachesize = 0;
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic int entries = 0;
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic int resolves = 0;
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic int withname = 0;
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic int doublefailed = 0;
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic int noreverse = 0;
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/*
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * prints various statistics to output
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder */
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#define NL APR_EOL_STR
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic void print_statistics (apr_file_t *output)
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder{
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, "logresolve Statistics:" NL);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, "Entries: %d" NL, entries);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, " With name : %d" NL, withname);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, " Resolves : %d" NL, resolves);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder if (noreverse) {
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, " - No reverse : %d" NL,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder noreverse);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder }
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder if (doublefailed) {
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, " - Double lookup failed : %d" NL,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder doublefailed);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder }
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, "Cache hits : %d" NL, cachehits);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, "Cache size : %d" NL, cachesize);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder}
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/*
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * usage info
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder */
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic void usage(void)
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder{
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(errfile,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder "%s -- Resolve IP-addresses to hostnames in Apache log files." NL
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder "Usage: %s [-s STATFILE] [-c]" NL
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder NL
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder "Options:" NL
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder " -s Record statistics to STATFILE when finished." NL
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder NL
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder " -c Perform double lookups when resolving IP addresses." NL,
shortname, shortname);
exit(1);
}
#undef NL
int main(int argc, const char * const argv[])
{
apr_file_t * outfile;
apr_file_t * infile;
apr_file_t * statsfile;
apr_sockaddr_t * ip;
apr_sockaddr_t * ipdouble;
apr_getopt_t * o;
apr_pool_t * pool;
apr_status_t status;
const char * arg;
char opt;
char * stats = NULL;
char * space;
char * hostname;
#if APR_MAJOR_VERSION > 1 || (APR_MAJOR_VERSION == 1 && APR_MINOR_VERSION >= 3)
char * inbuffer;
char * outbuffer;
#endif
char line[2048];
int doublelookups = 0;
if (apr_app_initialize(&argc, &argv, NULL) != APR_SUCCESS) {
return 1;
}
atexit(apr_terminate);
if (argc) {
shortname = apr_filepath_name_get(argv[0]);
}
if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
return 1;
}
apr_file_open_stderr(&errfile, pool);
apr_getopt_init(&o, pool, argc, argv);
while (1) {
status = apr_getopt(o, "s:c", &opt, &arg);
if (status == APR_EOF) {
break;
}
else if (status != APR_SUCCESS) {
usage();
}
else {
switch (opt) {
case 'c':
if (doublelookups) {
usage();
}
doublelookups = 1;
break;
case 's':
if (stats) {
usage();
}
stats = apr_pstrdup(pool, arg);
break;
} /* switch */
} /* else */
} /* while */
apr_file_open_stdout(&outfile, pool);
apr_file_open_stdin(&infile, pool);
#if APR_MAJOR_VERSION > 1 || (APR_MAJOR_VERSION == 1 && APR_MINOR_VERSION >= 3)
/* Allocate two new 10k file buffers */
if ((outbuffer = apr_palloc(pool, 10240)) == NULL ||
(inbuffer = apr_palloc(pool, 10240)) == NULL) {
return 1;
}
/* Set the buffers */
apr_file_buffer_set(infile, inbuffer, 10240);
apr_file_buffer_set(outfile, outbuffer, 10240);
#endif
cache = apr_hash_make(pool);
while (apr_file_gets(line, 2048, infile) == APR_SUCCESS) {
if (line[0] == '\0') {
continue;
}
/* Count our log entries */
entries++;
/* Check if this could even be an IP address */
if (!apr_isxdigit(line[0]) && line[0] != ':') {
withname++;
apr_file_puts(line, outfile);
continue;
}
/* Terminate the line at the next space */
if ((space = strchr(line, ' ')) != NULL) {
*space = '\0';
}
/* See if we have it in our cache */
hostname = (char *) apr_hash_get(cache, line, APR_HASH_KEY_STRING);
if (hostname) {
apr_file_printf(outfile, "%s %s", hostname, space + 1);
cachehits++;
continue;
}
/* Parse the IP address */
status = apr_sockaddr_info_get(&ip, line, APR_UNSPEC ,0, 0, pool);
if (status != APR_SUCCESS) {
/* Not an IP address */
withname++;
if (space) *space = ' ';
apr_file_puts(line, outfile);
continue;
}
/* This does not make much sense, but historically "resolves" means
* "parsed as an IP address". It does not mean we actually resolved
* the IP address into a hostname.
*/
resolves++;
/* From here on our we cache each result, even if it was not
* succesful
*/
cachesize++;
/* Try and perform a reverse lookup */
status = apr_getnameinfo(&hostname, ip, 0) != APR_SUCCESS;
if (status || hostname == NULL) {
/* Could not perform a reverse lookup */
*space = ' ';
apr_file_puts(line, outfile);
noreverse++;
/* Add to cache */
*space = '\0';
apr_hash_set(cache, line, APR_HASH_KEY_STRING,
apr_pstrdup(pool, line));
continue;
}
/* Perform a double lookup */
if (doublelookups) {
/* Do a forward lookup on our hostname, and see if that matches our
* original IP address.
*/
status = apr_sockaddr_info_get(&ipdouble, hostname, ip->family, 0,
0, pool);
if (status == APR_SUCCESS ||
memcmp(ipdouble->ipaddr_ptr, ip->ipaddr_ptr, ip->ipaddr_len)) {
/* Double-lookup failed */
*space = ' ';
apr_file_puts(line, outfile);
doublefailed++;
/* Add to cache */
*space = '\0';
apr_hash_set(cache, line, APR_HASH_KEY_STRING,
apr_pstrdup(pool, line));
continue;
}
}
/* Outout the resolved name */
apr_file_printf(outfile, "%s %s", hostname, space + 1);
/* Store it in the cache */
apr_hash_set(cache, line, APR_HASH_KEY_STRING,
apr_pstrdup(pool, hostname));
}
/* Flush any remaining output */
apr_file_flush(outfile);
if (stats) {
if (apr_file_open(&statsfile, stats,
APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE,
APR_OS_DEFAULT, pool) != APR_SUCCESS) {
apr_file_printf(errfile, "%s: Could not open %s for writing.",
shortname, stats);
return 1;
}
print_statistics(statsfile);
apr_file_close(statsfile);
}
return 0;
}