/****************************************/
/* This is the checkwrite.c module for 	*/
/* lsat program. This module checks for */
/* world writable files and directories */
/* and prints them to the outfile.      */
/*					*/
/* NOTE: this will miss some files if   */
/* run as a non-root user. Not sure if  */
/* you should run as root. :)		*/
/*					*/
/* See lsatmain.c for more details.	*/
/****************************************/

#include <stdio.h>
#include <stdlib.h>

checkwrite(filename, verbose)
const char *const filename;
int verbose;
{

	char line[80];  /* for reading lines		*/
	FILE *outfile; 	/* file pointer for outfile	*/
	FILE *fileptr;	/* file pointer for infile	*/

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

	/* 1st off generate a list of world writeables  */
	/* this should be done from /, but use -xdev so	*/
	/* that we don't traverse other filesystems	*/			
	/* note, we use -mount for compatibility with 	*/
	/* other versions of find.			*/
	
	if (verbose == 1)
	{
		printf("Creating list of world writable files...\n");
		printf("Note: we are not traversing \"other\" filesystems.\n");
	}

	if (system("find / -mount -type f \\( -perm -2 -o -perm -20 \\) 1>/dev/null 2>/dev/null >/tmp/findlist.txt") == 2)
	{
		perror("Problem writing file to tmp.\n");
		perror("Problem was in module: checkset.c\n");
		return(-1);
	}

	/* now simply read through them and print them 	*/
	/* out to the outfile with a header.		*/

	/* open the outfile for append			*/
	outfile = fopen(filename, "a");
	if (outfile == NULL)
	{
		perror("Error opening outfile!\n");
		perror("Problem was in module: checkset.c\n");
		return(-1);
	}

	/* open the file list of world writable files for read	*/
	fileptr = fopen("/tmp/findlist.txt", "r");
	if (fileptr == NULL)
	{
		perror("Error opening file for read!\n");
		perror("Problem was in module: checkset.c\n");
		return(-1);
	}
	
	/* write out a header to the outfile		*/
	fprintf(outfile, "****************************************\n");
	fprintf(outfile, "This is a list of world WRITABLE files\n");
	fprintf(outfile, "on the system. It is highly advisable to\n");
	fprintf(outfile, "go through this list and see what should\n");
	fprintf(outfile, "NOT be world wirtable.\n");
	fprintf(outfile, "**********\n");
	fprintf(outfile, "\nWorld WRITABLE files found:\n");

	/* ok, now just print them out...		*/
	while (fgets(line, sizeof(line), fileptr) != NULL)
	{
		fprintf(outfile, "%s\n", line);
	}
	
	/* close the input file and rm it		*/
	fclose(fileptr);
	if ((system("rm -f /tmp/findlist.txt")) < 0)
	{
		/* rhut-rho... problems.. */
		perror("Can't rm tempfile...\n");
		perror("Permissions problem??\n");
		return(-1);
	}

	if (verbose == 1)
	{
		printf("Creating list of world writable directories...\n");
		printf("Note: we are not traversing \"other\" filesystems.\n");
	}

	/* get a list of world WRITABLE directories 	*/
	if (system("find / -mount -type d \\( -perm -2 -o -perm -20 \\) 1>/dev/null 2>/dev/null >/tmp/findlist.txt") == 2)
        {
                perror("Problem writing file to tmp.\n");
                perror("Problem was in module: checkset.c\n");
                return(-1);
        }

	 /* open the file with list of world writable dirs for read     */
        fileptr = fopen("/tmp/findlist.txt", "r");
        if (fileptr == NULL)
        {
                perror("Error opening file for read!\n");
                perror("Problem was in module: checkset.c\n");
                return(-1);
        }

	/* print a small header				*/
	fprintf(outfile, "\n**********\n");
	fprintf(outfile, "World WRITABLE directories found:\n");

	/* ok, now just print them out...               */
        while (fgets(line, sizeof(line), fileptr) != NULL)
        {
                fprintf(outfile, "%s\n", line);
        }

        /* close the input file and rm it               */
        fclose(fileptr);
        if ((system("rm -f /tmp/findlist.txt")) < 0)
        {
                /* rhut-rho... problems.. */
                perror("Can't rm tempfile...\n");
                perror("Permissions problem??\n");
                return(-1);
        }
	 
	/* print out a footer... */
	fprintf(outfile, "\n");
	fprintf(outfile, "****************************************\n");
	
	/* close up */
	fclose(outfile);
	
	if (verbose == 1)
	{
		printf("Finished in checkwrite module.\n");
	}

	return(0);
}		
