Skip to content

Commit

Permalink
Add export command to CLI
Browse files Browse the repository at this point in the history
It's now possible to export the data directly from the CLI.
As it is for the GUI, data can be exported in andotp, freeotpplus and aegis format.

This fixes #187
  • Loading branch information
paolostivanin committed Dec 30, 2020
1 parent fc7b74c commit ec4b184
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 42 deletions.
22 changes: 15 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ set(GUI_HEADER_FILES
src/password-cb.h
src/qrcode-parser.h
src/treeview.h
src/exports.h
src/lock-app.h)
src/common/exports.h
src/lock-app.h src/common/get-providers-data.h)

set(GUI_SOURCE_FILES
src/common/common.c
src/add-common.c
src/andotp.c
src/common/andotp.c
src/app.c
src/authplus.c
src/gui-common.c
Expand All @@ -102,16 +102,19 @@ set(GUI_SOURCE_FILES
src/webcam-add-cb.c
src/exports.c
src/lock-app.c
src/freeotp.c
src/aegis.c)
src/common/freeotp.c
src/common/aegis.c)

set(CLI_HEADER_FILES
src/cli/help.h
src/cli/get-data.h
src/common/common.h
src/db-misc.h
src/gquarks.h
src/file-size.h)
src/file-size.h
src/common/exports.h
src/parse-uri.h
src/common/get-providers-data.h)

set(CLI_SOURCE_FILES
src/cli/main.c
Expand All @@ -120,7 +123,12 @@ set(CLI_SOURCE_FILES
src/common/common.c
src/db-misc.c
src/gquarks.c
src/file-size.c)
src/file-size.c
src/parse-uri.c
src/common/andotp.c
src/common/aegis.c
src/common/freeotp.c)

if(BUILD_GUI AND BUILD_CLI)
list(APPEND CLI_SOURCE_FILES
src/treeview.c
Expand Down
2 changes: 1 addition & 1 deletion src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "gui-common.h"
#include "gquarks.h"
#include "imports.h"
#include "exports.h"
#include "common/exports.h"
#include "message-dialogs.h"
#include "password-cb.h"
#include "get-builder.h"
Expand Down
31 changes: 24 additions & 7 deletions src/cli/help.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <glib.h>
#include "version.h"

static void print_main_help (const gchar *prg_name);
static void print_main_help (const gchar *prg_name);

static void print_show_help (const gchar *prg_name);
static void print_show_help (const gchar *prg_name);

static void print_export_help (const gchar *prg_name);


gboolean show_help (const gchar *prg_name,
Expand All @@ -16,10 +18,12 @@ gboolean show_help (const gchar *prg_name,
} else if (g_strcmp0 (help_command, "-v") == 0 || g_strcmp0 (help_command, "--version") == 0) {
g_print ("%s v%s\n", PROJECT_NAME, PROJECT_VER);
help_displayed = TRUE;
}
else if (g_strcmp0 (help_command, "--help-show") == 0 || g_strcmp0 (help_command, "help-show") == 0) {
} else if (g_strcmp0 (help_command, "--help-show") == 0 || g_strcmp0 (help_command, "help-show") == 0) {
print_show_help (prg_name);
help_displayed = TRUE;
} else if (g_strcmp0 (help_command, "--help-export") == 0 || g_strcmp0 (help_command, "help-export") == 0) {
print_export_help (prg_name);
help_displayed = TRUE;
} else if (help_command == NULL || g_utf8_strlen (help_command, -1) < 2) {
print_main_help (prg_name);
help_displayed = TRUE;
Expand All @@ -37,11 +41,13 @@ print_main_help (const gchar *prg_name)
g_print ("Help Options:\n");
g_print (" -h, --help\t\tShow this help\n");
g_print (" --help-show\t\tShow options\n");
g_print (" --help-export\t\tExport options\n");
g_print ("\n");
g_print ("Main Options:\n");
g_print (" -v, --version\t\tShow program version\n");
g_print (" show\t\tShow a token\n");
g_print (" list\t\tList all pairs of account and issuer\n");
g_print (" -v, --version\t\t\t\tShow program version\n");
g_print (" show <-a ..> [-i ..] [-m] [-n]\tShow a token\n");
g_print (" list\t\t\t\t\tList all pairs of account and issuer\n");
g_print (" export <-t ..>\t\t\tExport data\n");
g_print ("\n");
}

Expand All @@ -58,3 +64,14 @@ print_show_help (const gchar *prg_name)
g_print (" -n, --next\tShow also the next token, not only the current one (optional, valid only for TOTP)\n");
g_print ("\n");
}


static void
print_export_help (const gchar *prg_name)
{
g_print ("Usage:\n %s export <-t | --type> <andotp | freeotpplus | aegis>\n", prg_name);
g_print ("\n");
g_print ("Export Options:\n");
g_print (" -t, --type\t\tExport format. Must be either one of: andotp, freeotpplus, aegis\n");
g_print ("\n");
}
40 changes: 40 additions & 0 deletions src/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "../common/common.h"
#include "../db-misc.h"
#include "../otpclient.h"
#include "../common/exports.h"
#include "../common/get-providers-data.h"
#include "../parse-uri.h"

#define MAX_ABS_PATH_LEN 256

Expand Down Expand Up @@ -96,6 +99,43 @@ main (gint argc,
show_token (db_data, account, issuer, match_exactly, show_next_token);
} else if (g_strcmp0 (argv[1], "list") == 0) {
list_all_acc_iss (db_data);
} else if (g_strcmp0 (argv[1], "export") == 0) {
if (g_strcmp0 (argv[3], "andotp") != 0 || g_strcmp0 (argv[3], "freeotpplus") != 0 || g_strcmp0 (argv[3], "aegis") != 0) {
g_printerr ("Wrong argument(s). Please type '%s --help-export' to see the available options.\n", argv[0]);
g_free (db_data);
return -1;
}
const gchar *base_dir = NULL;
#ifndef USE_FLATPAK_APP_FOLDER
base_dir = g_get_home_dir ();
#else
base_dir = g_get_user_data_dir ();
#endif
gchar *andotp_export_pwd = NULL, *exported_file_path = NULL, *ret_msg = NULL;
if (g_strcmp0 (argv[3], "andotp") == 0) {
andotp_export_pwd = get_pwd ();
if (andotp_export_pwd == NULL) {
goto end;
}
exported_file_path = g_build_filename (base_dir, "andotp_exports.json.aes", NULL);
ret_msg = export_andotp (exported_file_path, andotp_export_pwd, db_data->json_data);
gcry_free (andotp_export_pwd);
}
if (g_strcmp0 (argv[3], "freeotpplus") == 0) {
exported_file_path = g_build_filename (base_dir, "freeotpplus-exports.txt", NULL);
ret_msg = export_freeotpplus (exported_file_path, db_data->json_data);
}
if (g_strcmp0 (argv[3], "aegis") == 0) {
exported_file_path = g_build_filename (base_dir, "aegis_export_plain.json", NULL);
ret_msg = export_aegis (exported_file_path, db_data->json_data);
}
if (ret_msg != NULL) {
g_printerr ("An error occurred while exporting the data: %s\n", ret_msg);
g_free (ret_msg);
} else {
g_print ("Data successfully exported to: %s\n", exported_file_path);
}
g_free (exported_file_path);
} else {
show_help (argv[0], "help");
return -1;
Expand Down
6 changes: 3 additions & 3 deletions src/aegis.c → src/common/aegis.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include <gcrypt.h>
#include <jansson.h>
#include <time.h>
#include "imports.h"
#include "gui-common.h"
#include "gquarks.h"
#include "../imports.h"
#include "../gui-common.h"
#include "../gquarks.h"


static GSList *parse_json_data (const gchar *data,
Expand Down
8 changes: 4 additions & 4 deletions src/andotp.c → src/common/andotp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <gcrypt.h>
#include <jansson.h>
#include <time.h>
#include "file-size.h"
#include "imports.h"
#include "gui-common.h"
#include "gquarks.h"
#include "../file-size.h"
#include "../imports.h"
#include "../gui-common.h"
#include "../gquarks.h"

#define ANDOTP_IV_SIZE 12
#define ANDOTP_SALT_SIZE 12
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/freeotp.c → src/common/freeotp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <gcrypt.h>
#include <jansson.h>
#include <time.h>
#include "file-size.h"
#include "parse-uri.h"
#include "../file-size.h"
#include "../parse-uri.h"


GSList *
Expand Down
24 changes: 24 additions & 0 deletions src/common/get-providers-data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <gtk/gtk.h>

G_BEGIN_DECLS

GSList *get_authplus_data (const gchar *zip_path,
const gchar *password,
gint32 max_file_size,
GError **err);

GSList *get_andotp_data (const gchar *path,
const gchar *password,
gint32 max_file_size,
gboolean encrypted,
GError **err);

GSList *get_freeotpplus_data (const gchar *path,
GError **err);

GSList *get_aegis_data (const gchar *path,
GError **err);

G_END_DECLS
2 changes: 1 addition & 1 deletion src/exports.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <jansson.h>
#include "password-cb.h"
#include "message-dialogs.h"
#include "exports.h"
#include "common/exports.h"


static void show_ret_msg_dialog (GtkWidget *mainwin, gchar *fpath, gchar *ret_msg);
Expand Down
1 change: 1 addition & 0 deletions src/imports.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "common/common.h"
#include "gui-common.h"
#include "db-misc.h"
#include "common/get-providers-data.h"


static gboolean parse_data_and_update_db (AppData *app_data,
Expand Down
17 changes: 0 additions & 17 deletions src/imports.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,6 @@ void select_file_cb (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data);

GSList *get_authplus_data (const gchar *zip_path,
const gchar *password,
gint32 max_file_size,
GError **err);

GSList *get_andotp_data (const gchar *path,
const gchar *password,
gint32 max_file_size,
gboolean encrypted,
GError **err);

GSList *get_freeotpplus_data (const gchar *path,
GError **err);

GSList *get_aegis_data (const gchar *path,
GError **err);

gchar *update_db_from_otps (GSList *otps,
AppData *app_data);

Expand Down

0 comments on commit ec4b184

Please # to comment.