/****************************************/
/* This is the checkdotfiles module for */
/* lsat program. This module checks for */
/* .forward and .exrc files and prints  */
/* them to the outfile.      		*/
/* 4/5/2002 Added checks for .rhost(s)  */
/* and .netrc files.			*/
/*					*/
/* 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>

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

    char line[100];  /* for reading lines		*/
    FILE *outfile; 	/* file pointer for outfile	*/
    FILE *fileptr;	/* file pointer for infile	*/
    int fileval=0;  /* return value    */

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

    /* 1st off generate a list of .exrc files       */
    /* 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 .exrc files...\n");
    }

    /* create a temp file for storage */
    if ((fileval = open("/tmp/findlist.txt", O_RDWR | O_CREAT, 0600)) < 0)
    {
        perror("Could not make file w/perms 0600...\n");
        perror("Possible link attack while creating/opening file!\n");
        perror("Bailing in checkdotfiles...\n");
        exit(-1);
    }

    if (system("find / -mount -name \'.exrc\' 1>/dev/null 2>/dev/null >/tmp/findlist.txt") == 2)
    {
        perror("Problem writing file to tmp.\n");
        perror("Problem was in module: checkdorfiles\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 .exrc files for read	*/
    fileptr = fopen("/tmp/findlist.txt", "r");
    if (fileptr == NULL)
    {
        perror("Error opening file for read!\n");
        perror("Problem was in module: checkdotfiles\n");
        return(-1);
    }

    /* write out a header to the outfile		*/
    fprintf(outfile, "****************************************\n");
    fprintf(outfile, "This is a list of .exrc files on the system.\n");
    fprintf(outfile, "Read through this and see what is needed.\n");
    fprintf(outfile, "**********\n");
    fprintf(outfile, "\n.exrc 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 .forward files...\n");
    }

    fileval = 0;
    /* create a temp file for storage */
    if ((fileval = open("/tmp/finslist.txt", O_RDWR | O_CREAT, 0600)) < 0)
    {
        perror("Could not make file w/perms 0600...\n");
        perror("Possible link attack while creating/opening file!\n");
        perror("Bailing in checkdotfiles...\n");
        exit(-1);
    }


    /* get a list of .forward files on the system 	*/
    if (system("find / -mount -find \'.forward\' 1>/dev/null 2>/dev/null >/tmp/findlist.txt") == 2)
    {
        perror("Problem writing file to tmp.\n");
        perror("Problem was in module: checkdotfiles\n");
        return(-1);
    }

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

    /* print a small header				*/
    fprintf(outfile, "\n**********\n");
    fprintf(outfile, ".forward 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 .rhosts files...\n");
    }

    fileval = 0;
    /* create a temp file for storage */
    if ((fileval = open("/tmp/findlist.txt", O_RDWR | O_CREAT, 0600)) < 0)
    {
        perror("Could not make file w/perms 0600...\n");
        perror("Possible link attack while creating/opening file!\n");
        perror("Bailing in checkdotfiles...\n");
        exit(-1);
    }

    /* get a list of .rhosts files on the system   */
    if (system("find / -mount -name .rhosts 1>/dev/null 2>/dev/null >/tmp/findlist.txt") == 2)
    {
        perror("Problem writing file to tmp.\n");
        perror("Problem was in module: checkdotfiles\n");
        return(-1);
    }

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

    /* print a small header                         */
    fprintf(outfile, "\n**********\n");
    fprintf(outfile, ".rhosts 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 .netrc files...\n");
    }


    fileval = 0;
    /* create a temp file for storage */
    if ((fileval = open("/tmp/findlist.txt", O_RDWR | O_CREAT, 0600)) < 0)
    {
        perror("Could not make file w/perms 0600...\n");
        perror("Possible link attack while creating/opening file!\n");
        perror("Bailing in checkdotfiles...\n");
        exit(-1);
    }

    /* get a list of .netrc files on the system   */
    if (system("find / -mount -name .netrc 1>/dev/null 2>/dev/null >/tmp/findlist.txt") == 2)
    {
        perror("Problem writing file to tmp.\n");
        perror("Problem was in module: checkdotfiles\n");
        return(-1);
    }

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

    /* print a small header                         */
    fprintf(outfile, "\n**********\n");
    fprintf(outfile, ".netrc 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);
    }



    /* print out a footer... */
    fprintf(outfile, "\n");
    fprintf(outfile, "****************************************\n");

    /* close up */
    fclose(outfile);

    if (verbose == 1)
    {
        printf("Finished in checkdotfiles module.\n");
    }
    return(0);
}
