diff --git a/c_adlib/AD_Facade.cs b/c_adlib/AD_Facade.cs index b5786bfd..d221db93 100644 --- a/c_adlib/AD_Facade.cs +++ b/c_adlib/AD_Facade.cs @@ -27,7 +27,8 @@ namespace Novell.CASA.DataEngines { // Always Aggregate miCASA. micasaengine = new miCASAEngine(); - + kwEngine = new KWalletEngine(); + gkEngine = new GKEngine(); /* // Reading Policy to see what else needs to be Aggregated. @@ -156,9 +157,12 @@ namespace Novell.CASA.DataEngines public int SetSecret(XmlNode secret, int StoreID) { - //TBD: Check for Store ID and call the right DataEngine. if (StoreID == ConstStrings.CASA_STORE_MICASA) return micasaengine.SetSecret(secret); + if (StoreID == ConstStrings.CASA_STORE_KWALLET) + return kwEngine.SetSecret(secret); + if (StoreID == ConstStrings.CASA_STORE_GK) + return gkEngine.SetSecret(secret); else { #if LINUX @@ -170,6 +174,7 @@ namespace Novell.CASA.DataEngines } + public int GetSecret(XmlNode secret, int StoreID) { @@ -191,9 +196,13 @@ namespace Novell.CASA.DataEngines public int Remove(XmlNode secret, int StoreID) { - //TBD: Check for Store ID and call the right DataEngine. - return micasaengine.Remove(secret); - + if (StoreID == ConstStrings.CASA_STORE_MICASA) + return micasaengine.Remove(secret); + if (StoreID == ConstStrings.CASA_STORE_KWALLET) + return kwEngine.Remove(secret); + if (StoreID == ConstStrings.CASA_STORE_GK) + return gkEngine.Remove(secret); + return -1; } public int AggregateStore(XmlDocument outDoc, int StoreID) diff --git a/c_adlib/GKEngine.cs b/c_adlib/GKEngine.cs index f4772584..1e9e92b0 100644 --- a/c_adlib/GKEngine.cs +++ b/c_adlib/GKEngine.cs @@ -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 } } diff --git a/c_adlib/KWalletEngine.cs b/c_adlib/KWalletEngine.cs index 3bf750e6..e8aa7138 100644 --- a/c_adlib/KWalletEngine.cs +++ b/c_adlib/KWalletEngine.cs @@ -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 } } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/c_adlib/ad_gk/GnomeKeyring.cs b/c_adlib/ad_gk/GnomeKeyring.cs index db14705e..4e4efdfc 100644 --- a/c_adlib/ad_gk/GnomeKeyring.cs +++ b/c_adlib/ad_gk/GnomeKeyring.cs @@ -1,10 +1,12 @@ using System; using System.Runtime.InteropServices; +using System.Collections.Specialized; using System.Collections; using System.Threading; using Gtk; using GLib; + namespace Novell.CASA.DataEngines.GK { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] @@ -54,6 +56,7 @@ namespace Novell.CASA.DataEngines.GK public IntPtr value; } + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public class Attribute { public uint type; @@ -76,6 +79,15 @@ namespace Novell.CASA.DataEngines.GK public static extern int GetAttributeList(string keyring,int itemId, out IntPtr attrList); [DllImport("libad_gk.so")] public static extern int FreeAttributeList(IntPtr attrList); + [DllImport("libad_gk.so")] + public static extern int SetPassword (string keyring, int itemid, string password); + [DllImport("libad_gk.so")] + public static extern int RemoveItem (string keyring, int itemid); + [DllImport("libad_gk.so")] + public static extern int SetItemAttributes (string keyring, int itemid, IntPtr[] attrs, int length); + [DllImport("libad_gk.so")] + public static extern int CreateItem(string keyringName, int itemType, string displayName, string password, IntPtr[] arrptr, int attrCount); + public static KeyringInfo GKGetKeyringInfo(string name) { @@ -282,5 +294,81 @@ namespace Novell.CASA.DataEngines.GK Console.WriteLine(e.ToString()); } } + + + public static int SetAttributes(String keyringName, int itemId, NameValueCollection nvc) + { + IntPtr[] arrptr = new IntPtr[nvc.Count]; + for(int i=0; i < nvc.Count; i++) + { + string key = nvc.GetKey(i); + string value = nvc.Get(key); + Attribute attr = new Attribute(); + attr.type=0; + attr.key=key; + attr.value=value; + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(attr)); + Marshal.StructureToPtr(attr,ptr,false); + arrptr[i] = ptr; + } + int ret = SetItemAttributes( keyringName,itemId,arrptr,nvc.Count); + FreeIntPtrArray(arrptr); + return ret; + } + + + public static int RemoveSecret(string keyringname, int itemid) + { + + return(RemoveItem(keyringname,itemid)); + } + + public static int CreateSecret(String keyringName, string strItemType, string displayName, string password, NameValueCollection nvc) + { + Console.WriteLine("In CreateSecret "); + int itemType = 3; //No Type + IntPtr[] arrptr = new IntPtr[nvc.Count]; + if(strItemType.CompareTo("Generic Secret") == 0 ) + { + itemType = 0; + } + else + if(strItemType.CompareTo("Network Password") == 0 ) + { + itemType = 1; + } + else + if(strItemType.CompareTo("Note") == 0 ) + { + itemType = 2; + } + Console.WriteLine("In CreateSecret ItemType = "+itemType); + for(int i=0; i < nvc.Count; i++) + { + string key = nvc.GetKey(i); + Console.WriteLine("In CreateSecret Key "+i + " = " + key); + string value = nvc.Get(key); + Console.WriteLine("In CreateSecret Value "+i + " = " + value); + Attribute attr = new Attribute(); + attr.type=0; + attr.key=key; + attr.value=value; + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(attr)); + Marshal.StructureToPtr(attr,ptr,false); + arrptr[i] = ptr; + } + Console.WriteLine("Calling Create item "); + int ret = CreateItem(keyringName, itemType, displayName, password, arrptr, nvc.Count); + FreeIntPtrArray(arrptr); + return ret; + } + + public static void FreeIntPtrArray(IntPtr[] arrptr) + { + for(int i=0; i < arrptr.Length; i++) + { + Marshal.FreeHGlobal(arrptr[i]); + } + } } } diff --git a/c_adlib/ad_gk/native/ad_gk.c b/c_adlib/ad_gk/native/ad_gk.c index 9aa7d59a..ea734c29 100644 --- a/c_adlib/ad_gk/native/ad_gk.c +++ b/c_adlib/ad_gk/native/ad_gk.c @@ -1,5 +1,7 @@ #include "ad_gk.h" +GMainLoop *loop = NULL; + void ListKeyringsCb (GnomeKeyringResult result, GList *keyrings, gpointer data) @@ -235,3 +237,151 @@ int FreeAttributeList(GList *attrList) g_list_free(attrList); return SUCCESS; } + +static void +OperationCompletedCb (GnomeKeyringResult result, + gpointer data) +{ + OperationCompleted *cbData = (OperationCompleted *)data; + g_print ("ad_gk.c : Operation %s Completed %d\n", cbData->OperationName, result); + cbData->result = result; + g_main_loop_quit (loop); +} + + +static void +CreateItemCb (GnomeKeyringResult result, + guint32 id, + gpointer data) +{ + OperationCompleted *cbData = (OperationCompleted *)data; + g_print ("ad_gk.c : CreateItemCb : created item: res: %d id: %d\n", result, id); + if (result != GNOME_KEYRING_RESULT_OK) + { + g_print ("ad_gk.c : CreateItemCb : Unable to create item : %d\n", result); + } + cbData->result = result; + g_main_loop_quit (loop); +} + + + + + +int +SetItemAttributes (char *keyring, guint32 itemid, Attribute **attrs, int length) +{ + GnomeKeyringAttributeList *attributes; + GnomeKeyringAttribute attribute; + OperationCompleted cbData; + int i; + printf("ad_gk.c : In SetItemAttributes\n"); + printf("ad_gk.c : Keyring %s, itemid %d\n",keyring,itemid); + cbData.OperationName = "Set Item Attributes"; + loop = g_main_loop_new (NULL, FALSE); + + attributes = gnome_keyring_attribute_list_new (); + for (i=0; i< length; i++) + { + printf("ad_gk.c : In key %s \n", attrs[i]->key); + attribute.name = g_strdup (attrs[i]->key); + attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING; + attribute.value.string = g_strdup (attrs[i]->value); + g_array_append_val (attributes, attribute); + } + gnome_keyring_item_set_attributes (keyring, itemid, attributes, + OperationCompletedCb, &cbData, NULL); + gnome_keyring_attribute_list_free (attributes); + g_main_loop_run (loop); + return cbData.result; +} + + +int +RemoveItem (char *keyring, guint32 itemid) +{ + OperationCompleted cbData; + loop = g_main_loop_new (NULL, FALSE); + cbData.OperationName = "Remove Item"; + gnome_keyring_item_delete (keyring, itemid, + OperationCompletedCb, &cbData, NULL); + g_main_loop_run (loop); + return cbData.result; +} + + +int +SetPassword (char *keyring, guint32 itemid, char *secret) +{ + GnomeKeyringItemInfo *info; + OperationCompleted cbData; + loop = g_main_loop_new (NULL, FALSE); + + cbData.OperationName = "Set Item Secret"; + info = gnome_keyring_item_info_new (); + gnome_keyring_item_info_set_secret (info, secret); + gnome_keyring_item_set_info (keyring, itemid, info, + OperationCompletedCb, &cbData, NULL); + gnome_keyring_item_info_free (info); + g_main_loop_run (loop); + return cbData.result; +} + + +int +CreateItem(char *keyring, int32_t itemType, char *display_name, char *secret, Attribute **attrs, int attrcnt) +{ + int ret; + ret = CreateItemInKeyring(keyring,itemType,display_name,secret,attrs,attrcnt); + if (ret == 4) + { + ret = CreateKeyring(keyring); + if (ret != 0) + { + return GNOME_KEYRING_RESULT_CANNOT_CREATE_KEYRING ; + } + ret = CreateItemInKeyring(keyring,itemType,display_name ,secret,attrs,attrcnt); + return ret; + } + return ret; +} + +static int +CreateItemInKeyring(char *keyring, int32_t itemType, char *display_name, char *secret, Attribute **attrs, int attrcnt) +{ + GnomeKeyringAttributeList *attributes; + GnomeKeyringAttribute attribute; + OperationCompleted cbData; + int i; + printf("ad:gk.c :In CreateItemInKeyring\n"); + printf("ad.gk.c : CreateItemInKeyring : Keyring %s, itemType %d displayname %s, secret %s \n",keyring,itemType, display_name,secret); + cbData.OperationName = "Create Item"; + loop = g_main_loop_new (NULL, FALSE); + + attributes = gnome_keyring_attribute_list_new (); + for (i=0; i< attrcnt; i++) + { + printf("as.gk.c : CreateItemInKeyring : In key %s \n", attrs[i]->key); + attribute.name = g_strdup (attrs[i]->key); + attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING; + attribute.value.string = g_strdup (attrs[i]->value); + g_array_append_val (attributes, attribute); + } + + gnome_keyring_item_create(keyring,itemType,display_name,attributes,secret,FALSE,CreateItemCb,&cbData,NULL); + gnome_keyring_attribute_list_free (attributes); + g_main_loop_run (loop); + return cbData.result; +} + +static int +CreateKeyring(char *keyring) +{ + + OperationCompleted cbData; + cbData.OperationName = "Create Keyring"; + loop = g_main_loop_new (NULL, FALSE); + gnome_keyring_create(keyring,NULL,OperationCompletedCb,&cbData,NULL); + g_main_loop_run(loop); + return cbData.result; +} diff --git a/c_adlib/ad_gk/native/ad_gk.h b/c_adlib/ad_gk/native/ad_gk.h index 0c33af6e..9c3b5fe1 100644 --- a/c_adlib/ad_gk/native/ad_gk.h +++ b/c_adlib/ad_gk/native/ad_gk.h @@ -11,6 +11,13 @@ #define KEY_SIZE 128 #define VAL_SIZE 128 + +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 + }; + typedef struct _KeyringInfo { int32_t lockOnIdle; @@ -44,6 +51,19 @@ int GetItemInfo(char *keyring, int itemId, ItemInfo *info); int GetAttributeList(char *keyring, int itemId, GList **); int FreeAttributeList(GList *attrList); +int +SetItemAttributes (char *keyring, guint32 item_id, Attribute **attrs, int length); +int +CreateItem(char *keyring, int32_t itemType, char *display_name, char *secret, Attribute **attrs, int attrcnt); +int +RemoveItem (char *keyring, guint32 itemid); +int +SetPassword (char *keyring, guint32 itemid, char *secret); + + + + + typedef struct _GetKeyringsCbData { GList **keyringList; @@ -73,4 +93,13 @@ typedef struct _GetAttributeListCbData GList **attrList; GMainLoop *loop; }GetAttributeListCbData; +typedef struct _OperationCompleted +{ + char *OperationName; + GnomeKeyringResult result; + +}OperationCompleted; + + + #endif diff --git a/c_adlib/ad_kw/KWalletNative.cs b/c_adlib/ad_kw/KWalletNative.cs index 58dbf68b..8b7df2d6 100644 --- a/c_adlib/ad_kw/KWalletNative.cs +++ b/c_adlib/ad_kw/KWalletNative.cs @@ -30,7 +30,13 @@ public class kwallet [DllImport(CPP_LIB)] public static extern void FreeList(); + [DllImport(CPP_LIB)] + public static extern int SetEntry(string walletName, string folderName, int entryType, string keyName, string value, int valueLen); + [DllImport(CPP_LIB)] + public static extern int SetMapEntry(string walletName, string folderName, string keyName, String[,] value, int eleCount); + [DllImport(CPP_LIB)] + public static extern int RemoveEntry(string walletName, string folderName, string keyName); /* public static int Try(EnumSecretList enumSecretList) @@ -53,6 +59,88 @@ public class kwallet FreeList(); return 0; } + + + public static int SetSecret(string walletName, string folderName, int entryType, string keyName, string value, int valueLen ) + { + return(SetEntry(walletName, folderName, entryType, keyName, value, valueLen )); + } + public static int SetSecret(string walletName, string folderName, string keyName, NameValueCollection nvc) + { + + //IntPtr[] mapele = new IntPtr[nvc.Count * 2]; + String[,] mapele = new String[nvc.Count,2 ]; + String str=" "; + + int j=0; + for (int i=0; i< nvc.Count; i++) + { + mapele[i,0] = nvc.GetKey(i); + mapele[i,1] = nvc.Get(nvc.GetKey(i)); + + + } + + + /* + for (int i=0; i< nvc.Count; i++) + { + str = String.Concat(str,nvc.GetKey(i)); + str = String.Concat(str, "\0"); + str = String.Concat(str,nvc.Get(nvc.GetKey(i))); + str = String.Concat(str, "\0"); + + } + */ + + char[] strarr = str.ToCharArray(); + + /*for (int i=0; i< nvc.Count; i++) + { + + string strKey = nvc.GetKey(i); + Console.WriteLine("Key is " + strKey); + //IntPtr ptr1 = Marshal.AllocHGlobal(strKey.Length); + //Marshal.StructureToPtr(strKey,ptr1,false); + + IntPtr ptr1 = Marshal.StringToHGlobalAnsi(strKey); + string strValue = nvc.Get(strKey); + //IntPtr ptr2 = Marshal.AllocHGlobal(strValue.Length); + //Marshal.StructureToPtr(strValue,ptr2,false); + IntPtr ptr2 = Marshal.StringToHGlobalAnsi(strValue); + Console.WriteLine("Value is " + strValue); + //IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(kv)); + // Marshal.StructureToPtr(kv,ptr,false); + mapele[j++] = ptr1; + Console.WriteLine("Pointer is " +ptr1); + mapele[j++] = ptr2; + Console.WriteLine("Pointer is " +ptr2); + } + */ + //IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(mapele)); + //Marshal.StructureToPtr(mapele,ptr,false); + + /*string[] str = {"a","bb","ccc"}; + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(str)); + Marshal.StructureToPtr(str,ptr,false); + */ + /*Console.WriteLine("Setting Map Entry Keys name is " + keyName); */ + return(SetMapEntry(walletName, folderName, keyName, mapele, nvc.Count)); + } + public static int DeleteSecret(string walletName, string folderName, string keyName) + { + return(RemoveEntry(walletName, folderName, keyName)); + } + + + + + + + + + + //TBD: All this for future. /* diff --git a/c_adlib/ad_kw/native/kwallets_rw.cpp b/c_adlib/ad_kw/native/kwallets_rw.cpp index fbcddd4d..f2078194 100644 --- a/c_adlib/ad_kw/native/kwallets_rw.cpp +++ b/c_adlib/ad_kw/native/kwallets_rw.cpp @@ -46,7 +46,8 @@ using namespace KWallet; { // Read the secret from the entry - QByteArray value; + QByteArray value; + printf("kwallet: Read Key entered\n"); if (wallet->readEntry(key, value)==0) { @@ -61,9 +62,13 @@ using namespace KWallet; if (wallet->entryType(key) == 1 ) { - // Convert the ByteArray to QString + //Commented by Austin + /* // Convert the ByteArray to QString QString passwd; + printf("In read Entry Convert Final before success\n"); convert >> passwd; + printf("In read Entry Convert Final success\n"); + */ } else if (wallet->entryType(key) == 3) { @@ -137,14 +142,15 @@ using namespace KWallet; extern "C" { - static struct EnumSecretList *tempEnumSecrets = NULL; + static struct EnumSecretList *tempEnumSecrets = NULL; //void MyTest(struct EnumSecretList *enumWalletSecrets) void Aggregate(struct EnumSecretList *enumWalletSecrets) { + printf("inside natiove agg"); int retVal = 0; - tempEnumSecrets = NULL; + tempEnumSecrets = NULL; retVal = win->ReadAllWalletSecrets(&tempEnumSecrets); struct EnumSecretList *iter = tempEnumSecrets; @@ -203,6 +209,28 @@ extern "C" */ } + + + int RemoveEntry(char *name, char *foldername, char *keyname) + { + DCOPDemoWidget kw; + return(kw.RemoveEntry(name,foldername,keyname)); + } + int SetEntry(char *name, char *foldername, int entryType, char *keyname, char *value, int valueLen ) + { + DCOPDemoWidget kw; + printf("kwallet : In Set Entry\n"); + return(kw.SetEntry(name,foldername,entryType,keyname,value,valueLen)); + } + int SetMapEntry(char *name, char *foldername, char *keyname, char **value, int eleCount ) + { + DCOPDemoWidget kw; + printf("kwallet: SetMapEntry\n"); + return(kw.SetMap(name,foldername,keyname,value,eleCount)); + } + + + @@ -223,11 +251,12 @@ extern "C" free(head); head = temp; } - - tempEnumSecrets = NULL; - + tempEnumSecrets = NULL; } + + + } @@ -334,10 +363,12 @@ extern "C" - + //Commented by Austin QDataStream convert(*secretVal, IO_ReadOnly); QString passwd; convert >> passwd; + //QString passwd(*secretVal); + printf("kwallet : ReadAll key %s value %s \n",key.latin1(), passwd.latin1()); tempWalletSecrets->secretVal = (char*)malloc(512); if (tempWalletSecrets->secretVal == NULL) @@ -385,6 +416,143 @@ extern "C" } +int DCOPDemoWidget::SetEntry(char *name, char *foldername, int entryType, char *keyname, char *value, int valueLen ) + { + + QString qWalletName(name); + QString qKey(keyname); + QString qFolderName(foldername); + + QString &refQKey = qKey; + + // Open the wallet + Wallet *wallet = NULL; + wallet = Wallet::openWallet(qWalletName,0,Wallet::Synchronous); + if (wallet == NULL) + { + printf("Could not open the wallet %s \n", qWalletName.latin1()); + return KWALLET_RESULT_CANNOT_OPEN_WALLET; + } + if (wallet->setFolder(qFolderName) == false) + { + printf("Could not open the folder %s \n", qFolderName.latin1()); + return KWALLET_RESULT_CANNOT_OPEN_FOLDER; + } + QString unicodeValue = tr(value); + // Read the secret from the entry + //QByteArray secretVal(valueLen *2 ); + + QByteArray secretVal; + QDataStream ds(secretVal, IO_WriteOnly); + ds << unicodeValue; + + /*for(int i=0; i< valueLen; i++) + { + secretVal[i] = 0; + secretVal[i+1] = value[i]; + } + */ + //secretVal[valueLen] = '\0'; + + // secretVal.fill('a'); + //secretVal.setRawData(value,valueLen); + QByteArray &refSecretVal = secretVal; + + //QDataStream convert( secretVal, IO_WriteOnly ); + //convert.readBytes( value, (uint)valueLen); +// Wallet::EntryType MyEntryType = 3; + if (wallet->entryType(qKey) != 3) + { + printf("kwallet : SetEntry : Before setting Entry key %s value = %s EntryType =%d \n" , qKey.latin1() , value, entryType ); + if (wallet->writeEntry(refQKey, refSecretVal , (Wallet::EntryType) entryType ) != 0) + { + return KWALLET_RESULT_CANNOT_WRITE_ENTRY; + } + } + // Free memory + wallet->sync(); + return KWALLET_RESULT_OK; + } +int DCOPDemoWidget::SetMap(char *name, char *foldername, char *keyname, char **value, int numOfKeys ) + { + QString qWalletName(name); + QString qKey(keyname); + QString qFolderName(foldername); + printf("kwallet : SetMap : Wallet %s Folder %s Key =%s\n", name, foldername, keyname); + + + // Open the wallet + Wallet *wallet = NULL; + wallet = Wallet::openWallet(qWalletName,0,Wallet::Synchronous); + if (wallet == NULL) + { + printf("Could not open the wallet\n"); + return KWALLET_RESULT_CANNOT_OPEN_WALLET; + } + + + if (wallet->hasFolder(qFolderName) == false) + { + if(wallet->createFolder(qFolderName) == false) + { + return KWALLET_RESULT_CANNOT_CREATE_FOLDER; + } + } + + if (wallet->setFolder(qFolderName) == false) + { + return KWALLET_RESULT_CANNOT_OPEN_FOLDER; + } + + QMap mapSecret; + for (int i=0; i < numOfKeys * 2; i+=2) + { + QString mapelekey((char *)value[i]); + QString mapelevalue((char *)value[i+1]); + mapSecret.insert(mapelekey,mapelevalue); + } + if (wallet->writeMap(qKey,mapSecret) != 0 ) + { + return KWALLET_RESULT_CANNOT_WRITE_ENTRY; + } + wallet->sync(); + return KWALLET_RESULT_OK; +} + + + +int DCOPDemoWidget::RemoveEntry(char *name, char *foldername, char *keyname ) +{ + + QString qWalletName(name); + QString qKey(keyname); + QString qFolderName(foldername); + printf("In DCOPDemoWidget:RemoveEntry\n"); + printf("In DCOPDemoWidget:False %d\n",false); + printf("In DCOPDemoWidget:False %d\n",FALSE); + + // Open the wallet + Wallet *wallet = NULL; + wallet = Wallet::openWallet(qWalletName,0,Wallet::Synchronous); + if (wallet == NULL) + { + printf("Could not open the wallet %s \n", qWalletName.latin1()); + return KWALLET_RESULT_CANNOT_OPEN_WALLET; + } + if (wallet->setFolder(qFolderName) == false) + { + printf("Could not set the folder %s \n", qFolderName.latin1()); + return KWALLET_RESULT_CANNOT_OPEN_FOLDER; + } + if (wallet->removeEntry(qKey) != 0) + { + printf("Could not remove Entry %s \n", qKey.latin1()); + return KWALLET_RESULT_CANNOT_REMOVE_ENTRY; + } + wallet->sync(); + printf("In DCOPDemoWidget:RemoveEntry Exit\n"); + return KWALLET_RESULT_OK; +} diff --git a/c_adlib/ad_kw/native/kwallets_rw.h b/c_adlib/ad_kw/native/kwallets_rw.h index 54404b9f..8ed9cf90 100644 --- a/c_adlib/ad_kw/native/kwallets_rw.h +++ b/c_adlib/ad_kw/native/kwallets_rw.h @@ -3,6 +3,22 @@ #define DCOPIFACEDEMO_H #include + +enum KWalletResult +{ 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 +}; + + + /** * Adding DCOP interface to an app. @@ -43,6 +59,10 @@ public slots: int ReadAllWalletSecrets(struct EnumSecretList **); // int ReadWalletSecret(QString, QString, QString, QByteArray*); // int WriteWalletSecret(QString, QString, QString, QByteArray , int); + int SetEntry(char *, char *, int , char *, char *, int); + int SetMap(char *, char *, char *, char** , int ); + int RemoveEntry(char *, char *, char *); + private: