text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE510_Trapdoor__hostname_based_logic_12.c
  Label Definition File: CWE510_Trapdoor.label.xml
  Template File: point-flaw-12.tmpl.c
  */
  /*
   * @description
   * CWE: 510 Trapdoor
   * Sinks: hostname_based_logic
   *    GoodSink: No host-based logic
   *    BadSink : Different logic if a connection is made from a specific host name
   * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())
   *
   * */
  
  #include "std_testcase.h"
  
  #ifdef _WIN32
  #include <winsock2.h>
  #include <windows.h>
  #include <direct.h>
  #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */
  #define CLOSE_SOCKET closesocket
  #else /* NOT _WIN32 */
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  #include <unistd.h>
  #define INVALID_SOCKET -1
  #define SOCKET_ERROR -1
  #define CLOSE_SOCKET close
  #define SOCKET int
  #endif
  
  #ifdef _WIN32
  #include <ws2tcpip.h>
  #else
  #include <netdb.h>
  #endif
  
  #define ADMIN_MESSAGE "Welcome, admin!"
  #define DEFAULT_MESSAGE "Welcome!"
  
  #ifndef OMITBAD
  
  void CWE510_Trapdoor__hostname_based_logic_12_bad()
  {
      if(globalReturnsTrueOrFalse())
      {
          {
  #ifdef _WIN32
              WSADATA wsaData;
              int wsaDataInit = 0;
  #endif
              struct sockaddr_in service, acceptService;
              int acceptServiceLen = sizeof(acceptService);
              SOCKET listenSocket = INVALID_SOCKET;
              SOCKET acceptSocket = INVALID_SOCKET;
              char hostname[NI_MAXHOST];
              char servInfo[NI_MAXSERV];
              do
              {
  #ifdef _WIN32
                  if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
                  {
                      break;
                  }
                  wsaDataInit = 1;
  #endif
                  listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                  if (listenSocket == INVALID_SOCKET)
                  {
                      break;
                  }
                  memset(&service, 0, sizeof(service));
                  service.sin_family = AF_INET;
                  service.sin_addr.s_addr = INADDR_ANY;
                  service.sin_port = htons(20000);
                  if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
                  {
                      break;
                  }
                  if (listen(listenSocket, 5) == SOCKET_ERROR)
                  {
                      break;
                  }
                  acceptSocket = accept(listenSocket, NULL, NULL);
                  if (acceptSocket == SOCKET_ERROR)
                  {
                      break;
                  }
                  if (getsockname(acceptSocket, (struct sockaddr *)&acceptService, &acceptServiceLen) == -1)
                  {
                      break;
                  }
                  if (getnameinfo((struct sockaddr *)&acceptService,
                                  sizeof(struct sockaddr),
                                  hostname,
                                  NI_MAXHOST, servInfo, NI_MAXSERV, NI_NUMERICSERV) != 0)
                  {
                      break;
                  }
                  /* FLAW: host-based logic */
                  if (strcmp("admin.google.com", hostname) == 0)
                  {
                      if (send(acceptSocket, ADMIN_MESSAGE, strlen(ADMIN_MESSAGE), 0) ==  SOCKET_ERROR)
                      {
                          /* Do not alert user to trapdoor, no action */
                          break;
                      }
                  }
                  else
                  {
                      if (send(acceptSocket, DEFAULT_MESSAGE, strlen(DEFAULT_MESSAGE), 0) == SOCKET_ERROR)
                      {
                          printLine("Send failed!");
                      }
                  }
              }
              while (0);
              if (listenSocket != INVALID_SOCKET)
              {
                  CLOSE_SOCKET(listenSocket);
              }
              if (acceptSocket != INVALID_SOCKET)
              {
                  CLOSE_SOCKET(acceptSocket);
              }
  #ifdef _WIN32
              if (wsaDataInit)
              {
                  WSACleanup();
              }
  #endif
          }
      }
      else
      {
          {
  #ifdef _WIN32
              WSADATA wsaData;
              int wsaDataInit = 0;
  #endif
              struct sockaddr_in service;
              SOCKET listenSocket = INVALID_SOCKET;
              SOCKET acceptSocket = INVALID_SOCKET;
              do
              {
  #ifdef _WIN32
                  if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
                  {
                      break;
                  }
                  wsaDataInit = 1;
  #endif
                  listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                  if (listenSocket == INVALID_SOCKET)
                  {
                      break;
                  }
                  memset(&service, 0, sizeof(service));
                  service.sin_family = AF_INET;
                  service.sin_addr.s_addr = INADDR_ANY;
                  service.sin_port = htons(20000);
                  if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
                  {
                      break;
                  }
                  if (listen(listenSocket, 5) == SOCKET_ERROR)
                  {
                      break;
                  }
                  acceptSocket = accept(listenSocket, NULL, NULL);
                  if (acceptSocket == SOCKET_ERROR)
                  {
                      break;
                  }
                  /* FIX: No host-based logic */
                  if (send(acceptSocket, DEFAULT_MESSAGE, strlen(DEFAULT_MESSAGE), 0) == SOCKET_ERROR)
                  {
                      printLine("Send failed!");
                  }
              }
              while (0);
              if (listenSocket != INVALID_SOCKET)
              {
                  CLOSE_SOCKET(listenSocket);
              }
              if (acceptSocket != INVALID_SOCKET)
              {
                  CLOSE_SOCKET(acceptSocket);
              }
  #ifdef _WIN32
              if (wsaDataInit)
              {
                  WSACleanup();
              }
  #endif
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* good1() uses the GoodSink on both sides of the "if" statement */
  static void good1()
  {
      if(globalReturnsTrueOrFalse())
      {
          {
  #ifdef _WIN32
              WSADATA wsaData;
              int wsaDataInit = 0;
  #endif
              struct sockaddr_in service;
              SOCKET listenSocket = INVALID_SOCKET;
              SOCKET acceptSocket = INVALID_SOCKET;
              do
              {
  #ifdef _WIN32
                  if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
                  {
                      break;
                  }
                  wsaDataInit = 1;
  #endif
                  listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                  if (listenSocket == INVALID_SOCKET)
                  {
                      break;
                  }
                  memset(&service, 0, sizeof(service));
                  service.sin_family = AF_INET;
                  service.sin_addr.s_addr = INADDR_ANY;
                  service.sin_port = htons(20000);
                  if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
                  {
                      break;
                  }
                  if (listen(listenSocket, 5) == SOCKET_ERROR)
                  {
                      break;
                  }
                  acceptSocket = accept(listenSocket, NULL, NULL);
                  if (acceptSocket == SOCKET_ERROR)
                  {
                      break;
                  }
                  /* FIX: No host-based logic */
                  if (send(acceptSocket, DEFAULT_MESSAGE, strlen(DEFAULT_MESSAGE), 0) == SOCKET_ERROR)
                  {
                      printLine("Send failed!");
                  }
              }
              while (0);
              if (listenSocket != INVALID_SOCKET)
              {
                  CLOSE_SOCKET(listenSocket);
              }
              if (acceptSocket != INVALID_SOCKET)
              {
                  CLOSE_SOCKET(acceptSocket);
              }
  #ifdef _WIN32
              if (wsaDataInit)
              {
                  WSACleanup();
              }
  #endif
          }
      }
      else
      {
          {
  #ifdef _WIN32
              WSADATA wsaData;
              int wsaDataInit = 0;
  #endif
              struct sockaddr_in service;
              SOCKET listenSocket = INVALID_SOCKET;
              SOCKET acceptSocket = INVALID_SOCKET;
              do
              {
  #ifdef _WIN32
                  if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
                  {
                      break;
                  }
                  wsaDataInit = 1;
  #endif
                  listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                  if (listenSocket == INVALID_SOCKET)
                  {
                      break;
                  }
                  memset(&service, 0, sizeof(service));
                  service.sin_family = AF_INET;
                  service.sin_addr.s_addr = INADDR_ANY;
                  service.sin_port = htons(20000);
                  if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
                  {
                      break;
                  }
                  if (listen(listenSocket, 5) == SOCKET_ERROR)
                  {
                      break;
                  }
                  acceptSocket = accept(listenSocket, NULL, NULL);
                  if (acceptSocket == SOCKET_ERROR)
                  {
                      break;
                  }
                  /* FIX: No host-based logic */
                  if (send(acceptSocket, DEFAULT_MESSAGE, strlen(DEFAULT_MESSAGE), 0) == SOCKET_ERROR)
                  {
                      printLine("Send failed!");
                  }
              }
              while (0);
              if (listenSocket != INVALID_SOCKET)
              {
                  CLOSE_SOCKET(listenSocket);
              }
              if (acceptSocket != INVALID_SOCKET)
              {
                  CLOSE_SOCKET(acceptSocket);
              }
  #ifdef _WIN32
              if (wsaDataInit)
              {
                  WSACleanup();
              }
  #endif
          }
      }
  }
  
  void CWE510_Trapdoor__hostname_based_logic_12_good()
  {
      good1();
  }
  
  #endif /* OMITGOOD */
  
  /* Below is the main(). It is only used when building this testcase on
     its own for testing or for building a binary to use in testing binary
     analysis tools. It is not used when compiling all the testcases as one
     application, which is how source code analysis tools are tested. */
  
  #ifdef INCLUDEMAIN
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      CWE510_Trapdoor__hostname_based_logic_12_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE510_Trapdoor__hostname_based_logic_12_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  