/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2022 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/calls/calls.h"
#include "libc/intrin/strace.internal.h"
#include "libc/nt/enum/accessmask.h"
#include "libc/nt/enum/securityimpersonationlevel.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"
#include "libc/nt/privilege.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/luid.h"
#include "libc/nt/struct/tokenprivileges.h"

static bool32 SetPrivilegeNt(int64_t hToken, const char16_t *lpwPrivilege,
                             bool32 bEnable) {
  struct NtLuid luid;
  uint32_t cbPrevious;
  struct NtTokenPrivileges tp, tpPrevious;
  cbPrevious = sizeof(struct NtTokenPrivileges);
  if (!LookupPrivilegeValue(0, lpwPrivilege, &luid)) {
    STRACE("LookupPrivilegeValue() failed");
    return false;
  }
  tp.PrivilegeCount = 1;
  tp.Privileges[0].Luid = luid;
  tp.Privileges[0].Attributes = 0;
  if (!AdjustTokenPrivileges(hToken, false, &tp, sizeof(tp), &tpPrevious,
                             &cbPrevious)) {
    STRACE("AdjustTokenPrivileges() failed");
    return false;
  }
  tpPrevious.PrivilegeCount = 1;
  tpPrevious.Privileges[0].Luid = luid;
  if (bEnable) {
    tpPrevious.Privileges[0].Attributes |= kNtSePrivilegeEnabled;
  } else {
    tpPrevious.Privileges[0].Attributes ^=
        kNtSePrivilegeEnabled & tpPrevious.Privileges[0].Attributes;
  }
  if (!AdjustTokenPrivileges(hToken, false, &tpPrevious, cbPrevious, 0, 0)) {
    STRACE("AdjustTokenPrivileges() failed");
    return false;
  }
  return true;
}

static int64_t GetCurrentProcessSecurityToken(void) {
  int64_t hToken;
  if (OpenProcessToken(GetCurrentProcess(),
                       kNtTokenAdjustPrivileges | kNtTokenQuery, &hToken)) {
    return hToken;
  } else if (GetLastError() == kNtErrorNoToken) {
    if (ImpersonateSelf(kNtSecurityImpersonation)) {
      if (OpenProcessToken(GetCurrentProcess(),
                           kNtTokenAdjustPrivileges | kNtTokenQuery, &hToken)) {
        return hToken;
      } else {
        STRACE("OpenProcessToken() failed");
      }
    } else {
      STRACE("ImpersonateSelf() failed");
    }
  } else {
    STRACE("OpenProcessToken() failed");
  }
  return 0;
}

bool32 ElevateSeDebugPrivilege(void) {
  int64_t hToken;
  if (!(hToken = GetCurrentProcessSecurityToken())) return false;
  SetPrivilegeNt(hToken, u"SeDebugPrivilege", true);
  RevertToSelf();
  CloseHandle(hToken);
  return true;
}
