Commits

Anonymous committed e4c7ea0

Simple support for encrypted images.

Comments (0)

Files changed (5)

 MIRAGE_LIBS = $$(pkg-config --libs-only-l libmirage)
 
 PROG = mirage2iso
-OBJS = mirage-getopt.o mirage-wrapper.o
+OBJS = mirage-getopt.o mirage-wrapper.o mirage-password.o
 
 CONFIGOUT = mirage-config.h
 CONFIGTESTS = check-getopt.o check-sysexits.o check-mmapio.o
 #define _MIRAGE_COMPAT_H 1
 
 #if MIRAGE_MAJOR_VERSION >= 1 && MIRAGE_MINOR_VERSION >= 2
+#	define MIRAGE_HAS_PASSWORD_SUPPORT
 #elif MIRAGE_MAJOR_VERSION <= 1 && MIRAGE_MINOR_VERSION < 2
 #	define MIRAGE_HAS_MIRAGE_OBJ
 #endif

mirage-password.c

+/* mirage2iso; password input support
+ * (c) 2009 Michał Górny
+ * released under 3-clause BSD license
+ */
+
+#define _ISOC99_SOURCE 1
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+static const int password_bufsize = 256;
+
+static char *buf = NULL;
+
+const char* const mirage_input_password(void) {
+	if (!buf) {
+		buf = malloc(password_bufsize);
+		if (!buf) {
+			fprintf(stderr, "malloc() for password buffer failed\n");
+			return NULL;
+		}
+
+		fprintf(stderr, "Please input password to the encrypted image: ");
+
+		if (!fgets(buf, password_bufsize, stdin)) {
+			fprintf(stderr, "Password input failed\n");
+			free(buf);
+			return NULL;
+		}
+
+		/* remove trailing newline */
+		const int len = strlen(buf);
+		char *last = &buf[len - 1];
+		char *plast = &buf[len - 2];
+
+		/* support single LF, single CR, CR/LF, LF/CR */
+		if (len >= 1 && (*last == '\n' || *last == '\r')) {
+			if (len >= 2 && *plast != *last && (*plast == '\r' || *plast == '\n'))
+				*plast = 0;
+			*last = 0;
+		}
+
+		/* buf got wiped? */
+		if (!*buf) {
+			fprintf(stderr, "No password supplied\n");
+			free(buf);
+			return NULL;
+		}
+	}
+
+	return buf;
+}
+
+void mirage_forget_password(void) {
+	if (buf) {
+		/* wipe out the buf */
+		memset(buf, 0, password_bufsize);
+		free(buf);
+	}
+}

mirage-password.h

+/* mirage2iso; password input support
+ * (c) 2009 Michał Górny
+ * released under 3-clause BSD license
+ */
+
+#ifndef _MIRAGE_PASSWORD_H
+#define _MIRAGE_PASSWORD_H 1
+
+const char* const mirage_input_password(void);
+void mirage_forget_password(void);
+
+#endif
 
 #include <mirage.h>
 #include "mirage-compat.h"
+#include "mirage-password.h"
 
 extern bool quiet;
 extern bool verbose;
 	return false;
 }
 
-#ifdef MIRAGE_HAS_MIRAGE_OBJ
+#ifdef MIRAGE_HAS_PASSWORD_SUPPORT
+
+gchar* miragewrap_password_callback(gpointer user_data) {
+	const char* const pass = mirage_input_password();
+
+	if (!pass)
+		return NULL;
+
+	return g_strdup(pass);
+}
+
+#endif
 
 const bool miragewrap_init(void) {
 	g_type_init();
 
-	mirage = g_object_new(MIRAGE_TYPE_MIRAGE, NULL);
+#ifdef MIRAGE_HAS_MIRAGE_OBJ
+	if (!((mirage = g_object_new(MIRAGE_TYPE_MIRAGE, NULL))))
+		return false;
+#else
+	if (!libmirage_init(&err))
+		return miragewrap_err("Unable to init libmirage");
+#endif
+
+#ifdef MIRAGE_HAS_PASSWORD_SUPPORT
+	if (!libmirage_set_password_function(miragewrap_password_callback, NULL, &err))
+		miragewrap_err("Unable to set password callback");
+#endif
 
-	return !!mirage;
+	return true;
 }
 
+#ifdef MIRAGE_HAS_MIRAGE_OBJ
+
 const char* const miragewrap_get_version(void) {
 	static char buf[10];
 	gchar *tmp;
 
 #else
 
-const bool miragewrap_init(void) {
-	g_type_init();
-
-	if (!libmirage_init(&err))
-		return miragewrap_err("Unable to init libmirage");
-
-	return true;
-}
-
 const char* const miragewrap_get_version(void) {
 	return mirage_version_long;
 }
 }
 
 void miragewrap_free(void) {
+#ifdef MIRAGE_HAS_PASSWORD_SUPPORT
+	mirage_forget_password();
+#endif
+
 	if (session) g_object_unref(session);
 	if (disc) g_object_unref(disc);
 #ifdef MIRAGE_HAS_MIRAGE_OBJ