Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

adjustTokenPrivileges problems

Posted on 1999-07-19
2
Medium Priority
?
1,640 Views
Last Modified: 2013-12-03
     Hi. I'm trying to kick-off a reboot under NT, and this code doesn't seem to work:

procedure tUpgrader._reboot;
var
  oldPri,
  pri        : _token_privileges;
  b          : boolean;
  hToken     : cardinal;
  tiSize     : cardinal; //ti = token information
  hPrivilege : int64;
  r          : integer;
  procedure fatalErr (dc : string);
  var sE : string; iE : integer;
  begin
    iE := getLastError;
    if iE<>0 then sE:=sysErrorMessage (iE);
    raise eUpgrader.create (etVisible, 'tUpgrader._reboot', sE, iE, dc);
  end;
begin
  if OSVersion.dwPlatformId=VER_PLATFORM_WIN32_NT then begin
  (* from the MSDN help for 'exitWindowsEx':
       Windows NT/2000: To shut down or restart the system, the calling process
       must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME
       privilege. For more information about security privileges, see Privileges.*)
  //GRRRRR!!! ;-)

  //get current privileges
    if not lookupPrivilegeValue (nil, 'SeShutdownPrivilege', hPrivilege)
      then fatalErr ('Can''t lookup privilege value for SE_SHUTDOWN_NAME');

    if not openProcessToken (getCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)
      then fatalErr ('Can''t open process'' token.');

    pri.PrivilegeCount := 1;
    pri.Privileges[0].luid:=hToken;
    pri.Privileges[0].attributes:=SE_PRIVILEGE_ENABLED;
    tiSize:=sizeOf (pri);

    adjustTokenPrivileges (hToken, false, pri, tiSize, oldPri, tiSize);
    r:=getLastError;
    if (r<>0) and (r<>ERROR_NOT_ALL_ASSIGNED) then fatalErr ('Couldn''t adjust token privileges.');
  end;

  b:=exitWindowsEx (EWX_REBOOT or EWX_FORCE,0);
  if not b then begin
  //hmm, the end-user's log-in-account doesn't have administrator rights to
  //actually force a reboot. Better pop up a window telling them that a manual
  //reboot might be necessary.
    b:=exitWindowsEx (EWX_REBOOT, 0);
  end;
  if b
    then _log.report ('forced reboot, NOW.')
    else begin
      _log.report ('Couldn''t reboot, getLastError returns : '+
                   sysErrorMessage (getLastError));
      messageDlg ('You will have to reboot manually.', mtWarning, [mbOK],0);
    end;
end;

This code runs just fine, but adjustTokenPrivileges will return with 1300. GetLastError returns 'Not all privileges referenced are assigned to the caller'.

The help on adjustTokenPrivileges says
(http://msdn.microsoft.com/library/sdkdoc/winbase/accctrl_6xbn.htm)

The AdjustTokenPrivileges function cannot add new privileges to the access token. It can only enable or disable the token's existing privileges. To determine the token's privileges, call the GetTokenInformation function.

But MSDN says on 'privileges' (http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/sdkdoc/winbase/accctrl_42pf.htm)

Before you can perform a privileged operation, you must first enable the necessary privileges in your access token. To do this, call the OpenThreadToken function to get a handle to your primary or impersonation access token, then call the AdjustTokenPrivileges function to enable the necessary privileges. After performing the privileged operation, call AdjustTokenPrivileges again to disable the privileges. For sample code that enables and disables a token's privileges, see Enabling and Disabling Privileges.

I don't get this paradox... BTW, I"m adjusting the *process*'s access token because that is what MSDN showed me at http://msdn.microsoft.com/library/sdkdoc/sysmgmt/shutdown_7z8u.htm
0
Comment
Question by:lowlevel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 1

Expert Comment

by:pavel030297
ID: 1403269
This means that you don't have that priviledge. Priviledge can be enabled only if you have it. You can check in User Manager Policies\User rights menu if you have a priviledge to shutdown the system. If you don't - you need to grant yourself this priviledge either in User Manager or by LSA functions, which are not fully documented. You can find some examples of their use in MSDN.
0
 
LVL 1

Accepted Solution

by:
pavel030297 earned 200 total points
ID: 1403270
You have some error in your code.
You make the following assignment:
       pri.Privileges[0].luid:=hToken;
which is wrong.
You should get luid using LookupPriviledgeValue function:
   LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&pri.Priviledges[0].luid);
(It's C code, I hope you can convert it)

I
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question