CASA/c_adlib/FFEngine.cs

364 lines
14 KiB
C#
Raw Normal View History

2006-02-28 12:28:32 +01:00
/***********************************************************************
*
* Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This library 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
* Library Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail,
* you may find current contact information at www.novell.com.
*
***********************************************************************/
using System;
using System.Collections;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using Novell.CASA.DataEngines.Common;
using Novell.CASA.DataEngines.FF;
namespace Novell.CASA.DataEngines
{
/*
* This class is implementation of Data engine for FireFox.
*/
class FFEngine : DataEngine
{
private static string XML_FIRFOXSECRET_TYPE_SIGNON = "Signon";
private String defaultProfileName=null;
private int initProfileStatus=-1;
enum FireFoxResultExtended {
FIREFOX_RESULT_SUCESS = 1,
FIREFOX_RESULT_ERROR_UNKNOWN = 128
};
public FFEngine()
{
2006-03-06 10:13:59 +01:00
//Get Profile Names
//Console.WriteLine("FFEngine Aggregate:Invoking GetDefaultProfileName:");
2006-02-28 12:28:32 +01:00
defaultProfileName=FireFox.GetDefaultProfileName();
//This can be extended to get all profiles : Use FireFox.GetAllProfileNames()
//Currently we support only the default profile.
if(defaultProfileName!=null)
{
2006-03-01 11:47:25 +01:00
//Console.WriteLine("FFEngine Constructor:Initializing the Profile "+defaultProfileName);
2006-02-28 12:28:32 +01:00
initProfileStatus=FireFox.InitProfile(defaultProfileName);
2006-03-01 11:47:25 +01:00
//Console.WriteLine("FFEngine Constructor:InitProfile returned "+initProfileStatus);
2006-02-28 12:28:32 +01:00
}
2006-03-06 10:13:59 +01:00
}
2006-02-28 12:28:32 +01:00
~FFEngine()
{
2006-03-06 10:13:59 +01:00
//Uninitialize the profile if initialized earlier
2006-02-28 12:28:32 +01:00
//FIXME:- CURRENTLY NOT IVOKED SINCE THERES IS A BUG WHICH NEEDS TO BE FIXED IN FF NATIVE
2006-03-06 10:13:59 +01:00
//Console.WriteLine("FFEngine Destructor:UnInitializing the Profile "+defaultProfileName);
FireFox.UninitProfile(defaultProfileName);
2006-02-28 12:28:32 +01:00
}
public XmlNode Aggregate()
{
//Init XML Document
XmlDocument doc = new XmlDocument();
XmlNode rootElem = doc.CreateElement(ConstStrings.CCF_FFTAG);
doc.AppendChild(rootElem);
//Initialize & Aggregate DefaultProfile from Firefox
readProfileToCCF(doc,rootElem,defaultProfileName);
//Initialize & Aggregate any other profiles here if required....
2006-03-01 11:47:25 +01:00
//Console.WriteLine("FFEngine:Returning Aggregated Node for FireFox:");
2006-02-28 12:28:32 +01:00
return rootElem;
}
public int SetSecret(XmlNode secret)
{
return (int)FireFoxResultExtended.FIREFOX_RESULT_ERROR_UNKNOWN;
}
public int SetSecret(XmlNode secret, int opnType)
{
return (int)FireFoxResultExtended.FIREFOX_RESULT_ERROR_UNKNOWN;
}
public int GetSecret(XmlNode secret)
{
return ConstStrings.CASA_SUCCESS;
}
public int Remove(XmlNode secret)
{
return (int)FireFoxResultExtended.FIREFOX_RESULT_ERROR_UNKNOWN;
}
//--------------------------------------------------------------
//isMasterPasswordSet
//Is MasterPassword Set For the specified (Currently-Default) profile
//@param
// None ; For supporting multiple profiles,
// this needs to be extended to pass profile names
//
// @return 1 if master password is set
// 0 if master password not set
//--------------------------------------------------------------
public int isMasterPasswordSet()
{
return (int)FireFox.isMasterPasswordSetFor(defaultProfileName);
}
//--------------------------------------------------------------
//verifyMasterPassword
//Check if the specified master password is correct or not for the (Currently-Default) profile.
//If it is correct then password is stored to the internal store for later use.
//If it is wrong then nothing is stored and 0 will be returned.
//
//@param
// masterPassword (string)
// ; For supporting multiple profiles, this needs to be extended to pass profile names
//
// @return 1 if the specified master password is correct
// 0 if the master password is wrong.
//--------------------------------------------------------------
public int verifyMasterPassword(string masterPassword)
{
return (int)FireFox.checkMasterPassword(defaultProfileName,masterPassword);
}
//=================Local Methods====================================
//--------------------------------------------------------------
//readProfileToCCF
//@param
// doc (XmlDocument) XML CCF Document for Firefox
// profileName (string) name of the profile to be read
//@return 1 on success
// <=0 on error
//  <FireFox>
// <Profile ID="Default">
// <Secret ID="hostname url" Type="Signon">
// <Key ID="UserName">
// <value>user1</value>
// </Key>
// <Key ID="Password">
// <value>password value</value>
// </Key>
// </Secret>
// </Profile>
//  </FireFox>
//--------------------------------------------------------------
public int readProfileToCCF(XmlDocument doc,XmlNode rootElement,string profileName)
{
int methodStatusCode=-1;
if((profileName!=null) && (initProfileStatus==(int)FireFoxResultExtended.FIREFOX_RESULT_SUCESS))
{
Host hostList = null;
2006-03-01 11:47:25 +01:00
//Console.WriteLine("FireFox:Getting Data for profile "+profileName);
2006-02-28 12:28:32 +01:00
hostList=FireFox.GetProfileData(profileName);
if(hostList!=null)
{// Something to Aggregate
XmlElement xmlProfileElement;
String hostName;
String name;
String value;
int isPassword;
methodStatusCode=1;
xmlProfileElement = doc.CreateElement(ConstStrings.CCF_FFPROFILE); //<Profile>
XmlAttribute idAttr = doc.CreateAttribute(ConstStrings.CCF_ID); //<Profile>-ID
idAttr.Value = profileName;
xmlProfileElement.SetAttributeNode(idAttr);
while (hostList != null)
{
HostElement hostElementList;
XmlElement xmlSecretElement = doc.CreateElement(ConstStrings.CCF_SECRET); //<Secret>
hostName = (String)Marshal.PtrToStringAnsi(hostList.hostName);
2006-03-01 11:47:25 +01:00
//Console.WriteLine("--------------------------------------------");
//Console.WriteLine("FFEngine::hostName="+hostName);
2006-02-28 12:28:32 +01:00
XmlAttribute secIdAttr = doc.CreateAttribute(ConstStrings.CCF_ID); //<Secret>-ID
secIdAttr.Value = hostName;
xmlSecretElement.SetAttributeNode(secIdAttr);
XmlAttribute typeAttr = doc.CreateAttribute(ConstStrings.CCF_TYPE); //<Secret>-Type
typeAttr.Value = XML_FIRFOXSECRET_TYPE_SIGNON;
xmlSecretElement.SetAttributeNode(typeAttr);
//hostElementList = new HostElement();
hostElementList = (HostElement)Marshal.PtrToStructure(hostList.hostElement, typeof(HostElement));
while (hostElementList != null)
{
XmlElement xmlKeyElement = null;
XmlAttribute keyIdAttr = null;
XmlElement xmlValueElement = null;
name = (String)Marshal.PtrToStringAnsi(hostElementList.name);
2006-03-01 11:47:25 +01:00
//Console.WriteLine("FFEngine::name="+name);
2006-02-28 12:28:32 +01:00
value = (String)Marshal.PtrToStringAnsi(hostElementList.value);
2006-03-01 11:47:25 +01:00
//Console.WriteLine("FFEngine::value="+value);
2006-02-28 12:28:32 +01:00
isPassword = hostElementList.isPassword;
2006-03-01 11:47:25 +01:00
//Console.WriteLine("FFEngine::isPassword="+isPassword);
2006-02-28 12:28:32 +01:00
xmlKeyElement = doc.CreateElement(ConstStrings.CCF_KEY); //<Key
keyIdAttr = doc.CreateAttribute(ConstStrings.CCF_ID); //<Key>-ID
keyIdAttr.Value = name;
xmlKeyElement.SetAttributeNode(keyIdAttr);
xmlValueElement = doc.CreateElement(ConstStrings.CCF_VALUE); //<value>
xmlValueElement.InnerText = value;
xmlKeyElement.AppendChild(xmlValueElement); //</value>
xmlSecretElement.AppendChild(xmlKeyElement); //</Key
//INNERLOOP-Loop for hostElementList
if (hostElementList.next == IntPtr.Zero)
{
break;
}
hostElementList = (HostElement)Marshal.PtrToStructure(hostElementList.next, typeof(HostElement));
}
xmlProfileElement.AppendChild(xmlSecretElement); //</Secret>
//OUTERLOOP-Loop for hostList
if (hostList.next == IntPtr.Zero)
{
2006-03-01 11:47:25 +01:00
//Console.WriteLine("FFEngine::End of all Data Items##");
2006-02-28 12:28:32 +01:00
break;
}
hostList = (Host)Marshal.PtrToStructure(hostList.next, typeof(Host));
}
rootElement.AppendChild(xmlProfileElement); //</Profile>
}
}//~Aggregate profileName
return methodStatusCode;
}
public static Boolean IsStoreAvailable()
{
try
{
System.Runtime.InteropServices.Marshal.PrelinkAll(typeof(FireFox));
return true;
}
catch(DllNotFoundException d)
{
//Console.WriteLine("Store Not Available Exception = " + d.ToString());
return false;
}
catch(Exception e)
{
//Console.WriteLine("Store Not Available Exception = " + e.ToString());
return false;
}
}
//#if TEST
public static void Main()
{
FFEngine ff = new FFEngine();
Console.WriteLine();
Console.WriteLine("********** Menu ***********");
Console.WriteLine("* 5. Refresh *");
Console.WriteLine("* 6. Is Store Avail *");
Console.WriteLine("* 7. Quit *");
Console.WriteLine("***************************");
Console.WriteLine("For all options the input is the file /root/fftest.xml");
Console.WriteLine("Select option and Press enter");
String line = Console.ReadLine();
int res = 0;
if (line.Length > 0)
{
char[] c = line.Substring(0, 1).ToCharArray();
if (c.Length > 0)
{
if (c[0].Equals('7'))
return;
if (c[0].Equals('6'))
{
Console.WriteLine("Store Available is = " + FFEngine.IsStoreAvailable());
return;
}
if (c[0].Equals('5'))
{
Console.WriteLine("FFEngine:Main:Aggregating:");
XmlNode node = ff.Aggregate ();
XmlDocument doc = node.OwnerDocument;
XmlTextWriter writer = new XmlTextWriter("/root/fftest.xml",null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
writer.Close();
}
else
{
XmlDocument xmlDoc = new XmlDocument();
XmlTextReader tr = new XmlTextReader("/root/fftest.xml");
tr.Read();
xmlDoc.Load(tr);
XmlNode root = xmlDoc.LastChild;
if (root == null)
{
Console.WriteLine("Root is null");
}
XmlNode secret = root.ChildNodes[0].ChildNodes[0];
Console.WriteLine("secret Name \n" + secret.Name);
}
}
}
Console.WriteLine("Result of Operation = " + res);
}
//#endif
}
}