
05-28-2007
|
|
|
|
LocalSystem cannot enumerate domains on Windows Vista
Hi.
I develop an application that needs to show the user a list of the
visible domain names in a organization. This used to be done with the
paif of functions WNetOpenEnum()/WNetEnumResource(). Up to Windows 2k3
these functions effectively allowed the program, running as
LocalSystem, to retrieve the list of domains, but with Windows Vista,
WNetOpenEnum() fails miserably with error code 1312:
bool CSelectComputerNetwork::EnumDomains(NETRESOURCE* pnr,
vector<CNETRESOURCE>& vecnrDomains)
{
HANDLE handle = NULL;
DWORD dwResult = ::WNetOpenEnum(RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
0,
pnr,
&handle);
if (dwResult == NO_ERROR)
{
NETRESOURCE nr, *pnrChildrenResource = new
NETRESOURCE[BUFSIZE];
DWORD dwRequested = -1;
DWORD& dwGotten = dwRequested;
DWORD dwBufSize = sizeof(NETRESOURCE) * BUFSIZE;
dwResult = ::WNetEnumResource(handle, &dwRequested,
pnrChildrenResource, &dwBufSize);
if (dwResult == NO_ERROR)
{
for (int i = 0; i < dwGotten; ++i)
{
if (pnrChildrenResource[i].dwDisplayType ==
RESOURCEDISPLAYTYPE_DOMAIN)
{
vecnrDomains.push_back(pnrChildrenResource[i]);
}
else
{
memcpy(&nr, &pnrChildrenResource[i],
sizeof(NETRESOURCE));
if (!EnumDomains(&nr, vecnrDomains))
{
delete[] pnrChildrenResource;
::WNetCloseEnum(handle);
return false;
}
}
}
delete[] pnrChildrenResource;
return (::WNetCloseEnum(handle) == NO_ERROR) ? true :
false;
}
else if (dwResult == ERROR_NO_MORE_ITEMS)
{
delete[] pnrChildrenResource;
return (::WNetCloseEnum(handle) == NO_ERROR) ? true :
false;
}
else//WNetEnumResource(_) failed
{
_ASSERTE(FALSE);
delete[] pnrChildrenResource;
::WNetCloseEnum(handle);
return false;
}
}
else//WNetOpenEnum(_) failed
{
_ASSERTE(FALSE);
return false;
}
}
Notice that if this code is run as Administrator, it works just fine;
however, this is not an option since that could give the program too
much power and can open security holes. I was wondering if there's a
local security police that I could tweak so that the LocalSystem user
or my program is allowed to enumerate the domains.
Thanks in advanced.
Omar Estrada
|