5d2ee9
From 9040e15cd3cba546b47aeae0ea133afa1a6ad292 Mon Sep 17 00:00:00 2001
5d2ee9
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
5d2ee9
Date: Wed, 13 Nov 2019 10:32:30 +0100
5d2ee9
Subject: [PATCH] cryptsetup-generator: guess whether the keyfile argument is
5d2ee9
 two items or one
5d2ee9
5d2ee9
Fixes #13615.
5d2ee9
5d2ee9
See the inline comment for documentation.
5d2ee9
5d2ee9
(cherry picked from commit 32c6237a7c2e697d2fc4f3403319db16858fb8e3)
5d2ee9
5d2ee9
Related: #1763155
5d2ee9
---
5d2ee9
 src/cryptsetup/cryptsetup-generator.c | 45 ++++++++++++++++++---------
5d2ee9
 1 file changed, 30 insertions(+), 15 deletions(-)
5d2ee9
5d2ee9
diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c
5d2ee9
index a09983b576..4117930925 100644
5d2ee9
--- a/src/cryptsetup/cryptsetup-generator.c
5d2ee9
+++ b/src/cryptsetup/cryptsetup-generator.c
5d2ee9
@@ -54,17 +54,36 @@ static int split_keyspec(const char *keyspec, char **ret_keyfile, char **ret_key
5d2ee9
 
5d2ee9
         c = strrchr(keyspec, ':');
5d2ee9
         if (c) {
5d2ee9
-                keyfile = strndup(keyspec, c-keyspec);
5d2ee9
-                keydev = strdup(c + 1);
5d2ee9
-                if (!keyfile || !keydev)
5d2ee9
+                /* The keydev part has to be either an absolute path to device node (/dev/something,
5d2ee9
+                 * /dev/foo/something, or even possibly /dev/foo/something:part), or a fstab device
5d2ee9
+                 * specification starting with LABEL= or similar. The keyfile part has the same syntax.
5d2ee9
+                 *
5d2ee9
+                 * Let's try to guess if the second part looks like a keydev specification, or just part of a
5d2ee9
+                 * filename with a colon. fstab_node_to_udev_node() will convert the fstab device syntax to
5d2ee9
+                 * an absolute path. If we didn't get an absolute path, assume that it is just part of the
5d2ee9
+                 * first keyfile argument. */
5d2ee9
+
5d2ee9
+                keydev = fstab_node_to_udev_node(c + 1);
5d2ee9
+                if (!keydev)
5d2ee9
                         return log_oom();
5d2ee9
-        } else {
5d2ee9
+
5d2ee9
+                if (path_is_absolute(keydev))
5d2ee9
+                        keyfile = strndup(keyspec, c-keyspec);
5d2ee9
+                else {
5d2ee9
+                        log_debug("Keyspec argument contains a colon, but \"%s\" doesn't look like a device specification.\n"
5d2ee9
+                                  "Assuming that \"%s\" is a single device specification.",
5d2ee9
+                                  c + 1, keyspec);
5d2ee9
+                        keydev = mfree(keydev);
5d2ee9
+                        c = NULL;
5d2ee9
+                }
5d2ee9
+        }
5d2ee9
+
5d2ee9
+        if (!c)
5d2ee9
                 /* No keydev specified */
5d2ee9
                 keyfile = strdup(keyspec);
5d2ee9
-                keydev = NULL;
5d2ee9
-                if (!keyfile)
5d2ee9
-                        return log_oom();
5d2ee9
-        }
5d2ee9
+
5d2ee9
+        if (!keyfile)
5d2ee9
+                return log_oom();
5d2ee9
 
5d2ee9
         *ret_keyfile = TAKE_PTR(keyfile);
5d2ee9
         *ret_keydev = TAKE_PTR(keydev);
5d2ee9
@@ -73,7 +92,7 @@ static int split_keyspec(const char *keyspec, char **ret_keyfile, char **ret_key
5d2ee9
 }
5d2ee9
 
5d2ee9
 static int generate_keydev_mount(const char *name, const char *keydev, const char *keydev_timeout, bool canfail, char **unit, char **mount) {
5d2ee9
-        _cleanup_free_ char *u = NULL, *what = NULL, *where = NULL, *name_escaped = NULL, *device_unit = NULL;
5d2ee9
+        _cleanup_free_ char *u = NULL, *where = NULL, *name_escaped = NULL, *device_unit = NULL;
5d2ee9
         _cleanup_fclose_ FILE *f = NULL;
5d2ee9
         int r;
5d2ee9
         usec_t timeout_us;
5d2ee9
@@ -111,22 +130,18 @@ static int generate_keydev_mount(const char *name, const char *keydev, const cha
5d2ee9
         if (r < 0)
5d2ee9
                 return r;
5d2ee9
 
5d2ee9
-        what = fstab_node_to_udev_node(keydev);
5d2ee9
-        if (!what)
5d2ee9
-                return -ENOMEM;
5d2ee9
-
5d2ee9
         fprintf(f,
5d2ee9
                 "[Unit]\n"
5d2ee9
                 "DefaultDependencies=no\n\n"
5d2ee9
                 "[Mount]\n"
5d2ee9
                 "What=%s\n"
5d2ee9
                 "Where=%s\n"
5d2ee9
-                "Options=ro%s\n", what, where, canfail ? ",nofail" : "");
5d2ee9
+                "Options=ro%s\n", keydev, where, canfail ? ",nofail" : "");
5d2ee9
 
5d2ee9
         if (keydev_timeout) {
5d2ee9
                 r = parse_sec_fix_0(keydev_timeout, &timeout_us);
5d2ee9
                 if (r >= 0) {
5d2ee9
-                        r = unit_name_from_path(what, ".device", &device_unit);
5d2ee9
+                        r = unit_name_from_path(keydev, ".device", &device_unit);
5d2ee9
                         if (r < 0)
5d2ee9
                                 return log_error_errno(r, "Failed to generate unit name: %m");
5d2ee9