Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTERNAL: Make the -S option available with ascii protocol #819

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -7693,6 +7693,46 @@ static void write_and_free(conn *c, char *buf, int bytes)
c->write_and_go = conn_new_cmd;
}

static bool authenticated_ascii(conn *c, token_t *tokens, const size_t ntokens)
{
if (c->authenticated) {
return true;
}

if ((ntokens >= 5) &&
(strcmp(tokens[COMMAND_TOKEN].value, "bop") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "lop") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "mop") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "sop") == 0)) {
return (strncmp(tokens[KEY_TOKEN+1].value, "arcus:", 6) == 0);
}
if ((ntokens >= 3) &&
(strcmp(tokens[COMMAND_TOKEN].value, "get") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "set") == 0)) {
return (strncmp(tokens[KEY_TOKEN].value, "arcus:", 6) == 0);
}
if ((ntokens >= 2) &&
(strcmp(tokens[COMMAND_TOKEN].value, "dump") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "cmdlog") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "config") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "flush_all") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "flush_prefix") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "getattr") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "help") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "lqdetect") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "quit") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "ready") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "scan") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "setattr") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "shutdown") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "stats") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "version") == 0 ||
strcmp(tokens[COMMAND_TOKEN].value, "zkensmeble") == 0)) {
return true;
}
return false;
Copy link
Collaborator

@jhpark816 jhpark816 Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@namsic
์•„๋ž˜์™€ ๊ฐ™์ด ์กฐ๊ธˆ์ด๋ผ๋„ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐฉ์•ˆ์„ ๊ฒ€ํ† ํ•ด ์ฃผ์„ธ์š”.

    if ((ntokens >= 5) &&
        (strcmp(tokens[COMMAND_TOKEN].value+1, "op ") == 0)) { // lop, sop, mop, bop
        return (strncmp(tokens[COMMAND_TOKEN+2].value, "arcus:", 6) == 0);
    }
    if ((ntokens >= 3) &&
        (strcmp(tokens[COMMAND_TOKEN].value+1, "et ") == 0)) { // get, set
        return (strncmp(tokens[KEY_TOKEN].value, "arcus:", 6) == 0);
    }
    if ((ntokens >= 2) &&
        (strcmp(tokens[COMMAND_TOKEN].value, "dump") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "cmdlog") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "config") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "flush_all") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "flush_prefix") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "getattr") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "help") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "lqdetect") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "scan") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "setattr") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "stats") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "zkensemble") == 0)) {
        return true;
    }
    if ((ntokens == 2) &&
        (strcmp(tokens[COMMAND_TOKEN].value, "quit") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "ready") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "version") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "shutdown") == 0)) {
        return true;
    } 

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฒซ ๊ธ€์ž ํ™•์ธํ•˜์—ฌ switchํ•œ ๋’ค ๋ฌธ์ž์—ด ๋น„๊ตํ•˜๋Š” ๊ตฌํ˜„๋„ ๊ฒ€ํ† ํ•  ๋งŒํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

https://github.com/naver/arcus-c-client/blob/09c014d1fd0a97f3ab19ea46422251a4db3dbf25/libmemcached/response.cc#L281

  switch(buffer[0])
  {
  case 'V':
    if (memcmp(buffer, "VALUE", 5) == 0)
    {
      /* We add back in one because we will need to search for END */
      memcached_server_response_increment(ptr);
      return textual_value_fetch(ptr, buffer, buffer_length, result);
    }
    else if (memcmp(buffer, "VERSION", 7) == 0)
    {
      /* Find the space, and then move one past it to copy version */
      char *version_ptr= index(buffer, ' ');
      version_ptr++;
      return textual_version_fetch(ptr, version_ptr);
    }
    break;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@namsic
c client ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•ฉ์‹œ๋‹ค.

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„๋ž˜ ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝํ•˜์‹œ์ฃ . ์•„๋ž˜ ์ฝ”๋“œ๊ฐ€ ์ง๊ด€์ ์œผ๋กœ ์ฝ๊ธฐ๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค.

static bool authenticated_ascii(conn *c, token_t *tokens, const size_t ntokens)
{
    if (c->authenticated) {
        return true;
    }
    if ((ntokens >= 2) &&
        (strcmp(tokens[COMMAND_TOKEN].value, "dump") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "cmdlog") == 0 ||
         . . . 
         strcmp(tokens[COMMAND_TOKEN].value, "version") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "zkensemble") == 0)) {
        return true;
    }
    if ((ntokens >= 3) &&
        (strcmp(tokens[COMMAND_TOKEN].value, "set") == 0 ||
         strcmp(tokens[COMMAND_TOKEN].value, "get") == 0)) {
        return (strncmp(tokens[KEY_TOKEN].value, "arcus:", 6) == 0);
    }
    if ((ntokens >= 5) &&
        (strcmp(tokens[COMMAND_TOKEN].value, "lop") == 0 ||
         ...
         strcmp(tokens[COMMAND_TOKEN].value, "bop") == 0)) {
        return (strncmp(tokens[COMMAND_TOKEN+2].value, "arcus:", 6) == 0);
    }
    return false;
}


#ifdef JHPARK_OLD_SMGET_INTERFACE
static inline int set_smget_mode_maybe(conn *c, token_t *tokens, size_t ntokens)
{
Expand Down Expand Up @@ -13092,6 +13132,11 @@ static void process_command_ascii(conn *c, char *command, int cmdlen)

ntokens = tokenize_command(command, cmdlen, tokens, MAX_TOKENS);

if (settings.require_sasl && !authenticated_ascii(c, tokens, ntokens)) {
out_string(c, "CLIENT_ERROR unauthenticated");
return;
}

if ((ntokens >= 3) && (strcmp(tokens[COMMAND_TOKEN].value, "get") == 0))
{
process_get_command(c, tokens, ntokens, false);
Expand Down Expand Up @@ -15061,7 +15106,6 @@ int main (int argc, char **argv)
int cache_memory_limit = 0;
int sticky_memory_limit = 0;

bool protocol_specified = false;
bool tcp_specified = false;
bool udp_specified = false;

Expand Down Expand Up @@ -15274,7 +15318,6 @@ int main (int argc, char **argv)
settings.backlog = atoi(optarg);
break;
case 'B':
protocol_specified = true;
if (strcmp(optarg, "auto") == 0) {
settings.binding_protocol = negotiating_prot;
} else if (strcmp(optarg, "binary") == 0) {
Expand Down Expand Up @@ -15399,23 +15442,6 @@ int main (int argc, char **argv)
}
}

if (settings.require_sasl) {
if (!protocol_specified) {
settings.binding_protocol = binary_prot;
} else {
if (settings.binding_protocol == negotiating_prot) {
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"ERROR: You cannot use auto-negotiating protocol while requiring SASL.\n");
exit(EX_USAGE);
}
if (settings.binding_protocol == ascii_prot) {
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"ERROR: You cannot use only ASCII protocol while requiring SASL.\n");
exit(EX_USAGE);
}
}
}

if (udp_specified && settings.udpport != 0 && !tcp_specified) {
settings.port = settings.udpport;
}
Expand Down
17 changes: 1 addition & 16 deletions t/binary-sasl.t.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if (supports_sasl()) {
plan skip_all => "The binary 'saslpasswd' is missing from your system";
}
else {
plan tests => 33;
plan tests => 30;
}
} else {
plan tests => 1;
Expand All @@ -42,21 +42,6 @@ if (supports_sasl()) {
exit 0;
}

eval {
my $server = get_memcached($engine, "-S -B auto");
};
ok($@, "SASL shouldn't be used with protocol auto negotiate");

eval {
my $server = get_memcached($engine, "-S -B ascii");
};
ok($@, "SASL isn't implemented in the ascii protocol");

eval {
my $server = get_memcached($engine, "-S -B binary -B ascii");
};
ok($@, "SASL isn't implemented in the ascii protocol");

# Based almost 100% off testClient.py which is:
# Copyright (c) 2007 Dustin Sallings <[email protected]>

Expand Down
Loading