- Bug 242404. Use "lstat()" to find the owner of micasad socket file.

This commit is contained in:
Rajasekaran Nagarajan 2007-03-12 11:11:27 +00:00
parent 27a4acbb83
commit 9f2f0ff262

View File

@ -18,165 +18,165 @@
* 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.Net;
using System.IO;
using System.Net.Sockets;
using Mono.Unix;
using System.Text;
namespace Novell.CASA.MiCasa.Communication
{
/// <summary>
/// Summary description for UnixIPCClientChannel.
/// </summary>
public class UnixIPCClientChannel : ClientChannel
{
private Socket mSocket = null;
private string socketFileName = "/tmp/.novellCASA";
private EndPoint sockEndPoint;
public UnixIPCClientChannel()
{
}
public void Open()
{
mSocket = new Socket(AddressFamily.Unix,
SocketType.Stream,
ProtocolType.IP);
if (mSocket == null)
{
throw new Exception("could not get socket");
}
sockEndPoint = new UnixEndPoint(socketFileName);
UnixFileSystemInfo sockFileInfo = new UnixFileInfo(socketFileName);
UnixUserInfo sockFileOwner = sockFileInfo.OwnerUser;
// root is the owner of the file "/tmp/.novellCASA"
if (sockFileOwner.UserId == 0)
{
mSocket.Connect(sockEndPoint);
}
else
{
throw new Exception("not a valid miCASA service");
}
}
public int Read(byte[] buf)
{
buf = Read();
if (buf != null)
{
//Console.WriteLine("Bytes read = " + buf.Length);
return buf.Length;
}
else
return 0;
}
public byte[] Read()
{
byte[] returnBuffer = null;
int bytesRecvd = 0;
try
{
***********************************************************************/
using System;
using System.Net;
using System.IO;
using System.Net.Sockets;
using Mono.Unix;
using Mono.Unix.Native;
using System.Text;
namespace Novell.CASA.MiCasa.Communication
{
/// <summary>
/// Summary description for UnixIPCClientChannel.
/// </summary>
public class UnixIPCClientChannel : ClientChannel
{
private Socket mSocket = null;
private string socketFileName = "/tmp/.novellCASA";
private EndPoint sockEndPoint;
public UnixIPCClientChannel()
{
}
public void Open()
{
Stat socketFileStatus;
mSocket = new Socket(AddressFamily.Unix,
SocketType.Stream,
ProtocolType.IP);
if (mSocket == null)
{
throw new Exception("could not get socket");
}
Syscall.lstat(socketFileName, out socketFileStatus);
// root is the owner of the file "/tmp/.novellCASA"
if (socketFileStatus.st_uid == 0)
{
sockEndPoint = new UnixEndPoint(socketFileName);
mSocket.Connect(sockEndPoint);
}
else
{
throw new Exception("not a valid miCASA service");
}
}
public int Read(byte[] buf)
{
buf = Read();
if (buf != null)
{
//Console.WriteLine("Bytes read = " + buf.Length);
return buf.Length;
}
else
return 0;
}
public byte[] Read()
{
byte[] returnBuffer = null;
int bytesRecvd = 0;
try
{
/* We need to read 'msgLen' to know how many bytes to
* allocate.
*/
byte[] msgIdBytes = new byte[2];
bytesRecvd = mSocket.Receive(msgIdBytes);
if (0 == bytesRecvd)
{
return null;
}
byte[] msgLenBytes = new byte[4];
bytesRecvd = mSocket.Receive(msgLenBytes);
if (0 == bytesRecvd)
{
return null;
}
uint msgLen = BitConverter.ToUInt32(msgLenBytes, 0);
if (msgLen > 6)
{
byte[] buf = null;
int bytesAvailable;
int totalBytes = 0;
int msgLencount = 0;
string bufstring = null;
// buffer for data
MemoryStream ms = new MemoryStream();
while (totalBytes < (msgLen - 6))
{
bytesAvailable = mSocket.Available;
if (0 == bytesAvailable)
{
break;
}
buf = new byte[bytesAvailable];
bytesRecvd = mSocket.Receive(buf);
ms.Write(buf, 0, bytesRecvd);
totalBytes = totalBytes + bytesAvailable;
}
if (totalBytes == 0)
return null;
byte[] finalbuf = ms.ToArray();
int returnBufferLen = msgIdBytes.Length + msgLenBytes.Length + totalBytes;
returnBuffer = new byte[returnBufferLen];
Array.Copy(msgIdBytes, returnBuffer, 2);
Array.Copy(msgLenBytes, 0, returnBuffer, 2, 4);
Array.Copy(finalbuf, 0, returnBuffer, 6, finalbuf.Length);
return returnBuffer;
}
else
{
returnBuffer = new byte[6];
Array.Copy(msgIdBytes, returnBuffer, 2);
Array.Copy(msgLenBytes, 0, returnBuffer, 2, 4);
return returnBuffer;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}
public int Write(byte[] buf)
{
try
{
mSocket.Send(buf);
//Console.WriteLine("Bytes written = " + buf.Length);
return buf.Length;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return 0;
}
}
public void Close()
{
mSocket.Close();
}
}
}
*/
byte[] msgIdBytes = new byte[2];
bytesRecvd = mSocket.Receive(msgIdBytes);
if (0 == bytesRecvd)
{
return null;
}
byte[] msgLenBytes = new byte[4];
bytesRecvd = mSocket.Receive(msgLenBytes);
if (0 == bytesRecvd)
{
return null;
}
uint msgLen = BitConverter.ToUInt32(msgLenBytes, 0);
if (msgLen > 6)
{
byte[] buf = null;
int bytesAvailable;
int totalBytes = 0;
int msgLencount = 0;
string bufstring = null;
// buffer for data
MemoryStream ms = new MemoryStream();
while (totalBytes < (msgLen - 6))
{
bytesAvailable = mSocket.Available;
if (0 == bytesAvailable)
{
break;
}
buf = new byte[bytesAvailable];
bytesRecvd = mSocket.Receive(buf);
ms.Write(buf, 0, bytesRecvd);
totalBytes = totalBytes + bytesAvailable;
}
if (totalBytes == 0)
return null;
byte[] finalbuf = ms.ToArray();
int returnBufferLen = msgIdBytes.Length + msgLenBytes.Length + totalBytes;
returnBuffer = new byte[returnBufferLen];
Array.Copy(msgIdBytes, returnBuffer, 2);
Array.Copy(msgLenBytes, 0, returnBuffer, 2, 4);
Array.Copy(finalbuf, 0, returnBuffer, 6, finalbuf.Length);
return returnBuffer;
}
else
{
returnBuffer = new byte[6];
Array.Copy(msgIdBytes, returnBuffer, 2);
Array.Copy(msgLenBytes, 0, returnBuffer, 2, 4);
return returnBuffer;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}
public int Write(byte[] buf)
{
try
{
mSocket.Send(buf);
//Console.WriteLine("Bytes written = " + buf.Length);
return buf.Length;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return 0;
}
}
public void Close()
{
mSocket.Close();
}
}
}