xref: /titanic_50/usr/src/cmd/cmd-inet/usr.bin/nca/ncab2clf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  *
31*7c478bd9Sstevel@tonic-gate  *	Converts binary log files to CLF (Common Log Format).
32*7c478bd9Sstevel@tonic-gate  *
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <unistd.h>
37*7c478bd9Sstevel@tonic-gate #include <strings.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <locale.h>
42*7c478bd9Sstevel@tonic-gate #include <errno.h>
43*7c478bd9Sstevel@tonic-gate #include <time.h>
44*7c478bd9Sstevel@tonic-gate #include <synch.h>
45*7c478bd9Sstevel@tonic-gate #include <syslog.h>
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #ifndef	TRUE
48*7c478bd9Sstevel@tonic-gate #define	TRUE	1
49*7c478bd9Sstevel@tonic-gate #endif	/* TRUE */
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #ifndef	FALSE
52*7c478bd9Sstevel@tonic-gate #define	FALSE	0
53*7c478bd9Sstevel@tonic-gate #endif	/* FALSE */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #include "ncadoorhdr.h"
56*7c478bd9Sstevel@tonic-gate #include "ncalogd.h"
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate extern char *gettext();
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate typedef	enum	{	/* Boolean type */
61*7c478bd9Sstevel@tonic-gate 	false = 0,
62*7c478bd9Sstevel@tonic-gate 	true  = 1
63*7c478bd9Sstevel@tonic-gate } bool;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate static const char *const
66*7c478bd9Sstevel@tonic-gate g_method_strings[8] = {
67*7c478bd9Sstevel@tonic-gate 	"UNKNOWN",
68*7c478bd9Sstevel@tonic-gate 	"OPTIONS",
69*7c478bd9Sstevel@tonic-gate 	"GET",
70*7c478bd9Sstevel@tonic-gate 	"HEAD",
71*7c478bd9Sstevel@tonic-gate 	"POST",
72*7c478bd9Sstevel@tonic-gate 	"PUT",
73*7c478bd9Sstevel@tonic-gate 	"DELETE",
74*7c478bd9Sstevel@tonic-gate 	"TRACE"
75*7c478bd9Sstevel@tonic-gate };
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate /* Short month strings */
78*7c478bd9Sstevel@tonic-gate static const char * const sMonthStr [12] = {
79*7c478bd9Sstevel@tonic-gate 	"Jan",
80*7c478bd9Sstevel@tonic-gate 	"Feb",
81*7c478bd9Sstevel@tonic-gate 	"Mar",
82*7c478bd9Sstevel@tonic-gate 	"Apr",
83*7c478bd9Sstevel@tonic-gate 	"May",
84*7c478bd9Sstevel@tonic-gate 	"Jun",
85*7c478bd9Sstevel@tonic-gate 	"Jul",
86*7c478bd9Sstevel@tonic-gate 	"Aug",
87*7c478bd9Sstevel@tonic-gate 	"Sep",
88*7c478bd9Sstevel@tonic-gate 	"Oct",
89*7c478bd9Sstevel@tonic-gate 	"Nov",
90*7c478bd9Sstevel@tonic-gate 	"Dec",
91*7c478bd9Sstevel@tonic-gate };
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate #define	SEC_PER_MIN		(60)
94*7c478bd9Sstevel@tonic-gate #define	SEC_PER_HOUR		(60*60)
95*7c478bd9Sstevel@tonic-gate #define	SEC_PER_DAY		(24*60*60)
96*7c478bd9Sstevel@tonic-gate #define	SEC_PER_YEAR		(365*24*60*60)
97*7c478bd9Sstevel@tonic-gate #define	LEAP_TO_70		(70/4)
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate #define	KILO_BYTE		(1024)
100*7c478bd9Sstevel@tonic-gate #define	MEGA_BYTE		(KILO_BYTE * KILO_BYTE)
101*7c478bd9Sstevel@tonic-gate #define	GIGA_BYTE		(KILO_BYTE * MEGA_BYTE)
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate #define	CLF_DATE_BUF_LENGTH	(128)
104*7c478bd9Sstevel@tonic-gate #define	OUTFILE_BUF_SIZE	(256 * KILO_BYTE)
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate static bool	g_enable_directio = true;
107*7c478bd9Sstevel@tonic-gate static ssize_t	g_invalid_count = 0;
108*7c478bd9Sstevel@tonic-gate static ssize_t	g_skip_count = 0;
109*7c478bd9Sstevel@tonic-gate static char	*g_start_time_str = NULL;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /* init value must match logd & NCA kmod */
112*7c478bd9Sstevel@tonic-gate static ssize_t	g_n_log_upcall = 0;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate /* input binary file was written in 64k chunks by default  */
115*7c478bd9Sstevel@tonic-gate static ssize_t	g_infile_blk_size = NCA_DEFAULT_LOG_BUF_SIZE;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate /* num of output records, by default infinite */
118*7c478bd9Sstevel@tonic-gate static ssize_t	g_out_records = -1;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /* start time for log output, default none (i.e. output all) */
121*7c478bd9Sstevel@tonic-gate static struct tm g_start_time;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate /*
124*7c478bd9Sstevel@tonic-gate  * http_version(version)
125*7c478bd9Sstevel@tonic-gate  *
126*7c478bd9Sstevel@tonic-gate  * Returns out the string of a given http version
127*7c478bd9Sstevel@tonic-gate  */
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate static char *
http_version(int http_ver)130*7c478bd9Sstevel@tonic-gate http_version(int http_ver)
131*7c478bd9Sstevel@tonic-gate {
132*7c478bd9Sstevel@tonic-gate 	char	*ver_num;
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	switch (http_ver) {
135*7c478bd9Sstevel@tonic-gate 	case HTTP_0_9:
136*7c478bd9Sstevel@tonic-gate 	case HTTP_0_0:
137*7c478bd9Sstevel@tonic-gate 		ver_num = "HTTP/0.9";
138*7c478bd9Sstevel@tonic-gate 		break;
139*7c478bd9Sstevel@tonic-gate 	case HTTP_ERR:
140*7c478bd9Sstevel@tonic-gate 	case HTTP_1_0:
141*7c478bd9Sstevel@tonic-gate 		ver_num = "HTTP/1.0";
142*7c478bd9Sstevel@tonic-gate 		break;
143*7c478bd9Sstevel@tonic-gate 	case HTTP_1_1:
144*7c478bd9Sstevel@tonic-gate 		ver_num = "HTTP/1.1";
145*7c478bd9Sstevel@tonic-gate 		break;
146*7c478bd9Sstevel@tonic-gate 	default:
147*7c478bd9Sstevel@tonic-gate 		ver_num = "HTTP/unknown";
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	return (ver_num);
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate static bool
valid_version(int http_ver)154*7c478bd9Sstevel@tonic-gate valid_version(int http_ver)
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	switch (http_ver) {
157*7c478bd9Sstevel@tonic-gate 	case HTTP_0_9:
158*7c478bd9Sstevel@tonic-gate 	case HTTP_0_0:
159*7c478bd9Sstevel@tonic-gate 	case HTTP_1_0:
160*7c478bd9Sstevel@tonic-gate 	case HTTP_1_1:
161*7c478bd9Sstevel@tonic-gate 		return (true);
162*7c478bd9Sstevel@tonic-gate 	default:
163*7c478bd9Sstevel@tonic-gate 		break;
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	return (false);
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate static bool
valid_method(int method)170*7c478bd9Sstevel@tonic-gate valid_method(int method)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate 	switch (method) {
173*7c478bd9Sstevel@tonic-gate 	case NCA_OPTIONS:
174*7c478bd9Sstevel@tonic-gate 	case NCA_GET:
175*7c478bd9Sstevel@tonic-gate 	case NCA_HEAD:
176*7c478bd9Sstevel@tonic-gate 	case NCA_POST:
177*7c478bd9Sstevel@tonic-gate 	case NCA_PUT:
178*7c478bd9Sstevel@tonic-gate 	case NCA_DELETE:
179*7c478bd9Sstevel@tonic-gate 	case NCA_TRACE:
180*7c478bd9Sstevel@tonic-gate 		return (true);
181*7c478bd9Sstevel@tonic-gate 	default:
182*7c478bd9Sstevel@tonic-gate 		break;
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	return (false);
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate /*
189*7c478bd9Sstevel@tonic-gate  * http_method
190*7c478bd9Sstevel@tonic-gate  *
191*7c478bd9Sstevel@tonic-gate  *   Returns the method string for the given method.
192*7c478bd9Sstevel@tonic-gate  */
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate static char *
http_method(int method)195*7c478bd9Sstevel@tonic-gate http_method(int method)
196*7c478bd9Sstevel@tonic-gate {
197*7c478bd9Sstevel@tonic-gate 	if (method < sizeof (g_method_strings) / sizeof (g_method_strings[0]))
198*7c478bd9Sstevel@tonic-gate 		return ((char *)(g_method_strings[method]));
199*7c478bd9Sstevel@tonic-gate 	else
200*7c478bd9Sstevel@tonic-gate 		return ((char *)(g_method_strings[0]));
201*7c478bd9Sstevel@tonic-gate }
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate /* sMonth: Return short month string */
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate static const char *
sMonth(int index)206*7c478bd9Sstevel@tonic-gate sMonth(int index)
207*7c478bd9Sstevel@tonic-gate {
208*7c478bd9Sstevel@tonic-gate 	return (sMonthStr[index]);
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate  * Debug formatting routine.  Returns a character string representation of the
213*7c478bd9Sstevel@tonic-gate  * addr in buf, of the form xxx.xxx.xxx.xxx.  This routine takes the address
214*7c478bd9Sstevel@tonic-gate  * as a pointer.  The "xxx" parts including left zero padding so the final
215*7c478bd9Sstevel@tonic-gate  * string will fit easily in tables.  It would be nice to take a padding
216*7c478bd9Sstevel@tonic-gate  * length argument instead.
217*7c478bd9Sstevel@tonic-gate  */
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate static char *
ip_dot_saddr(uchar_t * addr,char * buf)220*7c478bd9Sstevel@tonic-gate ip_dot_saddr(uchar_t *addr, char *buf)
221*7c478bd9Sstevel@tonic-gate {
222*7c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "%03d.%03d.%03d.%03d",
223*7c478bd9Sstevel@tonic-gate 	    addr[0] & 0xFF, addr[1] & 0xFF, addr[2] & 0xFF, addr[3] & 0xFF);
224*7c478bd9Sstevel@tonic-gate 	return (buf);
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate /*
228*7c478bd9Sstevel@tonic-gate  * Debug formatting routine.  Returns a character string representation of the
229*7c478bd9Sstevel@tonic-gate  * addr in buf, of the form xxx.xxx.xxx.xxx.  This routine takes the address
230*7c478bd9Sstevel@tonic-gate  * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
231*7c478bd9Sstevel@tonic-gate  */
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate static char *
ip_dot_addr(ipaddr_t addr,char * buf)234*7c478bd9Sstevel@tonic-gate ip_dot_addr(ipaddr_t addr, char *buf)
235*7c478bd9Sstevel@tonic-gate {
236*7c478bd9Sstevel@tonic-gate 	return (ip_dot_saddr((uchar_t *)&addr, buf));
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate static int
http_clf_date(char * buf,int bufsize,time_t t)240*7c478bd9Sstevel@tonic-gate http_clf_date(char *buf, int bufsize, time_t t)
241*7c478bd9Sstevel@tonic-gate {
242*7c478bd9Sstevel@tonic-gate 	struct tm	local_time;
243*7c478bd9Sstevel@tonic-gate 	long		time_zone_info;
244*7c478bd9Sstevel@tonic-gate 	char		sign;
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	if (localtime_r(&t, &local_time) == NULL)
247*7c478bd9Sstevel@tonic-gate 		return (0);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	if (g_start_time.tm_year > 0 &&
250*7c478bd9Sstevel@tonic-gate 	    (local_time.tm_year < g_start_time.tm_year ||
251*7c478bd9Sstevel@tonic-gate 	    (local_time.tm_year == g_start_time.tm_year &&
252*7c478bd9Sstevel@tonic-gate 	    local_time.tm_mon < g_start_time.tm_mon ||
253*7c478bd9Sstevel@tonic-gate 	    (local_time.tm_mon == g_start_time.tm_mon &&
254*7c478bd9Sstevel@tonic-gate 	    local_time.tm_mday < g_start_time.tm_mday ||
255*7c478bd9Sstevel@tonic-gate 	    (local_time.tm_mday == g_start_time.tm_mday &&
256*7c478bd9Sstevel@tonic-gate 	    local_time.tm_hour < g_start_time.tm_hour ||
257*7c478bd9Sstevel@tonic-gate 	    (local_time.tm_hour == g_start_time.tm_hour &&
258*7c478bd9Sstevel@tonic-gate 	    local_time.tm_min < g_start_time.tm_min ||
259*7c478bd9Sstevel@tonic-gate 	    (local_time.tm_min == g_start_time.tm_min &&
260*7c478bd9Sstevel@tonic-gate 	    local_time.tm_sec < g_start_time.tm_sec))))))) {
261*7c478bd9Sstevel@tonic-gate 		/* clf record before the specified start time */
262*7c478bd9Sstevel@tonic-gate 		return (1);
263*7c478bd9Sstevel@tonic-gate 	}
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	if (local_time.tm_isdst)
266*7c478bd9Sstevel@tonic-gate 		time_zone_info = -timezone + SEC_PER_HOUR;
267*7c478bd9Sstevel@tonic-gate 	else
268*7c478bd9Sstevel@tonic-gate 		time_zone_info = -timezone;
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	if (time_zone_info < 0) {
271*7c478bd9Sstevel@tonic-gate 		sign = '-';
272*7c478bd9Sstevel@tonic-gate 		time_zone_info = -time_zone_info;
273*7c478bd9Sstevel@tonic-gate 	} else {
274*7c478bd9Sstevel@tonic-gate 		sign = '+';
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, bufsize,
278*7c478bd9Sstevel@tonic-gate 		"[%02d/%s/%04d:%02d:%02d:%02d %c%02ld%02ld]",
279*7c478bd9Sstevel@tonic-gate 		local_time.tm_mday, sMonth(local_time.tm_mon),
280*7c478bd9Sstevel@tonic-gate 		1900 + local_time.tm_year, local_time.tm_hour,
281*7c478bd9Sstevel@tonic-gate 		local_time.tm_min, local_time.tm_sec,
282*7c478bd9Sstevel@tonic-gate 		sign, time_zone_info / SEC_PER_HOUR,
283*7c478bd9Sstevel@tonic-gate 		time_zone_info % SEC_PER_HOUR);
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 	return (0);
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate /*
289*7c478bd9Sstevel@tonic-gate  * xmalloc(size)
290*7c478bd9Sstevel@tonic-gate  * Abort if malloc fails
291*7c478bd9Sstevel@tonic-gate  */
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate static void *
xmalloc(size_t size)294*7c478bd9Sstevel@tonic-gate xmalloc(size_t size)
295*7c478bd9Sstevel@tonic-gate {
296*7c478bd9Sstevel@tonic-gate 	void *p;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	if (! size)
299*7c478bd9Sstevel@tonic-gate 		size = 1;
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	if ((p = malloc(size)) == NULL) {
302*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("Error: ncab2clf: Out of memory\n"));
303*7c478bd9Sstevel@tonic-gate 		abort();
304*7c478bd9Sstevel@tonic-gate 	}
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	return (p);
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate /*
310*7c478bd9Sstevel@tonic-gate  * xstrdup(string)
311*7c478bd9Sstevel@tonic-gate  *   duplicate string
312*7c478bd9Sstevel@tonic-gate  */
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate static char *
xstrdup(const char * string)315*7c478bd9Sstevel@tonic-gate xstrdup(const char *string)
316*7c478bd9Sstevel@tonic-gate {
317*7c478bd9Sstevel@tonic-gate 	char	*new_string;
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	if (string) {
320*7c478bd9Sstevel@tonic-gate 		new_string = xmalloc(strlen(string) + 1);
321*7c478bd9Sstevel@tonic-gate 		(void) strcpy(new_string, string);
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 		return (new_string);
324*7c478bd9Sstevel@tonic-gate 	}
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 	return (NULL);
327*7c478bd9Sstevel@tonic-gate }
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate static void
usage()330*7c478bd9Sstevel@tonic-gate usage()
331*7c478bd9Sstevel@tonic-gate {
332*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
333*7c478bd9Sstevel@tonic-gate 	    "\nncab2clf [-Dhv] [-b <block-size>] [-i <binary-log-file>] "
334*7c478bd9Sstevel@tonic-gate 			"[-n <n>]\n"
335*7c478bd9Sstevel@tonic-gate 		"    [-o <output-file>] [-s <date/time>]\n"
336*7c478bd9Sstevel@tonic-gate 	    "\tconverts a NCA binary log file to HTTP CLF"
337*7c478bd9Sstevel@tonic-gate 		" (Common Log Format)\n\n"
338*7c478bd9Sstevel@tonic-gate 	    "\t-b <block-size>\n"
339*7c478bd9Sstevel@tonic-gate 		"\t\tinput file blocking size in KB\n"
340*7c478bd9Sstevel@tonic-gate 		"\t\t- default is 64K bytes\n"
341*7c478bd9Sstevel@tonic-gate 	    "\t-D\tdisable directio on <output-file-name>\n"
342*7c478bd9Sstevel@tonic-gate 	    "\t-h\tthis usage message\n"
343*7c478bd9Sstevel@tonic-gate 	    "\t-i <binary-log-file>\n"
344*7c478bd9Sstevel@tonic-gate 		"\t\tspecify input file\n"
345*7c478bd9Sstevel@tonic-gate 	    "\t-n <n>\n"
346*7c478bd9Sstevel@tonic-gate 		"\t\toutput <n> CLF records\n"
347*7c478bd9Sstevel@tonic-gate 	    "\t-o <output-file>\n"
348*7c478bd9Sstevel@tonic-gate 		"\t\tspecify output file\n"
349*7c478bd9Sstevel@tonic-gate 	    "\t-s <date/time>\n"
350*7c478bd9Sstevel@tonic-gate 		"\t\tskip any records before <date/time>\n"
351*7c478bd9Sstevel@tonic-gate 		"\t\t- <date/time> may be in CLF format\n"
352*7c478bd9Sstevel@tonic-gate 		"\t\t- <date/time> may be in time format as specified "
353*7c478bd9Sstevel@tonic-gate 			"by touch(1)\n"
354*7c478bd9Sstevel@tonic-gate 	    "\t-v\tverbose output\n"
355*7c478bd9Sstevel@tonic-gate 	    "\tNote: if no <output-file> - output goes to standard output\n"
356*7c478bd9Sstevel@tonic-gate 	    "\tNote: if no <binary-log-file> - input is taken from standard "
357*7c478bd9Sstevel@tonic-gate 			"input\n"));
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	exit(3);
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate /*
363*7c478bd9Sstevel@tonic-gate  * atoi_for2(p, value)
364*7c478bd9Sstevel@tonic-gate  *   - stores the numerical value of the two digit string p into value
365*7c478bd9Sstevel@tonic-gate  *   - return TRUE upon success and FALSE upon failure
366*7c478bd9Sstevel@tonic-gate  */
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate static int
atoi_for2(char * p,int * value)369*7c478bd9Sstevel@tonic-gate atoi_for2(char *p, int *value) {
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 	*value = (*p - '0') * 10 + *(p+1) - '0';
372*7c478bd9Sstevel@tonic-gate 	if ((*value < 0) || (*value > 99))
373*7c478bd9Sstevel@tonic-gate 		return (FALSE);
374*7c478bd9Sstevel@tonic-gate 	return (TRUE);
375*7c478bd9Sstevel@tonic-gate }
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate /*
378*7c478bd9Sstevel@tonic-gate  * parse_time(t, tm)
379*7c478bd9Sstevel@tonic-gate  *   - parses the string t to retrieve the UNIX time format as specified by
380*7c478bd9Sstevel@tonic-gate  *     touch(1).
381*7c478bd9Sstevel@tonic-gate  *   - return TRUE upon success and FALSE upon failure
382*7c478bd9Sstevel@tonic-gate  */
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate static int
parse_time(char * t,struct tm * tm)385*7c478bd9Sstevel@tonic-gate parse_time(char *t, struct tm *tm)
386*7c478bd9Sstevel@tonic-gate {
387*7c478bd9Sstevel@tonic-gate 	int		century = 0;
388*7c478bd9Sstevel@tonic-gate 	int		seconds = 0;
389*7c478bd9Sstevel@tonic-gate 	time_t		when;
390*7c478bd9Sstevel@tonic-gate 	char		*p;
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	/*
393*7c478bd9Sstevel@tonic-gate 	 * time in the following format (defined by the touch(1) spec):
394*7c478bd9Sstevel@tonic-gate 	 *	[[CC]YY]MMDDhhmm[.SS]
395*7c478bd9Sstevel@tonic-gate 	 */
396*7c478bd9Sstevel@tonic-gate 	if ((p = strchr(t, '.')) != NULL) {
397*7c478bd9Sstevel@tonic-gate 		if (strchr(p+1, '.') != NULL)
398*7c478bd9Sstevel@tonic-gate 			return (FALSE);
399*7c478bd9Sstevel@tonic-gate 		if (!atoi_for2(p+1, &seconds))
400*7c478bd9Sstevel@tonic-gate 			return (FALSE);
401*7c478bd9Sstevel@tonic-gate 		*p = '\0';
402*7c478bd9Sstevel@tonic-gate 	}
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	when = time(0);
405*7c478bd9Sstevel@tonic-gate 	bzero(tm, sizeof (struct tm));
406*7c478bd9Sstevel@tonic-gate 	tm->tm_year = localtime(&when)->tm_year;
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	switch (strlen(t)) {
409*7c478bd9Sstevel@tonic-gate 		case 12:	/* CCYYMMDDhhmm */
410*7c478bd9Sstevel@tonic-gate 			if (!atoi_for2(t, &century))
411*7c478bd9Sstevel@tonic-gate 				return (FALSE);
412*7c478bd9Sstevel@tonic-gate 			t += 2;
413*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
414*7c478bd9Sstevel@tonic-gate 		case 10:	/* YYMMDDhhmm */
415*7c478bd9Sstevel@tonic-gate 			if (!atoi_for2(t, &tm->tm_year))
416*7c478bd9Sstevel@tonic-gate 				return (FALSE);
417*7c478bd9Sstevel@tonic-gate 			t += 2;
418*7c478bd9Sstevel@tonic-gate 			if (century == 0) {
419*7c478bd9Sstevel@tonic-gate 				if (tm->tm_year < 69)
420*7c478bd9Sstevel@tonic-gate 					tm->tm_year += 100;
421*7c478bd9Sstevel@tonic-gate 			} else
422*7c478bd9Sstevel@tonic-gate 				tm->tm_year += (century - 19) * 100;
423*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
424*7c478bd9Sstevel@tonic-gate 		case 8:		/* MMDDhhmm */
425*7c478bd9Sstevel@tonic-gate 			if (!atoi_for2(t, &tm->tm_mon))
426*7c478bd9Sstevel@tonic-gate 				return (FALSE);
427*7c478bd9Sstevel@tonic-gate 			tm->tm_mon--;
428*7c478bd9Sstevel@tonic-gate 			t += 2;
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 			if (!atoi_for2(t, &tm->tm_mday))
431*7c478bd9Sstevel@tonic-gate 				return (FALSE);
432*7c478bd9Sstevel@tonic-gate 			t += 2;
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 			if (!atoi_for2(t, &tm->tm_hour))
435*7c478bd9Sstevel@tonic-gate 				return (FALSE);
436*7c478bd9Sstevel@tonic-gate 			t += 2;
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate 			if (!atoi_for2(t, &tm->tm_min))
439*7c478bd9Sstevel@tonic-gate 				return (FALSE);
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 			tm->tm_sec = seconds;
442*7c478bd9Sstevel@tonic-gate 			break;
443*7c478bd9Sstevel@tonic-gate 		default:
444*7c478bd9Sstevel@tonic-gate 			return (FALSE);
445*7c478bd9Sstevel@tonic-gate 	}
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	return (TRUE);
448*7c478bd9Sstevel@tonic-gate }
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate static void
close_files(int ifd,int ofd)451*7c478bd9Sstevel@tonic-gate close_files(int ifd, int ofd)
452*7c478bd9Sstevel@tonic-gate {
453*7c478bd9Sstevel@tonic-gate 	if (ifd != STDIN_FILENO)
454*7c478bd9Sstevel@tonic-gate 		(void) close(ifd);
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	if (ofd != STDOUT_FILENO)
457*7c478bd9Sstevel@tonic-gate 		(void) close(ofd);
458*7c478bd9Sstevel@tonic-gate }
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate /*
461*7c478bd9Sstevel@tonic-gate  * Read the requested number of bytes from the given file descriptor
462*7c478bd9Sstevel@tonic-gate  */
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate static ssize_t
read_n_bytes(int fd,char * buf,ssize_t bufsize)465*7c478bd9Sstevel@tonic-gate read_n_bytes(int fd, char *buf, ssize_t bufsize)
466*7c478bd9Sstevel@tonic-gate {
467*7c478bd9Sstevel@tonic-gate 	ssize_t	num_to_read = bufsize;
468*7c478bd9Sstevel@tonic-gate 	ssize_t	num_already_read = 0;
469*7c478bd9Sstevel@tonic-gate 	ssize_t	i;
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 	while (num_to_read > 0) {
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 		i = read(fd, &(buf[num_already_read]), num_to_read);
474*7c478bd9Sstevel@tonic-gate 		if (i < 0) {
475*7c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
476*7c478bd9Sstevel@tonic-gate 				continue;
477*7c478bd9Sstevel@tonic-gate 			else
478*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
479*7c478bd9Sstevel@tonic-gate 				    "Error: ncab2clf: "
480*7c478bd9Sstevel@tonic-gate 				    "reading input file: %s\n"),
481*7c478bd9Sstevel@tonic-gate 				    strerror(errno));
482*7c478bd9Sstevel@tonic-gate 				return (-1);	/* some wierd interrupt */
483*7c478bd9Sstevel@tonic-gate 		}
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate 		if (i == 0)
486*7c478bd9Sstevel@tonic-gate 			break;
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate 		num_already_read += i;
489*7c478bd9Sstevel@tonic-gate 		num_to_read -= i;
490*7c478bd9Sstevel@tonic-gate 	}
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate 	return (num_already_read);
493*7c478bd9Sstevel@tonic-gate }
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate /*
496*7c478bd9Sstevel@tonic-gate  * Write the requested number of bytes to the given file descriptor
497*7c478bd9Sstevel@tonic-gate  */
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate static ssize_t
write_n_bytes(int fd,char * buf,ssize_t bufsize)500*7c478bd9Sstevel@tonic-gate write_n_bytes(int fd, char *buf, ssize_t bufsize)
501*7c478bd9Sstevel@tonic-gate {
502*7c478bd9Sstevel@tonic-gate 	ssize_t	num_to_write = bufsize;
503*7c478bd9Sstevel@tonic-gate 	ssize_t	num_written = 0;
504*7c478bd9Sstevel@tonic-gate 	ssize_t	i;
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 	while (num_to_write > 0) {
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate 		i = write(fd, &(buf[num_written]), num_to_write);
509*7c478bd9Sstevel@tonic-gate 		if (i < 0) {
510*7c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
511*7c478bd9Sstevel@tonic-gate 				continue;
512*7c478bd9Sstevel@tonic-gate 			else
513*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
514*7c478bd9Sstevel@tonic-gate 				    "Error: ncab2clf: "
515*7c478bd9Sstevel@tonic-gate 				    "writing output file: %s\n"),
516*7c478bd9Sstevel@tonic-gate 				    strerror(errno));
517*7c478bd9Sstevel@tonic-gate 				return (-1);	/* some wierd interrupt */
518*7c478bd9Sstevel@tonic-gate 		}
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate 		num_written += i;
521*7c478bd9Sstevel@tonic-gate 		num_to_write -= i;
522*7c478bd9Sstevel@tonic-gate 	}
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 	return (num_written);
525*7c478bd9Sstevel@tonic-gate }
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate /* do constraint checks and determine if it's a valid header */
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate static bool
is_valid_header(void * ibuf)530*7c478bd9Sstevel@tonic-gate is_valid_header(void *ibuf)
531*7c478bd9Sstevel@tonic-gate {
532*7c478bd9Sstevel@tonic-gate 	nca_log_buf_hdr_t	*h;
533*7c478bd9Sstevel@tonic-gate 	nca_log_stat_t		*s;
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 	h = (nca_log_buf_hdr_t *)ibuf;
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 	/* Do some validity checks on ibuf */
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 	if (((h->nca_loghdr).nca_version != NCA_LOG_VERSION1) ||
540*7c478bd9Sstevel@tonic-gate 	    ((h->nca_loghdr).nca_op != log_op)) {
541*7c478bd9Sstevel@tonic-gate 		return (false);
542*7c478bd9Sstevel@tonic-gate 	}
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	s = &(h->nca_logstats);
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 	if (g_n_log_upcall == 0) {
547*7c478bd9Sstevel@tonic-gate 		g_n_log_upcall = s->n_log_upcall;
548*7c478bd9Sstevel@tonic-gate 	} else {
549*7c478bd9Sstevel@tonic-gate 		if ((++g_n_log_upcall) != (ssize_t)s->n_log_upcall) {
550*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
551*7c478bd9Sstevel@tonic-gate 				"Warning: ncab2clf:"
552*7c478bd9Sstevel@tonic-gate 				" expected record number (%d) is"
553*7c478bd9Sstevel@tonic-gate 				" different from the one seen (%d)\n."
554*7c478bd9Sstevel@tonic-gate 				" Resetting the expected record"
555*7c478bd9Sstevel@tonic-gate 				" number.\n"), g_n_log_upcall, s->n_log_upcall);
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate 			g_n_log_upcall = s->n_log_upcall;
558*7c478bd9Sstevel@tonic-gate 		}
559*7c478bd9Sstevel@tonic-gate 	}
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate 	return (true);
562*7c478bd9Sstevel@tonic-gate }
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate /* convert input binary buffer into CLF */
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate static int
b2clf_buf(void * ibuf,char * obuf,ssize_t isize,ssize_t osize,ssize_t * out_size)567*7c478bd9Sstevel@tonic-gate b2clf_buf(
568*7c478bd9Sstevel@tonic-gate 	void	*ibuf,
569*7c478bd9Sstevel@tonic-gate 	char	*obuf,
570*7c478bd9Sstevel@tonic-gate 	ssize_t	isize,
571*7c478bd9Sstevel@tonic-gate 	ssize_t	osize,
572*7c478bd9Sstevel@tonic-gate 	ssize_t	*out_size)
573*7c478bd9Sstevel@tonic-gate {
574*7c478bd9Sstevel@tonic-gate 	nca_log_buf_hdr_t	*h;
575*7c478bd9Sstevel@tonic-gate 	nca_log_stat_t		*s;
576*7c478bd9Sstevel@tonic-gate 	nca_request_log_t	*r;
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate 	char	*br;
579*7c478bd9Sstevel@tonic-gate 	void	*er;
580*7c478bd9Sstevel@tonic-gate 	char	ip_buf[64];
581*7c478bd9Sstevel@tonic-gate 	ssize_t	max_input_size, num_bytes_read;
582*7c478bd9Sstevel@tonic-gate 	int	n_recs;
583*7c478bd9Sstevel@tonic-gate 	bool	error_seen;
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 	ssize_t	count;
586*7c478bd9Sstevel@tonic-gate 	char	clf_timebuf[CLF_DATE_BUF_LENGTH];
587*7c478bd9Sstevel@tonic-gate 	char	*method;
588*7c478bd9Sstevel@tonic-gate 	char	*http_version_string;
589*7c478bd9Sstevel@tonic-gate 	char	*ruser;
590*7c478bd9Sstevel@tonic-gate 	char	*req_url;
591*7c478bd9Sstevel@tonic-gate 	char	*remote_ip;
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 	h = (nca_log_buf_hdr_t *)ibuf;
594*7c478bd9Sstevel@tonic-gate 	s = &(h->nca_logstats);
595*7c478bd9Sstevel@tonic-gate 	r = (nca_request_log_t *)(&(h[1]));
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 	/* OK, it's a valid buffer which we can use, go ahead and convert it */
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate 	max_input_size = (ssize_t)isize - sizeof (nca_log_buf_hdr_t);
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 	*out_size = 0;
602*7c478bd9Sstevel@tonic-gate 	error_seen = false;
603*7c478bd9Sstevel@tonic-gate 	num_bytes_read = 0;
604*7c478bd9Sstevel@tonic-gate 	for (n_recs = 0; n_recs < s->n_log_recs; n_recs++) {
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate 		/* Make sure there is enough space in the output buffer */
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 		if ((*out_size >= osize) ||
609*7c478bd9Sstevel@tonic-gate 				(num_bytes_read >= max_input_size)) {
610*7c478bd9Sstevel@tonic-gate 			error_seen = true;
611*7c478bd9Sstevel@tonic-gate 			break;
612*7c478bd9Sstevel@tonic-gate 		}
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 		if (http_clf_date(clf_timebuf, sizeof (clf_timebuf),
615*7c478bd9Sstevel@tonic-gate 		    ((time_t)r->start_process_time))) {
616*7c478bd9Sstevel@tonic-gate 			/* A start time was speced and we're not there yet */
617*7c478bd9Sstevel@tonic-gate 			++g_skip_count;
618*7c478bd9Sstevel@tonic-gate 			goto skip;
619*7c478bd9Sstevel@tonic-gate 		}
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 		/* Only logs valid HTTP ops */
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate 		if ((! valid_method((int)r->method)) ||
624*7c478bd9Sstevel@tonic-gate 				(! valid_version((int)r->version))) {
625*7c478bd9Sstevel@tonic-gate 			++g_invalid_count;
626*7c478bd9Sstevel@tonic-gate 			goto skip;
627*7c478bd9Sstevel@tonic-gate 		}
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate 		method = http_method((int)r->method);
630*7c478bd9Sstevel@tonic-gate 		http_version_string = http_version((int)r->version);
631*7c478bd9Sstevel@tonic-gate 
632*7c478bd9Sstevel@tonic-gate 		remote_ip = ip_dot_addr(r->remote_host, (char *)&ip_buf);
633*7c478bd9Sstevel@tonic-gate 		if (r->remote_user_len) {
634*7c478bd9Sstevel@tonic-gate 			ruser = NCA_REQLOG_RDATA(r, remote_user);
635*7c478bd9Sstevel@tonic-gate 		} else {
636*7c478bd9Sstevel@tonic-gate 			ruser = "-";
637*7c478bd9Sstevel@tonic-gate 		}
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 		if (r->request_url_len) {
640*7c478bd9Sstevel@tonic-gate 			req_url = NCA_REQLOG_RDATA(r, request_url);
641*7c478bd9Sstevel@tonic-gate 		} else {
642*7c478bd9Sstevel@tonic-gate 			req_url = "UNKNOWN";
643*7c478bd9Sstevel@tonic-gate 		}
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate 		count = (ssize_t)snprintf(&(obuf[*out_size]), osize - *out_size,
646*7c478bd9Sstevel@tonic-gate 				"%s %s %s %s \"%s %s %s\" %d %d\n",
647*7c478bd9Sstevel@tonic-gate 				((remote_ip) ? remote_ip : "-"),
648*7c478bd9Sstevel@tonic-gate 				/* should be remote_log_name */
649*7c478bd9Sstevel@tonic-gate 				"-",
650*7c478bd9Sstevel@tonic-gate 				ruser,
651*7c478bd9Sstevel@tonic-gate 				clf_timebuf,
652*7c478bd9Sstevel@tonic-gate 				method,
653*7c478bd9Sstevel@tonic-gate 				req_url,
654*7c478bd9Sstevel@tonic-gate 				http_version_string,
655*7c478bd9Sstevel@tonic-gate 				r->response_status,
656*7c478bd9Sstevel@tonic-gate 				r->response_len);
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate 		*out_size += count;
659*7c478bd9Sstevel@tonic-gate 	skip:
660*7c478bd9Sstevel@tonic-gate 		br = (char *)r;
661*7c478bd9Sstevel@tonic-gate 		er = ((char *)r) + NCA_LOG_REC_SIZE(r);
662*7c478bd9Sstevel@tonic-gate 
663*7c478bd9Sstevel@tonic-gate 		/*LINTED*/
664*7c478bd9Sstevel@tonic-gate 		r = (nca_request_log_t *)NCA_LOG_ALIGN(er);
665*7c478bd9Sstevel@tonic-gate 		num_bytes_read += (ssize_t)(((char *)r) - br);
666*7c478bd9Sstevel@tonic-gate 		if (g_out_records > 0 && --g_out_records == 0)
667*7c478bd9Sstevel@tonic-gate 			break;
668*7c478bd9Sstevel@tonic-gate 	}
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 	if (error_seen) {
671*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
672*7c478bd9Sstevel@tonic-gate 			"Error: ncab2clf: "
673*7c478bd9Sstevel@tonic-gate 			"Input buffer not fully converted.\n"));
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate 		if (n_recs != s->n_log_recs)
676*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
677*7c478bd9Sstevel@tonic-gate 				"Warning: ncab2clf: "
678*7c478bd9Sstevel@tonic-gate 				"Converted only %d of %d records\n"),
679*7c478bd9Sstevel@tonic-gate 				n_recs, s->n_log_recs);
680*7c478bd9Sstevel@tonic-gate 	}
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate 	return (0);
683*7c478bd9Sstevel@tonic-gate }
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate static int
b2clf(int ifd,int ofd)686*7c478bd9Sstevel@tonic-gate b2clf(int ifd, int ofd)
687*7c478bd9Sstevel@tonic-gate {
688*7c478bd9Sstevel@tonic-gate 	char	*ibuf;
689*7c478bd9Sstevel@tonic-gate 	char	*obuf;
690*7c478bd9Sstevel@tonic-gate 	bool	error_seen;
691*7c478bd9Sstevel@tonic-gate 	bool	eof_seen;
692*7c478bd9Sstevel@tonic-gate 	ssize_t	num_iterations, ni, nh, no, olen;
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 	nca_log_buf_hdr_t	*h;
695*7c478bd9Sstevel@tonic-gate 	nca_log_stat_t		*s;
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 	ibuf = xmalloc(g_infile_blk_size);
698*7c478bd9Sstevel@tonic-gate 	obuf = xmalloc(OUTFILE_BUF_SIZE);
699*7c478bd9Sstevel@tonic-gate 	error_seen = false;
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate 	eof_seen = false;
702*7c478bd9Sstevel@tonic-gate 	num_iterations = 0;
703*7c478bd9Sstevel@tonic-gate 	while (! eof_seen && g_out_records != 0) {
704*7c478bd9Sstevel@tonic-gate 		++num_iterations;
705*7c478bd9Sstevel@tonic-gate 
706*7c478bd9Sstevel@tonic-gate 		nh = ni = no = 0;
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 		/* read the binary header first */
709*7c478bd9Sstevel@tonic-gate 		nh = read_n_bytes(ifd, ibuf, sizeof (nca_log_buf_hdr_t));
710*7c478bd9Sstevel@tonic-gate 		if (nh != sizeof (nca_log_buf_hdr_t)) {
711*7c478bd9Sstevel@tonic-gate 			eof_seen = true;
712*7c478bd9Sstevel@tonic-gate 			break;
713*7c478bd9Sstevel@tonic-gate 		}
714*7c478bd9Sstevel@tonic-gate 
715*7c478bd9Sstevel@tonic-gate 		if (! is_valid_header(ibuf)) {
716*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
717*7c478bd9Sstevel@tonic-gate 			    "Error: ncab2clf: "
718*7c478bd9Sstevel@tonic-gate 			    "Can't convert the input data to CLF\n"));
719*7c478bd9Sstevel@tonic-gate 			continue;
720*7c478bd9Sstevel@tonic-gate 		}
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 		/* read the data to be converted */
723*7c478bd9Sstevel@tonic-gate 		/* LINTED */
724*7c478bd9Sstevel@tonic-gate 		h = (nca_log_buf_hdr_t *)ibuf;
725*7c478bd9Sstevel@tonic-gate 		s = &(h->nca_logstats);
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate 		if (s->n_log_size == 0)
728*7c478bd9Sstevel@tonic-gate 			continue;
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 		ni = read_n_bytes(ifd, &(ibuf[nh]), (ssize_t)s->n_log_size);
731*7c478bd9Sstevel@tonic-gate 		if (ni < 0) {
732*7c478bd9Sstevel@tonic-gate 			error_seen = true;
733*7c478bd9Sstevel@tonic-gate 			break;
734*7c478bd9Sstevel@tonic-gate 		} else if (ni < (ssize_t)s->n_log_size) {
735*7c478bd9Sstevel@tonic-gate 			eof_seen = true;
736*7c478bd9Sstevel@tonic-gate 		}
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate 		if (ni == 0)
739*7c478bd9Sstevel@tonic-gate 			break;
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate 		/* convert binary input into text output */
742*7c478bd9Sstevel@tonic-gate 
743*7c478bd9Sstevel@tonic-gate 		if (b2clf_buf(ibuf, obuf, ni + nh, OUTFILE_BUF_SIZE, &olen)) {
744*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
745*7c478bd9Sstevel@tonic-gate 			    "Error: ncab2clf: "
746*7c478bd9Sstevel@tonic-gate 			    "Can't convert the input data to CLF\n"));
747*7c478bd9Sstevel@tonic-gate 			error_seen = true;
748*7c478bd9Sstevel@tonic-gate 			break;
749*7c478bd9Sstevel@tonic-gate 		}
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate 		/* write out the text data */
752*7c478bd9Sstevel@tonic-gate 		no = write_n_bytes(ofd, obuf, olen);
753*7c478bd9Sstevel@tonic-gate 		if (no != olen) {
754*7c478bd9Sstevel@tonic-gate 			error_seen = true;
755*7c478bd9Sstevel@tonic-gate 			break;
756*7c478bd9Sstevel@tonic-gate 		}
757*7c478bd9Sstevel@tonic-gate 
758*7c478bd9Sstevel@tonic-gate 		bzero(ibuf, nh + ni);
759*7c478bd9Sstevel@tonic-gate 		bzero(obuf, no);
760*7c478bd9Sstevel@tonic-gate 	}
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	free(ibuf);
763*7c478bd9Sstevel@tonic-gate 	free(obuf);
764*7c478bd9Sstevel@tonic-gate 
765*7c478bd9Sstevel@tonic-gate 	if (error_seen)
766*7c478bd9Sstevel@tonic-gate 		return (-1);
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate 	return (0);
769*7c478bd9Sstevel@tonic-gate }
770*7c478bd9Sstevel@tonic-gate 
771*7c478bd9Sstevel@tonic-gate 
772*7c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)773*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
774*7c478bd9Sstevel@tonic-gate {
775*7c478bd9Sstevel@tonic-gate 	int	c;
776*7c478bd9Sstevel@tonic-gate 	int	ifd;		/* input fd - binary log file */
777*7c478bd9Sstevel@tonic-gate 	int	ofd;
778*7c478bd9Sstevel@tonic-gate 	struct tm t;
779*7c478bd9Sstevel@tonic-gate 
780*7c478bd9Sstevel@tonic-gate 	char	*infile = NULL;  /* input file name */
781*7c478bd9Sstevel@tonic-gate 	char	*outfile = NULL; /* output file name */
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 	char	monstr[64];
784*7c478bd9Sstevel@tonic-gate 
785*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
788*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
789*7c478bd9Sstevel@tonic-gate #endif
790*7c478bd9Sstevel@tonic-gate 
791*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
792*7c478bd9Sstevel@tonic-gate 
793*7c478bd9Sstevel@tonic-gate 	/* parse any arguments */
794*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hvDi:o:b:n:s:")) != EOF) {
795*7c478bd9Sstevel@tonic-gate 		switch (c) {
796*7c478bd9Sstevel@tonic-gate 		case 'h':
797*7c478bd9Sstevel@tonic-gate 			usage();
798*7c478bd9Sstevel@tonic-gate 			break;
799*7c478bd9Sstevel@tonic-gate 		case 'i':
800*7c478bd9Sstevel@tonic-gate 			infile = xstrdup(optarg);
801*7c478bd9Sstevel@tonic-gate 			break;
802*7c478bd9Sstevel@tonic-gate 		case 'D':
803*7c478bd9Sstevel@tonic-gate 			g_enable_directio = false;
804*7c478bd9Sstevel@tonic-gate 			break;
805*7c478bd9Sstevel@tonic-gate 		case 'o':
806*7c478bd9Sstevel@tonic-gate 			outfile = xstrdup(optarg);
807*7c478bd9Sstevel@tonic-gate 			break;
808*7c478bd9Sstevel@tonic-gate 		case 'b':
809*7c478bd9Sstevel@tonic-gate 			g_infile_blk_size = (KILO_BYTE * atoi(optarg));
810*7c478bd9Sstevel@tonic-gate 			break;
811*7c478bd9Sstevel@tonic-gate 		case 'n':
812*7c478bd9Sstevel@tonic-gate 			g_out_records = atoi(optarg);
813*7c478bd9Sstevel@tonic-gate 			break;
814*7c478bd9Sstevel@tonic-gate 		case 's':
815*7c478bd9Sstevel@tonic-gate 			g_start_time_str = strdup(optarg);
816*7c478bd9Sstevel@tonic-gate 			bzero(&t, sizeof (t));
817*7c478bd9Sstevel@tonic-gate 			if (sscanf(optarg, "%d/%3s/%d:%d:%d:%d", &t.tm_mday,
818*7c478bd9Sstevel@tonic-gate 			    &monstr[0], &t.tm_year, &t.tm_hour, &t.tm_min,
819*7c478bd9Sstevel@tonic-gate 			    &t.tm_sec) == 6) {
820*7c478bd9Sstevel@tonic-gate 				/* Valid CLF time (e.g. 06/Apr/2001:09:14:14) */
821*7c478bd9Sstevel@tonic-gate 				t.tm_mon = 0;
822*7c478bd9Sstevel@tonic-gate 				do {
823*7c478bd9Sstevel@tonic-gate 					if (strcasecmp(monstr,
824*7c478bd9Sstevel@tonic-gate 					    sMonthStr[t.tm_mon]) == 0)
825*7c478bd9Sstevel@tonic-gate 						break;
826*7c478bd9Sstevel@tonic-gate 				} while (t.tm_mon++ < 12);
827*7c478bd9Sstevel@tonic-gate 				t.tm_year -= 1900;
828*7c478bd9Sstevel@tonic-gate 				g_start_time = t;
829*7c478bd9Sstevel@tonic-gate 			} else if (parse_time(optarg, &t)) {
830*7c478bd9Sstevel@tonic-gate 				g_start_time = t;
831*7c478bd9Sstevel@tonic-gate 			} else {
832*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
833*7c478bd9Sstevel@tonic-gate 				    gettext("Error: ncab2clf:"
834*7c478bd9Sstevel@tonic-gate 				    " %s: unrecognized date/time.\n"),
835*7c478bd9Sstevel@tonic-gate 				    optarg);
836*7c478bd9Sstevel@tonic-gate 			}
837*7c478bd9Sstevel@tonic-gate 			break;
838*7c478bd9Sstevel@tonic-gate 		case 'v':
839*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Error: ncab2clf: "
840*7c478bd9Sstevel@tonic-gate 			    "verbose functionality not yet supported\n"));
841*7c478bd9Sstevel@tonic-gate 			exit(3);
842*7c478bd9Sstevel@tonic-gate 			break;
843*7c478bd9Sstevel@tonic-gate 		case '?':
844*7c478bd9Sstevel@tonic-gate 			usage();
845*7c478bd9Sstevel@tonic-gate 			break;
846*7c478bd9Sstevel@tonic-gate 		}
847*7c478bd9Sstevel@tonic-gate 	}
848*7c478bd9Sstevel@tonic-gate 
849*7c478bd9Sstevel@tonic-gate 	/* set up the input stream */
850*7c478bd9Sstevel@tonic-gate 
851*7c478bd9Sstevel@tonic-gate 	if (infile) {
852*7c478bd9Sstevel@tonic-gate 
853*7c478bd9Sstevel@tonic-gate 		if ((ifd = open(infile, O_RDONLY)) < 0) {
854*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
855*7c478bd9Sstevel@tonic-gate 				gettext("Error: ncab2clf: "
856*7c478bd9Sstevel@tonic-gate 				"Failure to open binary log file %s: %s\n"),
857*7c478bd9Sstevel@tonic-gate 				infile, strerror(errno));
858*7c478bd9Sstevel@tonic-gate 			exit(1);
859*7c478bd9Sstevel@tonic-gate 		}
860*7c478bd9Sstevel@tonic-gate 
861*7c478bd9Sstevel@tonic-gate 	} else {
862*7c478bd9Sstevel@tonic-gate 		ifd = STDIN_FILENO;
863*7c478bd9Sstevel@tonic-gate 	}
864*7c478bd9Sstevel@tonic-gate 
865*7c478bd9Sstevel@tonic-gate 	/* set up the output stream */
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate 	if (outfile) {
868*7c478bd9Sstevel@tonic-gate 
869*7c478bd9Sstevel@tonic-gate 		if ((ofd = open(outfile, O_WRONLY|O_CREAT, 0644)) < 0) {
870*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
871*7c478bd9Sstevel@tonic-gate 			    "Error: ncab2clf: "
872*7c478bd9Sstevel@tonic-gate 			    "Failure to open output file %s: %s\n"),
873*7c478bd9Sstevel@tonic-gate 			    outfile, strerror(errno));
874*7c478bd9Sstevel@tonic-gate 			exit(1);
875*7c478bd9Sstevel@tonic-gate 		}
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate 		/* Enable directio on output stream if specified */
878*7c478bd9Sstevel@tonic-gate 
879*7c478bd9Sstevel@tonic-gate 		if (g_enable_directio)
880*7c478bd9Sstevel@tonic-gate 			(void) directio(ofd, DIRECTIO_ON);
881*7c478bd9Sstevel@tonic-gate 
882*7c478bd9Sstevel@tonic-gate 	} else {
883*7c478bd9Sstevel@tonic-gate 		ofd = STDOUT_FILENO;
884*7c478bd9Sstevel@tonic-gate 	}
885*7c478bd9Sstevel@tonic-gate 
886*7c478bd9Sstevel@tonic-gate 	if ((b2clf(ifd, ofd) != 0)) {
887*7c478bd9Sstevel@tonic-gate 		close_files(ifd, ofd);
888*7c478bd9Sstevel@tonic-gate 		exit(2);
889*7c478bd9Sstevel@tonic-gate 	}
890*7c478bd9Sstevel@tonic-gate 
891*7c478bd9Sstevel@tonic-gate 	close_files(ifd, ofd);
892*7c478bd9Sstevel@tonic-gate 
893*7c478bd9Sstevel@tonic-gate 	if (g_invalid_count) {
894*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning: ncab2clf: %d"
895*7c478bd9Sstevel@tonic-gate 		" number of invalid log records encountered in binary input"
896*7c478bd9Sstevel@tonic-gate 		" file were skipped\n"), g_invalid_count);
897*7c478bd9Sstevel@tonic-gate 	}
898*7c478bd9Sstevel@tonic-gate 	if (g_skip_count) {
899*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning: ncab2clf:"
900*7c478bd9Sstevel@tonic-gate 		    " %d log records in binary input file before %s"
901*7c478bd9Sstevel@tonic-gate 		    " were skipped\n"),
902*7c478bd9Sstevel@tonic-gate 		    g_skip_count, g_start_time_str);
903*7c478bd9Sstevel@tonic-gate 	}
904*7c478bd9Sstevel@tonic-gate 
905*7c478bd9Sstevel@tonic-gate 	return (0);
906*7c478bd9Sstevel@tonic-gate }
907