chiark / gitweb /
unit: introduce ConditionVirtualization=
[elogind.git] / src / cryptsetup.c
index 91a4436ae1531f75bb75230817ca35dfba63f20a..c80572aed9672f26c0255cbac561182b21a017dc 100644 (file)
 
 #include <string.h>
 #include <errno.h>
+#include <sys/mman.h>
 
 #include <libcryptsetup.h>
+#include <libudev.h>
 
 #include "log.h"
 #include "util.h"
@@ -37,6 +39,18 @@ static bool opt_readonly = false;
 static bool opt_verify = false;
 static usec_t opt_timeout = 0;
 
+/* Options Debian's crypttab knows we don't:
+
+    offset=
+    skip=
+    precheck=
+    check=
+    checkargs=
+    noearly=
+    loud=
+    keyscript=
+*/
+
 static int parse_one_option(const char *option) {
         assert(option);
 
@@ -127,11 +141,47 @@ static void log_glue(int level, const char *msg, void *usrptr) {
         log_debug("%s", msg);
 }
 
+static char *disk_description(const char *path) {
+        struct udev *udev = NULL;
+        struct udev_device *device = NULL;
+        struct stat st;
+        char *description = NULL;
+        const char *model;
+
+        assert(path);
+
+        if (stat(path, &st) < 0)
+                return NULL;
+
+        if (!S_ISBLK(st.st_mode))
+                return NULL;
+
+        if (!(udev = udev_new()))
+                return NULL;
+
+        if (!(device = udev_device_new_from_devnum(udev, 'b', st.st_rdev)))
+                goto finish;
+
+        if ((model = udev_device_get_property_value(device, "ID_MODEL_FROM_DATABASE")) ||
+            (model = udev_device_get_property_value(device, "ID_MODEL")))
+                description = strdup(model);
+
+finish:
+        if (device)
+                udev_device_unref(device);
+
+        if (udev)
+                udev_unref(udev);
+
+        return description;
+}
+
 int main(int argc, char *argv[]) {
         int r = EXIT_FAILURE;
         struct crypt_device *cd = NULL;
         char *password = NULL, *truncated_cipher = NULL;
-        const char *cipher = NULL, *cipher_mode = NULL, *hash = NULL;
+        const char *cipher = NULL, *cipher_mode = NULL, *hash = NULL, *name = NULL;
+        char *description = NULL;
 
         if (argc < 3) {
                 log_error("This program requires at least two arguments.");
@@ -155,7 +205,10 @@ int main(int argc, char *argv[]) {
                         goto finish;
                 }
 
-                if (argc >= 5 && argv[4][0] && !streq(argv[4], "-")) {
+                if (argc >= 5 &&
+                    argv[4][0] &&
+                    !streq(argv[4], "-") &&
+                    !streq(argv[4], "none")) {
 
                         if (!path_is_absolute(argv[4]))
                                 log_error("Password file path %s is not absolute. Ignoring.", argv[4]);
@@ -166,6 +219,12 @@ int main(int argc, char *argv[]) {
                 if (argc >= 6 && argv[5][0] && !streq(argv[5], "-"))
                         parse_options(argv[5]);
 
+                /* A delicious drop of snake oil */
+                mlockall(MCL_FUTURE);
+
+                description = disk_description(argv[3]);
+                name = description ? description : argv[2];
+
                 if ((k = crypt_init(&cd, argv[3]))) {
                         log_error("crypt_init() failed: %s", strerror(-k));
                         goto finish;
@@ -213,8 +272,17 @@ int main(int argc, char *argv[]) {
                         password = NULL;
 
                         if (!key_file) {
+                                char *text;
+
+                                if (asprintf(&text, "Please enter passphrase for disk %s!", name) < 0) {
+                                        log_error("Out of memory");
+                                        goto finish;
+                                }
+
+                                k = ask_password_auto(text, "drive-harddisk", until, &password);
+                                free(text);
 
-                                if ((k = ask_password_auto("Please enter passphrase for disk:", "drive-harddisk", until, &password)) < 0) {
+                                if (k < 0) {
                                         log_error("Failed to query password: %s", strerror(-k));
                                         goto finish;
                                 }
@@ -222,7 +290,15 @@ int main(int argc, char *argv[]) {
                                 if (opt_verify) {
                                         char *password2 = NULL;
 
-                                        if ((k = ask_password_auto("Please enter passphrase for disk (verification):", "drive-harddisk", until, &password2)) < 0) {
+                                        if (asprintf(&text, "Please enter passphrase for disk %s! (verification)", name) < 0) {
+                                                log_error("Out of memory");
+                                                goto finish;
+                                        }
+
+                                        k = ask_password_auto(text, "drive-harddisk", until, &password2);
+                                        free(text);
+
+                                        if (k < 0) {
                                                 log_error("Failed to query verification password: %s", strerror(-k));
                                                 goto finish;
                                         }
@@ -310,6 +386,7 @@ int main(int argc, char *argv[]) {
                 if (try >= opt_tries) {
                         log_error("Too many attempts.");
                         r = EXIT_FAILURE;
+                        goto finish;
                 }
 
         } else if (streq(argv[1], "detach")) {
@@ -346,5 +423,7 @@ finish:
 
         free(password);
 
+        free(description);
+
         return r;
 }