123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821 |
- #define _POSIX_C_SOURCE 1
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/file.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <assert.h>
- #include "Server.h"
- int getMailCounter()
- {
- FILE* test = fopen("mailcounter", "r+");
- flock(fileno(test), LOCK_EX);
- if(test == NULL)
- {
- if(creat("mailcounter", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
- {
- return 0;
- }
- test = fopen("mailcounter", "r+");
- if(test == NULL)
- {
- return 0;
- }
- }
-
- int counter;
- if(fread(&counter, sizeof(int), 1, test) == 0)
- {
- counter = -1;
- }
- counter++;
-
- rewind(test);
- if(fwrite(&counter, sizeof(int), 1, test) == 0)
- {
- return 0;
- }
-
- if(fflush(test) == EOF)
- {
- return 0;
- }
- flock(fileno(test), LOCK_UN);
- if(fclose(test) == EOF)
- {
- return 0;
- }
-
- return counter;
- }
- int isSpecialFolder(char* folder)
- {
- return strcmp(folder, ".") == 0 || strcmp(folder, "..") == 0;
- }
- Server server;
- void interruptHandler(int signal)
- {
- serverRemove(&server);
- exit(EXIT_SUCCESS);
- }
- void ensureBufferSize(char** data, int* size, int minSize)
- {
- if(minSize >= *size)
- {
- int newSize = *size;
- while(newSize <= minSize)
- {
- newSize *= 2;
- }
-
- //printf("Resize from %d to %d\n", *size, newSize);
-
- char* newData = malloc(sizeof(char) * newSize);
- memcpy(newData, *data, *size);
-
- *size = newSize;
- free(*data);
- *data = newData;
- }
- }
- void packageSendFailAnswer(int clientSocket)
- {
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 0);
- streamWriteChars(&out, "ERR\n");
- serverSend(clientSocket, &out);
- streamRemove(&out);
- }
- void packageSendOkAnswer(int clientSocket)
- {
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 0);
- streamWriteChars(&out, "OK\n");
- serverSend(clientSocket, &out);
- streamRemove(&out);
- }
- int createDirectory(char* path)
- {
- DIR* dir = opendir(path);
- if(dir == NULL)
- {
- if(mkdir(path, S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
- {
- perror("create dir fail");
- return -1;
- }
- dir = opendir(path);
- if(dir == NULL)
- {
- perror("open dir fail");
- return -1;
- }
- }
- if(closedir(dir))
- {
- perror("close dir fail");
- return -1;
- }
- if(chdir(path) == -1)
- {
- perror("change dir fail");
- return -1;
- }
- return 0;
- }
- void storeMail(char* sender, char* receiver, char* subject, char* message)
- {
- /*printf("sender: %s\n", sender);
- printf("receiver: %s\n", receiver);
- printf("subject: %s\n", subject);
- printf("message: %s\n", message);*/
-
- if(createDirectory(receiver) == -1)
- {
- return;
- }
-
- int id = getMailCounter();
-
- if(createDirectory(sender) == -1)
- {
- return;
- }
-
- char buffer[12];
- snprintf(buffer, 12, "%d", id);
-
- if(creat(buffer, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
- {
- return;
- }
-
- FILE* file = fopen(buffer, "w");
- if(file == NULL)
- {
- perror("cant open file");
- return;
- }
- if(fputs(subject, file) == EOF)
- {
- perror("cant write to file");
- return;
- }
- if(fputc('\n', file) == EOF)
- {
- perror("cant write to file");
- return;
- }
- if(fputs(message, file) == EOF)
- {
- perror("cant write to file");
- return;
- }
- if(fflush(file) == EOF)
- {
- perror("cant flush the file");
- return;
- }
- if(fclose(file) == EOF)
- {
- perror("cant close");
- return;
- }
- }
- int packageSend(int client, int clientSocket, Stream* in)
- {
- char* user = serverGetUser(&server, client);
- if(user == NULL)
- {
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- char c;
- int i;
- const char* error = "invalid SEND package\n";
-
- // receiver
- i = 0;
- char receiver[9];
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- receiver[i] = '\0';
- break;
- }
- else
- {
- if(i == 8)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- receiver[i] = c;
- i++;
- }
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- }
- if(strlen(receiver) == 0 || isSpecialFolder(receiver))
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- // subject
- i = 0;
- char subject[81];
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- subject[i] = '\0';
- break;
- }
- else
- {
- if(i == 80)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- subject[i] = c;
- i++;
- }
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- }
- if(strlen(subject) == 0)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- // message
- i = 0;
- int buffer = 16;
- char* message = malloc(sizeof(char) * buffer);
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- char n1;
- if(streamGetChar(in, &n1) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- free(message);
- return 0;
- }
- char n2;
- if(streamGetChar(in, &n2) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- free(message);
- return 0;
- }
-
- if(n1 == '.' && n2 == '\n')
- {
- ensureBufferSize(&message, &buffer, i);
- message[i] = '\0';
- break;
- }
- else
- {
- ensureBufferSize(&message, &buffer, i + 2);
- message[i] = c;
- i++;
- message[i] = n1;
- i++;
- message[i] = n2;
- i++;
- continue;
- }
- }
- ensureBufferSize(&message, &buffer, i);
- message[i] = c;
- i++;
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- free(message);
- return 0;
- }
- }
-
- char workingPath[256];
- if(getcwd(workingPath, 256) != NULL)
- {
- storeMail(user, receiver, subject, message);
- assert(chdir(workingPath) == 0);
- }
-
- free(message);
- packageSendOkAnswer(clientSocket);
- return 0;
- }
- void sendMailList(char* user, Stream* out)
- {
- DIR* receiverDir = opendir(user);
- if(receiverDir == NULL)
- {
- streamWriteInt(out, 0);
- return;
- }
- assert(chdir(user) == 0);
-
- int counterPos = streamGetPosition(out);
- streamWriteInt(out, 0);
- int counter = 0;
-
- struct dirent* receiverIterator = readdir(receiverDir);
- while(receiverIterator != NULL)
- {
- if(!isSpecialFolder(receiverIterator->d_name))
- {
- DIR* senderDir = opendir(receiverIterator->d_name);
- if(senderDir != NULL)
- {
- assert(chdir(receiverIterator->d_name) == 0);
-
- struct dirent* senderIterator = readdir(senderDir);
- while(senderIterator != NULL)
- {
- if(!isSpecialFolder(senderIterator->d_name))
- {
- // mails found
- counter++;
- // reading subject from first line
- FILE* file = fopen(senderIterator->d_name, "r");
- if(file != NULL)
- {
- int size = 16;
- int pos = 0;
- char* buffer = malloc(sizeof(char) * size);
- int data;
- while(1)
- {
- data = fgetc(file);
- if(data == '\n' || data == EOF)
- {
- ensureBufferSize(&buffer, &size, pos);
- buffer[pos] = '\0';
- break;
- }
- ensureBufferSize(&buffer, &size, pos);
- buffer[pos] = data;
- pos++;
- }
-
- streamWriteChars(out, senderIterator->d_name);
- streamWriteChars(out, " - ");
- streamWriteChars(out, buffer);
- free(buffer);
- streamWriteChar(out, '\n');
-
- if(fclose(file) == EOF)
- {
- perror("cannot close file");
- }
- }
- else
- {
- streamWriteChars(out, "ERR\n");
- }
- }
- senderIterator = readdir(senderDir);
- }
-
- assert(chdir("../") == 0);
- closedir(senderDir);
- }
- }
- receiverIterator = readdir(receiverDir);
- }
-
- int current = streamGetPosition(out);
- streamSetPosition(out, counterPos);
- streamWriteInt(out, counter);
- streamSetPosition(out, current);
-
- closedir(receiverDir);
- }
- int packageList(int client, int clientSocket, Stream* in)
- {
- char* user = serverGetUser(&server, client);
- if(user == NULL)
- {
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 1);
- char workingPath[256];
- if(getcwd(workingPath, 256) != NULL)
- {
- sendMailList(user, &out);
- assert(chdir(workingPath) == 0);
- }
- serverSend(clientSocket, &out);
- streamRemove(&out);
- return 0;
- }
- void readMail(char* user, int number, Stream* out)
- {
- DIR* receiverDir = opendir(user);
- if(receiverDir == NULL)
- {
- streamWriteChars(out, "ERR\n");
- return;
- }
- assert(chdir(user) == 0);
-
- char buffer[12];
- snprintf(buffer, 12, "%d", number);
-
- struct dirent* receiverIterator;
- receiverIterator = readdir(receiverDir);
- while(receiverIterator != NULL)
- {
- if(!isSpecialFolder(receiverIterator->d_name))
- {
- if(chdir(receiverIterator->d_name) == 0)
- {
- FILE* file = fopen(buffer, "r");
- if(file != NULL)
- {
- // skip subject heading
- int data;
- while(1)
- {
- data = fgetc(file);
- if(data == '\n' || data == EOF)
- {
- break;
- }
- }
- // write data to stream
- while(1)
- {
- data = fgetc(file);
- if(data == EOF)
- {
- break;
- }
- streamWriteChar(out, data);
- }
-
- if(fclose(file) == EOF)
- {
- perror("cannot close file");
- }
-
- if(closedir(receiverDir) == -1)
- {
- perror("cannot close receiver dir");
- }
- return;
- }
- assert(chdir("../") == 0);
- }
- }
- receiverIterator = readdir(receiverDir);
- }
-
- if(closedir(receiverDir) == -1)
- {
- perror("cannot close dir");
- }
- streamWriteChars(out, "ERR\n");
- }
- int packageRead(int client, int clientSocket, Stream* in)
- {
- char* user = serverGetUser(&server, client);
- if(user == NULL)
- {
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- char c;
- const char* error = "invalid READ package\n";
-
- int number;
- if(streamGetInt(in, &number) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- if(streamGetChar(in, &c) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- else if(c != '\n')
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 2);
- char workingPath[256];
- if(getcwd(workingPath, 256) != NULL)
- {
- readMail(user, number, &out);
- assert(chdir(workingPath) == 0);
- }
- else
- {
- streamWriteChars(&out, "ERR\n");
- }
- serverSend(clientSocket, &out);
- streamRemove(&out);
-
- return 0;
- }
- int deleteMail(char* user, int number)
- {
- DIR* receiverDir = opendir(user);
- if(receiverDir == NULL)
- {
- return 0;
- }
- assert(chdir(user) == 0);
-
- char buffer[12];
- snprintf(buffer, 12, "%d", number);
-
- int rValue = 0;
-
- struct dirent* receiverIterator;
- receiverIterator = readdir(receiverDir);
- while(receiverIterator != NULL)
- {
- if(!isSpecialFolder(receiverIterator->d_name))
- {
- if(chdir(receiverIterator->d_name) == 0)
- {
- if(remove(buffer) == 0)
- {
- rValue = 1;
- break;
- }
- assert(chdir("../") == 0);
- }
- }
- receiverIterator = readdir(receiverDir);
- }
-
- if(closedir(receiverDir) == -1)
- {
- perror("cannot close dir");
- }
- return rValue;
- }
- int packageDelete(int client, int clientSocket, Stream* in)
- {
- char* user = serverGetUser(&server, client);
- if(user == NULL)
- {
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- char c;
- const char* error = "invalid DELETE package\n";
-
- int number;
- if(streamGetInt(in, &number) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- if(streamGetChar(in, &c) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- else if(c != '\n')
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 2);
- char workingPath[256];
- if(getcwd(workingPath, 256) != NULL)
- {
- if(deleteMail(user, number))
- {
- streamWriteChars(&out, "OK\n");
- }
- else
- {
- streamWriteChars(&out, "ERR\n");
- }
- assert(chdir(workingPath) == 0);
- }
- else
- {
- streamWriteChars(&out, "ERR\n");
- }
- serverSend(clientSocket, &out);
- streamRemove(&out);
- return 0;
- }
- int packageQuit(int client, int clientSocket, Stream* in)
- {
- printf("%d wants to disconnect ...\n", client);
- return 1;
- }
- int packageLogin(int client, int clientSocket, Stream* in)
- {
- printf("%d wants to login ...\n", client);
-
- char c;
- int i;
- const char* error = "invalid DELETE package\n";
-
- // user
- i = 0;
- char user[9];
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- user[i] = '\0';
- break;
- }
- else
- {
- if(i == 8)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- user[i] = c;
- i++;
- }
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- }
- if(strlen(user) == 0)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- // password
- i = 0;
- int buffer = 16;
- char* password = malloc(sizeof(char) * buffer);
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- ensureBufferSize(&password, &buffer, i);
- password[i] = '\0';
- break;
- }
- ensureBufferSize(&password, &buffer, i);
- password[i] = c;
- i++;
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- free(password);
- return 0;
- }
- }
-
- printf("%s %s\n", user, password);
- // insert LDAP check here
-
- if(serverSetUser(&server, client, user))
- {
- packageSendFailAnswer(clientSocket);
- free(password);
- return 0;
- }
- free(password);
- packageSendOkAnswer(clientSocket);
- return 0;
- }
- int main(int argc, char** argv)
- {
- if(argc < 3)
- {
- printf("Usage: %s directory port\n", argv[0]);
- return EXIT_FAILURE;
- }
-
- int port = atoi(argv[2]);
- if(port <= 0 || port >= 65536)
- {
- printf("invalid port");
- return EXIT_FAILURE;
- }
- serverInitDefaults(&server);
-
- signal(SIGINT, interruptHandler);
- signal(SIGKILL, interruptHandler);
-
- if(serverInit(&server, 3, port, argv[1]) == -1)
- {
- return EXIT_FAILURE;
- }
-
- serverRegisterHandler(&server, packageSend);
- serverRegisterHandler(&server, packageList);
- serverRegisterHandler(&server, packageRead);
- serverRegisterHandler(&server, packageDelete);
- serverRegisterHandler(&server, packageQuit);
- serverRegisterHandler(&server, packageLogin);
-
- serverWaitForConnection(&server);
-
- serverRemove(&server);
- return EXIT_SUCCESS;
- }
|