Chinaunix首页 | 论坛 | 博客

=.=

  • 博客访问: 139343
  • 博文数量: 50
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 550
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-25 17:22
文章分类

全部博文(50)

文章存档

2010年(7)

2009年(43)

我的朋友

分类: WINDOWS

2009-12-28 17:32:17


    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 );


阅读(1170) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~