LDAP Login
This commit is contained in:
parent
8272a16e90
commit
1d9bf5d298
@ -18,7 +18,8 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
enum commands {
|
enum commands {
|
||||||
SEND = 1,
|
LOGIN = 1,
|
||||||
|
SEND,
|
||||||
LIST,
|
LIST,
|
||||||
READ,
|
READ,
|
||||||
DEL,
|
DEL,
|
||||||
@ -108,7 +109,7 @@ int main(int argc, char **argv)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
do {
|
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)
|
if (fgets(buffer, BUF - 1, stdin) != NULL)
|
||||||
{
|
{
|
||||||
size = strlen(buffer);
|
size = strlen(buffer);
|
||||||
@ -124,7 +125,8 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum commands cmd;
|
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, "LIST") == 0) cmd = LIST;
|
||||||
else if (strcmp(buffer, "READ") == 0) cmd = READ;
|
else if (strcmp(buffer, "READ") == 0) cmd = READ;
|
||||||
else if (strcmp(buffer, "DEL") == 0) cmd = DEL;
|
else if (strcmp(buffer, "DEL") == 0) cmd = DEL;
|
||||||
@ -133,6 +135,15 @@ int main(int argc, char **argv)
|
|||||||
char username[BUF], msgNum[10];
|
char username[BUF], msgNum[10];
|
||||||
|
|
||||||
switch (cmd) {
|
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:
|
case SEND:
|
||||||
char sender[BUF], receiver[BUF], subject[81], message[BUF * 10];
|
char sender[BUF], receiver[BUF], subject[81], message[BUF * 10];
|
||||||
printf("Sender: ");
|
printf("Sender: ");
|
||||||
|
|||||||
@ -3,6 +3,10 @@
|
|||||||
|
|
||||||
#include "mail.h"
|
#include "mail.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <ldap.h>
|
||||||
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
@ -32,6 +36,9 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define BUF 1024
|
#define BUF 1024
|
||||||
|
|
||||||
@ -385,8 +392,46 @@ inline void exiting()
|
|||||||
|
|
||||||
std::string cmdLOGIN(std::vector<std::string>& received)
|
std::string cmdLOGIN(std::vector<std::string>& received)
|
||||||
{
|
{
|
||||||
// code
|
if (received.size() < 3) {
|
||||||
return "";
|
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)
|
std::string cmdSEND(std::vector<std::string>& received)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user