123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740 |
- #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"
- #include "String.h"
- #include "LDAP.h"
- #include <string.h>
- int getMailCounter()
- {
- FILE* test = fopen("mailcounter", "r+");
- 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;
- }
- }
- flock(fileno(test), LOCK_EX);
-
- 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 ensureBufferIndex(char** data, int* size, int index)
- {
- if(index >= *size)
- {
- int newSize = *size;
- while(newSize <= index)
- {
- newSize *= 2;
- }
-
- 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);
- streamWriteChars(&out, "ERR\n");
- serverSend(clientSocket, &out);
- streamRemove(&out);
- }
- void packageSendOkAnswer(int clientSocket)
- {
- Stream out;
- streamInit(&out, 16);
- 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)
- {
- 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(sender, file) == EOF)
- {
- perror("cant write to file");
- return;
- }
- if(fputc('\n', file) == EOF)
- {
- perror("cant write to file");
- return;
- }
- if(fputs(receiver, file) == EOF)
- {
- perror("cant write to file");
- return;
- }
- if(fputc('\n', file) == EOF)
- {
- perror("cant write to 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;
- }
-
- int length;
-
- // receiver
- String receiver;
- stringInit(&receiver);
- stringReadStreamLine(&receiver, in);
- length = stringGetLength(&receiver);
- if(length == 0 || length > 8)
- {
- packageSendFailAnswer(clientSocket);
- stringRemove(&receiver);
- return 0;
- }
-
- // subject
- String subject;
- stringInit(&subject);
- stringReadStreamLine(&subject, in);
- length = stringGetLength(&subject);
- if(length == 0 || length > 80)
- {
- packageSendFailAnswer(clientSocket);
- stringRemove(&subject);
- return 0;
- }
-
- // message
- int index = 0;
- int buffer = 16;
- char* message = malloc(sizeof(char) * buffer);
-
- String sMessage;
- stringInit(&sMessage);
- while(streamHasData(in))
- {
- stringReadStreamLine(&sMessage, in);
- if(stringCompare(&sMessage, ".") || buffer > 60)
- {
- break;
- }
- int length = stringGetLength(&sMessage);
- // "i + length" is one too much but right for "length + \n"
- ensureBufferIndex(&message, &buffer, index + length);
- memcpy(message + index, sMessage.data, length);
- index += length;
- message[index] = '\n';
- index++;
- }
- stringRemove(&sMessage);
-
- char workingPath[256];
- if(getcwd(workingPath, 256) != NULL)
- {
- storeMail(user, receiver.data, subject.data, message);
- assert(chdir(workingPath) == 0);
- }
-
- stringRemove(&receiver);
- stringRemove(&subject);
- free(message);
- packageSendOkAnswer(clientSocket);
- return 0;
- }
- void sendMailList(char* user, Stream* out)
- {
- DIR* receiverDir = opendir(user);
- if(receiverDir == NULL)
- {
- streamWriteChars(out, "0\n");
- return;
- }
- assert(chdir(user) == 0);
-
- // count mails first, this is easier than buffering everything else
- 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++;
- }
- senderIterator = readdir(senderDir);
- }
-
- assert(chdir("../") == 0);
- closedir(senderDir);
- }
- }
- receiverIterator = readdir(receiverDir);
- }
-
- // write the number of mails
- char buffer[12];
- snprintf(buffer, 12, "%d", counter);
- streamWriteChars(out, buffer);
- streamWriteChar(out, '\n');
-
- // add the mail subjects
- assert(chdir("../") == 0);
- closedir(receiverDir);
- receiverDir = opendir(user);
- if(receiverDir == NULL)
- {
- return;
- }
- assert(chdir(user) == 0);
-
- 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))
- {
- // reading subject from first line
- FILE* file = fopen(senderIterator->d_name, "r");
- if(file != NULL)
- {
- streamWriteChars(out, senderIterator->d_name);
- streamWriteChars(out, " - ");
-
- //skip first 2 lines (receiver, sender)
- int skip = 0;
- int data;
- while(1)
- {
- data = fgetc(file);
- if(data == '\n' || data == EOF)
- {
- skip++;
- if(skip == 2)
- {
- break;
- }
- }
- }
-
- while(1)
- {
- data = fgetc(file);
- if(data == '\n' || data == EOF)
- {
- break;
- }
- streamWriteChar(out, data);
- }
- 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);
- }
- 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);
- 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, char* number, Stream* out)
- {
- DIR* receiverDir = opendir(user);
- if(receiverDir == NULL)
- {
- streamWriteChars(out, "ERR\n");
- return;
- }
- assert(chdir(user) == 0);
-
- struct dirent* receiverIterator;
- receiverIterator = readdir(receiverDir);
- while(receiverIterator != NULL)
- {
- if(!isSpecialFolder(receiverIterator->d_name))
- {
- if(chdir(receiverIterator->d_name) == 0)
- {
- FILE* file = fopen(number, "r");
- if(file != NULL)
- {
- streamWriteChars(out, "OK\n");
- // write data to stream
- int data;
- while(1)
- {
- data = fgetc(file);
- if(data == EOF)
- {
- break;
- }
- streamWriteChar(out, data);
- }
- streamWriteChars(out, ".\n");
-
- 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;
- }
-
- // number
- String number;
- stringInit(&number);
- stringReadStreamLine(&number, in);
- if(stringGetLength(&number) == 0)
- {
- packageSendFailAnswer(clientSocket);
- stringRemove(&number);
- return 0;
- }
-
- Stream out;
- streamInit(&out, 16);
- char workingPath[256];
- if(getcwd(workingPath, 256) != NULL)
- {
- readMail(user, number.data, &out);
- assert(chdir(workingPath) == 0);
- }
- else
- {
- streamWriteChars(&out, "ERR\n");
- }
- stringRemove(&number);
- serverSend(clientSocket, &out);
- streamRemove(&out);
- return 0;
- }
- int deleteMail(char* user, char* number)
- {
- DIR* receiverDir = opendir(user);
- if(receiverDir == NULL)
- {
- return 0;
- }
- assert(chdir(user) == 0);
-
- 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(number) == 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;
- }
-
- // number
- String number;
- stringInit(&number);
- stringReadStreamLine(&number, in);
- if(stringGetLength(&number) == 0)
- {
- packageSendFailAnswer(clientSocket);
- stringRemove(&number);
- return 0;
- }
-
- Stream out;
- streamInit(&out, 16);
- char workingPath[256];
- if(getcwd(workingPath, 256) != NULL)
- {
- if(deleteMail(user, number.data))
- {
- streamWriteChars(&out, "OK\n");
- }
- else
- {
- streamWriteChars(&out, "ERR\n");
- }
- assert(chdir(workingPath) == 0);
- }
- else
- {
- streamWriteChars(&out, "ERR\n");
- }
- stringRemove(&number);
- serverSend(clientSocket, &out);
- streamRemove(&out);
- return 0;
- }
- int packageLogin(int client, int clientSocket, Stream* in)
- {
- int length;
-
- // receiver
- String user;
- stringInit(&user);
- stringReadStreamLine(&user, in);
- length = stringGetLength(&user);
- if(length == 0 || length > 8)
- {
- packageSendFailAnswer(clientSocket);
- stringRemove(&user);
- return 0;
- }
-
- // password
- String password;
- stringInit(&password);
- stringReadStreamLine(&password, in);
-
- if(!ldapTryLogin(user.data, password.data))
- {
- stringRemove(&user);
- stringRemove(&password);
-
- if(serverRaiseLoginFails(&server, client))
- {
- serverBann(&server, client);
- return 1;
- }
-
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- stringRemove(&password);
- serverResetLoginFails(&server, client);
-
- if(serverSetUser(&server, client, user.data))
- {
- packageSendFailAnswer(clientSocket);
- stringRemove(&user);
- return 0;
- }
- packageSendOkAnswer(clientSocket);
- stringRemove(&user);
- return 0;
- }
- int package(int client, int clientSocket, Stream* in)
- {
- if(streamHasData(in))
- {
- String s;
- stringInit(&s);
- stringReadStreamLine(&s, in);
- int r = 0;
- if(stringCompare(&s, "SEND"))
- {
- r = packageSend(client, clientSocket, in);
- }
- else if(stringCompare(&s, "LIST"))
- {
- r = packageList(client, clientSocket, in);
- }
- else if(stringCompare(&s, "READ"))
- {
- r = packageRead(client, clientSocket, in);
- }
- else if(stringCompare(&s, "DEL"))
- {
- r = packageDelete(client, clientSocket, in);
- }
- else if(stringCompare(&s, "LOGIN"))
- {
- r = packageLogin(client, clientSocket, in);
- }
- else
- {
- printf("Invalid package from %d\n", client);
- r = 1;
- }
- stringRemove(&s);
- return r;
- }
- 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, package);
-
- serverWaitForConnection(&server);
-
- serverRemove(&server);
- return EXIT_SUCCESS;
- }
|