SHARE_INFO_502 *pShareInfo = NULL;
DWORD dEnumNum = 0;
DWORD dTotalNum = 0;
DWORD dResumNum = 0;
NET_API_STATUS dStatus;
DWORD i = 0;
do
{
dStatus = NetShareEnum( NULL, 502, (LPBYTE *)&pShareInfo, -1, &dEnumNum, &dTotalNum, &dResumNum );
if ( dStatus == ERROR_SUCCESS || dStatus == ERROR_SUCCESS )
{
for ( i = 0; i < dTotalNum; i++, pShareInfo++ )
{
// 如果有安全描述符,就可以得到这个共享路径的权限(一般的默认共享,像C$是没有共享权限配置的 )
if ( pShareInfo->shi502_security_descriptor )
{
wprintf( L"%s:n", pShareInfo->shi502_path );
BOOL bDaclPresent;
PACL pACL;
BOOL bDaclDefault;
if ( !GetSecurityDescriptorDacl(
pShareInfo->shi502_security_descriptor,
&bDaclPresent,
&pACL,
&bDaclDefault ) )
{
printf("GetSecurityDescriptorDacl Fault, %d!n", GetLastError() );
continue;
}
if ( bDaclPresent )
{
ACL_SIZE_INFORMATION AclInfo;
if ( !GetAclInformation(
pACL,
&AclInfo,
sizeof(ACL_SIZE_INFORMATION), AclSizeInformation ) )
{
printf("GetAclInformation Error, %d!n", GetLastError() );
continue;
}
DWORD dwAceIndex = 0;
LPVOID pTmpAce = NULL;
for ( ; dwAceIndex < AclInfo.AceCount; dwAceIndex++ )
{
if ( !GetAce( pACL, dwAceIndex, &pTmpAce ) )
{
printf("GetAce Error: %d!n", GetLastError() );
continue;
}
PACCESS_ALLOWED_ACE pAccessAce = (PACCESS_ALLOWED_ACE)pTmpAce;
DWORD UserNameSize = 0;
DWORD GroupNameSize = 0;
SID_NAME_USE Snu;
LookupAccountSidW( NULL, &pAccessAce->SidStart,
NULL, &UserNameSize,
NULL, &GroupNameSize, &Snu );
TCHAR *pUserName = (TCHAR *)malloc(UserNameSize + 102 * sizeof(TCHAR) );
if ( !pUserName )
{
continue;
}
TCHAR *pGroupName = (TCHAR *)malloc( GroupNameSize + 101 * sizeof( TCHAR) );
if ( !pGroupName )
{
continue;
}
if ( !LookupAccountSidW( NULL, &pAccessAce->SidStart,
(LPWSTR)pUserName, &UserNameSize,
(LPWSTR)pGroupName, &GroupNameSize, &Snu ) )
{
printf( "LookupAccountSidW Error, %d!n", GetLastError() );
continue;
}
if ( pGroupName[0] == 0 )
{
wprintf( L" %s ", pUserName );
}
else
{
wprintf( L" %s\%s ", pGroupName, pUserName );
}
if ( pAccessAce->Mask & WRITE_OWNER )
{
printf("WRITE ");
}
if ( pAccessAce->Mask & READ_CONTROL )
{
printf("READn");
}
else
{
printf("n");
}
free( pUserName );
free( pGroupName );
}
}
}
}
}
else
{
printf( "NetShareEnum error: %dn", GetLastError() );
}
} while ( dStatus == ERROR_MORE_DATA );
|