1 #include "cgichk.h"
2 
3 struct in_addr      addr;
4 struct sockaddr_in  _sin;
5 struct hostent      *he = NULL;
6 int					sock = 0;
7 proxy_t				proxy = {NULL, 8000};
8 
9 /*----------------------------------------------------------------*/
setup_socket(char * host,int port)10 int setup_socket(char *host, int port)
11 {
12 	struct in_addr addr;
13 	unsigned long start;
14 	unsigned long end;
15 	unsigned long counter;
16 
17 	if (!he) // have we looked this up before?
18 	{
19 		if ((he = gethostbyname(host)) == NULL)
20 		{
21 			fprintf(stderr, "ERROR: Couldn't connect to host -> ");
22 			herror("gethostbyname");
23 			exit(0);
24 		}
25 	}
26 
27 	//start   = inet_addr(host);
28 	inet_aton(host, &addr);		// rather than inet_addr, etc.
29 	counter = ntohl(addr.s_addr);
30 
31 	sock = socket(AF_INET, SOCK_STREAM, 0);
32 	bcopy(he->h_addr, (char *) &_sin.sin_addr, he->h_length);
33 
34 	_sin.sin_family = AF_INET;
35 	_sin.sin_port   = htons(port);
36 
37 	if (connect(sock, (struct sockaddr *) &_sin, sizeof(_sin)) != 0)
38 	{
39 		return -1;
40 	}
41 	return 0;
42 }
43 /*----------------------------------------------------------------*/
read_head()44 void read_head()
45 {
46 	char *buffer;
47 
48 	buffer = malloc(BUFFER_SIZE);
49 	bzero(buffer, BUFFER_SIZE);
50 
51 	if (parms.proxy)
52 	{
53 		char *tempstr;
54 
55 		tempstr = (char *) malloc(1024);
56 
57 		if (strstr(parms.URL, "http://"))
58 		{
59 			snprintf(tempstr, 1024, "HEAD %s/ HTTP/1.0\n\n", parms.URL);
60 		}
61 		else
62 		{
63 			snprintf(tempstr, 1024, "HEAD http://%s/ HTTP/1.0\n\n", parms.URL);
64 		}
65 
66 		if (setup_socket(proxy.host, proxy.port))
67 		{
68 			fprintf(stderr, "ERROR: Problem connecting to proxy! -> ");
69 			perror("proxy connect");
70 			exit(-2);
71 		}
72 
73 		send(sock, tempstr, strlen(tempstr), 0);
74 	}
75 	else
76 	{
77 		send(sock, "HEAD / HTTP/1.0\n\n", 17, 0);
78 	}
79 	recv(sock, buffer, BUFFER_SIZE, 0);
80 	PRINT("%s", buffer);
81 	close(sock);
82 	free(buffer);
83 }
84 /*----------------------------------------------------------------*/
http_has(char * file,int result)85 int http_has(char *file, int result)
86 {
87 	int  err = 0;
88 	char query[256] = "";
89 
90 	if (parms.delay)
91 		waitafew(parms.delay);
92 
93 	if (parms.proxy)
94 	{
95 		if (strstr(parms.URL, "http://"))
96 		{
97 			snprintf(query, 256, "GET %s/%s HTTP/1.0\nUser-Agent: %s\nHost: %s\n\n", parms.URL, file, parms.agent, domain);
98 		}
99 		else
100 		{
101 			snprintf(query, 256, "GET http://%s/%s HTTP/1.0\nUser-Agent: %s\nHost: %s\n\n", parms.URL, file, parms.agent, domain);
102 		}
103 		//fprintf(stderr, "::%s\n", query);
104 		if (setup_socket(proxy.host, proxy.port))
105 		{
106 			fprintf(stderr, "ERROR: Problem connecting to proxy! -> ");
107 			perror("proxy connect");
108 			exit(-2);
109 		}
110 	}
111 	else
112 	{
113 		if (strstr(domain, "http://"))
114 			snprintf(query, 256, "GET %s%s/%s HTTP/1.0\nUser-Agent: %s\nHost: %s\n\n", domain, dir, file, parms.agent, domain);
115 		else
116 			snprintf(query, 256, "GET http://%s%s/%s HTTP/1.0\nUser-Agent: %s\nHost: %s\n\n", domain, dir, file, parms.agent, domain);
117 
118 		if (setup_socket(domain, parms.port))
119 		{
120 			fprintf(stderr, "ERROR: Problem connecting to host! -> ");
121 			perror("connect");
122 			exit(-1);
123 		}
124 	}
125 
126 	if (parms.sdebug)
127 			puts(query);
128 
129 	bzero(httpbuff, parms.buffer_length);
130 	send(sock, query, strlen(query), 0);
131 	{
132 		int x, c;
133 		x = 0;
134 		while(recv(sock, &c, 1, 0) == 1)
135 		{
136 			httpbuff[x] = c;
137 			x++;
138 			if (x == parms.buffer_length)
139 			{
140 				break;
141 			}
142 		};
143 	}
144 
145 	bzero(query, 256);
146 	memcpy(query, httpbuff+9, 3);  // Skip "HTTP/1.0 " and grab the 3 digits
147 
148 	err = atoi(query);
149 
150 	if (err == HTTP_OK || err == HTTP_MOVED)
151     {
152 		if (parms.no_false_200_search == FALSE)
153 		{
154 			if (cgichk_strnstr(httpbuff, "file not found") ||
155 				cgichk_strnstr(httpbuff, "error 404") ||
156 				cgichk_strnstr(httpbuff, "document has moved") ||
157 				cgichk_strnstr(httpbuff, "page you have requested") ) // Wise?
158 			{
159 				err = HTTP_NOEXIST;
160 			}
161 
162 			// If user spcified an additional string to search for, check for that too.
163 			if (parms.alt_fake_404_string)
164 			{
165 				if (cgichk_strnstr(httpbuff, parms.alt_fake_404_string))
166 				{
167 					err = HTTP_NOEXIST;
168 				}
169 			}
170 		}
171 	}
172 
173 	// Time for some debuggin...
174 	switch(parms.sdebug)
175 	{
176 		case DEBUG_RESULT:
177 			PRINT("[returned: %d]\n", err);
178 			break;
179 		case DEBUG_FOUND:
180 			if ((err != HTTP_NOEXIST) && (err != 0))
181 			{
182 				PRINT("\n\n ------------------------\n %s \n ------------------------\n", httpbuff);
183 				PRINT("Press any key to continue....\n");
184 				getchar();
185 			}
186 			break;
187 		case DEBUG_ALL:
188 			PRINT("\n\n ------------------------\n %s \n ------------------------\n", httpbuff);
189 			PRINT("Press any key to continue....\n");
190 			getchar();
191 			break;
192 		default:
193 			break;
194 	}
195 
196 	if ((err != HTTP_NOEXIST) && (err != 0))
197 	{
198 		/* Is this right? */
199 		if (parms.ignore)
200 		{
201 			if (parms.ignore == err)
202 				return 0;
203 		}
204 
205 		if (result)
206 		{
207 			PRINT("\007\r             \r\t%s found! (%d)\n", file, err);
208 		}
209 
210 		switch(err)
211 		{
212 			case  HTTP_MOVED:
213 			case  HTTP_FORBID:
214 				if (parms.ignore403)
215 					return 0;			//Fall through
216 			case HTTP_GATEWAY:
217 			case HTTP_SERVERR:
218 			case    HTTP_AUTH: 			//return 0;
219 			default:
220 					return err;
221 		}
222 	}
223 	else
224 	{
225 		return 0;
226 	}
227 }
228 
229