223 lines
5.5 KiB
C#
223 lines
5.5 KiB
C#
/***********************************************************************
|
|
*
|
|
* Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; version 2.1
|
|
* of the License.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Library Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, Novell, Inc.
|
|
*
|
|
* To contact Novell about this file by physical or electronic mail,
|
|
* you may find current contact information at www.novell.com.
|
|
*
|
|
***********************************************************************/
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
#if LINUX
|
|
using Mono.Unix;
|
|
using Mono.Unix.Native;
|
|
#endif
|
|
|
|
|
|
using sscs.common;
|
|
using sscs.constants;
|
|
|
|
namespace sscs.common
|
|
{
|
|
class CSSSUtils
|
|
{
|
|
|
|
#if W32
|
|
private static string CASA_REG_KEY = "SOFTWARE\\Novell\\CASA";
|
|
|
|
private static bool IsRegKeySet(string sPath, string sValue)
|
|
{
|
|
Microsoft.Win32.RegistryKey key;
|
|
|
|
try
|
|
{
|
|
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sPath);
|
|
int iValue = (int)key.GetValue(sValue);
|
|
key.Close();
|
|
if (iValue > 0)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
|
|
}
|
|
return false;
|
|
}
|
|
|
|
internal static void SetAllowDesktopAccess()
|
|
{
|
|
// create a reg key
|
|
Microsoft.Win32.RegistryKey key;
|
|
key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(CASA_REG_KEY);
|
|
key.SetValue("CacheDesktopPassword", 1, Microsoft.Win32.RegistryValueKind.DWord);
|
|
key.Close();
|
|
}
|
|
|
|
internal static void RemoveGlobalCASASettings()
|
|
{
|
|
// Delete CASA settings
|
|
try
|
|
{
|
|
Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree(CASA_REG_KEY);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine(e.ToString());
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
public static bool StoreDesktopPasswordInCache()
|
|
{
|
|
#if LINUX
|
|
return true;
|
|
#else
|
|
return IsRegKeySet(CASA_REG_KEY, "CacheDesktopPassword");
|
|
#endif
|
|
}
|
|
|
|
public static bool IsFileOwnedByRoot(string filePath)
|
|
{
|
|
#if LINUX
|
|
try
|
|
{
|
|
int retVal = -1;
|
|
int fd = -1;
|
|
Stat fStatus;
|
|
|
|
fd = Syscall.open(filePath, OpenFlags.O_NOFOLLOW);
|
|
|
|
retVal = Syscall.fstat(fd, out fStatus);
|
|
if ( retVal < 0 )
|
|
{
|
|
CSSSLogger.DbgLog("fstat() failed...");
|
|
return false;
|
|
}
|
|
|
|
if(fStatus.st_uid != 0)
|
|
{
|
|
CSSSLogger.DbgLog("File not owned by root: {0}");
|
|
return false;
|
|
}
|
|
|
|
retVal = Syscall.close(fd);
|
|
if ( retVal < 0 )
|
|
{
|
|
CSSSLogger.DbgLog("close() failed...");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
CSSSLogger.ExpLog(e.ToString());
|
|
CSSSLogger.DbgLog("Failed while checking whether the file is owned by root or not");
|
|
}
|
|
return false;
|
|
#else
|
|
return true;
|
|
#endif
|
|
}
|
|
|
|
|
|
public static bool SetSocketUserAsOwner(string filePath, UserIdentifier userID)
|
|
{
|
|
#if LINUX
|
|
try
|
|
{
|
|
int retVal = -1;
|
|
int fd = -1;
|
|
|
|
fd = Syscall.open(filePath, OpenFlags.O_NOFOLLOW);
|
|
|
|
UnixUserInfo uui = new UnixUserInfo(userID.GetUID());
|
|
|
|
retVal = Syscall.fchown(fd, (uint) uui.UserId, (uint) uui.GroupId);
|
|
if ( retVal < 0 )
|
|
{
|
|
CSSSLogger.DbgLog("fchown() failed...");
|
|
return false;
|
|
}
|
|
|
|
retVal = Syscall.close(fd);
|
|
if ( retVal < 0 )
|
|
{
|
|
CSSSLogger.DbgLog("close() failed...");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
CSSSLogger.ExpLog(e.ToString());
|
|
CSSSLogger.DbgLog("Unable to set the owner of the file to the socket user");
|
|
}
|
|
return false;
|
|
#else
|
|
return true;
|
|
#endif
|
|
}
|
|
|
|
|
|
public static bool CompareSocketAndFileUserIds(string filePath, UserIdentifier userID)
|
|
{
|
|
#if LINUX
|
|
try
|
|
{
|
|
int retVal = -1;
|
|
int fd = -1;
|
|
Stat fStatus;
|
|
|
|
fd = Syscall.open(filePath, OpenFlags.O_NOFOLLOW);
|
|
|
|
retVal = Syscall.fstat(fd, out fStatus);
|
|
if ( retVal < 0 )
|
|
{
|
|
CSSSLogger.DbgLog("fstat() failed...");
|
|
return false;
|
|
}
|
|
|
|
if(fStatus.st_uid != userID.GetUID())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
retVal = Syscall.close(fd);
|
|
if ( retVal < 0 )
|
|
{
|
|
CSSSLogger.DbgLog("close() failed...");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
CSSSLogger.ExpLog(e.ToString());
|
|
CSSSLogger.DbgLog("Failed during the comparision of the user ids on the file and socket");
|
|
}
|
|
return false;
|
|
#else
|
|
return true;
|
|
#endif
|
|
}
|
|
}
|
|
}
|