[libgda] Allow to specify a passphrase when creating a database with SQLCipher



commit f64b562472656135f322ab713b0c01023811dafd
Author: Vivien Malerba <malerba gnome-db org>
Date:   Tue Sep 14 18:38:00 2010 +0200

    Allow to specify a passphrase when creating a database with SQLCipher

 libgda/sqlite/Makefile.am                          |   64 +++--------------
 libgda/sqlite/gda-sqlite-provider.c                |   75 ++++++++++++++++++--
 .../sqlcipher/sqlcipher_specs_create_db.xml.in     |   10 +++-
 3 files changed, 88 insertions(+), 61 deletions(-)
---
diff --git a/libgda/sqlite/Makefile.am b/libgda/sqlite/Makefile.am
index 0c0f4b6..1d2df2c 100644
--- a/libgda/sqlite/Makefile.am
+++ b/libgda/sqlite/Makefile.am
@@ -36,10 +36,7 @@ xml_embedded.h: gen_emb_string$(EXEEXT_FOR_BUILD) $(top_srcdir)/providers/sqlite
 	./gen_emb_string$(EXEEXT_FOR_BUILD) $(top_srcdir)/providers/sqlite/*.xml.in > xml_embedded.h
 
 sqliteheaders = gda-sqlite-provider.h
-
-$(libgda_sqlite_la_OBJECTS): xml_embedded.h keywords_hash.c
-
-libgda_sqlite_la_SOURCES = \
+sqlitesources = \
 	$(sqliteheaders) \
 	gda-sqlite-blob-op.c \
 	gda-sqlite-blob-op.h \
@@ -63,66 +60,23 @@ libgda_sqlite_la_SOURCES = \
 	gda-symbols-util.h \
 	gda-symbols-util.c
 
-libgda_sqlite_la_CFLAGS = -DSEARCH_LIB_PATH=\""$(SQLITE_PATH)"\" -DCLASS_PREFIX=\""GdaSqlite"\"
+$(libgda_sqlite_la_OBJECTS): xml_embedded.h keywords_hash.c
+
+libgda_sqlite_la_SOURCES = $(sqlitesources)
+libgda_sqlite_la_CFLAGS = -DPNAME=\""SQLite"\" -DCLASS_PREFIX=\""GdaSqlite"\" -DSEARCH_LIB_PATH=\""$(SQLITE_PATH)"\"
 libgda_sqlite_la_LDFLAGS = -export-dynamic -module -avoid-version $(NO_UNDEFINED)
 libgda_sqlite_la_LIBADD = \
 	virtual/libgda-virtual-4.0.la \
 	$(sqlitelibs) \
 	$(LIBGDA_LIBS)
 
-libgda_bdbsqlite_la_SOURCES = \
-	$(sqliteheaders) \
-	gda-sqlite-blob-op.c \
-	gda-sqlite-blob-op.h \
-	gda-sqlite-ddl.c \
-	gda-sqlite-ddl.h \
-	gda-sqlite-handler-bin.c \
-	gda-sqlite-handler-bin.h \
-	gda-sqlite-handler-boolean.c \
-	gda-sqlite-handler-boolean.h \
-	gda-sqlite-meta.c \
-	gda-sqlite-meta.h \
-	gda-sqlite-provider.c \
-	gda-sqlite-pstmt.h \
-	gda-sqlite-pstmt.c \
-	gda-sqlite-recordset.c \
-	gda-sqlite-recordset.h \
-	gda-sqlite-util.c \
-	gda-sqlite-util.h \
-	gda-sqlite.h \
-	keywords_hash.h \
-	gda-symbols-util.h \
-	gda-symbols-util.c
-
-libgda_bdbsqlite_la_CFLAGS = $(BDBSQL_CFLAGS) -DWITH_BDBSQLITE -DCLASS_PREFIX=\""GdaDBDSql"\" -DSEARCH_LIB_PATH=\""$(BDBSQL_PATH)"\"
+libgda_bdbsqlite_la_SOURCES = $(sqlitesources)
+libgda_bdbsqlite_la_CFLAGS = $(BDBSQL_CFLAGS) -DPNAME=\""BDBSql"\" -DCLASS_PREFIX=\""GdaDBDSql"\" -DWITH_BDBSQLITE -DSEARCH_LIB_PATH=\""$(BDBSQL_PATH)"\"
 libgda_bdbsqlite_la_LDFLAGS = -export-dynamic -module -avoid-version $(NO_UNDEFINED)
 libgda_bdbsqlite_la_LIBADD = 
 
-libgda_sqlcipher_la_SOURCES = \
-	$(sqliteheaders) \
-	gda-sqlite-blob-op.c \
-	gda-sqlite-blob-op.h \
-	gda-sqlite-ddl.c \
-	gda-sqlite-ddl.h \
-	gda-sqlite-handler-bin.c \
-	gda-sqlite-handler-bin.h \
-	gda-sqlite-handler-boolean.c \
-	gda-sqlite-handler-boolean.h \
-	gda-sqlite-meta.c \
-	gda-sqlite-meta.h \
-	gda-sqlite-provider.c \
-	gda-sqlite-pstmt.h \
-	gda-sqlite-pstmt.c \
-	gda-sqlite-recordset.c \
-	gda-sqlite-recordset.h \
-	gda-sqlite-util.c \
-	gda-sqlite-util.h \
-	gda-sqlite.h \
-	keywords_hash.h \
-	gda-symbols-util.h \
-	gda-symbols-util.c
-
-libgda_sqlcipher_la_CFLAGS = -DSTATIC_SQLITE -DSQLITE_HAS_CODEC -DCLASS_PREFIX=\""GdaSQLCipher"\" -DSEARCH_LIB_PATH=\"""\"
+libgda_sqlcipher_la_SOURCES = $(sqlitesources)
+libgda_sqlcipher_la_CFLAGS = -DPNAME=\""SQLCipher"\" -DCLASS_PREFIX=\""GdaSQLCipher"\" -DSTATIC_SQLITE -DSQLITE_HAS_CODEC -DSEARCH_LIB_PATH=\"""\"
 libgda_sqlcipher_la_LDFLAGS = -export-dynamic -module -avoid-version $(NO_UNDEFINED)
 libgda_sqlcipher_la_LIBADD = 
 
diff --git a/libgda/sqlite/gda-sqlite-provider.c b/libgda/sqlite/gda-sqlite-provider.c
index a65c3b7..8259188 100644
--- a/libgda/sqlite/gda-sqlite-provider.c
+++ b/libgda/sqlite/gda-sqlite-provider.c
@@ -529,7 +529,7 @@ gda_sqlite_provider_get_type (void)
 static const gchar *
 gda_sqlite_provider_get_name (GdaServerProvider *provider)
 {
-	return "SQLite";
+	return PNAME;
 }
 
 /* 
@@ -905,8 +905,8 @@ gda_sqlite_provider_create_operation (GdaServerProvider *provider, GdaConnection
         gchar *str;
 	gchar *dir;
 
-        file = g_utf8_strdown (gda_server_operation_op_type_to_string (type), -1);
-        str = g_strdup_printf ("sqlite_specs_%s.xml", file);
+        file = g_strdup_printf (PNAME "_specs_%s.xml", gda_server_operation_op_type_to_string (type));
+        str = g_utf8_strdown (file, -1);
         g_free (file);
 
 	dir = gda_gbr_get_file_path (GDA_DATA_DIR, LIBGDA_ABI_NAME, NULL);
@@ -948,8 +948,9 @@ gda_sqlite_provider_render_operation (GdaServerProvider *provider, GdaConnection
 	gchar *dir;
 
 	/* test @op's validity */
-        file = g_utf8_strdown (gda_server_operation_op_type_to_string (gda_server_operation_get_op_type (op)), -1);
-        str = g_strdup_printf ("sqlite_specs_%s.xml", file);
+	file = g_strdup_printf (PNAME "_specs_%s.xml",
+				gda_server_operation_op_type_to_string (gda_server_operation_get_op_type (op)));
+        str = g_utf8_strdown (file, -1);
         g_free (file);
 
 	dir = gda_gbr_get_file_path (GDA_DATA_DIR, LIBGDA_ABI_NAME, NULL);
@@ -1058,6 +1059,70 @@ gda_sqlite_provider_perform_operation (GdaServerProvider *provider, GdaConnectio
 			g_set_error (error, 0, 0, "%s", SQLITE3_CALL (sqlite3_errmsg) (cdata->connection)); 
 			retval = FALSE;
 		}
+
+#ifdef SQLITE_HAS_CODEC
+		value = gda_server_operation_get_value_at (op, "/DB_DEF_P/PASSWORD");
+		if (value && G_VALUE_HOLDS (value, G_TYPE_STRING) &&
+		    g_value_get_string (value) &&
+		    *g_value_get_string (value) &&
+		    SQLITE3_CALL (sqlite3_key)) {
+			const gchar *passphrase = g_value_get_string (value);
+			errmsg = SQLITE3_CALL (sqlite3_key) (cdata->connection, (void*) passphrase,
+							     strlen (passphrase));
+			if (errmsg != SQLITE_OK) {
+				g_set_error (error, 0, 0, "%s", SQLITE3_CALL (sqlite3_errmsg) (cdata->connection)); 
+				retval = FALSE;
+			}
+			else {
+				/* create some contents */
+				int res;
+				sqlite3_stmt *pStmt;
+				res = SQLITE3_CALL (sqlite3_prepare) (cdata->connection,
+								      "CREATE TABLE data (id int)", -1,
+								      &pStmt, NULL);
+
+				if (res != SQLITE_OK) {
+					g_set_error (error, 0, 0, "%s",
+						     _("Error initializing database with passphrase"));
+					retval = FALSE;
+					goto outcontents;
+				}
+				res = SQLITE3_CALL (sqlite3_step) (pStmt);
+				SQLITE3_CALL (sqlite3_reset) (pStmt);
+				SQLITE3_CALL (sqlite3_finalize) (pStmt);
+				if (res != SQLITE_DONE) {
+					g_set_error (error, 0, 0, "%s",
+						     _("Error initializing database with passphrase"));
+					retval = FALSE;
+					goto outcontents;
+					/* end */
+				}
+
+				res = SQLITE3_CALL (sqlite3_prepare) (cdata->connection,
+								      "DROP TABLE data", -1,
+								      &pStmt, NULL);
+
+				if (res != SQLITE_OK) {
+					g_set_error (error, 0, 0, "%s",
+						     _("Error initializing database with passphrase"));
+					retval = FALSE;
+					goto outcontents;
+				}
+				res = SQLITE3_CALL (sqlite3_step) (pStmt);
+				SQLITE3_CALL (sqlite3_reset) (pStmt);
+				SQLITE3_CALL (sqlite3_finalize) (pStmt);
+				if (res != SQLITE_DONE) {
+					g_set_error (error, 0, 0, "%s",
+						     _("Error initializing database with passphrase"));
+					retval = FALSE;
+					goto outcontents;
+					/* end */
+				}
+			outcontents:
+				;
+			}
+		}
+#endif
 		gda_sqlite_free_cnc_data (cdata);
 
 		return retval;
diff --git a/providers/sqlcipher/sqlcipher_specs_create_db.xml.in b/providers/sqlcipher/sqlcipher_specs_create_db.xml.in
deleted file mode 120000
index c8f1a76..0000000
--- a/providers/sqlcipher/sqlcipher_specs_create_db.xml.in
+++ /dev/null
@@ -1 +0,0 @@
-../sqlite/sqlite_specs_create_db.xml.in
\ No newline at end of file
diff --git a/providers/sqlcipher/sqlcipher_specs_create_db.xml.in b/providers/sqlcipher/sqlcipher_specs_create_db.xml.in
new file mode 100644
index 0000000..dff5a78
--- /dev/null
+++ b/providers/sqlcipher/sqlcipher_specs_create_db.xml.in
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<serv_op>
+  <!-- DSN parameters -->
+  <parameters id="DB_DEF_P" _name="Database's description">
+    <parameter id="DB_NAME" _name="Database name" _descr="The name of a database to create" gdatype="gchararray" nullok="FALSE"/>
+    <parameter id="DB_DIR" _name="Directory" _descr="Directory where the database file will be" gdatype="gchararray" nullok="FALSE" plugin="filesel:MODE=PICKFOLDER"/>
+    <parameter id="PASSWORD" _name="Passphrase" _descr="Encryption passphrase" gdatype="gchararray" nullok="TRUE" plugin="string:HIDDEN=true"/>
+  </parameters>
+</serv_op>



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