// File: tylerkaraszewski[P]Argyle/main.cpp
// Function: setUser(char const*, Logger&)
bool setUser(const char* runAs, Logger& logger)
{
  bool retval = true;
  struct passwd pwd;
  struct passwd* pwdPtr = 0;

  size_t bufsize = 32;
  char* buffer = static_cast<char*>(malloc(bufsize));
  if (buffer == NULL)
  {
    logger.logError("Setuser malloc error: Out of Memory");
    return false;
  }
  int e;
  while ((e = getpwnam_r(runAs, &pwd, buffer, bufsize, &pwdPtr)) == ERANGE)
  {
    bufsize *= 2;
    // this needs to do a better job of freeing things if it fails.
    buffer = static_cast<char*>(realloc(static_cast<void*>(buffer), bufsize));
    if (buffer == NULL)
    {
      logger.logError("Setuser realloc error: Out of Memory");
      return false;
    }
  }
  if (e != 0)
  {
    logger.logError("Getpwdnam_r error(" + Utils::llToString(errno) + "): " + strerror(errno));
    retval = false;
  }
  else if (pwdPtr == 0)
  {
    logger.logError(string("Getpwdnam_r failed, couldn't find user ") + runAs);
    retval = false;
  }

  // Haven't failed yet.
  if (retval)
  {
    // Set group first or we lose the abilty to after we're not superuser.
    if (setegid(pwd.pw_gid) == -1)
    {
      logger.logError("Setegid error(" + Utils::llToString(errno) + "): " + strerror(errno));
      retval = false;
    }
    else if (seteuid(pwd.pw_uid) == -1)
    {
      logger.logError("Seteuid error(" + Utils::llToString(errno) + "): " + strerror(errno));
      retval = false;
    }
  }
  free(buffer);
  return retval;
}

// Assembly:
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rdi, 0x2e8(%rsp)
movq %rsi, 0x2e0(%rsp)
movb $0x1, 0x2df(%rsp)
movq $0x0, 0x2a0(%rsp)
movq $0x20, 0x298(%rsp)
movq 0x298(%rsp), %rdi
callq 0x4530
movq %rax, 0x290(%rsp)
cmpq $0x0, 0x290(%rsp)
jne 0x9d89
movq 0x2e0(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x26f(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x46c0
movq 0x60(%rsp), %rdx
leaq 0x947a(%rip), %rsi      # 0x13170
leaq 0x270(%rsp), %rdi
callq 0x6450
jmp 0x9d05
movq 0x58(%rsp), %rdi
leaq 0x270(%rsp), %rsi
callq 0x10f40
jmp 0x9d19
leaq 0x270(%rsp), %rdi
callq 0x4788
leaq 0x26f(%rsp), %rdi
callq 0x4400
movb $0x0, 0x2f7(%rsp)
jmp 0xa47e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0x9d77
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
leaq 0x270(%rsp), %rdi
callq 0x4788
leaq 0x26f(%rsp), %rdi
callq 0x4400
jmp 0xa48f
jmp 0x9d8b
movq 0x2e8(%rsp), %rdi
movq 0x290(%rsp), %rdx
movq 0x298(%rsp), %rcx
leaq 0x2a8(%rsp), %rsi
leaq 0x2a0(%rsp), %r8
callq 0x46a0
movl %eax, 0x258(%rsp)
cmpl $0x22, %eax
jne 0x9eca
movq 0x298(%rsp), %rax
shlq %rax
movq %rax, 0x298(%rsp)
movq 0x290(%rsp), %rdi
movq 0x298(%rsp), %rsi
callq 0x4660
movq %rax, 0x290(%rsp)
cmpq $0x0, 0x290(%rsp)
jne 0x9ec5
movq 0x2e0(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x237(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x46c0
movq 0x50(%rsp), %rdx
leaq 0x9362(%rip), %rsi      # 0x13194
leaq 0x238(%rsp), %rdi
callq 0x6450
jmp 0x9e41
movq 0x48(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x10f40
jmp 0x9e55
leaq 0x238(%rsp), %rdi
callq 0x4788
leaq 0x237(%rsp), %rdi
callq 0x4400
movb $0x0, 0x2f7(%rsp)
jmp 0xa47e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0x9eb3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
leaq 0x238(%rsp), %rdi
callq 0x4788
leaq 0x237(%rsp), %rdi
callq 0x4400
jmp 0xa48f
jmp 0x9d8b
cmpl $0x0, 0x258(%rsp)
je 0xa042
movq 0x2e0(%rsp), %rax
movq %rax, 0x38(%rsp)
callq 0x4060
movslq (%rax), %rsi
leaq 0x1b0(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x10f70
movq 0x40(%rsp), %rdx
leaq 0x92ae(%rip), %rsi      # 0x131b9
leaq 0x1d0(%rsp), %rdi
callq 0x6230
jmp 0x9f1a
leaq 0x90f1(%rip), %rdx      # 0x13012
leaq 0x1f0(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x61e0
jmp 0x9f38
callq 0x4060
movl (%rax), %edi
callq 0x4150
movq %rax, %rdx
leaq 0x210(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x61e0
jmp 0x9f5e
movq 0x38(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0x10f40
jmp 0x9f72
leaq 0x210(%rsp), %rdi
callq 0x4788
leaq 0x1f0(%rsp), %rdi
callq 0x4788
leaq 0x1d0(%rsp), %rdi
callq 0x4788
leaq 0x1b0(%rsp), %rdi
callq 0x4788
movb $0x0, 0x2df(%rsp)
jmp 0xa15d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa030
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa023
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa016
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
leaq 0x210(%rsp), %rdi
callq 0x4788
leaq 0x1f0(%rsp), %rdi
callq 0x4788
leaq 0x1d0(%rsp), %rdi
callq 0x4788
leaq 0x1b0(%rsp), %rdi
callq 0x4788
jmp 0xa48f
cmpq $0x0, 0x2a0(%rsp)
jne 0xa15b
movq 0x2e0(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x16f(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x46c0
movq 0x30(%rsp), %rdx
leaq 0x9150(%rip), %rsi      # 0x131cc
leaq 0x170(%rsp), %rdi
callq 0x6450
jmp 0xa08b
movq 0x2e8(%rsp), %rdx
leaq 0x190(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x61e0
jmp 0xa0aa
movq 0x28(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x10f40
jmp 0xa0be
leaq 0x190(%rsp), %rdi
callq 0x4788
leaq 0x170(%rsp), %rdi
callq 0x4788
leaq 0x16f(%rsp), %rdi
callq 0x4400
movb $0x0, 0x2df(%rsp)
jmp 0xa15b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa149
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa13c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x4788
leaq 0x170(%rsp), %rdi
callq 0x4788
leaq 0x16f(%rsp), %rdi
callq 0x4400
jmp 0xa48f
jmp 0xa15d
testb $0x1, 0x2df(%rsp)
je 0xa461
movl 0x2bc(%rsp), %edi
callq 0x4510
cmpl $-0x1, %eax
jne 0xa2ea
movq 0x2e0(%rsp), %rax
movq %rax, 0x18(%rsp)
callq 0x4060
movslq (%rax), %rsi
leaq 0xe8(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x10f70
movq 0x20(%rsp), %rdx
leaq 0x9041(%rip), %rsi      # 0x131f4
leaq 0x108(%rsp), %rdi
callq 0x6230
jmp 0xa1c2
leaq 0x8e49(%rip), %rdx      # 0x13012
leaq 0x128(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x61e0
jmp 0xa1e0
callq 0x4060
movl (%rax), %edi
callq 0x4150
movq %rax, %rdx
leaq 0x148(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x61e0
jmp 0xa206
movq 0x18(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x10f40
jmp 0xa21a
leaq 0x148(%rsp), %rdi
callq 0x4788
leaq 0x128(%rsp), %rdi
callq 0x4788
leaq 0x108(%rsp), %rdi
callq 0x4788
leaq 0xe8(%rsp), %rdi
callq 0x4788
movb $0x0, 0x2df(%rsp)
jmp 0xa45f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa2d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa2cb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa2be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x4788
leaq 0x128(%rsp), %rdi
callq 0x4788
leaq 0x108(%rsp), %rdi
callq 0x4788
leaq 0xe8(%rsp), %rdi
callq 0x4788
jmp 0xa48f
movl 0x2b8(%rsp), %edi
callq 0x41a0
cmpl $-0x1, %eax
jne 0xa45d
movq 0x2e0(%rsp), %rax
movq %rax, 0x8(%rsp)
callq 0x4060
movslq (%rax), %rsi
leaq 0x68(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x10f70
movq 0x10(%rsp), %rdx
leaq 0x8ed4(%rip), %rsi      # 0x13203
leaq 0x88(%rsp), %rdi
callq 0x6230
jmp 0xa33e
leaq 0x8ccd(%rip), %rdx      # 0x13012
leaq 0xa8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x61e0
jmp 0xa35c
callq 0x4060
movl (%rax), %edi
callq 0x4150
movq %rax, %rdx
leaq 0xc8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x61e0
jmp 0xa382
movq 0x8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x10f40
jmp 0xa396
leaq 0xc8(%rsp), %rdi
callq 0x4788
leaq 0xa8(%rsp), %rdi
callq 0x4788
leaq 0x88(%rsp), %rdi
callq 0x4788
leaq 0x68(%rsp), %rdi
callq 0x4788
movb $0x0, 0x2df(%rsp)
jmp 0xa45d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa451
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa444
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
jmp 0xa437
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x260(%rsp)
movl %eax, 0x25c(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x4788
leaq 0xa8(%rsp), %rdi
callq 0x4788
leaq 0x88(%rsp), %rdi
callq 0x4788
leaq 0x68(%rsp), %rdi
callq 0x4788
jmp 0xa48f
jmp 0xa45f
jmp 0xa461
movq 0x290(%rsp), %rdi
callq 0x44a0
movb 0x2df(%rsp), %al
andb $0x1, %al
movb %al, 0x2f7(%rsp)
movb 0x2f7(%rsp), %al
andb $0x1, %al
addq $0x2f8, %rsp            # imm = 0x2F8
retq
movq 0x260(%rsp), %rdi
callq 0x46b0
nopl (%rax)