* \password -- set user password */
| 2018 | * \password -- set user password |
| 2019 | */ |
| 2020 | static backslashResult |
| 2021 | exec_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; |
no test coverage detected