/* This is the checkumask module for    */
/* the lsatmain.c program. It checks    */
/* It checks /etc/csh.login and profile */
/* for the umask setting. It makes sure */
/* that is it set at 022, 027 or 077.	*/
/*					*/
/* see lsatmain.c for more details	*/
/****************************************/

#include "lsatheader.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

int checkumask(filename, verbose, html)
const char *const filename;
int verbose;
int html;
{

    char *umaskptr;
    char uline[80];	/* array for a line	*/
    char utemparray[20][80]; /*temparray. */
    int i=0;		/* counter variable	*/
    int j=0;		/* counter variable  */
    int numread;

    FILE *outfile;	/* output file pointer 	*/
    FILE *umaskfile;	/* file pointer to templist file */

    const char * tempfile;
    const char * shellcode;
    const char * header;

    /* init array to zero */
    for (i=0; i<20; i++)
    {
        for (j=0; j<80; j++)
        {
            utemparray[i][j]=0;
        }
    }

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

    if (verbose > 0)
    {
        printf(" Checking umask file...\n");
    }

    // dummy call, just write the header
    tempfile="/tmp/lsat1.lsat";
    shellcode="";
    header="Checking default umask on system:\n\nDefault umask should be 022, 027\nor 077. 002 is ok for RedHat.\nCheck these for compliance.\n";
    if ((dostuff(tempfile, filename, shellcode, header, html)) < 0)
    {
	/* something went wrong */
	perror(" Could not write to outfile.");
	return(-1);
    }    

    // create the tempfile and call shellcode
    tempfile ="/tmp/lsat1.lsat";
    shellcode="grep umask /etc/* 2>/dev/null |grep -v directory 2>/dev/null |grep -v if 2>/dev/null |awk -F\"umask\" 'length($1 $2) > 0 {print($1 $2)}' |grep -v \\#  >/tmp/lsat1.lsat";
    if ((dostuff(tempfile, NULL, shellcode, NULL, html)) < 0)
    {
	/* something went wrong */
	perror(" Creation of file list failed.");
	return(-1);
    }

    outfile=fopen(filename, "a");
    if (outfile == NULL)
    {
        perror(" Can't open outfile!\n");
        perror(" Check perms or something...\n");
        return(-1);
    }

    /* NOTE: Redhat systems use User Private*/
    /* Groups and a umask of 002 is ok.     */
    /* If we see 002, check for RedHat...   */

    /* had a problem with this... this 	*/
    /* should be ok on redhat & debian...   */

    /* open the newly created file */
    umaskfile=fopen("/tmp/lsat1.lsat", "r");
    if (umaskfile == NULL)
    {
        perror(" Can't open templist...\n");
        perror(" Strange, we just created that file...\n");
        perror(" Giving up in checkumask...\n");
        return(-1);
    }

    /* due to the format of the files we grep through */
    /* (note that only csh.login and profile should   */
    /* have umask), there should be only 3 fields     */
    /* per line in the lsat1.lsat file.		      */
    /* we therefore know that for each one we will 	  */
    /* have 3 entries in the temparray, the file,     */
    /* "umask" and the umask number.		  */

    /* reset counter */
    i = 0;
    numread = 0;
    while (fgets(uline, sizeof(uline), umaskfile) != NULL)
    {
        numread++;
        umaskptr = strtok(uline, ", \n");
        while (umaskptr != NULL)
        {
            if (sscanf(umaskptr, "%79s", utemparray[i]) < 1)
            {
                perror(" sscanf failed.\n");
                perror(" Failure was in module: checkumask.\n");
                return(-1);
            }
            i++;
	    if (i>20)
	    {
		perror(" Error in checkumask: Too much data.\n");
		return(-1);
	    }
            umaskptr = strtok(NULL, ", \n");
            if (umaskptr == NULL)
            {
                ;
            }
        } /* end while (passptr != NULL) */
    } /* end while (fgets...) */

    /* just print out the umask & filename  */
    /* for the user...			*/
    for (i = 0; i < numread; i++)
    {
	if (2*i+1 > 20)
	{
	perror (" Error in checkumask: Too much data.\n");
	return(-1);	
	}
        /* matched "good" masks*/
        fprintf(outfile, "file %s, umask %s\n", utemparray[i*2], utemparray[i+i+1]);
    } /* end for (i=0...) */




    // clean up
    fclose(outfile);
    fclose(umaskfile);

    if (system("rm -f /tmp/lsat1.lsat") < 0)
    {
        perror(" Can't rm tempfile??\n");
        perror(" Strange, we made the file...\n");
        perror(" Giving up in checkumask...\n");
        return(-1);
    }

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