/****************************************/
/* This is the checkwrite.c module for 	*/
/* lsat program. This module checks for */
/* world/group writable files and dirs  */
/* 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>
#include <fcntl.h>

int checkwrite(filename, verbose)
const char *const filename;
int verbose;
{
    const char * tempfile =NULL;
    const char * shellcode=NULL;
    const char * header   =NULL;

    if (verbose >= 0)
    {
    	printf(" Running checkwrite module...\n");
    }

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

    
    /*
    Generate a list of world/group writables from / .
    -mount is used so that we don't traverse other
    filesystems. We use -mount instead of -xdev for
    compatibility with other versions of find.
    */
    tempfile = "/tmp/lsat1.lsat";
    shellcode= "find / -mount -type f \\( -perm -2 -o -perm -20 \\) 2>/dev/null | tee >/tmp/lsat1.lsat";
    header   = "This is a list of world/group writable files\n";
    if ((dostuff(tempfile, filename, shellcode, header)) < 0)
    {
	//something went wrong
	perror(" Creation of list failed.");
	return (-1);
    }

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

    
    /* and now the directories */
    tempfile ="/tmp/lsat1.lsat";
    shellcode="find / -mount -type d \\( -perm -2 -o -perm -20 \\) 2>/dev/null | tee >/tmp/lsat1.lsat";
    header   ="List of group/world writable directories:\n";
    if ((dostuff(tempfile, filename, shellcode, header)) < 0)
    {
	//something went wrong
	perror(" Creation of list failed.");
	return (-1);
    }



    if (verbose > 0)
    {
        printf(" Finished in checkwrite module.\n");
    }

    return(0);
}
