mirror of
https://github.com/postgres/postgres.git
synced 2025-10-25 13:17:41 +03:00
libpq error message refactoring, part 2
This applies the new APIs to the code. Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://www.postgresql.org/message-id/flat/7c0232ef-7b44-68db-599d-b327d0640a77@enterprisedb.com
This commit is contained in:
@@ -218,14 +218,12 @@ scram_exchange(void *opaq, char *input, int inputlen,
|
||||
{
|
||||
if (inputlen == 0)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("malformed SCRAM message (empty message)\n"));
|
||||
libpq_append_conn_error(conn, "malformed SCRAM message (empty message)");
|
||||
goto error;
|
||||
}
|
||||
if (inputlen != strlen(input))
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("malformed SCRAM message (length mismatch)\n"));
|
||||
libpq_append_conn_error(conn, "malformed SCRAM message (length mismatch)");
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
@@ -268,15 +266,13 @@ scram_exchange(void *opaq, char *input, int inputlen,
|
||||
*/
|
||||
if (!verify_server_signature(state, success, &errstr))
|
||||
{
|
||||
appendPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("could not verify server signature: %s\n"), errstr);
|
||||
libpq_append_conn_error(conn, "could not verify server signature: %s", errstr);
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!*success)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("incorrect server signature\n"));
|
||||
libpq_append_conn_error(conn, "incorrect server signature");
|
||||
}
|
||||
*done = true;
|
||||
state->state = FE_SCRAM_FINISHED;
|
||||
@@ -284,8 +280,7 @@ scram_exchange(void *opaq, char *input, int inputlen,
|
||||
|
||||
default:
|
||||
/* shouldn't happen */
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("invalid SCRAM exchange state\n"));
|
||||
libpq_append_conn_error(conn, "invalid SCRAM exchange state");
|
||||
goto error;
|
||||
}
|
||||
return;
|
||||
@@ -311,18 +306,18 @@ read_attr_value(char **input, char attr, PQExpBuffer errorMessage)
|
||||
|
||||
if (*begin != attr)
|
||||
{
|
||||
appendPQExpBuffer(errorMessage,
|
||||
libpq_gettext("malformed SCRAM message (attribute \"%c\" expected)\n"),
|
||||
attr);
|
||||
libpq_append_error(errorMessage,
|
||||
"malformed SCRAM message (attribute \"%c\" expected)",
|
||||
attr);
|
||||
return NULL;
|
||||
}
|
||||
begin++;
|
||||
|
||||
if (*begin != '=')
|
||||
{
|
||||
appendPQExpBuffer(errorMessage,
|
||||
libpq_gettext("malformed SCRAM message (expected character \"=\" for attribute \"%c\")\n"),
|
||||
attr);
|
||||
libpq_append_error(errorMessage,
|
||||
"malformed SCRAM message (expected character \"=\" for attribute \"%c\")",
|
||||
attr);
|
||||
return NULL;
|
||||
}
|
||||
begin++;
|
||||
@@ -361,8 +356,7 @@ build_client_first_message(fe_scram_state *state)
|
||||
*/
|
||||
if (!pg_strong_random(raw_nonce, SCRAM_RAW_NONCE_LEN))
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("could not generate nonce\n"));
|
||||
libpq_append_conn_error(conn, "could not generate nonce");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -371,16 +365,14 @@ build_client_first_message(fe_scram_state *state)
|
||||
state->client_nonce = malloc(encoded_len + 1);
|
||||
if (state->client_nonce == NULL)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("out of memory\n"));
|
||||
libpq_append_conn_error(conn, "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
encoded_len = pg_b64_encode(raw_nonce, SCRAM_RAW_NONCE_LEN,
|
||||
state->client_nonce, encoded_len);
|
||||
if (encoded_len < 0)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("could not encode nonce\n"));
|
||||
libpq_append_conn_error(conn, "could not encode nonce");
|
||||
return NULL;
|
||||
}
|
||||
state->client_nonce[encoded_len] = '\0';
|
||||
@@ -446,8 +438,7 @@ build_client_first_message(fe_scram_state *state)
|
||||
|
||||
oom_error:
|
||||
termPQExpBuffer(&buf);
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("out of memory\n"));
|
||||
libpq_append_conn_error(conn, "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -569,9 +560,7 @@ build_client_final_message(fe_scram_state *state)
|
||||
client_proof, &errstr))
|
||||
{
|
||||
termPQExpBuffer(&buf);
|
||||
appendPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("could not calculate client proof: %s\n"),
|
||||
errstr);
|
||||
libpq_append_conn_error(conn, "could not calculate client proof: %s", errstr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -586,8 +575,7 @@ build_client_final_message(fe_scram_state *state)
|
||||
if (encoded_len < 0)
|
||||
{
|
||||
termPQExpBuffer(&buf);
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("could not encode client proof\n"));
|
||||
libpq_append_conn_error(conn, "could not encode client proof");
|
||||
return NULL;
|
||||
}
|
||||
buf.len += encoded_len;
|
||||
@@ -602,8 +590,7 @@ build_client_final_message(fe_scram_state *state)
|
||||
|
||||
oom_error:
|
||||
termPQExpBuffer(&buf);
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("out of memory\n"));
|
||||
libpq_append_conn_error(conn, "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -623,8 +610,7 @@ read_server_first_message(fe_scram_state *state, char *input)
|
||||
state->server_first_message = strdup(input);
|
||||
if (state->server_first_message == NULL)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("out of memory\n"));
|
||||
libpq_append_conn_error(conn, "out of memory");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -641,16 +627,14 @@ read_server_first_message(fe_scram_state *state, char *input)
|
||||
if (strlen(nonce) < strlen(state->client_nonce) ||
|
||||
memcmp(nonce, state->client_nonce, strlen(state->client_nonce)) != 0)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("invalid SCRAM response (nonce mismatch)\n"));
|
||||
libpq_append_conn_error(conn, "invalid SCRAM response (nonce mismatch)");
|
||||
return false;
|
||||
}
|
||||
|
||||
state->nonce = strdup(nonce);
|
||||
if (state->nonce == NULL)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("out of memory\n"));
|
||||
libpq_append_conn_error(conn, "out of memory");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -664,8 +648,7 @@ read_server_first_message(fe_scram_state *state, char *input)
|
||||
state->salt = malloc(decoded_salt_len);
|
||||
if (state->salt == NULL)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("out of memory\n"));
|
||||
libpq_append_conn_error(conn, "out of memory");
|
||||
return false;
|
||||
}
|
||||
state->saltlen = pg_b64_decode(encoded_salt,
|
||||
@@ -674,8 +657,7 @@ read_server_first_message(fe_scram_state *state, char *input)
|
||||
decoded_salt_len);
|
||||
if (state->saltlen < 0)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("malformed SCRAM message (invalid salt)\n"));
|
||||
libpq_append_conn_error(conn, "malformed SCRAM message (invalid salt)");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -688,14 +670,12 @@ read_server_first_message(fe_scram_state *state, char *input)
|
||||
state->iterations = strtol(iterations_str, &endptr, 10);
|
||||
if (*endptr != '\0' || state->iterations < 1)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("malformed SCRAM message (invalid iteration count)\n"));
|
||||
libpq_append_conn_error(conn, "malformed SCRAM message (invalid iteration count)");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (*input != '\0')
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("malformed SCRAM message (garbage at end of server-first-message)\n"));
|
||||
libpq_append_conn_error(conn, "malformed SCRAM message (garbage at end of server-first-message)");
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -714,8 +694,7 @@ read_server_final_message(fe_scram_state *state, char *input)
|
||||
state->server_final_message = strdup(input);
|
||||
if (!state->server_final_message)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("out of memory\n"));
|
||||
libpq_append_conn_error(conn, "out of memory");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -730,9 +709,8 @@ read_server_final_message(fe_scram_state *state, char *input)
|
||||
/* read_attr_value() has appended an error message */
|
||||
return false;
|
||||
}
|
||||
appendPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("error received from server in SCRAM exchange: %s\n"),
|
||||
errmsg);
|
||||
libpq_append_conn_error(conn, "error received from server in SCRAM exchange: %s",
|
||||
errmsg);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -746,15 +724,13 @@ read_server_final_message(fe_scram_state *state, char *input)
|
||||
}
|
||||
|
||||
if (*input != '\0')
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("malformed SCRAM message (garbage at end of server-final-message)\n"));
|
||||
libpq_append_conn_error(conn, "malformed SCRAM message (garbage at end of server-final-message)");
|
||||
|
||||
server_signature_len = pg_b64_dec_len(strlen(encoded_server_signature));
|
||||
decoded_server_signature = malloc(server_signature_len);
|
||||
if (!decoded_server_signature)
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("out of memory\n"));
|
||||
libpq_append_conn_error(conn, "out of memory");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -765,8 +741,7 @@ read_server_final_message(fe_scram_state *state, char *input)
|
||||
if (server_signature_len != SCRAM_KEY_LEN)
|
||||
{
|
||||
free(decoded_server_signature);
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("malformed SCRAM message (invalid server signature)\n"));
|
||||
libpq_append_conn_error(conn, "malformed SCRAM message (invalid server signature)");
|
||||
return false;
|
||||
}
|
||||
memcpy(state->ServerSignature, decoded_server_signature, SCRAM_KEY_LEN);
|
||||
|
||||
Reference in New Issue
Block a user