CASA/CASA/gui/ImportSecrets.cs

172 lines
4.4 KiB
C#

using System;
using System.IO;
using Novell.CASA.MiCasa.Communication;
using Novell.CASA.MiCasa.Common;
namespace Novell.CASA.GUI
{
/// <summary>
/// Summary description for ImportSecrets.
/// </summary>
public class ImportSecrets
{
Config m_config = null;
public MiCasa m_objMiCasa = null;
string sFile = null;
byte[] buffer = null;
[Glade.Widget]
Gtk.Entry entryMasterPassword;
[Glade.Widget]
Gtk.Dialog dialogImport,
dialogLogin;
public ImportSecrets(Config config, MiCasa objMiCasa)
{
m_config = config;
m_objMiCasa = objMiCasa;
}
public void Run()
{
String sHintDir = m_config.GetConfigSetting(CommonGUI.HINT_DIR, null);;
String sHintFilename = m_config.GetConfigSetting(CommonGUI.HINT_FILENAME, null);
FileChooser fc = new FileChooser(FileChooser.ACTION_OPEN);
sFile = fc.GetFile(sHintDir, sHintFilename, "*.casa");
//fc.Show(sHintDir, sHintFilename);
//sFile = fc.GetSelectedFile();
#if W32
// ask the user to locate the secret file to import
//sFile = CommonGUI.FileChooser(Gtk.FileChooserAction.Open, "Select import file", sHintDir, sHintFilename);
#else
//sFile = null;
//CommonGUI.DisplayMessage(Gtk.MessageType.Info, "Not implemented");
#endif
if (sFile != null)
{
// parse of the file:///
if (sFile.StartsWith("file:///"))
{
sFile = sFile.Substring(8);
}
if (File.Exists(sFile))
{
try
{
// let's read it
FileStream fs = new FileStream(sFile, FileMode.Open);
buffer = new byte[fs.Length];
int iBytes = fs.Read(buffer, 0, (int)fs.Length);
string data = System.Text.Encoding.ASCII.GetString(buffer);
fs.Flush();
fs.Close();
// check for clear text secrets
if (data.StartsWith("<?xml"))
{
ImportXMLSecrets addSecrets = new ImportXMLSecrets(null, null, sFile);
MiCasaRequestReply.Send(MiCasaRequestReply.VERB_ADD_XML_SECRETS, null, null, null, addSecrets);
CommonGUI.DisplayMessage(Gtk.MessageType.Info, "Import complete");
}
else
{
GetMasterPasswordUsedForImport();
}
}
catch (Exception e)
{
CommonGUI.DisplayMessage(Gtk.MessageType.Error, e.Message);
}
}
}
}
public void GetMasterPasswordUsedForImport()
{
// prompt for master password, and optional passphrase to encrypt
//Logger.DbgLog("GUI:CasaMain.Login() - IsMasterPasswordSet returned false");
#if W32
Glade.XML gxmlTemp = new Glade.XML ("../images/casa.glade", "dialogImport", null);
#endif
#if LINUX
Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogImport", null);
#endif
gxmlTemp.Autoconnect (this);
dialogImport.TransientFor = (Gtk.Window)CasaMain.gxmlMain.GetWidget("windowMain");;
dialogImport.Modal = true;
dialogImport.Show();
}
public void OnDialogLoginDeleted(object obj, EventArgs args)
{
//cbuttonShowPassword.Active = false;
dialogLogin.Destroy();
}
public void okbuttonLogin_clicked(object obj, EventArgs args)
{
dialogLogin.Destroy();
}
public void closebuttonLogin_clicked(object obj, EventArgs args)
{
dialogLogin.Destroy();
}
public void on_entryMasterPassword3_activate(object obj, EventArgs args)
{
}
public void on_entryMasterPassword4_activate(object obj, EventArgs args)
{
}
public void on_helpbuttonAuthentication_clicked(object obj, EventArgs args)
{
Common.ShowHelpUrl("CASAMasterPasswordAuthentication.htm");
}
private void on_buttonImportOK_clicked(object sender, EventArgs args)
{
if (entryMasterPassword != null)
{
ImportXMLSecrets addSecrets = new ImportXMLSecrets(entryMasterPassword.Text, null, sFile);
addSecrets = (ImportXMLSecrets)MiCasaRequestReply.Send(MiCasaRequestReply.VERB_ADD_XML_SECRETS, null, null, null, addSecrets);
if (dialogImport != null)
{
dialogImport.Destroy();
}
if (m_objMiCasa != null)
{
m_objMiCasa.AggregateStore();
}
if (addSecrets.GetStatus().Equals("Success"))
CommonGUI.DisplayMessage(Gtk.MessageType.Info, "Import complete");
else
CommonGUI.DisplayMessage(Gtk.MessageType.Info, "Failed: " + addSecrets.GetStatus());
}
}
private void on_buttonImportClose_clicked(object sender, EventArgs args)
{
if (dialogImport != null)
{
dialogImport.Destroy();
}
}
}
}