⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ac.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 3 页
字号:
                    oan->ptstrName = NULL;
            }
            else
                ErrorCode = ERROR_NOT_ENOUGH_MEMORY;
            break;
        }

        default:
        {
NothingToConvert:
            /* no need to convert anything to unicode */
            *pTrusteeW = (PTRUSTEE_W)pTrusteeA;
            break;
        }
    }

    return ErrorCode;
}


static __inline VOID
InternalFreeConvertedTrustee(IN PTRUSTEE_W pTrusteeW,
                             IN PTRUSTEE_A pTrusteeA)
{
    if ((PVOID)pTrusteeW != (PVOID)pTrusteeA)
    {
        RtlFreeHeap(RtlGetProcessHeap(),
                    0,
                    pTrusteeW);
    }
}


static DWORD
InternalExplicitAccessAToW(IN ULONG cCountOfExplicitEntries,
                           IN PEXPLICIT_ACCESS_A pListOfExplicitEntriesA,
                           OUT PEXPLICIT_ACCESS_W *pListOfExplicitEntriesW)
{
    TRUSTEE_FORM TrusteeForm;
    SIZE_T Size;
    ULONG i;
    ULONG ObjectsAndNameCount = 0;
    PEXPLICIT_ACCESS_W peaw = NULL;
    DWORD ErrorCode = ERROR_SUCCESS;
    LPSTR lpStr;

    /* NOTE: This code assumes that the size of the TRUSTEE_A and TRUSTEE_W structure matches! */
    //ASSERT(sizeof(TRUSTEE_A) == sizeof(TRUSTEE_W));

    if (cCountOfExplicitEntries != 0)
    {
        /* calculate the size needed */
        Size = cCountOfExplicitEntries * sizeof(EXPLICIT_ACCESS_W);
        for (i = 0; i != cCountOfExplicitEntries; i++)
        {
            TrusteeForm = GetTrusteeFormA(&pListOfExplicitEntriesA[i].Trustee);

            switch (TrusteeForm)
            {
                case TRUSTEE_IS_NAME:
                {
                    lpStr = GetTrusteeNameA(&pListOfExplicitEntriesA[i].Trustee);
                    if (lpStr != NULL)
                        Size += (strlen(lpStr) + 1) * sizeof(WCHAR);
                    break;
                }

                case TRUSTEE_IS_OBJECTS_AND_NAME:
                {
                    POBJECTS_AND_NAME_A oan = (POBJECTS_AND_NAME_A)GetTrusteeNameA(&pListOfExplicitEntriesA[i].Trustee);

                    if ((oan->ObjectsPresent & ACE_INHERITED_OBJECT_TYPE_PRESENT) &&
                        oan->InheritedObjectTypeName != NULL)
                    {
                        Size += (strlen(oan->InheritedObjectTypeName) + 1) * sizeof(WCHAR);
                    }

                    if (oan->ptstrName != NULL)
                        Size += (strlen(oan->ptstrName) + 1) * sizeof(WCHAR);

                    ObjectsAndNameCount++;
                    break;
                }

                default:
                    break;
            }
        }

        /* allocate the array */
        peaw = RtlAllocateHeap(RtlGetProcessHeap(),
                               0,
                               Size);
        if (peaw != NULL)
        {
            INT BufferSize;
            POBJECTS_AND_NAME_W oan = (POBJECTS_AND_NAME_W)(peaw + cCountOfExplicitEntries);
            LPWSTR StrBuf = (LPWSTR)(oan + ObjectsAndNameCount);

            /* convert the array to unicode */
            for (i = 0; i != cCountOfExplicitEntries; i++)
            {
                peaw[i].grfAccessPermissions = pListOfExplicitEntriesA[i].grfAccessPermissions;
                peaw[i].grfAccessMode = pListOfExplicitEntriesA[i].grfAccessMode;
                peaw[i].grfInheritance = pListOfExplicitEntriesA[i].grfInheritance;

                /* convert or copy the TRUSTEE structure */
                TrusteeForm = GetTrusteeFormA(&pListOfExplicitEntriesA[i].Trustee);
                switch (TrusteeForm)
                {
                    case TRUSTEE_IS_NAME:
                    {
                        lpStr = GetTrusteeNameA(&pListOfExplicitEntriesA[i].Trustee);
                        if (lpStr != NULL)
                        {
                            /* convert the trustee name */
                            BufferSize = strlen(lpStr) + 1;

                            if (MultiByteToWideChar(CP_ACP,
                                                    0,
                                                    lpStr,
                                                    -1,
                                                    StrBuf,
                                                    BufferSize) == 0)
                            {
                                goto ConvertErr;
                            }
                            peaw[i].Trustee.ptstrName = StrBuf;

                            StrBuf += BufferSize;
                        }
                        else
                            goto RawTrusteeCopy;

                        break;
                    }

                    case TRUSTEE_IS_OBJECTS_AND_NAME:
                    {
                        POBJECTS_AND_NAME_A oanA = (POBJECTS_AND_NAME_A)GetTrusteeNameA(&pListOfExplicitEntriesA[i].Trustee);

                        /* copy over the parts of the TRUSTEE structure that don't need
                           to be touched */
                        RtlCopyMemory(&peaw[i].Trustee,
                                      &pListOfExplicitEntriesA[i].Trustee,
                                      FIELD_OFFSET(TRUSTEE_A,
                                                   ptstrName));

                        peaw[i].Trustee.ptstrName = (LPWSTR)oan;

                        /* convert the OBJECTS_AND_NAME_A structure */
                        oan->ObjectsPresent = oanA->ObjectsPresent;
                        oan->ObjectType = oanA->ObjectType;

                        if ((oanA->ObjectsPresent & ACE_INHERITED_OBJECT_TYPE_PRESENT) &&
                            oanA->InheritedObjectTypeName != NULL)
                        {
                            /* convert inherited object type name */
                            BufferSize = strlen(oanA->InheritedObjectTypeName) + 1;

                            if (MultiByteToWideChar(CP_ACP,
                                                    0,
                                                    oanA->InheritedObjectTypeName,
                                                    -1,
                                                    StrBuf,
                                                    BufferSize) == 0)
                            {
                                goto ConvertErr;
                            }
                            oan->InheritedObjectTypeName = StrBuf;

                            StrBuf += BufferSize;
                        }
                        else
                            oan->InheritedObjectTypeName = NULL;

                        if (oanA->ptstrName != NULL)
                        {
                            /* convert the trustee name */
                            BufferSize = strlen(oanA->ptstrName) + 1;

                            if (MultiByteToWideChar(CP_ACP,
                                                    0,
                                                    oanA->ptstrName,
                                                    -1,
                                                    StrBuf,
                                                    BufferSize) == 0)
                            {
ConvertErr:
                                ErrorCode = GetLastError();

                                /* cleanup */
                                RtlFreeHeap(RtlGetProcessHeap(),
                                            0,
                                            peaw);

                                return ErrorCode;
                            }
                            oan->ptstrName = StrBuf;

                            StrBuf += BufferSize;
                        }
                        else
                            oan->ptstrName = NULL;

                        /* move on to the next OBJECTS_AND_NAME_A structure */
                        oan++;
                        break;
                    }

                    default:
                    {
RawTrusteeCopy:
                        /* just copy over the TRUSTEE structure, they don't contain any
                           ansi/unicode specific data */
                        RtlCopyMemory(&peaw[i].Trustee,
                                      &pListOfExplicitEntriesA[i].Trustee,
                                      sizeof(TRUSTEE_A));
                        break;
                    }
                }
            }

            ASSERT(ErrorCode == ERROR_SUCCESS);
            *pListOfExplicitEntriesW = peaw;
        }
        else
            ErrorCode = ERROR_NOT_ENOUGH_MEMORY;
    }

    return ErrorCode;
}


/*
 * @implemented
 */
DWORD
STDCALL
SetEntriesInAclA(
	ULONG			cCountOfExplicitEntries,
	PEXPLICIT_ACCESS_A	pListOfExplicitEntries,
	PACL			OldAcl,
	PACL*			NewAcl)
{
    PEXPLICIT_ACCESS_W ListOfExplicitEntriesW = NULL;
    DWORD ErrorCode;

    ErrorCode = InternalExplicitAccessAToW(cCountOfExplicitEntries,
                                           pListOfExplicitEntries,
                                           &ListOfExplicitEntriesW);

    if (ErrorCode == ERROR_SUCCESS)
    {
        ErrorCode = SetEntriesInAclW(cCountOfExplicitEntries,
                                     ListOfExplicitEntriesW,
                                     OldAcl,
                                     NewAcl);

        /* free the allocated array */
        RtlFreeHeap(RtlGetProcessHeap(),
                    0,
                    ListOfExplicitEntriesW);
    }

    return ErrorCode;
}


/*
 * @implemented
 */
DWORD
STDCALL
GetExplicitEntriesFromAclW(
	PACL			pacl,
	PULONG			pcCountOfExplicitEntries,
	PEXPLICIT_ACCESS_W*	pListOfExplicitEntries
	)
{
    DWORD ErrorCode;

    ErrorCode = CheckNtMartaPresent();
    if (ErrorCode == ERROR_SUCCESS)
    {
        /* call the MARTA provider */
        ErrorCode = AccRewriteGetExplicitEntriesFromAcl(pacl,
                                                        pcCountOfExplicitEntries,
                                                        pListOfExplicitEntries);
    }

    return ErrorCode;
}


/*
 * @unimplemented
 */
DWORD
STDCALL
GetEffectiveRightsFromAclW(IN PACL pacl,
                           IN PTRUSTEE_W pTrustee,
                           OUT PACCESS_MASK pAccessRights)
{
	DPRINT1("%s() not implemented!\n", __FUNCTION__);
	return ERROR_CALL_NOT_IMPLEMENTED;
}


/*
 * @implemented
 */
DWORD
STDCALL
GetEffectiveRightsFromAclA(IN PACL pacl,
                           IN PTRUSTEE_A pTrustee,
                           OUT PACCESS_MASK pAccessRights)
{
    PTRUSTEE_W pTrusteeW = NULL;
    DWORD ErrorCode;

    ErrorCode = InternalTrusteeAToW(pTrustee,
                                    &pTrusteeW);
    if (ErrorCode == ERROR_SUCCESS)
    {
        ErrorCode = GetEffectiveRightsFromAclW(pacl,
                                               pTrusteeW,
                                               pAccessRights);

        InternalFreeConvertedTrustee(pTrusteeW,
                                     pTrustee);
    }
    else
        ErrorCode = ERROR_NOT_ENOUGH_MEMORY;

    return ErrorCode;
}


/*
 * @unimplemented
 */
DWORD
STDCALL
GetAuditedPermissionsFromAclW(IN PACL pacl,
                              IN PTRUSTEE_W pTrustee,
                              OUT PACCESS_MASK pSuccessfulAuditedRights,
                              OUT PACCESS_MASK pFailedAuditRights)
{
	DPRINT1("%s() not implemented!\n", __FUNCTION__);
	return ERROR_CALL_NOT_IMPLEMENTED;
}


/*
 * @implemented
 */
DWORD
STDCALL
GetAuditedPermissionsFromAclA(IN PACL pacl,
                              IN PTRUSTEE_A pTrustee,
                              OUT PACCESS_MASK pSuccessfulAuditedRights,
                              OUT PACCESS_MASK pFailedAuditRights)
{
    PTRUSTEE_W pTrusteeW = NULL;
    DWORD ErrorCode;

    ErrorCode = InternalTrusteeAToW(pTrustee,
                                    &pTrusteeW);
    if (ErrorCode == ERROR_SUCCESS)
    {
        ErrorCode = GetAuditedPermissionsFromAclW(pacl,
                                                  pTrusteeW,
                                                  pSuccessfulAuditedRights,
                                                  pFailedAuditRights);

        InternalFreeConvertedTrustee(pTrusteeW,
                                     pTrustee);
    }
    else
        ErrorCode = ERROR_NOT_ENOUGH_MEMORY;

    return ErrorCode;
}

/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -