home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / b4b0 / b4b0-08 / hhp-ms.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  2.8 KB  |  136 lines

  1. /*
  2.    hhp-ms.c (Messaging System v0.2)
  3.    by tGb.
  4.    h0h0h0 hhp kickin it in 99.
  5.  
  6.    Tested on Slackware and RedHat 2.0.36 (Might work on FreeBSD)
  7.    To compile: gcc hhp-ms.c -o hhp-ms.
  8.  
  9.    Coming in future versions.
  10.    Account management, message management, user-to-user messaging,
  11.    and passworded accounts.
  12. */
  13.  
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <stdio.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include <unistd.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23.  
  24. #define VER "v0.2"
  25. #define BACKLOG 20
  26. #define BUF 10000
  27.  
  28. int newfd;
  29.  
  30. int main()
  31. {
  32.   int sockfd, sin_size, bytes, ss;
  33.  
  34.   struct sockaddr_in mine;
  35.   struct sockaddr_in theirs;
  36.  
  37.   char passwd[1024];
  38.   char *gp;
  39.   char dir[50];
  40.   int port;
  41.   char password[50];
  42.  
  43.   printf("What /path/filename would you like the msgs to be stored? ");
  44.   scanf(" %s", dir);
  45.   printf("What port do you want to run this on? ");
  46.   scanf(" %d", &port);
  47.   printf("What do you want the port password to be? ");
  48.   scanf(" %s", password);
  49.  
  50.   if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  51.     {
  52.       perror("socket()");
  53.       exit(1);
  54.     }
  55.  
  56.   mine.sin_family = AF_INET;
  57.   mine.sin_addr.s_addr = INADDR_ANY;
  58.   mine.sin_port = htons(port);
  59.   bzero(&(mine.sin_zero), 8);
  60.  
  61.   if (bind(sockfd, (struct sockaddr *)&mine, sizeof(struct sockaddr)) ==
  62. -1)
  63.    {
  64.      perror("bind()");
  65.      exit(1);
  66.    }
  67.  
  68.  
  69.   if (listen(sockfd, BACKLOG) == -1) {
  70.    perror("listen()");
  71.    exit(1);
  72.   }
  73.   printf("hhp-ms.c (Message System %s) - by: tGb Loaded! on port: %d\n",
  74. VER, p
  75. ort);
  76.         if(fork())
  77.          {
  78.           exit(0);
  79.          }
  80.         else
  81.  while(1)
  82.    {
  83.      ss = sizeof (struct sockaddr_in);
  84.      if((newfd = accept(sockfd, (struct sockaddr *) &theirs, &sin_size))
  85. == -1)
  86.        {
  87.          perror("accept()");
  88.        }
  89.          bytes = send(newfd, "Welcome to tGb's hhp-ms.c (Message System
  90. v0.2)\n
  91. Please enter your password: ", 77, 0);
  92.          recv(newfd, passwd, 1024, 0);
  93.  
  94.          if((gp = strchr(passwd, 13)) != NULL)
  95.            *(gp) = '\0';
  96.  
  97.          if(!strcmp (passwd, password))
  98.            {
  99.              send(newfd, "Access Granted!\n", 16, 0);
  100.              message(dir);
  101.            }
  102.          else if (passwd != password)
  103.            {
  104.              send(newfd, "Access denied!\n", 15, 0);
  105.              close(newfd);
  106.            }
  107.         }
  108.      }
  109.  
  110.  
  111. message(char *dir)
  112. {
  113.   FILE *log;
  114.   char msg[BUF];
  115.   char name[30];
  116.  
  117.   memset(msg, 0, BUF);
  118.   memset(name, 0, 30);
  119.  
  120.   log = fopen(dir, "a+");
  121.   send(newfd, "Name: ", 6, 0);
  122.   recv(newfd, name, 30, 0);
  123.   fprintf(log, "-----------------------------------\n");
  124.   fprintf(log, "From: %s\n", name);
  125.  
  126.   send(newfd, "Your message: ", 14, 0);
  127.   recv(newfd, msg, BUF, 0);
  128.  
  129.   fprintf(log, "%s\n", msg);
  130.   fprintf(log, "-----------------------------------\n");
  131.   fclose(log);
  132.  
  133.   send(newfd, "Message sent.\n", 14, 0);
  134.   close(newfd);
  135. }
  136.