Fix for linux build and Server Keychain. Work-in-progress

This commit is contained in:
Jim Norman 2006-12-04 12:00:27 +00:00
parent 3216d2b739
commit b0fad0f85f
2 changed files with 179 additions and 152 deletions

View File

@ -27,7 +27,7 @@ using System.IO;
using System.Xml; using System.Xml;
using System.Xml.Serialization; using System.Xml.Serialization;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Diagnostics; using System.Diagnostics;
using sscs.cache; using sscs.cache;
@ -64,63 +64,81 @@ namespace sscs.cache
string m_persistenceDirectory = null; string m_persistenceDirectory = null;
private static string POLICY_DIRECTORY = "/home/.casa"; private static string POLICY_DIRECTORY = "/home/.casa";
private MPFileWatcher mpWatcher = null; private MPFileWatcher mpWatcher = null;
private static SecretStore casaStore; private static SecretStore casaStore;
static SecretStore() static SecretStore()
{ {
if (casaStore == null) if (casaStore == null)
{ {
User casaUser; User casaUser;
#if LINUX #if LINUX
Directory.CreateDirectory("/home/.casa/" + constants.ConstStrings.MICASA_SERVICE_NAME); casaUser = new UnixUser(new UnixUserIdentifier(GetCasaServiceUID()), "/home/.casa/" + constants.ConstStrings.MICASA_SERVICE_NAME);
casaUser = new UnixUser(new UnixUserIdentifier(GetCasaServiceUID()), "/home/.casa/" + constants.ConstStrings.MICASA_SERVICE_NAME)); #else
#else // create a data directory for server secrets
// create a data directory for server secrets Process proc = Process.GetCurrentProcess();
Process proc = Process.GetCurrentProcess(); string exePath = proc.MainModule.FileName;
string exePath = proc.MainModule.FileName; exePath = exePath.Substring(0, exePath.LastIndexOf("\\"));
exePath = exePath.Substring(0, exePath.LastIndexOf("\\")); Directory.CreateDirectory(exePath + "\\data");
Directory.CreateDirectory(exePath + "\\data");
// create a casa User
// create a casa User casaUser = new WinUser(new WinUserIdentifier(998, 0), exePath + "\\data");
casaUser = new WinUser(new WinUserIdentifier(998, 0), exePath + "\\data"); #endif
#endif casaUser.SetUserName(constants.ConstStrings.MICASA_SERVICE_NAME);
casaUser.SetUserName(constants.ConstStrings.MICASA_SERVICE_NAME); casaStore = casaUser.GetSecretStore();
casaStore = casaUser.GetSecretStore(); casaStore.refCount++;
casaStore.refCount++;
casaStore.AddKeyChain(new KeyChain(constants.ConstStrings.SSCS_SERVER_KEY_CHAIN_ID + "\0"));
casaStore.AddKeyChain(new KeyChain(constants.ConstStrings.SSCS_SERVER_KEY_CHAIN_ID + "\0")); casaStore.StartPersistenceOfServerSecretsBySystemKey();
casaStore.StartPersistenceOfServerSecretsBySystemKey(); }
} }
}
#if LINUX
#if LINUX static private int GetCasaServiceUID()
static private int GetCasaServiceUID() {
{ Mono.Unix.UnixUserInfo uui;
Mono.Unix.UnixUserInfo uui;
try try
{ {
uui = new Mono.Unix.UnixUserInfo(constants.ConstStrings.MICASA_SERVICE_NAME); uui = new Mono.Unix.UnixUserInfo(constants.ConstStrings.MICASA_SERVICE_NAME);
} }
catch catch
{ {
Process proc = new Process(); Process proc = new Process();
ProcessStartInfo psi = new ProcessStartInfo("useradd"); ProcessStartInfo psi = new ProcessStartInfo("useradd");
psi.Arguments = constants.ConstStrings.MICASA_SERVICE_NAME; psi.Arguments = constants.ConstStrings.MICASA_SERVICE_NAME;
psi.UseShellExecute = false; psi.UseShellExecute = false;
psi.RedirectStandardOutput = true; psi.RedirectStandardOutput = true;
proc.StartInfo = psi; proc.StartInfo = psi;
proc.Start(); proc.Start();
proc.WaitForExit(); proc.WaitForExit();
uui = new Mono.Unix.UnixUserInfo(constants.ConstStrings.MICASA_SERVICE_NAME); uui = new Mono.Unix.UnixUserInfo(constants.ConstStrings.MICASA_SERVICE_NAME);
} }
return uui.UserId;
if (uui != null)
{
// create directory for casa
Mono.Unix.Native.FilePermissions permissions = Mono.Unix.Native.Syscall.umask(
Mono.Unix.Native.FilePermissions.S_IWGRP |
Mono.Unix.Native.FilePermissions.S_IWOTH);
Directory.CreateDirectory("/home/.casa/" + constants.ConstStrings.MICASA_SERVICE_NAME);
// set ownership
Mono.Unix.Native.Syscall.chown("/home/.casa/" + constants.ConstStrings.MICASA_SERVICE_NAME,
(uint)uui.UserId,
(uint)uui.GroupId);
Mono.Unix.Native.Syscall.umask(permissions);
}
return (int)uui.UserId;
} }
#endif #endif
private DateTime createTime; private DateTime createTime;
@ -766,30 +784,30 @@ namespace sscs.cache
{ {
keyChainList.Remove(id); keyChainList.Remove(id);
return true; return true;
} }
internal KeyChain GetKeyChainDefault(bool bCreateIfNotFound) internal KeyChain GetKeyChainDefault(bool bCreateIfNotFound)
{ {
KeyChain kc; KeyChain kc;
try try
{ {
kc = GetKeyChainDefault(); kc = GetKeyChainDefault();
} }
catch (Exception e) catch (Exception e)
{ {
if (bCreateIfNotFound) if (bCreateIfNotFound)
{ {
kc = new KeyChain("SSCS_SESSION_KEY_CHAIN_ID\0"); kc = new KeyChain("SSCS_SESSION_KEY_CHAIN_ID\0");
AddKeyChain(kc); AddKeyChain(kc);
} }
else else
{ {
throw e; throw e;
} }
} }
return kc; return kc;
} }
internal KeyChain GetKeyChainDefault() internal KeyChain GetKeyChainDefault()
@ -798,12 +816,17 @@ namespace sscs.cache
} }
internal KeyChain GetKeyChain(string id) internal KeyChain GetKeyChain(string id)
{
{
Console.WriteLine("Keychain {0}", id);
// if this is the server keychain, return the casaStore controlled one. // if this is the server keychain, return the casaStore controlled one.
if (id.StartsWith(constants.ConstStrings.SSCS_SERVER_KEY_CHAIN_ID)) if (id.StartsWith(constants.ConstStrings.SSCS_SERVER_KEY_CHAIN_ID))
{ {
KeyChain casakc = (KeyChain)casaStore.keyChainList[id]; KeyChain casakc = (KeyChain)casaStore.keyChainList[id];
casakc.AccessedTime = DateTime.Now; casakc.AccessedTime = DateTime.Now;
Console.WriteLine("Returned casakc");
return casakc; return casakc;
} }
@ -824,8 +847,8 @@ namespace sscs.cache
internal bool CheckIfKeyChainExists(string id) internal bool CheckIfKeyChainExists(string id)
{ {
// return true if this is the server keychain // return true if this is the server keychain
if (id.StartsWith(constants.ConstStrings.SSCS_SERVER_KEY_CHAIN_ID)) if (id.StartsWith(constants.ConstStrings.SSCS_SERVER_KEY_CHAIN_ID))
return true; return true;
if(keyChainList.ContainsKey(id)) if(keyChainList.ContainsKey(id))
@ -840,10 +863,10 @@ namespace sscs.cache
if (lss != null) if (lss != null)
lss.PersistStoreWithDelay(); lss.PersistStoreWithDelay();
if (slss != null) if (slss != null)
slss.PersistServerStoreWithDelay(); slss.PersistServerStoreWithDelay();
// persist casastore data // persist casastore data
if (casaStore.slss != null) if (casaStore.slss != null)
casaStore.slss.PersistServerStoreWithDelay(); casaStore.slss.PersistServerStoreWithDelay();
} }
@ -1103,7 +1126,7 @@ namespace sscs.cache
internal byte[] GetSecrets(string sEncryptionString, ref byte[] baIV) internal byte[] GetSecrets(string sEncryptionString, ref byte[] baIV)
{ {
if (lss != null) if (lss != null)
{ {
MemoryStream ms = LocalStorage.GetSecretsAsXMLStream(this, ConstStrings.SSCS_SESSION_KEY_CHAIN_ID); MemoryStream ms = LocalStorage.GetSecretsAsXMLStream(this, ConstStrings.SSCS_SESSION_KEY_CHAIN_ID);
byte[] baSecrets = ms.ToArray(); byte[] baSecrets = ms.ToArray();
@ -1149,8 +1172,10 @@ namespace sscs.cache
#if LINUX #if LINUX
// set up mask // set up mask
Mono.Unix.Native.FilePermissions permissions = Mono.Unix.Native.Syscall.umask( Mono.Unix.Native.FilePermissions permissions = Mono.Unix.Native.Syscall.umask(
Mono.Unix.Native.FilePermissions.S_IWGRP | Mono.Unix.Native.FilePermissions.S_IWGRP |
Mono.Unix.Native.FilePermissions.S_IWOTH); Mono.Unix.Native.FilePermissions.S_IRGRP |
Mono.Unix.Native.FilePermissions.S_IROTH |
Mono.Unix.Native.FilePermissions.S_IWOTH);
// create the directory if necessary // create the directory if necessary
if (!Directory.Exists(POLICY_DIRECTORY)) if (!Directory.Exists(POLICY_DIRECTORY))

View File

@ -21,60 +21,62 @@
***********************************************************************/ ***********************************************************************/
using System; using System;
using System.Collections; using System.Collections;
using System.IO; using System.IO;
using sscs.cache; using sscs.cache;
using sscs.common; using sscs.common;
using sscs.constants; using sscs.constants;
namespace sscs.common namespace sscs.common
{ {
internal class UnixUser : User internal class UnixUser : User
{ {
private string m_userHome = null; private string m_userHome = null;
internal UnixUser() internal UnixUser()
{ {
} }
internal UnixUser(UserIdentifier unixUserId, string sUserHome) internal UnixUser(UserIdentifier unixUserId, string sUserHome)
{ {
m_userHome = sUserHome; m_userHome = sUserHome;
this.UnixUser(unixUserId); userId = unixUserId;
} secretStore = new SecretStore(this);
internal UnixUser(UserIdentifier unixUserId) }
{
userId = unixUserId; internal UnixUser(UserIdentifier unixUserId)
secretStore = new SecretStore(this); {
} userId = unixUserId;
secretStore = new SecretStore(this);
override internal void SetUserName(string username) }
{
userName = username; override internal void SetUserName(string username)
} {
userName = username;
override internal string GetUserName() }
{
return userName; override internal string GetUserName()
} {
return userName;
override internal string GetUserHomeDir() }
{
if (m_userHome == null) override internal string GetUserHomeDir()
{ {
uint uid = (uint)userId.GetUID(); if (m_userHome == null)
Mono.Unix.UnixUserInfo uui = new Mono.Unix.UnixUserInfo(uid); {
userName = uui.UserName; uint uid = (uint)userId.GetUID();
return uui.HomeDirectory; Mono.Unix.UnixUserInfo uui = new Mono.Unix.UnixUserInfo(uid);
} userName = uui.UserName;
else return uui.HomeDirectory;
{ }
return m_userHome; else
} {
} return m_userHome;
}
}
}
}
}
}