]> saetta.ns0.it Git - zakauthe/plugins/smbldap/commitdiff
Added function exec_ssh_command.
authorAndrea Zagli <azagli@libero.it>
Sat, 4 Sep 2010 15:56:27 +0000 (17:56 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 4 Sep 2010 15:56:27 +0000 (17:56 +0200)
Enable deleting groups.

src/aute_smbldap.c

index 9dd1dee7c679711eadbd4f529b20163f74ca2dc0..412e6fe6a7ef7a80c19b350bf725b1a9e0cfc868 100644 (file)
@@ -31,6 +31,7 @@
 #endif
 
 #include "user.h"
+#include "group.h"
 
 static GtkBuilder *gtkbuilder;
 
@@ -42,6 +43,11 @@ static gchar *base_dn;
 static gchar *users_ou;
 static gchar *groups_ou;
 
+static gchar *host;
+static gchar *host_ssh;
+static gchar *utente;
+static gchar *password;
+
 static GtkWidget *txt_utente;
 static GtkWidget *txt_password;
 static GtkWidget *exp_cambio;
@@ -49,6 +55,7 @@ static GtkWidget *txt_password_nuova;
 static GtkWidget *txt_password_conferma;
 
 static GtkWidget *w_users;
+static GtkWidget *notebook;
 
 static GtkTreeView *trv_users;
 static GtkListStore *lstore_users;
@@ -131,17 +138,115 @@ static GtkWindow
        return w;
 }
 
+static void
+exec_ssh_command (const gchar *host, const gchar *utente,
+                  const gchar *password, const gchar *command)
+{
+       ssh_session session;
+       ssh_channel channel;
+       int rc;
+       char buf[4096];
+
+       session = ssh_new ();
+       if (session == NULL)
+               {
+                       g_warning ("Errore durante il cambio della password: impossibile aprire la sessione SSH.");
+                       return;
+               }
+
+       if (ssh_options_set (session, SSH_OPTIONS_USER, utente) < 0)
+               {
+                       ssh_disconnect (session);
+                       g_warning ("Errore durante il cambio della password: impossibile impostare l'utente.");
+                       return;
+               }
+
+       /*g_message("HOST SSH: %s",host_ssh);*/
+       if (ssh_options_set (session, SSH_OPTIONS_HOST, host) < 0)
+               {
+                       ssh_disconnect (session);
+                       g_warning ("Errore durante il cambio della password: impossibile impostare l'host (%s).", host);
+                       return;
+               }
+       if (ssh_connect (session))
+               {
+                       g_warning ("Errore durante il cambio della password: connessione SSH fallita all'host «%s» (%s).", host, ssh_get_error (session));
+                       ssh_disconnect (session);
+                       return;
+               }
+
+       rc = ssh_userauth_password (session, NULL, password);
+       if (rc == SSH_AUTH_ERROR)
+               {
+                       g_warning ("Errore durante il cambio della password: autenticazione SSH fallita.");
+                       return;
+               }
+
+       if (session == NULL)
+               {
+                       g_warning ("Errore durante il cambio della password: sessione SSH nulla.");
+                       return;
+               }
+
+       channel = channel_new (session);
+       if (channel == NULL)
+               {
+                       g_warning ("Errore durante il cambio della password: impossibile aprire il canale SSH.");
+                       ssh_disconnect (session);
+                       ssh_finalize ();
+                       return;
+               }
+
+       rc = channel_open_session (channel);
+       if (rc < 0)
+               {
+                       g_warning ("Errore durante il cambio della password: impossibile aprire la sessione sul canale SSH.");
+                       channel_close (channel);
+                       ssh_disconnect (session);
+                       ssh_finalize ();
+                       return;
+               }
+
+       /*g_message ("COMMAND: %s", command);*/
+       rc = channel_request_exec (channel, command);
+       if (rc < 0)
+               {
+                       channel_close (channel);
+                       ssh_disconnect (session);
+                       ssh_finalize ();
+                       return;
+               }
+
+       do
+               {
+                       if (channel_is_open (channel)
+                           && !channel_is_eof (channel))
+                               {
+                                       rc = channel_read (channel, buf, sizeof (buf), 0);
+                                       if (rc > 0)
+                                               {
+                                                       /*fwrite (buf, 1, rc, stdout);*/
+                                               }
+                               }
+               } while (rc > 0);
+
+       channel_send_eof (channel);
+       channel_close (channel);
+
+       ssh_disconnect (session);
+       ssh_finalize ();
+}
+
 static gchar
 *controllo (GSList *parameters)
 {
        gchar *user_dn;
-       gchar *utente;
-       gchar *password;
        gchar *password_nuova;
 
-       gchar *host;
        gchar *user_field;
 
+       gchar **parts;
+
        int version;
        int retldap;
 
@@ -248,6 +353,18 @@ static gchar
                {
                        return NULL;
                }
+
+       parts = g_strsplit_set (host, "/:", -1);
+       if (g_strv_length (parts) < 3)
+               {
+                       g_warning ("Impossibile ottenere l'host per l'SSH.");
+                       host_ssh = NULL;
+               }
+       else
+               {
+                       host_ssh = parts[3];
+               }
+
        ldap = NULL;
        version = 3;
 
@@ -297,109 +414,9 @@ static gchar
                        else
                                {
                                        /* cambio la password */
-                                       ssh_session session;
-                                       ssh_channel channel;
-                                       int rc;
-                                       char buf[4096];
-
-                                       session = ssh_new ();
-                                       if (session == NULL)
-                                               {
-                                                       g_warning ("Errore durante il cambio della password: impossibile aprire la sessione SSH.");
-                                                       return user_dn;
-                                               }
-
-                                       if (ssh_options_set (session, SSH_OPTIONS_USER, utente) < 0)
-                                               {
-                                                       ssh_disconnect (session);
-                                                       g_warning ("Errore durante il cambio della password: impossibile impostare l'utente.");
-                                                       return user_dn;
-                                               }
-
-                                       gchar *host_ssh;
-                                       gchar **parts;
-                                       parts = g_strsplit_set (host, "/:", -1);
-                                       if (g_strv_length (parts) < 1)
-                                               {
-                                                       ssh_disconnect (session);
-                                                       g_warning ("Errore durante il cambio della password: impossibile ottenere l'host.");
-                                                       return user_dn;
-                                               }
-                                       host_ssh = parts[3];
-                                       /*g_message("HOST SSH: %s",host_ssh);*/
-                                       if (ssh_options_set (session, SSH_OPTIONS_HOST, host_ssh) < 0)
-                                               {
-                                                       ssh_disconnect (session);
-                                                       g_warning ("Errore durante il cambio della password: impossibile impostare l'host (%s).", host_ssh);
-                                                       return user_dn;
-                                               }
-                                       if (ssh_connect (session))
-                                               {
-                                                       g_warning ("Errore durante il cambio della password: connessione SSH fallita all'host «%s» (%s).", host_ssh, ssh_get_error (session));
-                                                       ssh_disconnect (session);
-                                                       return user_dn;
-                                               }
-
-                                       rc = ssh_userauth_password (session, NULL, password);
-                                       if (rc == SSH_AUTH_ERROR)
-                                               {
-                                                       g_warning ("Errore durante il cambio della password: autenticazione SSH fallita.");
-                                                       return user_dn;
-                                               }
-
-                                       if (session == NULL)
-                                               {
-                                                       g_warning ("Errore durante il cambio della password: sessione SSH nulla.");
-                                                       return user_dn;
-                                               }
-
-                                       channel = channel_new (session);
-                                       if (channel == NULL)
-                                               {
-                                                       g_warning ("Errore durante il cambio della password: impossibile aprire il canale SSH.");
-                                                       ssh_disconnect (session);
-                                                       ssh_finalize ();
-                                                       return user_dn;
-                                               }
-
-                                       rc = channel_open_session (channel);
-                                       if (rc < 0)
-                                               {
-                                                       g_warning ("Errore durante il cambio della password: impossibile aprire la sessione sul canale SSH.");
-                                                       channel_close (channel);
-                                                       ssh_disconnect (session);
-                                                       ssh_finalize ();
-                                                       return user_dn;
-                                               }
-
                                        gchar *command = g_strdup_printf ("echo -e \"%s\\n\" | sudo -k -S echo -e \"%s\\\\n%s\\\\n\" | sudo /usr/sbin/smbldap-passwd \"%s\"", password, password_nuova, password_nuova, utente);
-                                       /*g_message ("COMMAND: %s", command);*/
-                                       rc = channel_request_exec (channel, command);
-                                       if (rc < 0)
-                                               {
-                                                       channel_close (channel);
-                                                       ssh_disconnect (session);
-                                                       ssh_finalize ();
-                                                       return user_dn;
-                                               }
-
-                                       do
-                                               {
-                                                       if (channel_is_open (channel))
-                                                               {
-                                                                       rc = channel_read (channel, buf, sizeof (buf), 0);
-                                                                       if (rc > 0)
-                                                                               {
-                                                                                       /*fwrite (buf, 1, rc, stdout);*/
-                                                                               }
-                                                               }
-                                               } while (rc > 0);
 
-                                       channel_send_eof (channel);
-                                       channel_close (channel);
-
-                                       ssh_disconnect (session);
-                                       ssh_finalize ();
+                                       exec_ssh_command (host_ssh, utente, password, command);
                                }
                }
 
@@ -424,6 +441,7 @@ autesmbldap_load_users_list ()
 
        gtk_list_store_clear (lstore_users);
 
+       result = NULL;
        retldap = ldap_search_ext_s (ldap, g_strdup_printf ("%s,%s", users_ou, base_dn), LDAP_SCOPE_ONELEVEL,
                                     NULL, attrs, 0, NULL, NULL, LDAP_NO_LIMIT,
                                     LDAP_NO_LIMIT, &result);
@@ -505,6 +523,7 @@ autesmbldap_load_groups_list ()
 
        gtk_list_store_clear (lstore_groups);
 
+       result = NULL;
        retldap = ldap_search_ext_s (ldap, g_strdup_printf ("%s,%s", groups_ou, base_dn), LDAP_SCOPE_ONELEVEL,
                                     NULL, attrs, 0, NULL, NULL, LDAP_NO_LIMIT,
                                     LDAP_NO_LIMIT, &result);
@@ -636,30 +655,65 @@ static void
 autesmbldap_on_btn_delete_clicked (GtkButton *button,
                            gpointer user_data)
 {
-       /*GtkWidget *dialog;
+       GtkWidget *dialog;
        gboolean risp;
 
        GtkTreeIter iter;
        gchar *code;
 
-       if (gtk_tree_selection_get_selected (sel_users, NULL, &iter))
+       GtkListStore *lstore;
+       GtkTreeSelection *selection;
+       gchar *str_type;
+       static void (*load_list) (void);
+       guint col_cn;
+       gchar *command;
+
+       if (gtk_notebook_get_current_page (GTK_NOTEBOOK (notebook)) == 0)
+               {
+                       lstore = lstore_users;
+                       selection = sel_users;
+                       str_type = g_strdup ("user");
+                       load_list = &autesmbldap_load_users_list;
+                       col_cn = COL_USERS_CN;
+                       command = g_strdup ("sudo ");
+               }
+       else if (gtk_notebook_get_current_page (GTK_NOTEBOOK (notebook)) == 1)
+               {
+                       lstore = lstore_groups;
+                       selection = sel_groups;
+                       str_type = g_strdup ("group");
+                       load_list = &autesmbldap_load_groups_list;
+                       col_cn = COL_GROUPS_CN;
+                       command = g_strdup ("sudo /usr/sbin/smbldap-groupdel ");
+               }
+       else
+               {
+                       dialog = gtk_message_dialog_new (autesmbldap_get_gtkwidget_parent_gtkwindow (w_users),
+                                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                        GTK_MESSAGE_WARNING,
+                                                                                        GTK_BUTTONS_OK,
+                                                                                        "Wrong notebook page.");
+                       return;
+               }
+
+       if (gtk_tree_selection_get_selected (selection, NULL, &iter))
                {
                        dialog = gtk_message_dialog_new (autesmbldap_get_gtkwidget_parent_gtkwindow (w_users),
                                                                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                                                                         GTK_MESSAGE_QUESTION,
                                                                                         GTK_BUTTONS_YES_NO,
-                                                                                        "Sicuro di voler eliminare l'utente selezionato?");
+                                                                                        "Are you sure to want to delete the selected %s?", str_type);
                        risp = gtk_dialog_run (GTK_DIALOG (dialog));
                        if (risp == GTK_RESPONSE_YES)
                                {
-                                       gtk_tree_model_get (GTK_TREE_MODEL (lstore_users), &iter,
-                                                                                       COL_CODE, &code,
+                                       gchar *cn;
+                                       gtk_tree_model_get (GTK_TREE_MODEL (lstore), &iter,
+                                                                                       col_cn, &cn,
                                                                                        -1);
 
-                                       gdaex_execute (gdaex,
-                                                      g_strdup_printf ("UPDATE users SET status = 'E' WHERE code = '%s'", code));
+                                       exec_ssh_command (host_ssh, utente, password, g_strdup_printf ("%s%s", command, cn));
 
-                                       autesmbldap_load_users_list ();
+                                       (*load_list) ();
                                }
                        gtk_widget_destroy (dialog);
                }
@@ -669,10 +723,10 @@ autesmbldap_on_btn_delete_clicked (GtkButton *button,
                                                                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                                                                         GTK_MESSAGE_WARNING,
                                                                                         GTK_BUTTONS_OK,
-                                                                                        "Occorre prima selezionare un modello");
+                                                                                        "You must select a %s before.", str_type);
                        gtk_dialog_run (GTK_DIALOG (dialog));
                        gtk_widget_destroy (dialog);
-               }*/
+               }
 }
 
 static void
@@ -810,6 +864,8 @@ GtkWidget
                        return NULL;
                }
 
+       notebook = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "notebook1"));
+
        trv_users = GTK_TREE_VIEW (gtk_builder_get_object (gtkbuilder, "treeview1"));
        lstore_users = GTK_LIST_STORE (gtk_builder_get_object (gtkbuilder, "lstore_users"));
        sel_users = gtk_tree_view_get_selection (trv_users);