MCPcopy Create free account
hub / github.com/apache/cloudberry / exec_command_password

Function exec_command_password

src/bin/psql/command.c:2020–2093  ·  view source on GitHub ↗

* \password -- set user password */

Source from the content-addressed store, hash-verified

2018 * \password -- set user password
2019 */
2020static backslashResult
2021exec_command_password(PsqlScanState scan_state, bool active_branch)
2022{
2023 bool success = true;
2024
2025 if (active_branch)
2026 {
2027 char *user = psql_scan_slash_option(scan_state,
2028 OT_SQLID, NULL, true);
2029 char *pw1;
2030 char *pw2;
2031 PQExpBufferData buf;
2032
2033 if (user == NULL)
2034 {
2035 /* By default, the command applies to CURRENT_USER */
2036 PGresult *res;
2037
2038 res = PSQLexec("SELECT CURRENT_USER");
2039 if (!res)
2040 return PSQL_CMD_ERROR;
2041
2042 user = pg_strdup(PQgetvalue(res, 0, 0));
2043 PQclear(res);
2044 }
2045
2046 initPQExpBuffer(&buf);
2047 printfPQExpBuffer(&buf, _("Enter new password for user \"%s\": "), user);
2048
2049 pw1 = simple_prompt(buf.data, false);
2050 pw2 = simple_prompt("Enter it again: ", false);
2051
2052 if (strcmp(pw1, pw2) != 0)
2053 {
2054 pg_log_error("Passwords didn't match.");
2055 success = false;
2056 }
2057 else
2058 {
2059 char *encrypted_password;
2060
2061 encrypted_password = PQencryptPasswordConn(pset.db, pw1, user, NULL);
2062
2063 if (!encrypted_password)
2064 {
2065 pg_log_info("%s", PQerrorMessage(pset.db));
2066 success = false;
2067 }
2068 else
2069 {
2070 PGresult *res;
2071
2072 printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ",
2073 fmtId(user));
2074 appendStringLiteralConn(&buf, encrypted_password, pset.db);
2075 res = PSQLexec(buf.data);
2076 if (!res)
2077 success = false;

Callers 1

exec_commandFunction · 0.85

Calls 14

PSQLexecFunction · 0.85
pg_strdupFunction · 0.85
PQgetvalueFunction · 0.85
PQclearFunction · 0.85
initPQExpBufferFunction · 0.85
printfPQExpBufferFunction · 0.85
simple_promptFunction · 0.85
PQencryptPasswordConnFunction · 0.85
PQerrorMessageFunction · 0.85
fmtIdFunction · 0.85
appendStringLiteralConnFunction · 0.85
PQfreememFunction · 0.85

Tested by

no test coverage detected