/**************************************/
/* This is the checkfiles module for  */
/* the lsatmain.c program. It checks  */
/* that /tmp and /var/tmp have the    */
/* sticky bit set. 		      */
/*                                    */
/* Future tests on dirs should be put */
/* in this file. I am working on it   */
/**************************************/

#include <stdio.h> /* fprintf,fgets,fputs,fopen,fclose,perror,sscanf */
#include <stdlib.h> /* the std stuff */
#include <string.h> /* strtok */

int checkfiles(filename, verbose)
const char *const filename;
int verbose;
{
	/* some local vars...		*/
	FILE *infile, *outfile;
	char line[1024];
	char *ptr;
	int count = 0;
	int Linecount = 0;
	int found = 0;
	char tempstring[40];


	if (verbose == 1)
	{
		printf("In checkfiles module...\n");
	}

	/* make a list of the files */
	if ((system("ls -l / /var 1>/dev/null 2>/dev/null |grep tmp 1>/dev/null 2>/dev/null >/tmp/templist.txt")) < 0)
	{
		/* failure */
		perror("Error performing ls?\n");
		return(-1);
	}


	/* open input file for write... */
	infile = fopen("/tmp/templist.txt", "r");
	if (infile == NULL)
	{
		/* rhut-rho */
		perror("Can't open the file we just made!?!\n");
		perror("Giving up, check perms or something...\n");
		return(-1);
	}

	/* oopen output file for write, in cwd...						*/
	outfile = fopen(filename, "a");
	if (outfile == NULL)
	{
		/* rhut-rho */
		perror("Can't open output file!?!\n");
		perror("Giving up, check perms or something...\n");
		return(-1);
	}

	/* ok, first print out some header info to the file */
	fprintf(outfile, "****************************************\n");
	fprintf(outfile, "Check for non-sticky /tmp or /var/tmp:\n");
	

	if (verbose == 1)
	{
		printf("Checking for sticky bits...\n");
	}

	/* get a line from the file... */
	while (fgets(line, sizeof(line), infile) != NULL)
	{
		ptr = strtok(line, ", \n");
		while (ptr != NULL)
		{
			if (sscanf(ptr, "%s", &tempstring) < 1)
			{
				perror("sccanf bit the dust");
				return(-1);
			}
			if ((count == 0) && (Linecount == 0))
			{
				/* 1st line in file...  */
				/* note this is ls of / |grep tmp */
				if ((tempstring[9] != 't') || (tempstring[9] != 'T'))
				{
					/* sticky bit NOT set ! */
					found = 1;
					fprintf(outfile, "Sticky bit not set on /tmp.\n");
					fprintf(outfile, "Please set sticky bit on /tmp.\n");
				}
			}
			if ((count ==  0) && (Linecount == 1))
			{
				/* 2nd line in file */
				/* this is ls of /var |grep tmp */
				if ((tempstring[9] != 't') || (tempstring[9] != 'T'))
				{
					/* sticky bit NOT set! */
					found = 1;
					fprintf(outfile, "Sticky bit not set on /var/tmp.\n");
					fprintf(outfile, "Please set sticky bit on /var/tmp.\n");
				}
			}
				
			/* grab another token */
			ptr = strtok(NULL, ", \"\n");
			if (ptr == NULL)
			{
				;
			}
			/* inc counter...*/
			count++;
		} /* end of while(ptr != NULL), reset the counter */
		count=0;
		Linecount++;
	} /* end of while(fgets...) */

	/* print footer */
	if (found == 0)
	{
		/* no problems... */
		fprintf(outfile, "Sticky bits already set. Good.\n");
	}
	fprintf(outfile, "***************************************\n");


	/* clean up		*/
	fclose(outfile);
	fclose(infile);

	if (verbose == 1)
	{
		printf("Finished in checkfiles.\n");
	}

	return(0);
}

