LDAP Login

This commit is contained in:
Patrik Karasek 2023-11-18 16:31:54 +01:00
parent 8272a16e90
commit 1d9bf5d298
3 changed files with 61 additions and 5 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -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: ");

View File

@ -3,6 +3,10 @@
#include "mail.h"
#include <ldap.h>
#include <algorithm>
#include <ranges>
#include <cstddef>
@ -32,6 +36,9 @@
#include <arpa/inet.h>
#include <signal.h>
#include <openssl/sha.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define BUF 1024
@ -385,8 +392,46 @@ inline void exiting()
std::string cmdLOGIN(std::vector<std::string>& 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<std::string>& received)