Browse Source

bug fixes, dummy login handling (no ldap)

Kajetan Johannes Hammerle 5 years ago
parent
commit
64fbe684e0

+ 77 - 75
ClientMain.c

@@ -14,6 +14,11 @@ void interruptHandler(int signal)
     exit(EXIT_SUCCESS);
 }
 
+int isSpecialFolder(char* folder)
+{
+    return strcmp(folder, ".") == 0 || strcmp(folder, "..") == 0;
+}
+
 int readNumber(const char* text)
 {
     while(1)
@@ -39,12 +44,12 @@ void waitForData()
 {
     while(1)
     {
-        printf("Type a command: ");
+        printf("Type a command:\n");
         String s;
         stringInit(&s);
         stringRead(&s);
         
-        if(stringCompare(&s, "SEND"))
+        if(stringCompareNoCase(&s, "SEND"))
         {
             printf("Sending a mail to the server...\n");
             
@@ -53,31 +58,13 @@ void waitForData()
             streamInit(&out, 128);
             streamWriteInt(&out, 0);
             
-            // sender
-            printf("Type a sender with at most 8 characters: ");
-            String sender;
-            stringInit(&sender);
-            stringRead(&sender);
-            int l = stringGetLength(&sender);
-            while(l < 1 || l > 8)
-            {
-                printf("Please type a valid sender: ");
-                stringRemove(&sender);
-                stringInit(&sender);
-                stringRead(&sender);
-                l = stringGetLength(&sender);
-            }
-            streamWriteChars(&out, sender.data);
-            streamWriteChar(&out, '\n');
-            stringRemove(&sender);
-            
             // receiver
             printf("Type a receiver with at most 8 characters: ");
             String receiver;
             stringInit(&receiver);
             stringRead(&receiver);
-            l = stringGetLength(&receiver);
-            while(l < 1 || l > 8)
+            int l = stringGetLength(&receiver);
+            while(l < 1 || l > 8 || isSpecialFolder(receiver.data))
             {
                 printf("Please type a valid receiver: ");
                 stringRemove(&receiver);
@@ -97,7 +84,7 @@ void waitForData()
             l = stringGetLength(&subject);
             while(l < 1 || l > 80)
             {
-                printf("Please type a valid receiver: ");
+                printf("Please type a valid subject: ");
                 stringRemove(&subject);
                 stringInit(&subject);
                 stringRead(&subject);
@@ -120,59 +107,39 @@ void waitForData()
             clientSendData(&client, &out);
             streamRemove(&out);
         }
-        else if(stringCompare(&s, "LIST"))
+        else if(stringCompareNoCase(&s, "LIST"))
         {
             //outgoing stream
             Stream out;
             streamInit(&out, 16);
             streamWriteInt(&out, 1);
             
-            // user
-            printf("Type a user with at most 8 characters: ");
-            String sender;
-            stringInit(&sender);
-            stringRead(&sender);
-            int l = stringGetLength(&sender);
-            while(l < 1 || l > 8)
-            {
-                printf("Please type a valid user: ");
-                stringRemove(&sender);
-                stringInit(&sender);
-                stringRead(&sender);
-                l = stringGetLength(&sender);
-            }
-            streamWriteChars(&out, sender.data);
-            streamWriteChar(&out, '\n');
-            stringRemove(&sender);
-            
             // send and clear stream
             clientSendData(&client, &out);
             streamRemove(&out);
         }
-        else if(stringCompare(&s, "READ"))
+        else if(stringCompareNoCase(&s, "READ"))
         {
             //outgoing stream
             Stream out;
             streamInit(&out, 16);
             streamWriteInt(&out, 2);
             
-            // user
-            printf("Type a user with at most 8 characters: ");
-            String sender;
-            stringInit(&sender);
-            stringRead(&sender);
-            int l = stringGetLength(&sender);
-            while(l < 1 || l > 8)
-            {
-                printf("Please type a valid user: ");
-                stringRemove(&sender);
-                stringInit(&sender);
-                stringRead(&sender);
-                l = stringGetLength(&sender);
-            }
-            streamWriteChars(&out, sender.data);
+            // message number
+            int number = readNumber("Type a message number: ");
+            streamWriteInt(&out, number);
             streamWriteChar(&out, '\n');
-            stringRemove(&sender);
+            
+            // send and clear stream
+            clientSendData(&client, &out);
+            streamRemove(&out);
+        }
+        else if(stringCompareNoCase(&s, "DEL"))
+        {
+            //outgoing stream
+            Stream out;
+            streamInit(&out, 16);
+            streamWriteInt(&out, 3);
             
             // message number
             int number = readNumber("Type a message number: ");
@@ -183,12 +150,29 @@ void waitForData()
             clientSendData(&client, &out);
             streamRemove(&out);
         }
-        else if(stringCompare(&s, "DEL"))
+        else if(stringCompareNoCase(&s, "QUIT"))
+        {
+            Stream out;
+            streamInit(&out, 16);
+            streamWriteInt(&out, 4);
+            clientSendData(&client, &out);
+            streamRemove(&out);
+        }
+        else if(stringCompareNoCase(&s, "HELP"))
+        {
+            printf("- LOGIN: log in at the server\n");
+            printf("- SEND: sends a message to a receiver\n");
+            printf("- LIST: shows all your mails\n");
+            printf("- READ: read a mail\n");
+            printf("- DEL: delete a mail\n");
+            printf("- QUIT: disconnects your from the server\n");
+        }
+        else if(stringCompareNoCase(&s, "LOGIN"))
         {
             //outgoing stream
             Stream out;
             streamInit(&out, 16);
-            streamWriteInt(&out, 3);
+            streamWriteInt(&out, 5);
             
             // user
             printf("Type a user with at most 8 characters: ");
@@ -208,23 +192,28 @@ void waitForData()
             streamWriteChar(&out, '\n');
             stringRemove(&sender);
             
-            // message number
-            int number = readNumber("Type a message number: ");
-            streamWriteInt(&out, number);
+            // password
+            printf("Type a password: ");
+            String password;
+            stringInit(&password);
+            stringRead(&password);
+            l = stringGetLength(&password);
+            while(l == 0)
+            {
+                printf("Please type a valid password: ");
+                stringRemove(&password);
+                stringInit(&password);
+                stringRead(&password);
+                l = stringGetLength(&password);
+            }
+            streamWriteChars(&out, password.data);
             streamWriteChar(&out, '\n');
+            stringRemove(&sender);
             
             // send and clear stream
             clientSendData(&client, &out);
             streamRemove(&out);
         }
-        else if(stringCompare(&s, "QUIT"))
-        {
-            Stream out;
-            streamInit(&out, 16);
-            streamWriteInt(&out, 4);
-            clientSendData(&client, &out);
-            streamRemove(&out);
-        }
         
         stringRemove(&s);
     }
@@ -235,6 +224,8 @@ void packageAnswer(Stream* in)
     char buffer[32];
     streamGetChars(in, buffer, 32);
     printf("%s\n", buffer);
+    
+    printf("Type a command:\n");
 }
 
 void packageList(Stream* in)
@@ -259,6 +250,8 @@ void packageList(Stream* in)
         fputc(c, stdout);
     }
     fputc('\n', stdout);
+    
+    printf("Type a command: \n");
 }
 
 void packageReadDel(Stream* in)
@@ -269,13 +262,22 @@ void packageReadDel(Stream* in)
         fputc(c, stdout);
     }
     fputc('\n', stdout);
+    
+    printf("Type a command:\n");
 }
 
 int main(int argc, char** argv)
 {
-    if(argc < 2)
+    if(argc < 3)
+    {
+        printf("Usage: %s server_address port\n", argv[0]);
+        return EXIT_FAILURE;
+    }
+    
+    int port = atoi(argv[2]);
+    if(port <= 0 || port >= 65536)
     {
-        printf("Usage: %s server_address\n", argv[0]);
+        printf("invalid port");
         return EXIT_FAILURE;
     }
     
@@ -284,7 +286,7 @@ int main(int argc, char** argv)
     signal(SIGINT, interruptHandler);
     signal(SIGKILL, interruptHandler);
     
-    if(clientInit(&client, argv[1], 6543) == -1)
+    if(clientInit(&client, argv[1], port) == -1)
     {
         return EXIT_FAILURE;
     }

+ 59 - 1
Server.c

@@ -16,6 +16,7 @@ void serverInitDefaults(Server* s)
     s->port = -1;
     s->threads = NULL;
     s->clientSockets = NULL;
+    s->clientUser = NULL;
     s->connectSocket = -1;
     
     s->hAmount = -1;
@@ -43,11 +44,13 @@ int serverInit(Server* s, int maxClients, short port, char* directory)
     // initialize storage for clients
     s->threads = malloc(sizeof(pthread_t) * maxClients);
     s->clientSockets = malloc(sizeof(int) * maxClients);
+    s->clientUser = malloc(sizeof(char*) * maxClients);
     
     for(int i = 0; i < maxClients; i++)
     {
         s->threads[i] = -1;
         s->clientSockets[i] = -1;
+        s->clientUser[i] = NULL;
     }
     
     // create the socket for clients to connect
@@ -135,6 +138,20 @@ void serverRemove(Server* s)
         s->clientSockets = NULL;
     }
     
+    if(s->clientUser != NULL)
+    {
+        for(int i = 0; i < s->maxClients; i++)
+        {
+            if(s->clientUser[i] != NULL)
+            {
+                free(s->clientUser[i]);
+                s->clientUser[i] = NULL;
+            }
+        }
+        free(s->clientUser);
+        s->clientUser = NULL;
+    }
+    
     s->maxClients = -1;
     
     if(s->connectSocket != -1)
@@ -217,6 +234,7 @@ static void* clientHandler(void* data)
         printf("%d closed\n", id);
     }
     
+    serverFreeUser(s, id);
     s->clientSockets[id] = -1;
     return NULL;
 }
@@ -275,7 +293,13 @@ void serverWaitForConnection(Server* s)
                         Stream out;
                         streamInit(&out, 64);
                         streamWriteChar(&out, 1);
-                        streamWriteChars(&out, "Welcome to the server, please enter your command:\n");
+                        streamWriteChars(&out, "Welcome to the server, please enter one of these commands:\n");
+                        streamWriteChars(&out, "- HELP\n");
+                        streamWriteChars(&out, "- SEND\n");
+                        streamWriteChars(&out, "- LIST\n");
+                        streamWriteChars(&out, "- READ\n");
+                        streamWriteChars(&out, "- DEL\n");
+                        streamWriteChars(&out, "- QUIT\n");
                         if(serverSend(clientSocket, &out) == -1)
                         {
                             perror("Cannot send welcome message");
@@ -329,4 +353,38 @@ void serverRegisterHandler(Server* s, ServerStreamFunction f)
     }
     s->handlers[s->hIndex] = f;
     s->hIndex++;
+}
+
+int serverSetUser(Server* s, int clientId, char* name)
+{
+    if(clientId < 0 || clientId >= s->maxClients)
+    {
+        return -1;
+    }
+    int l = strlen(name) + 1;
+    s->clientUser[clientId] = malloc(sizeof(char) * l);
+    strncpy(s->clientUser[clientId], name, l);
+    return 0;
+}
+
+char* serverGetUser(Server* s, int clientId)
+{
+    if(clientId < 0 || clientId >= s->maxClients)
+    {
+        return NULL;
+    }
+    return s->clientUser[clientId];
+}
+
+void serverFreeUser(Server* s, int clientId)
+{
+    if(clientId < 0 || clientId >= s->maxClients)
+    {
+        return;
+    }
+    if(s->clientUser[clientId] != NULL)
+    {
+        free(s->clientUser[clientId]);
+        s->clientUser[clientId] = NULL;
+    }
 }

+ 5 - 0
Server.h

@@ -10,6 +10,7 @@ typedef struct Server
     short port;
     pthread_t* threads;
     int* clientSockets;
+    char** clientUser;
     int connectSocket;
     
     int hAmount;
@@ -34,5 +35,9 @@ void serverWaitForConnection(Server* s);
 
 void serverRegisterHandler(Server* s, ServerStreamFunction f);
 
+int serverSetUser(Server* s, int clientId, char* name);
+char* serverGetUser(Server* s, int clientId);
+void serverFreeUser(Server* s, int clientId);
+
 #endif
 

+ 221 - 300
ServerMain.c

@@ -1,3 +1,4 @@
+#define	_POSIX_C_SOURCE 1
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -6,6 +7,7 @@
 #include <dirent.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/file.h>
 #include <fcntl.h>
 #include <dirent.h>
 #include <assert.h>
@@ -14,6 +16,7 @@
 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)
@@ -44,6 +47,7 @@ int getMailCounter()
     {
         return 0;
     }
+    flock(fileno(test), LOCK_UN);
     if(fclose(test) == EOF)
     {
         return 0;
@@ -158,19 +162,25 @@ void storeMail(char* sender, char* receiver, char* subject, char* message)
     char buffer[12];
     snprintf(buffer, 12, "%d", id);
     
-    if(createDirectory(buffer) == -1)
+    if(creat(buffer, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
     {
         return;
     }
     
-    if(creat(subject, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
+    FILE* file = fopen(buffer, "w");
+    if(file == NULL)
     {
+        perror("cant open file");
         return;
     }
-    FILE* file = fopen(subject, "w");
-    if(file == NULL)
+    if(fputs(subject, file) == EOF)
     {
-        perror("cant open file");
+        perror("cant write to file");
+        return;
+    }
+    if(fputc('\n', file) == EOF)
+    {
+        perror("cant write to file");
         return;
     }
     if(fputs(message, file) == EOF)
@@ -192,48 +202,17 @@ void storeMail(char* sender, char* receiver, char* subject, char* message)
 
 int packageSend(int client, int clientSocket, Stream* in)
 {
-    char c;
-    int i;
-    const char* error = "invalid SEND package\n";
-    
-    // sender
-    i = 0;
-    char sender[9];
-    while(1)
-    {
-        if(streamGetChar(in, &c) != -1)
-        {
-            if(c == '\n')
-            {
-                sender[i] = '\0';
-                break;
-            }
-            else
-            {
-                if(i == 8)
-                {
-                    printf("%s", error);
-                    packageSendFailAnswer(clientSocket);
-                    return 0;
-                }
-                sender[i] = c;
-                i++;
-            }
-        }
-        else
-        {
-            printf("%s", error);
-            packageSendFailAnswer(clientSocket);
-            return 0;
-        }
-    }
-    if(strlen(sender) == 0)
+    char* user = serverGetUser(&server, client);
+    if(user == NULL)
     {
-        printf("%s", error);
         packageSendFailAnswer(clientSocket);
         return 0;
     }
     
+    char c;
+    int i;
+    const char* error = "invalid SEND package\n";
+      
     // receiver
     i = 0;
     char receiver[9];
@@ -265,7 +244,7 @@ int packageSend(int client, int clientSocket, Stream* in)
             return 0;
         }
     }
-    if(strlen(receiver) == 0)
+    if(strlen(receiver) == 0 || isSpecialFolder(receiver))
     {
         printf("%s", error);
         packageSendFailAnswer(clientSocket);
@@ -371,7 +350,7 @@ int packageSend(int client, int clientSocket, Stream* in)
     char workingPath[256];
     if(getcwd(workingPath, 256) != NULL)
     {
-        storeMail(sender, receiver, subject, message);
+        storeMail(user, receiver, subject, message);
         assert(chdir(workingPath) == 0);
     }
             
@@ -383,8 +362,8 @@ int packageSend(int client, int clientSocket, Stream* in)
 
 void sendMailList(char* user, Stream* out)
 {
-    DIR* dir = opendir(user);
-    if(dir == NULL)
+    DIR* receiverDir = opendir(user);
+    if(receiverDir == NULL)
     {
         streamWriteInt(out, 0);
         return;
@@ -395,48 +374,69 @@ void sendMailList(char* user, Stream* out)
     streamWriteInt(out, 0);
     int counter = 0;
     
-    struct dirent* pDir = readdir(dir);
-    while(pDir != NULL)
+    struct dirent* receiverIterator = readdir(receiverDir);
+    while(receiverIterator != NULL)
     {
-        if(strcmp(pDir->d_name, ".") != 0 && strcmp(pDir->d_name, "..") != 0)
+        if(!isSpecialFolder(receiverIterator->d_name))
         {
-            DIR* innerdir = opendir(pDir->d_name);
-            if(innerdir != NULL)
+            DIR* senderDir = opendir(receiverIterator->d_name);
+            if(senderDir != NULL)
             {
-                assert(chdir(pDir->d_name) == 0);
+                assert(chdir(receiverIterator->d_name) == 0);
                 
-                struct dirent* idDir = readdir(innerdir);
-                while(idDir != NULL)
+                struct dirent* senderIterator = readdir(senderDir);
+                while(senderIterator != NULL)
                 {
-                    if(strcmp(idDir->d_name, ".") != 0 && strcmp(idDir->d_name, "..") != 0)
+                    if(!isSpecialFolder(senderIterator->d_name))
                     {
+                        // mails found
                         counter++;
-                        DIR* innerIdDir = opendir(idDir->d_name);
-                        if(innerIdDir != NULL)
+                        // reading subject from first line
+                        FILE* file = fopen(senderIterator->d_name, "r");
+                        if(file != NULL)
                         {
-                            struct dirent* mails = readdir(innerIdDir);
-                            while(mails != NULL)
+                            int size = 16;
+                            int pos = 0;
+                            char* buffer = malloc(sizeof(char) * size);
+                            int data;
+                            while(1)
                             {
-                                if(strcmp(mails->d_name, ".") != 0 && strcmp(mails->d_name, "..") != 0)
+                                data = fgetc(file);
+                                if(data == '\n' || data == EOF)
                                 {
-                                    streamWriteChars(out, idDir->d_name);
-                                    streamWriteChars(out, " - ");
-                                    streamWriteChars(out, mails->d_name);
-                                    streamWriteChar(out, '\n');
+                                    ensureBufferSize(&buffer, &size, pos);
+                                    buffer[pos] = '\0';
+                                    break;
                                 }
-                                mails = readdir(innerIdDir);
+                                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");
                             }
-                            closedir(innerIdDir);
+                        }
+                        else
+                        {
+                            streamWriteChars(out, "ERR\n");
                         }
                     }
-                    idDir = readdir(innerdir);
+                    senderIterator = readdir(senderDir);
                 }
                 
                 assert(chdir("../") == 0);
-                closedir(innerdir);
+                closedir(senderDir);
             }
         }
-        pDir = readdir(dir);
+        receiverIterator = readdir(receiverDir);
     }
     
     int current = streamGetPosition(out);
@@ -444,49 +444,14 @@ void sendMailList(char* user, Stream* out)
     streamWriteInt(out, counter);
     streamSetPosition(out, current);
     
-    closedir(dir);
+    closedir(receiverDir);
 }
 
 int packageList(int client, int clientSocket, Stream* in)
 {
-    char c;
-    int i;
-    const char* error = "invalid LIST package\n";
-    
-    // user
-    i = 0;
-    char user[9];
-    while(1)
+    char* user = serverGetUser(&server, client);
+    if(user == NULL)
     {
-        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;
     }
@@ -507,8 +472,8 @@ int packageList(int client, int clientSocket, Stream* in)
 
 void readMail(char* user, int number, Stream* out)
 {
-    DIR* dir = opendir(user);
-    if(dir == NULL)
+    DIR* receiverDir = opendir(user);
+    if(receiverDir == NULL)
     {
         streamWriteChars(out, "ERR\n");
         return;
@@ -518,83 +483,56 @@ void readMail(char* user, int number, Stream* out)
     char buffer[12];
     snprintf(buffer, 12, "%d", number);
     
-    struct dirent* senders;
-    senders = readdir(dir);
-    while(senders != NULL)
+    struct dirent* receiverIterator;
+    receiverIterator = readdir(receiverDir);
+    while(receiverIterator != NULL)
     {
-        if(strcmp(senders->d_name, ".") != 0 && strcmp(senders->d_name, "..") != 0)
+        if(!isSpecialFolder(receiverIterator->d_name))
         {
-            if(chdir(senders->d_name) == 0)
+            if(chdir(receiverIterator->d_name) == 0)
             {
-                DIR* innerDir = opendir(buffer);
-                if(innerDir != NULL)
+                FILE* file = fopen(buffer, "r");
+                if(file != NULL)
                 {
-                    struct dirent* mail;
-                    mail = readdir(innerDir);
-                    while(mail != NULL && (strcmp(mail->d_name, ".") == 0 || strcmp(mail->d_name, "..") == 0))
-                    {
-                        mail = readdir(innerDir);
-                    }
-                    
-                    if(mail != NULL)
+                    // skip subject heading
+                    int data;
+                    while(1)
                     {
-                        assert(chdir(buffer) == 0);
-                        FILE* file = fopen(mail->d_name, "r");
-                        if(file != NULL)
+                        data = fgetc(file);
+                        if(data == '\n' || data == EOF)
                         {
-                            struct stat stats;
-                            if(stat(mail->d_name, &stats) == 0)
-                            {
-                                char* filebuffer = malloc(sizeof(char) * (stats.st_size + 1));
-                                if(fgets(filebuffer, stats.st_size + 1, file) != NULL)
-                                {
-                                    streamWriteChars(out, "OK\n");
-                                    streamWriteChars(out, filebuffer);
-                                    streamWriteChar(out, '\n');
-                                }
-                                else
-                                {
-                                    streamWriteChars(out, "ERR\n");
-                                }
-                            }
-                            else
-                            {
-                                streamWriteChars(out, "ERR\n");
-                            }
-                            
-                            if(fclose(file) == EOF)
-                            {
-                                perror("cannot close file");
-                            }
-                        }
-                        else
-                        {
-                            streamWriteChars(out, "ERR\n");
+                            break;
                         }
                     }
-                    else
+                    // write data to stream
+                    while(1)
                     {
-                        streamWriteChars(out, "ERR\n");
+                        data = fgetc(file);
+                        if(data == EOF)
+                        {
+                            break;
+                        }
+                        streamWriteChar(out, data);
                     }
                     
-                    if(closedir(innerDir) == -1)
+                    if(fclose(file) == EOF)
                     {
-                        perror("cannot close innerDir");
+                        perror("cannot close file");
                     }
-                    if(closedir(dir) == -1)
+                    
+                    if(closedir(receiverDir) == -1)
                     {
-                        perror("cannot close dir");
+                        perror("cannot close receiver dir");
                     }
                     return;
                 }
-                
                 assert(chdir("../") == 0);
             }
         }
-        senders = readdir(dir);
+        receiverIterator = readdir(receiverDir);
     }
     
-    if(closedir(dir) == -1)
+    if(closedir(receiverDir) == -1)
     {
         perror("cannot close dir");
     }
@@ -603,48 +541,16 @@ void readMail(char* user, int number, Stream* out)
 
 int packageRead(int client, int clientSocket, Stream* in)
 {
-    char c;
-    int i;
-    const char* error = "invalid READ package\n";
-    
-    // user
-    i = 0;
-    char user[9];
-    while(1)
+    char* user = serverGetUser(&server, client);
+    if(user == NULL)
     {
-        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;
     }
     
+    char c;
+    const char* error = "invalid READ package\n";
+    
     int number;
     if(streamGetInt(in, &number) == -1)
     {
@@ -687,8 +593,8 @@ int packageRead(int client, int clientSocket, Stream* in)
 
 int deleteMail(char* user, int number)
 {
-    DIR* dir = opendir(user);
-    if(dir == NULL)
+    DIR* receiverDir = opendir(user);
+    if(receiverDir == NULL)
     {
         return 0;
     }
@@ -699,74 +605,26 @@ int deleteMail(char* user, int number)
     
     int rValue = 0;
     
-    struct dirent* senders;
-    senders = readdir(dir);
-    while(senders != NULL)
+    struct dirent* receiverIterator;
+    receiverIterator = readdir(receiverDir);
+    while(receiverIterator != NULL)
     {
-        if(strcmp(senders->d_name, ".") != 0 && strcmp(senders->d_name, "..") != 0)
+        if(!isSpecialFolder(receiverIterator->d_name))
         {
-            if(chdir(senders->d_name) == 0)
+            if(chdir(receiverIterator->d_name) == 0)
             {
-                DIR* innerDir = opendir(buffer);
-                if(innerDir != NULL)
+                if(remove(buffer) == 0)
                 {
-                    if(chdir(buffer) == -1)
-                    {
-                        perror("cannot change dir");
-                        if(closedir(innerDir) == -1)
-                        {
-                            perror("cannot close innerDir");
-                        }
-                        break;
-                    }
-                    
                     rValue = 1;
-                    
-                    struct dirent* mail;
-                    mail = readdir(innerDir);
-                    while(mail != NULL)
-                    {
-                        if(!isSpecialFolder(mail->d_name) && remove(mail->d_name) == -1)
-                        {
-                            perror("cannot delete file");
-                            rValue = 0;
-                        }
-                        mail = readdir(innerDir);
-                    }
-                    
-                    if(closedir(innerDir) == -1)
-                    {
-                        perror("cannot close innerDir");
-                        rValue = 0;
-                    }
-                    
-                    if(chdir("../") == 0)
-                    {
-                        if(rmdir(buffer) == -1)
-                        {
-                            perror("cannot remove dir");
-                            rValue = 0;
-                        }
-                    }
-                    else
-                    {
-                        perror("cannot change dir");
-                        rValue = 0;
-                    }
-                    break;
-                }
-                
-                if(chdir("../") == -1)
-                {
-                    perror("cannot change dir");
                     break;
                 }
+                assert(chdir("../") == 0);
             }
         }
-        senders = readdir(dir);
+        receiverIterator = readdir(receiverDir);
     }
     
-    if(closedir(dir) == -1)
+    if(closedir(receiverDir) == -1)
     {
         perror("cannot close dir");
     }
@@ -775,48 +633,16 @@ int deleteMail(char* user, int number)
 
 int packageDelete(int client, int clientSocket, Stream* in)
 {
-    char c;
-    int i;
-    const char* error = "invalid DELETE package\n";
-    
-    // user
-    i = 0;
-    char user[9];
-    while(1)
+    char* user = serverGetUser(&server, client);
+    if(user == NULL)
     {
-        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;
     }
     
+    char c;
+    const char* error = "invalid DELETE package\n";
+    
     int number;
     if(streamGetInt(in, &number) == -1)
     {
@@ -869,11 +695,105 @@ int packageQuit(int client, int clientSocket, Stream* in)
     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 < 2)
+    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("Usage: %s directory\n", argv[0]);
+        printf("invalid port");
         return EXIT_FAILURE;
     }
 
@@ -882,7 +802,7 @@ int main(int argc, char** argv)
     signal(SIGINT, interruptHandler);
     signal(SIGKILL, interruptHandler);
     
-    if(serverInit(&server, 3, 6543, argv[1]) == -1)
+    if(serverInit(&server, 3, port, argv[1]) == -1)
     {
         return EXIT_FAILURE;
     }
@@ -892,6 +812,7 @@ int main(int argc, char** argv)
     serverRegisterHandler(&server, packageRead);
     serverRegisterHandler(&server, packageDelete);
     serverRegisterHandler(&server, packageQuit);
+    serverRegisterHandler(&server, packageLogin);
     
     serverWaitForConnection(&server);
     

+ 6 - 0
String.c

@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 
 void stringInit(String* s)
 {
@@ -59,4 +60,9 @@ void stringRead(String* s)
 int stringCompare(String* s, const char* chars)
 {
     return strcmp(s->data, chars) == 0;
+}
+
+int stringCompareNoCase(String* s, const char* chars)
+{
+    return strcasecmp(s->data, chars) == 0;
 }

+ 1 - 0
String.h

@@ -14,6 +14,7 @@ int stringGetLength(String* s);
 
 void stringRead(String* s);
 int stringCompare(String* s, const char* chars);
+int stringCompareNoCase(String* s, const char* chars);
 
 #endif
 

BIN
mailcounter


+ 0 - 1
mails/Gusi/Kajetan/0/H

@@ -1 +0,0 @@
-Hallo Gusi, das ist ein Test.

+ 0 - 0
mails/heinz/mailcounter → mails/Receiver/mailcounter


+ 0 - 1
mails/Wusler/Gusi/0/gdfgdf

@@ -1 +0,0 @@
-gfdhfghfghhfgg

+ 0 - 1
mails/Wusler/Wusi/1/ddddd

@@ -1 +0,0 @@
-bbbbbbbbbbb

BIN
mails/Wusler/mailcounter


+ 0 - 0
mails/Gusi/mailcounter → mails/a/mailcounter


+ 0 - 1
mails/b/a/1/c

@@ -1 +0,0 @@
-wusi

+ 2 - 0
mails/b/b/10

@@ -0,0 +1,2 @@
+ich 
+es geht auch an sich selbst

+ 0 - 1
mails/b/b/3/fdfg

@@ -1 +0,0 @@
-dfgdfg

+ 0 - 1
mails/b/b/4/sgdh

@@ -1 +0,0 @@
-dfgdf

+ 2 - 0
mails/b/b/6

@@ -0,0 +1,2 @@
+Das ist ein Test.
+Hi

+ 2 - 0
mails/b/b/9

@@ -0,0 +1,2 @@
+Hallo B
+Login teeeest

BIN
mails/b/mailcounter


+ 0 - 1
mails/heinz/wusler/0/Hallo Heinz

@@ -1 +0,0 @@
-Hallo Heinz wie geht es dir?

+ 14 - 0
todo

@@ -0,0 +1,14 @@
+Subjects shouldn't be the exact filename because of names like ".."
+- subjects can be "." or ".." and receivers can no more be "." or ".."
+Lock Mail-ID-File to ensure multi users
+- done
+Port as start parameter
+- done
+
+help command, show commands on connection
+- done
+
+dummy login
+- done
+ldap login
+multiline messages