From: Zbigniew Jędrzejewski-Szmek Date: Wed, 10 Dec 2025 14:42:51 +0000 (+0100) Subject: homectl: split out helpers parse_{fido2_device,pkcs11_token_uri}_field X-Git-Tag: v260-rc1~391^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47f631fda58cc6431ffb7c65c957529b02ab499f;p=thirdparty%2Fsystemd.git homectl: split out helpers parse_{fido2_device,pkcs11_token_uri}_field Both implement the 'list' verb. --- diff --git a/src/home/homectl.c b/src/home/homectl.c index 99fd0c23066..06bb8e82b47 100644 --- a/src/home/homectl.c +++ b/src/home/homectl.c @@ -3768,6 +3768,81 @@ static int parse_tmpfs_limit_field( return drop_from_identity(field); } +static int parse_pkcs11_token_uri_field(const char *arg) { + int r; + + assert(arg); + + if (streq(arg, "list")) + return pkcs11_list_tokens(); + + /* If --pkcs11-token-uri= is specified we always drop everything old */ + FOREACH_STRING(p, "pkcs11TokenUri", "pkcs11EncryptedKey") { + r = drop_from_identity(p); + if (r < 0) + return r; + } + + if (isempty(arg)) { + arg_pkcs11_token_uri = strv_free(arg_pkcs11_token_uri); + return 1; + } + + if (streq(arg, "auto")) { + char *found; + r = pkcs11_find_token_auto(&found); + if (r < 0) + return r; + + r = strv_consume(&arg_pkcs11_token_uri, found); + } else { + if (!pkcs11_uri_valid(arg)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid PKCS#11 URI: %s", arg); + + r = strv_extend(&arg_pkcs11_token_uri, arg); + } + if (r < 0) + return r; + + strv_uniq(arg_pkcs11_token_uri); + return 1; +} + +static int parse_fido2_device_field(const char *arg) { + int r; + + assert(arg); + + if (streq(arg, "list")) + return fido2_list_devices(); + + FOREACH_STRING(p, "fido2HmacCredential", "fido2HmacSalt") { + r = drop_from_identity(p); + if (r < 0) + return r; + } + + if (isempty(arg)) { + arg_fido2_device = strv_free(arg_fido2_device); + return 1; + } + + if (streq(arg, "auto")) { + char *found; + r = fido2_find_device_auto(&found); + if (r < 0) + return r; + + r = strv_consume(&arg_fido2_device, found); + } else + r = strv_extend(&arg_fido2_device, arg); + if (r < 0) + return r; + + strv_uniq(arg_fido2_device); + return 1; +} + static int help(int argc, char *argv[], void *userdata) { _cleanup_free_ char *link = NULL; int r; @@ -4628,38 +4703,9 @@ static int parse_argv(int argc, char *argv[]) { } case ARG_PKCS11_TOKEN_URI: - if (streq(optarg, "list")) - return pkcs11_list_tokens(); - - /* If --pkcs11-token-uri= is specified we always drop everything old */ - FOREACH_STRING(p, "pkcs11TokenUri", "pkcs11EncryptedKey") { - r = drop_from_identity(p); - if (r < 0) - return r; - } - - if (isempty(optarg)) { - arg_pkcs11_token_uri = strv_free(arg_pkcs11_token_uri); - break; - } - - if (streq(optarg, "auto")) { - _cleanup_free_ char *found = NULL; - - r = pkcs11_find_token_auto(&found); - if (r < 0) - return r; - r = strv_consume(&arg_pkcs11_token_uri, TAKE_PTR(found)); - } else { - if (!pkcs11_uri_valid(optarg)) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid PKCS#11 URI: %s", optarg); - - r = strv_extend(&arg_pkcs11_token_uri, optarg); - } - if (r < 0) + r = parse_pkcs11_token_uri_field(optarg); + if (r <= 0) return r; - - strv_uniq(arg_pkcs11_token_uri); break; case ARG_FIDO2_CRED_ALG: @@ -4669,34 +4715,9 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_FIDO2_DEVICE: - if (streq(optarg, "list")) - return fido2_list_devices(); - - FOREACH_STRING(p, "fido2HmacCredential", "fido2HmacSalt") { - r = drop_from_identity(p); - if (r < 0) - return r; - } - - if (isempty(optarg)) { - arg_fido2_device = strv_free(arg_fido2_device); - break; - } - - if (streq(optarg, "auto")) { - _cleanup_free_ char *found = NULL; - - r = fido2_find_device_auto(&found); - if (r < 0) - return r; - - r = strv_consume(&arg_fido2_device, TAKE_PTR(found)); - } else - r = strv_extend(&arg_fido2_device, optarg); - if (r < 0) + r = parse_fido2_device_field(optarg); + if (r <= 0) return r; - - strv_uniq(arg_fido2_device); break; case ARG_FIDO2_WITH_PIN: