BASH (LINUX)-HistoryConv.c

/*
 * File Name: HistoryConv.c
 * File Creation Date: 19980104
 * Copyright 1998 by OZUS LLC       Auth0r: Lance Lenz
 * Copyright 2009 by TriParadigm LLC
 * Copyright 2009 by MtXia Inc.     Author: Dana French
 * Copyright 2009 by Edgetec Inc.   Author: Joseph Stephens
 * 
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby Restricted,
 * the above copyright notice must appear in all copies and that both the
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the names of 
 * OZUS LLC, TriParadigm LLC, MtXia Inc., Edgetec Inc.
 * not be used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  
 *  makes no representations about the OZUS LLC, TriParadigm LLC, 
 *  MtXia Inc., Edgetec Inc. suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 * 
 * Users Restricted Rights - Use, duplication or disclosure restricted
 * Restricted License: 2000      Company Name Here
 * 
 * Update Log:
 * 20091231: Lance Lenz:
 *
 * Compile cc user.c -lpq
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define NUL '\x00'	// Start of Line
#define SOH '\x01'	// Start of Record
#define DE '\xde'	// Start of Time
#define DF '\xdf'	// End of Time
#define LF '\x0a'	// Line Feed  
#define CR '\x0d'	// Line Feed  
#define MAXDATA 4096


// Global Counts
int usCnt = 0, secCnt = 0;
int ServerNumber = 0;

// Time Conversion
convertTime(char *timeEpoch, char *rtnBuf)
{
	struct tm *t;
	time_t seconds;
	sscanf (timeEpoch, "%ld", &seconds);
	t = localtime(&seconds);
	sprintf(rtnBuf,"%4d-%02d-%02d %02d:%02d:%02d"
		, t->tm_year+1900
		, t->tm_mon+1
		, t->tm_mday
		, t->tm_hour
		, t->tm_min
		, t->tm_sec
	);
}

int main(int argc, char * *argv)
{
	FILE *in_file, *out_file;  /* input data file            */
	//struct tm *t;
	//time_t seconds;
	int i = 0;
	int vl_1 = 0;             /* Used in for loop read in from OCC         */
	int vl_2 = 0;             /* Used in for loop read in from OCC         */
	int c;                    /* Used as a char to read in from OCC        */
	// Epoch is a sliding window. Once secondEpochEnd is set copy out the data 
	// for the firstEpoch and commandText then change it to the firstEpoch
	// and look for the secondEpoch again.
	// #1234567890;text;#1234567890;
	int firstEpochStart = 0;
	int firstEpochEnd = 0;
	int secondEpochStart = 0;
	int secondEpochEnd = 0;

	char buf1[MAXDATA];
	char hold_buf[MAXDATA];
	char commandText[MAXDATA];
	char commandTextBuf[MAXDATA];
	char epoch[20];
	char convertedEpoch[20];


	if (argc != 2)
	{
		fprintf(stderr,"Usage: HistoryConv FileName\n");
		fprintf(stderr,"Usage: HistoryConv /OZUS/History/odcapmdm00-bmcaddm-sh_history.20120415.015741.bmcaddm.bmcaddm.146.61.182.59.pts-3\n");
		fprintf(stderr,"Usage: HistoryConv /root/.bash_history\n");
		exit(1);
	}

	/* Open the password file and read into x array the user names */
	//strcpy(buf1,"/home/infosec/OZUS_Infosec/TestText");
	//strcat(buf1,"/odcapmdm00-bmcaddm-sh_history.20120415.015741.bmcaddm.bmcaddm.146.61.182.59.pts-3");

	memset(&buf1,'\0', sizeof(buf1));
	strcat(buf1,argv[1]);
	if (!(in_file = fopen(buf1, "r"))) {
		printf("error opening input file; %s\n", buf1);
		exit(1);
	}
	printf("\n<TABLE Border=\"2\" Width=\"100%\" Cellspacing=\"2\" Cellpadding=\"2\"><TR><TD width=\"15%\">******History_File******</TD> <TD width=\"75%\"> %s</TD></TR>\n",buf1);
	/*
 	* Read in char-x-char to look for CTRL and NewLine to determin where the line breaks are
 	* A line ends just after the EPOCH with a 0x0A or something like #df#\n
 	*/
	vl_1 = 0;
	memset(&commandText,'\0', sizeof(commandText));
	memset(&hold_buf,'\0', sizeof(hold_buf));

	// Linux is based on Time on one Line then data on the Next
	// We need to trap from Epoch to Epoch and cut from the first Ephoc to the start of the second Epoch
	// and not loose any of the second Epoch
	while (( c = getc(in_file)) != EOF)  /* read in record */
	{
		// if for some reason we miss a counter reset everyting to keep the buffer from over flowing
		if((vl_1 >= MAXDATA) || (vl_2 >= MAXDATA))
		{
			printf("Error MAXDATA: read exceeded %d chars for vl_1 %d or vl_2 %d.\n File Name: %s\n",MAXDATA, vl_1, vl_2, buf1);
			fclose(in_file); // Close the input file
			exit(2);
		}

		// Load the buffer on char at a time
		hold_buf[vl_1] = c; 
		//printf("VL= %d %s\n",vl_1, hold_buf);

		// Setting the buffer to NULL and the vl_1 counter to -1 for the else statment that does a vl_1++
		// this is a port way to make it Zero after the while loop
		// Conver to lower ascii value on the chart
		//
		// Conver to a different char to test with
		//if(hold_buf[vl_1] == LF)
		//{
		//	 hold_buf[vl_1] = ';'; 
		//}

		// Setting the buffer to NULL and the vl_1 counter to -1 for the else statment that does a vl_1++
		// this is a port way to make it Zero after the while loop
		if(hold_buf[vl_1] == NUL)
		{
			memset(&hold_buf,'\0', sizeof(hold_buf));
			vl_1 = -1;
		}

		// Linux Test for the start location of epoch Linux
		// Test for an Epoch. Should be on the first line read in if not clean up the file
		// #1234567890\n
		// DATA DATA DATA DATA DATA \n
		// #1234567890\n
		// DATA DATA DATA DATA DATA \n
		//
		// Buff loaded from the while(c = getc(....
		// #   EPOCH  \n    SOME DATA         \n#   EPOCH  \n
		// #1234567890;DATA DATA DATA DATA DATA;#1234567890;
		// |          |                        ||          |
		// 0         11                       x 0         11
		//
		//if(((strncmp(&hold_buf[0],"#", 1)) == 0) && ((strncmp(&hold_buf[vl_1],";", 1)) == 0) && (vl_1 < 12))
		if(((strncmp(&hold_buf[0],"#", 1)) == 0) && ((strncmp(&hold_buf[vl_1],"\n", 1)) == 0) && (vl_1 < 12))
		{
			firstEpochStart=vl_1 -11;
			firstEpochEnd= vl_1 ;
		}

		// Linux Test for the end location of epoch and end of the record we want to capture
		//if(((strncmp(&hold_buf[vl_1 -12],";#", 2)) == 0) && ((strncmp(&hold_buf[vl_1],";", 1)) == 0) && (vl_1 > 13))
		if(((strncmp(&hold_buf[vl_1 -12],"\n#", 2)) == 0) && ((strncmp(&hold_buf[vl_1],"\n", 1)) == 0) && (vl_1 > 13))
		{
			memset(&commandText,'\0', sizeof(commandText));
			secondEpochStart = vl_1 - 12;
			secondEpochEnd = vl_1 -1;
			strncpy(epoch,&hold_buf[firstEpochStart +1],firstEpochEnd);
			convertTime(epoch,convertedEpoch);
			strncpy(commandText,&hold_buf[firstEpochEnd + 1],secondEpochStart - firstEpochEnd - 1);

			// Setup the Epoch Window
			strncpy(epoch,&hold_buf[secondEpochStart + 1],secondEpochEnd);

			// This is a good place to put a function call to parce the commandText for chars that cannot go into the database
			// or wrap escape chars around before the insert into the DB
			//
			// Print to HTML
			printf("<TR><TD>%s</TD><TD>%s</TD></TR>\n",convertedEpoch,commandText);

			// Setup for DataBase by wrapping chars before Insert 
			vl_2 = 0;
			memset(&commandTextBuf,'\0', sizeof(commandTextBuf));
			 for(i = 0; i<= sizeof(commandText);i++)
			 {
				commandTextBuf[vl_2] =  commandText[i]; 

				if(commandTextBuf[vl_2] == '\'')
				{
					commandTextBuf[vl_2] = '\\'; 
					vl_2++;
					commandTextBuf[vl_2] = '\''; 
					vl_2++;
				}
				else if(commandTextBuf[vl_2] == '\"')
				{
					commandTextBuf[vl_2] = '\\'; 
					vl_2++;
					commandTextBuf[vl_2] = '\"'; 
					vl_2++;
				}
				else
					vl_2++;
			}
			// Print to HTML
			//printf("<TR><TD>%s</TD><TD>%s</TD></TR>\n",convertedEpoch,commandTextBuf);
			// Insert into the DataBase

			// Reset all the buffers back to zero and keep reading in the next X chars.
			vl_2 = 0;
			memset(&commandText,'\0', sizeof(commandText));
			memset(&hold_buf,'\0', sizeof(hold_buf));
			// Now copy in the new Epoch Time from the window
			strcpy(hold_buf,epoch);
			vl_1 = strlen(epoch);
		}
		else
			vl_1++;
	}

	fclose(in_file); // Close the input file
	printf("</TABLE>\n");
	exit(0);