I need to write a program to add an entry to Local Security Settings -> User Rights Assignment -> Act as part of the operating system.

There is user in the network called \\lol_dev\yapa I need to add it to the computer that I am running the program (\\lol_dev\janaka).

The code that I have written to do it is


VB Code:
  1. #include "winNT.h"
  2. #include "NTSecAPI.h"
  3. #include "lm.h"
  4. #include "assert.h"
  5. #pragma hdrstop
  6. #pragma comment( lib, "netapi32.lib" )
  7.  
  8.  
  9. void CThirdDlg::AddUser() {
  10.  
  11.  
  12.     //char localgroup_name[100];
  13.     char domain_name[100];
  14.     DWORD domname_len = 100;
  15.     char psid_buffer[1024];
  16.     PSID psid = (PSID) psid_buffer;
  17.     DWORD sid_length = 1024;
  18.  
  19.     SID_NAME_USE acc_type;
  20.  
  21.  
  22.     BOOL fRet =  LookupAccountName (NULL, "\\lol_dev\yapa", psid,
  23.                   &sid_length, domain_name, &domname_len,
  24.                   &acc_type);
  25.    
  26.     ASSERT(fRet != 0);
  27.  
  28.     LSA_HANDLE          hPolicy = GetPolicyHandle();
  29.  
  30.    
  31.     LSA_UNICODE_STRING lsastrPrivs[1] = { 0 };
  32.  
  33.     //lsastrPrivs[0].Buffer = (PWSTR)SE_TCB_NAME;
  34.     lsastrPrivs[0].Buffer = (PWSTR)SE_SYSTEMTIME_NAME;
  35.     lsastrPrivs[0].Length = lstrlen((LPCSTR)lsastrPrivs[0].Buffer) * sizeof(WCHAR);
  36.     lsastrPrivs[0].MaximumLength = lsastrPrivs[0].Length + sizeof(WCHAR);
  37.  
  38.    
  39.     // this is the place I get the error
  40. NTSTATUS ntStatus = LsaAddAccountRights(hPolicy, psid, lsastrPrivs, 1);
  41.     ULONG lErr = LsaNtStatusToWinError(ntStatus);
  42.     CString msg = "Success";
  43.     if (lErr >0)
  44.     {
  45.        
  46.         msg.Format("Error %d", lErr);
  47.     }
  48.     AfxMessageBox( msg);
  49.  
  50. }
  51.  
  52. #define TARGET_SYSTEM_NAME L"JANAKA"
  53.  
  54.  
  55. LSA_HANDLE GetPolicyHandle()
  56. {
  57.   LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  58.   WCHAR SystemName[] = TARGET_SYSTEM_NAME;
  59.   USHORT SystemNameLength;
  60.   LSA_UNICODE_STRING lusSystemName;
  61.   NTSTATUS ntsResult;
  62.   LSA_HANDLE lsahPolicyHandle;
  63.  
  64.   // Object attributes are reserved, so initialize to zeroes.
  65.   ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
  66.  
  67.   //Initialize an LSA_UNICODE_STRING to the server name.
  68.   SystemNameLength = wcslen(SystemName);
  69.   lusSystemName.Buffer = SystemName;
  70.   lusSystemName.Length = SystemNameLength * sizeof(WCHAR);
  71.   lusSystemName.MaximumLength = (SystemNameLength+1) * sizeof(WCHAR);
  72.  
  73.   // Get a handle to the Policy object.
  74.   ntsResult = LsaOpenPolicy(
  75.         &lusSystemName,    //Name of the target system.
  76.         &ObjectAttributes, //Object attributes.
  77.         POLICY_ALL_ACCESS, //Desired access permissions.
  78.         &lsahPolicyHandle  //Receives the policy handle.
  79.     );
  80.  
  81.   if (ntsResult != 0 )//STATUS_SUCCESS)
  82.   {
  83.     // An error occurred. Display it as a win32 error code.
  84.     wprintf(L"OpenPolicy returned %lu\n",
  85.       LsaNtStatusToWinError(ntsResult));
  86.     return NULL;
  87.   }
  88.   return lsahPolicyHandle;
  89. }

when I run the program in the line

VB Code:
  1. NTSTATUS ntStatus = LsaAddAccountRights(hPolicy, psid, lsastrPrivs, 1);
I get the error the error code is 1313
How do I fix it?