Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef unix
- #define WIN32
- #include <windows.h>
- #include <winsock.h>
- #include <process.h>
- #else
- #define closesocket close
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/stat.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifdef WIN32
- #pragma comment(lib, "Ws2_32.lib")
- #define snprintf _snprintf
- #endif
- #define GET_METHOD 0
- #define POST_METHOD 1
- #define NOP_STATUS -1
- #define TAG_STATUS 0
- #define VAL_STATUS 1
- #define BUF_SIZE 1024
- void print_error(void) {
- printf ("Content-Type: text/plain\r\n");
- //printf ("Content-Length: %d\r\n\r\n", 20);
- printf("Problem in REQUEST\r\n");
- return;
- }
- char *get_next_var(char *line, char *tag, char *val) {
- char *ptr;
- int status, count;
- ptr = line;
- if (*ptr)
- status = TAG_STATUS;
- count = 0;
- while(*ptr) {
- switch (*ptr) {
- case '&':
- val[count] = '\0';
- return ptr+1;
- break;
- case '=':
- tag[count] = '\0';
- count = 0;
- status = VAL_STATUS;
- break;
- default:
- if (status == TAG_STATUS) tag[count++] = *ptr;
- else val[count++] = *ptr;
- break;
- }
- ptr++;
- }
- if (status != VAL_STATUS) {
- //print_error();
- exit(0);
- }
- val[count] = '\0';
- return NULL;
- }
- // It's my party and I'll sing if I want to
- void get_request(char *result)
- {
- char *env;
- int method, contLength;
- env = getenv("REQUEST_METHOD");
- if (strcmp(env, "GET") == 0) {
- method = GET_METHOD;
- } else if (strcmp(env, "POST") == 0) {
- method = POST_METHOD;
- } else {
- print_error();
- exit(0);
- }
- if (method == GET_METHOD) {
- env = getenv("QUERY_STRING");
- strcpy(result, env);
- } else {
- env = getenv("CONTENT_LENGTH");
- contLength = atoi(env);
- fgets(result, contLength + 1, stdin);
- }
- }
- // You'll clean that up before you leave
- void process_request(char *request, char *username, char *password)
- {
- char *pline, tag[20], val[80], res[1024];
- FILE *fdlog;
- pline = request;
- while (pline) {
- pline = get_next_var(pline, tag, val);
- if (strcmp(tag, "username") == 0) { strncpy(username, val, 80);}
- if (strcmp(tag, "password") == 0) { strncpy(password, val, 80);}
- }
- fdlog = fopen("mail.log", "a+");
- fprintf (fdlog, "username: %s, password: %s\n", username, password);
- fclose(fdlog);
- }
- // Facilyce, upon waking
- int pop3_connect(int *sockfd, char *host, int portno)
- {
- struct sockaddr_in serv_addr;
- struct hostent *server;
- *sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd < 0)
- return -1;
- server = gethostbyname(host);
- if (server == NULL)
- return -1;
- memset(&serv_addr, 0, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
- serv_addr.sin_port = htons(portno);
- if (connect(*sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
- return -1;
- return 0;
- }
- // Coalescence, Inspiral, Ringdown
- int pop3_auth(int sockfd, FILE *log, char *username, char *password)
- {
- char buffer[BUF_SIZE];
- int n;
- snprintf(buffer, BUF_SIZE, "user %s\r\n", username);
- fprintf(log, "Sending (%d bytes): %s", strlen(buffer), buffer);
- n = send(sockfd, buffer, strlen(buffer), 0);
- if (n < 0) {
- fprintf(log, "Error writing to server\n");
- return -1;
- }
- memset(buffer, 0, sizeof(buffer));
- n = recv(sockfd, buffer, BUF_SIZE, 0);
- if (n < 0) {
- fprintf(log, "Error reading from server\n");
- return -1;
- } else {
- fprintf(log, "Response (%d bytes): %s", n, buffer);
- }
- memset(buffer, 0, sizeof(buffer));
- snprintf(buffer, BUF_SIZE, "pass %s\r\n", password);
- fprintf(log, "Sending (%d bytes): %s", strlen(buffer), buffer);
- n = send(sockfd, buffer, strlen(buffer), 0);
- if (n < 0) {
- fprintf(log, "Error writing to server\n");
- return -1;
- }
- memset(buffer, 0, sizeof(buffer));
- n = recv(sockfd, buffer, BUF_SIZE, 0);
- if (n < 0) {
- fprintf(log, "Error reading from server\n");
- return -1;
- } else {
- fprintf(log, "Response (%d bytes): %s", n, buffer);
- }
- return 0;
- }
- // Hence the fortress
- int main (int argc, char *argv[], char *envp[]) {
- char request[BUF_SIZE];
- char username[80], password[80];
- char buffer[BUF_SIZE];
- int sockfd, portno, n;
- struct sockaddr_in serv_addr;
- struct hostent *server;
- FILE *fdlog;
- #ifdef WIN32
- WSADATA wsaData;
- WSAStartup(0x0101, &wsaData);
- #endif
- fdlog = fopen("mail.log", "a+");
- printf("Content-Type: text/html;charset=iso-8859-1\n\n");
- get_request(request);
- process_request(request, username, password);
- if (pop3_connect(&sockfd, "localhost", 5193) < 0)
- {
- printf("<p>Could not connect to POP3 server.</p>\n");
- return 0;
- } else {
- fprintf(fdlog, "--- NEW SESSION ---\n");
- }
- //Server comm
- n = recv(sockfd, buffer, BUF_SIZE, 0);
- if (n < 0)
- fprintf(fdlog, "Error reading from server\n");
- else
- fprintf(fdlog, "Response (%d bytes): %s", n, buffer);
- n = pop3_auth(sockfd, fdlog, username, password);
- if (n < 0)
- {
- fprintf(fdlog, "Wrong username or password!\n");
- printf("<p>Wrong username or password!</p>\n");
- fclose(fdlog);
- #ifdef WIN32
- closesocket(sockfd);
- #else
- close(sockfd);
- #endif
- return 0;
- } else {
- fprintf(fdlog, "Authenticated. n = %d\n", n);
- }
- memset(buffer, 0, sizeof(buffer));
- snprintf(buffer, BUF_SIZE, "retr 1\r\n");
- fprintf(fdlog, "Sending (%d bytes): %s", n, buffer);
- n = send(sockfd, buffer, strlen(buffer), 0);
- if (n < 0)
- fprintf(fdlog, "Error writing to server\n");
- memset(buffer, 0, sizeof(buffer));
- n = recv(sockfd, buffer, BUF_SIZE, 0);
- if (n < 0)
- fprintf(fdlog, "Error reading from server\n");
- else {
- printf("<p>Response (%d bytes):</p>\n<p>%s</p>\n", n, buffer);
- fprintf(fdlog, "Response (%d bytes):\n%s", n, buffer);
- }
- fclose(fdlog);
- #ifdef WIN32
- closesocket(sockfd);
- #else
- close(sockfd);
- #endif
- #ifdef WIN32
- WSACleanup();
- #endif
- return 0;
- }
Add Comment
Please, Sign In to add comment