Skip to content

Commit

Permalink
bsock: moved response message functions into class bsock
Browse files Browse the repository at this point in the history
  • Loading branch information
franku committed Oct 30, 2018
1 parent d21a11a commit c87a00a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions core/src/console/console.cc
Expand Up @@ -879,10 +879,10 @@ static bool ExaminePamAuthentication(bool use_pam_credentials_file, const std::s
}
BStringList args;
args << user << pw;
FormatAndSendResponseMessage(UA_sock, kMessageIdPamUserCredentials, args);
UA_sock->FormatAndSendResponseMessage(kMessageIdPamUserCredentials, args);
}
} else {
FormatAndSendResponseMessage(UA_sock, kMessageIdPamInteractive, "OK");
UA_sock->FormatAndSendResponseMessage(kMessageIdPamInteractive, "OK");
if (!ConsolePamAuthenticate(stdin, UA_sock)) {
TerminateConsole(0);
return false;
Expand Down
6 changes: 3 additions & 3 deletions core/src/dird/authenticate.cc
Expand Up @@ -259,9 +259,9 @@ static void SendOkMessage(UaContext *ua)
::snprintf(buffer, 100, "OK: %s Version: %s (%s)", my_name, VERSION, BDATE);

if (ua->cons && ua->cons->use_pam_authentication_) {
FormatAndSendResponseMessage(ua->UA_sock, kMessageIdPamRequired, std::string(buffer));
ua->UA_sock->FormatAndSendResponseMessage(kMessageIdPamRequired, std::string(buffer));
} else {
FormatAndSendResponseMessage(ua->UA_sock, kMessageIdOk, std::string(buffer));
ua->UA_sock->FormatAndSendResponseMessage(kMessageIdOk, std::string(buffer));
}
}

Expand Down Expand Up @@ -319,7 +319,7 @@ static bool OptionalAuthenticatePamUser(std::string console_name, UaContext *ua,
uint32_t response_id;
BStringList message_arguments;

if (!ReceiveAndEvaluateResponseMessage(ua->UA_sock, response_id, message_arguments)) {
if (!ua->UA_sock->ReceiveAndEvaluateResponseMessage(response_id, message_arguments)) {
Dmsg2(100, "Could not evaluate response_id: %d - %d", response_id,
message_arguments.JoinReadable().c_str());
auth_success = false;
Expand Down
18 changes: 9 additions & 9 deletions core/src/lib/bnet.cc
Expand Up @@ -627,17 +627,17 @@ bool EvaluateResponseMessageId(const std::string &message, uint32_t &id_out, BSt
return ok;
}

bool ReceiveAndEvaluateResponseMessage(BareosSocket *bsock, uint32_t &id_out, BStringList &args_out)
bool BareosSocket::ReceiveAndEvaluateResponseMessage(uint32_t &id_out, BStringList &args_out)
{
int ret = bsock->recv();
bsock->StopTimer();
int ret = recv();
StopTimer();

if (ret <= 0) {
Dmsg1(100, "Error while receiving response message: %s", bsock->msg);
Dmsg1(100, "Error while receiving response message: %s", msg);
return false;
}

std::string message(bsock->msg);
std::string message(msg);

if (message.empty()) {
Dmsg0(100, "Received message is empty\n");
Expand All @@ -647,24 +647,24 @@ bool ReceiveAndEvaluateResponseMessage(BareosSocket *bsock, uint32_t &id_out, BS
return EvaluateResponseMessageId(message, id_out, args_out);
}

bool FormatAndSendResponseMessage(BareosSocket *bsock, uint32_t id, const BStringList &list_of_agruments)
bool BareosSocket::FormatAndSendResponseMessage(uint32_t id, const BStringList &list_of_agruments)
{
std::string m = std::to_string(id);
m += AsciiControlCharacters::RecordSeparator();
m += list_of_agruments.Join(AsciiControlCharacters::RecordSeparator());

if (bsock->send(m.c_str(), m.size()) <=0 ) {
if (send(m.c_str(), m.size()) <=0 ) {
Dmsg1(100, "Could not send response message: %d\n", m.c_str());
return false;
}
return true;
}

bool FormatAndSendResponseMessage(BareosSocket *bsock, uint32_t id, const std::string &str)
bool BareosSocket::FormatAndSendResponseMessage(uint32_t id, const std::string &str)
{
BStringList message;
message << str;
message << "\n";

return FormatAndSendResponseMessage(bsock, id, message);
return FormatAndSendResponseMessage(id, message);
}
2 changes: 1 addition & 1 deletion core/src/lib/bsock.cc
Expand Up @@ -351,7 +351,7 @@ bool BareosSocket::ConsoleAuthenticateWithDirector(JobControlRecord *jcr,

uint32_t message_id;
BStringList args;
if (ReceiveAndEvaluateResponseMessage(dir, message_id, args)) {
if (dir->ReceiveAndEvaluateResponseMessage(message_id, args)) {
response_id = message_id;
Bsnprintf(response_text, response_len, "%s\n", args.JoinReadable().c_str());
return true;
Expand Down
4 changes: 4 additions & 0 deletions core/src/lib/bsock.h
Expand Up @@ -45,6 +45,7 @@
struct btimer_t; /* forward reference */
class BareosSocket;
class Tls;
class BStringList;
btimer_t *StartBsockTimer(BareosSocket *bs, uint32_t wait);
void StopBsockTimer(btimer_t *wid);

Expand Down Expand Up @@ -185,6 +186,9 @@ class BareosSocket : public SmartAlloc {
bool IsCleartextBareosHello();
void OutputCipherMessageString(std::function<void(const char *)>);
void GetCipherMessageString(std::string &str);
bool ReceiveAndEvaluateResponseMessage(uint32_t &id_out, BStringList &args_out);
bool FormatAndSendResponseMessage(uint32_t id, const BStringList &list_of_agruments);
bool FormatAndSendResponseMessage(uint32_t id, const std::string &str);

bool AuthenticateOutboundConnection(JobControlRecord *jcr,
const char *what,
Expand Down
4 changes: 2 additions & 2 deletions core/src/tests/bsock_test.cc
Expand Up @@ -446,11 +446,11 @@ TEST(BNet, FormatAndSendResponseMessage)

std::string m("Test123");

FormatAndSendResponseMessage(test_sockets->client.get(), kMessageIdOk, m);
test_sockets->client->FormatAndSendResponseMessage(kMessageIdOk, m);

uint32_t id = kMessageIdUnknown;
BStringList args;
bool ok = ReceiveAndEvaluateResponseMessage(test_sockets->server.get(), id, args);
bool ok = test_sockets->server->ReceiveAndEvaluateResponseMessage(id, args);

EXPECT_TRUE(ok) << "ReceiveAndEvaluateResponseMessage errored.";
EXPECT_EQ(id, kMessageIdOk) << "Wrong MessageID received.";
Expand Down

0 comments on commit c87a00a

Please sign in to comment.