tiny web program in C

From a book im reading.

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 3276 Views - Last Post: 26 May 2009 - 08:44 PM Rate Topic: -----

#1 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

tiny web program in C

Posted 25 May 2009 - 04:51 PM

So i'm reading this book on exploits and such and it has had me create a tiny website using some C code to accept outside connections, the only problem is I can not mod the website to actually display anything, such as words or a colored background. It seems as though the ./webroot directory is not being created. If anyone could help me trouble shoot this I would greatly appreciate.

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include "hacking.h"				  //These header files are just for error handling, there is no "hacking" involved
#include "hacking-network.h"	//with these header files. They are just functions to call error messages.

#define PORT 80	//Port users will be connecting to
#define WEBROOT "./webroot"	//Webservers root directory

void handle_connection(int, struct sockaddr_in *);	//Handle web requests
int get_file_size(int);		//Returns file size of open file descriptor

int main(void) {
	int sockfd, new_sockfd, yes = 1;
	struct sockaddr_in host_addr, client_addr;		//My address information
	socklen_t sin_size;
	
	printf("Accepting web requests on port %d\n", PORT);
	
	if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
		fatal("in socket");
	if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
		fatal("setting socket option SO_REUSEADDR\n");
	
	host_addr.sin_family = AF_INET;		//Host byte order
	host_addr.sin_port = htons(PORT);		//Short, newtork byte order
	host_addr.sin_addr.s_addr = INADDR_ANY;	//Automatically fill with my IP
	memset(&(host_addr.sin_zero), '\0', 8);	//Zero the rest of the struct
	
	if(bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) == -1)
		fatal("binding to socket");
	
	if(listen(sockfd, 20) == -1)
		fatal("listening on socket");
	
	while(1) {	//accept loop
		sin_size = sizeof(struct sockaddr_in);
		new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
		if(new_sockfd == -1)
			fatal("accepting connection");
		handle_connection(new_sockfd, &client_addr);
	}//Close while
	return 0;
}//Close main

/***************************************************
This funtion handles the connection on the passed socket
from the passed client address. The connection is processed
as a web request, and this function replies over the 
connected socket. Finally, the passed socket is closed
at the end of the function.
****************************************************/
void handle_connection(int sockfd, struct sockaddr_in *client_addr_ptr) {
	unsigned char *ptr, request[500], resource[500];
	int fd, length;
	
	length = recv_line(sockfd, request);
	
	printf("Got request from %s:%d \"\%s\"\n", inet_ntoa(client_addr_ptr->sin_addr),
	ntohs(client_addr_ptr->sin_port), request);
	
	ptr = strstr(request, "HTTP/");		//Search for valid looking request
	if(ptr == NULL) {		//The this isnt a valid HTTP
		printf("NOT HTTP!\n");
	} else {
		*ptr = 0;	//Terminate the buffer at the end of the URL
		ptr = NULL;
		if(strncmp(request, "GET", 4) == 0)	//GET request
			ptr = request + 4;	//ptr is URL
		if(strncmp(request, "HEAD", 5) == 0)	//HEAD request
			ptr = request + 5;	//ptr is URL
		
		if(ptr == NULL) {		//Then this is not a recognized request
			printf("\tUNKNOWN REQUEST!\n");
		} else {		//Valid request, with ptr ponting to the resource name
			if(ptr[strlen(ptr) - 1] == '/')	//For resources ending with '/'
				strcat(ptr, "index.html");	//ad index.html to end.
			strcpy(resource, WEBROOT);	//Begin resource with WEBROOT
			strcat(resource, ptr);			//and join it with resource path
			fd = open(resource, O_RDONLY, 0);	//Try to open the file
			printf("\tOpening \'%s \'\t", resource);
			if(fd == -1) {		//If file is not found
				printf(" 404 Not Found\n");
				send_string(sockfd, "HTTP/1.0 404 Not Found\r\n");
				send_string(sockfd, "Server: Tiny webserver \r\n\r\n");
				send_string(sockfd, "<html><head><title>404 not found</title></head>");
				send_string(sockfd, "<body><h1>URL not found</h1></body></html>\r\n");
			} else {		//Otherwise serve up the file 
				printf(" 200 OK\n");
				send_string(sockfd, "HTTP/1.0 200 OK\r\n");
				send_string(sockfd, "Server: Tiny webserver\r\n\r\n");
				if(ptr == request + 4) {	//Then this is a GET request
					if( (length = get_file_size(fd)) == -1)
						fatal("getting resource file size");
					if( (ptr = (unsigned char *) malloc(length)) == NULL)
						fatal("allocating memory for reading resource");
					read(fd, ptr, length);	//Read file into memory
					send(sockfd, ptr, length, 0); 	//Send it to socket
					free(ptr);		//Free file memory
				}//Close if
				close(fd);		//Close file
			} //Close else printf 200 block
		}// close preceding else to above else statement
	}//End if block for valid HTTP
	shutdown(sockfd, SHUT_RDWR);	//close socket ever so gingerly
}//Close void handle_connection

/*******************************************************
This function accepts an open file descriptor and returns
the size of the associated file. returns -1 on failure
********************************************************/
int get_file_size(int fd) {
	struct stat stat_struct;
	
	if(fstat(fd, &stat_struct) == -1)
		return -1;
	return (int) stat_struct.st_size;
}// close get_file_size



I get the following output when ran and connected to the site

Accepting web requests on port 80
Got request from 127.0.0.1:47241 "GET / HTTP/1.1"
UNKNOWN REQUEST!
Got request from 127.0.0.1:47242 "GET /favicon.ico HTTP/1.1"
UNKNOWN REQUEST!
Got request from 127.0.0.1:47243 "GET /favicon.ico HTTP/1.1"
UNKNOWN REQUEST!


It works, but for some reason I can not get the webroot directory to come up so I can edit the blank page.

:~$ webroot/
bash: webroot/: No such file or directory
:~$ ./webroot
bash: ./webroot: No such file or directory
:~$ sudo -i

[email protected]:~# ./webroot
-bash: ./webroot: No such file or directory
[email protected]:~# webroot/
-bash: webroot/: No such file or directory
[email protected]:~#
I clearly don't have the directory

This post has been edited by IngeniousHax: 25 May 2009 - 05:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: tiny web program in C

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6715
  • View blog
  • Posts: 31,111
  • Joined: 10-May 07

Re: tiny web program in C

Posted 25 May 2009 - 06:57 PM

The directory from where you are running the binary exe, you should create that directory, & be sure to give it the correct privileges, & user group permissions to allow for the share to be viewable.
Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: tiny web program in C

Posted 25 May 2009 - 08:02 PM

Ok, I still get no webroot directory and now I can't even run the program...It gives me the fatal error of-->

Accepting web requests on port 80
[!!] Fatal error binding to socket: Address already in use


And I don't have the program running...This is weird is it not, considering all web pages connect to port 80 which means something is clearly out of sync. I even deleted the file and re-compiled it, and still, nothing.

This post has been edited by IngeniousHax: 25 May 2009 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6715
  • View blog
  • Posts: 31,111
  • Joined: 10-May 07

Re: tiny web program in C

Posted 25 May 2009 - 08:07 PM

You have to kill the previous server. It bound itself to port 80, so the port is in use. Think of it how fopen creates a file lock, & now you are trying to open a 2nd lock w/ that file.

check for the pid w/ ps.

ps -aux | grep *command*

Then you'll kill it with -9.

kill -9 *pid*

Post back :) Lets get this working.
Was This Post Helpful? 0
  • +
  • -

#5 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: tiny web program in C

Posted 25 May 2009 - 08:12 PM

under *command* what should I be placing there? I used the compiled program, and I get this output:

ps -aux | grep ./tinyweb
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
root 16621 0.0 0.0 1660 396 ? S 16:49 0:00 ./tinyweb
1000 20626 1.0 1.1 38060 17640 ? S 20:07 0:02 gedit /home/ingenioushax/Desktop/Book folder/0x400/0x410/tinyweb.c
1000 20775 0.0 0.0 3336 816 pts/2 R+ 20:11 0:00 grep ./tinyweb


EDIT::

root 16621 0.0 0.0 1660 396 ? S 16:49 0:00 ./tinyweb
1000 20626 0.6 1.1 38060 17640 ? S 20:07 0:02 gedit /home/ingenioushax/Desktop/Book folder/0x400/0x410/tinyweb.c
1000 20780 0.0 0.0 3340 872 pts/2 S+ 20:13 0:00 grep ./tinyweb

This post has been edited by IngeniousHax: 25 May 2009 - 08:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6715
  • View blog
  • Posts: 31,111
  • Joined: 10-May 07

Re: tiny web program in C

Posted 25 May 2009 - 09:28 PM

The dot slash is only required when you launch the command, because the curent path isn't in the system path.

ps -aux | grep tinyweb
Was This Post Helpful? 0
  • +
  • -

#7 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: tiny web program in C

Posted 25 May 2009 - 10:07 PM


root 16621 0.0 0.0 1660 396 ? S 16:49 0:00 ./tinyweb
1000 20626 0.0 1.1 38060 17640 ? S 20:07 0:02 gedit /home/ingenioushax/Desktop/Book folder/0x400/0x410/tinyweb.c
1000 22598 0.0 0.0 3336 804 pts/2 R+ 22:06 0:00 grep tinyweb

What do I do from here, I don't understand the kill -9 *pid* portion, I tried to kill -9 all those numbers and nothing worked.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6715
  • View blog
  • Posts: 31,111
  • Joined: 10-May 07

Re: tiny web program in C

Posted 25 May 2009 - 10:15 PM

kill -9 16621
Was This Post Helpful? 1
  • +
  • -

#9 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: tiny web program in C

Posted 25 May 2009 - 10:19 PM

Hooray! You got it to work again, now accepting hosts on port 80!!! huzzah!

Do you know how I could get to be able to view the webroot directory? Should I add permissions such as

sudo chown root ./tinyweb
sudo chmod u+s ./tinyweb
?

EDIT::
I just did so, and I still get no webroot directory

This post has been edited by IngeniousHax: 25 May 2009 - 10:20 PM

Was This Post Helpful? 0
  • +
  • -

#10 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6715
  • View blog
  • Posts: 31,111
  • Joined: 10-May 07

Re: tiny web program in C

Posted 25 May 2009 - 10:23 PM

View PostIngeniousHax, on 25 May, 2009 - 11:19 PM, said:

sudo chown root ./tinyweb

You want to set it to a lower user level than root. I would change it to nouser

sudo chown -R nouser tinyweb
This will act recursively, changing all subdirectories as well.

View PostIngeniousHax, on 25 May, 2009 - 11:19 PM, said:

sudo chmod u+s ./tinyweb

I'm not sure what this means. Though I'm not sure what the two methods are called, exactly, I use the numbers.

sudo chmod tinyweb 777
Do this again for your webroot directory as well.
Was This Post Helpful? 0
  • +
  • -

#11 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: tiny web program in C

Posted 25 May 2009 - 10:25 PM

Thats the thing though, I have no such directory as webroot. I ls -l webroot, and ls -l webroot/ and ls -l web* and the only thing I come up with is my simple webserver_id program.

IDK where abouts this thing would be located let alone how to access such a thing.
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6715
  • View blog
  • Posts: 31,111
  • Joined: 10-May 07

Re: tiny web program in C

Posted 25 May 2009 - 10:27 PM

View PostIngeniousHax, on 25 May, 2009 - 11:25 PM, said:

Thats the thing though, I have no such directory as webroot. I ls -l webroot, and ls -l webroot/ and ls -l web* and the only thing I come up with is my simple webserver_id program.

Then you need to make one...
Was This Post Helpful? 0
  • +
  • -

#13 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: tiny web program in C

Posted 25 May 2009 - 10:29 PM

Do you think I should just place it in my home directory? I imagine that would be easiest right?

EDIT::
I figured it out I believe. I would like to thank you so much for all your assistance and patience in this endeavor. id be lost with-out your guidance!

Many thanks.

This post has been edited by IngeniousHax: 25 May 2009 - 10:32 PM

Was This Post Helpful? 0
  • +
  • -

#14 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6715
  • View blog
  • Posts: 31,111
  • Joined: 10-May 07

Re: tiny web program in C

Posted 25 May 2009 - 10:33 PM

When you run dot slash, as in ./tinyweb, this is saying execute the file tinyweb that exists in this directory. Since it's looking for webroot in that directory, then make webroot in the same directory that tinyweb is being launched from.

You'll need to make sure that the directory webroot has the same permissions that were suggested previously.
Was This Post Helpful? 0
  • +
  • -

#15 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: tiny web program in C

Posted 25 May 2009 - 10:44 PM

Ok, yeah i'm trying to run it now and kinda trying to edit all this stuff to work properly, i've created a .html file with an image, bg color and text, now im just trying to get it to display when I do the website address, which is 127.0.0.1 or the loopback address
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2