From 1d9bf5d298772afe34b7f5a4d62caf3558c20328 Mon Sep 17 00:00:00 2001 From: Patrik Karasek Date: Sat, 18 Nov 2023 16:31:54 +0100 Subject: [PATCH] LDAP Login --- .DS_Store | Bin 0 -> 6148 bytes client/client.cpp | 17 +++++++++++++--- server/server.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b35b2c22ba0c2fb43120dc48c915f35df4e0e7fa GIT binary patch literal 6148 zcmeHKL24T@6n%1x>$IepQfM}TIYGem0uwW)WYJA?fY$aTZt2)#YP;DEWZ6x2IYO2_ zLl%KvpxI?rD0J0z7omM$QtXkO$JF1} zDJxd9Juiq#a%98y!5|+^Ry;#TP#`Gq5EYQ$ZXLTA;sa*R@3(Q$E02xxBuIFPo9%-u$IDz_ z{;1t@b#jdICEqF^hchDK@rZoKn)e(j4n5Xi=}~6wc{{wfZoaMND>Fa;yqlb@F{D|wweEw>OK4t!!c!MtAMZ&l)zmgGBcg5<97tz3!*+1d4)SmC3 z`+Oa3@al@bPOh>t?X$)A=lLw(X_b$o3Qjoa zQX6_}Y2A@y!c2KZ{C6*2c&yu$nXvg}-4loTf&xK-2d{wa4-t!CY_Tw?w+>eN3P7y0 zTN~SQd`nDZF}7G3W&R1qFfv|CIu&o*X0{F3Fy)D~pq}Hez|lA|`%?K^?-% i9>=;NNAWg`HjYKoAjTF8gY2P&KLT2YPz43ns=z-|y6&9- literal 0 HcmV?d00001 diff --git a/client/client.cpp b/client/client.cpp index d490030..1139562 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -18,7 +18,8 @@ /////////////////////////////////////////////////////////////////////////////// enum commands { - SEND = 1, + LOGIN = 1, + SEND, LIST, READ, DEL, @@ -108,7 +109,7 @@ int main(int argc, char **argv) */ do { - printf("Please specify a command (SEND, LIST, READ, DEL, QUIT): "); + printf("Please specify a command (SEND, LIST, READ, DEL, QUIT, LOGIN): "); if (fgets(buffer, BUF - 1, stdin) != NULL) { size = strlen(buffer); @@ -124,7 +125,8 @@ int main(int argc, char **argv) } enum commands cmd; - if (strcmp(buffer, "SEND") == 0) cmd = SEND; + if (strcmp(buffer, "LOGIN") == 0) cmd = LOGIN; + else if (strcmp(buffer, "SEND") == 0) cmd = SEND; else if (strcmp(buffer, "LIST") == 0) cmd = LIST; else if (strcmp(buffer, "READ") == 0) cmd = READ; else if (strcmp(buffer, "DEL") == 0) cmd = DEL; @@ -133,6 +135,15 @@ int main(int argc, char **argv) char username[BUF], msgNum[10]; switch (cmd) { + case LOGIN: + char ldapUsername[BUF], ldapPassword[BUF]; + printf("LDAP Username: "); + fgets(ldapUsername, BUF - 1, stdin); + printf("Password: "); + fgets(ldapPassword, BUF - 1, stdin); + snprintf(buffer, sizeof(buffer), "LOGIN\n%s%s", ldapUsername, ldapPassword); + break; + case SEND: char sender[BUF], receiver[BUF], subject[81], message[BUF * 10]; printf("Sender: "); diff --git a/server/server.cpp b/server/server.cpp index 175cbdc..24f6a18 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -3,6 +3,10 @@ #include "mail.h" + +#include + + #include #include #include @@ -32,6 +36,9 @@ #include #include #include +#include +#include +#include #define BUF 1024 @@ -385,8 +392,46 @@ inline void exiting() std::string cmdLOGIN(std::vector& received) { - // code - return ""; + if (received.size() < 3) { + return "ERR\n"; + } + + const char* ldapUri = "ldap://ldap.technikum-wien.at:389"; + const int ldapVersion = LDAP_VERSION3; + LDAP* ldapHandle; + int rc = ldap_initialize(&ldapHandle, ldapUri); + if (rc != LDAP_SUCCESS) { + return "ERR\n"; + } + + rc = ldap_set_option(ldapHandle, LDAP_OPT_PROTOCOL_VERSION, &ldapVersion); + if (rc != LDAP_OPT_SUCCESS) { + ldap_unbind_ext_s(ldapHandle, NULL, NULL); + return "ERR\n"; + } + + rc = ldap_start_tls_s(ldapHandle, NULL, NULL); + if (rc != LDAP_SUCCESS) { + ldap_unbind_ext_s(ldapHandle, NULL, NULL); + return "ERR\n"; + } + + std::string ldapBindUser = "uid=" + received[1] + ",ou=people,dc=technikum-wien,dc=at"; + std::string ldapBindPassword = received[2]; + + BerValue bindCredentials; + bindCredentials.bv_val = (char*)ldapBindPassword.c_str(); + bindCredentials.bv_len = ldapBindPassword.length(); + rc = ldap_sasl_bind_s(ldapHandle, ldapBindUser.c_str(), LDAP_SASL_SIMPLE, &bindCredentials, NULL, NULL, NULL); + if (rc != LDAP_SUCCESS) { + ldap_unbind_ext_s(ldapHandle, NULL, NULL); + return "ERR\n"; + } + + + ldap_unbind_ext_s(ldapHandle, NULL, NULL); + + return "OK\n"; } std::string cmdSEND(std::vector& received)