/*
This is a function that does all the filehandling stuff for the lsat modules so that
you don't have to reinvent the wheel every time you want to do something. After all
we have to get rid off, eradicate and stamp out all kinds of redundancies.

As arguments it takes four pointers to strings: tempfile, outfile, shellcode and
header. Header can be NULL, then it simply will not be printed. One of the other
pointers can be NULL as well. In this case refer to the following list:

no NULL pointer            default behaviour
  create tempfile
  run shellcode
  append header to outfile
  append tempfile to outfile
  delete tempfile

outfile == NULL            if you want to do keep working with tempfile
  create tempfile
  run shellcode

tempfile == NULL           if you already have a tempfile and want to keep it
  run shellcode

shellcode == NULL          if you already got a tempfile but now you're finished
  append header to outfile
  append tempfile to outfile
  delete tempfile
  
Special Case:		   just print the header to the outfile
tempfile==shellcode==NULL
  append header to outfile
  
  
Will return 0 on success and negative value on failure.
*/


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

//int dostuff(const char *const tempfile,const char *const outfile,const char *const shellcode,const char *const header)
int dostuff (tempfile, outfile, shellcode, header)
const char *const tempfile;
const char *const outfile;
const char *const shellcode;
const char *const header;
{
    int  fileval=0;	/* return val */
    char line[80];	/* for reading lines		*/
    FILE *out;		/* file pointer for outfile	*/
    FILE *tmp;		/* file pointer for tempfile	*/


/* create a temp file for storage */
if ((tempfile!=NULL)&&(shellcode!=NULL))
{
    if ((fileval = open(tempfile, O_RDWR | O_CREAT, 0600)) < 0)
    {
        perror("  Could not create temporary file...\n");
        perror("  Possible link attack while creating/opening file!\n");
        exit(-1);
    }
}


/* run the shellcode */
if (shellcode!=NULL)
{
    if ((system(shellcode)) < 0 )

    {
        perror("  Problem running the shellcode or writing to tempfile\n");
	/* sanity check */
        printf("  This was shellcode: %s\n",shellcode);
        return(-1);
    }
}


/* output the header (if given) and append tempfile to outfile and remove tempfile*/
if (outfile!=NULL)//&&(tempfile!=NULL))
{
    // open outfile for append
    out = fopen(outfile, "a");
    if (out == NULL)
    {
        perror("  Error opening outfile!\n");
        return(-1);
    }
    
    // print header to outfile if given
    if (!(header==NULL))
    {
	fprintf(out, "\n****************************************\n");
        fprintf(out, "%s\n", header);
    }

    // do tempfile stuff if given
    if (tempfile!=NULL)
    {
        // open tempfile for reading
        tmp = fopen(tempfile, "r");
        if (tmp == NULL)
        {
            perror("  Error opening tempfile for read!\n");
	    fclose(out);
            return(-1);
        }

        // append everything in tempfile to outfile
        while (fgets(line, sizeof(line), tmp) != NULL)
        {
            fprintf(out, "%s", line);
        }

        // close the tempfile and rm it
        fclose(tmp);
        remove(tempfile);
        }
    
    // close up
    fclose(out);
}


    return(0);
}
