几个代码

BOOL
ForceFileNoCompress(
    IN  DWORD cArgs,
    IN  LPSTR Args[],
    OUT LPSTR *TextOut
    )

/*++

Routine Description:

    Check to see if this file is using NTFS compression, and if so,
    uncompress it.

Arguments:

    Args[0] - name of file to force uncompressed

Return Value:

    none

--*/

{
    DWORD  FileAttribs, Length;
    HANDLE FileHandle;
    USHORT State = 0;

    *TextOut = ReturnTextBuffer;
    if(cArgs != 1) {
        SetErrorText(IDS_ERROR_BADARGS);
        return(FALSE);
    }

    if((FileAttribs = GetFileAttributes(Args[0])) == 0xFFFFFFFF) {
        SetErrorText(IDS_ERROR_DLLOOM);
        return (FALSE);
    }

    if(FileAttribs & FILE_ATTRIBUTE_COMPRESSED) {
        //
        // We must turn off compression
        //
        SetFileAttributes(Args[0], FILE_ATTRIBUTE_NORMAL);

        FileHandle = CreateFile(Args[0],
                                GENERIC_WRITE,
                                FILE_SHARE_READ | FILE_SHARE_WRITE,
                                NULL,
                                OPEN_EXISTING,
                                FILE_FLAG_BACKUP_SEMANTICS,
                                NULL
                                );

        if(FileHandle == INVALID_HANDLE_VALUE) {
            SetErrorText(IDS_ERROR_DLLOOM);
            return (FALSE);
        }

        DeviceIoControl(FileHandle,
                        FSCTL_SET_COMPRESSION,
                        &State,
                        sizeof(State),
                        NULL,
                        0,
                        &Length,
                        NULL
                        );

        CloseHandle(FileHandle);
    }
    return( TRUE );
}
 

 

BOOL
GetIEPathName(
    IN  DWORD cArgs,
    IN  LPSTR Args[],
    OUT LPSTR *TextOut
    )

/*++

Routine Description:

    Get the Path Name in the registry for IE.

Return Value:

    none

--*/
{
    NTSTATUS status;
    HKEY     hIEKey;
    DWORD    cbValue;
    DWORD    dwType;
    LPBYTE   lpValue;
    LPBYTE   lpEndValue;
    TCHAR    szIExplore[] = TEXT("SOFTWARE//Microsoft//Windows//CurrentVersion//App Paths//IEXPLORE.EXE");
    TCHAR    szSys[]     = TEXT("Path");

    *TextOut = ReturnTextBuffer;

    SetReturnText("FAILURE");

    status = RegOpenKeyEx(
                HKEY_LOCAL_MACHINE,
                szIExplore,
                0,
                KEY_READ,
                &hIEKey
                );

    if (status != ERROR_SUCCESS){
        return(TRUE);
    }

    cbValue = 0;
    status  = RegQueryValueEx(
                 hIEKey,
                 szSys,
                 NULL,     // Reserved
                 &dwType,
                 NULL,     // Buffer
                 &cbValue  // size in bytes returned
                 );

    if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {

        //
        // Allocate space for value
        //

        lpValue = MyMalloc(cbValue);
        if (lpValue != NULL) {

            status  = RegQueryValueEx(
                         hIEKey,
                         szSys,
                         NULL, // Reserved
                         &dwType,
                         lpValue,
                         &cbValue
                         );

            if (status != ERROR_SUCCESS) {
                return(TRUE);
            }

            lpEndValue = strstr(lpValue, ";");
            *lpEndValue = '/0';
            SetReturnText(lpValue);
        }
    }

    return (TRUE);
}

 

 

BOOL
GetHtrPathName(
    IN  DWORD cArgs,
    IN  LPSTR Args[],
    OUT LPSTR *TextOut
    )

/*++

Routine Description:

    Get the Path Name in the registry for where to put IIS .htr files

Return Value:

    none

--*/
{
    NTSTATUS status;
    HKEY     hHtrKey;
    DWORD    cbValue;
    DWORD    dwType;
    LPBYTE   lpValue;
    LPBYTE   lpEndValue;
    TCHAR    szHtr[] = TEXT("SYSTEM//CurrentControlSet//Services//W3SVC//Parameters//Virtual Roots");
    TCHAR    szSys[]     = TEXT("/Scripts");

    *TextOut = ReturnTextBuffer;

    SetReturnText("FAILURE");

    status = RegOpenKeyEx(
                HKEY_LOCAL_MACHINE,
                szHtr,
                0,
                KEY_READ,
                &hHtrKey
                );

    if (status != ERROR_SUCCESS){
        return(TRUE);
    }

    cbValue = 0;
    status  = RegQueryValueEx(
                 hHtrKey,
                 szSys,
                 NULL,     // Reserved
                 &dwType,
                 NULL,     // Buffer
                 &cbValue  // size in bytes returned
                 );

    if (status == ERROR_SUCCESS || status == ERROR_MORE_DATA) {

        //
        // Allocate space for value
        //

        lpValue = MyMalloc(cbValue);
        if (lpValue != NULL) {

            status  = RegQueryValueEx(
                         hHtrKey,
                         szSys,
                         NULL, // Reserved
                         &dwType,
                         lpValue,
                         &cbValue
                         );

            if (status != ERROR_SUCCESS) {
                return(TRUE);
            }

            lpEndValue = strstr(lpValue, ",");
            *lpEndValue = '/0';
            SetReturnText(lpValue);
        }
    }

    return (TRUE);
}


// arg0 = YES for Reboot after Shutdown, NO for no Reboot.
BOOL
ShutdownSystem2(
    IN  DWORD cArgs,
    IN  LPSTR Args[],
    OUT LPSTR *TextOut
    )
{
    BOOL Reboot, Status, ForceClose;
    LONG              Privilege = SE_SHUTDOWN_PRIVILEGE;
    TOKEN_PRIVILEGES  PrevState;
    ULONG             ReturnLength = sizeof( TOKEN_PRIVILEGES );
    DWORD dwError;


    *TextOut = ReturnTextBuffer;
    if(cArgs != 2) {
        SetErrorText(IDS_ERROR_BADARGS);         // if reboot indication not given
        return(FALSE);
    }

    *ReturnTextBuffer = '/0';

    if (!lstrcmpi(Args[0], "YES"))
       Reboot = TRUE;
    else if (!lstrcmpi(Args[0], "NO"))
       Reboot = FALSE;
    else
       return(FALSE);

    if (!lstrcmpi(Args[1], "YES"))
       ForceClose = TRUE;
    else if (!lstrcmpi(Args[1], "NO"))
       ForceClose = FALSE;
    else
       return(FALSE);


    //
    // Enable the shutdown privilege
    //

    if ( !AdjustPrivilege(
              Privilege,
              ENABLE_PRIVILEGE,
              &PrevState,
              &ReturnLength
              )
       ) {

        SetErrorText( IDS_ERROR_DLLOOM );
        return( FALSE );
    }

    Status = InitiateSystemShutdown(
                         NULL,              // machinename
                         NULL,              // shutdown message
                         0,                 // delay
                         ForceClose,        // force apps close
                         Reboot             // reboot after shutdown
                         );

    RestorePrivilege( &PrevState );
    if( !Status ) {
        dwError = GetLastError();
        SetErrorText( IDS_ERROR_DLLOOM );
    }
    return( Status );

}

BOOL
AdjustPrivilege(
    IN LONG PrivilegeType,
    IN INT  Action,
    IN PTOKEN_PRIVILEGES PrevState, OPTIONAL
    IN PULONG ReturnLength          OPTIONAL
    )
/*++

Routine Description:

    Routine to enable or disable a particular privilege

Arguments:

    PrivilegeType    - Name of the privilege to enable / disable

    Action           - ENABLE_PRIVILEGE | DISABLE_PRIVILEGE

    PrevState        - Optional pointer to TOKEN_PRIVILEGES structure
                       to receive the previous state of privilege.

    ReturnLength     - Optional pointer to a ULONG to receive the length
                       of the PrevState returned.

Return value:

    TRUE if succeeded, FALSE otherwise.

--*/
{
    NTSTATUS          NtStatus;
    HANDLE            Token;
    LUID              Privilege;
    TOKEN_PRIVILEGES  NewState;
    ULONG             BufferLength = 0;


    //
    // Get Privilege LUID
    //

    Privilege = RtlConvertLongToLuid(PrivilegeType);
    // Privilege.LowPart = PrivilegeType;

    NewState.PrivilegeCount = 1;
    NewState.Privileges[0].Luid = Privilege;

    //
    // Look at action and determine the attributes
    //

    switch( Action ) {

    case ENABLE_PRIVILEGE:
        NewState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
        break;

    case DISABLE_PRIVILEGE:
        NewState.Privileges[0].Attributes = 0;
        break;

    default:
        return ( FALSE );
    }

    //
    // Open our own token
    //

    NtStatus = NtOpenProcessToken(
                   NtCurrentProcess(),
                   TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
                   &Token
                   );

    if (!NT_SUCCESS(NtStatus)) {
        return( FALSE );
    }

    //
    // See if return buffer is present and accordingly set the parameter
    // of buffer length
    //

    if ( PrevState && ReturnLength ) {
        BufferLength = *ReturnLength;
    }


    //
    // Set the state of the privilege
    //

    NtStatus = NtAdjustPrivilegesToken(
                   Token,                         // TokenHandle
                   FALSE,                         // DisableAllPrivileges
                   &NewState,                     // NewState
                   BufferLength,                  // BufferLength
                   PrevState,                     // PreviousState (OPTIONAL)
                   ReturnLength                   // ReturnLength (OPTIONAL)
                   );

    if ( NT_SUCCESS( NtStatus ) ) {

        NtClose( Token );
        return( TRUE );

    }
    else {

        NtClose( Token );
        return( FALSE );

    }
}

 

BOOL
RestorePrivilege(
    IN PTOKEN_PRIVILEGES PrevState
    )
/*++

Routine Description:

    To restore a privilege to its previous state

Arguments:

    PrevState    - Pointer to token privileges returned from an earlier
                   AdjustPrivileges call.

Return value:

    TRUE on success, FALSE otherwise

--*/
{
    NTSTATUS          NtStatus;
    HANDLE            Token;

    //
    // Parameter checking
    //

    if ( !PrevState ) {
        return ( FALSE );
    }

    //
    // Open our own token
    //

    NtStatus = NtOpenProcessToken(
                   NtCurrentProcess(),
                   TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
                   &Token
                   );

    if (!NT_SUCCESS(NtStatus)) {
        return( FALSE );
    }


    //
    // Set the state of the privilege
    //

    NtStatus = NtAdjustPrivilegesToken(
                   Token,                         // TokenHandle
                   FALSE,                         // DisableAllPrivileges
                   PrevState,                     // NewState
                   0,                             // BufferLength
                   NULL,                          // PreviousState (OPTIONAL)
                   NULL                           // ReturnLength (OPTIONAL)
                   );

    if ( NT_SUCCESS( NtStatus ) ) {
        NtClose( Token );
        return( TRUE );
    }
    else {

        NtClose( Token );
        return( FALSE );
    }
}


BOOL
FFileFound(
    IN LPSTR szPath
    )
{
    WIN32_FIND_DATA ffd;
    HANDLE          SearchHandle;

    if ( (SearchHandle = FindFirstFile( szPath, &ffd )) == INVALID_HANDLE_VALUE ) {
        return( FALSE );
    }
    else {
        FindClose( SearchHandle );
        return( TRUE );
    }
}


BOOL
FTransferSecurity(
    PCHAR Source,
    PCHAR Dest
    )
{
    #define CBSDBUF 1024
    CHAR  SdBuf[CBSDBUF];
    SECURITY_INFORMATION Si;
    PSECURITY_DESCRIPTOR Sd = (PSECURITY_DESCRIPTOR)SdBuf;
    DWORD cbSd = CBSDBUF;
    DWORD cbSdReq;

    PVOID  AllocBuffer = NULL;
    BOOL  Status;

    //
    // Get the security information from the source file
    //

    Si = OWNER_SECURITY_INFORMATION |
         GROUP_SECURITY_INFORMATION |
         DACL_SECURITY_INFORMATION;

    Status = GetFileSecurity(
                 Source,
                 Si,
                 Sd,
                 cbSd,
                 &cbSdReq
                 );

    if(!Status) {
        if( cbSdReq > CBSDBUF  && (AllocBuffer = malloc( cbSdReq ))) {

            Sd   = (PSECURITY_DESCRIPTOR)AllocBuffer;
            cbSd = cbSdReq;
            Status = GetFileSecurity(
                         Source,
                         Si,
                         (PSECURITY_DESCRIPTOR)Sd,
                         cbSd,
                         &cbSdReq
                         );
        }
    }

    if( !Status ) {
        return( FALSE );
    }

    //
    // Set the Security on the dest file
    //

    Status = SetFileSecurity(
                 Dest,
                 Si,
                 Sd
                 );

    if ( AllocBuffer ) {
        free( AllocBuffer );
    }

    return ( Status );
}


DWORD
GetSizeOfFile(
    IN LPSTR szFile
    )
{

    HANDLE          hff;
    WIN32_FIND_DATA ffd;
    DWORD           Size  = 0;

    //
    // get find file information and get the size information out of
    // that
    //

    if ((hff = FindFirstFile(szFile, &ffd)) != INVALID_HANDLE_VALUE) {
        Size = ffd.nFileSizeLow;
        FindClose(hff);
    }

    return Size;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值