Guest User

Untitled

a guest
Oct 28th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. #ifndef unix
  2. #define WIN32
  3. #include <windows.h>
  4. #include <winsock.h>
  5. #include <process.h>
  6. #else
  7. #define closesocket close
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <sys/stat.h>
  11. #include <netinet/in.h>
  12. #include <netdb.h>
  13. #endif
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #ifdef WIN32
  20. #pragma comment(lib, "Ws2_32.lib")
  21. #define snprintf _snprintf
  22. #endif
  23.  
  24.  
  25. #define GET_METHOD 0
  26. #define POST_METHOD 1
  27.  
  28. #define NOP_STATUS -1
  29. #define TAG_STATUS 0
  30. #define VAL_STATUS 1
  31.  
  32. #define BUF_SIZE 1024
  33.  
  34. void print_error(void) {
  35. printf ("Content-Type: text/plain\r\n");
  36. //printf ("Content-Length: %d\r\n\r\n", 20);
  37.  
  38. printf("Problem in REQUEST\r\n");
  39.  
  40. return;
  41. }
  42.  
  43. char *get_next_var(char *line, char *tag, char *val) {
  44. char *ptr;
  45. int status, count;
  46.  
  47. ptr = line;
  48. if (*ptr)
  49. status = TAG_STATUS;
  50. count = 0;
  51. while(*ptr) {
  52. switch (*ptr) {
  53. case '&':
  54. val[count] = '\0';
  55. return ptr+1;
  56. break;
  57. case '=':
  58. tag[count] = '\0';
  59. count = 0;
  60. status = VAL_STATUS;
  61. break;
  62. default:
  63. if (status == TAG_STATUS) tag[count++] = *ptr;
  64. else val[count++] = *ptr;
  65. break;
  66. }
  67. ptr++;
  68. }
  69. if (status != VAL_STATUS) {
  70. //print_error();
  71. exit(0);
  72. }
  73. val[count] = '\0';
  74.  
  75. return NULL;
  76. }
  77.  
  78. // It's my party and I'll sing if I want to
  79. void get_request(char *result)
  80. {
  81. char *env;
  82. int method, contLength;
  83.  
  84. env = getenv("REQUEST_METHOD");
  85.  
  86. if (strcmp(env, "GET") == 0) {
  87. method = GET_METHOD;
  88. } else if (strcmp(env, "POST") == 0) {
  89. method = POST_METHOD;
  90. } else {
  91. print_error();
  92. exit(0);
  93. }
  94.  
  95. if (method == GET_METHOD) {
  96. env = getenv("QUERY_STRING");
  97. strcpy(result, env);
  98. } else {
  99. env = getenv("CONTENT_LENGTH");
  100. contLength = atoi(env);
  101. fgets(result, contLength + 1, stdin);
  102. }
  103. }
  104.  
  105. // You'll clean that up before you leave
  106. void process_request(char *request, char *username, char *password)
  107. {
  108. char *pline, tag[20], val[80], res[1024];
  109. FILE *fdlog;
  110.  
  111. pline = request;
  112. while (pline) {
  113. pline = get_next_var(pline, tag, val);
  114. if (strcmp(tag, "username") == 0) { strncpy(username, val, 80);}
  115. if (strcmp(tag, "password") == 0) { strncpy(password, val, 80);}
  116. }
  117.  
  118. fdlog = fopen("mail.log", "a+");
  119. fprintf (fdlog, "username: %s, password: %s\n", username, password);
  120. fclose(fdlog);
  121.  
  122. }
  123.  
  124. // Facilyce, upon waking
  125. int pop3_connect(int *sockfd, char *host, int portno)
  126. {
  127. struct sockaddr_in serv_addr;
  128. struct hostent *server;
  129.  
  130. *sockfd = socket(AF_INET, SOCK_STREAM, 0);
  131. if (sockfd < 0)
  132. return -1;
  133.  
  134. server = gethostbyname(host);
  135. if (server == NULL)
  136. return -1;
  137.  
  138. memset(&serv_addr, 0, sizeof(serv_addr));
  139. serv_addr.sin_family = AF_INET;
  140. memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
  141. serv_addr.sin_port = htons(portno);
  142.  
  143. if (connect(*sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  144. return -1;
  145.  
  146. return 0;
  147. }
  148.  
  149. // Coalescence, Inspiral, Ringdown
  150. int pop3_auth(int sockfd, FILE *log, char *username, char *password)
  151. {
  152. char buffer[BUF_SIZE];
  153. int n;
  154.  
  155. snprintf(buffer, BUF_SIZE, "user %s\r\n", username);
  156. fprintf(log, "Sending (%d bytes): %s", strlen(buffer), buffer);
  157.  
  158. n = send(sockfd, buffer, strlen(buffer), 0);
  159. if (n < 0) {
  160. fprintf(log, "Error writing to server\n");
  161. return -1;
  162. }
  163.  
  164. memset(buffer, 0, sizeof(buffer));
  165. n = recv(sockfd, buffer, BUF_SIZE, 0);
  166. if (n < 0) {
  167. fprintf(log, "Error reading from server\n");
  168. return -1;
  169. } else {
  170. fprintf(log, "Response (%d bytes): %s", n, buffer);
  171. }
  172.  
  173. memset(buffer, 0, sizeof(buffer));
  174. snprintf(buffer, BUF_SIZE, "pass %s\r\n", password);
  175. fprintf(log, "Sending (%d bytes): %s", strlen(buffer), buffer);
  176.  
  177. n = send(sockfd, buffer, strlen(buffer), 0);
  178. if (n < 0) {
  179. fprintf(log, "Error writing to server\n");
  180. return -1;
  181. }
  182.  
  183. memset(buffer, 0, sizeof(buffer));
  184. n = recv(sockfd, buffer, BUF_SIZE, 0);
  185. if (n < 0) {
  186. fprintf(log, "Error reading from server\n");
  187. return -1;
  188. } else {
  189. fprintf(log, "Response (%d bytes): %s", n, buffer);
  190. }
  191.  
  192. return 0;
  193. }
  194.  
  195. // Hence the fortress
  196. int main (int argc, char *argv[], char *envp[]) {
  197. char request[BUF_SIZE];
  198. char username[80], password[80];
  199. char buffer[BUF_SIZE];
  200.  
  201. int sockfd, portno, n;
  202. struct sockaddr_in serv_addr;
  203. struct hostent *server;
  204.  
  205. FILE *fdlog;
  206.  
  207. #ifdef WIN32
  208. WSADATA wsaData;
  209. WSAStartup(0x0101, &wsaData);
  210. #endif
  211.  
  212. fdlog = fopen("mail.log", "a+");
  213.  
  214. printf("Content-Type: text/html;charset=iso-8859-1\n\n");
  215.  
  216. get_request(request);
  217. process_request(request, username, password);
  218.  
  219. if (pop3_connect(&sockfd, "localhost", 5193) < 0)
  220. {
  221. printf("<p>Could not connect to POP3 server.</p>\n");
  222. return 0;
  223. } else {
  224. fprintf(fdlog, "--- NEW SESSION ---\n");
  225. }
  226.  
  227. //Server comm
  228. n = recv(sockfd, buffer, BUF_SIZE, 0);
  229. if (n < 0)
  230. fprintf(fdlog, "Error reading from server\n");
  231. else
  232. fprintf(fdlog, "Response (%d bytes): %s", n, buffer);
  233.  
  234. n = pop3_auth(sockfd, fdlog, username, password);
  235. if (n < 0)
  236. {
  237. fprintf(fdlog, "Wrong username or password!\n");
  238. printf("<p>Wrong username or password!</p>\n");
  239. fclose(fdlog);
  240.  
  241. #ifdef WIN32
  242. closesocket(sockfd);
  243. #else
  244. close(sockfd);
  245. #endif
  246. return 0;
  247. } else {
  248. fprintf(fdlog, "Authenticated. n = %d\n", n);
  249. }
  250.  
  251. memset(buffer, 0, sizeof(buffer));
  252. snprintf(buffer, BUF_SIZE, "retr 1\r\n");
  253. fprintf(fdlog, "Sending (%d bytes): %s", n, buffer);
  254.  
  255. n = send(sockfd, buffer, strlen(buffer), 0);
  256. if (n < 0)
  257. fprintf(fdlog, "Error writing to server\n");
  258.  
  259. memset(buffer, 0, sizeof(buffer));
  260. n = recv(sockfd, buffer, BUF_SIZE, 0);
  261. if (n < 0)
  262. fprintf(fdlog, "Error reading from server\n");
  263. else {
  264. printf("<p>Response (%d bytes):</p>\n<p>%s</p>\n", n, buffer);
  265. fprintf(fdlog, "Response (%d bytes):\n%s", n, buffer);
  266. }
  267.  
  268.  
  269. fclose(fdlog);
  270.  
  271. #ifdef WIN32
  272. closesocket(sockfd);
  273. #else
  274. close(sockfd);
  275. #endif
  276.  
  277. #ifdef WIN32
  278. WSACleanup();
  279. #endif
  280. return 0;
  281.  
  282. }
Add Comment
Please, Sign In to add comment