Checked in for changes in ADLib for Add-Modify-Delete for KWallet and

Keyring
This commit is contained in:
austinsfdsouza
2005-12-16 10:29:25 +00:00
parent aa9c895a41
commit ec113054eb
9 changed files with 1037 additions and 72 deletions

View File

@@ -4,8 +4,6 @@ using System.Xml;
using System.IO;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using Gtk;
using GLib;
using Novell.CASA.DataEngines.Common;
using Novell.CASA.DataEngines.GK;
@@ -19,6 +17,16 @@ namespace Novell.CASA.DataEngines
class GKEngine : DataEngine
{
enum KeyringResultExtended {
GNOME_KEYRING_RESULT_NO_SUCH_ITEM = 128,
GNOME_KEYRING_RESULT_CANNOT_DELETE_SECRET_VALUE,
GNOME_KEYRING_RESULT_MALFORMED_XML,
GNOME_KEYRING_RESULT_CANNOT_CREATE_KEYRING,
GNOME_KEYRING_RESULT_ERROR_UNKNOWN
};
public GKEngine()
{
@@ -152,10 +160,69 @@ namespace Novell.CASA.DataEngines
return doc.ChildNodes[0];
}
public int SetSecret(XmlNode secret)
public int SetSecret(XmlNode secret)
{
return ConstStrings.CASA_SUCCESS;
}
string password = null;
int retValue;
try
{
Console.WriteLine("In SetSecret Name = "+ secret.Name);
int itemid = ExtractSecretId(secret);
string keyringname = ExtractKeyringName(secret);
NameValueCollection newNVC = new System.Collections.Specialized.NameValueCollection();
XmlNodeList keylist = secret.SelectNodes("descendant::Key");
Console.WriteLine("In SetSecret");
foreach (XmlNode tuple in keylist)
{
//Get the Key
XmlAttributeCollection at = tuple.Attributes;
String keyname = (at["ID"]).InnerText;
if (keyname.Equals("GKPassword"))
{
password = tuple.ChildNodes[0].InnerText;
}
else
{
newNVC.Add(keyname, tuple.ChildNodes[0].InnerText);
}
}
if (itemid == -2) //Add Item Opn
{
string strItemType = ExtractItemType(secret);
string secretName = ExtractSecretName(secret);
return(GnomeKeyring.CreateSecret(keyringname, strItemType, secretName, password, newNVC));
}
//Modify secret Opn
if ( password != null)
{
retValue = GnomeKeyring.SetPassword(keyringname, itemid, password);
if (retValue != 0)
{
return retValue;
}
}
if (newNVC.Count != 0)
{
return (GnomeKeyring.SetAttributes( keyringname, itemid, newNVC));
}
return 0;
}
catch(NullReferenceException n)
{
Console.WriteLine("Exception in SetSecret = "+n.ToString());
return (int)KeyringResultExtended.GNOME_KEYRING_RESULT_MALFORMED_XML;
}
catch(Exception e)
{
Console.WriteLine("Exception in SetSecret = "+e.ToString());
return (int)KeyringResultExtended.GNOME_KEYRING_RESULT_ERROR_UNKNOWN;
}
}
public int GetSecret(XmlNode secret)
{
@@ -163,8 +230,150 @@ namespace Novell.CASA.DataEngines
}
public int Remove(XmlNode secret)
{
return ConstStrings.CASA_SUCCESS;
try
{
int itemid = ExtractSecretId(secret);
string keyringname = ExtractKeyringName(secret);
return (GnomeKeyring.RemoveItem( keyringname, itemid));
}
catch(NullReferenceException n)
{
Console.WriteLine("Exception in Remove = "+n.ToString());
return (int)KeyringResultExtended.GNOME_KEYRING_RESULT_MALFORMED_XML;
}
catch(Exception e)
{
Console.WriteLine("Exception in Remove = "+e.ToString());
return (int)KeyringResultExtended.GNOME_KEYRING_RESULT_ERROR_UNKNOWN;
}
}
int ExtractSecretId(XmlNode secret)
{
XmlAttributeCollection atcol = secret.Attributes;
String secretid = atcol["ID"].InnerXml;
Console.WriteLine("In Extract Secret Id");
//Check if itemId is present
if (secretid.EndsWith(":"))
{
Console.WriteLine("In Extract Secret Id : Add Opn");
return -2;
}
int itemIdx = secretid.LastIndexOf(":");
if (itemIdx == -1)
{
return -1;
}
int itemid = Convert.ToInt32(secretid.Substring(itemIdx + 1));
return itemid;
}
string ExtractSecretName(XmlNode secret)
{
XmlAttributeCollection atcol = secret.Attributes;
String secretid = atcol["ID"].InnerXml;
Console.WriteLine("In Extract Secret name");
int itemIdx = secretid.LastIndexOf(":");
Console.WriteLine("Extracting Secret Name Last Index Of : = " + itemIdx);
//Check if itemId is present
return(secretid.Substring(0,itemIdx));
}
string ExtractKeyringName(XmlNode secret)
{
XmlAttributeCollection atcol;
XmlNode parentNode = secret.ParentNode;
Console.WriteLine("In Extract Keyring Name");
atcol = parentNode.Attributes;
String keyringname = atcol["ID"].InnerXml;
return keyringname;
}
string ExtractItemType(XmlNode secret)
{
XmlAttributeCollection atcol = secret.Attributes;
String itemType = atcol[ConstStrings.CCF_TYPE].InnerXml;
Console.WriteLine("In ExtractItemType");
return(itemType);
}
#if TEST
public static void Main()
{
Console.WriteLine("Hello there");
GKEngine gk = new GKEngine();
Console.WriteLine();
Console.WriteLine("********** Menu ***********");
Console.WriteLine("* 1. Set secret *");
Console.WriteLine("* 2. Remove secret *");
Console.WriteLine("* 3. Refresh *");
Console.WriteLine("* 4. Quit *");
Console.WriteLine("***************************");
Console.WriteLine("For all options the input is the file /root/gktest.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('4'))
return;
if (c[0].Equals('3'))
{
XmlNode node = gk.Aggregate ();
XmlDocument doc = node.OwnerDocument;
XmlTextWriter writer = new XmlTextWriter("/root/gktest.xml",null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
writer.Close();
}
else
{
XmlDocument xmlDoc = new XmlDocument();
XmlTextReader tr = new XmlTextReader("/root/gktest.xml");
tr.Read();
xmlDoc.Load(tr);
XmlNode root = xmlDoc.FirstChild;
if (root == null)
{
Console.WriteLine("Root is null");
}
Console.WriteLine("Root is not null\n");
Console.WriteLine("Root Name \n" + root.Name);
Console.WriteLine("Keyring Name \n" + root.ChildNodes[0].Name);
Console.WriteLine("Secret Name \n" + root.ChildNodes[0].ChildNodes[0].Name);
XmlNode secret = root.ChildNodes[0].ChildNodes[0];
Console.WriteLine("secret Name \n" + secret.Name);
if (c[0].Equals('2'))
res =gk.Remove(secret);
else if (c[0].Equals('1'))
res = gk.SetSecret(secret);
}
}
}
Console.WriteLine("Result of Operation = " + res);
}
#endif
}
}