/************************************************/
/* This checks inetd.conf for baddies...	*/
/* This function checks inetd.conf or xinetd.d  */
/* for unwanted services. It outputs the list   */
/* of all servies running (all are unwanted, 	*/
/* right?) to the outfile.			*/
/* If inetd.conf exists or if xinetd.d is 	*/
/* present it also returns a 1 to main to tell  */
/* main to run a check of /etc/hosts.deny.	*/
/*						*/
/* See lsatmain.c for more details...		*/
/************************************************/

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

int checkinetd(release, filename)
char release[50];
const char *const filename;
{

	char *ptr;
	char line[1024];        /* temp line for reading   */
	char tempstring[50];    /* tempstring for inet     */
	int linecount, field;   /* some vars...		   */
	int emptyflag=0;	/* is inet an empty file?  */
	int inetexists=0;	/* does inet exist?	   */
	FILE *infile;  		/* tempfile file pointer   */
	FILE *outfile; 		/* output file pointer     */
	FILE *inetfile;    		/* file pointer for checks */

	/* open the output file for append. */
	outfile = fopen(filename, "a");
	if (outfile == NULL)
	{
		perror("Error opening outfile.\n");
		perror("Check perms perhaps?\n");
		return(-1);
	}
	

	/* silly me... FIRST check if we are on redhat 5/6 */
	/* Then check to see if inetd exists if we are on  */
	/* redhat 5.x, 6.x...				   */

	
	/* check the release we are on */
	if ((release[0]=='6') || (release[0]=='5'))	
	{
		
		/* check to see if inetd.conf exists at all    */
		if ((inetfile=fopen("/etc/inetd.conf", "r"))!=NULL)
		{
			inetexists=1;
		}
		else
		{
		/* it does not exist */
			inetexists=0;
		}
		fclose(inetfile);
		if (inetexists == 1)
		{

			/* print some header info... */
			fprintf(outfile, "****************************************\n");
        		fprintf(outfile, "inetd.conf items not commented out:\n\n");      
			/* note: grep returns
			*  0 on match
			*  1 on no match
			*  2 on system/file/read/other failure
			*  HOWEVER, -v inverts the sense of 0,1.
			*/
			if (system("grep -v \\# /etc/inetd.conf >> /tmp/greplist.txt") == 2)
			{
				perror("Problem executing grep in checkinetd\n");
				return(-1);
			}
			/* now instead of reading the whole file and 	*/
			/* say looking for baddie servies, I consider	*/
			/* that all in there are evil. Merely tell the  */
			/* user what we have found that is open.        */
			/* perhaps also tell them to close them. :)     */

			/* note that if the file is empty, thats great. */
			/* it _should_ mean that they have commented    */
			/* out everything in /etc/inetd.conf		*/

			infile = fopen("/tmp/greplist.txt", "r");
			if (infile == NULL)
			{
				perror("Error opening tmp file.\n");
				return(-1);
			}
			while (fgets(line, sizeof(line), infile) != NULL)
			{
				field=0;
				ptr = strtok(line, ", \"\n");
				if (ptr == NULL)
				{
					if (linecount == 0)
					{
						/* 1st line was empty, must have been */
						/* an empty file. This _should_ be a  */
						/* "good thing"....		      */
						/* set flag, then break.	      */
						emptyflag = 1;
						break;
					}
					/* otherwise just break... */
					break;
				}
				linecount++;
				while (ptr != NULL)
				{
					if (sscanf(ptr, "%s", &tempstring) < 1)
					{
						perror("sccanf bit the dust");
						return(-1);
					}
					/* note: we just need the 1st field... */
					if (field == 0)
					{
						fprintf(outfile, "%s \n", tempstring);
					}
					ptr = strtok(NULL, ", \"\n");
					if (ptr == NULL)
					{	
						/* freak out */
						break;
					}
					field++;
				}					
			} /* end while(fgets...) */	
		} /* end if(inetexists == 1) */
		else
		{
			/* inetd.conf did not exist */
			inetexists=0;
		}
		/* clean up... */
		fclose(infile);
	}
	if (release[0]=='7')
	{
		if (system("ls -l /etc/xinetd.d >> /tmp/xinetdlist.txt") < 0)
		{
			perror("Problem executing ls??\n");
			return(-1);
		}
		
	}
	
	/* print some footer information */
	if (emptyflag == 1)
	{
		/* there were no non-commented inetd.conf lines */
		fprintf(outfile, "\nNo non-essential items found in inetd.conf. Good.\n");
		fprintf(outfile, "****************************************\n");
	}
	if (emptyflag == 0)
	{
		/* there were services in inetd.conf left open */
		fprintf(outfile, "\nIt is strongly recommended that you comment out\n");
		fprintf(outfile, "all of the above from your /etc/inetd.conf file.\n");
		fprintf(outfile, "At the very least run tcpwrappers and edit your \n");
		fprintf(outfile, "/etc/hosts.allow and /etc/hosts.deny files.\n");
		fprintf(outfile, "\n");
		fprintf(outfile, "****************************************\n");
	}
	/* clean up... */
	if ((system("rm -f /tmp/greplist.txt")) < 0)
	{
		/* rhut-rho... problems.. */
		perror("Can't rm tempfile...\n");
		perror("Permissions problem??\n");
		return(-1);
	}
	fclose(outfile);

	/* instead of just returning 0 here */
	/* we return inetextists... 	    */
	/* 0 for no existance, 1 if exists  */
	return(inetexists);
}
