From f3f172ce8ca34c41b45ea4435ab9107772e47afd Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 24 Jan 2016 12:00:22 +0100 Subject: [PATCH] Aded function ZakAuthe::plg_authe_nogui. --- .gitignore | 1 + src/aute_db.c | 96 ++++++++++++++++++++++++++++++++++------------ tests/Makefile.am | 3 +- tests/test_nogui.c | 45 ++++++++++++++++++++++ 4 files changed, 120 insertions(+), 25 deletions(-) create mode 100644 tests/test_nogui.c diff --git a/.gitignore b/.gitignore index b8aeb3d..93e51ac 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ missing config.h stamp-h1 tests/test +tests/test_nogui tools/zakauthedb-mkpwd *.tar.gz *.exe diff --git a/src/aute_db.c b/src/aute_db.c index 45865c9..33ebf6c 100644 --- a/src/aute_db.c +++ b/src/aute_db.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2015 Andrea Zagli + * Copyright (C) 2005-2016 Andrea Zagli * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -175,23 +175,17 @@ gchar } static gchar -*controllo () +*controllo (const gchar *username, const gchar *password, const gchar *new_password) { gchar *sql; - gchar *utente = ""; - gchar *password; - gchar *password_nuova; GdaDataModel *dm; - utente = g_strstrip (g_strdup (gtk_entry_get_text (GTK_ENTRY (txt_utente)))); - password = g_strstrip (g_strdup (gtk_entry_get_text (GTK_ENTRY (txt_password)))); - sql = g_strdup_printf ("SELECT code" " FROM users" " WHERE code = '%s'" " AND password = '%s'" " AND status <> 'E'", - gdaex_strescape (utente, NULL), + gdaex_strescape (username, NULL), gdaex_strescape (zak_authe_db_encrypt_password (password), NULL)); dm = gdaex_query (gdaex, sql); g_free (sql); @@ -204,41 +198,39 @@ static gchar g_warning ("User name or password aren't valids."); return NULL; } + if (dm != NULL) + { + g_object_unref (dm); + } - utente = g_strstrip (g_strdup (gdaex_data_model_get_field_value_stringify_at (dm, 0, "code"))); - - if (strcmp (utente, "") != 0 - && gtk_expander_get_expanded (GTK_EXPANDER (exp_cambio))) + if (new_password != NULL) { - password_nuova = g_strstrip (g_strdup (gtk_entry_get_text (GTK_ENTRY (txt_password_nuova)))); - if (strlen (password_nuova) == 0 || strcmp (g_strstrip (password_nuova), "") == 0) + gchar *_new_password = g_strstrip (g_strdup (new_password)); + if (strlen (_new_password) == 0 || g_strcmp0 (_new_password, "") == 0) { /* TO DO */ g_warning ("The new password cannot be empty."); } - else if (strcmp (g_strstrip (password_nuova), g_strstrip (g_strdup (gtk_entry_get_text (GTK_ENTRY (txt_password_conferma))))) != 0) - { - /* TO DO */ - g_warning ("The new and confirm password don't match."); - } else { /* cambio la password */ sql = g_strdup_printf ("UPDATE users" " SET password = '%s'" " WHERE code = '%s'", - gdaex_strescape (zak_authe_db_encrypt_password (password_nuova), NULL), - gdaex_strescape (utente, NULL)); + gdaex_strescape (zak_authe_db_encrypt_password (_new_password), NULL), + gdaex_strescape (username, NULL)); if (gdaex_execute (gdaex, sql) == -1) { /* TO DO */ + g_free (sql); g_warning ("Error changing password."); return NULL; } + g_free (sql); } } - return utente; + return g_strdup (username); } static void @@ -402,6 +394,10 @@ gchar GError *error; gchar *ret = NULL; + gchar *utente; + gchar *password; + gchar *new_password; + error = NULL; get_gdaex (parameters); @@ -472,7 +468,21 @@ gchar { case GTK_RESPONSE_OK: /* controllo dell'utente e della password */ - ret = controllo (); + password = g_strstrip (g_strdup (gtk_entry_get_text (GTK_ENTRY (txt_password)))); + utente = g_strstrip (g_strdup (gtk_entry_get_text (GTK_ENTRY (txt_utente)))); + new_password = NULL; + if (gtk_expander_get_expanded (GTK_EXPANDER (exp_cambio))) + { + new_password = g_strstrip (g_strdup (gtk_entry_get_text (GTK_ENTRY (txt_password_nuova)))); + if (strcmp (g_strstrip (new_password), g_strstrip (g_strdup (gtk_entry_get_text (GTK_ENTRY (txt_password_conferma))))) != 0) + { + /* TO DO */ + g_warning ("The new and confirm password don't match."); + g_free (new_password); + new_password = NULL; + } + } + ret = controllo (utente, password, new_password); break; case GTK_RESPONSE_CANCEL: @@ -491,6 +501,44 @@ gchar return ret; } +/** + * zak_authe_plg_authe_nogui: + * @parameters: + * @username: + * @password: + * @new_password: + * + */ +gboolean +zak_authe_plg_authe_nogui (GSList *parameters, const gchar *username, const gchar *password, const gchar *new_password) +{ + gboolean ret; + + gchar *_username; + + get_gdaex (parameters); + if (gdaex == NULL) + { + return FALSE; + } + + /* inizializzo libgcrypt */ + gcry_check_version (GCRYPT_VERSION); + + _username = controllo (username, password, new_password); + if (_username != NULL + && g_strcmp0 (_username, "") != 0) + { + ret = TRUE; + } + else + { + ret = FALSE; + } + + return ret; +} + /** * get_management_gui: * @parameters: diff --git a/tests/Makefile.am b/tests/Makefile.am index 0c43185..9cda210 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -8,4 +8,5 @@ LIBS = $(GTK_LIBS) \ LDADD = ../src/libzakauthedb.la -noinst_PROGRAMS = test +noinst_PROGRAMS = test \ + test_nogui diff --git a/tests/test_nogui.c b/tests/test_nogui.c new file mode 100644 index 0000000..66a13f3 --- /dev/null +++ b/tests/test_nogui.c @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2016 Andrea Zagli + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include + +#include + +int +main (int argc, char **argv) +{ + ZakAuthe *aute; + GSList *params; + + gtk_init (&argc, &argv); + + aute = zak_authe_new (); + + params = NULL; + + /* the libaute module to use */ + params = g_slist_append (params, argv[1]); + /* the libgda connection string */ + params = g_slist_append (params, argv[2]); + + zak_authe_set_config (aute, params); + + g_message ("User %s\n", zak_authe_authe_nogui (aute, argv[3], argc >= 5 ? argv[4] : "", argc >= 6 ? argv[5] : NULL) ? argv[3] : NULL); + + return 0; +} -- 2.49.0