MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / helloCommand

Function helloCommand

src/networking.cpp:3611–3686  ·  view source on GitHub ↗

HELLO [ [AUTH ] [SETNAME ] ] */

Source from the content-addressed store, hash-verified

3609
3610/* HELLO [<protocol-version> [AUTH <user> <password>] [SETNAME <name>] ] */
3611void helloCommand(client *c) {
3612 long long ver = 0;
3613 int next_arg = 1;
3614
3615 if (c->argc >= 2) {
3616 if (getLongLongFromObjectOrReply(c, c->argv[next_arg++], &ver,
3617 "Protocol version is not an integer or out of range") != C_OK) {
3618 return;
3619 }
3620
3621 if (ver < 2 || ver > 3) {
3622 addReplyError(c,"-NOPROTO unsupported protocol version");
3623 return;
3624 }
3625 }
3626
3627 for (int j = next_arg; j < c->argc; j++) {
3628 int moreargs = (c->argc-1) - j;
3629 const char *opt = (const char*)ptrFromObj(c->argv[j]);
3630 if (!strcasecmp(opt,"AUTH") && moreargs >= 2) {
3631 redactClientCommandArgument(c, j+1);
3632 redactClientCommandArgument(c, j+2);
3633 if (ACLAuthenticateUser(c, c->argv[j+1], c->argv[j+2]) == C_ERR) {
3634 addReplyError(c,"-WRONGPASS invalid username-password pair or user is disabled.");
3635 return;
3636 }
3637 j += 2;
3638 } else if (!strcasecmp(opt,"SETNAME") && moreargs) {
3639 if (clientSetNameOrReply(c, c->argv[j+1]) == C_ERR) return;
3640 j++;
3641 } else {
3642 addReplyErrorFormat(c,"Syntax error in HELLO option '%s'",opt);
3643 return;
3644 }
3645 }
3646
3647 /* At this point we need to be authenticated to continue. */
3648 if (!c->authenticated) {
3649 addReplyError(c,"-NOAUTH HELLO must be called with the client already "
3650 "authenticated, otherwise the HELLO AUTH <user> <pass> "
3651 "option can be used to authenticate the client and "
3652 "select the RESP protocol version at the same time");
3653 return;
3654 }
3655
3656 /* Let's switch to the specified RESP mode. */
3657 if (ver) c->resp = ver;
3658 addReplyMapLen(c,6 + !g_pserver->sentinel_mode);
3659
3660 addReplyBulkCString(c,"server");
3661 addReplyBulkCString(c,"redis");
3662
3663 addReplyBulkCString(c,"version");
3664 addReplyBulkCString(c,KEYDB_SET_VERSION);
3665
3666 addReplyBulkCString(c,"proto");
3667 addReplyLongLong(c,c->resp);
3668

Callers

nothing calls this directly

Calls 11

addReplyErrorFunction · 0.85
ptrFromObjFunction · 0.85
ACLAuthenticateUserFunction · 0.85
clientSetNameOrReplyFunction · 0.85
addReplyErrorFormatFunction · 0.85
addReplyMapLenFunction · 0.85
addReplyBulkCStringFunction · 0.85
addReplyLongLongFunction · 0.85
addReplyLoadedModulesFunction · 0.85

Tested by

no test coverage detected