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