Beispiel #1
0
void CheckAuthUsernamePrompt (char *line, int config)
{
  DWORD nCharsWritten;
  struct user_auth user_auth;

  /* Check for Passphrase prompt */
  if (strncmp(line, "Enter Auth Username:"******"");
            }
        }
      else
        {
          if (!WriteFile(o.cnn[config].hStdIn, "\n",
                        1, &nCharsWritten, NULL))
            {
              ShowLocalizedMsg(GUI_NAME, ERR_CR2STDIN, "");
            }
        }

      if (strlen(user_auth.password) > 0)
        {
          if (!WriteFile(o.cnn[config].hStdIn, user_auth.password,
                    strlen(user_auth.password), &nCharsWritten, NULL))
            {
              ShowLocalizedMsg(GUI_NAME, ERR_AUTH_PASSWORD2STDIN, "");
            }
        }
      else
        {
          if (!WriteFile(o.cnn[config].hStdIn, "\n",
                        1, &nCharsWritten, NULL))
            {
              ShowLocalizedMsg(GUI_NAME, ERR_CR2STDIN, "");
            }
        }

      /* Remove Username prompt from lastline buffer */
      line[0]='\0';

      /* Clear user_auth buffer */
      CLEAR(user_auth);  
    }

}
Beispiel #2
0
/* Check that proxy settings are valid */
int
CheckProxySettings(HWND hwndDlg)
{
    if (IsDlgButtonChecked(hwndDlg, ID_RB_PROXY_MANUAL) == BST_CHECKED)
    {
        TCHAR text[100];
        BOOL http = (IsDlgButtonChecked(hwndDlg, ID_RB_PROXY_HTTP) == BST_CHECKED);

        GetDlgItemText(hwndDlg, ID_EDT_PROXY_ADDRESS, text, _countof(text));
        if (_tcslen(text) == 0)
        {
            /* proxy address not specified */
            ShowLocalizedMsg((http ? IDS_ERR_HTTP_PROXY_ADDRESS : IDS_ERR_SOCKS_PROXY_ADDRESS));
            return 0;
        }

        GetDlgItemText(hwndDlg, ID_EDT_PROXY_PORT, text, _countof(text));
        if (_tcslen(text) == 0)
        {
            /* proxy port not specified */
            ShowLocalizedMsg((http ? IDS_ERR_HTTP_PROXY_PORT : IDS_ERR_SOCKS_PROXY_PORT));
            return 0;
        }

        long port = _tcstol(text, NULL, 10);
        if ((port < 1) || (port > 65535))
        {
            /* proxy port range error */
            ShowLocalizedMsg((http ? IDS_ERR_HTTP_PROXY_PORT_RANGE : IDS_ERR_SOCKS_PROXY_PORT_RANGE));
            return 0;
        }
    }

    return 1;
}
Beispiel #3
0
int CheckServiceStatus()
{

  SC_HANDLE schSCManager;
  SC_HANDLE schService;
  SERVICE_STATUS ssStatus; 

    // Open a handle to the SC Manager database. 
    schSCManager = OpenSCManager( 
      NULL,                    // local machine 
      NULL,                    // ServicesActive database 
      SC_MANAGER_CONNECT);     // Connect rights 
   
    if (NULL == schSCManager) {
      o.service_state = service_noaccess;
      SetServiceMenuStatus();
      return(false);
    }

    schService = OpenService( 
      schSCManager,          // SCM database 
      _T("OpenVPNService"),  // service name
      SERVICE_QUERY_STATUS); 
 
    if (schService == NULL) {
      /* open vpn service failed */
      ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE);
      o.service_state = service_noaccess;
      SetServiceMenuStatus();
      return(false);
    }

    if (!QueryServiceStatus( 
            schService,   // handle to service 
            &ssStatus) )  // address of status information structure
    {
      /* query failed */
      ShowLocalizedMsg(IDS_ERR_QUERY_SERVICE);
      return(false);
    }
 
    if (ssStatus.dwCurrentState == SERVICE_RUNNING) 
    {
        o.service_state = service_connected;
        SetServiceMenuStatus(); 
        SetTrayIcon(connected);
        return(true);
    }
    else 
    { 
        o.service_state = service_disconnected;
        SetServiceMenuStatus();
        SetTrayIcon(disconnected);
        return(false);
    } 
} 
Beispiel #4
0
int MyStopService()
{

  SC_HANDLE schSCManager;
  SC_HANDLE schService;
  SERVICE_STATUS ssStatus; 
  int i;

    // Open a handle to the SC Manager database. 
    schSCManager = OpenSCManager( 
       NULL,                    // local machine 
       NULL,                    // ServicesActive database 
       SC_MANAGER_CONNECT);     // Connect rights 
   
    if (NULL == schSCManager) {
       /* can't open SCManager */
       ShowLocalizedMsg(IDS_ERR_OPEN_SCMGR, (int) GetLastError());
       return(false);
    }

    schService = OpenService( 
        schSCManager,          // SCM database 
        _T("OpenVPNService"),  // service name
        SERVICE_STOP); 
 
    if (schService == NULL) {
      /* can't open vpn service */
      ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE);
      return(false);
    }

    /* Run DisConnect script */
    for (i=0; i<o.num_configs; i++)    
      RunDisconnectScript(&o.conn[i], true);

    if (!ControlService( 
            schService,   // handle to service 
            SERVICE_CONTROL_STOP,   // control value to send 
            &ssStatus) )  // address of status info 
    {
      /* stop failed */
      ShowLocalizedMsg(IDS_ERR_STOP_SERVICE);
      return(false);
    }

    o.service_state = service_disconnected;
    SetServiceMenuStatus();
    CheckAndSetTrayIcon();
    return(true);
}
Beispiel #5
0
static void
parse_argv(options_t *options, int argc, TCHAR **argv)
{
    int i, j;

    /* parse command line */
    for (i = 1; i < argc; ++i)
    {
        TCHAR *p[MAX_PARMS];
        CLEAR(p);
        p[0] = argv[i];
        if (_tcsncmp(p[0], _T("--"), 2) != 0)
        {
            /* Missing -- before option. */
            ShowLocalizedMsg(IDS_ERR_BAD_PARAMETER, p[0]);
            exit(0);
        }

        p[0] += 2;

        for (j = 1; j < MAX_PARMS; ++j)
        {
            if (i + j < argc)
            {
                TCHAR *arg = argv[i + j];
                if (_tcsncmp(arg, _T("--"), 2) == 0)
                    break;
                p[j] = arg;
            }
        }
        i = add_option(options, i, p);
    }
}
Beispiel #6
0
static int
VerifyAutoConnections()
{
    int i;
    BOOL match;

    for (i = 0; o.auto_connect[i] != 0 && i < MAX_CONFIGS; i++)
    {
        int j;
        match = FALSE;
        for (j = 0; j < MAX_CONFIGS; j++)
        {
            if (_tcsicmp(o.conn[j].config_file, o.auto_connect[i]) == 0)
            {
                match = TRUE;
                break;
            }
        }
        if (match == FALSE)
        {
            /* autostart config not found */
            ShowLocalizedMsg(IDS_ERR_AUTOSTART_CONF, o.auto_connect[i]);
            return FALSE;
        }
    }

    return TRUE;
}
Beispiel #7
0
int
SetRegistryValueNumeric(HKEY regkey, const TCHAR *name, DWORD data)
{
  LONG status = RegSetValueEx(regkey, name, 0, REG_DWORD, (PBYTE) &data, sizeof(data));
  if (status == ERROR_SUCCESS)
    return 1;

  ShowLocalizedMsg(IDS_ERR_WRITE_REGVALUE, GUI_REGKEY_HKCU, name);
  return 0;
}
Beispiel #8
0
static void
SetGUILanguage(LANGID langId)
{
    HKEY regkey;
    if (RegCreateKeyEx(HKEY_CURRENT_USER, GUI_REGKEY_HKCU, 0, NULL, 0,
        KEY_WRITE, NULL, &regkey, NULL) != ERROR_SUCCESS )
        ShowLocalizedMsg(IDS_ERR_CREATE_REG_HKCU_KEY, GUI_REGKEY_HKCU);

    SetRegistryValueNumeric(regkey, _T("ui_language"), langId);
    InitMUILanguage(langId);
    gui_language = langId;
}
Beispiel #9
0
/*
 * Read one line from OpenVPN's stdout.
 */
static BOOL
ReadLineFromStdOut(HANDLE hStdOut, char *line, DWORD size)
{
    DWORD len, read;

    while (TRUE)
    {
        if (!PeekNamedPipe(hStdOut, line, size, &read, NULL, NULL))
        {
            if (GetLastError() != ERROR_BROKEN_PIPE)
                ShowLocalizedMsg(IDS_ERR_READ_STDOUT_PIPE);
            return FALSE;
        }

        char *pos = memchr(line, '\r', read);
        if (pos)
        {
            len = pos - line + 2;
            if (len > size)
                return FALSE;
            break;
        }

        /* Line doesn't fit into the buffer */
        if (read == size)
            return FALSE;

        Sleep(100);
    }

    if (!ReadFile(hStdOut, line, len, &read, NULL) || read != len)
    {
        if (GetLastError() != ERROR_BROKEN_PIPE)
            ShowLocalizedMsg(IDS_ERR_READ_STDOUT_PIPE);
        return FALSE;
    }

    line[read - 2] = '\0';
    return TRUE;
}
Beispiel #10
0
int SetRegistryValue(HKEY regkey, const TCHAR *name, const TCHAR *data)
{
  /* set a registry string */
  DWORD size = (_tcslen(data) + 1) * sizeof(*data);
  if(RegSetValueEx(regkey, name, 0, REG_SZ, (PBYTE) data, size) != ERROR_SUCCESS)
    {
      /* Error writing registry value */
      ShowLocalizedMsg(IDS_ERR_WRITE_REGVALUE, GUI_REGKEY_HKCU, name);
      return(0);
    }

  return(1);

}
Beispiel #11
0
void
ShowChangePassphraseDialog(connection_t *c)
{
  HANDLE hThread;
  DWORD IDThread;

  /* Start a new thread to have our own message-loop for this dialog */
  hThread = CreateThread(NULL, 0, ChangePassphraseThread, c, 0, &IDThread);
  if (hThread == NULL)
    {
    /* error creating thread */
    ShowLocalizedMsg(IDS_ERR_CREATE_PASS_THREAD);
    return;
  }

}
Beispiel #12
0
void
BuildFileList()
{
    static bool issue_warnings = true;

    o.num_configs = 0;

    BuildFileList0 (o.config_dir, issue_warnings);

    if (_tcscmp (o.global_config_dir, o.config_dir))
        BuildFileList0 (o.global_config_dir, issue_warnings);

    if (o.num_configs == 0 && issue_warnings)
        ShowLocalizedMsg(IDS_NFO_NO_CONFIGS);

    issue_warnings = false;
}
Beispiel #13
0
void ShowChangePassphraseDialog(int config)
{
  HANDLE hThread; 
  DWORD IDThread;

  /* Start a new thread to have our own message-loop for this dialog */
  hThread = CreateThread(NULL, 0, 
            (LPTHREAD_START_ROUTINE) ChangePassphraseThread,
            (int *) config,  // pass config nr
            0, &IDThread); 
  if (hThread == NULL) 
    {
    /* error creating thread */
    ShowLocalizedMsg (GUI_NAME, ERR_CREATE_PASS_THREAD, "");
    return;
  }

}
Beispiel #14
0
void EditConfig(int config)
{
  char filename[200];

  extern char config_dir[MAX_PATH];
  extern char editor[MAX_PATH];

  STARTUPINFO start_info;
  PROCESS_INFORMATION proc_info;
  SECURITY_ATTRIBUTES sa;
  SECURITY_DESCRIPTOR sd;
  char command_line[256];

  CLEAR (start_info);
  CLEAR (proc_info);
  CLEAR (sa);
  CLEAR (sd);

  mysnprintf(filename, "%s \"%s\\%s\"", o.editor, o.cnn[config].config_dir, o.cnn[config].config_file);

  /* fill in STARTUPINFO struct */
  GetStartupInfo(&start_info);
  start_info.cb = sizeof(start_info);
  start_info.dwFlags = 0;
  start_info.wShowWindow = SW_SHOWDEFAULT;
  start_info.hStdInput = NULL;
  start_info.hStdOutput = NULL;

  if (!CreateProcess(NULL,
		     filename, 	//commandline
		     NULL,
		     NULL,
		     TRUE,
		     CREATE_NEW_CONSOLE,
		     NULL,
		     o.cnn[config].config_dir,	//start-up dir
		     &start_info,
		     &proc_info))
    {
        /* could not start editor */ 
	ShowLocalizedMsg(GUI_NAME, ERR_START_CONF_EDITOR, o.editor);
    }

}
Beispiel #15
0
void EditConfig(int config)
{
  TCHAR filename[200];

  STARTUPINFO start_info;
  PROCESS_INFORMATION proc_info;
  SECURITY_ATTRIBUTES sa;
  SECURITY_DESCRIPTOR sd;

  CLEAR (start_info);
  CLEAR (proc_info);
  CLEAR (sa);
  CLEAR (sd);

  _sntprintf_0(filename, _T("%s \"%s\\%s\""), o.editor, o.conn[config].config_dir, o.conn[config].config_file);

  /* fill in STARTUPINFO struct */
  GetStartupInfo(&start_info);
  start_info.cb = sizeof(start_info);
  start_info.dwFlags = 0;
  start_info.wShowWindow = SW_SHOWDEFAULT;
  start_info.hStdInput = NULL;
  start_info.hStdOutput = NULL;

  if (!CreateProcess(NULL,
		     filename, 	//commandline
		     NULL,
		     NULL,
		     TRUE,
		     CREATE_NEW_CONSOLE,
		     NULL,
		     o.conn[config].config_dir,	//start-up dir
		     &start_info,
		     &proc_info))
    {
        /* could not start editor */ 
	ShowLocalizedMsg(IDS_ERR_START_CONF_EDITOR, o.editor);
    }

  CloseHandle(proc_info.hThread);
  CloseHandle(proc_info.hProcess);
}
Beispiel #16
0
/*
 * Set priority based on the registry or cmd-line value
 */
static BOOL
SetProcessPriority(DWORD *priority)
{
    *priority = NORMAL_PRIORITY_CLASS;
    if (!_tcscmp(o.priority_string, _T("IDLE_PRIORITY_CLASS")))
        *priority = IDLE_PRIORITY_CLASS;
    else if (!_tcscmp(o.priority_string, _T("BELOW_NORMAL_PRIORITY_CLASS")))
        *priority = BELOW_NORMAL_PRIORITY_CLASS;
    else if (!_tcscmp(o.priority_string, _T("NORMAL_PRIORITY_CLASS")))
        *priority = NORMAL_PRIORITY_CLASS;
    else if (!_tcscmp(o.priority_string, _T("ABOVE_NORMAL_PRIORITY_CLASS")))
        *priority = ABOVE_NORMAL_PRIORITY_CLASS;
    else if (!_tcscmp(o.priority_string, _T("HIGH_PRIORITY_CLASS")))
        *priority = HIGH_PRIORITY_CLASS;
    else
    {
        ShowLocalizedMsg(IDS_ERR_UNKNOWN_PRIORITY, o.priority_string);
        return FALSE;
    }
    return TRUE;
}
Beispiel #17
0
BOOL CALLBACK ProxyAuthDialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam, UNUSED LPARAM lParam)
{
  char username[50];
  char password[50];
  FILE *fp;

  switch (msg) {

    case WM_INITDIALOG:
      //SetForegroundWindow(hwndDlg);
      break;

    case WM_COMMAND:
      switch (LOWORD(wParam)) {

        case IDOK:
          GetDlgItemText(hwndDlg, EDIT_PROXY_USERNAME, username, sizeof(username) - 1);
          GetDlgItemText(hwndDlg, EDIT_PROXY_PASSWORD, password, sizeof(password) - 1);
          if (!(fp = fopen(o.proxy_authfile, "w")))
            {
              /* error creating AUTH file */
              ShowLocalizedMsg(GUI_NAME, ERR_CREATE_AUTH_FILE, o.proxy_authfile);
              EndDialog(hwndDlg, LOWORD(wParam));
            }
          fputs(username, fp);
          fputs("\n", fp);
          fputs(password, fp);
          fputs("\n", fp);
          fclose(fp);
          EndDialog(hwndDlg, LOWORD(wParam));
          return TRUE;
      }
      break;
    case WM_CLOSE:
      EndDialog(hwndDlg, LOWORD(wParam));
      return TRUE;
     
  }
  return FALSE;
}
void ViewLog(int config)
{
  TCHAR filename[200];

  STARTUPINFO start_info;
  PROCESS_INFORMATION proc_info;
  SECURITY_ATTRIBUTES sa;
  SECURITY_DESCRIPTOR sd;

  CLEAR (start_info);
  CLEAR (proc_info);
  CLEAR (sa);
  CLEAR (sd);

  _sntprintf_0(filename, _T("%s \"%s\""), o.log_viewer, o.conn[config].log_path);

  /* fill in STARTUPINFO struct */
  GetStartupInfo(&start_info);
  start_info.cb = sizeof(start_info);
  start_info.dwFlags = 0;
  start_info.wShowWindow = SW_SHOWDEFAULT;
  start_info.hStdInput = NULL;
  start_info.hStdOutput = NULL;

  if (!CreateProcess(NULL,
		     filename, 	//commandline
		     NULL,
		     NULL,
		     TRUE,
		     CREATE_NEW_CONSOLE,
		     NULL,
		     o.log_dir,	//start-up dir
		     &start_info,
		     &proc_info))
    {
      /* could not start log viewer */
      ShowLocalizedMsg(IDS_ERR_START_LOG_VIEWER, o.log_viewer);
    }

}
Beispiel #19
0
int AddConfigFileToList(int config, char filename[], char config_dir[])
{
  char log_file[MAX_PATH];
  int i;

  /* Save config file name */
  strncpy(o.cnn[config].config_file, filename, sizeof(o.cnn[config].config_file));

  /* Save config dir */
  strncpy(o.cnn[config].config_dir, config_dir, sizeof(o.cnn[config].config_dir));

  /* Save connection name (config_name - extension) */
  strncpy(o.cnn[config].config_name, o.cnn[config].config_file,
          sizeof(o.cnn[config].config_name));
  o.cnn[config].config_name[strlen(o.cnn[config].config_name) - 
                                   (strlen(o.ext_string)+1)]=0;

  /* get log file pathname */
  if (!modext (log_file, sizeof (log_file), o.cnn[config].config_file, "log"))
    {
      /* cannot construct logfile-name */
      ShowLocalizedMsg (GUI_NAME, ERR_CANNOT_CONSTRUCT_LOG, o.cnn[config].config_file);
      return(false);
    }
  mysnprintf (o.cnn[config].log_path, "%s\\%s", o.log_dir, log_file);

  /* Check if connection should be autostarted */
  for (i=0; (i < MAX_CONFIGS) && o.auto_connect[i]; i++)
    {
      if (stricmp(o.cnn[config].config_file, o.auto_connect[i]) == 0)
        {
          o.cnn[config].auto_connect = true;
          break;
        }
    }

  return(true);
}
Beispiel #20
0
BOOL CALLBACK ChangePassphraseDialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam, UNUSED LPARAM lParam)
{
  HICON hIcon;
  char keyfile[MAX_PATH];
  int keyfile_format;
  BOOL Translated;
  TCHAR buf[1000];

  switch (msg) {

    case WM_INITDIALOG:
      hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(APP_ICON), 
                                                      IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
      if (hIcon) {
        SendMessage(hwndDlg, WM_SETICON, (WPARAM) (ICON_SMALL), (LPARAM) (hIcon));
        SendMessage(hwndDlg, WM_SETICON, (WPARAM) (ICON_BIG), (LPARAM) (hIcon));
      }
      return FALSE;

    case WM_COMMAND:
      switch (LOWORD(wParam)) {

        case IDOK:

          /* Check if the type new passwords match. */
          if (!ConfirmNewPassword (hwndDlg))
            {
              /* passwords don't match */
              ShowLocalizedMsg(GUI_NAME, ERR_PWD_DONT_MATCH, "");
              break;
            }

          /* Check minimum length of password */
          if (NewPasswordLengh(hwndDlg) < MIN_PASSWORD_LEN)
            {
              ShowLocalizedMsg(GUI_NAME, ERR_PWD_TO_SHORT, MIN_PASSWORD_LEN);
              break;
            }

          /* Check if the new password is empty. */
          if (NewPasswordLengh(hwndDlg) == 0)
            {
              myLoadString(INFO_EMPTY_PWD);
              if (MessageBox(NULL, buf, GUI_NAME, MB_YESNO) != IDYES)
                break;
            }

          GetDlgItemText(hwndDlg, TEXT_KEYFILE, keyfile, sizeof(keyfile) - 1);
          keyfile_format=GetDlgItemInt(hwndDlg, TEXT_KEYFORMAT, &Translated, FALSE);
          if (keyfile_format == KEYFILE_FORMAT_PEM)
            {
              /* Change password of a PEM file */
              if (ChangePasswordPEM(hwndDlg) == -1) /* Wrong password */
                break;
            }
          else if (keyfile_format == KEYFILE_FORMAT_PKCS12)
            {
              /* Change password of a .P12 file */
              if (ChangePasswordPKCS12(hwndDlg) == -1) /* Wrong password */
                break;
            }
          else
            {
              /* Unknown key format */
              ShowLocalizedMsg (GUI_NAME, ERR_UNKNOWN_KEYFILE_FORMAT, "");
            }

          DestroyWindow(hwndDlg);       
          break;

        case IDCANCEL:
          DestroyWindow(hwndDlg);       
          break;
      }
      break;


    case WM_DESTROY: 
      PostQuitMessage(0); 
      break; 


    case WM_CLOSE:
      DestroyWindow(hwndDlg);       
      return FALSE;
     
  }
  return FALSE;
}
Beispiel #21
0
int MyStartService()
{

  SC_HANDLE schSCManager;
  SC_HANDLE schService;
  SERVICE_STATUS ssStatus; 
  DWORD dwOldCheckPoint; 
  DWORD dwStartTickCount;
  DWORD dwWaitTime;
  int i;

    /* Set Service Status = Connecting */
    o.service_state = service_connecting;
    SetServiceMenuStatus();
    CheckAndSetTrayIcon();

    // Open a handle to the SC Manager database. 
    schSCManager = OpenSCManager( 
       NULL,                    // local machine 
       NULL,                    // ServicesActive database 
       SC_MANAGER_CONNECT);     // Connect rights 
   
    if (NULL == schSCManager) {
       /* open SC manager failed */
       ShowLocalizedMsg(IDS_ERR_OPEN_SCMGR);
       goto failed;
    }

    schService = OpenService( 
        schSCManager,          // SCM database 
        _T("OpenVPNService"),  // service name
        SERVICE_START | SERVICE_QUERY_STATUS); 
 
    if (schService == NULL) {
      /* can't open VPN service */
      ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE);
      goto failed;
    }
 
    /* Run Pre-connect script */
    for (i=0; i<o.num_configs; i++)
        RunPreconnectScript(&o.conn[i]);

    if (!StartService(
            schService,  // handle to service 
            0,           // number of arguments 
            NULL) )      // no arguments 
    {
      /* can't start */
      ShowLocalizedMsg(IDS_ERR_START_SERVICE);
      goto failed;
    }
    else 
    {
        //printf("Service start pending.\n"); 
    }
 
    // Check the status until the service is no longer start pending. 
    if (!QueryServiceStatus( 
            schService,   // handle to service 
            &ssStatus) )  // address of status information structure
    {
      /* query failed */
      ShowLocalizedMsg(IDS_ERR_QUERY_SERVICE);
      goto failed;
    }
 
    // Save the tick count and initial checkpoint.
    dwStartTickCount = GetTickCount();
    dwOldCheckPoint = ssStatus.dwCheckPoint;

    while (ssStatus.dwCurrentState == SERVICE_START_PENDING) 
    { 
        // Do not wait longer than the wait hint. A good interval is 
        // one tenth the wait hint, but no less than 1 second and no 
        // more than 5 seconds. 
 
        dwWaitTime = ssStatus.dwWaitHint / 10;

        if( dwWaitTime < 1000 )
            dwWaitTime = 1000;
        else if ( dwWaitTime > 5000 )
            dwWaitTime = 5000;

        Sleep( dwWaitTime );

        // Check the status again. 
        if (!QueryServiceStatus( 
                schService,   // handle to service 
                &ssStatus) )  // address of structure
            break; 
 
        if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
        {
            // The service is making progress.
            dwStartTickCount = GetTickCount();
            dwOldCheckPoint = ssStatus.dwCheckPoint;
        }
        else
        {
            if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
            {
                // No progress made within the wait hint
                break;
            }
        }
    } 

    CloseServiceHandle(schService); 
    CloseServiceHandle(schSCManager);

    if (ssStatus.dwCurrentState != SERVICE_RUNNING) 
    { 
        /* service hasn't started */
        ShowLocalizedMsg(IDS_ERR_SERVICE_START_FAILED);
        goto failed;
    } 

    /* Run Connect script */
    for (i=0; i<o.num_configs; i++)    
      RunConnectScript(&o.conn[i], true);

    /* Set Service Status = Connected */
    o.service_state = service_connected;
    SetServiceMenuStatus();
    CheckAndSetTrayIcon();

    /* Show "OpenVPN Service Started" Tray Balloon msg */
    ShowTrayBalloon(LoadLocalizedString(IDS_NFO_SERVICE_STARTED), _T(""));

    return(true);

failed:

    /* Set Service Status = Disconnecting */
    o.service_state = service_disconnected;
    SetServiceMenuStatus();
    CheckAndSetTrayIcon();
    return(false);
}
Beispiel #22
0
/* ChangePasswordPEM() returns:
 * -1 Wrong password
 * 0  Changing password failed for unknown reason
 * 1  Password changed successfully
 */
int ChangePasswordPEM(HWND hwndDlg)
{
  char keyfile[MAX_PATH];
  char oldpsw[50];
  char newpsw[50];
  WCHAR oldpsw_unicode[50];
  WCHAR newpsw_unicode[50];
  FILE *fp;

  EVP_PKEY *privkey;
 
  /* Get filename, old_psw and new_psw from Dialog */
  GetDlgItemText(hwndDlg, TEXT_KEYFILE, keyfile, sizeof(keyfile) - 1); 
  GetDlgItemTextW(hwndDlg, EDIT_PSW_CURRENT, oldpsw_unicode, sizeof(oldpsw_unicode)/2 - 1); 
  GetDlgItemTextW(hwndDlg, EDIT_PSW_NEW, newpsw_unicode, sizeof(newpsw_unicode)/2 - 1); 

  /* Convert Unicode to ASCII (CP850) */
  ConvertUnicode2Ascii(oldpsw_unicode, oldpsw, sizeof(oldpsw));
  if (!ConvertUnicode2Ascii(newpsw_unicode, newpsw, sizeof(newpsw)))
    {
      ShowLocalizedMsg(GUI_NAME, ERR_INVALID_CHARS_IN_PSW, "");
      return(-1);
    }

  privkey = EVP_PKEY_new();

  /* Open old keyfile for reading */
  if (! (fp = fopen (keyfile, "r")))
    {
      /* can't open key file */
      ShowLocalizedMsg(GUI_NAME, ERR_OPEN_PRIVATE_KEY_FILE, keyfile);
      return(0);
    }

  /* Import old key */
  if (! (privkey = PEM_read_PrivateKey (fp, NULL, NULL, oldpsw)))
    {
      /* wrong password */
      ShowLocalizedMsg(GUI_NAME, ERR_OLD_PWD_INCORRECT, ""); 
      fclose(fp);
      return(-1);
    }

  fclose(fp);

  /* Open keyfile for writing */
  if (! (fp = fopen (keyfile, "w")))
    {
      /* can't open file rw */
      ShowLocalizedMsg(GUI_NAME, ERR_OPEN_WRITE_KEY, keyfile);
      EVP_PKEY_free(privkey);
      return(0);
    }
 
  /* Write new key to file */
  if (strlen(newpsw) == 0)
    {
      /* No passphrase */
      if ( !(PEM_write_PrivateKey(fp, privkey, \
                                  NULL, NULL,          /* Use NO encryption */
                                  0, 0, NULL)))
        {
          /* error writing new key */
          ShowLocalizedMsg(GUI_NAME, ERR_WRITE_NEW_KEY, keyfile);
          EVP_PKEY_free(privkey);
          fclose(fp);
          return(0);
        }
    }
  else
    {
      /* Use passphrase */
      if ( !(PEM_write_PrivateKey(fp, privkey, \
                                  EVP_des_ede3_cbc(),  /* Use 3DES encryption */
                                  newpsw, (int) strlen(newpsw), 0, NULL)))
        {
          /* can't write new key */
          ShowLocalizedMsg(GUI_NAME, ERR_WRITE_NEW_KEY, keyfile);
          EVP_PKEY_free(privkey);
          fclose(fp);
          return(0);
        }
    }

  EVP_PKEY_free(privkey);
  fclose(fp);

  /* signal success to user */
  ShowLocalizedMsg(GUI_NAME, INFO_PWD_CHANGED, "");
  return(1);
}
Beispiel #23
0
void
SaveProxySettings(HWND hwndDlg)
{
    HKEY regkey;
    DWORD dwDispos;
    TCHAR proxy_source_string[2] = _T("0");
    TCHAR proxy_type_string[2] = _T("0");

    /* Save Proxy Settings Source */
    if (IsDlgButtonChecked(hwndDlg, ID_RB_PROXY_OPENVPN) == BST_CHECKED)
    {
        o.proxy_source = config;
        proxy_source_string[0] = _T('0');
    }
    else if (IsDlgButtonChecked(hwndDlg, ID_RB_PROXY_MSIE) == BST_CHECKED)
    {
        o.proxy_source = windows;
        proxy_source_string[0] = _T('1');
    }
    else if (IsDlgButtonChecked(hwndDlg, ID_RB_PROXY_MANUAL) == BST_CHECKED)
    {
        o.proxy_source = manual;
        proxy_source_string[0] = _T('2');
    }

    /* Save Proxy type, address and port */
    if (IsDlgButtonChecked(hwndDlg, ID_RB_PROXY_HTTP) == BST_CHECKED)
    {
        o.proxy_type = http;
        proxy_type_string[0] = _T('0');

        GetDlgItemText(hwndDlg, ID_EDT_PROXY_ADDRESS, o.proxy_http_address,
                       _countof(o.proxy_http_address));
        GetDlgItemText(hwndDlg, ID_EDT_PROXY_PORT, o.proxy_http_port,
                       _countof(o.proxy_http_port));
    }
    else
    {
        o.proxy_type = socks;
        proxy_type_string[0] = _T('1');

        GetDlgItemText(hwndDlg, ID_EDT_PROXY_ADDRESS, o.proxy_socks_address,
                       _countof(o.proxy_socks_address));
        GetDlgItemText(hwndDlg, ID_EDT_PROXY_PORT, o.proxy_socks_port,
                       _countof(o.proxy_socks_port));
    }

    /* Open Registry for writing */
    if (RegCreateKeyEx(HKEY_CURRENT_USER, GUI_REGKEY_HKCU, 0, _T(""), REG_OPTION_NON_VOLATILE,
                       KEY_WRITE, NULL, &regkey, &dwDispos) != ERROR_SUCCESS)
    {
        /* error creating Registry-Key */
        ShowLocalizedMsg(IDS_ERR_CREATE_REG_HKCU_KEY, GUI_REGKEY_HKCU);
        return;
    }

    /* Save Settings to registry */
    SetRegistryValue(regkey, _T("proxy_source"), proxy_source_string);
    SetRegistryValue(regkey, _T("proxy_type"), proxy_type_string);
    SetRegistryValue(regkey, _T("proxy_http_address"), o.proxy_http_address);
    SetRegistryValue(regkey, _T("proxy_http_port"), o.proxy_http_port);
    SetRegistryValue(regkey, _T("proxy_socks_address"), o.proxy_socks_address);
    SetRegistryValue(regkey, _T("proxy_socks_port"), o.proxy_socks_port);

    RegCloseKey(regkey);
}
Beispiel #24
0
static int
add_option(options_t *options, int i, TCHAR **p)
{
    if (streq(p[0], _T("help")))
    {
        TCHAR caption[200];
        LoadLocalizedStringBuf(caption, _countof(caption), IDS_NFO_USAGECAPTION);
        ShowLocalizedMsgEx(MB_OK, caption, IDS_NFO_USAGE);
        exit(0);
    }
    else if (streq(p[0], _T("connect")) && p[1])
    {
        ++i;
        static int auto_connect_nr = 0;
        if (auto_connect_nr == MAX_CONFIGS)
        {
            /* Too many configs */
            ShowLocalizedMsg(IDS_ERR_MANY_CONFIGS, MAX_CONFIGS);
            exit(1);
        }
        options->auto_connect[auto_connect_nr++] = p[1];
    }
    else if (streq(p[0], _T("exe_path")) && p[1])
    {
        ++i;
        _tcsncpy(options->exe_path, p[1], _countof(options->exe_path) - 1);
    }
    else if (streq(p[0], _T("config_dir")) && p[1])
    {
        ++i;
        _tcsncpy(options->config_dir, p[1], _countof(options->config_dir) - 1);
    }
    else if (streq(p[0], _T("ext_string")) && p[1])
    {
        ++i;
        _tcsncpy(options->ext_string, p[1], _countof(options->ext_string) - 1);
    }
    else if (streq(p[0], _T("log_dir")) && p[1])
    {
        ++i;
        _tcsncpy(options->log_dir, p[1], _countof(options->log_dir) - 1);
    }
    else if (streq(p[0], _T("priority_string")) && p[1])
    {
        ++i;
        _tcsncpy(options->priority_string, p[1], _countof(options->priority_string) - 1);
    }
    else if (streq(p[0], _T("append_string")) && p[1])
    {
        ++i;
        _tcsncpy(options->append_string, p[1], _countof(options->append_string) - 1);
    }
    else if (streq(p[0], _T("log_viewer")) && p[1])
    {
        ++i;
        _tcsncpy(options->log_viewer, p[1], _countof(options->log_viewer) - 1);
    }
    else if (streq(p[0], _T("editor")) && p[1])
    {
        ++i;
        _tcsncpy(options->editor, p[1], _countof(options->editor) - 1);
    }
    else if (streq(p[0], _T("allow_edit")) && p[1])
    {
        ++i;
        _tcsncpy(options->allow_edit, p[1], _countof(options->allow_edit) - 1);
    }
    else if (streq(p[0], _T("allow_service")) && p[1])
    {
        ++i;
        _tcsncpy(options->allow_service, p[1], _countof(options->allow_service) - 1);
    }
    else if (streq(p[0], _T("allow_password")) && p[1])
    {
        ++i;
        _tcsncpy(options->allow_password, p[1], _countof(options->allow_password) - 1);
    }
    else if (streq(p[0], _T("allow_proxy")) && p[1])
    {
        ++i;
        _tcsncpy(options->allow_proxy, p[1], _countof(options->allow_proxy) - 1);
    }
    else if (streq(p[0], _T("show_balloon")) && p[1])
    {
        ++i;
        _tcsncpy(options->show_balloon, p[1], _countof(options->show_balloon) - 1);
    }
    else if (streq(p[0], _T("service_only")) && p[1])
    {
        ++i;
        _tcsncpy(options->service_only, p[1], _countof(options->service_only) - 1);
    }
    else if (streq(p[0], _T("show_script_window")) && p[1])
    {
        ++i;
        _tcsncpy(options->show_script_window, p[1], _countof(options->show_script_window) - 1);
    }
    else if (streq(p[0], _T("silent_connection")) && p[1])
    {
        ++i;
        _tcsncpy(options->silent_connection, p[1], _countof(options->silent_connection) - 1);
    }
    else if (streq(p[0], _T("passphrase_attempts")) && p[1])
    {
        ++i;
        _tcsncpy(options->psw_attempts_string, p[1], _countof(options->psw_attempts_string) - 1);
    }
    else if (streq(p[0], _T("connectscript_timeout")) && p[1])
    {
        ++i;
        _tcsncpy(options->connectscript_timeout_string, p[1], _countof(options->connectscript_timeout_string) - 1);
    }
    else if (streq(p[0], _T("disconnectscript_timeout")) && p[1])
    {
        ++i;
        _tcsncpy(options->disconnectscript_timeout_string, p[1], _countof(options->disconnectscript_timeout_string) - 1);
    }
    else if (streq(p[0], _T("preconnectscript_timeout")) && p[1])
    {
        ++i;
        _tcsncpy(options->preconnectscript_timeout_string, p[1], _countof(options->preconnectscript_timeout_string) - 1);
    }
    else
    {
        /* Unrecognized option or missing parameter */
        ShowLocalizedMsg(IDS_ERR_BAD_OPTION, p[0]);
        exit(1);
    }
    return i;
}
Beispiel #25
0
void CheckPrivateKeyPassphrasePrompt (char *line, int config)
{
  DWORD nCharsWritten;
  char passphrase_ascii[256];

  /* Check for Passphrase prompt */
  if (strncmp(line, "Enter PEM pass phrase:", 22) == 0) 
    {
      CLEAR(passphrase);  
      if (DialogBox(o.hInstance, (LPCTSTR)IDD_PASSPHRASE, NULL, 
                   (DLGPROC)PassphraseDialogFunc) == IDCANCEL)
        {
          StopOpenVPN(config);
        }

      if (wcslen(passphrase) > 0)
        {
          CLEAR(passphrase_ascii);
          ConvertUnicode2Ascii(passphrase, passphrase_ascii, sizeof(passphrase_ascii));

          if (!WriteFile(o.cnn[config].hStdIn, passphrase_ascii,
                    strlen(passphrase_ascii), &nCharsWritten, NULL))
            {
              /* PassPhrase -> stdin failed */
              ShowLocalizedMsg(GUI_NAME, ERR_PASSPHRASE2STDIN, "");
            }
        }
      if (!WriteFile(o.cnn[config].hStdIn, "\r\n",
                        2, &nCharsWritten, NULL))
        {
          /* CR -> stdin failed */
          ShowLocalizedMsg(GUI_NAME, ERR_CR2STDIN, "");
        }
      /* Remove Passphrase prompt from lastline buffer */
      line[0]='\0';

      /* Clear passphrase buffer */
      CLEAR(passphrase);
      CLEAR(passphrase_ascii);
    }

  /* Check for new passphrase prompt introduced with OpenVPN 2.0-beta12. */
  if (strncmp(line, "Enter Private Key Password:"******"");
            }
        }
      else
        {
          if (!WriteFile(o.cnn[config].hStdIn, "\n",
                        1, &nCharsWritten, NULL))
            {
              /* CR -> stdin failed */
              ShowLocalizedMsg(GUI_NAME, ERR_CR2STDIN, "");
            }
        }
      /* Remove Passphrase prompt from lastline buffer */
      line[0]='\0';

      /* Clear passphrase buffer */
      CLEAR(passphrase);
      CLEAR(passphrase_ascii);
    }

}
Beispiel #26
0
void
RunConnectScript(connection_t *c, int run_as_service)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    TCHAR cmdline[256];
    DWORD exit_code;
    struct _stat st;
    int i;

    /* Cut off extention from config filename and add "_up.bat" */
    int len = _tcslen(c->config_file) - _tcslen(o.ext_string) - 1;
    _sntprintf_0(cmdline, _T("%s\\%.*s_up.bat"), c->config_dir, len, c->config_file);

    /* Return if no script exists */
    if (_tstat(cmdline, &st) == -1)
        return;

    if (!run_as_service)
        SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONN_SCRIPT));

    CLEAR(si);
    CLEAR(pi);

    /* fill in STARTUPINFO struct */
    GetStartupInfo(&si);
    si.cb = sizeof(si);
    si.dwFlags = 0;
    si.wShowWindow = SW_SHOWDEFAULT;
    si.hStdInput = NULL;
    si.hStdOutput = NULL;

    if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE,
                       (o.show_script_window[0] == '1' ? CREATE_NEW_CONSOLE : CREATE_NO_WINDOW),
                       NULL, c->config_dir, &si, &pi))
    {
        ShowLocalizedMsg(IDS_ERR_RUN_CONN_SCRIPT, cmdline);
        return;
    }

    if (o.connectscript_timeout == 0)
        goto out;

    for (i = 0; i <= o.connectscript_timeout; i++)
    {
        if (!GetExitCodeProcess(pi.hProcess, &exit_code))
        {
            ShowLocalizedMsg(IDS_ERR_GET_EXIT_CODE, cmdline);
            goto out;
        }

        if (exit_code != STILL_ACTIVE)
        {
            if (exit_code != 0)
                ShowLocalizedMsg(IDS_ERR_CONN_SCRIPT_FAILED, exit_code);
            goto out;
        }

        Sleep(1000);
    }

    ShowLocalizedMsg(IDS_ERR_RUN_CONN_SCRIPT_TIMEOUT, o.connectscript_timeout);

out:
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
}
Beispiel #27
0
int GetKeyFilename(int config, char *keyfilename, unsigned int keyfilenamesize, int *keyfile_format)
{
  FILE *fp;
  char line[256];
  int found_key=0;
  int found_pkcs12=0;
  char configfile_path[MAX_PATH];

  strncpy(configfile_path, o.cnn[config].config_dir, sizeof(configfile_path));
  if (!(configfile_path[strlen(configfile_path)-1] == '\\'))
    strcat(configfile_path, "\\");
  strncat(configfile_path, o.cnn[config].config_file, 
          sizeof(configfile_path) - strlen(configfile_path) - 1);

  if (!(fp=fopen(configfile_path, "r")))
    {
      /* can't open config file */
      ShowLocalizedMsg(GUI_NAME, ERR_OPEN_CONFIG, configfile_path);
      return(0);
    }

  while (fgets(line, sizeof (line), fp))
    {
      if (LineBeginsWith(line, "key", 3))
        {
          if (found_key)
            {
              /* only one key option */
              ShowLocalizedMsg(GUI_NAME, ERR_ONLY_ONE_KEY_OPTION, "");
              return(0);
            }
          if (found_pkcs12)
            {
              /* key XOR pkcs12 */
              ShowLocalizedMsg(GUI_NAME, ERR_ONLY_KEY_OR_PKCS12, "");
              return(0);
            }
          found_key=1;
          *keyfile_format = KEYFILE_FORMAT_PEM;
          if (!ParseKeyFilenameLine(config, keyfilename, keyfilenamesize, &line[4]))
            return(0);
        }
      if (LineBeginsWith(line, "pkcs12", 6))
        {
          if (found_pkcs12)
            {
              /* only one pkcs12 option */
              ShowLocalizedMsg(GUI_NAME, ERR_ONLY_ONE_PKCS12_OPTION, "");
              return(0);
            }
          if (found_key)
            {
              /* only key XOR pkcs12 */
              ShowLocalizedMsg(GUI_NAME, ERR_ONLY_KEY_OR_PKCS12, "");
              return(0);
            }
          found_pkcs12=1;
          *keyfile_format = KEYFILE_FORMAT_PKCS12;
          if (!ParseKeyFilenameLine(config, keyfilename, keyfilenamesize, &line[7]))
            return(0);
        }      
    }

  if ((!found_key) && (!found_pkcs12))
    {
      /* must have key or pkcs12 option */
      ShowLocalizedMsg(GUI_NAME, ERR_MUST_HAVE_KEY_OR_PKCS12, "");
      return(0);
    }

  return(1);
}
Beispiel #28
0
/* ChangePasswordPKCS12() returns:
 * -1 Wrong password
 * 0  Changing password failed for unknown reason
 * 1  Password changed successfully
 */
int ChangePasswordPKCS12(HWND hwndDlg)
{
  char keyfile[MAX_PATH];
  char oldpsw[50];
  char newpsw[50];
  WCHAR oldpsw_unicode[50];
  WCHAR newpsw_unicode[50];
  FILE *fp;

  EVP_PKEY *privkey;
  X509 *cert;
  STACK_OF(X509) *ca = NULL;
  PKCS12 *p12;
  PKCS12 *new_p12;
  char *alias;

  /* Get filename, old_psw and new_psw from Dialog */
  GetDlgItemText(hwndDlg, TEXT_KEYFILE, keyfile, sizeof(keyfile) - 1); 
  GetDlgItemTextW(hwndDlg, EDIT_PSW_CURRENT, oldpsw_unicode, sizeof(oldpsw_unicode)/2 - 1); 
  GetDlgItemTextW(hwndDlg, EDIT_PSW_NEW, newpsw_unicode, sizeof(newpsw_unicode)/2 - 1); 

  /* Convert Unicode to ASCII (CP850) */
  ConvertUnicode2Ascii(oldpsw_unicode, oldpsw, sizeof(oldpsw));
  if (!ConvertUnicode2Ascii(newpsw_unicode, newpsw, sizeof(newpsw)))
    {
      ShowLocalizedMsg(GUI_NAME, ERR_INVALID_CHARS_IN_PSW, "");
      return(-1);
    }

  /* Load the PKCS #12 file */
  if (!(fp = fopen(keyfile, "rb")))
    {
      /* error opening file */
      ShowLocalizedMsg(GUI_NAME, ERR_OPEN_PRIVATE_KEY_FILE, keyfile);
      return(0);
    }
  p12 = d2i_PKCS12_fp(fp, NULL);
  fclose (fp);
  if (!p12) 
    {
      /* error reading PKCS #12 */
      ShowLocalizedMsg(GUI_NAME, ERR_READ_PKCS12, keyfile);
      return(0);
    }

  /* Parse the PKCS #12 file */
  if (!PKCS12_parse(p12, oldpsw, &privkey, &cert, &ca))
    {
      /* old password incorrect */
      ShowLocalizedMsg(GUI_NAME, ERR_OLD_PWD_INCORRECT, ""); 
      PKCS12_free(p12);
      return(-1);
    }

  /* Free old PKCS12 object */
  PKCS12_free(p12);

  /* Get FriendlyName of old cert */
  alias = X509_alias_get0(cert, NULL);

  /* Create new PKCS12 object */
  p12 = PKCS12_create(newpsw, alias, privkey, cert, ca, 0,0,0,0,0);
  if (!p12)
    {
      /* create failed */
      //ShowMsg(GUI_NAME, ERR_error_string(ERR_peek_last_error(), NULL));
      ShowLocalizedMsg(GUI_NAME, ERR_CREATE_PKCS12, "");
      return(0);
    }

  /* Free old key, cert and ca */
  EVP_PKEY_free(privkey);
  X509_free(cert);
  sk_X509_pop_free(ca, X509_free);

  /* Open keyfile for writing */
  if (!(fp = fopen(keyfile, "wb")))
    {
      ShowLocalizedMsg(GUI_NAME, ERR_OPEN_WRITE_KEY, keyfile);
      PKCS12_free(p12);
      return(0);
    }

  /* Write new key to file */
  i2d_PKCS12_fp(fp, p12);

  PKCS12_free(p12);
  fclose(fp);
  /* signal success to user */
  ShowLocalizedMsg(GUI_NAME, INFO_PWD_CHANGED, "");

  return(1);
}
Beispiel #29
0
void RunConnectScript(int config, int run_as_service)
{
	WIN32_FIND_DATA FindFileData;
	HANDLE hFind;
	STARTUPINFO start_info;
	PROCESS_INFORMATION proc_info;
	SECURITY_ATTRIBUTES sa;
	SECURITY_DESCRIPTOR sd;
	char command_line[256];
	char batch_file[100];
	DWORD ExitCode;
	int i, TimeOut;
	TCHAR buf[1000];

	/* Cut of extention from config filename and add "_up.bat". */
	strncpy(batch_file, o.cnn[config].config_file, sizeof(batch_file));
	batch_file[strlen(batch_file) - (strlen(o.ext_string)+1)]=0;
	strncat(batch_file, "_up.bat", sizeof(batch_file) - strlen(batch_file) - 1);
	mysnprintf(command_line, "%s\\%s", o.cnn[config].config_dir, batch_file);


	/* Return if no script exists */
	hFind = FindFirstFile(command_line, &FindFileData);
	if (hFind == INVALID_HANDLE_VALUE) 
	{
		return;
	}

	FindClose(hFind);

	if (!run_as_service)
	{
		/* UserInfo: Connect Script running */
		myLoadString(INFO_STATE_CONN_SCRIPT);
		SetDlgItemText(o.cnn[config].hwndStatus, TEXT_STATUS, buf); 
	}

	CLEAR (start_info);
	CLEAR (proc_info);
	CLEAR (sa);
	CLEAR (sd);

	/* fill in STARTUPINFO struct */
	GetStartupInfo(&start_info);
	start_info.cb = sizeof(start_info);
	start_info.dwFlags = 0;
	start_info.wShowWindow = SW_SHOWDEFAULT;
	start_info.hStdInput = NULL;
	start_info.hStdOutput = NULL;

	if (!CreateProcess(NULL,
				command_line, 	//commandline
				NULL,
				NULL,
				TRUE,
				((o.show_script_window[0] == '1') ? (DWORD) CREATE_NEW_CONSOLE : 
					(DWORD) CREATE_NO_WINDOW),
				NULL,
				o.cnn[config].config_dir,	//start-up dir
				&start_info,
				&proc_info))
	{
		/* Running Script failed */
		ShowLocalizedMsg(GUI_NAME, ERR_RUN_CONN_SCRIPT, command_line);
		return;
	}

	TimeOut = o.connectscript_timeout;

	if (TimeOut == 0)
	{
		/* Don't check exist status, just return */
		return;
	}

	for (i=0; i <= TimeOut; i++)
	{
		if (!GetExitCodeProcess(proc_info.hProcess, &ExitCode))
		{
			/* failed to get ExitCode */
			ShowLocalizedMsg(GUI_NAME, ERR_GET_EXIT_CODE, command_line);
			return;
		}

		if (ExitCode != STILL_ACTIVE)
		{
			if (ExitCode != 0)
			{
				/* ConnectScript failed */
				ShowLocalizedMsg(GUI_NAME, ERR_CONN_SCRIPT_FAILED, ExitCode);
				return;
			}
			return;
		}
		
		Sleep(1000);
	}    

	/* UserInfo: Timeout */
	ShowLocalizedMsg(GUI_NAME, ERR_RUN_CONN_SCRIPT_TIMEOUT, TimeOut);

}
Beispiel #30
0
int ParseKeyFilenameLine(int config, char *keyfilename, unsigned int keyfilenamesize, char *line)
{
  const int STATE_INITIAL = 0;
  const int STATE_READING_QUOTED_PARM = 1;
  const int STATE_READING_UNQUOTED_PARM = 2;
  int i=0;
  unsigned int j=0;
  int state = STATE_INITIAL;
  int backslash=0;
  char temp_filename[MAX_PATH];

  while(line[i] != '\0')
    {
      if (state == STATE_INITIAL)
        {
          if (line[i] == '\"')
            {
              state=STATE_READING_QUOTED_PARM;
            }
          else if ((line[i] == 0x0A) || (line[i] == 0x0D))
            break;
          else if ((line[i] == ';') || (line[i] == '#'))
            break;
          else if ((line[i] != ' ') && (line[i] != '\t')) 
            {
              if (line[i] == '\\')
                {
                  if(!backslash)
                    {
                      keyfilename[j++]=line[i];
                      state=STATE_READING_UNQUOTED_PARM;
                      backslash=1;
                    }
                  else
                    {
                      backslash=0;
                    }
                }
              else
                {
                  if (backslash) backslash=0;
                  keyfilename[j++]=line[i];
                  state=STATE_READING_UNQUOTED_PARM;
                }
            }
        }  

      else if (state == STATE_READING_QUOTED_PARM)
        {
          if (line[i] == '\"')
            break;
          if ((line[i] == 0x0A) || (line[i] == 0x0D))
            break;
          if (line[i] == '\\')
            {
              if (!backslash)
                {
                  keyfilename[j++]=line[i];
                  backslash=1;
                }
              else
                {
                  backslash=0;
                }
            }
          else
            {
              if (backslash) backslash=0;
              keyfilename[j++]=line[i];
            }
        }

      else if (state == STATE_READING_UNQUOTED_PARM)
        {
          if (line[i] == '\"')
            break;
          if ((line[i] == 0x0A) || (line[i] == 0x0D))
            break;
          if ((line[i] == ';') || (line[i] == '#'))
            break;
          if (line[i] == ' ')
            break;
          if (line[i] == '\t')
            break;
          if (line[i] == '\\')
            {
              if (!backslash)
                {
                  keyfilename[j++]=line[i];
                  backslash=1;
                }
              else
                {
                  backslash=0;
                }
            }
          else
            {
              if (backslash) backslash=0;
              keyfilename[j++]=line[i];
            }
        }

      if (j >= (keyfilenamesize - 1))
        {
          /* key filename to long */
          ShowLocalizedMsg(GUI_NAME, ERR_KEY_FILENAME_TO_LONG, "");
          return(0);
        }
      i++;
    }
  keyfilename[j]='\0';

  /* Prepend filename with configdir path if needed */
  if ((keyfilename[0] != '\\') && (keyfilename[0] != '/') && (keyfilename[1] != ':'))
    {
      strncpy(temp_filename, o.cnn[config].config_dir, sizeof(temp_filename));
      if (temp_filename[strlen(temp_filename) - 1] != '\\')
        strcat(temp_filename, "\\");
      strncat(temp_filename, keyfilename, 
              sizeof(temp_filename) - strlen(temp_filename) - 1);
      strncpy(keyfilename, temp_filename, keyfilenamesize - 1);
    }

  return(1);
}