Friday, February 13, 2009

Allowing access when opening a named pipe from a Service

//Basic idea is create the pipe with a NULL DACL. This code worked for Vista as well.
//http://msdn.microsoft.com/en-us/library/aa379286(VS.85).aspx

BYTE sd[SECURITY_DESCRIPTOR_MIN_LENGTH];
SECURITY_ATTRIBUTES sa;

sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = &sd;

if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
{
//handle the error here
}

//A NULL DACL is assigned to the security descriptor, which allows ALL ACCESS to the named pipe.
if (!SetSecurityDescriptorDacl(&sd, TRUE, (PACL) 0, FALSE))
{
//handle the error here
}

//Use the security descriptor in the CreateNamedPipe()
hPipe = CreateNamedPipe(..., &sa);