Skip to content

Commit

Permalink
console: refactored AuthenticateWithDirector
Browse files Browse the repository at this point in the history
- no failure messages sent back to the user
- only the starting message id "1000" is evaluated
  • Loading branch information
franku committed Oct 15, 2018
1 parent 233d7eb commit 7cc5875
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 81 deletions.
12 changes: 6 additions & 6 deletions core/src/console/console.cc
Expand Up @@ -1093,17 +1093,17 @@ int main(int argc, char *argv[])

ConsoleOutput(errmsg);

#if 0 // Ueb
#if defined(HAVE_PAM)
if (console_resource && console_resource->use_pam_authentication_) {
// UA_sock->fsend("@@username:bareos-pam");
// UA_sock->fsend("@@password:linuxlinux");
Bmicrosleep(1,0);
if (!ConsolePamAuthenticate(stdin, UA_sock)) {
TerminateConsole(0);
return 1;
}
Bmicrosleep(1,0);
if (!ConsolePamAuthenticate(stdin, UA_sock)) {
TerminateConsole(0);
return 1;
}
#endif /* HAVE_PAM */
#endif

Dmsg0(40, "Opened connection with Director daemon\n");

Expand Down
1 change: 0 additions & 1 deletion core/src/console/console_conf.cc
Expand Up @@ -91,7 +91,6 @@ static ResourceItem cons_items[] = {
{ "Password", CFG_TYPE_MD5PASSWORD, ITEM(res_cons.password), 0, CFG_ITEM_REQUIRED, NULL, NULL, NULL },
{ "Director", CFG_TYPE_STR, ITEM(res_cons.director), 0, 0, NULL, NULL, NULL },
{ "HeartbeatInterval", CFG_TYPE_TIME, ITEM(res_cons.heartbeat_interval), 0, CFG_ITEM_DEFAULT, "0", NULL, NULL },
{ "UsePamAuthentication", CFG_TYPE_BOOL, ITEM(res_cons.use_pam_authentication_), 0, CFG_ITEM_DEFAULT, "true", NULL, NULL },
TLS_COMMON_CONFIG(res_dir),
TLS_CERT_CONFIG(res_dir),
TLS_PSK_CONFIG(res_dir),
Expand Down
1 change: 0 additions & 1 deletion core/src/console/console_conf.h
Expand Up @@ -71,7 +71,6 @@ class ConsoleResource : public TlsResource {
uint32_t history_length; /**< readline history length */
char *director; /**< bind to director */
utime_t heartbeat_interval; /**< Interval to send heartbeats to Dir */
bool use_pam_authentication_; /**< Use this console as a PAM console */
ConsoleResource() : TlsResource() {}
};

Expand Down
4 changes: 0 additions & 4 deletions core/src/dird/auth_pam.cc
Expand Up @@ -40,10 +40,6 @@ struct PamData {
, passwd_(passwd) { }
};

/*
* PAM-Callback called by Bareos PAM-Handler
*
*/
static bool PamConvSendMessage(BareosSocket *UA_sock, const char *msg, int msg_style)
{
char buf = msg_style;
Expand Down
45 changes: 26 additions & 19 deletions core/src/lib/bnet.cc
Expand Up @@ -590,43 +590,50 @@ const char *BnetSigToAscii(BareosSocket * bs)
}
}

uint32_t ReadoutCommandIdFromString(std::string message)
bool ReadoutCommandIdFromString(const std::string &message, uint32_t &id_out)
{
size_t pos = message.find(' ');
const char delimiter = ' ';
size_t pos = message.find(delimiter);
if (pos == std::string::npos) {
return kProtokollError;
id_out = kMessageIdProtokollError;
return false;
}
std::string id_string;
id_string = message.substr(0,pos);

uint32_t id;
size_t pos1;

try {
id = std::stoul(id_string);
id = std::stoul(message, &pos1);
} catch (const std::exception &e) {
id = kProtokollError;
id_out = kMessageIdProtokollError;
return false;
}
if (pos == pos1) {
id_out = id;
return true;
} else {
id_out = kMessageIdProtokollError;
return false;
}
return id;
}

uint32_t ReceiveAndEvaluateResponse(BareosSocket *bsock, std::string &message_output)
bool ReceiveAndEvaluateResponse(BareosSocket *bsock, uint32_t &id_out, std::string &message_out)
{
int recv_return_value = bsock->recv();
bsock->StopTimer();

if (recv_return_value <= 0) {
return kReceiveError;
return false;
}

Dmsg1(10, "<bsockd: %s", bsock->msg);

std::string message(bsock->msg);
uint32_t id = ReadoutCommandIdFromString(message);
const std::string message(bsock->msg);
uint32_t id;
bool ok = ReadoutCommandIdFromString(message, id);

id_out = id;
message_out = message;

// if (!bstrncmp(bsock->msg, OKhello, sizeof(OKhello) - 1)) {
// Bsnprintf(response, response_len, _("bsockector at \"%s:%d\" rejected Hello command\n"), bsock->host(),
// bsock->port());
// return false;
// } else {
// Bsnprintf(response, response_len, "%s", bsock->msg);
// }
return ok;
}
18 changes: 9 additions & 9 deletions core/src/lib/bnet.h
Expand Up @@ -50,16 +50,16 @@ BareosSocket *BnetBind(int port);
BareosSocket *BnetAccept(BareosSocket *bsock, char *who);

enum : uint32_t {
kUnknown = 0,
kProtokollError = 1,
kReceiveError = 2,
kOk = 1000,
kPamRequired = 1001,
kPamInteractive = 4001,
kPamUserCredentials = 4002
kMessageIdUnknown = 0,
kMessageIdProtokollError = 1,
kMessageIdReceiveError = 2,
kMessageIdOk = 1000,
kMessageIdPamRequired = 1001,
kMessageIdPamInteractive = 4001,
kMessageIdPamUserCredentials = 4002
};

uint32_t ReadoutCommandIdFromString(std::string message);
uint32_t ReceiveAndEvaluateResponse(BareosSocket *bsock, std::string &message);
bool ReadoutCommandIdFromString(const std::string &message, uint32_t &id_out);
bool ReceiveAndEvaluateResponse(BareosSocket *bsock, uint32_t &id_out, std::string &message_out);

#endif // BAREOS_LIB_BNET_H_
42 changes: 10 additions & 32 deletions core/src/lib/bsock.cc
Expand Up @@ -323,9 +323,6 @@ static char hello[] = "Hello %s calling\n";
/** Response from Director */
static char OKhello[] = "1000 OK:";

/**
* Authenticate with Director
*/
bool BareosSocket::AuthenticateWithDirector(JobControlRecord *jcr,
const char *identity,
s_password &password,
Expand All @@ -350,40 +347,21 @@ bool BareosSocket::AuthenticateWithDirector(JobControlRecord *jcr,
dir->StartTimer(60 * 5);
dir->fsend(hello, bashed_name);

if (!AuthenticateOutboundConnection(jcr, "Director", identity, password, tls_resource)) { goto bail_out; }

Dmsg1(6, ">dird: %s", dir->msg);
if (dir->recv() <= 0) {
if (!AuthenticateOutboundConnection(jcr, "Director", identity, password, tls_resource)) {
dir->StopTimer();
Bsnprintf(response, response_len,
_("Bad response to Hello command: ERR=%s\n"
"The Director at \"%s:%d\" is probably not running.\n"),
dir->bstrerror(), dir->host(), dir->port());
return false;
}

dir->StopTimer();
Dmsg1(10, "<dird: %s", dir->msg);
if (!bstrncmp(dir->msg, OKhello, sizeof(OKhello) - 1)) {
Bsnprintf(response, response_len, _("Director at \"%s:%d\" rejected Hello command\n"), dir->host(),
dir->port());
return false;
} else {
Bsnprintf(response, response_len, "%s", dir->msg);
}

return true;

bail_out:
dir->StopTimer();
Bsnprintf(response, response_len,
_("Authorization problem with Director at \"%s:%d\"\n"
"Most likely the passwords do not agree.\n"
"If you are using TLS, there may have been a certificate "
"validation error during the TLS handshake.\n"
"Please see %s for help.\n"),
dir->host(), dir->port(), MANUAL_AUTH_URL);
Dmsg1(6, ">dird: %s", dir->msg);

uint32_t message_id;
std::string received_message;
if (ReceiveAndEvaluateResponse(dir, message_id, received_message)) {
if (message_id == kMessageIdOk) {
Bsnprintf(response, response_len, "%s\n", received_message.c_str());
return true;
}
}
return false;
}

Expand Down
29 changes: 20 additions & 9 deletions core/src/tests/lib_tests.cc
Expand Up @@ -23,14 +23,25 @@
#include "include/bareos.h"
#include "lib/bnet.h"

TEST(ReadoutCommandIdFromStringTest, BNet)
TEST(BNet, ReadoutCommandIdFromStringTest)
{
int id;
std::string message1 {"1000 OK: <director-name> Version: <version>"};
id = ReadoutCommandIdFromString(message1);
EXPECT_EQ(id, 1000);

std::string message2 {"1001 OK: <director-name> Version: <version>"};
id = ReadoutCommandIdFromString(message2);
EXPECT_NE(id, 1000);
bool ok;
uint32_t id;

const std::string message1 {"1000 OK: <director-name> Version: <version>"};
ok = ReadoutCommandIdFromString(message1, id);
EXPECT_EQ(id, kMessageIdOk);
EXPECT_EQ(ok, true);

const std::string message2 {"1001 OK: <director-name> Version: <version>"};
ok = ReadoutCommandIdFromString(message2, id);
EXPECT_NE(id, kMessageIdOk);
EXPECT_EQ(ok, true);

const char *m3 {"10A1 OK: <director-name> Version: <version>"};
const std::string message3 (m3);
ok = ReadoutCommandIdFromString(message3, id);
EXPECT_EQ(id, kMessageIdProtokollError);
EXPECT_EQ(ok, false);
EXPECT_STREQ(message3.c_str(), m3);
}

0 comments on commit 7cc5875

Please sign in to comment.