text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE319_Plaintext_Tx_Sensitive_Info__w32_wchar_t_connect_socket_10.c
  Label Definition File: CWE319_Plaintext_Tx_Sensitive_Info__w32.label.xml
  Template File: sources-sink-10.tmpl.c
  */
  /*
   * @description
   * CWE: 319 Plaintext Transmission of Sensitive Information
   * BadSource: connect_socket Read the password using a connect socket (client side)
   * GoodSource: Use a hardcoded password (one that was not sent over the network)
   * Sink:
   *    BadSink : Authenticate the user using LogonUserW()
   * Flow Variant: 10 Control flow: if(global_t) and if(global_f)
   *
   * */
  
  #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 */
  # define INVALID_SOCKET -1
  # define SOCKET_ERROR -1
  # define CLOSE_SOCKET close
  # define SOCKET int
  #endif
  
  #define TCP_PORT 27015
  
  #ifdef _WIN32
  #include <windows.h>
  #pragma comment(lib, "advapi32.lib")
  #endif
  
  #ifndef OMITBAD
  
  void CWE319_Plaintext_Tx_Sensitive_Info__w32_wchar_t_connect_socket_10_bad()
  {
      wchar_t * password;
      wchar_t password_buf[100] = L"";
      password = password_buf;
      if(global_t)
      {
          {
  #ifdef _WIN32
              WSADATA wsa_data;
              int wsa_data_init = 0;
  #endif
              int recv_rv;
              struct sockaddr_in s_in;
              wchar_t *replace;
              SOCKET connect_socket = INVALID_SOCKET;
              size_t password_len = wcslen(password);
              do
              {
  #ifdef _WIN32
                  if (WSAStartup(MAKEWORD(2,2), &wsa_data) != NO_ERROR) break;
                  wsa_data_init = 1;
  #endif
                  connect_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                  if (connect_socket == INVALID_SOCKET) break;
                  memset(&s_in, 0, sizeof(s_in));
                  s_in.sin_family = AF_INET;
                  s_in.sin_addr.s_addr = inet_addr("127.0.0.1");
                  s_in.sin_port = htons(TCP_PORT);
                  if (connect(connect_socket, (struct sockaddr*)&s_in, sizeof(s_in)) == SOCKET_ERROR) break;
                  /* Abort on error or the connection was closed, make sure to recv one
                   * less char than is in the recv_buf in order to append a terminator */
                  /* FLAW: Reading plaintext data from the network */
                  recv_rv = recv(connect_socket, (char*)password+password_len, (int)(100-password_len-1), 0);
                  if (recv_rv == SOCKET_ERROR || recv_rv == 0) break;
                  /* Append null terminator */
                  password[recv_rv] = L'\0';
                  /* Eliminate CRLF */
                  replace = wcschr(password, L'\r');
                  if (replace) *replace = L'\0';
                  replace = wcschr(password, L'\n');
                  if (replace) *replace = L'\0';
              }
              while (0);
              if (connect_socket != INVALID_SOCKET) CLOSE_SOCKET(connect_socket);
  #ifdef _WIN32
              if (wsa_data_init) WSACleanup();
  #endif
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use a hardcoded password (it was not sent over the network)
           * INCIDENTAL FLAW: CWE-259 Hard Coded Password */
          wcscpy(password, L"Password1234!");
      }
      {
  #ifdef _WIN32 /* this is WIN32 specific */
          HANDLE pHandle;
          wchar_t * username = L"User";
          wchar_t * domain = L"Domain";
          /* Use the password in LogonUser() to establish that it is "sensitive" */
          /* POTENTIAL FLAW: Using sensitive information that was possibly sent in plaintext over the network */
          if (LogonUserW(
                      username,
                      domain,
                      password,
                      LOGON32_LOGON_NETWORK,
                      LOGON32_PROVIDER_DEFAULT,
                      &pHandle) != 0)
          {
              printLine("User logged in successfully.");
              CloseHandle(pHandle);
          }
          else
          {
              printLine("Unable to login.");
          }
  #endif
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the global_t to global_f */
  static void goodG2B1()
  {
      wchar_t * password;
      wchar_t password_buf[100] = L"";
      password = password_buf;
      if(global_f)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
  #ifdef _WIN32
              WSADATA wsa_data;
              int wsa_data_init = 0;
  #endif
              int recv_rv;
              struct sockaddr_in s_in;
              wchar_t *replace;
              SOCKET connect_socket = INVALID_SOCKET;
              size_t password_len = wcslen(password);
              do
              {
  #ifdef _WIN32
                  if (WSAStartup(MAKEWORD(2,2), &wsa_data) != NO_ERROR) break;
                  wsa_data_init = 1;
  #endif
                  connect_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                  if (connect_socket == INVALID_SOCKET) break;
                  memset(&s_in, 0, sizeof(s_in));
                  s_in.sin_family = AF_INET;
                  s_in.sin_addr.s_addr = inet_addr("127.0.0.1");
                  s_in.sin_port = htons(TCP_PORT);
                  if (connect(connect_socket, (struct sockaddr*)&s_in, sizeof(s_in)) == SOCKET_ERROR) break;
                  /* Abort on error or the connection was closed, make sure to recv one
                   * less char than is in the recv_buf in order to append a terminator */
                  /* FLAW: Reading plaintext data from the network */
                  recv_rv = recv(connect_socket, (char*)password+password_len, (int)(100-password_len-1), 0);
                  if (recv_rv == SOCKET_ERROR || recv_rv == 0) break;
                  /* Append null terminator */
                  password[recv_rv] = L'\0';
                  /* Eliminate CRLF */
                  replace = wcschr(password, L'\r');
                  if (replace) *replace = L'\0';
                  replace = wcschr(password, L'\n');
                  if (replace) *replace = L'\0';
              }
              while (0);
              if (connect_socket != INVALID_SOCKET) CLOSE_SOCKET(connect_socket);
  #ifdef _WIN32
              if (wsa_data_init) WSACleanup();
  #endif
          }
      }
      else
      {
          /* FIX: Use a hardcoded password (it was not sent over the network)
           * INCIDENTAL FLAW: CWE-259 Hard Coded Password */
          wcscpy(password, L"Password1234!");
      }
      {
  #ifdef _WIN32 /* this is WIN32 specific */
          HANDLE pHandle;
          wchar_t * username = L"User";
          wchar_t * domain = L"Domain";
          /* Use the password in LogonUser() to establish that it is "sensitive" */
          /* POTENTIAL FLAW: Using sensitive information that was possibly sent in plaintext over the network */
          if (LogonUserW(
                      username,
                      domain,
                      password,
                      LOGON32_LOGON_NETWORK,
                      LOGON32_PROVIDER_DEFAULT,
                      &pHandle) != 0)
          {
              printLine("User logged in successfully.");
              CloseHandle(pHandle);
          }
          else
          {
              printLine("Unable to login.");
          }
  #endif
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      wchar_t * password;
      wchar_t password_buf[100] = L"";
      password = password_buf;
      if(global_t)
      {
          /* FIX: Use a hardcoded password (it was not sent over the network)
           * INCIDENTAL FLAW: CWE-259 Hard Coded Password */
          wcscpy(password, L"Password1234!");
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
  #ifdef _WIN32
              WSADATA wsa_data;
              int wsa_data_init = 0;
  #endif
              int recv_rv;
              struct sockaddr_in s_in;
              wchar_t *replace;
              SOCKET connect_socket = INVALID_SOCKET;
              size_t password_len = wcslen(password);
              do
              {
  #ifdef _WIN32
                  if (WSAStartup(MAKEWORD(2,2), &wsa_data) != NO_ERROR) break;
                  wsa_data_init = 1;
  #endif
                  connect_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                  if (connect_socket == INVALID_SOCKET) break;
                  memset(&s_in, 0, sizeof(s_in));
                  s_in.sin_family = AF_INET;
                  s_in.sin_addr.s_addr = inet_addr("127.0.0.1");
                  s_in.sin_port = htons(TCP_PORT);
                  if (connect(connect_socket, (struct sockaddr*)&s_in, sizeof(s_in)) == SOCKET_ERROR) break;
                  /* Abort on error or the connection was closed, make sure to recv one
                   * less char than is in the recv_buf in order to append a terminator */
                  /* FLAW: Reading plaintext data from the network */
                  recv_rv = recv(connect_socket, (char*)password+password_len, (int)(100-password_len-1), 0);
                  if (recv_rv == SOCKET_ERROR || recv_rv == 0) break;
                  /* Append null terminator */
                  password[recv_rv] = L'\0';
                  /* Eliminate CRLF */
                  replace = wcschr(password, L'\r');
                  if (replace) *replace = L'\0';
                  replace = wcschr(password, L'\n');
                  if (replace) *replace = L'\0';
              }
              while (0);
              if (connect_socket != INVALID_SOCKET) CLOSE_SOCKET(connect_socket);
  #ifdef _WIN32
              if (wsa_data_init) WSACleanup();
  #endif
          }
      }
      {
  #ifdef _WIN32 /* this is WIN32 specific */
          HANDLE pHandle;
          wchar_t * username = L"User";
          wchar_t * domain = L"Domain";
          /* Use the password in LogonUser() to establish that it is "sensitive" */
          /* POTENTIAL FLAW: Using sensitive information that was possibly sent in plaintext over the network */
          if (LogonUserW(
                      username,
                      domain,
                      password,
                      LOGON32_LOGON_NETWORK,
                      LOGON32_PROVIDER_DEFAULT,
                      &pHandle) != 0)
          {
              printLine("User logged in successfully.");
              CloseHandle(pHandle);
          }
          else
          {
              printLine("Unable to login.");
          }
  #endif
      }
  }
  
  void CWE319_Plaintext_Tx_Sensitive_Info__w32_wchar_t_connect_socket_10_good()
  {
      goodG2B1();
      goodG2B2();
  }
  
  #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()...");
      CWE319_Plaintext_Tx_Sensitive_Info__w32_wchar_t_connect_socket_10_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE319_Plaintext_Tx_Sensitive_Info__w32_wchar_t_connect_socket_10_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  