Skip to content

Commit 0c8f5fd

Browse files
committed
fix auth fail
1 parent b3f2824 commit 0c8f5fd

4 files changed

Lines changed: 62 additions & 45 deletions

File tree

src/acl.cc

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ bool User::MatchPassword(const std::string& password) {
214214
void User::GetUserDescribe(CmdRes* res) {
215215
std::shared_lock l(mutex_);
216216

217-
res->AppendArrayLen(6);
217+
res->AppendArrayLen(12);
218218

219219
res->AppendString("flags");
220220
std::vector<std::string> vector;
@@ -235,17 +235,21 @@ void User::GetUserDescribe(CmdRes* res) {
235235
size_t i = 0;
236236
for (const auto& selector : selectors_) {
237237
vector.clear();
238-
if (i == 0) {
238+
if (i == 0) { // root selector
239239
selector->ACLDescribeSelector(vector);
240-
res->AppendStringVector(vector);
240+
for (const auto& item : vector) {
241+
res->AppendString(item);
242+
}
243+
244+
res->AppendString("selectors");
241245
if (selectors_.size() == 1) {
242-
res->AppendArrayLen(-1);
246+
res->AppendArrayLen(0);
243247
}
244248
++i;
245249
continue;
246250
}
247251
if (i == 1) {
248-
res->AppendArrayLen(selectors_.size() - 1);
252+
res->AppendArrayLen(static_cast<int64_t>(selectors_.size()) - 1);
249253
}
250254
selector->ACLDescribeSelector(vector);
251255
res->AppendStringVector(vector);
@@ -382,7 +386,7 @@ pstd::Status Acl::LoadUserFromFile(const std::string& fileName) {
382386
std::shared_ptr<User> defaultUser;
383387

384388
while (sequentialFile->ReadLine(line, lineLength) != nullptr) {
385-
int lineLen = strlen(line);
389+
size_t lineLen = strlen(line);
386390
if (lineLen == 0) {
387391
continue;
388392
}
@@ -465,10 +469,6 @@ std::shared_ptr<User> Acl::CreateDefaultUser() {
465469
std::shared_ptr<User> Acl::CreatedUser(const std::string& name) { return std::make_shared<User>(name); }
466470

467471
pstd::Status Acl::SetUser(const std::string& userName, std::vector<std::string>& op) {
468-
if (op.empty()) {
469-
return pstd::Status::OK();
470-
}
471-
472472
auto user = GetUser(userName, true);
473473

474474
bool add = false;
@@ -549,6 +549,14 @@ std::shared_ptr<User> Acl::Auth(const std::string& userName, const std::string&
549549
if (!user) {
550550
return nullptr;
551551
}
552+
if (user->HasFlags(static_cast<uint32_t>(AclUserFlag::DISABLED))) {
553+
return nullptr;
554+
}
555+
556+
if (user->HasFlags(static_cast<uint32_t>(AclUserFlag::NO_PASS))) {
557+
return user;
558+
}
559+
552560
if (user->MatchPassword(pstd::sha256(password))) {
553561
return user;
554562
}
@@ -912,7 +920,7 @@ void AclSelector::ACLDescribeSelector(std::vector<std::string>& vector) {
912920
if (HasFlags(static_cast<uint32_t>(AclSelectorFlag::ALL_KEYS))) {
913921
vector.emplace_back("+@all");
914922
} else {
915-
vector.emplace_back(commandRules_);
923+
vector.emplace_back(commandRules_ == "" ? "-@all" : commandRules_);
916924
}
917925

918926
vector.emplace_back("key");

src/pika_acl.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void PikaAclCmd::Cat() {
9494
void PikaAclCmd::DelUser() {
9595
std::vector<std::string> userNames(argv_.begin() + 2, argv_.end());
9696
auto delUserNames = g_pika_server->Acl()->DeleteUser(userNames);
97-
res().AppendInteger(delUserNames.size());
97+
res().AppendInteger(static_cast<int64_t>(delUserNames.size()));
9898

9999
g_pika_server->AllClientUnAuth(delUserNames);
100100
}
@@ -216,6 +216,11 @@ void PikaAclCmd::SetUser() {
216216
if (argv_.size() > 3) {
217217
rule = std::vector<std::string>(argv_.begin() + 3, argv_.end());
218218
}
219+
220+
if (pstd::isspace(argv_[2])){
221+
res().SetRes(CmdRes::kErrOther, "Usernames can't contain spaces or null characters");
222+
return;
223+
}
219224
auto status = g_pika_server->Acl()->SetUser(argv_[2], rule);
220225
if (status.ok()) {
221226
res().SetRes(CmdRes::kOk);

src/pika_admin.cc

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
#include "include/pika_admin.h"
77

8+
#include <sys/statvfs.h>
89
#include <sys/time.h>
910
#include <sys/utsname.h>
10-
#include <sys/statvfs.h>
1111

1212
#include <algorithm>
1313
#include <unordered_map>
@@ -60,10 +60,12 @@ enum AuthResult {
6060
};
6161

6262
static AuthResult AuthenticateUser(const std::string& userName, const std::string& pwd,
63-
const std::shared_ptr<net::NetConn>& conn) {
64-
std::string root_password(g_pika_conf->requirepass());
65-
if (userName == Acl::DefaultUser && root_password.empty()) {
66-
return AuthResult::NO_REQUIRE_PASS;
63+
const std::shared_ptr<net::NetConn>& conn, bool defaultAuth) {
64+
if (defaultAuth) {
65+
auto defaultUser = g_pika_server->Acl()->GetUser(Acl::DefaultUser, true);
66+
if (defaultUser->HasFlags(static_cast<uint32_t>(AclUserFlag::NO_PASS))) {
67+
return AuthResult::NO_REQUIRE_PASS;
68+
}
6769
}
6870

6971
auto user = g_pika_server->Acl()->Auth(userName, pwd);
@@ -261,22 +263,24 @@ void AuthCmd::Do(std::shared_ptr<Slot> slot) {
261263

262264
std::string userName = "";
263265
std::string pwd = "";
266+
bool defaultAuth = false;
264267
if (argv_.size() == 2) {
265268
userName = Acl::DefaultUser;
266269
pwd = argv_[1];
270+
defaultAuth = true;
267271
} else {
268272
userName = argv_[1];
269273
pwd = argv_[2];
270274
}
271275

272-
auto authResult = AuthenticateUser(userName, pwd, conn);
276+
auto authResult = AuthenticateUser(userName, pwd, conn, defaultAuth);
273277

274278
switch (authResult) {
275279
case AuthResult::INVALID_CONN:
276280
res_.SetRes(CmdRes::kErrOther, kCmdNamePing);
277281
return;
278282
case AuthResult::INVALID_PASSWORD:
279-
res_.SetRes(CmdRes::kInvalidPwd);
283+
res_.SetRes(CmdRes::kErrOther, "WRONGPASS invalid username-password pair or user is disabled");
280284
return;
281285
case AuthResult::NO_REQUIRE_PASS:
282286
res_.SetRes(CmdRes::kErrOther, "Client sent AUTH, but no password is set");
@@ -858,8 +862,10 @@ void InfoCmd::InfoStats(std::string& info) {
858862
tmp_stream << "total_commands_processed:" << g_pika_server->ServerQueryNum() << "\r\n";
859863

860864
// Network stats
861-
tmp_stream << "total_net_input_bytes:" << g_pika_server->NetInputBytes() + g_pika_server->NetReplInputBytes() << "\r\n";
862-
tmp_stream << "total_net_output_bytes:" << g_pika_server->NetOutputBytes() + g_pika_server->NetReplOutputBytes() << "\r\n";
865+
tmp_stream << "total_net_input_bytes:" << g_pika_server->NetInputBytes() + g_pika_server->NetReplInputBytes()
866+
<< "\r\n";
867+
tmp_stream << "total_net_output_bytes:" << g_pika_server->NetOutputBytes() + g_pika_server->NetReplOutputBytes()
868+
<< "\r\n";
863869
tmp_stream << "total_net_repl_input_bytes:" << g_pika_server->NetReplInputBytes() << "\r\n";
864870
tmp_stream << "total_net_repl_output_bytes:" << g_pika_server->NetReplOutputBytes() << "\r\n";
865871
tmp_stream << "instantaneous_input_kbps:" << g_pika_server->InstantaneousInputKbps() << "\r\n";
@@ -1263,25 +1269,24 @@ void InfoCmd::InfoDebug(std::string& info) {
12631269
}
12641270

12651271
void InfoCmd::InfoCommandStats(std::string& info) {
1266-
std::stringstream tmp_stream;
1267-
tmp_stream.precision(2);
1268-
tmp_stream.setf(std::ios::fixed);
1269-
tmp_stream << "# Commandstats" << "\r\n";
1270-
for (auto iter : *g_pika_server->GetCommandStatMap()) {
1271-
if (iter.second.cmd_count != 0) {
1272-
tmp_stream << iter.first << ":"
1273-
<< "calls=" << iter.second.cmd_count << ", usec="
1274-
<< MethodofTotalTimeCalculation(iter.second.cmd_time_consuming)
1275-
<< ", usec_per_call=";
1276-
if (!iter.second.cmd_time_consuming) {
1277-
tmp_stream << 0 << "\r\n";
1278-
} else {
1279-
tmp_stream << MethodofCommandStatistics(iter.second.cmd_time_consuming, iter.second.cmd_count)
1280-
<< "\r\n";
1281-
}
1272+
std::stringstream tmp_stream;
1273+
tmp_stream.precision(2);
1274+
tmp_stream.setf(std::ios::fixed);
1275+
tmp_stream << "# Commandstats"
1276+
<< "\r\n";
1277+
for (auto iter : *g_pika_server->GetCommandStatMap()) {
1278+
if (iter.second.cmd_count != 0) {
1279+
tmp_stream << iter.first << ":"
1280+
<< "calls=" << iter.second.cmd_count
1281+
<< ", usec=" << MethodofTotalTimeCalculation(iter.second.cmd_time_consuming) << ", usec_per_call=";
1282+
if (!iter.second.cmd_time_consuming) {
1283+
tmp_stream << 0 << "\r\n";
1284+
} else {
1285+
tmp_stream << MethodofCommandStatistics(iter.second.cmd_time_consuming, iter.second.cmd_count) << "\r\n";
12821286
}
12831287
}
1284-
info.append(tmp_stream.str());
1288+
}
1289+
info.append(tmp_stream.str());
12851290
}
12861291

12871292
void ConfigCmd::DoInitial() {
@@ -1344,7 +1349,7 @@ static void EncodeString(std::string* dst, const std::string& value) {
13441349
dst->append(kNewLine);
13451350
}
13461351

1347-
template<class T>
1352+
template <class T>
13481353
static void EncodeNumber(std::string* dst, const T v) {
13491354
std::string vstr = std::to_string(v);
13501355
dst->append("$");
@@ -2216,7 +2221,7 @@ void ConfigCmd::ConfigRewrite(std::string& ret) {
22162221
}
22172222
}
22182223

2219-
void ConfigCmd::ConfigRewriteReplicationID(std::string &ret) {
2224+
void ConfigCmd::ConfigRewriteReplicationID(std::string& ret) {
22202225
if (g_pika_conf->ConfigRewriteReplicationID() != 0) {
22212226
ret = "+OK\r\n";
22222227
} else {
@@ -2532,13 +2537,13 @@ void HelloCmd::Do(std::shared_ptr<Slot> slot) {
25322537
const std::string opt = argv_[next_arg];
25332538
if ((strcasecmp(opt.data(), "AUTH") == 0) && (more_args != 0U)) {
25342539
const std::string pwd = argv_[next_arg + 1];
2535-
auto authResult = AuthenticateUser(Acl::DefaultUser, pwd, conn);
2540+
auto authResult = AuthenticateUser(Acl::DefaultUser, pwd, conn, true);
25362541
switch (authResult) {
25372542
case AuthResult::INVALID_CONN:
25382543
res_.SetRes(CmdRes::kErrOther, kCmdNamePing);
25392544
return;
25402545
case AuthResult::INVALID_PASSWORD:
2541-
res_.SetRes(CmdRes::kInvalidPwd);
2546+
res_.SetRes(CmdRes::kErrOther, "WRONGPASS invalid username-password pair or user is disabled");
25422547
return;
25432548
case AuthResult::NO_REQUIRE_PASS:
25442549
res_.SetRes(CmdRes::kErrOther, "Client sent AUTH, but no password is set");
@@ -2630,12 +2635,12 @@ void DiskRecoveryCmd::Do(std::shared_ptr<Slot> slot) {
26302635
db_item.second->SetBinlogIoErrorrelieve();
26312636
std::shared_lock slot_rwl(slots_rw);
26322637
// loop every slot
2633-
for (const auto &slot_item: db_item.second->GetSlots()) {
2638+
for (const auto& slot_item : db_item.second->GetSlots()) {
26342639
background_errors_.clear();
26352640
slot_item.second->DbRWLockReader();
26362641
slot_item.second->db()->GetUsage(storage::PROPERTY_TYPE_ROCKSDB_BACKGROUND_ERRORS, &background_errors_);
26372642
slot_item.second->DbRWUnLock();
2638-
for (const auto &item: background_errors_) {
2643+
for (const auto& item : background_errors_) {
26392644
if (item.second != 0) {
26402645
rocksdb::Status s = slot_item.second->db()->GetDBByType(item.first)->Resume();
26412646
if (!s.ok()) {

src/pika_command.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,6 @@ void Cmd::Initial(const PikaCmdArgsType& argv, const std::string& db_name) {
788788

789789
std::vector<std::string> Cmd::current_key() const {
790790
std::vector<std::string> res;
791-
res.emplace_back("");
792791
return res;
793792
}
794793

0 commit comments

Comments
 (0)