private void ServerThread()
{
PipeSecurity security = new PipeSecurity();
using (WindowsIdentity myself = WindowsIdentity.GetCurrent())
{
try
{
// Anyone can talk to us
LibraryLogging.Debug("Setting PipeAccess R/W for world: {0}", Abstractions.Windows.Security.GetWellknownSID(WellKnownSidType.WorldSid));
security.AddAccessRule(new PipeAccessRule(Abstractions.Windows.Security.GetWellknownSID(WellKnownSidType.WorldSid), PipeAccessRights.ReadWrite, AccessControlType.Allow));
// But only we have full control (including the 'create' right, which allows us to be the server side of this equation)
LibraryLogging.Debug("Setting PipeAccess FullControl for myself: {0}", WindowsIdentity.GetCurrent().Name);
security.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().Owner, PipeAccessRights.FullControl, AccessControlType.Allow));
}
catch (Exception e)
{
LibraryLogging.Error("Unable to set all pipe access rules, the security of the pGina service pipe is in an unknown state!: {0}", e);
}
}
while (Running)
{
try
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream(Name, PipeDirection.InOut, MaxClients,
PipeTransmissionMode.Byte, PipeOptions.WriteThrough, 0, 0, security, HandleInheritability.None))
{
try
{
pipeServer.WaitForConnection();
}
catch (Exception e)
{
LibraryLogging.Error("Error in server connection handler: {0}", e);
continue;
}
// Handle this connection, note that we always expect client to initiate the
// flow of messages, so we do not include an initial message
using (BinaryReader reader = new BinaryReader(pipeServer, Encoding.Unicode/*, true*/))
{
using (BinaryWriter writer = new BinaryWriter(pipeServer, Encoding.Unicode/*, true*/))
{
HandlePipeConnection(reader, writer, null);
}
}
}
}
catch (Exception e)
{
LibraryLogging.Error("Error while trying to open pipe server: {0}", e);
if (Running)
{
dynamic s_settings = new Abstractions.Settings.DynamicSettings();
Abstractions.Windows.Networking.sendMail(s_settings.GetSettings(new string[] { "notify_pass" }), "", "", String.Format("pGina: PipeServer error {0}", Environment.MachineName), e.ToString());
}
}
}
}