/*
  This is just a little proggie to scan the www server for their
  server type and version.
  
  I cannot and will not be held responsible nor legally bound for the
  malicious activities of individuals who come into possession of this

  [29.10.99] - v2.01 some options for diferent ports and logging
  [03.10.99] - v1.02 some lame segmentation faults fixed :\
  [20.09.99] - v1.01 added the option for mass scan 

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>

#define ERROR -1
#define OK 0
#define MAXDATASIZE 300
#define MESSAGE "HEAD / HTTP/1.0\n\n"
#define DATE "[29.10.99]"
#define VERSION "v2.00"

typedef struct t          // structure of control of options
	{
		int multi;
		int single;
		int log;
		int port;
    int run;
  } t_ctrl;



/* Function prototypes */
char *GetHTTP(int s,char *);           // funtion that scans the host
void usage();                          // usage of program
void log(char str[MAXDATASIZE]);       // logging function
void scanning();                       // starts the scan

/* Global Vars */

t_ctrl control;
char filename[MAXDATASIZE];
char host[MAXDATASIZE];
char *result;
char logfile[MAXDATASIZE];
char name[MAXDATASIZE];

void main(int argc,char *argv[])
{
  char c;

  int i=0;

 /**** Initializing the control for defaults ***/
  strcpy(name,argv[0]);
  control.single=OK;
  control.multi=ERROR;
  control.log=ERROR;
  control.port=80;
  control.run=ERROR;

  if (argc<=1)
    usage();
  
  while ((c=getopt(argc,argv,"s:vf:p:l:"))!= -1)
    {
      switch(c)
        {
          case 'f': strncpy(filename,optarg,MAXDATASIZE);
                    control.single=ERROR;
                    control.multi=OK;
                    control.run=OK;
                    break;
          case 'p': control.port=atoi(optarg);
                    control.run=OK;
                    break;
          case 'l': strncpy(logfile,optarg,MAXDATASIZE);
                    control.log=OK;
                    control.run=OK;
                    break;
          case 's': strncpy(host,optarg,MAXDATASIZE);
                    control.run=OK;
                    break;
          case 'v': usage();
                    break;
          case '?': usage();
                    break;
             
        }
    
    }        
 		if (control.run==OK)
 		  scanning();
 	}
 	
 	
 	
 	
void scanning()
 {
    FILE *in;
    int s;

    if ((control.multi==OK) && (control.single==ERROR) )
      {
        if ((in=fopen(filename, "r+"))==NULL)
          {
            printf("Error: Unable to open IP file\n");
            exit(1);
          };

        while (!feof(in))
        	{
          	memset(host,0,MAXDATASIZE);
          	fscanf(in,"%s",host);
          
            if ((s=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
              {
                printf("ERROR: socket() failed\n");
                exit(0);
              };
            printf("\nServer: %s\n",host);

            result=(char *) GetHTTP(s,host);
            if (control.log==OK)
            	{
            		log(host);
            		log(result);
            	}
            printf("%s",result);
        
          };
        fclose(in);
      }

    if ((control.multi==ERROR) && (control.single==OK))
      {
        if ((s=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
          {
            printf("ERROR: socket() failed\n");
            exit(0);
          };
    
        printf("SERVER: %s\n",host);
        printf("%s\n",GetHTTP(s,host));
        exit(0);
      }
    else
      {
      	usage();
      }
 };


char *GetHTTP(int s,char *host)
{
  char *p;
  int i;    
  int numbytes;
  static char buf[MAXDATASIZE];

  struct sockaddr_in sock;
  struct hostent *he;


  if ((he=gethostbyname(host)) == NULL) 
    {
      perror("gethostbyname()");
      exit(1);
    }

  sock.sin_family = AF_INET;
  sock.sin_port = htons(control.port);
  sock.sin_addr =  *((struct in_addr *)he->h_addr);  
  bzero(&(sock.sin_zero), 8);

  if (connect(s, (struct sockaddr *)&sock, sizeof(struct sockaddr)) == ERROR) 
    {
	perror("connect()");
    }
  else
    if ((send(s, MESSAGE, strlen(MESSAGE), 0)) == ERROR)
      perror("send()");
    else
      if ((numbytes=recv(s, buf, MAXDATASIZE, 0)) == ERROR)
        perror("recv()");

  close(s);
  return buf;
}

void usage()
  {
    printf("\n%s %s\n",name,VERSION);
    printf("\nCoded By SkeMet");
    printf("\n%s",DATE);
    printf("\n\nusage:");
    printf("\n				-s ip");
    printf("\n				-f filename");
    printf("\n				-p port number (default 80)");
    printf("\n				-l log file name (not enabled))");
    printf("\n\nCoded by Skemet, U might fool many people but U can't fool your self!\n\n");
    exit(0);
  };

void log(char str[MAXDATASIZE])
	{
  	FILE *fd;

    if ((fd=fopen(logfile,"a"))==NULL)
	  	{
			  perror("Error fopen()");
        fprintf(stderr,"Not Logging!!!!!");
				return;
			};
		
		fprintf(fd,"\n%s",str);
		fclose(fd);	
	}







