VirtualBox

Changeset 39729 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Jan 8, 2012 5:29:43 PM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
75590
Message:

Acquire the necessary privileges to perform shutdown if need too. Also, don't leak the log message string.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/win/RTSystemShutdown-win.cpp

    r39728 r39729  
    4343    AssertPtrReturn(pszLogMsg, VERR_INVALID_POINTER);
    4444    AssertReturn(!(fFlags & ~RTSYSTEM_SHUTDOWN_VALID_MASK), VERR_INVALID_PARAMETER);
    45    
     45
    4646    PRTUTF16 pwszLogMsg;
    4747    int rc = RTStrToUtf16(pszLogMsg, &pwszLogMsg);
     
    5252                               ? TRUE : FALSE;
    5353    BOOL  fForceAppsClosed = fFlags & RTSYSTEM_SHUTDOWN_FORCE ? TRUE : FALSE;
     54
     55    /*
     56     * Do the
     57     */
    5458    if (InitiateSystemShutdownW(NULL /*pwszMachineName = NULL = localhost*/,
    5559                                pwszLogMsg,
     
    5761                                fForceAppsClosed,
    5862                                fRebootAfterShutdown))
    59         return (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) == RTSYSTEM_SHUTDOWN_HALT ? VINF_SYS_MAY_POWER_OFF : VINF_SUCCESS;
    60     return RTErrConvertFromWin32(GetLastError());
     63        rc = (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) == RTSYSTEM_SHUTDOWN_HALT ? VINF_SYS_MAY_POWER_OFF : VINF_SUCCESS;
     64    else
     65    {
     66        /* If we failed because of missing privileges, try get the right to
     67           shut down the system and call the api again. */
     68        DWORD dwErr = GetLastError();
     69        rc = RTErrConvertFromWin32(dwErr);
     70        if (dwErr == ERROR_ACCESS_DENIED)
     71        {
     72            HANDLE hToken = NULL;
     73            if (OpenThreadToken(GetCurrentThread(),
     74                                TOKEN_ADJUST_PRIVILEGES,
     75                                TRUE /*OpenAsSelf*/,
     76                                &hToken))
     77                dwErr = NO_ERROR;
     78            else
     79            {
     80                dwErr  = GetLastError();
     81                if (dwErr == ERROR_NO_TOKEN)
     82                {
     83                    if (OpenProcessToken(GetCurrentProcess(),
     84                                         TOKEN_ADJUST_PRIVILEGES,
     85                                         &hToken))
     86                        dwErr = NO_ERROR;
     87                    else
     88                        dwErr = GetLastError();
     89                }
     90            }
     91
     92            if (dwErr == NO_ERROR)
     93            {
     94                union
     95                {
     96                    TOKEN_PRIVILEGES TokenPriv;
     97                    char ab[sizeof(TOKEN_PRIVILEGES) + sizeof(LUID_AND_ATTRIBUTES)];
     98                } u;
     99                u.TokenPriv.PrivilegeCount = 1;
     100                u.TokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
     101                if (LookupPrivilegeValue(NULL /*localhost*/, SE_SHUTDOWN_NAME, &u.TokenPriv.Privileges[0].Luid))
     102                {
     103                    if (AdjustTokenPrivileges(hToken,
     104                                              FALSE /*DisableAllPrivileges*/,
     105                                              &u.TokenPriv,
     106                                              RT_OFFSETOF(TOKEN_PRIVILEGES, Privileges[1]),
     107                                              NULL,
     108                                              NULL) )
     109                    {
     110                        if (InitiateSystemShutdownW(NULL /*pwszMachineName = NULL = localhost*/,
     111                                                    pwszLogMsg,
     112                                                    cSecsTimeout,
     113                                                    fForceAppsClosed,
     114                                                    fRebootAfterShutdown))
     115                            rc = (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) == RTSYSTEM_SHUTDOWN_HALT ? VINF_SYS_MAY_POWER_OFF : VINF_SUCCESS;
     116                        else
     117                        {
     118                            dwErr = GetLastError();
     119                            rc = RTErrConvertFromWin32(dwErr);
     120                        }
     121                    }
     122                    CloseHandle(hToken);
     123                }
     124            }
     125        }
     126    }
     127
     128    RTUtf16Free(pwszLogMsg);
     129    return rc;
    61130}
    62131RT_EXPORT_SYMBOL(RTSystemShutdown);
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette