[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[PATCH] Don't hardcode the device for luksOpen. (#500830)



Hardcoded device names are really bad, and we shouldn't be using them.

We don't interpret variables in strings, and it's very invasive to add
that functionality, so instead, build the entire cryptsetup command line
ahead of time, and pass it as an environment variable to plymouth.

This adds two commands, setDeviceEnv and buildEnv.  setDeviceEnv stashes
a device ("/dev/sda2" or "UUID=blah" or "LABEL=blah") into an
environmental variable.  buildEnv builds an environmental variable from
its command line.
---
 mkinitrd    |    7 ++++-
 nash/nash.c |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/mkinitrd b/mkinitrd
index 271f0c4..df7a400 100755
--- a/mkinitrd
+++ b/mkinitrd
@@ -1580,8 +1580,11 @@ emitdmraids
 
 emitcrypto()
 {
-    emit "echo Setting up disk encryption: $1"
-    emit "plymouth ask-for-password --command \"cryptsetup luksOpen $1 $2\""
+    local luksuuid=$(grep "^$2 " /etc/crypttab | awk '{ print $2 }')
+    emit "setDeviceEnv LUKSUUID $luksuuid"
+    emit "echo Setting up disk encryption: \$LUKSUUID"
+    emit "buildEnv LUKSUUID cryptsetup luksOpen \$LUKSUUID $2"
+    emit "plymouth ask-for-password --command \$LUKSUUID"
 }
 
 for cryptdev in ${!cryptopart } ; do
diff --git a/nash/nash.c b/nash/nash.c
index b951578..44af527 100644
--- a/nash/nash.c
+++ b/nash/nash.c
@@ -1451,6 +1451,10 @@ resolveDeviceCommand(char *cmd, char *end)
         eprintf("resolveDevice: device spec expected\n");
         return 1;
     }
+    if (cmd < end) {
+        eprintf("resolveDevice: unexpected arguments\n");
+        return 1;
+    }
 
     device = nashGetPathBySpec(_nash_context, spec);
     if (device) {
@@ -1460,6 +1464,63 @@ resolveDeviceCommand(char *cmd, char *end)
     return 1;
 }
 
+static int
+buildEnvCommand(char *cmd, char *end)
+{
+    char *name = NULL;
+    char *arg = NULL;
+    char *value = NULL;
+    ssize_t len = 1;
+
+    value = calloc(1, len);
+
+    if (!(cmd = getArg(cmd, end, &name))) {
+        eprintf("buildEnv: environment variable expected\n");
+        return 1;
+    }
+
+    while ((cmd = getArg(cmd, end, &arg))) {
+        len += strlen(" ") + strlen(arg);
+        if (value[0] != '\0')
+            strcat(value, " ");
+        strcat(value, arg);
+    }
+    if (value[0] == '\0') {
+        eprintf("buildEnv: value expected\n");
+        return 1;
+    }
+    setenv(name, value, 1);
+    return 0;
+}
+
+static int
+setDeviceEnvCommand(char *cmd, char *end)
+{
+    char *env = NULL;
+    char *spec = NULL;
+    char *device = NULL;
+
+    if (!(cmd = getArg(cmd, end, &env))) {
+        eprintf("setDeviceEnv: environment variable expected\n");
+        return 1;
+    }
+    if (!(cmd = getArg(cmd, end, &spec))) {
+        eprintf("setDeviceEnv: device spec expected\n");
+        return 1;
+    }
+    if (cmd < end) {
+        eprintf("setDeviceEnv: unexpected arguments\n");
+        return 1;
+    }
+
+    device = nashGetPathBySpec(_nash_context, spec);
+    if (device) {
+        setenv(env, device, 1);
+        return 0;
+    }
+    return 1;
+}
+
 /* 2.6 magic swsusp stuff */
 static int
 resumeCommand(char * cmd, char * end)
@@ -3176,6 +3237,7 @@ setQuietCommand(char * cmd, char * end)
 
 static const struct commandHandler handlers[] = {
     { "access", accessCommand },
+    { "buildEnv", buildEnvCommand },
 #ifdef DEBUG
     { "cat", catCommand },
 #endif
@@ -3214,6 +3276,7 @@ static const struct commandHandler handlers[] = {
     { "resume", resumeCommand },
     { "resolveDevice", resolveDeviceCommand },
     { "rmparts", rmpartsCommand },
+    { "setDeviceEnv", setDeviceEnvCommand },
     { "setquiet", setQuietCommand },
     { "setuproot", setuprootCommand },
     { "showelfinterp", showElfInterpCommand },
-- 
1.6.2.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]