/* Virtual Hostname/Machine IRC Bouncer
   *** Includes:
  -choose any irc server after connect!
  -virtual hostnames...duh!
  -password protected after connnect.
   *** Future Plans: To make it so it allows more than one connect.
       */
/* INSTRUCTIONS FOR USE: First, type export IRC_HOST=your.irc.host
   Next, Type ./vhost <port num>  eg. ./vhost 9090 */

/* DEFINES */
#define password "darkk"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
extern h_errno;

/* SunOS 4.1.4 is really lame and doesn't define this */

#ifndef INADDR_NONE
 #define INADDR_NONE -1
#endif

struct  in_addr LocalHostAddr;

/* m_strdup stolen from ircII epic */
/* changed to default malloc instead of ircII epic's new_malloc routine */
/* Hope it doesn't create any adverse affects */

char    *m_strdup (const char *str)
{
 char *ptr;
 char empty_string[] = "";

 if (!str)
  str = empty_string;
 ptr = (char *)malloc(strlen(str) + 1);
 return strcpy(ptr, str);
}

int do_connect(char *hostname, u_short port)
{
 int s = -1;
 struct hostent *he;
 struct sockaddr_in sin;
 struct sockaddr_in localaddr;

 s = socket(AF_INET, SOCK_STREAM, 0);
 if(s < 0)
  return -2;

/* bind to the local address, this was a pain in the ass to understand */

 memset(&localaddr, 0, sizeof(struct sockaddr_in));
 localaddr.sin_family = AF_INET;
 localaddr.sin_addr = LocalHostAddr;
 localaddr.sin_port = 0;
 if (bind(s, (struct sockaddr *)&localaddr, sizeof(localaddr)))
 return close(s), -2;

/* now connect from the address.. fun for the entire family. */

 memset(&sin, 0, sizeof(struct sockaddr_in));
 sin.sin_family = AF_INET;
 sin.sin_port = htons(port);
 sin.sin_addr.s_addr = inet_addr(hostname);
 if(sin.sin_addr.s_addr == INADDR_NONE)
 {
  he = gethostbyname(hostname);
  if(!he)
   return -1;
  memcpy(&sin.sin_addr, he->h_addr, he->h_length);
 }

 if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
 {
  close(s);
  return -2;
 }
 return s;
}

void server(int s)
{
 int sock_c=-1;
 int maxfd = s+1;
 char buffer[1024];
 char user[1024];
 char nick[1024];
 fd_set rset;
 u_short myport;
 char *cmd, *server, *port, *passwd;
 char myserver[1024];

 memset(buffer, 0, 1023);
 if(read(s, buffer, 1024) <= 0)
 {
  close(s);
  return;
 }
 if(!strncmp(buffer, "USER", 4))
  strcpy(user, buffer);
 else
  strcpy(nick, buffer);
 memset(buffer, 0, 1024);
 if(read(s, buffer, 1023) <= 0)
 {
  close(s);
  return;
 }
 if(!strncmp(buffer, "USER", 4))
  strcpy(user, buffer);
 else
  strcpy(nick, buffer);
 sprintf(buffer, "NOTICE usr :****************************************\n");
 sprintf(buffer, "NOTICE usr :*  Welcome To Virtual IRC GateWay      *\n");
 sprintf(buffer, "NOTICE usr :****************************************\n");
 sprintf(buffer, "NOTICE usr : Enter your password....\n");
 sprintf(buffer, "NOTICE usr : e.g. /quote connect mypasswd irc.nwlink.com\n");
 write(s, buffer, strlen(buffer));
 while(1)
 {
  FD_ZERO(&rset);
  FD_SET(s, &rset);
  if(sock_c >= 0)
   FD_SET(sock_c, &rset);
  if(sock_c > s)
   maxfd = sock_c + 1;
  else
   maxfd = s + 1;
  select(maxfd, &rset, NULL, NULL, NULL);
  memset(buffer, 0, 1024);
  if(FD_ISSET(s, &rset))
  {
   if(read(s, buffer, 1023) <= 0)
   {
    close(s);
    close(sock_c);
    return;
   }
   if(sock_c >= 0)
   {
    write(sock_c, buffer, strlen(buffer));
   }
   else
   {
    cmd = NULL;
    server = NULL;
    port = NULL;
    passwd = NULL;
    cmd = strtok(buffer, " ");
    if(!cmd)
     continue;
    if(strcmp(cmd, "connect"))
     continue;
    passwd = strtok(NULL, " \n\r");
    if(!passwd)
     continue;
    if((strcmp(passwd, password) != 0))
    {
     sprintf(buffer, "NOTICE usr :*** Password Invalid. UNAUTHORIZED ACCESS IS PROHIBITED \n");
     write(s, buffer, strlen(buffer));
     continue;
    }

    server = strtok(NULL, " \n\r");
    if(!server)
     continue;
    strcpy(myserver, server);
    port = strtok(NULL, " \n\r");
    if(!port)
     port = "6667";
    myport = atoi(port);
    sock_c = do_connect(myserver, myport);
    if(sock_c < 0)
     sock_c = -1;
    write(sock_c, user, strlen(user));
    write(sock_c, nick, strlen(nick));
    memset(buffer, 0, 1024);
    read(sock_c, buffer, 1023);
    if(strlen(buffer) > 0)
    {
     write(s, buffer, strlen(buffer));
    }
    continue;
   }
  }
  if(sock_c >= 0)
  {
   if(FD_ISSET(sock_c, &rset))
   {
    memset(buffer, 0, 1024);
    if(read(sock_c, buffer, 1023) <= 0)
    {
     close(s);
     close(sock_c);
     return;
    }
    write(s, buffer, strlen(buffer));
   }
  }
 }
}
 

 
int
main(int argc, char *argv[])
{
 int a_sock = 0;
 int s_sock = 0;
 char    *LocalHostName = NULL;
 char    *ptr = NULL;
 char hostname[81];
 struct hostent * hp;
 int dport;
 struct sockaddr_in sin;
 int sinlen;
 int xx;
 int mypid;
 if(argc >= 2) dport=atoi(argv[1]); else dport=9090;
 if ((ptr = getenv("IRC_HOST")) || (ptr = getenv("IRCHOST")))
  LocalHostName = m_strdup(ptr);
 if ((gethostname(hostname, sizeof(hostname))))
  if (!LocalHostName)
   exit(1);
 printf("Virtual host ircgate, *hop* Hold on..\n");
 printf("Attempting to bind on port: %d\n",dport);

 if (LocalHostName)
 {
  printf("Attempting to bind on host: %s\n", LocalHostName);
  memset((void *)&LocalHostAddr, 0, sizeof(LocalHostAddr));
  if ((hp = gethostbyname(LocalHostName)))
   memcpy((void *)&LocalHostAddr, hp->h_addr, sizeof(LocalHostAddr));
 }
 else
 {
  if ((hp = gethostbyname(hostname)))
   memcpy((char *) &LocalHostAddr, hp->h_addr, sizeof(LocalHostAddr));
 }

 s_sock = socket(AF_INET, SOCK_STREAM, 0);
 if(s_sock < 0)
 {
  perror("socket");
  exit(0);
 }
 memset(&sin, 0, sizeof(struct sockaddr_in));
 sin.sin_family = AF_INET;
 sin.sin_port = htons(dport);
 sin.sin_addr.s_addr = INADDR_ANY;
 if(bind(s_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0)
 {
  perror("bind");
  exit(0);
 }
  if(listen(s_sock, 10) < 0)
 {
  perror("listen");
  exit(0);
 }
 {
 xx = fork(); /* Fork the process */

 if (xx == -1) {
  printf("FATAL ERROR: UNABLE TO FORK PROCESS!\n");
  exit(1);
 }
 if (xx!=0) {
   printf("Launched into the background [pid: %d]\n\n",xx);
   setpgid(xx,xx);
   exit(0);
 }
 setpgid(0,0);
 /* No more TTY i/o for us! */
  freopen("/dev/null","r",stdin);
   freopen("/dev/null","w",stdout);
    freopen("/dev/null","w",stderr);

     while(1)
      {
 

  sinlen = sizeof(sin);
  close(a_sock);
  a_sock = accept(s_sock, (struct sockaddr *)&sin, &sinlen);
  if(a_sock < 0)
  {
   perror("accept");
   continue;
  }
#ifdef MULTIPLE_OCCURANCES
  switch(fork())
  {
   case -1:
    continue;
   case 0:
#endif
    server(a_sock);
#ifdef MULTIPLE_OCCURANCES
    exit(0);
  }
  /* Might want to set the last param to WNOHANG if it
     hangs here... Careful, creates zombies if you do. */
  while(waitpid(-1, NULL, NULL) > 0) ;
#endif
 }
}
}