what you don't know can hurt you
Home Files News &[SERVICES_TAB]About Contact Add New

redir-httpd.c

redir-httpd.c
Posted Jan 8, 2001
Site technopagan.org

redir-httpd is an ultra-minimalist, non-RFC-compliant HTTP server that will ONLY issue redirects to another site. It's good for running on home systems that have permanent connectivity (i.e. DSL and cable-modem subscribers). It should be short enough to be easily understood (and thus audited for potential security issues), and still fairly robust.

tags | web
SHA-256 | f9d88a4cd09ef7c39dc3301fb37e374a6aa58d621506ed6948a2aef19eb42d95

redir-httpd.c

Change Mirror Download
/***

redir-httpd is an ultra-minimalist, non-RFC-compliant HTTP server that
will ONLY issue redirects to another site. It's good for running on
home systems that have permanent connectivity (i.e. DSL and cable-modem
subscribers). It should be short enough to be easily understood (and
thus audited for potential security issues), and still fairly robust.

It *is* vulnerable to DOS attacks, but what the hell. I wrote it
in like a half-hour.

If you use this as a "real" httpd (i.e. port 80) you'll have to be
root to start it (presumably from inetd).

All configuration is done via the list of #define statements below.

(C)2001 David E. Smith <dave@bureau42.com> . Released under GNU GPL v2.

***/

#define SERVER_ADMIN "dave@bureau42.com"
#define SERVER_TARGET "http://www.bureau42.com/"
#define SERVER_PORT 8020
#define ERROR_CODE 302

/* A note on the ERROR_CODE: 301 is "Moved Permanently", 302 is "Found"
(i.e. a temporary redirect), and 303 is "See Other". 303 is probably
the most "correct" one to use, but Netscape doesn't like it. I
default to using 302, but hey, it's your httpd. */

#define LOGGING 1

/* if LOGGING is defined, we'll record some stuff to syslog. */

/* no user-serviceable parts below this point */

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#ifdef LOGGING
#include <syslog.h>
#endif

#define TRUE 1
#define FALSE !TRUE
#define SERVER_NAME "redir-httpd"
#define SERVER_VERSION "0.0.2"

/* sigkill and friends handler -- should actually be installed at some stage */
int funeral(int sig) {
#ifdef LOGGING
syslog(LOG_WARNING, "shutting down (received signal %i)", sig);
closelog();
#endif
exit(EXIT_SUCCESS);
return 0;
}

/* print stuff to remote site */
/* As a side effect, this zeroes out its input. Wheee. */
int render (int fd, char *str) {
int ret = write (fd, str, strlen(str));
memset (str, '\0', strlen(str));
return ret;
}

int iscool (char c) {
if ( (c=='\n') || (c=='\r') ) return TRUE;
return FALSE;
}

int main(void) {
int server_sockfd, client_sockfd;
int server_len, client_len;
int errtrack;
struct sockaddr_in server_address;
struct sockaddr_in client_address;

#ifdef LOGGING
openlog(SERVER_NAME,LOG_PID,LOG_DAEMON);
syslog(LOG_INFO, "launching (target %s) on port %i", SERVER_TARGET, SERVER_PORT);
#endif

/* install signal handlers now, so we can die quickly if need be */
signal(SIGTERM, (void *)funeral);
signal(SIGKILL, (void *)funeral);
signal(SIGINT, (void *)funeral);
signal(SIGSEGV, (void *)funeral);

server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (server_sockfd == -1) {
#ifdef LOGGING
syslog (LOG_ERR, "unable to start");
#endif
exit(EXIT_FAILURE);
}
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(SERVER_PORT);
server_len = sizeof(server_address);
errtrack = bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
if (errtrack == -1) {
#ifdef LOGGING
syslog (LOG_ERR, "unable to bind() socket");
#endif
exit(EXIT_FAILURE);
}
errtrack = listen(server_sockfd, 5);
if (errtrack == -1) {
#ifdef LOGGING
syslog (LOG_ERR, "unable to listen() to socket");
#endif
exit(EXIT_FAILURE);
}

while(1) {
char outstr[1024];
char ch, ch1, ch2, ch3, ch4;

client_sockfd = accept(server_sockfd,
(struct sockaddr *)&client_address, &client_len);

/* wait 'til we get 4 OK chars. NON standard, but quick'n'easy */
/* The HTTP RFC specifies that we wait for two CR/LF linefeeds */

while (read(client_sockfd, &ch, 1)) {
ch1 = ch2; ch2 = ch3; ch3 = ch4; ch4 = ch;
if ( (iscool(ch1)) && (iscool(ch2)) &&
(iscool(ch3)) && (iscool(ch4)) ) {
sprintf (outstr, "HTTP/1.1 %i Try Again\n", ERROR_CODE);
render (client_sockfd, outstr);
sprintf (outstr, "Server: %s %s\n", SERVER_NAME, SERVER_VERSION);
render (client_sockfd, outstr);
sprintf (outstr, "X-Server-Admin: %s\n", SERVER_ADMIN);
render (client_sockfd, outstr);
sprintf (outstr, "Location: %s\n", SERVER_TARGET);
render (client_sockfd, outstr);
sprintf (outstr, "Cache-Control: public\n");
render (client_sockfd, outstr);
sprintf (outstr, "Connection: close\n");
render (client_sockfd, outstr);
sprintf (outstr, "Content-Type: text/html\n\n");
render (client_sockfd, outstr);

sprintf (outstr, "<HTML><HEAD><TITLE>%i Try Again</TITLE></HEAD>\n", ERROR_CODE);
render (client_sockfd, outstr);
sprintf (outstr, "</HEAD><BODY>\n");
render (client_sockfd, outstr);
sprintf (outstr, "The document has moved <A HREF=\"%s\">here</A>.\n", SERVER_TARGET);
render (client_sockfd, outstr);
sprintf (outstr, "</BODY></HTML>\n");
render (client_sockfd, outstr);


#ifdef LOGGING
syslog(LOG_INFO, "connection accepted from %s",
inet_ntoa(client_address.sin_addr) );
#endif
close(client_sockfd);
ch = ch1 = ch2 = ch3 = ch4 = '0' ;
break;
} /* if */
} /* while read */
} /* while 1 */
} /* main() */

/* Thus endeth the lesson. */

Login or Register to add favorites

File Archive:

November 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Nov 1st
    30 Files
  • 2
    Nov 2nd
    0 Files
  • 3
    Nov 3rd
    0 Files
  • 4
    Nov 4th
    12 Files
  • 5
    Nov 5th
    44 Files
  • 6
    Nov 6th
    18 Files
  • 7
    Nov 7th
    9 Files
  • 8
    Nov 8th
    8 Files
  • 9
    Nov 9th
    3 Files
  • 10
    Nov 10th
    0 Files
  • 11
    Nov 11th
    14 Files
  • 12
    Nov 12th
    20 Files
  • 13
    Nov 13th
    63 Files
  • 14
    Nov 14th
    18 Files
  • 15
    Nov 15th
    8 Files
  • 16
    Nov 16th
    0 Files
  • 17
    Nov 17th
    0 Files
  • 18
    Nov 18th
    18 Files
  • 19
    Nov 19th
    7 Files
  • 20
    Nov 20th
    13 Files
  • 21
    Nov 21st
    6 Files
  • 22
    Nov 22nd
    48 Files
  • 23
    Nov 23rd
    0 Files
  • 24
    Nov 24th
    0 Files
  • 25
    Nov 25th
    60 Files
  • 26
    Nov 26th
    0 Files
  • 27
    Nov 27th
    44 Files
  • 28
    Nov 28th
    0 Files
  • 29
    Nov 29th
    0 Files
  • 30
    Nov 30th
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2024 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close