/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
*
* Converts binary log files to CLF (Common Log Format).
*
*/
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <fcntl.h>
#include <stdio.h>
#include <locale.h>
#include <errno.h>
#include <time.h>
#include <synch.h>
#include <syslog.h>
#ifndef TRUE
#endif /* TRUE */
#ifndef FALSE
#define FALSE 0
#endif /* FALSE */
#include "ncadoorhdr.h"
#include "ncalogd.h"
extern char *gettext();
typedef enum { /* Boolean type */
false = 0,
true = 1
} bool;
static const char *const
"UNKNOWN",
"OPTIONS",
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"TRACE"
};
/* Short month strings */
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
};
static bool g_enable_directio = true;
/* init value must match logd & NCA kmod */
/* input binary file was written in 64k chunks by default */
/* num of output records, by default infinite */
/* start time for log output, default none (i.e. output all) */
/*
* http_version(version)
*
* Returns out the string of a given http version
*/
static char *
{
char *ver_num;
switch (http_ver) {
case HTTP_0_9:
case HTTP_0_0:
ver_num = "HTTP/0.9";
break;
case HTTP_ERR:
case HTTP_1_0:
ver_num = "HTTP/1.0";
break;
case HTTP_1_1:
ver_num = "HTTP/1.1";
break;
default:
}
return (ver_num);
}
static bool
{
switch (http_ver) {
case HTTP_0_9:
case HTTP_0_0:
case HTTP_1_0:
case HTTP_1_1:
return (true);
default:
break;
}
return (false);
}
static bool
{
switch (method) {
case NCA_OPTIONS:
case NCA_GET:
case NCA_HEAD:
case NCA_POST:
case NCA_PUT:
case NCA_DELETE:
case NCA_TRACE:
return (true);
default:
break;
}
return (false);
}
/*
* http_method
*
* Returns the method string for the given method.
*/
static char *
{
return ((char *)(g_method_strings[method]));
else
return ((char *)(g_method_strings[0]));
}
/* sMonth: Return short month string */
static const char *
{
}
/*
* Debug formatting routine. Returns a character string representation of the
* addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
* as a pointer. The "xxx" parts including left zero padding so the final
* string will fit easily in tables. It would be nice to take a padding
* length argument instead.
*/
static char *
{
return (buf);
}
/*
* Debug formatting routine. Returns a character string representation of the
* addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
* in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
*/
static char *
{
}
static int
{
long time_zone_info;
char sign;
return (0);
if (g_start_time.tm_year > 0 &&
/* clf record before the specified start time */
return (1);
}
if (local_time.tm_isdst)
else
if (time_zone_info < 0) {
sign = '-';
} else {
sign = '+';
}
"[%02d/%s/%04d:%02d:%02d:%02d %c%02ld%02ld]",
return (0);
}
/*
* xmalloc(size)
* Abort if malloc fails
*/
static void *
{
void *p;
if (! size)
size = 1;
abort();
}
return (p);
}
/*
* xstrdup(string)
* duplicate string
*/
static char *
{
char *new_string;
if (string) {
return (new_string);
}
return (NULL);
}
static void
usage()
{
"\nncab2clf [-Dhv] [-b <block-size>] [-i <binary-log-file>] "
"[-n <n>]\n"
"\tconverts a NCA binary log file to HTTP CLF"
" (Common Log Format)\n\n"
"\t-b <block-size>\n"
"\t\tinput file blocking size in KB\n"
"\t\t- default is 64K bytes\n"
"\t-D\tdisable directio on <output-file-name>\n"
"\t-h\tthis usage message\n"
"\t-i <binary-log-file>\n"
"\t\tspecify input file\n"
"\t-n <n>\n"
"\t\toutput <n> CLF records\n"
"\t-o <output-file>\n"
"\t\tspecify output file\n"
"by touch(1)\n"
"\t-v\tverbose output\n"
"\tNote: if no <output-file> - output goes to standard output\n"
"\tNote: if no <binary-log-file> - input is taken from standard "
"input\n"));
exit(3);
}
/*
* atoi_for2(p, value)
* - stores the numerical value of the two digit string p into value
* - return TRUE upon success and FALSE upon failure
*/
static int
return (FALSE);
return (TRUE);
}
/*
* parse_time(t, tm)
* - parses the string t to retrieve the UNIX time format as specified by
* touch(1).
* - return TRUE upon success and FALSE upon failure
*/
static int
{
int century = 0;
int seconds = 0;
char *p;
/*
* time in the following format (defined by the touch(1) spec):
* [[CC]YY]MMDDhhmm[.SS]
*/
return (FALSE);
return (FALSE);
*p = '\0';
}
switch (strlen(t)) {
case 12: /* CCYYMMDDhhmm */
return (FALSE);
t += 2;
/* FALLTHROUGH */
case 10: /* YYMMDDhhmm */
return (FALSE);
t += 2;
if (century == 0) {
} else
/* FALLTHROUGH */
case 8: /* MMDDhhmm */
return (FALSE);
t += 2;
return (FALSE);
t += 2;
return (FALSE);
t += 2;
return (FALSE);
break;
default:
return (FALSE);
}
return (TRUE);
}
static void
{
if (ifd != STDIN_FILENO)
if (ofd != STDOUT_FILENO)
}
/*
* Read the requested number of bytes from the given file descriptor
*/
static ssize_t
{
ssize_t i;
while (num_to_read > 0) {
if (i < 0) {
continue;
else
"Error: ncab2clf: "
"reading input file: %s\n"),
return (-1); /* some wierd interrupt */
}
if (i == 0)
break;
num_already_read += i;
num_to_read -= i;
}
return (num_already_read);
}
/*
* Write the requested number of bytes to the given file descriptor
*/
static ssize_t
{
ssize_t i;
while (num_to_write > 0) {
if (i < 0) {
continue;
else
"Error: ncab2clf: "
"writing output file: %s\n"),
return (-1); /* some wierd interrupt */
}
num_written += i;
num_to_write -= i;
}
return (num_written);
}
/* do constraint checks and determine if it's a valid header */
static bool
{
nca_log_stat_t *s;
h = (nca_log_buf_hdr_t *)ibuf;
/* Do some validity checks on ibuf */
return (false);
}
s = &(h->nca_logstats);
if (g_n_log_upcall == 0) {
g_n_log_upcall = s->n_log_upcall;
} else {
"Warning: ncab2clf:"
" expected record number (%d) is"
" different from the one seen (%d)\n."
" Resetting the expected record"
g_n_log_upcall = s->n_log_upcall;
}
}
return (true);
}
/* convert input binary buffer into CLF */
static int
void *ibuf,
char *obuf,
{
nca_log_stat_t *s;
char *br;
void *er;
int n_recs;
bool error_seen;
char *method;
char *http_version_string;
char *ruser;
char *req_url;
char *remote_ip;
h = (nca_log_buf_hdr_t *)ibuf;
s = &(h->nca_logstats);
r = (nca_request_log_t *)(&(h[1]));
/* OK, it's a valid buffer which we can use, go ahead and convert it */
*out_size = 0;
error_seen = false;
num_bytes_read = 0;
/* Make sure there is enough space in the output buffer */
(num_bytes_read >= max_input_size)) {
error_seen = true;
break;
}
((time_t)r->start_process_time))) {
/* A start time was speced and we're not there yet */
++g_skip_count;
goto skip;
}
/* Only logs valid HTTP ops */
if ((! valid_method((int)r->method)) ||
(! valid_version((int)r->version))) {
goto skip;
}
if (r->remote_user_len) {
} else {
ruser = "-";
}
if (r->request_url_len) {
} else {
req_url = "UNKNOWN";
}
"%s %s %s %s \"%s %s %s\" %d %d\n",
/* should be remote_log_name */
"-",
r->response_status,
r->response_len);
skip:
br = (char *)r;
er = ((char *)r) + NCA_LOG_REC_SIZE(r);
/*LINTED*/
if (g_out_records > 0 && --g_out_records == 0)
break;
}
if (error_seen) {
"Error: ncab2clf: "
"Input buffer not fully converted.\n"));
if (n_recs != s->n_log_recs)
"Warning: ncab2clf: "
"Converted only %d of %d records\n"),
n_recs, s->n_log_recs);
}
return (0);
}
static int
{
char *ibuf;
char *obuf;
bool error_seen;
bool eof_seen;
nca_log_stat_t *s;
error_seen = false;
eof_seen = false;
num_iterations = 0;
while (! eof_seen && g_out_records != 0) {
/* read the binary header first */
if (nh != sizeof (nca_log_buf_hdr_t)) {
eof_seen = true;
break;
}
if (! is_valid_header(ibuf)) {
"Error: ncab2clf: "
"Can't convert the input data to CLF\n"));
continue;
}
/* read the data to be converted */
/* LINTED */
h = (nca_log_buf_hdr_t *)ibuf;
s = &(h->nca_logstats);
if (s->n_log_size == 0)
continue;
if (ni < 0) {
error_seen = true;
break;
eof_seen = true;
}
if (ni == 0)
break;
/* convert binary input into text output */
"Error: ncab2clf: "
"Can't convert the input data to CLF\n"));
error_seen = true;
break;
}
/* write out the text data */
error_seen = true;
break;
}
}
if (error_seen)
return (-1);
return (0);
}
int
{
int c;
int ofd;
struct tm t;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
/* parse any arguments */
switch (c) {
case 'h':
usage();
break;
case 'i':
break;
case 'D':
g_enable_directio = false;
break;
case 'o':
break;
case 'b':
break;
case 'n':
break;
case 's':
bzero(&t, sizeof (t));
&t.tm_sec) == 6) {
/* Valid CLF time (e.g. 06/Apr/2001:09:14:14) */
t.tm_mon = 0;
do {
if (strcasecmp(monstr,
break;
} while (t.tm_mon++ < 12);
t.tm_year -= 1900;
g_start_time = t;
} else if (parse_time(optarg, &t)) {
g_start_time = t;
} else {
gettext("Error: ncab2clf:"
optarg);
}
break;
case 'v':
"verbose functionality not yet supported\n"));
exit(3);
break;
case '?':
usage();
break;
}
}
/* set up the input stream */
if (infile) {
gettext("Error: ncab2clf: "
"Failure to open binary log file %s: %s\n"),
exit(1);
}
} else {
ifd = STDIN_FILENO;
}
/* set up the output stream */
if (outfile) {
"Error: ncab2clf: "
"Failure to open output file %s: %s\n"),
exit(1);
}
/* Enable directio on output stream if specified */
if (g_enable_directio)
} else {
ofd = STDOUT_FILENO;
}
exit(2);
}
if (g_invalid_count) {
" number of invalid log records encountered in binary input"
" file were skipped\n"), g_invalid_count);
}
if (g_skip_count) {
" %d log records in binary input file before %s"
" were skipped\n"),
}
return (0);
}