diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | flake.nix | 2 | ||||
-rw-r--r-- | include/npassd/db.h | 3 | ||||
-rw-r--r-- | include/npassd/service.h | 2 | ||||
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/npassd/db.c | 49 | ||||
-rw-r--r-- | src/npassd/meson.build | 3 | ||||
-rw-r--r-- | src/npassd/npassd.c | 11 |
8 files changed, 71 insertions, 1 deletions
@@ -47,6 +47,7 @@ Dependencies - meson (*) - pkg-config (*+) - gpgme +- sqlite (+) - basu, elogind or systemd (+) _\* build-time dependency, + npassd dependency_ @@ -20,6 +20,7 @@ buildInputs = with pkgs; [ gpgme + sqlite systemdLibs pkg-config @@ -53,6 +54,7 @@ ]; buildInputs = with pkgs; [ gpgme + sqlite systemdLibs ]; diff --git a/include/npassd/db.h b/include/npassd/db.h new file mode 100644 index 0000000..d5166a4 --- /dev/null +++ b/include/npassd/db.h @@ -0,0 +1,3 @@ +#include <sqlite3.h> + +int db_init(const char *store_path, struct sqlite3 **db); diff --git a/include/npassd/service.h b/include/npassd/service.h index e1a42fc..6219dd6 100644 --- a/include/npassd/service.h +++ b/include/npassd/service.h @@ -1,3 +1,4 @@ +#include <sqlite3.h> #include <systemd/sd-bus.h> #include "npassd/session.h" @@ -7,6 +8,7 @@ struct service { sd_bus *bus; sd_bus_slot *slot; + struct sqlite3 *db; struct session session[MAX_SESSION]; }; diff --git a/meson.build b/meson.build index b77292e..f8cfebe 100644 --- a/meson.build +++ b/meson.build @@ -18,6 +18,7 @@ add_project_arguments( language: 'c', ) +sqlite_dep = dependency('sqlite3') gpgme_dep = dependency('gpgme', version: '>= 1.0') sdbus_dep = dependency('libsystemd', 'libelogind', 'basu') diff --git a/src/npassd/db.c b/src/npassd/db.c new file mode 100644 index 0000000..5afa45e --- /dev/null +++ b/src/npassd/db.c @@ -0,0 +1,49 @@ +#include <errno.h> +#include <linux/limits.h> +#include <stdio.h> + +#include "npassd/db.h" +#include "util.h" + +#define DB_NAME "secret_service.sqlite" + +const char sql_stmt_init[] = "CREATE TABLE IF NOT EXISTS store (" + " storeid INTEGER PRIMARY KEY," + " name TEXT NOT NULL UNIQUE" + ");" + "CREATE TABLE IF NOT EXISTS lookup (" + " key TEXT NOT NULL," + " value TEXT NOT NULL," + " storeid INTEGER," + " UNIQUE(storeid, key)," + " FOREIGN KEY(storeid) REFERENCES store(storeid)" + ");"; + +int db_init(const char *store_path, struct sqlite3 **db) +{ + char db_path[PATH_MAX]; + int ret; + + ret = snprintf(db_path, sizeof(db_path), "%s/" DB_NAME, store_path); + if (ret < 0 || (size_t)ret > sizeof(db_path)) { + print_err("Path exceeded PATH_MAX"); + return -ENAMETOOLONG; + } + + ret = sqlite3_open_v2(db_path, db, + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | + SQLITE_OPEN_FULLMUTEX, + NULL); + if (ret != SQLITE_OK) { + print_err("Can't open database: %s", sqlite3_errmsg(*db)); + return -EPERM; + } + + ret = sqlite3_exec(*db, sql_stmt_init, NULL, NULL, NULL); + if (ret != SQLITE_OK) { + print_err("Failed to create tables: %s", sqlite3_errmsg(*db)); + return -EPERM; + } + + return 0; +} diff --git a/src/npassd/meson.build b/src/npassd/meson.build index 10302cd..e73c28f 100644 --- a/src/npassd/meson.build +++ b/src/npassd/meson.build @@ -4,10 +4,11 @@ e = executable( 'npassd.c', 'service.c', 'session.c', + 'db.c', ], include_directories: npass_inc, - dependencies: sdbus_dep, + dependencies: [ sdbus_dep, sqlite_dep ], link_with: lib_npass, install: true, ) diff --git a/src/npassd/npassd.c b/src/npassd/npassd.c index a2f5173..22164dd 100644 --- a/src/npassd/npassd.c +++ b/src/npassd/npassd.c @@ -4,6 +4,8 @@ #include <string.h> #include <systemd/sd-bus.h> +#include "libnpass/libnpass.h" +#include "npassd/db.h" #include "npassd/service.h" #include "util.h" @@ -13,8 +15,17 @@ int main(void) { int ret; sd_bus *bus; + char *store_path; struct service service; + ret = pass_store_path_set(&store_path); + if (ret < 0) + return EXIT_FAILURE; + + ret = db_init(store_path, &service.db); + if (ret < 0) + return EXIT_FAILURE; + ret = sd_bus_open_user(&bus); if (ret < 0) { print_err("Failed to connect to bus: %s", strerror(-ret)); |