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

int FUN_004067a4(void)

{
  bool bVar1;
  undefined *__ptr;
  char *pcVar2;
  int iVar3;
  char *pcVar4;
  int iVar5;
  FILE *pFVar6;
  int iVar7;
  int iVar8;
  undefined4 uVar9;
  undefined4 uVar10;
  int *piVar11;
  void *pvVar12;
  char *pcVar13;
  int in_a3;
  undefined4 uVar14;
  code *pcVar15;
  bool bVar16;
  char local_1b0 [80];
  stat sStack_160;
  stat sStack_c8;
  int local_30;
  
  pcVar2 = (char *)sobj_new();
  iVar3 = sobj_new();
  if (((pcVar2 == (char *)0x0) || (iVar3 == 0)) ||
     (pcVar4 = getenv("REQUEST_METHOD"), pcVar4 == (char *)0x0)) {   //possible issue identified - semgrep
    iVar5 = -1;
    goto LAB_004070d8;
  }
  uVar14 = 0x3a0000;
  if (in_a3 != 1) {
    uVar14 = 0x100000;
  }
  iVar5 = strcasecmp(pcVar4,"POST");
  if (iVar5 != 0) {
    cgibin_print_http_status(400,"unsupported HTTP request","unsupported HTTP request");
    iVar5 = -1;
    goto LAB_004070d8;
  }
  iVar5 = cgibin_parse_request(&LAB_00407228,0,uVar14);
  if (-1 < iVar5) {
    pcVar4 = (char *)FUN_00405b10("REPORT_METHOD");
    bVar16 = true;
    if (pcVar4 != (char *)0x0) {
      iVar7 = strcasecmp(pcVar4,"xml");
      bVar16 = iVar7 != 0;
    }
    iVar7 = FUN_00405b10("REPORT");
    if (iVar7 == 0) {
      pcVar4 = getenv("HTTP_REFERER")   //possible issue identified - semgrep
      pcVar15 = strdup;
    }
    else {
      sobj_add_string(pcVar2,"http://");
      pcVar4 = getenv("HTTP_HOST")   //possible issue identified - semgrep
      sobj_add_string(pcVar2,pcVar4);
      pcVar4 = getenv("SERVER_PORT")   //possible issue identified - semgrep
      iVar8 = strcasecmp(pcVar4,"80");
      if (iVar8 != 0) {
        sobj_add_char(pcVar2,0x3a);
        pcVar4 = getenv("SERVER_PORT")   //possible issue identified - semgrep
        sobj_add_string(pcVar2,pcVar4);
      }
      sobj_add_char(pcVar2,0x2f);
      sobj_add_string(pcVar2,iVar7);
      pcVar15 = sobj_strdup;
      pcVar4 = pcVar2;
    }
    pvVar12 = (void *)(*pcVar15)(pcVar4);
    pcVar4 = (char *)FUN_00405b10("DELAY");
    local_30 = 0;
    if (pcVar4 != (char *)0x0) {
      local_30 = atoi(pcVar4);
    }
    iVar7 = sess_ispoweruser();
    if (iVar7 == 0) {
      cgibin_print_http_resp(bVar16,pvVar12,&DAT_0041e01c,"ERR_UNAUTHORIZED_SESSION",0xffffffff,"");
      fwupgrade_log(3);
LAB_00406ca4:
      iVar5 = -1;
    }
    else {
      uVar14 = sess_validate();
      pFVar6 = fopen("/etc/config/image_sign","r")   //possible issue identified - semgrep
      if (pFVar6 == (FILE *)0x0) goto LAB_004070b8;
      pcVar4 = fgets(&DAT_00430de0,0x80,pFVar6)   //possible issue identified - semgrep
      if (pcVar4 == (char *)0x0) {
        cgibin_print_http_resp(bVar16,pvVar12,&DAT_0041e01c,"ERR_READ_SIGN_FAIL",0xffffffff,"");
        goto LAB_00406ca4;
      }
      fclose(pFVar6);
      cgibin_reatwhite(&DAT_00430de0);
      if (in_a3 == 1) {
        iVar7 = stat("/var/run/fwseama.lock",&sStack_160)   //possible issue identified - semgrep
        if ((iVar7 == -1) && (iVar7 = open("/var/run/fwseama.lock",0x302,0x1ff), iVar7 != 0)) {   //possible issue identified - semgrep
          iVar8 = lockf(iVar7,1,0);
          bVar1 = true;
          if (iVar8 == 0) {
            sobj_free(pcVar2);
            iVar5 = FUN_00405bb8(pcVar2,1);
            FUN_0040667c(iVar3);
            if (iVar5 == 0) {
              pcVar4 = "SUCCESS";
            }
            else {
              pcVar4 = "FAIL";
            }
            uVar9 = sobj_get_string(pcVar2);
            uVar10 = sobj_get_string(iVar3);
            cgibin_print_http_resp(bVar16,pvVar12,pcVar4,uVar9,uVar14,uVar10);
            if (iVar5 == 0) {
              sprintf(local_1b0 + 0x10,"preupdate:%d:event PREFWUPDATE",local_30)   //possible issue identified - semgrep
              xmldbc_timer(0,0,local_1b0 + 0x10);
              bVar1 = false;
            }
            else {
              fwupgrade_log(2);
              bVar1 = false;
            }
          }
          close(iVar7);
          if (iVar5 != 0) {
            unlink("/var/run/fwseama.lock")   //possible issue identified - semgrep
          }
          iVar5 = 0;
          if (!bVar1) goto LAB_004070b8;
        }
        FUN_0040667c(iVar3);
        uVar9 = sobj_get_string(iVar3);
        pcVar13 = "FAIL";
        pcVar4 = "ERR_ANOTHER_FWUP_PROGRESS";
LAB_00406f54:
        cgibin_print_http_resp(bVar16,pvVar12,pcVar13,pcVar4,uVar14,uVar9);
      }
      else {
        if (in_a3 != 2) {
          iVar5 = 0;
          if (in_a3 == 0) {
            sobj_free(pcVar2);
            iVar5 = FUN_00405bb8(pcVar2,0);
            FUN_0040667c(iVar3);
            if (iVar5 == 0) {
              pcVar13 = "SUCCESS";
            }
            else {
              pcVar13 = "FAIL";
            }
            pcVar4 = (char *)sobj_get_string(pcVar2);
            uVar9 = sobj_get_string(iVar3);
            goto LAB_00406f54;
          }
          goto LAB_004070b8;
        }
        sobj_free(pcVar2);
        system("/etc/scripts/dlcfg_hlper.sh")   //possible issue identified - semgrep
        pFVar6 = fopen("/htdocs/web/docs/config.bin","r")   //possible issue identified - semgrep
        if ((pFVar6 == (FILE *)0x0) ||
           (iVar5 = stat("/htdocs/web/docs/config.bin",&sStack_c8), iVar5 != 0)) {   //possible issue identified - semgrep
          cgibin_print_http_status(0x194,0,0);
          iVar5 = 0;
          if (pFVar6 == (FILE *)0x0) goto LAB_004070b8;
        }
        else {
          puts("HTTP/1.1 200 OK\r");
          puts("Content-Type: application/octet-stream\r");
          printf("Content-Disposition: attachment; filename=\"%s\"\r\n","config.bin");
          printf("Content-Length: %d\r\n\r\n",sStack_c8.st_blocks);
          iVar5 = 0;
          while (bVar16 = iVar5 < sStack_c8.st_blocks, iVar5 = iVar5 + 1, bVar16) {
            iVar7 = fgetc(pFVar6)   //possible issue identified - semgrep
            fputc(iVar7,_stdout);
          }
        }
        fclose(pFVar6);
      }
      iVar5 = 0;
    }
LAB_004070b8:
    if (pvVar12 != (void *)0x0) {
      free(pvVar12);
    }
    goto LAB_004070d8;
  }
  if (iVar5 == -100) {
    pcVar4 = (char *)FUN_00405b10("REPORT");
    if (pcVar4 == (char *)0x0) {
      pFVar6 = fopen("/htdocs/web/info.php","r")   //possible issue identified - semgrep
      if (pFVar6 == (FILE *)0x0) {
        local_1b0[0] = '\0';
        local_1b0[1] = '\0';
        local_1b0[2] = '\0';
        local_1b0[3] = '\0';
        local_1b0[4] = '\0';
        local_1b0[5] = '\0';
        local_1b0[6] = '\0';
        local_1b0[7] = '\0';
        local_1b0[8] = '\0';
        local_1b0[9] = '\0';
        local_1b0[10] = '\0';
        local_1b0[0xb] = '\0';
        local_1b0[0xc] = '\0';
        local_1b0[0xd] = '\0';
        local_1b0[0xe] = '\0';
        local_1b0[0xf] = '\0';
        pcVar4 = getenv("CONTENT_LENGTH")   //possible issue identified - semgrep
        pcVar13 = local_1b0;
        iVar7 = atoi(pcVar4);
        sprintf(pcVar13,"%d",iVar7)   //possible issue identified - semgrep
        pcVar4 = local_1b0 + 8;
        sprintf(pcVar4,"%d",uVar14)   //possible issue identified - semgrep
        uVar14 = 0x19d;
        goto LAB_004069d0;
      }
      fclose(pFVar6);
      pcVar4 = "info.php";
    }
    FUN_0040667c(iVar3);
    uVar14 = sobj_get_string(iVar3);
    cgibin_print_http_resp(1,pcVar4,&DAT_0041e01c,"ERR_REQ_TOO_LONG",0,uVar14);
  }
  else {
    pcVar4 = "unsupported HTTP request";
    uVar14 = 400;
    pcVar13 = "unsupported HTTP request";
LAB_004069d0:
    cgibin_print_http_status(uVar14,pcVar4,pcVar13);
  }
LAB_004070d8:
  if (DAT_00430e60 != 0) {
    free((void *)DAT_00430e60);
  }
  if (pcVar2 != (char *)0x0) {
    sobj_del(pcVar2);
  }
  if (iVar3 != 0) {
    sobj_del(iVar3);
  }
  while (__ptr = PTR_LOOP_00430420, (undefined **)PTR_LOOP_00430420 != &PTR_LOOP_00430420) {
    piVar11 = *(int **)(PTR_LOOP_00430420 + 4);
    iVar3 = *(int *)PTR_LOOP_00430420;
    pvVar12 = *(void **)(PTR_LOOP_00430420 + 8);
    *piVar11 = iVar3;
    *(int **)(iVar3 + 4) = piVar11;
    *(undefined4 *)__ptr = 0;
    *(undefined4 *)(__ptr + 4) = 0;
    if (pvVar12 != (void *)0x0) {
      free(pvVar12);
    }
    if (*(void **)(__ptr + 0xc) != (void *)0x0) {
      free(*(void **)(__ptr + 0xc));
    }
    free(__ptr);
  }
  cgibin_clean_tempfiles();
  return iVar5;
}