home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit 2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Unix / c-src / fm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-04  |  3.1 KB  |  162 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netdb.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <signal.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11.  
  12.  
  13. int openSock(name,port)
  14. char *name;
  15. int port;
  16.  
  17. {
  18.       int mysock,opt=1;
  19.       struct sockaddr_in sin;
  20.       struct hostent *he;
  21.       he = gethostbyname(name);
  22.       if (he == NULL) {
  23.             printf("No host found..\n");
  24.             exit(0);
  25.       }
  26.  
  27.       memcpy((caddr_t)&sin.sin_addr,he->h_addr_list[0],he->h_length);
  28.       sin.sin_port = port;
  29.  
  30.       sin.sin_family = AF_INET;
  31.  
  32.       mysock = socket(AF_INET,SOCK_STREAM,0);
  33.  
  34.       opt = connect(mysock,(struct sockaddr *)&sin,sizeof(sin));
  35.  
  36.       return mysock;
  37.  
  38. }
  39.  
  40. /* This allows us to have many people on one TO line, seperated by
  41.    commas or spaces. */
  42.  
  43. process(s,d)
  44. int d;
  45. char *s;
  46. {
  47.       char *tmp;
  48.       char buf[120];
  49.  
  50.       tmp = strtok(s," ,");
  51.  
  52.       while (tmp != NULL) {
  53.             sprintf(buf,"RCPT TO: %s\n",tmp);
  54.             write(d,buf,strlen(buf));
  55.             tmp = strtok(NULL," ,");
  56.       }
  57.  
  58. }
  59.  
  60.  
  61.  
  62. getAndSendFrom(fd)
  63. int fd;
  64. {
  65.       char from[100];
  66.       char outbound[200];
  67.  
  68.       printf("You must should specify a From address now.\nFrom: ");
  69.       gets(from);
  70.  
  71.       sprintf(outbound,"MAIL FROM: %s\n",from);
  72.       write(fd,outbound,strlen(outbound));
  73.  
  74.  
  75.  
  76. }
  77.  
  78. getAndSendTo(fd)
  79. int fd;
  80. {
  81.       char addrs[100];
  82.  
  83.       printf("Enter Recipients, with a blank line to end.\n");
  84.  
  85.       addrs[0] = '_';
  86.  
  87.       while (addrs[0] != '\0') {
  88.             printf("To: ");
  89.             gets(addrs);
  90.             process(addrs,fd);
  91.       }
  92.  
  93. }
  94.  
  95. getAndSendMsg(fd)
  96. int fd;
  97. {
  98.       char textline[90];
  99.       char outbound[103];
  100.  
  101.       sprintf(textline,"DATA\n");
  102.       write(fd,textline,strlen(textline));
  103.  
  104.  
  105.       printf("You may now enter your message.  End with a period\n\n");
  106.       printf("[---------------------------------------------------------]\n");
  107.  
  108.       textline[0] = '_';
  109.  
  110.       while (textline[0] != '.') {
  111.             gets(textline);
  112.             sprintf(outbound,"%s\n",textline);
  113.             write(fd,outbound,strlen(outbound));
  114.       }
  115.  
  116. }
  117.  
  118.  
  119. main(argc,argv)
  120. int argc;
  121. char *argv[];
  122. {
  123.  
  124.       char text[200];
  125.       int file_d;
  126.  
  127.       /* Get ready to connect to host. */
  128.       printf("SMTP Host: ");
  129.       gets(text);
  130.  
  131.       /* Connect to standard SMTP port. */
  132.       file_d = openSock(text,25);
  133.  
  134.       if (file_d < 0) {
  135.             printf("Error connecting to SMTP host.\n");
  136.             perror("smtp_connect");
  137.             exit(0);
  138.       }
  139.  
  140.       printf("\n\n[+ Connected to SMTP host %s +]\n",text);
  141.  
  142.       sleep(1);
  143.  
  144.       getAndSendFrom(file_d);
  145.  
  146.       getAndSendTo(file_d);
  147.  
  148.       getAndSendMsg(file_d);
  149.  
  150.       sprintf(text,"QUIT\n");
  151.       write(file_d,text,strlen(text));
  152.  
  153.     /* Here we just print out all the text we got from the SMTP
  154.        Host.  Since this is a simple program, we didnt need to do
  155.        anything with it. */
  156.  
  157.     printf("[Session Message dump]:\n");
  158.       while(read(file_d,text,78) > 0)
  159.             printf("%s\n",text);
  160.       close(file_d);
  161. }
  162.