diff --git a/CASA/cli/CASAUtil.cs b/CASA/cli/CASAUtil.cs new file mode 100644 index 00000000..ef474906 --- /dev/null +++ b/CASA/cli/CASAUtil.cs @@ -0,0 +1,285 @@ +using System; +using System.Collections.Specialized; +using System.Text; + +using Novell.CASA; + +namespace Novell.CASA +{ + class CASAUtil + { + + private static string m_sAppname = "CASAUtil"; + private static string m_sKeyChainID = SecretStore.SERVER_KEY_CHAIN; + private const int MODE_GET_CREDENTIAL = 1; + private const int MODE_SET_CREDENTIAL = 2; + private const int MODE_DELETE_CREDENTIAL = 3; + + private static SecretStore m_ss = SecretStore.GetInstance(); + + + static void Main(string[] args) + { + System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess(); + m_sAppname = proc.ProcessName; + + try + { + if (args.Length > 0) + ParseArgs(args); + else + ShowBasicHelp(); + } + catch (Novell.CASA.miCasaException mce) + { + Console.WriteLine("Error occured: {0}", mce.GetMessage()); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + } + } + + static void ParseArgs(string[] args) + { + string sUsername = null; + string sPassword = null; + string sCredname = null; + string sKeyname = null; + string sValue = null; + int iMode = 0; + + + for (int i = 0; i 0) + { + // did we get a credential name? + if (sCredname == null) + { + Console.WriteLine("No credential name entered"); + return; + } + + switch (iMode) + { + case MODE_GET_CREDENTIAL: + { + // get named credential and display it. + Console.WriteLine("Getting {0}", sCredname); + DisplaySecret(m_ss.GetSecret(m_sKeyChainID, 0, sCredname, Secret.SS_CREDSET, "")); + break; + } + case MODE_SET_CREDENTIAL: + { + Console.WriteLine("Setting {0}", sCredname); + Secret secret = m_ss.GetSecret(m_sKeyChainID, 0, sCredname, Secret.SS_CREDSET, ""); + + if (sKeyname != null) + { + if (sValue != null) + { + secret.SetKeyValuePair(sKeyname, sValue); + } + else + { + Console.WriteLine("No value entered for named Key"); + Console.WriteLine("Example: {0} -s -n [credname] -k [keyname] -v [value]", m_sAppname); + return; + } + } + + if (sPassword != null) + { + secret.SetKeyValuePair("Password", sPassword); + } + + if (sUsername != null) + { + secret.SetKeyValuePair("CN", sUsername); + } + + m_ss.SetSecret(0, m_sKeyChainID, secret, Secret.SS_CREDSET); + + break; + } + case MODE_DELETE_CREDENTIAL: + { + Console.WriteLine("Deleting {0}", sCredname); + m_ss.RemoveSecret(0, m_sKeyChainID, "", sCredname, Secret.SS_CREDSET); + break; + } + + } + + + } + + } + + private static void ShowBasicHelp() + { + Console.WriteLine("Try '{0} --help' for more infomation", m_sAppname); + } + + private static void ShowUsage() + { + ShowDescription(); + + Console.WriteLine(" Usage: {0} [OPTIONS]", m_sAppname); + Console.WriteLine(" Options"); + Console.WriteLine(" -l, --list List all credentials used by services"); + Console.WriteLine(" -h, --help Displays this help screen"); + Console.WriteLine(); + Console.WriteLine(" -s, --set Sets the named credential"); + Console.WriteLine(" -g, --get Gets and displays the named credential"); + Console.WriteLine(" -d, --del Deletes the named credential"); + Console.WriteLine(); + Console.WriteLine(" -n, --name Specify the credential name"); + Console.WriteLine(" -k, --key Specify the key name to set"); + Console.WriteLine(" -v, --value Specify the value for the key to set"); + Console.WriteLine(); + + Console.WriteLine(" Examples"); + Console.WriteLine(" {0} --get -n MyCredential", m_sAppname); + Console.WriteLine(" {0} --set -n MyCredential -k CN -v admin", m_sAppname); + Console.WriteLine(" {0} --set -n MyCredential -k Password -v password", m_sAppname); + Console.WriteLine(" {0} --del -n MyCredential", m_sAppname); + + } + + private static void ShowDescription() + { + Console.WriteLine(); + Console.WriteLine(" Description:"); + Console.WriteLine(" This program is a simply utility to display, set, and delete"); + Console.WriteLine(" credentials used by services on this computer. Because credentials"); + Console.WriteLine(" are scoped by the UID of the running process, you must run"); + Console.WriteLine(" this utility as the same UID as the service being configured."); + Console.WriteLine(); + } + + private static void ListCredentials() + { + + StringCollection sc = m_ss.EnumerateSecretIDs(m_sKeyChainID); + + StringEnumerator se = sc.GetEnumerator(); + while (se.MoveNext()) + { + string sSecretID = (string)se.Current; + Secret secret = m_ss.GetSecret(m_sKeyChainID, 0, sSecretID.Substring(11), Secret.SS_CREDSET, ""); + DisplaySecret(secret); + } + } + + private static void DisplaySecret(Secret secret) + { + + Console.WriteLine(" Name: {0}", secret.GetID()); + NameValueCollection nvc = secret.GetKeyValueCollection(); + + for (int i = 0; i < nvc.Count; i++) + { + string sKeyname = nvc.Keys[i]; + if (sKeyname.ToLower().StartsWith("p")) + { + Console.WriteLine(" key: {0} (*******)", nvc.Keys[i]); + } + else + { + Console.WriteLine(" key: {0} ({1})", nvc.Keys[i], nvc.Get(i)); + } + } + + Console.WriteLine(); + + } + } +} diff --git a/CASA/cli/CASAUtil.csproj b/CASA/cli/CASAUtil.csproj new file mode 100644 index 00000000..5c809e1f --- /dev/null +++ b/CASA/cli/CASAUtil.csproj @@ -0,0 +1,53 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {B4182B62-2F56-4835-9479-403FFA134E5D} + Exe + Properties + CASAUtil + CASAUtil + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + {E21DD887-22F4-4935-9851-409715F663B0} + Novell.CASA.miCASAWrapper + + + + + + + + \ No newline at end of file diff --git a/CASA/cli/Properties/AssemblyInfo.cs b/CASA/cli/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..72de46f5 --- /dev/null +++ b/CASA/cli/Properties/AssemblyInfo.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("CASAManagerCLI")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Novell, Inc")] +[assembly: AssemblyProduct("CASAManagerCLI")] +[assembly: AssemblyCopyright("Copyright © Novell, Inc 2007")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("dc4be258-86ca-4284-bbe1-23b64a01e197")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]