diff --git a/main.c b/main.c index 404ddee..06d8634 100644 --- a/main.c +++ b/main.c @@ -125,9 +125,7 @@ int WINAPI WinMain (HINSTANCE hThisInstance, if (!CheckVersion()) { exit(1); } - if (!BuildFileList()) { - exit(1); - } + BuildFileList(); if (!VerifyAutoConnections()) { exit(1); } diff --git a/openvpn-gui-res-de.rc b/openvpn-gui-res-de.rc index 4faa619..8c0a27f 100644 --- a/openvpn-gui-res-de.rc +++ b/openvpn-gui-res-de.rc @@ -190,7 +190,6 @@ BEGIN /* OpenVPN */ IDS_ERR_MANY_CONFIGS "OpenVPN GUI unterstützt nicht mehr als %d Konfigurationen. Bitte kontaktieren Sie bei Bedarf den Author." - IDS_ERR_LOG_CONSTRUCT "Kann die Logdatei mit Namem: %s nicht erstellen" IDS_ERR_ONE_CONN_OLD_VER "Sie können nur eine Verbindung zur gleichen Zeit aufbauen, wenn Sie einen ältere Version als 2.0-beta6 von OpenVPN verwenden." IDS_ERR_STOP_SERV_OLD_VER "You cannot use OpenVPN GUI to start a connection while the OpenVPN Service is running (with OpenVPN 1.5/1.6). Stop OpenVPN Service first if you want to use OpenVPN GUI." IDS_ERR_CREATE_EVENT "CreateEvent fehlgeschlagen beim Beenden. Event: %s" diff --git a/openvpn-gui-res-en.rc b/openvpn-gui-res-en.rc index a59f233..9543229 100644 --- a/openvpn-gui-res-en.rc +++ b/openvpn-gui-res-en.rc @@ -185,7 +185,6 @@ BEGIN /* OpenVPN */ IDS_ERR_MANY_CONFIGS "OpenVPN GUI does not support more than %d configs. Please contact the author if you have the need for more." - IDS_ERR_LOG_CONSTRUCT "Cannot construct logfile name based on: %s" IDS_ERR_ONE_CONN_OLD_VER "You can only have one connection running at the same time when using an older version on OpenVPN than 2.0-beta6." IDS_ERR_STOP_SERV_OLD_VER "You cannot use OpenVPN GUI to start a connection while the OpenVPN Service is running (with OpenVPN 1.5/1.6). Stop OpenVPN Service first if you want to use OpenVPN GUI." IDS_ERR_CREATE_EVENT "CreateEvent failed on exit event: %s" diff --git a/openvpn-gui-res-es.rc b/openvpn-gui-res-es.rc index c425316..bb4bff0 100644 --- a/openvpn-gui-res-es.rc +++ b/openvpn-gui-res-es.rc @@ -172,7 +172,6 @@ BEGIN /* OpenVPN */ IDS_ERR_MANY_CONFIGS "OpenVPN GUI no soporta más de %d configuraciones. Contacte con el autor si necesita más." - IDS_ERR_LOG_CONSTRUCT "No se puede crear el fichero de log basado en: %s" IDS_ERR_ONE_CONN_OLD_VER "Sólo se puede tener una conexión activa a la vez usando una version de OpenVPN anterior a la 2.0-beta6." IDS_ERR_STOP_SERV_OLD_VER "No se puede usar OpenVPN GUI para inicar una conexión mientras el servicio OpenVPN está ejecutándose (con OpenVPN 1.5/1.6). Detenga primero el servicio OpenVPN si quiere utilizar OpenVPN GUI." IDS_ERR_CREATE_EVENT "CreateEvent fallido con evento de salida: %s" diff --git a/openvpn-gui-res-fr.rc b/openvpn-gui-res-fr.rc index a16d8c9..8a5a367 100644 --- a/openvpn-gui-res-fr.rc +++ b/openvpn-gui-res-fr.rc @@ -183,7 +183,6 @@ BEGIN /* OpenVPN */ IDS_ERR_MANY_CONFIGS "OpenVPN GUI ne permet pas plus de %d configurations. Contacter l'auteur si vous en avez besoin de plus." - IDS_ERR_LOG_CONSTRUCT "Impossible de créer le fichier de log basé sur: %s" IDS_ERR_ONE_CONN_OLD_VER "Vous ne pouvez avoir qu'une connexion en même temps si vous utilisez une version d'OpenVPN plus ancienne que la 2.0-beta6." IDS_ERR_STOP_SERV_OLD_VER "Vous ne pouvez utiliser OpenVPN GUI pour ouvrir une connexion lorsque que le service OpenVPN est actif (avec OpenVPN 1.5/1.6). Arrêtez le service OpenVPN si vous voulez utiliser OpenVPN GUI." IDS_ERR_CREATE_EVENT "CreateEvent échoué à la terminaison. Code d'événnement: %s" diff --git a/openvpn-gui-res-it.rc b/openvpn-gui-res-it.rc index 17b5993..e6b4e29 100644 --- a/openvpn-gui-res-it.rc +++ b/openvpn-gui-res-it.rc @@ -171,7 +171,6 @@ BEGIN /* OpenVPN */ IDS_ERR_MANY_CONFIGS "OpenVPN GUI non supporta più di %d configurazioni. Per favore contatta l'autore se hai bisogno che ne supporti di più." - IDS_ERR_LOG_CONSTRUCT "Non posso costruire il nome del file di log basato su: %s" IDS_ERR_ONE_CONN_OLD_VER "Puoi avere una sola connessione alla volta in funzionamento quando usi una versione di OpenVPN più vecchia della 2.0-beta6." IDS_ERR_STOP_SERV_OLD_VER "Non puoi usare OpenVPN GUI per far partire una connessione mentre il servizio di OpenVPN è in funzionamento (con OpenVPN 1.5/1.6). Arresta il servizio di OpenVPN se vuoi usare OpenVPN GUI." IDS_ERR_CREATE_EVENT "Il creatore di eventi è fallito all'uscita dell'evento: %s" diff --git a/openvpn-gui-res-nl.rc b/openvpn-gui-res-nl.rc index 67be872..33a0565 100644 --- a/openvpn-gui-res-nl.rc +++ b/openvpn-gui-res-nl.rc @@ -171,7 +171,6 @@ BEGIN /* OpenVPN */ IDS_ERR_MANY_CONFIGS "OpenVPN GUI ondersteunt niet meer dan %d configuratie's. Neem contact op met de auteur indien u er meer wenst." - IDS_ERR_LOG_CONSTRUCT "Kan geen logfile aanmaken met naam: %s" IDS_ERR_ONE_CONN_OLD_VER "Er kan maar 1 verbinding per OpenVPN-client gestart worden met versie's ouder dan OpenVPN 2.0-beta6." IDS_ERR_STOP_SERV_OLD_VER "De OpenVPN GUI kan niet gestart worden als de OpenVPN Service actief is. (OpenVPN 1.5/1.6). Stop eerst de OpenVPN Service indien u de OpenVPN GUI wenst te gebruiken." IDS_ERR_CREATE_EVENT "CreateEvent mislukt tijdens exit event: %s" diff --git a/openvpn-gui-res-no.rc b/openvpn-gui-res-no.rc index 3ae3628..574ffa5 100644 --- a/openvpn-gui-res-no.rc +++ b/openvpn-gui-res-no.rc @@ -176,7 +176,6 @@ BEGIN /* OpenVPN */ IDS_ERR_MANY_CONFIGS "Du kan ikke ha fler enn %d konfig-filer. Kontakt utvikleren av OpenVPN GUI om du har behov for å håndtere fler." - IDS_ERR_LOG_CONSTRUCT "Feil under opprettelse av logg filnavn: %s" IDS_ERR_ONE_CONN_OLD_VER "Du kan bara ha en tilkobling igang samtidig med eldre versjoner av OpenVPN enn 2.0-beta6." IDS_ERR_STOP_SERV_OLD_VER "Du kan ikke koble til med OpenVPN GUI når OpenVPN Service kjører. (kun OpenVPN 1.5/1.6). Stopp tjenesten først om du vil benytte OpenVPN GUI." IDS_ERR_CREATE_EVENT "CreateEvent feilet med å opprette event: %s" diff --git a/openvpn-gui-res-pt.rc b/openvpn-gui-res-pt.rc index 913172c..3504387 100644 --- a/openvpn-gui-res-pt.rc +++ b/openvpn-gui-res-pt.rc @@ -172,7 +172,6 @@ BEGIN /* OpenVPN */ IDS_ERR_MANY_CONFIGS "OpenVPN GUI não suporta mais do que %d configurações. Contate o autor se você necessita mais." - IDS_ERR_LOG_CONSTRUCT "Impossível criar arquivo de log baseado em: %s" IDS_ERR_ONE_CONN_OLD_VER "Você pode ter apenas uma conexão rodando ao mesmo tempo quando estiver usando uma versão anterior a OpenVPN 2.0-beta6." IDS_ERR_STOP_SERV_OLD_VER "Você não pode usar o OpenVPN GUI para iniciar uma conexão enquanto o servico OpenVPN estiver rodando (com OpenVPN 1.5/1.6). Pare o serviço OpenVPN primero se você quer utilizar o OpenVPN GUI." IDS_ERR_CREATE_EVENT "CreateEvent falhou no evento de saída: %s" diff --git a/openvpn-gui-res-se.rc b/openvpn-gui-res-se.rc index 988200b..5ada70a 100644 --- a/openvpn-gui-res-se.rc +++ b/openvpn-gui-res-se.rc @@ -177,7 +177,6 @@ BEGIN /* OpenVPN */ IDS_ERR_MANY_CONFIGS "Du kan inte ha fler än %d konfig-filer. Kontakta utvecklaren av OpenVPN GUI om du har behov av att hantera fler." - IDS_ERR_LOG_CONSTRUCT "Fel vid konstruering av logg filnamn: %s" IDS_ERR_ONE_CONN_OLD_VER "Du kan bara ha en uppkoppling igång samtidigt med äldre versioner av OpenVPN än 2.0-beta6." IDS_ERR_STOP_SERV_OLD_VER "Du kan inte ansluta med OpenVPN GUI medan OpenVPN Service är igång. (med OpenVPN 1.5/1.6). Stoppa tjänsten först om du vill använda OpenVPN GUI." IDS_ERR_CREATE_EVENT "CreateEvent misslyckades med att skapa event: %s" diff --git a/openvpn-gui-res.h b/openvpn-gui-res.h index 195340d..07340e1 100644 --- a/openvpn-gui-res.h +++ b/openvpn-gui-res.h @@ -116,7 +116,6 @@ /* OpenVpn Related */ #define IDS_ERR_MANY_CONFIGS 1201 -#define IDS_ERR_LOG_CONSTRUCT 1202 #define IDS_ERR_ONE_CONN_OLD_VER 1203 #define IDS_ERR_STOP_SERV_OLD_VER 1204 #define IDS_ERR_CREATE_EVENT 1205 diff --git a/openvpn_config.c b/openvpn_config.c index 07c3e3e..71fa6af 100644 --- a/openvpn_config.c +++ b/openvpn_config.c @@ -2,6 +2,7 @@ * OpenVPN-GUI -- A Windows GUI for OpenVPN. * * Copyright (C) 2004 Mathias Sundman + * 2010 Heiko Hund * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,7 +20,6 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - #include #include "config.h" @@ -28,227 +28,153 @@ #include "options.h" #include "localization.h" -#define MATCH_FALSE 0 -#define MATCH_FILE 1 -#define MATCH_DIR 2 +typedef enum +{ + match_false, + match_file, + match_dir +} match_t; extern struct options o; +static match_t +match(const WIN32_FIND_DATA *find, const TCHAR *ext) +{ + size_t ext_len = _tcslen(ext); + int i; + + if (find->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + return match_dir; + + if (ext_len == 0) + return match_file; + + i = _tcslen(find->cFileName) - ext_len - 1; + + if (i > 0 && find->cFileName[i] == '.' + && _tcsicmp(find->cFileName + i + 1, ext) == 0) + return match_file; + + return match_false; +} + + static int -match (const WIN32_FIND_DATA *find, const TCHAR *ext) +ConfigAlreadyExists(TCHAR *newconfig) { - int i; - - if (find->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - return MATCH_DIR; - - if (!_tcslen(ext)) - return MATCH_FILE; - - i = _tcslen(find->cFileName) - _tcslen(ext) - 1; - if (i < 1) - return MATCH_FALSE; - - if (find->cFileName[i] == '.' && !_tcsicmp(find->cFileName + i + 1, ext)) - return MATCH_FILE; - else - return MATCH_FALSE; + int i; + for (i = 0; i < o.num_configs; ++i) + { + if (_tcsicmp(o.cnn[i].config_file, newconfig) == 0) + return true; + } + return false; } -/* - * Modify the extension on a filename. - */ -static bool -modext (TCHAR *dest, unsigned int size, const TCHAR *src, const TCHAR *newext) + +static void +AddConfigFileToList(int config, TCHAR *filename, TCHAR *config_dir) { - int i; + connection_t *conn = &o.cnn[config]; + int i; - if (size > 0 && (_tcslen(src) + 1) <= size) + _tcsncpy(conn->config_file, filename, _tsizeof(conn->config_file) - 1); + _tcsncpy(conn->config_dir, config_dir, _tsizeof(conn->config_dir) - 1); + _tcsncpy(conn->config_name, conn->config_file, _tsizeof(conn->config_name) - 1); + conn->config_name[_tcslen(conn->config_name) - _tcslen(o.ext_string) - 1] = _T('\0'); + _sntprintf_0(conn->log_path, _T("%s\\%s.log"), o.log_dir, conn->config_name); + + /* Check if connection should be autostarted */ + for (i = 0; i < MAX_CONFIGS && o.auto_connect[i]; ++i) { - _tcscpy(dest, src); - dest[size - 1] = _T('\0'); - i = _tcslen(dest); - while (--i >= 0) - { - if (dest[i] == _T('\\')) - break; - if (dest[i] == _T('.')) - { - dest[i] = _T('\0'); - break; - } - } - if (_tcslen(dest) + _tcslen(newext) + 2 <= size) - { - _tcscat(dest, _T(".")); - _tcscat(dest, newext); - return true; - } - dest[0] = _T('\0'); - } - return false; -} - -int ConfigAlreadyExists(TCHAR newconfig[]) -{ - int i; - - for (i=0; iconfig_file, o.auto_connect[i]) == 0) { - o.cnn[config].auto_connect = true; - break; + conn->auto_connect = true; + break; } } - - return(true); } -int +void BuildFileList() { - WIN32_FIND_DATA find_obj; - HANDLE find_handle; - BOOL more_files; - TCHAR find_string[MAX_PATH]; - TCHAR subdir_table[MAX_CONFIG_SUBDIRS][MAX_PATH]; - int subdir=0; - int subdir_counter=0; + WIN32_FIND_DATA find_obj; + HANDLE find_handle; + TCHAR find_string[MAX_PATH]; + TCHAR subdir_table[MAX_CONFIG_SUBDIRS][MAX_PATH]; + int subdirs = 0; + int i; - /* Reset config counter */ - o.num_configs=0; - - _sntprintf_0(find_string, _T("%s\\*"), o.config_dir); + /* Reset config counter */ + o.num_configs = 0; - find_handle = FindFirstFile (find_string, &find_obj); - if (find_handle == INVALID_HANDLE_VALUE) + _sntprintf_0(find_string, _T("%s\\*"), o.config_dir); + find_handle = FindFirstFile(find_string, &find_obj); + if (find_handle == INVALID_HANDLE_VALUE) + return; + + /* Loop over each config file in main config dir */ + do { - return(true); + if (o.num_configs >= MAX_CONFIGS) + { + ShowLocalizedMsg(IDS_ERR_MANY_CONFIGS, MAX_CONFIGS); + break; + } + + match_t match_type = match(&find_obj, o.ext_string); + if (match_type == match_file) + { + AddConfigFileToList(o.num_configs++, find_obj.cFileName, o.config_dir); + } + else if (match_type == match_dir) + { + if (_tcsncmp(find_obj.cFileName, _T("."), _tcslen(find_obj.cFileName)) != 0 + && _tcsncmp(find_obj.cFileName, _T(".."), _tcslen(find_obj.cFileName)) != 0 + && subdirs < MAX_CONFIG_SUBDIRS) + { + /* Add dir to dir_table */ + _sntprintf_0(subdir_table[subdirs], _T("%s\\%s"), o.config_dir, find_obj.cFileName); + subdirs++; + } + } + } while (FindNextFile(find_handle, &find_obj)); + + FindClose(find_handle); + + /* Loop over each config file in every subdir */ + for (i = 0; i < subdirs; ++i) + { + _sntprintf_0(find_string, _T("%s\\*"), subdir_table[i]); + + find_handle = FindFirstFile (find_string, &find_obj); + if (find_handle == INVALID_HANDLE_VALUE) + continue; + + do + { + if (o.num_configs >= MAX_CONFIGS) + { + ShowLocalizedMsg(IDS_ERR_MANY_CONFIGS, MAX_CONFIGS); + FindClose(find_handle); + return; + } + + /* does file have the correct type and extension? */ + if (match(&find_obj, o.ext_string) != match_file) + continue; + + if (ConfigAlreadyExists(find_obj.cFileName)) + { + ShowLocalizedMsg(IDS_ERR_CONFIG_EXIST, find_obj.cFileName); + continue; + } + + AddConfigFileToList(o.num_configs++, find_obj.cFileName, subdir_table[i]); + } while (FindNextFile(find_handle, &find_obj)); + + FindClose(find_handle); } - - /* - * Loop over each config file in main config dir - */ - do - { - if (o.num_configs >= MAX_CONFIGS) - { - /* too many configs */ - ShowLocalizedMsg(IDS_ERR_MANY_CONFIGS, MAX_CONFIGS); - break; - } - - /* does file have the correct type and extension? */ - if (match (&find_obj, o.ext_string) == MATCH_FILE) - { - /* Add config file to list */ - AddConfigFileToList(o.num_configs, find_obj.cFileName, o.config_dir); - - o.num_configs++; - } - - if (match (&find_obj, o.ext_string) == MATCH_DIR) - { - if ((_tcsncmp(find_obj.cFileName, _T("."), _tcslen(find_obj.cFileName)) != 0) && - (_tcsncmp(find_obj.cFileName, _T(".."), _tcslen(find_obj.cFileName)) != 0) && - (subdir < MAX_CONFIG_SUBDIRS)) - { - /* Add dir to dir_table */ - _sntprintf_0(subdir_table[subdir], _T("%s\\%s"), o.config_dir, find_obj.cFileName); - subdir++; - } - } - - /* more files to process? */ - more_files = FindNextFile (find_handle, &find_obj); - } while (more_files); - - FindClose (find_handle); - - - /* - * Loop over each config file in every subdir - */ - for (subdir_counter=0; subdir_counter < subdir; subdir_counter++) - { - - _sntprintf_0(find_string, _T("%s\\*"), subdir_table[subdir_counter]); - - find_handle = FindFirstFile (find_string, &find_obj); - if (find_handle == INVALID_HANDLE_VALUE) - continue; - - do - { - if (o.num_configs >= MAX_CONFIGS) - { - /* too many configs */ - ShowLocalizedMsg(IDS_ERR_MANY_CONFIGS, MAX_CONFIGS); - FindClose (find_handle); - return(true); - } - - /* does file have the correct type and extension? */ - if (match (&find_obj, o.ext_string) == MATCH_FILE) - { - if (!ConfigAlreadyExists(find_obj.cFileName)) - { - /* Add config file to list */ - AddConfigFileToList(o.num_configs, find_obj.cFileName, subdir_table[subdir_counter]); - o.num_configs++; - } - else - { - /* Config filename already exists */ - ShowLocalizedMsg(IDS_ERR_CONFIG_EXIST, find_obj.cFileName); - } - } - - /* more files to process? */ - more_files = FindNextFile (find_handle, &find_obj); - } while (more_files); - - FindClose (find_handle); - } - - return (true); } diff --git a/openvpn_config.h b/openvpn_config.h index 861c917..e23468b 100644 --- a/openvpn_config.h +++ b/openvpn_config.h @@ -22,6 +22,6 @@ #ifndef OPENVPN_CONFIG_H #define OPENVPN_CONFIG_H -int BuildFileList(); +void BuildFileList(); #endif diff --git a/options.h b/options.h index 1da3cf0..a1fbb64 100644 --- a/options.h +++ b/options.h @@ -47,7 +47,7 @@ #define SERVICE_CONNECTED 2 /* Connections parameters */ -struct connections +typedef struct { TCHAR config_file[MAX_PATH]; /* Name of the config file */ TCHAR config_name[MAX_PATH]; /* Name of the connection */ @@ -68,7 +68,7 @@ struct connections HANDLE hStdOut; HANDLE hStdIn; HWND hwndStatus; /* Handle to Status Dialog Window */ -}; +} connection_t; /* All options used within OpenVPN GUI */ struct options @@ -77,7 +77,7 @@ struct options const TCHAR *auto_connect[MAX_CONFIGS]; /* Connection parameters */ - struct connections cnn[MAX_CONFIGS]; /* Connection structure */ + connection_t cnn[MAX_CONFIGS]; /* Connection structure */ int num_configs; /* Number of configs */ int oldversion; /* 1=OpenVPN version below 2.0-beta6 */