/************************************************/
/* This checks open network ports.		*/
/* This module checks for open network ports on */
/* the system by means of a netstat -an.	*/
/* the output is stripped for LISTEN lines and  */
/* that is checked against /etc/services in     */
/* order to print out a "guess" at what is 	*/
/* running on that port. I could have used nmap */
/* or something else, but I wanted to just have */
/* a quick check without having to install 	*/
/* yet another program (YAP).			*/
/*						*/
/* See lsatmain.c for more details...		*/
/************************************************/

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

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

    char *ptr;            /* char ptr for strtok		*/
    char line[192];      /* temp line for reading		*/
    char tempstring[192];  /* tempstring for entries	*/
    int field;  			 /* some vars...	*/
    int i;					 /* counter	*/
    int fileval=0;		/* return val  	*/
    FILE *infile;  		 /* tempfile file pointer   	*/
    FILE *tmpfile;		/* pointer for tmp_file	*/
    const char * tmp_file = "/tmp/lsat2.lsat";

    const char * tempfile =NULL;
    const char * shellcode=NULL;
    const char * header   =NULL;

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

    if (verbose > 0)
    {
        printf(" Checking for open/running network services...\n");
    }

    tempfile = "/tmp/lsat1.lsat";
    shellcode = "netstat -an |grep LISTEN |grep 0.0.0.0 2>/dev/null >>/tmp/lsat1.lsat";
    if ((dostuff(tempfile, 0, shellcode, 0)) < 0)
    {
 	/* something went wrong */
	perror(" Creation of list failed.");
	return(-1);
    }


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

    /* ok, open the file dostuff just made for read */
    infile = fopen("/tmp/lsat1.lsat", "r");
    if (infile == NULL)
    {
        perror("Error opening tmp file.");
        perror("Error was in module: checknet.");
        return(-1);
    }

    /* open the tmp_file for append */
    tmpfile = fopen(tmp_file, "a");
    if (tmpfile == NULL)
    {
	perror("Error opening tmp_file.");
	perror("Error was in module: checknet.");
	return(-1);
    }

    /* ok, the file should look something like: 			*/
    /* protocol	n	n	<ip>:<port>      <ip>:<port> LISTEN 	*/
    /* so we want the port on the 1st <ip>:<port> pair.		*/
    /* we then want to look that port up in /etc/services    */
    /* and print out that line to the outfile...			*/

    i = 0; /* counter for our array */
    while (fgets(line, sizeof(line), infile) != NULL)
    {
        field=0;
        /* if we say a token is a ' ' or :, then we need to 	*/
        /* grab the 5th field of each line of the file.		*/
        /* note here we will also print out the 1st field,	*/
        /* although AFAIK they will all be tcp.			*/
        ptr = strtok(line, ", :\n");
        /* do not read in past field 5, buffer overflow can	*/
        /* exist, thanks nordi.					*/
        while ((ptr != NULL) && (field < 5))
        {
            if (sscanf(ptr, "%s", tempstring) < 1)
            {
                perror("sscanf bit the dust");
                perror("Dust biting was in checknet.\n");
                return(-1);
            }
            /* get the 1st field...*/
            if (field == 0)
            {
                fprintf(tmpfile, "%s", tempstring);
            }
            /* get the 5th field... */
            if (field == 4)
            {
                fprintf(tmpfile, "\t\t%s\n", tempstring);
            }
            ptr = strtok(NULL, ", :\n");
            if (ptr == NULL)
            {
                /* freak out */
                break;
            }
            field++;
        } /* end while (ptr!=NULL) */
    } /* end while(fgets...) */

    /* close infile  */
    fclose(infile);
    /* rm infile as dostuff is going to read in tmpfile */
    remove(tempfile);
    /* dont forget to close tmpfile or dostuff will crash */
    fclose(tmpfile);

    header = "Check these ports in /etc/services to see what they are.\nClose all ports you do not need.\n\nPorts listening on this system:\nProtocol\tPort\n";
    if ((dostuff(tmp_file, filename, 0, header)) < 0)
    {
	/* something went wrong */
	perror(" Creation of list failed.");
	return(-1);
    }

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