]> saetta.ns0.it Git - zakauthe/plugins/smbldap/commitdiff
Bugfixes and adjustments.
authorAndrea Zagli <azagli@libero.it>
Sun, 5 Sep 2010 09:10:21 +0000 (11:10 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 5 Sep 2010 09:10:21 +0000 (11:10 +0200)
Now open the SSH connection only at the first request.

src/aute_smbldap.c

index 412e6fe6a7ef7a80c19b350bf725b1a9e0cfc868..b2b4044682d2e3318ae2a7bc5e421ddf327229d0 100644 (file)
@@ -65,6 +65,9 @@ static GtkTreeView *trv_groups;
 static GtkListStore *lstore_groups;
 static GtkTreeSelection *sel_groups;
 
+static ssh_session session = NULL;
+static ssh_channel channel = NULL;
+
 enum
 {
        COL_USERS_STATUS,
@@ -82,20 +85,20 @@ enum
 /* PRIVATE */
 #ifdef HAVE_LIBCONFI
 static gboolean
-get_connection_parameters_from_confi (Confi *confi, gchar **host, gchar **base_dn, gchar **user_field)
+get_connection_parameters_from_confi (Confi *confi, gchar **user_field)
 {
        gboolean ret = TRUE;
 
-       *host = confi_path_get_value (confi, "aute/aute-smbldap/ldap/host");
-       *base_dn = confi_path_get_value (confi, "aute/aute-smbldap/ldap/base_dn");
+       host = confi_path_get_value (confi, "aute/aute-smbldap/ldap/host");
+       base_dn = confi_path_get_value (confi, "aute/aute-smbldap/ldap/base_dn");
        users_ou = confi_path_get_value (confi, "aute/aute-smbldap/ldap/users_ou");
        groups_ou = confi_path_get_value (confi, "aute/aute-smbldap/ldap/groups_ou");
        *user_field = confi_path_get_value (confi, "aute/aute-smbldap/ldap/user_field");
 
-       if (*host == NULL
-           || strcmp (g_strstrip (*host), "") == 0
+       if (host == NULL
+           || strcmp (g_strstrip (host), "") == 0
            || base_dn == NULL
-           || strcmp (g_strstrip (*base_dn), "") == 0
+           || strcmp (g_strstrip (base_dn), "") == 0
            || users_ou == NULL
            || strcmp (g_strstrip (users_ou), "") == 0
            || groups_ou == NULL
@@ -103,8 +106,8 @@ get_connection_parameters_from_confi (Confi *confi, gchar **host, gchar **base_d
            || user_field == NULL
            || strcmp (g_strstrip (*user_field), "") == 0)
                {
-                       *host = NULL;
-                       *base_dn = NULL;
+                       host = NULL;
+                       base_dn = NULL;
                        users_ou = NULL;
                        groups_ou = NULL;
                        *user_field = NULL;
@@ -142,99 +145,130 @@ 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;
-               }
+                       session = ssh_new ();
+                       if (session == NULL)
+                               {
+                                       g_warning ("Unable to open the SSH session.");
+                                       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;
-               }
+                       if (ssh_options_set (session, SSH_OPTIONS_USER, utente) < 0)
+                               {
+                                       g_warning ("Unable to set the SSH user.");
+                                       ssh_disconnect (session);
+                                       session = NULL;
+                                       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;
-               }
+                       /*g_message("HOST SSH: %s",host_ssh);*/
+                       if (ssh_options_set (session, SSH_OPTIONS_HOST, host) < 0)
+                               {
+                                       g_warning ("Unable to set the SSH host (%s).", host);
+                                       ssh_disconnect (session);
+                                       session = NULL;
+                                       return;
+                               }
+                       if (ssh_connect (session))
+                               {
+                                       g_warning ("SSH connection failed to host «%s» (%s).", host, ssh_get_error (session));
+                                       ssh_disconnect (session);
+                                       session = NULL;
+                                       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;
+                       rc = ssh_userauth_password (session, NULL, password);
+                       if (rc == SSH_AUTH_ERROR)
+                               {
+                                       g_warning ("SSH authentication failed.");
+                                       ssh_disconnect (session);
+                                       session = NULL;
+                                       return;
+                               }
+
+                       if (session == NULL)
+                               {
+                                       g_warning ("SSH session null.");
+                                       return;
+                               }
                }
 
-       if (session == NULL)
+       if (channel == NULL || !channel_is_open (channel))
                {
-                       g_warning ("Errore durante il cambio della password: sessione SSH nulla.");
-                       return;
+                       if (channel == NULL)
+                               {
+                                       channel = channel_new (session);
+                                       if (channel == NULL)
+                                               {
+                                                       g_warning ("Unable to open the SSH channel.");
+                                                       ssh_disconnect (session);
+                                                       ssh_finalize ();
+                                                       return;
+                                               }
+                               }
+
+                       rc = channel_open_session (channel);
+                       if (rc < 0)
+                               {
+                                       g_warning ("Unable to open the SSH session on the channel.");
+                                       channel_close (channel);
+                                       ssh_disconnect (session);
+                                       ssh_finalize ();
+                                       channel = NULL;
+                                       return;
+                               }
                }
 
-       channel = channel_new (session);
-       if (channel == NULL)
+       if (command != NULL)
                {
-                       g_warning ("Errore durante il cambio della password: impossibile aprire il canale SSH.");
-                       ssh_disconnect (session);
-                       ssh_finalize ();
-                       return;
+                       gchar *cmd;
+
+                       cmd = g_strstrip (g_strdup (command));
+                       if (g_strcmp0 (cmd, "") == 0) return;
+
+                       /*g_message ("COMMAND: %s", cmd);*/
+                       rc = channel_request_exec (channel, cmd);
+                       if (rc < 0)
+                               {
+                                       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);
                }
+}
 
-       rc = channel_open_session (channel);
-       if (rc < 0)
+static void
+autesmbldap_disconnect_ssh ()
+{
+       if (channel != NULL)
                {
-                       g_warning ("Errore durante il cambio della password: impossibile aprire la sessione sul canale SSH.");
+                       channel_send_eof (channel);
                        channel_close (channel);
-                       ssh_disconnect (session);
-                       ssh_finalize ();
-                       return;
                }
 
-       /*g_message ("COMMAND: %s", command);*/
-       rc = channel_request_exec (channel, command);
-       if (rc < 0)
+       if (session != NULL)
                {
-                       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 ();
+       channel = NULL;
+       session = NULL;
 }
 
 static gchar
@@ -266,7 +300,7 @@ static gchar
        /* leggo i parametri di connessione dalla configurazione */
        if (IS_CONFI (parameters->data))
                {
-                       if (!get_connection_parameters_from_confi (CONFI (parameters->data), &host, &base_dn, &user_field))
+                       if (!get_connection_parameters_from_confi (CONFI (parameters->data), &user_field))
                                {
                                        host = NULL;
                                        base_dn = NULL;
@@ -371,14 +405,14 @@ static gchar
        retldap = ldap_initialize (&ldap, host);
        if (retldap != LDAP_SUCCESS)
                {
-                       g_warning ("Errore nell'inizializzazione: %s", ldap_err2string (retldap));
+                       g_warning ("Error on LDAP initialization: %s", ldap_err2string (retldap));
                        return NULL;
                }
 
        retldap = ldap_set_option (ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
        if (retldap != LDAP_OPT_SUCCESS)
                {
-                       g_warning ("Errore nell'impostazione della versione del protocollo: %s", ldap_err2string (retldap));
+                       g_warning ("Error on setting LDAP protocol version: %s", ldap_err2string (retldap));
                        return NULL;
                }
 
@@ -386,7 +420,7 @@ static gchar
        retldap = ldap_simple_bind_s (ldap, user_dn, password);
        if (retldap != LDAP_SUCCESS)
                {
-                       g_warning ("Errore nel bind: %s", ldap_err2string (retldap));
+                       g_warning ("Error on LDAP binding: %s", ldap_err2string (retldap));
                        return NULL;
                }
 
@@ -404,12 +438,12 @@ static gchar
                        if (strlen (password_nuova) == 0 || strcmp (g_strstrip (password_nuova), "") == 0)
                                {
                                        /* TO DO */
-                                       g_warning ("La nuova password è vuota.");
+                                       g_warning ("The new password is 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 ("La nuova password e la conferma non coincidono.");
+                                       g_warning ("The new password and the confirm don't match.");
                                }
                        else
                                {
@@ -435,15 +469,20 @@ autesmbldap_load_users_list ()
 
        guint i;
 
-       gchar *attrs[] = {"cn", "displayName", "uidNumber"};
+       gchar **attrs;
 
        GtkTreeIter iter;
+       gchar *base;
 
        gtk_list_store_clear (lstore_users);
 
+       attrs = g_strsplit ("cn|displayName|uidNumber", "|", -1);
+
        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,
+       base = g_strdup_printf ("%s,%s", users_ou, base_dn);
+       /*g_warning ("BASE USERS %s", base);*/
+       retldap = ldap_search_ext_s (ldap, base, LDAP_SCOPE_ONELEVEL,
+                                    NULL, attrs, 0, NULL, NULL, NULL,
                                     LDAP_NO_LIMIT, &result);
        if (retldap != LDAP_SUCCESS)
                {
@@ -503,6 +542,7 @@ autesmbldap_load_users_list ()
 
                        entry = ldap_next_entry (ldap, entry);  
                }
+       ldap_msgfree (result);
 }
 
 static void
@@ -517,14 +557,19 @@ autesmbldap_load_groups_list ()
 
        guint i;
 
-       gchar *attrs[] = {"cn", "gidNumber"};
+       gchar **attrs;
 
        GtkTreeIter iter;
+       gchar *base;
 
        gtk_list_store_clear (lstore_groups);
 
+       attrs = g_strsplit ("cn|gidNumber", "|", -1);
+
        result = NULL;
-       retldap = ldap_search_ext_s (ldap, g_strdup_printf ("%s,%s", groups_ou, base_dn), LDAP_SCOPE_ONELEVEL,
+       base = g_strdup_printf ("%s,%s", groups_ou, base_dn);
+       /*g_warning ("BASE GROUPS %s", base);*/
+       retldap = ldap_search_ext_s (ldap, base, LDAP_SCOPE_ONELEVEL,
                                     NULL, attrs, 0, NULL, NULL, LDAP_NO_LIMIT,
                                     LDAP_NO_LIMIT, &result);
        if (retldap != LDAP_SUCCESS)
@@ -675,7 +720,7 @@ autesmbldap_on_btn_delete_clicked (GtkButton *button,
                        str_type = g_strdup ("user");
                        load_list = &autesmbldap_load_users_list;
                        col_cn = COL_USERS_CN;
-                       command = g_strdup ("sudo ");
+                       command = g_strdup ("sudo /usr/sbin/smbldap-userdel -r ");
                }
        else if (gtk_notebook_get_current_page (GTK_NOTEBOOK (notebook)) == 1)
                {
@@ -827,6 +872,13 @@ gchar
        return ret;
 }
 
+static void
+autesmbldap_on_vbx_users_list_destroy (GtkObject *object,
+                            gpointer user_data)
+{
+       autesmbldap_disconnect_ssh ();
+}
+
 /**
  * get_management_gui:
  * @parameters:
@@ -864,6 +916,9 @@ GtkWidget
                        return NULL;
                }
 
+       g_signal_connect (gtk_builder_get_object (gtkbuilder, "vbx_users_list"),
+                         "destroy", G_CALLBACK (autesmbldap_on_vbx_users_list_destroy), NULL);
+
        notebook = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "notebook1"));
 
        trv_users = GTK_TREE_VIEW (gtk_builder_get_object (gtkbuilder, "treeview1"));