1 Commits
2.0.1 ... 1.1.0

Author SHA1 Message Date
693ebfefb2 pthread added 2020-10-26 20:36:57 +01:00
5 changed files with 337 additions and 656 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
.vscode/

View File

@ -54,68 +54,26 @@ int main (int argc, char **argv) {
if (connect ( create_socket, (struct sockaddr *) &address, sizeof (address)) == 0) if (connect ( create_socket, (struct sockaddr *) &address, sizeof (address)) == 0)
{ {
printf ("\x1b[32mConnection with server (%s) established\n\x1b[0m", inet_ntoa (address.sin_addr)); printf ("Connection with server (%s) established\n", inet_ntoa (address.sin_addr));
size=recv(create_socket,buffer,BUF-1, 0); size=recv(create_socket,buffer,BUF-1, 0);
if (size>0) if (size>0)
{ {
buffer[size]= '\0'; buffer[size]= '\0';
printf("\x1b[36m%s\x1b[0m",buffer); printf("%s",buffer);
} }
} }
else else
{ {
perror("\x1b[31mConnect error - no server available\x1b[0m"); perror("Connect error - no server available");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
bool login = false;
do { do {
//command vom user überprüfen //command vom user überprüfen
char command[7] = ""; char command[6] = "";
printf ("\n\x1b[35mEnter Command:\x1b[0m \n"); printf ("\nEnter Command: \n");
fgets (command, 7, stdin); fgets (command, 6, stdin);
if (startsWith("login", command) == true && login == false)
{
char username[10] = "";
char pw[50] = "";
char bufferString[BUF] = "";
strcpy(bufferString, "login;");
//User Dateneingabe und daten einlesen
printf("\x1b[35m<Username max. 8 Zeichen>\x1b[0m\n");
fgets (username, 10, stdin);
printf("\x1b[35m<Passwort max. 50 Zeichen>\x1b[0m\n");
fgets (pw, 50, stdin);
strcat(bufferString, username);
strcat(bufferString, ";");
strcat(bufferString, pw);
remN(bufferString);
send(create_socket, bufferString, strlen (bufferString), 0);
//Response vom Server (OK oder ERR) empfangen und ausgeben
char readBuffer[BUF] = "";
read(create_socket , readBuffer, 1024);
printf("\n%s\n", readBuffer);
if (strncmp(readBuffer, "OK", 2) == 0)
{
login = true;
}
}else if (login == false && startsWith("quit", command) == true)
{
strcpy(buffer, "quit\n");
}else if (login == false)
{
printf("\x1b[33mBitte zuerst <login> eingeben. Erst danach können sie auf das Program zugreifen!\x1b[0m\n");
}
if(login == true){
//je nach command wird was audgeführt //je nach command wird was audgeführt
if(startsWith("send", command) == true) { if(startsWith("send", command) == true) {
@ -129,16 +87,16 @@ int main (int argc, char **argv) {
strcat(bufferString, "send"); strcat(bufferString, "send");
//User Dateneingabe und daten einlesen //User Dateneingabe und daten einlesen
printf("\x1b[35m<Sender max. 8 Zeichen>\x1b[0m\n"); printf("<Sender max. 8 Zeichen>\n");
fgets (sender, 8, stdin); fgets (sender, 8, stdin);
printf("\x1b[35m<Empfänger max. 8 Zeichen>\x1b[0m\n"); printf("<Empfänger max. 8 Zeichen>\n");
fgets (empfaenger, 8, stdin); fgets (empfaenger, 8, stdin);
printf("\x1b[35m<Betreff max. 80 Zeichen>\x1b[0m\n"); printf("<Betreff max. 80 Zeichen>\n");
fgets (betreff, 80, stdin); fgets (betreff, 80, stdin);
printf("\x1b[35m<Nachricht, beliebige Anzahl an Zeilen>\x1b[0m\n"); printf("<Nachricht, beliebige Anzahl an Zeilen>\n");
fgets (nachricht, 80, stdin); fgets (nachricht, 80, stdin);
//Buffer formatieren //Buffer formatieren
@ -156,7 +114,7 @@ int main (int argc, char **argv) {
//Response vom Server (OK oder ERR) empfangen und ausgeben //Response vom Server (OK oder ERR) empfangen und ausgeben
char readBuffer[BUF] = ""; char readBuffer[BUF] = "";
read(create_socket , readBuffer, 1024); int valread = read(create_socket , readBuffer, 1024);
printf("\n%s\n", readBuffer); printf("\n%s\n", readBuffer);
} }
@ -167,9 +125,9 @@ int main (int argc, char **argv) {
char p[20] = ""; char p[20] = "";
int counter = 0; int counter = 0;
printf("\x1b[35m<Username max. 8 Zeichen>\x1b[0m\n"); printf("<Username max. 8 Zeichen>\n");
fgets (username, 8, stdin); fgets (username, 8, stdin);
printf("\x1b[35m<Posteingang oder Postausgang>\x1b[0m\n"); printf("<Posteingang oder Postausgang>\n");
fgets (p, 20, stdin); fgets (p, 20, stdin);
strcat(bufferString, ";"); strcat(bufferString, ";");
@ -181,7 +139,7 @@ int main (int argc, char **argv) {
send(create_socket, bufferString, strlen (bufferString), 0); send(create_socket, bufferString, strlen (bufferString), 0);
char readBuffer[BUF] = ""; char readBuffer[BUF] = "";
read(create_socket , readBuffer, 1024); int valread = read(create_socket , readBuffer, 1024);
char delimiter[] = ";"; char delimiter[] = ";";
char *ptr; char *ptr;
@ -199,7 +157,7 @@ int main (int argc, char **argv) {
} }
else if (counter == 0) { else if (counter == 0) {
printf("\x1b[35m<Anzahl der Nachrichten für den User: %s>\x1b[0m\n", ptr); printf("<Anzahl der Nachrichten für den User: %s>\n", ptr);
} }
else { else {
@ -221,11 +179,11 @@ int main (int argc, char **argv) {
char nid[80] = ""; char nid[80] = "";
char folder[20] = ""; char folder[20] = "";
printf("\x1b[35m<Username max. 8 Zeichen>\x1b[0m\n"); printf("<Username max. 8 Zeichen>\n");
fgets (username, 8, stdin); fgets (username, 8, stdin);
printf("\x1b[35m<posteingang oder postausgang>\x1b[0m\n"); printf("<posteingang oder postausgang>\n");
fgets (folder, 20, stdin); fgets (folder, 20, stdin);
printf("\x1b[35m<Nachrichten-Nummer>\x1b[0m\n"); printf("<Nachrichten-Nummer>\n");
fgets (nid, 80, stdin); fgets (nid, 80, stdin);
strcat(bufferString, ";"); strcat(bufferString, ";");
@ -243,7 +201,7 @@ int main (int argc, char **argv) {
//strcpy(buffer, ""); //strcpy(buffer, "");
//buffer[size]= '\0'; //buffer[size]= '\0';
char readBuffer[BUF] = ""; char readBuffer[BUF] = "";
read(create_socket , readBuffer, 1000); int valread = read(create_socket , readBuffer, 1000);
printf("\n%s\n", readBuffer ); printf("\n%s\n", readBuffer );
} }
@ -256,11 +214,11 @@ int main (int argc, char **argv) {
char bufferString[BUF] = "del"; char bufferString[BUF] = "del";
char p[20] = ""; char p[20] = "";
printf("\x1b[35m<Username max. 8 Zeichen>\x1b[0m\n"); printf("<Username max. 8 Zeichen>\n");
fgets (username, 8, stdin); fgets (username, 8, stdin);
printf("\x1b[35m<Posteingang oder Postausgang>\x1b[0m\n"); printf("<Posteingang oder Postausgang>\n");
fgets (p, 20, stdin); fgets (p, 20, stdin);
printf("\x1b[35m<Nachrichten-Nummer>\x1b[0m\n"); printf("<Nachrichten-Nummer>\n");
fgets (nachrichtennummer, 80, stdin); fgets (nachrichtennummer, 80, stdin);
strcat(bufferString, ";"); strcat(bufferString, ";");
@ -274,7 +232,7 @@ int main (int argc, char **argv) {
send(create_socket, bufferString, strlen (bufferString), 0); send(create_socket, bufferString, strlen (bufferString), 0);
char readBuffer[BUF] = ""; char readBuffer[BUF] = "";
read(create_socket , readBuffer, 1024); int valread = read(create_socket , readBuffer, 1024);
printf("\n%s\n", readBuffer ); printf("\n%s\n", readBuffer );
} }
@ -286,7 +244,6 @@ int main (int argc, char **argv) {
else { else {
printf("DAS IST KEIN COMMAND!!!!!"); printf("DAS IST KEIN COMMAND!!!!!");
} }
}

View File

@ -1,8 +1,7 @@
#Client und Server kompilieren #Client und Server kompilieren
all: server.c client.c all: server.c client.c
gcc -Wall -o server server.c -lldap -llber -lpthread gcc server.c -o server -lpthread
gcc -Wall -o client client.c -lldap -llber -lpthread gcc client.c -o client -lpthread
#Kompilierten Client und Server löschen #Kompilierten Client und Server löschen
clean: client server clean: client server
@ -10,11 +9,11 @@ clean: client server
#Nur den Client kompilieren: #Nur den Client kompilieren:
client: client.c client: client.c
gcc -Wall -o client client.c -lldap -llber -lpthread gcc client.c -o client -lpthread
#Nur den Server kompilieren: #Nur den Server kompilieren:
server: server.c server: server.c
gcc -Wall -o server server.c -lldap -llber -lpthread gcc server.c -o server -lpthread
#Kompilierten Server löschen #Kompilierten Server löschen
cleanS: server cleanS: server

View File

@ -2,7 +2,6 @@
# Protokoll 🔧 # Protokoll 🔧
``` ```
LOGIN: login;username;passwort
SEND: send;sender;empänger;betreff;nachricht SEND: send;sender;empänger;betreff;nachricht
LIST: list:username;postEinAusgang LIST: list:username;postEinAusgang
READ: read;username;postEinAusgang;nid READ: read;username;postEinAusgang;nid

891
server.c
View File

@ -14,9 +14,6 @@
#include <dirent.h> #include <dirent.h>
#include <time.h> #include <time.h>
#include <pthread.h> #include <pthread.h>
#include <ldap.h>
#include <termios.h>
#define MAXCHAR 1000 #define MAXCHAR 1000
#define BUF 1024 #define BUF 1024
@ -54,646 +51,373 @@ int countFiles(char path[]) {
return file_count; return file_count;
} }
int login_user(char username[8], char pw[50])
{
////////////////////////////////////////////////////////////////////////////
// LDAP config
// anonymous bind with user and pw empty
const char *ldapUri = "ldap://ldap.technikum-wien.at:389";
const int ldapVersion = LDAP_VERSION3;
// read username (bash: export ldapuser=<yourUsername>)
char ldapBindUser[256];
char rawLdapUser[128];
strcpy(rawLdapUser, username);
sprintf(ldapBindUser, "uid=%s,ou=people,dc=technikum-wien,dc=at", rawLdapUser);
printf("user set to: %s\n", ldapBindUser);
// read password (bash: export ldappw=<yourPW>)
char ldapBindPassword[256];
strcpy(ldapBindPassword, pw);
printf("pw taken over from commandline: %s\n", pw);
// search settings
const char *ldapSearchBaseDomainComponent = "dc=technikum-wien,dc=at";
const char *ldapSearchFilter = "(uid=if20b20*)";
ber_int_t ldapSearchScope = LDAP_SCOPE_SUBTREE;
const char *ldapSearchResultAttributes[] = {"uid", "cn", NULL};
// general
int rc = 0; // return code
////////////////////////////////////////////////////////////////////////////
// setup LDAP connection
// https://linux.die.net/man/3/ldap_initialize
LDAP *ldapHandle;
rc = ldap_initialize(&ldapHandle, ldapUri);
if (rc != LDAP_SUCCESS)
{
fprintf(stderr, "ldap_init failed\n");
return 0;
}
printf("connected to LDAP server %s\n", ldapUri);
////////////////////////////////////////////////////////////////////////////
// set verison options
// https://linux.die.net/man/3/ldap_set_option
rc = ldap_set_option(
ldapHandle,
LDAP_OPT_PROTOCOL_VERSION, // OPTION
&ldapVersion); // IN-Value
if (rc != LDAP_OPT_SUCCESS)
{
// https://www.openldap.org/software/man.cgi?query=ldap_err2string&sektion=3&apropos=0&manpath=OpenLDAP+2.4-Release
fprintf(stderr, "ldap_set_option(PROTOCOL_VERSION): %s\n", ldap_err2string(rc));
ldap_unbind_ext_s(ldapHandle, NULL, NULL);
return 0;
}
////////////////////////////////////////////////////////////////////////////
// start connection secure (initialize TLS)
// https://linux.die.net/man/3/ldap_start_tls_s
// int ldap_start_tls_s(LDAP *ld,
// LDAPControl **serverctrls,
// LDAPControl **clientctrls);
// https://linux.die.net/man/3/ldap
// https://docs.oracle.com/cd/E19957-01/817-6707/controls.html
// The LDAPv3, as documented in RFC 2251 - Lightweight Directory Access
// Protocol (v3) (http://www.faqs.org/rfcs/rfc2251.html), allows clients
// and servers to use controls as a mechanism for extending an LDAP
// operation. A control is a way to specify additional information as
// part of a request and a response. For example, a client can send a
// control to a server as part of a search request to indicate that the
// server should sort the search results before sending the results back
// to the client.
rc = ldap_start_tls_s(
ldapHandle,
NULL,
NULL);
if (rc != LDAP_SUCCESS)
{
fprintf(stderr, "ldap_start_tls_s(): %s\n", ldap_err2string(rc));
ldap_unbind_ext_s(ldapHandle, NULL, NULL);
return 0;
}
////////////////////////////////////////////////////////////////////////////
// bind credentials
// https://linux.die.net/man/3/lber-types
// SASL (Simple Authentication and Security Layer)
// https://linux.die.net/man/3/ldap_sasl_bind_s
// int ldap_sasl_bind_s(
// LDAP *ld,
// const char *dn,
// const char *mechanism,
// struct berval *cred,
// LDAPControl *sctrls[],
// LDAPControl *cctrls[],
// struct berval **servercredp);
BerValue bindCredentials;
bindCredentials.bv_val = (char *)ldapBindPassword;
bindCredentials.bv_len = strlen(ldapBindPassword);
BerValue *servercredp; // server's credentials
rc = ldap_sasl_bind_s(
ldapHandle,
ldapBindUser,
LDAP_SASL_SIMPLE,
&bindCredentials,
NULL,
NULL,
&servercredp);
if (rc != LDAP_SUCCESS)
{
fprintf(stderr, "LDAP bind error: %s\n", ldap_err2string(rc));
ldap_unbind_ext_s(ldapHandle, NULL, NULL);
return 0;
}
////////////////////////////////////////////////////////////////////////////
// perform ldap search
// https://linux.die.net/man/3/ldap_search_ext_s
// _s : synchronous
// int ldap_search_ext_s(
// LDAP *ld,
// char *base,
// int scope,
// char *filter,
// char *attrs[],
// int attrsonly,
// LDAPControl **serverctrls,
// LDAPControl **clientctrls,
// struct timeval *timeout,
// int sizelimit,
// LDAPMessage **res );
LDAPMessage *searchResult;
rc = ldap_search_ext_s(
ldapHandle,
ldapSearchBaseDomainComponent,
ldapSearchScope,
ldapSearchFilter,
(char **)ldapSearchResultAttributes,
0,
NULL,
NULL,
NULL,
500,
&searchResult);
if (rc != LDAP_SUCCESS)
{
fprintf(stderr, "LDAP search error: %s\n", ldap_err2string(rc));
ldap_unbind_ext_s(ldapHandle, NULL, NULL);
return 0;
}
// https://linux.die.net/man/3/ldap_count_entries
printf("Total results: %d\n", ldap_count_entries(ldapHandle, searchResult));
////////////////////////////////////////////////////////////////////////////
// get result of search
// https://linux.die.net/man/3/ldap_first_entry
// https://linux.die.net/man/3/ldap_next_entry
LDAPMessage *searchResultEntry;
for (searchResultEntry = ldap_first_entry(ldapHandle, searchResult);
searchResultEntry != NULL;
searchResultEntry = ldap_next_entry(ldapHandle, searchResultEntry))
{
/////////////////////////////////////////////////////////////////////////
// Base Information of the search result entry
// https://linux.die.net/man/3/ldap_get_dn
printf("DN: %s\n", ldap_get_dn(ldapHandle, searchResultEntry));
/////////////////////////////////////////////////////////////////////////
// Attributes
// https://linux.die.net/man/3/ldap_first_attribute
// https://linux.die.net/man/3/ldap_next_attribute
//
// berptr: berptr, a pointer to a BerElement it has allocated to keep
// track of its current position. This pointer should be passed
// to subsequent calls to ldap_next_attribute() and is used to
// effectively step through the entry's attributes.
BerElement *ber;
char *searchResultEntryAttribute;
for (searchResultEntryAttribute = ldap_first_attribute(ldapHandle, searchResultEntry, &ber);
searchResultEntryAttribute != NULL;
searchResultEntryAttribute = ldap_next_attribute(ldapHandle, searchResultEntry, ber))
{
BerValue **vals;
if ((vals = ldap_get_values_len(ldapHandle, searchResultEntry, searchResultEntryAttribute)) != NULL)
{
for (int i = 0; i < ldap_count_values_len(vals); i++)
{
printf("\t%s: %s\n", searchResultEntryAttribute, vals[i]->bv_val);
if(strcmp(searchResultEntryAttribute,"uid") == 0){
if(strcmp(vals[i]->bv_val,rawLdapUser) == 0){
printf("\n\nGEFUNDEN!!!!!!!!!!!!!!!!!\n\n");
// Free Memory
ldap_value_free_len(vals);
ldap_memfree(searchResultEntryAttribute);
if (ber != NULL){
ber_free(ber, 0);
}
printf("\n");
ldap_msgfree(searchResult);
ldap_unbind_ext_s(ldapHandle, NULL, NULL);
return 1;
}
}
}
ldap_value_free_len(vals);
}
// free memory
ldap_memfree(searchResultEntryAttribute);
}
// free memory
if (ber != NULL)
{
ber_free(ber, 0);
}
printf("\n");
}
// free memory
ldap_msgfree(searchResult);
////////////////////////////////////////////////////////////////////////////
// https://linux.die.net/man/3/ldap_unbind_ext_s
// int ldap_unbind_ext_s(
// LDAP *ld,
// LDAPControl *sctrls[],
// LDAPControl *cctrls[]);
ldap_unbind_ext_s(ldapHandle, NULL, NULL);
return 0;
}
void *threadFun(void *arg){ void *threadFun(void *arg){
int new_socket = *((int *)arg); int new_socket = *((int *)arg);
char buffer[BUF]; char buffer[BUF];
pthread_detach(pthread_self()); pthread_detach(pthread_self());
if(new_socket > 0){ if(new_socket > 0){
strcpy(buffer, "Welcome to Lukas & Georgs Server, Please type <login> to login:\n"); strcpy(buffer, "Welcome to myserver, Please enter your command:\n");
send(new_socket, buffer, strlen(buffer),0); send(new_socket, buffer, strlen(buffer),0);
} }
bool login = false;
do { do {
int size = recv (new_socket, buffer, BUF-1, 0); int size = recv (new_socket, buffer, BUF-1, 0);
if( size > 0) if( size > 0)
{ {
buffer[size] = '\0'; buffer[size] = '\0';
if (login == false)
{ //Überprüfen welchen command der client eingegeben hat,
// Do login // wenn command nicht zutreffend error zurücksenden
if (strncmp("login", buffer, 5) == 0) if(startsWith("send", buffer) == true) {
{
char delimiter[] = ";"; char delimiter[] = ";";
char *ptr; char *ptr;
//den buffer mit dem seperator splitten //den buffer mit dem seperator splitten
ptr = strtok(buffer, delimiter); ptr = strtok(buffer, delimiter);
int counter = 0;
char username[9]; int counter = 0;
char pw[50]; char filename1[] = "";
while(ptr != NULL) { char filename2[] = "";
if (counter == 1) { char sender[8] = "";
strcpy(username, ptr); char content[] = "";
}else if(counter == 2) char empfaenger[8] = "";
{ char betreff[80] = "";
strcpy(pw, ptr); char nid[100] = "";
}
counter = counter + 1; //timestamp als ersten teil der id erstellen
// naechsten Abschnitt erstellen char ts[20];
ptr = strtok(NULL, delimiter); time_t now = time(NULL);
} strftime(ts, 20, "%Y-%m-%d%H:%M:%S", localtime(&now));
if(login_user(username, pw) == 1){
login = true;
printf("Login geschafft!!");
//OK an den client zurücksenden
char suc[] = "OK";
send(new_socket , suc , strlen(suc) , 0 );
}else
{
printf("Login error!!!");
//OK an den client zurücksenden
char err[] = "ERR";
send(new_socket , err , strlen(err) , 0 );
}
}
}else{// Is logedin
// Überprüfen welchen command der client eingegeben hat,
// wenn command nicht zutreffend error zurücksenden
if(startsWith("send", buffer) == true) {
char delimiter[] = ";"; //den gesplitteten buffer schritt für schritt durchgehen
char *ptr; while(ptr != NULL) {
//den buffer mit dem seperator splitten //printf("Abschnitt gefunden: %s\n", ptr);
ptr = strtok(buffer, delimiter); //sender auslesen, ordner für sender erstellen, wenn nicht vorhanden
if (counter == 1) {
struct stat st = {0};
char dir[] = "./data/postausgang/";
strcat(dir, ptr);
int counter = 0; if (stat(dir, &st) == -1) {
char sender[8] = ""; mkdir(dir, 0700);
char empfaenger[8] = ""; }
char betreff[80] = "";
//timestamp als ersten teil der id erstellen strcat(sender, ptr);
char ts[20]; printf("1 %s \n", ptr);
time_t now = time(NULL); }
strftime(ts, 20, "%Y-%m-%d%H:%M:%S", localtime(&now));
//empfänger auslesen, ordner für empfänger erstellen, wenn nicht vorhanden
if (counter == 2) {
struct stat st2 = {0};
char dir2[] = "./data/posteingang/";
strcat(dir2, ptr);
//den gesplitteten buffer schritt für schritt durchgehen if (stat(dir2, &st2) == -1) {
while(ptr != NULL) { mkdir(dir2, 0700);
//printf("Abschnitt gefunden: %s\n", ptr); }
//sender auslesen, ordner für sender erstellen, wenn nicht vorhanden
if (counter == 1) {
struct stat st = {0};
char dir[] = "./data/postausgang/";
strcat(dir, ptr);
if (stat(dir, &st) == -1) { strcat(empfaenger, ptr);
mkdir(dir, 0700); printf("2 %s \n" , ptr);
} }
strcat(sender, ptr); //betreff auslesen, id (ts) fertigstellen,
printf("1 %s \n", ptr); //2 dateien erstellen für sender und empfänger mit ts als filename
if (counter == 3) {
//printf("3 %s \n", ptr);
strcat(betreff, ptr);
strcat(ts, betreff);
//Pfad erstellen für den sender und file erstellen
char fb[100] = "touch ./data/postausgang/";
strcat(fb, "/");
strcat(fb, sender);
strcat(fb, "/");
strcat(fb, ts);
printf("PATH: %s \n", fb);
system(fb);
//Pfad erstellen für den empfänger und file erstellen
strcpy(fb, "touch ./data/posteingang/");
strcat(fb, "/");
strcat(fb, empfaenger);
strcat(fb, "/");
strcat(fb, ts);
printf("PATH: %s \n", fb);
system(fb);
}
//buffer in files schreiben
if (counter == 4) {
//printf("4 %s \n", ptr);
//file content für den empfänger erstellen
char fp[100] = "./data/posteingang/";
char content1[1000] = "";
strcpy(content1, "ID: ");
strcat(content1, ts);
strcat(content1, "\n");
strcat(content1, "Received from: ");
strcat(content1, sender);
strcat(content1, "\n");
strcat(content1, "Betreff: ");
strcat(content1, betreff);
strcat(content1, "\n");
strcat(content1, "Message: ");
strcat(content1, ptr);
strcat(fp, empfaenger);
strcat(fp, "/");
strcat(fp, ts);
//content in das file storen
mstore_data(fp, content1);
//file content für den sender erstellen
char content2[1000] = "";
strcpy(content2, "ID: ");
strcat(content2, ts);
strcat(content2, "\n");
strcat(content2, "Sent to: ");
strcat(content2, empfaenger);
strcat(content2, "\n");
strcat(content2, "Betreff: ");
strcat(content2, betreff);
strcat(content2, "\n");
strcat(content2, "Message: ");
strcat(content2, ptr);
strcpy(fp, "./data/postausgang/");
strcat(fp, sender);
strcat(fp, "/");
strcat(fp, ts);
//content2 in das file storen
mstore_data(fp, content2);
}
counter = counter + 1;
// naechsten Abschnitt erstellen
ptr = strtok(NULL, delimiter);
}
//printf ("\n Message received: \n %s", buffer);
//OK an den client zurücksenden
char suc[] = "OK";
send(new_socket , suc , strlen(suc) , 0 );
}
else if(startsWith("del", buffer) == true) {
printf("delete: %s ", buffer);
char delimiter[] = ";";
char *ptr;
ptr = strtok(buffer, delimiter);
int counter = 0;
char username[8] = "";
char nid[80] = "";
char p[20] = "";
char path[100] = "";
while(ptr != NULL) {
//username speichern
if(counter == 1) {
strcat(username, ptr);
}
//post ein- oder ausgang speichern
if(counter == 2) {
strcpy(p, ptr);
}
//nid speichern, den path string erstellen, rm -rf path ausführen
if(counter == 3) {
strcat(nid, ptr);
if(strcmp(p, "postausgang") == 0) {
strcpy(path, "rm -rf ./data/postausgang/");
}
if(strcmp(p, "posteingang") == 0) {
strcpy(path, "rm -rf ./data/posteingang/");
} }
//empfänger auslesen, ordner für empfänger erstellen, wenn nicht vorhanden strcat(path, username);
if (counter == 2) { strcat(path, "/");
struct stat st2 = {0}; strcat(path, nid);
char dir2[] = "./data/posteingang/";
strcat(dir2, ptr);
if (stat(dir2, &st2) == -1) {
mkdir(dir2, 0700);
}
strcat(empfaenger, ptr);
printf("2 %s \n" , ptr);
}
//betreff auslesen, id (ts) fertigstellen,
//2 dateien erstellen für sender und empfänger mit ts als filename
if (counter == 3) {
//printf("3 %s \n", ptr);
strcat(betreff, ptr);
strcat(ts, betreff);
//Pfad erstellen für den sender und file erstellen
char fb[100] = "touch ./data/postausgang/";
strcat(fb, "/");
strcat(fb, sender);
strcat(fb, "/");
strcat(fb, ts);
printf("PATH: %s \n", fb);
system(fb);
//Pfad erstellen für den empfänger und file erstellen
strcpy(fb, "touch ./data/posteingang/");
strcat(fb, "/");
strcat(fb, empfaenger);
strcat(fb, "/");
strcat(fb, ts);
printf("PATH: %s \n", fb);
system(fb);
}
//buffer in files schreiben
if (counter == 4) {
//printf("4 %s \n", ptr);
//file content für den empfänger erstellen
char fp[100] = "./data/posteingang/";
char content1[1000] = "";
strcpy(content1, "ID: ");
strcat(content1, ts);
strcat(content1, "\n");
strcat(content1, "Received from: ");
strcat(content1, sender);
strcat(content1, "\n");
strcat(content1, "Betreff: ");
strcat(content1, betreff);
strcat(content1, "\n");
strcat(content1, "Message: ");
strcat(content1, ptr);
strcat(fp, empfaenger);
strcat(fp, "/");
strcat(fp, ts);
//content in das file storen
mstore_data(fp, content1);
//file content für den sender erstellen
char content2[1000] = "";
strcpy(content2, "ID: ");
strcat(content2, ts);
strcat(content2, "\n");
strcat(content2, "Sent to: ");
strcat(content2, empfaenger);
strcat(content2, "\n");
strcat(content2, "Betreff: ");
strcat(content2, betreff);
strcat(content2, "\n");
strcat(content2, "Message: ");
strcat(content2, ptr);
strcpy(fp, "./data/postausgang/");
strcat(fp, sender);
strcat(fp, "/");
strcat(fp, ts);
//content2 in das file storen
mstore_data(fp, content2);
}
counter = counter + 1;
// naechsten Abschnitt erstellen
ptr = strtok(NULL, delimiter);
system(path);
} }
counter = counter + 1;
//printf ("\n Message received: \n %s", buffer); // naechsten Abschnitt erstellen
//OK an den client zurücksenden ptr = strtok(NULL, delimiter);
char suc[] = "OK";
send(new_socket , suc , strlen(suc) , 0 );
} }
else if(startsWith("del", buffer) == true) { //OK an client zurücksenden
printf("delete: %s ", buffer); char resBuff[BUF] = "OK\n";
send(new_socket, resBuff, strlen(resBuff),0);
strcpy(buffer, "");
char delimiter[] = ";"; }
char *ptr;
ptr = strtok(buffer, delimiter);
int counter = 0; //read
else if(startsWith("read", buffer) == true) {
char username[8] = ""; char delimiter[] = ";";
char nid[80] = ""; char *ptr;
char p[20] = ""; ptr = strtok(buffer, delimiter);
char path[100] = "";
while(ptr != NULL) { int counter = 0;
char username[8] = "";
char nid[80] = "";
char betreff[80] = "";
char folder[20] = "";
while(ptr != NULL) {
if(counter == 1) {
//username speichern //username speichern
if(counter == 1) { strcat(username, ptr);
strcat(username, ptr);
}
//post ein- oder ausgang speichern
if(counter == 2) {
strcpy(p, ptr);
}
//nid speichern, den path string erstellen, rm -rf path ausführen
if(counter == 3) {
strcat(nid, ptr);
if(strcmp(p, "postausgang") == 0) {
strcpy(path, "rm -rf ./data/postausgang/");
}
if(strcmp(p, "posteingang") == 0) {
strcpy(path, "rm -rf ./data/posteingang/");
}
strcat(path, username);
strcat(path, "/");
strcat(path, nid);
system(path);
}
counter = counter + 1;
// naechsten Abschnitt erstellen
ptr = strtok(NULL, delimiter);
} }
//OK an client zurücksenden //postein- oder ausgang speichern
char resBuff[BUF] = "OK\n"; if(counter == 2) {
send(new_socket, resBuff, strlen(resBuff),0); strcpy(folder, ptr);
strcpy(buffer, "");
}
//read
else if(startsWith("read", buffer) == true) {
char delimiter[] = ";";
char *ptr;
ptr = strtok(buffer, delimiter);
int counter = 0;
char username[8] = "";
char nid[80] = "";
char betreff[80] = "";
char folder[20] = "";
while(ptr != NULL) {
if(counter == 1) {
//username speichern
strcat(username, ptr);
}
//postein- oder ausgang speichern
if(counter == 2) {
strcpy(folder, ptr);
}
//nid speichern, file mit dem path lesen und an client zzrücksenden
if(counter == 3) {
//nid
strcat(nid, ptr);
strcpy(betreff, nid);
char filename[] = "./data/";
strcat(filename, folder);
strcat(filename, "/");
strcat(filename, username);
strcat(filename, "/");
strcat(filename, nid);
printf("PATH: %s", filename);
FILE *ptr_file;
char buf[1000];
char res[1000];
ptr_file = fopen(filename,"r");
if (!ptr_file)
perror("File Open error!");
while (fgets(buf,1000, ptr_file)!=NULL) {
strcat(res, buf);
}
fclose(ptr_file);
// and send that buffer to client
printf("%s", res);
send(new_socket , res, strlen(res), 0 );
}
counter = counter + 1;
// naechsten Abschnitt erstellen
ptr = strtok(NULL, delimiter);
} }
}
//list //nid speichern, file mit dem path lesen und an client zzrücksenden
else if(startsWith("list", buffer) == true) { if(counter == 3) {
//nid
strcat(nid, ptr);
strcpy(betreff, nid);
char delimiter[] = ";"; char filename[] = "./data/";
char *ptr; strcat(filename, folder);
ptr = strtok(buffer, delimiter); strcat(filename, "/");
int counter = 0; strcat(filename, username);
char username[8] = ""; strcat(filename, "/");
char p[20] = ""; strcat(filename, nid);
char path[100] = ""; printf("PATH: %s", filename);
FILE *ptr_file;
char buf[1000];
char res[1000];
while(ptr != NULL) { ptr_file = fopen(filename,"r");
if (!ptr_file)
perror("File Open error!");
//username speichern while (fgets(buf,1000, ptr_file)!=NULL) {
if(counter == 1) { strcat(res, buf);
strcat(username, ptr);
} }
//postein - oder ausgang speichern
if(counter == 2) {
strcat(p, ptr);
if(strcmp(p, "postausgang") == 0) { fclose(ptr_file);
strcpy(path, "./data/postausgang/");
}
if(strcmp(p, "posteingang") == 0) {
strcpy(path, "./data/posteingang/"); // and send that buffer to client
}
strcat(path, username);
strcat(path, "/");
DIR *d; printf("%s", res);
struct dirent *dir; send(new_socket , res, strlen(res), 0 );
char filenames[1000] = "";
//Messages zählen }
int fc = countFiles(path);
char str[1000];
//int in string konverten
sprintf(str, "%d", fc);
strcat(filenames, str); counter = counter + 1;
strcat(filenames, ";");
//file lesen // naechsten Abschnitt erstellen
d = opendir(path); ptr = strtok(NULL, delimiter);
}
}
if (d) //list
else if(startsWith("list", buffer) == true) {
char delimiter[] = ";";
char *ptr;
ptr = strtok(buffer, delimiter);
int counter = 0;
char username[8] = "";
char p[20] = "";
char path[100] = "";
while(ptr != NULL) {
//username speichern
if(counter == 1) {
strcat(username, ptr);
}
//postein - oder ausgang speichern
if(counter == 2) {
strcat(p, ptr);
if(strcmp(p, "postausgang") == 0) {
strcpy(path, "./data/postausgang/");
}
if(strcmp(p, "posteingang") == 0) {
strcpy(path, "./data/posteingang/");
}
strcat(path, username);
strcat(path, "/");
DIR *d;
struct dirent *dir;
char filenames[1000] = "";
//Messages zählen
int fc = countFiles(path);
char str[1000];
//int in string konverten
sprintf(str, "%d", fc);
strcat(filenames, str);
strcat(filenames, ";");
//file lesen
d = opendir(path);
if (d)
{
while ((dir = readdir(d)) != NULL)
{ {
strcat(filenames, dir->d_name);
while ((dir = readdir(d)) != NULL) strcat(filenames, ";");
{
strcat(filenames, dir->d_name);
strcat(filenames, ";");
}
closedir(d);
//an client senden
send(new_socket , filenames , strlen(filenames) , 0 );
strcpy(buffer, "");
} }
closedir(d);
//an client senden
send(new_socket , filenames , strlen(filenames) , 0 );
strcpy(buffer, "");
} }
counter = counter + 1;
// naechsten Abschnitt erstellen
ptr = strtok(NULL, delimiter);
} }
}else { counter = counter + 1;
printf("error");
char err[] = "ERR";
send(new_socket , err , strlen(err) , 0 );
// naechsten Abschnitt erstellen
ptr = strtok(NULL, delimiter);
} }
}else {
printf("error");
char err[] = "ERR";
send(new_socket , err , strlen(err) , 0 );
} }
//printf ("\n content: \n %s", buffer); //printf ("\n content: \n %s", buffer);
@ -721,6 +445,8 @@ void *threadFun(void *arg){
int main (int argc, char **argv) { int main (int argc, char **argv) {
int create_socket, new_socket; int create_socket, new_socket;
socklen_t addrlen; socklen_t addrlen;
char buffer[BUF];
int size;
struct sockaddr_in address, cliaddress; struct sockaddr_in address, cliaddress;
if( argc < 3 ){ if( argc < 3 ){
@ -747,6 +473,7 @@ int main (int argc, char **argv) {
mkdir(argv[2], 0711); mkdir(argv[2], 0711);
pthread_t tid; pthread_t tid;
int i = 0;
while (1) { while (1) {
printf("Waiting for connections...\n"); printf("Waiting for connections...\n");
new_socket = accept ( create_socket, (struct sockaddr *) &cliaddress, &addrlen ); new_socket = accept ( create_socket, (struct sockaddr *) &cliaddress, &addrlen );