Initial checkin of CASAUtil for cypress.

This commit is contained in:
Jim Norman 2007-04-16 18:18:42 +00:00
parent 0c5a911d89
commit 675358cc74
3 changed files with 371 additions and 0 deletions

285
CASA/cli/CASAUtil.cs Normal file
View File

@ -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<args.Length; i++)
{
string arg = args[i];
if (arg.StartsWith("--"))
{
arg = arg.Substring(1);
}
if (arg.ToLower().StartsWith("-h"))
{
ShowUsage();
return;
}
if (arg.ToLower().StartsWith("-l"))
{
ListCredentials();
return;
}
else if (arg.ToLower().StartsWith("-g"))
{
iMode = MODE_GET_CREDENTIAL;
}
else if (arg.ToLower().StartsWith("-s"))
{
iMode = MODE_SET_CREDENTIAL;
}
else if (arg.ToLower().StartsWith("-d"))
{
iMode = MODE_DELETE_CREDENTIAL;
}
else if (arg.ToLower().StartsWith("-n"))
{
// get next arg as username
if (i + 1 < args.Length)
{
sCredname = args[++i];
}
}
else if (arg.ToLower().StartsWith("-k"))
{
// get next arg as username
if (i + 1 < args.Length)
{
sKeyname = args[++i];
}
}
else if (arg.ToLower().StartsWith("-v"))
{
// get next arg as username
if (i + 1 < args.Length)
{
sValue = args[++i];
}
}
else if (arg.ToLower().StartsWith("-u"))
{
// get next arg as username
if (i + 1 < args.Length)
{
sUsername = args[++i];
}
}
else if (arg.ToLower().StartsWith("-p"))
{
// get next arg as password
if (i + 1 < args.Length)
{
sPassword = args[++i];
}
}
else
{
Console.WriteLine("Invalid arg: {0}", arg);
return;
}
}
// if we get here, check for operation
if (iMode > 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();
}
}
}

53
CASA/cli/CASAUtil.csproj Normal file
View File

@ -0,0 +1,53 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B4182B62-2F56-4835-9479-403FFA134E5D}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CASAUtil</RootNamespace>
<AssemblyName>CASAUtil</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="CASAUtil.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\sharp\NSSCSWrapper\Novell.CASA.miCASAWrapper.csproj">
<Project>{E21DD887-22F4-4935-9851-409715F663B0}</Project>
<Name>Novell.CASA.miCASAWrapper</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -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")]