]> saetta.ns0.it Git - zakauthe/plugins/smbldap/commitdiff
Enabled changing user password.
authorAndrea Zagli <azagli@libero.it>
Sat, 4 Sep 2010 09:15:47 +0000 (11:15 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 4 Sep 2010 09:15:47 +0000 (11:15 +0200)
src/aute_smbldap.c

index 48658ea0f318c9056eddd38dd05fbd64af5410d1..29710c6e57e4dbaf486fe8380aa21587ef1d98db 100644 (file)
@@ -22,6 +22,7 @@
   
 #include <gtk/gtk.h>
 #include <ldap.h>
+#include <libssh/libssh.h>
 
 #include <libgtkform/fielddatetime.h>
 
@@ -279,6 +280,128 @@ static gchar
                        return NULL;
                }*/
 
+       if (g_strcmp0 (user_dn, "") != 0
+           && gtk_expander_get_expanded (GTK_EXPANDER (exp_cambio)))
+               {
+                       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)
+                               {
+                                       /* TO DO */
+                                       g_warning ("La nuova password è vuota.");
+                               }
+                       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.");
+                               }
+                       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 -S -s \"echo -e \\\"%s\\n%s\\\" | /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 ();
+                               }
+               }
+
        return user_dn;
 }