From b079aa3498ac2ae2bffbeacedd8b9430b8375c0e Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Tue, 2 Aug 2022 08:28:49 +0200 Subject: [PATCH] RPC: Fix "id" not showing up in error responses. rpc_error() and rpc_error_fmt() were called with a NULL request. This also fixes logging of RPC errors to show the name of the RPC call. --- src/modules/rpc/server_ban.c | 38 ++++++++++++++++++------------------ src/modules/rpc/user.c | 4 ++-- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/modules/rpc/server_ban.c b/src/modules/rpc/server_ban.c index 08ef71609..e94325e0a 100644 --- a/src/modules/rpc/server_ban.c +++ b/src/modules/rpc/server_ban.c @@ -132,34 +132,34 @@ RPC_CALL_FUNC(rpc_server_ban_get) name = json_object_get_string(params, "name"); if (!name) { - rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'name'"); + rpc_error(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'name'"); return; } type_name = json_object_get_string(params, "type"); if (!type_name) { - rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'type'"); + rpc_error(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'type'"); return; } tkl_type_char = tkl_configtypetochar(type_name); if (!tkl_type_char) { - rpc_error_fmt(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Invalid type: '%s'", type_name); + rpc_error_fmt(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Invalid type: '%s'", type_name); return; } tkl_type_int = tkl_chartotype(tkl_type_char); if (!server_ban_parse_mask(client, 0, tkl_type_int, name, &usermask, &hostmask, &soft, &error)) { - rpc_error_fmt(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Error: %s", error); + rpc_error_fmt(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Error: %s", error); return; } if (!(tkl = find_tkl_serverban(tkl_type_int, usermask, hostmask, soft))) { - rpc_error(client, NULL, JSON_RPC_ERROR_NOT_FOUND, "Ban not found"); + rpc_error(client, request, JSON_RPC_ERROR_NOT_FOUND, "Ban not found"); return; } @@ -186,21 +186,21 @@ RPC_CALL_FUNC(rpc_server_ban_del) name = json_object_get_string(params, "name"); if (!name) { - rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'name'"); + rpc_error(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'name'"); return; } type_name = json_object_get_string(params, "type"); if (!type_name) { - rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'type'"); + rpc_error(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'type'"); return; } tkl_type_char = tkl_configtypetochar(type_name); if (!tkl_type_char) { - rpc_error_fmt(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Invalid type: '%s'", type_name); + rpc_error_fmt(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Invalid type: '%s'", type_name); return; } tkl_type_int = tkl_chartotype(tkl_type_char); @@ -209,13 +209,13 @@ RPC_CALL_FUNC(rpc_server_ban_del) if (!server_ban_parse_mask(client, 0, tkl_type_int, name, &usermask, &hostmask, &soft, &error)) { - rpc_error_fmt(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Error: %s", error); + rpc_error_fmt(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Error: %s", error); return; } if (!(tkl = find_tkl_serverban(tkl_type_int, usermask, hostmask, soft))) { - rpc_error(client, NULL, JSON_RPC_ERROR_NOT_FOUND, "Ban not found"); + rpc_error(client, request, JSON_RPC_ERROR_NOT_FOUND, "Ban not found"); return; } @@ -237,7 +237,7 @@ RPC_CALL_FUNC(rpc_server_ban_del) /* Actually this may not be an internal error, it could be an * incorrect request, such as asking to remove a config-based ban. */ - rpc_error(client, NULL, JSON_RPC_ERROR_INTERNAL_ERROR, "Unable to remove item"); + rpc_error(client, request, JSON_RPC_ERROR_INTERNAL_ERROR, "Unable to remove item"); } json_decref(result); } @@ -262,21 +262,21 @@ RPC_CALL_FUNC(rpc_server_ban_add) name = json_object_get_string(params, "name"); if (!name) { - rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'name'"); + rpc_error(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'name'"); return; } type_name = json_object_get_string(params, "type"); if (!type_name) { - rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'type'"); + rpc_error(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'type'"); return; } tkl_type_char = tkl_configtypetochar(type_name); if (!tkl_type_char) { - rpc_error_fmt(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Invalid type: '%s'", type_name); + rpc_error_fmt(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Invalid type: '%s'", type_name); return; } tkl_type_int = tkl_chartotype(tkl_type_char); @@ -286,7 +286,7 @@ RPC_CALL_FUNC(rpc_server_ban_add) reason = json_object_get_string(params, "reason"); if (!reason) { - rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'reason'"); + rpc_error(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'reason'"); return; } @@ -306,19 +306,19 @@ RPC_CALL_FUNC(rpc_server_ban_add) if ((tkl_expire_at != 0) && (tkl_expire_at < TStime())) { - rpc_error_fmt(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Error: the specified expiry time is before current time (before now)"); + rpc_error_fmt(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Error: the specified expiry time is before current time (before now)"); return; } if (!server_ban_parse_mask(client, 0, tkl_type_int, name, &usermask, &hostmask, &soft, &error)) { - rpc_error_fmt(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Error: %s", error); + rpc_error_fmt(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Error: %s", error); return; } if (find_tkl_serverban(tkl_type_int, usermask, hostmask, soft)) { - rpc_error(client, NULL, JSON_RPC_ERROR_ALREADY_EXISTS, "A ban with that mask already exists"); + rpc_error(client, request, JSON_RPC_ERROR_ALREADY_EXISTS, "A ban with that mask already exists"); return; } @@ -328,7 +328,7 @@ RPC_CALL_FUNC(rpc_server_ban_add) if (!tkl) { - rpc_error(client, NULL, JSON_RPC_ERROR_INTERNAL_ERROR, "Unable to add item"); + rpc_error(client, request, JSON_RPC_ERROR_INTERNAL_ERROR, "Unable to add item"); return; } diff --git a/src/modules/rpc/user.c b/src/modules/rpc/user.c index 6a54ca3c7..530390271 100644 --- a/src/modules/rpc/user.c +++ b/src/modules/rpc/user.c @@ -93,7 +93,7 @@ RPC_CALL_FUNC(rpc_user_get) nick = json_object_get_string(params, "nick"); if (!nick) { - rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'nick'"); + rpc_error(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'nick'"); return; } @@ -101,7 +101,7 @@ RPC_CALL_FUNC(rpc_user_get) { // FIXME: wrong error! // consider re-using IRC numerics? the positive ones, eg ERR_NOSUCHNICK - rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_REQUEST, "Nickname not found"); + rpc_error(client, request, JSON_RPC_ERROR_INVALID_REQUEST, "Nickname not found"); return; }