CASA/c_adlib/FFEngine.cs
Manohar 2fb2da98a9
2006-03-06 09:13:59 +00:00

364 lines
14 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/***********************************************************************
*
* 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()
{
//Get Profile Names
//Console.WriteLine("FFEngine Aggregate:Invoking GetDefaultProfileName:");
defaultProfileName=FireFox.GetDefaultProfileName();
//This can be extended to get all profiles : Use FireFox.GetAllProfileNames()
//Currently we support only the default profile.
if(defaultProfileName!=null)
{
//Console.WriteLine("FFEngine Constructor:Initializing the Profile "+defaultProfileName);
initProfileStatus=FireFox.InitProfile(defaultProfileName);
//Console.WriteLine("FFEngine Constructor:InitProfile returned "+initProfileStatus);
}
}
~FFEngine()
{
//Uninitialize the profile if initialized earlier
//FIXME:- CURRENTLY NOT IVOKED SINCE THERES IS A BUG WHICH NEEDS TO BE FIXED IN FF NATIVE
//Console.WriteLine("FFEngine Destructor:UnInitializing the Profile "+defaultProfileName);
FireFox.UninitProfile(defaultProfileName);
}
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....
//Console.WriteLine("FFEngine:Returning Aggregated Node for FireFox:");
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;
//Console.WriteLine("FireFox:Getting Data for profile "+profileName);
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);
//Console.WriteLine("--------------------------------------------");
//Console.WriteLine("FFEngine::hostName="+hostName);
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);
//Console.WriteLine("FFEngine::name="+name);
value = (String)Marshal.PtrToStringAnsi(hostElementList.value);
//Console.WriteLine("FFEngine::value="+value);
isPassword = hostElementList.isPassword;
//Console.WriteLine("FFEngine::isPassword="+isPassword);
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)
{
//Console.WriteLine("FFEngine::End of all Data Items##");
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
}
}