/* WARNING: Globals starting with '_' overlap smaller symbols at the same address */

int hnap_main(void)

{
  bool bVar1;
  char *pcVar2;
  char *pcVar3;
  int iVar4;
  FILE *__stream;
  size_t sVar5;
  char *pcVar6;
  undefined4 uVar7;
  int iVar8;
  char *__format;
  uint uVar9;
  int iVar10;
  undefined *puVar11;
  char *local_1b8;
  char local_1b4 [388];
  char *local_30;
  
  memset(local_1b4 + 0x80,0,0x100)   //possible issue identified - semgrep
  pcVar2 = getenv("HTTP_AUTHORIZATION")   //possible issue identified - semgrep
  pcVar3 = getenv("HTTP_SOAPACTION")   //possible issue identified - semgrep
  local_30 = getenv("REQUEST_METHOD")   //possible issue identified - semgrep
  if (pcVar3 != (char *)0x0) {
    local_1b8 = (char *)0x0;
    if (pcVar2 != (char *)0x0) {
      iVar4 = strncasecmp(pcVar2,"basic ",6);
      pcVar2 = pcVar2 + 6;
      if (iVar4 == 0) {
        do {
          pcVar6 = pcVar2;
          pcVar2 = pcVar6 + 1;
        } while (*pcVar6 == ' ');
        __stream = fopen("/var/etc/hnapasswd","r")   //possible issue identified - semgrep
        if (__stream != (FILE *)0x0) {
          sVar5 = strlen(pcVar6);
          iVar4 = base64decode(pcVar6,sVar5,&local_1b8)   //possible issue identified - semgrep
          pcVar2 = local_1b8;
          if (-1 < iVar4) {
            pcVar6 = strchr(local_1b8,0x3a);
            if (pcVar6 == (char *)0x0) {
              if (pcVar2 != (char *)0x0) {
                free(pcVar2);
              }
            }
            else {
              *pcVar6 = '\0';
              uVar9 = 0;
              bVar1 = false;
              while (iVar4 = fgetc(__stream), iVar4 != -1) {   //possible issue identified - semgrep
                if (iVar4 == 10) {
                  if (uVar9 < 0x80) {
                    local_1b4[uVar9] = '\0';
                    pcVar2 = strchr(local_1b4,0x3a);
                    if (pcVar2 != (char *)0x0) {
                      *pcVar2 = '\0';
                      iVar4 = strcmp(local_1b8,local_1b4);
                      if (iVar4 == 0) {
                        iVar4 = strcmp(pcVar2 + 1,pcVar6 + 1);
                        iVar10 = 0;
                        if (iVar4 == 0) goto LAB_0040f3b8;
                      }
                    }
                  }
                  uVar9 = 0;
                  bVar1 = false;
                }
                else {
                  if ((bVar1) && (uVar9 < 0x80)) {
                    local_1b4[uVar9] = '\r';
                    uVar9 = uVar9 + 1;
                  }
                  bVar1 = iVar4 == 0xd;
                  if ((!bVar1) && (uVar9 < 0x80)) {
                    local_1b4[uVar9] = (char)iVar4;
                    uVar9 = uVar9 + 1;
                  }
                }
              }
              iVar10 = -1;
LAB_0040f3b8:
              fclose(__stream);
              if (local_1b8 != (char *)0x0) {
                free(local_1b8);
              }
              if (iVar10 == 0) goto LAB_0040f4ac;
            }
          }
        }
      }
    }
    cgibin_parse_request(0,0,0);
    puts("HTTP/1.1 401 Not Authorized\r");
    puts("WWW-Authenticate: Basic realm=\"HNAP 1.0\"\r");
    puts("Content-Type: text/html\r\n\r");
    puts("<title>1 Not Authorized</title>\r");
    puts("<h1>1 Not Authorized</h1>\r");
    puts("You need proper authorization to use this resource.\r\n\r");
    iVar10 = -2;
    goto LAB_0040f710;
  }
  pcVar3 = "http://purenetworks.com/HNAP1/GetDeviceSettings";
LAB_0040f4ac:
  pcVar2 = strrchr(pcVar3,0x2f);
  pcVar3 = pcVar2 + 1;
  if (pcVar2 == (char *)0x0) {
    cgibin_parse_request(0,0,0);
    iVar10 = -3;
    goto LAB_0040f710;
  }
  sVar5 = strlen(pcVar3);
  if (pcVar3[sVar5 - 1] == '\"') {   //possible issue identified - semgrep
    pcVar3[sVar5 - 1] = '\0'   //possible issue identified - semgrep
  }
  iVar4 = open("/var/run/hnap.lock",0x101,0x1a4)   //possible issue identified - semgrep
  if ((iVar4 < 0) || (lockf(iVar4,1,0), iVar4 == 0)) {
    iVar10 = 0;
    goto LAB_0040f710;
  }
  iVar10 = strcasecmp(local_30,"POST");
  if (iVar10 == 0) {
    DAT_00432300 = sobj_new();
    iVar10 = cgibin_parse_request(&LAB_0040ee00,0,0x10000);
    if (iVar10 < 0) goto LAB_0040f710;
    iVar10 = -1;
    uVar7 = sobj_get_string(DAT_00432300);
    iVar8 = FUN_0040efd4(pcVar3,uVar7,"/runtime/hnap/");
    if (iVar8 == 0) goto LAB_0040f5e8;
  }
  else {
LAB_0040f5e8:
    iVar10 = 0;
  }
  pcVar6 = local_1b4 + 0x80;
  puVar11 = &DAT_0041fe24;
  pcVar2 = pcVar3;
  sprintf(pcVar6,"%s%s.php\nShellPath=%s%s.sh","/etc/templates/hnap/",pcVar3,&DAT_0041fe24,pcVar3)   //possible issue identified - semgrep
  xmldbc_ephp(0,0,pcVar6,_stdout);
  strcpy(pcVar6,pcVar3)   //possible issue identified - semgrep
  iVar8 = FUN_0040eb80(pcVar6,"/etc/templates/hnap//.shell_action");
  if (iVar8 == 0) {
    __format = "sh %s%s.sh > /dev/console";
  }
  else {
    __format = "sh %s%s.sh > /dev/console &";
  }
  sprintf(pcVar6,__format,&DAT_0041fe24,pcVar3,puVar11,pcVar2)   //possible issue identified - semgrep
  system(local_1b4 + 0x80)   //possible issue identified - semgrep
  lockf(iVar4,2,0);
  close(iVar4);
  unlink("/var/run/hnap.lock")   //possible issue identified - semgrep
LAB_0040f710:
  if (DAT_00432300 != 0) {
    sobj_del();
  }
  return iVar10;
}