From 068c4c8677f8c8a95d1cebbc13182d0ce319fffa Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 27 Jun 2025 14:02:56 +0200 Subject: [PATCH] shared/bus-unit-util: define helper for LoadCredential=/LoadCredentialEncrypted= --- src/shared/bus-unit-util.c | 96 ++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 46 deletions(-) diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c index 04db19aa475..cda870cc0fd 100644 --- a/src/shared/bus-unit-util.c +++ b/src/shared/bus-unit-util.c @@ -1065,6 +1065,54 @@ static int bus_append_set_credential(sd_bus_message *m, const char *field, const return 1; } +static int bus_append_load_credential(sd_bus_message *m, const char *field, const char *eq) { + int r; + + r = sd_bus_message_open_container(m, 'r', "sv"); + if (r < 0) + return bus_log_create_error(r); + + r = sd_bus_message_append_basic(m, 's', field); + if (r < 0) + return bus_log_create_error(r); + + r = sd_bus_message_open_container(m, 'v', "a(ss)"); + if (r < 0) + return bus_log_create_error(r); + + if (isempty(eq)) + r = sd_bus_message_append(m, "a(ss)", 0); + else { + _cleanup_free_ char *word = NULL; + const char *p = eq; + + r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS); + if (r == -ENOMEM) + return log_oom(); + if (r < 0) + return log_error_errno(r, "Failed to parse %s= parameter: %s", field, eq); + if (r == 0) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Missing argument to %s=.", field); + + if (isempty(p)) /* If only one field is specified, then this means "inherit from above" */ + p = eq; + + r = sd_bus_message_append(m, "a(ss)", 1, word, p); + } + if (r < 0) + return bus_log_create_error(r); + + r = sd_bus_message_close_container(m); + if (r < 0) + return bus_log_create_error(r); + + r = sd_bus_message_close_container(m); + if (r < 0) + return bus_log_create_error(r); + + return 1; +} + static int bus_append_cgroup_property(sd_bus_message *m, const char *field, const char *eq) { if (STR_IN_SET(field, "DevicePolicy", "Slice", @@ -1338,52 +1386,8 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con if (STR_IN_SET(field, "SetCredential", "SetCredentialEncrypted")) return bus_append_set_credential(m, field, eq); - - if (STR_IN_SET(field, "LoadCredential", "LoadCredentialEncrypted")) { - r = sd_bus_message_open_container(m, 'r', "sv"); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_append_basic(m, 's', field); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_open_container(m, 'v', "a(ss)"); - if (r < 0) - return bus_log_create_error(r); - - if (isempty(eq)) - r = sd_bus_message_append(m, "a(ss)", 0); - else { - _cleanup_free_ char *word = NULL; - const char *p = eq; - - r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS); - if (r == -ENOMEM) - return log_oom(); - if (r < 0) - return log_error_errno(r, "Failed to parse %s= parameter: %s", field, eq); - if (r == 0) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Missing argument to %s=.", field); - - if (isempty(p)) /* If only one field is specified, then this means "inherit from above" */ - p = eq; - - r = sd_bus_message_append(m, "a(ss)", 1, word, p); - } - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_close_container(m); - if (r < 0) - return bus_log_create_error(r); - - r = sd_bus_message_close_container(m); - if (r < 0) - return bus_log_create_error(r); - - return 1; - } + if (STR_IN_SET(field, "LoadCredential", "LoadCredentialEncrypted")) + return bus_append_load_credential(m, field, eq); if (streq(field, "ImportCredential")) { if (isempty(eq)) -- 2.47.3