Checked in for changes in ADLib for Add-Modify-Delete for KWallet and
Keyring
This commit is contained in:
@@ -23,6 +23,19 @@ namespace Novell.CASA.DataEngines
|
||||
{
|
||||
|
||||
string[] EntryTypes = {"Binary","Passwords","Unknown", "Maps"};
|
||||
enum KWalletResult : int
|
||||
{
|
||||
KWALLET_RESULT_OK,
|
||||
|
||||
KWALLET_RESULT_CANNOT_OPEN_WALLET,
|
||||
KWALLET_RESULT_CANNOT_OPEN_FOLDER,
|
||||
KWALLET_RESULT_CANNOT_WRITE_ENTRY,
|
||||
KWALLET_RESULT_MALFORMED_XML,
|
||||
KWALLET_RESULT_CANNOT_CREATE_FOLDER,
|
||||
KWALLET_RESULT_CANNOT_CREATE_WALLET,
|
||||
KWALLET_RESULT_CANNOT_REMOVE_ENTRY,
|
||||
KWALLET_RESULT_UNKNOWN_ERROR
|
||||
};
|
||||
|
||||
|
||||
public KWalletEngine()
|
||||
@@ -154,6 +167,7 @@ namespace Novell.CASA.DataEngines
|
||||
if (attr.InnerXml.Equals(foldername))
|
||||
{
|
||||
xpath = "descendant::Type[@ID='"+entryType+"']";
|
||||
|
||||
XmlNodeList keylist = folder.SelectNodes(xpath);
|
||||
if (keylist.Count == 0)
|
||||
{
|
||||
@@ -161,7 +175,8 @@ namespace Novell.CASA.DataEngines
|
||||
}
|
||||
else
|
||||
{
|
||||
XmlNode TargetType = folder;
|
||||
|
||||
XmlNode TargetType = keylist[0]; //Type Node
|
||||
string[] split = null;
|
||||
|
||||
int index = secretval.IndexOf('=');
|
||||
@@ -172,6 +187,11 @@ namespace Novell.CASA.DataEngines
|
||||
XmlAttribute idattr = doc.CreateAttribute(ConstStrings.CCF_ID);
|
||||
idattr.Value = secid;
|
||||
Secret.SetAttributeNode(idattr);
|
||||
|
||||
XmlAttribute typeAttr = doc.CreateAttribute(ConstStrings.CCF_TYPE);
|
||||
typeAttr.Value = entryType;
|
||||
Secret.SetAttributeNode(typeAttr);
|
||||
|
||||
|
||||
|
||||
if (entryType.Equals("Maps"))
|
||||
@@ -215,22 +235,14 @@ namespace Novell.CASA.DataEngines
|
||||
value1.InnerText = value;
|
||||
key1.AppendChild(value1);
|
||||
Secret.AppendChild(key1);
|
||||
|
||||
|
||||
|
||||
TargetType.AppendChild(Secret);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}//entrytype=Maps
|
||||
else if (entryType.Equals("Passwords"))
|
||||
{
|
||||
|
||||
//Console.WriteLine("Passwords");
|
||||
val = secretval.Substring(index+1);
|
||||
|
||||
|
||||
//Key
|
||||
key1 = doc.CreateElement(ConstStrings.CCF_KEY);
|
||||
@@ -245,10 +257,8 @@ namespace Novell.CASA.DataEngines
|
||||
Secret.AppendChild(key1);
|
||||
|
||||
TargetType.AppendChild(Secret);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}//entryType=Password
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,17 +275,89 @@ namespace Novell.CASA.DataEngines
|
||||
}
|
||||
}
|
||||
kwallet.FreeResources();
|
||||
#if TEST
|
||||
XmlTextWriter writer = new XmlTextWriter("/root/kwtest.xml",null);
|
||||
writer.Formatting = Formatting.Indented;
|
||||
doc.Save(writer);
|
||||
writer.Close();
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
return doc.ChildNodes[0];
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************************
|
||||
SetSecret will modify the Value(s) of a Key(s) for an existing secret
|
||||
SetSecret will also add new secrets
|
||||
|
||||
|
||||
Parameters
|
||||
|
||||
secret : Secrets XMLNode
|
||||
1. If a Key node of a secret is missing then that key will be deleted
|
||||
2. For Gnome keyring, Key having Id "GkPassword" cannot be deleted as
|
||||
Gnome Api's do not allow it.
|
||||
|
||||
3. All Time nodes for a Secret need not be passed as they cannot be set.
|
||||
4. Keyring attributes have a fixed datatype of Int and String. Currently we support only String types.
|
||||
To support int types CCF needs to be modified accordingly.
|
||||
5. To signify that a GnomeKeyring secret should be added, append the secret's ID with a ":".
|
||||
|
||||
|
||||
StoreID : int value
|
||||
Novell.CASA.DataEngines.Common.ConstStrings.CASA_STORE_KWALLET;
|
||||
|
||||
Returns
|
||||
An Error code or 0 if operation is successfull.
|
||||
***************************************************************************************/
|
||||
|
||||
|
||||
public int SetSecret(XmlNode secret)
|
||||
{
|
||||
return ConstStrings.CASA_SUCCESS;
|
||||
try
|
||||
{
|
||||
string walletName = ExtractWalletName(secret);
|
||||
string folderName = ExtractFolderName(secret);
|
||||
string keyName = ExtractKeyName(secret);
|
||||
int secretType = ExtractSecretType(secret);
|
||||
if (secretType != 3) //Type not Map
|
||||
{
|
||||
string value = secret.ChildNodes[0].ChildNodes[0].InnerText; //Secret.Key.Value
|
||||
return(kwallet.SetSecret(walletName, folderName, secretType, keyName, value, value.Length));
|
||||
}
|
||||
else //If type is Map
|
||||
{
|
||||
NameValueCollection nvc = new NameValueCollection();
|
||||
for (int i =0; i< secret.ChildNodes.Count; i++)
|
||||
{
|
||||
XmlNode key = secret.ChildNodes[i];
|
||||
XmlAttributeCollection atcol;
|
||||
atcol = key.Attributes;
|
||||
String keyMapName = atcol["ID"].InnerXml;
|
||||
Console.WriteLine("Map Ele KeyName = " + keyMapName);
|
||||
string value = key.ChildNodes[0].InnerText; //Secret.Key.Value
|
||||
Console.WriteLine("Map Ele Value = " + value);
|
||||
nvc.Add(keyMapName,value);
|
||||
}
|
||||
|
||||
return(kwallet.SetSecret(walletName, folderName,keyName,nvc));
|
||||
}
|
||||
}
|
||||
catch(NullReferenceException n)
|
||||
{
|
||||
Console.WriteLine("Exception in Set Secret Cause :" + n.ToString());
|
||||
return (int)KWalletResult.KWALLET_RESULT_MALFORMED_XML;
|
||||
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Console.WriteLine("Exception in Set Secret Cause :" + e.ToString());
|
||||
return (int)KWalletResult.KWALLET_RESULT_UNKNOWN_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -287,53 +369,175 @@ namespace Novell.CASA.DataEngines
|
||||
return ConstStrings.CASA_SUCCESS;
|
||||
}
|
||||
|
||||
/**************************************************************************************
|
||||
Remove will delete a Secret.
|
||||
|
||||
Parameters
|
||||
|
||||
secret : Secrets XmlNode
|
||||
1. This node will be deleted from its parent.
|
||||
|
||||
|
||||
StoreID : int value
|
||||
Novell.CASA.DataEngines.Common.ConstStrings.CASA_STORE_KWALLET;
|
||||
|
||||
|
||||
Returns
|
||||
An Error code or 0 if operation is successfull. Error code is same as above
|
||||
|
||||
**************************************************************************************/
|
||||
|
||||
|
||||
public int Remove(XmlNode secret)
|
||||
public int Remove(XmlNode secret)
|
||||
{
|
||||
try
|
||||
{
|
||||
string walletName = ExtractWalletName(secret);
|
||||
string folderName = ExtractFolderName(secret);
|
||||
string keyName = ExtractKeyName(secret);
|
||||
int secretType = ExtractSecretType(secret);
|
||||
int res = kwallet.DeleteSecret(walletName, folderName, keyName);
|
||||
if (res == 0)
|
||||
{
|
||||
XmlNode root = secret.ParentNode;
|
||||
root.RemoveChild(secret);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
catch(NullReferenceException n)
|
||||
{
|
||||
Console.WriteLine("Exception in Set Secret Cause :" + n.ToString());
|
||||
return (int)KWalletResult.KWALLET_RESULT_MALFORMED_XML;
|
||||
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Console.WriteLine("Exception in Set Secret Cause :" + e.ToString());
|
||||
return (int)KWalletResult.KWALLET_RESULT_UNKNOWN_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
string ExtractWalletName(XmlNode secret)
|
||||
{
|
||||
return ConstStrings.CASA_SUCCESS;
|
||||
XmlAttributeCollection atcol;
|
||||
XmlNode parentNode = secret.ParentNode.ParentNode.ParentNode;
|
||||
Console.WriteLine("In Extract Wallet Name ");
|
||||
atcol = parentNode.Attributes;
|
||||
String walletname = atcol["ID"].InnerXml;
|
||||
Console.WriteLine("In Extract Wallet Name Wallet Name = " + walletname);
|
||||
return walletname;
|
||||
}
|
||||
|
||||
string ExtractFolderName(XmlNode secret)
|
||||
{
|
||||
XmlAttributeCollection atcol;
|
||||
XmlNode parentNode = secret.ParentNode.ParentNode; //Folder.Type.Secret
|
||||
Console.WriteLine("In Extract Folder Name ");
|
||||
atcol = parentNode.Attributes;
|
||||
String foldername = atcol["Name"].InnerXml;
|
||||
Console.WriteLine("In Extract Folder Name Folder Name = " + foldername);
|
||||
return foldername;
|
||||
}
|
||||
|
||||
string ExtractKeyName(XmlNode secret)
|
||||
{
|
||||
XmlAttributeCollection atcol;
|
||||
Console.WriteLine("In Extract Key Name ");
|
||||
atcol = secret.Attributes;
|
||||
String keyname = atcol["ID"].InnerXml;
|
||||
Console.WriteLine("In Extract Key Name Key Name = " + keyname);
|
||||
return keyname;
|
||||
}
|
||||
|
||||
int ExtractSecretType(XmlNode secret)
|
||||
{
|
||||
XmlAttributeCollection atcol;
|
||||
XmlNode parentNode = secret.ParentNode; //Type.Secret
|
||||
Console.WriteLine("In Extract Entry Type ");
|
||||
atcol = parentNode.Attributes;
|
||||
String entryType = atcol["ID"].InnerXml;
|
||||
Console.WriteLine("In Extract Entry Type = " + entryType);
|
||||
if (entryType.CompareTo("Passwords")== 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (entryType.CompareTo("Binary") == 0)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
if (entryType.CompareTo("Maps") == 0)
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#if TEST
|
||||
public static void Main()
|
||||
{
|
||||
Console.WriteLine("Hello there");
|
||||
KWalletEngine kw = new KWalletEngine();
|
||||
|
||||
|
||||
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/kwtest.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'))
|
||||
kw.Aggregate ();
|
||||
else
|
||||
{
|
||||
|
||||
XmlDocument xmlDoc = new XmlDocument();
|
||||
XmlTextReader tr = new XmlTextReader("/root/kwtest.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("Wallet Name \n" + root.ChildNodes[0].Name);
|
||||
Console.WriteLine("Folder Name \n" + root.ChildNodes[0].ChildNodes[0].Name);
|
||||
Console.WriteLine("Type Name \n" + root.ChildNodes[0].ChildNodes[0].ChildNodes[0].Name);
|
||||
Console.WriteLine("Secret Name \n" + root.ChildNodes[0].ChildNodes[0].ChildNodes[0].ChildNodes[0].Name);
|
||||
XmlNode secret = root.ChildNodes[0].ChildNodes[0].ChildNodes[0].ChildNodes[0];
|
||||
|
||||
if (c[0].Equals('2'))
|
||||
res =kw.Remove(secret);
|
||||
else if (c[0].Equals('1'))
|
||||
res = kw.SetSecret(secret);
|
||||
}
|
||||
}
|
||||
}
|
||||
Console.WriteLine("Result of Operation = " + res);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user