Skip to content

Commit

Permalink
Merge pull request #227 from paolostivanin/small-fixes
Browse files Browse the repository at this point in the history
Small fixes
  • Loading branch information
paolostivanin authored Nov 15, 2021
2 parents 13fc9fc + 9e4aaab commit 59ce1b4
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 28 deletions.
31 changes: 24 additions & 7 deletions src/cli/get-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,39 @@ show_token (DatabaseData *db_data,
json_t *obj;
gboolean found = FALSE;
json_array_foreach (db_data->json_data, index, obj) {
if (compare_strings (json_string_value (json_object_get (obj, "label")), account, match_exactly) == 0) {
if (issuer != NULL) {
if (compare_strings (json_string_value (json_object_get (obj, "issuer")), issuer, match_exactly) == 0) {
const gchar *account_from_db = json_string_value (json_object_get (obj, "label"));
const gchar *issuer_from_db = NULL;
if (issuer != NULL) {
issuer_from_db = json_string_value (json_object_get (obj, "issuer"));
}
if (account_from_db != NULL && issuer_from_db != NULL && account != NULL) {
// both account and issuer are present
if (compare_strings (account_from_db, account, match_exactly) == 0 && compare_strings (issuer_from_db, issuer, match_exactly) == 0) {
get_token (obj, db_data, show_next_token);
found = TRUE;
}
} else {
if (account_from_db != NULL && account != NULL) {
// account is present, but issuer is not
if (compare_strings (account_from_db, account, match_exactly) == 0) {
get_token (obj, db_data, show_next_token);
found = TRUE;
}
} else {
get_token (obj, db_data, show_next_token);
found = TRUE;
// account was null, but issue may be present
if (issuer_from_db != NULL) {
if (compare_strings (issuer_from_db, issuer, match_exactly) == 0) {
get_token (obj, db_data, show_next_token);
found = TRUE;
}
}
}
}
}
if (!found) {
g_printerr ("Couldn't find the data. Either the given data is wrong or is not in the database.\n");
g_printerr ("Given account: %s\n", account);
if (issuer != NULL) g_printerr ("Given issuer: %s\n", issuer);
g_printerr ("Given account: %s\n", account != NULL ? account : "<none>");
g_printerr ("Given issuer: %s\n", issuer != NULL ? issuer : "<none>");
return;
}
}
Expand Down
24 changes: 19 additions & 5 deletions src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,31 @@ json_object_get_hash (json_t *obj)
return hash;
}

void
g_trim_whitespace (gchar *str)
gchar *
secure_strdup (const gchar *src)
{
gchar *sec_buf = gcry_calloc_secure (strlen (src) + 1, 1);
memcpy (sec_buf, src, strlen (src) + 1);

return sec_buf;
}


gchar *
g_trim_whitespace (const gchar *str)
{
if (g_utf8_strlen (str, -1) == 0) {
return;
return NULL;
}
gchar *sec_buf = gcry_calloc_secure (strlen (str) + 1, 1);
int pos = 0;
for (int i = 0; str[i]; i++) {
if (str[i] != ' ') {
str[pos++] = str[i];
sec_buf[pos++] = str[i];
}
}
str[pos] = '\0';
sec_buf[pos] = '\0';
gcry_realloc (sec_buf, g_utf8_strlen(sec_buf, -1) + 1);

return sec_buf;
}
4 changes: 3 additions & 1 deletion src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ guint32 jenkins_one_at_a_time_hash (const gchar *key,

guint32 json_object_get_hash (json_t *obj);

void g_trim_whitespace (gchar *str);
gchar *secure_strdup (const gchar *src);

gchar *g_trim_whitespace (const gchar *str);

G_END_DECLS
10 changes: 0 additions & 10 deletions src/gui-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ get_row_number_from_iter (GtkListStore *list_store,
}


gchar *
secure_strdup (const gchar *src)
{
gchar *sec_buf = gcry_calloc_secure (strlen (src) + 1, 1);
memcpy (sec_buf, src, strlen (src) + 1);

return sec_buf;
}


json_t *
build_json_obj (const gchar *type,
const gchar *acc_label,
Expand Down
2 changes: 0 additions & 2 deletions src/gui-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ void icon_press_cb (GtkEntry *entry,
guint get_row_number_from_iter (GtkListStore *list_store,
GtkTreeIter iter);

gchar *secure_strdup (const gchar *src);

json_t *build_json_obj (const gchar *type,
const gchar *acc_label,
const gchar *acc_iss,
Expand Down
9 changes: 6 additions & 3 deletions src/parse-data.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <gtk/gtk.h>
#include <string.h>
#include <jansson.h>
#include <gcrypt.h>
#include "otpclient.h"
#include "db-misc.h"
#include "manual-add-cb.h"
Expand Down Expand Up @@ -47,9 +48,9 @@ parse_user_data (Widgets *widgets,
const gchar *counter = gtk_entry_get_text (GTK_ENTRY (widgets->counter_entry));
gboolean period_active = gtk_widget_get_sensitive (widgets->period_entry);
gboolean counter_active = gtk_widget_get_sensitive (widgets->counter_entry);
g_trim_whitespace (acc_key);
if (is_input_valid (widgets->dialog, acc_label, acc_iss, acc_key, digits, period, period_active, counter, counter_active)) {
obj = get_json_obj (widgets, acc_label, acc_iss, acc_key, digits, period, counter);
gchar *acc_key_trimmed = g_trim_whitespace (acc_key);
if (is_input_valid (widgets->dialog, acc_label, acc_iss, acc_key_trimmed, digits, period, period_active, counter, counter_active)) {
obj = get_json_obj (widgets, acc_label, acc_iss, acc_key_trimmed, digits, period, counter);
guint32 hash = json_object_get_hash (obj);
if (g_slist_find_custom (db_data->objects_hash, GUINT_TO_POINTER (hash), check_duplicate) == NULL) {
db_data->objects_hash = g_slist_append (db_data->objects_hash, g_memdupX (&hash, sizeof (guint)));
Expand All @@ -58,8 +59,10 @@ parse_user_data (Widgets *widgets,
g_print ("[INFO] Duplicate element not added\n");
}
} else {
gcry_free (acc_key_trimmed);
return FALSE;
}
gcry_free (acc_key_trimmed);
return TRUE;
}

Expand Down
1 change: 1 addition & 0 deletions src/password-cb.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "message-dialogs.h"
#include "get-builder.h"
#include "otpclient.h"
#include "common/common.h"

typedef struct _entrywidgets {
GtkWidget *entry_old;
Expand Down
1 change: 1 addition & 0 deletions src/qrcode-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <png.h>
#include <glib/gstdio.h>
#include "gui-common.h"
#include "common/common.h"

typedef struct _image_data_t {
gulong width;
Expand Down
1 change: 1 addition & 0 deletions src/webcam-add-cb.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "message-dialogs.h"
#include "add-common.h"
#include "get-builder.h"
#include "common/common.h"


typedef struct _config_data {
Expand Down

0 comments on commit 59ce1b4

Please sign in to comment.