You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

325 lines
9.2 KiB

/*
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
*
* Copyright (C) 2004 Mathias Sundman <mathias@nilings.se>
* 2010 Heiko Hund <heikoh@users.sf.net>
* 2016 Selva Nair <selva.nair@gmail.com>
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <windows.h>
#include "main.h"
#include "openvpn-gui-res.h"
#include "options.h"
#include "localization.h"
#include "save_pass.h"
#include "misc.h"
#include "passphrase.h"
typedef enum
{
match_false,
match_file,
match_dir
} match_t;
extern options_t 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 bool
CheckReadAccess (const TCHAR *dir, const TCHAR *file)
{
TCHAR path[MAX_PATH];
_sntprintf_0 (path, _T("%s\\%s"), dir, file);
return CheckFileAccess (path, GENERIC_READ);
}
static int
ConfigAlreadyExists(TCHAR *newconfig)
{
int i;
for (i = 0; i < o.num_configs; ++i)
{
if (_tcsicmp(o.conn[i].config_file, newconfig) == 0)
return true;
}
return false;
}
static void
AddConfigFileToList(int config, const TCHAR *filename, const TCHAR *config_dir)
{
connection_t *c = &o.conn[config];
int i;
memset(c, 0, sizeof(*c));
_tcsncpy(c->config_file, filename, _countof(c->config_file) - 1);
_tcsncpy(c->config_dir, config_dir, _countof(c->config_dir) - 1);
_tcsncpy(c->config_name, c->config_file, _countof(c->config_name) - 1);
c->config_name[_tcslen(c->config_name) - _tcslen(o.ext_string) - 1] = _T('\0');
_sntprintf_0(c->log_path, _T("%s\\%s.log"), o.log_dir, c->config_name);
c->manage.sk = INVALID_SOCKET;
c->manage.skaddr.sin_family = AF_INET;
c->manage.skaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
c->manage.skaddr.sin_port = htons(25340 + config);
#ifndef DISABLE_CHANGE_PASSWORD
if (CheckKeyFileWriteAccess (c))
c->flags |= FLAG_ALLOW_CHANGE_PASSPHRASE;
#endif
/* Check if connection should be autostarted */
for (i = 0; i < MAX_CONFIGS && o.auto_connect[i]; ++i)
{
if (_tcsicmp(c->config_file, o.auto_connect[i]) == 0
|| _tcsicmp(c->config_name, o.auto_connect[i]) == 0)
{
c->auto_connect = true;
break;
}
}
/* check whether passwords are saved */
if (o.disable_save_passwords)
{
DisableSavePasswords(c);
}
else
{
if (IsAuthPassSaved(c->config_name))
c->flags |= FLAG_SAVE_AUTH_PASS;
if (IsKeyPassSaved(c->config_name))
c->flags |= FLAG_SAVE_KEY_PASS;
}
}
#define FLAG_WARN_DUPLICATES (0x1)
#define FLAG_WARN_MAX_CONFIGS (0x2)
#define FLAG_ADD_CONFIG_GROUPS (0x4)
/*
* Create a new group with the given name as a child of the
* specified parent group and return pointer to the new group.
* If FLAG_ADD_CONFIG_GROUPS is not enabled, returns the
* parent itself.
*/
static config_group_t *
NewConfigGroup(const wchar_t *name, config_group_t *parent, int flags)
{
if (!(flags & FLAG_ADD_CONFIG_GROUPS))
return parent;
if (!o.groups || o.num_groups == o.max_groups)
{
o.max_groups += 10;
void *tmp = realloc(o.groups, sizeof(*o.groups)*o.max_groups);
if (!tmp)
{
o.max_groups -= 10;
ErrorExit(1, L"Out of memory while grouping configs");
}
o.groups = tmp;
}
config_group_t *cg = &o.groups[o.num_groups];
memset(cg, 0, sizeof(*cg));
_sntprintf_0(cg->name, L"%s", name);
cg->id = o.num_groups++;
cg->parent = parent;
cg->active = false; /* activated later if not empty */
return cg;
}
/*
* All groups that link at least one config to the root are
* enabled. Dangling entries with no terminal configs will stay
* disabled and are not displayed in the menu tree.
*/
static void
ActivateConfigGroups(void)
{
/* the root group is always active */
o.groups[0].active = true;
/* activate all groups that connect a config to the root */
for (int i = 0; i < o.num_configs; i++)
{
config_group_t *cg = o.conn[i].group;
while (cg)
{
cg->active = true;
cg = cg->parent;
}
}
}
/* Scan for configs in config_dir recursing down up to recurse_depth.
* Input: config_dir -- root of the directory to scan from
* group -- the group into which add the configs to
* flags -- enable warnings, use directory based
* grouping of configs etc.
* Currently configs in a directory are grouped together and group is
* a pointer to the current parent group in the global group array |o.groups|
* This may be recursively called until depth becomes 1 and each time
* the group is changed to that of the directory being recursed into.
*/
static void
BuildFileList0(const TCHAR *config_dir, int recurse_depth, config_group_t *group, int flags)
{
WIN32_FIND_DATA find_obj;
HANDLE find_handle;
TCHAR find_string[MAX_PATH];
TCHAR subdir_name[MAX_PATH];
_sntprintf_0(find_string, _T("%s\\*"), config_dir);
find_handle = FindFirstFile(find_string, &find_obj);
if (find_handle == INVALID_HANDLE_VALUE)
return;
/* Loop over each config file in config dir */
do
{
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)
{
if (ConfigAlreadyExists(find_obj.cFileName))
{
if (flags & FLAG_WARN_DUPLICATES)
ShowLocalizedMsg(IDS_ERR_CONFIG_EXIST, find_obj.cFileName);
continue;
}
if (CheckReadAccess (config_dir, find_obj.cFileName))
{
AddConfigFileToList(o.num_configs, find_obj.cFileName, config_dir);
o.conn[o.num_configs++].group = group;
}
}
} while (FindNextFile(find_handle, &find_obj));
FindClose(find_handle);
/* optionally loop over each subdir */
if (recurse_depth <= 1)
return;
find_handle = FindFirstFile (find_string, &find_obj);
if (find_handle == INVALID_HANDLE_VALUE)
return;
do
{
match_t match_type = match(&find_obj, o.ext_string);
if (match_type == match_dir)
{
if (wcscmp(find_obj.cFileName, _T("."))
&& wcscmp(find_obj.cFileName, _T("..")))
{
/* recurse into subdirectory */
_sntprintf_0(subdir_name, _T("%s\\%s"), config_dir, find_obj.cFileName);
config_group_t *sub_group = NewConfigGroup(find_obj.cFileName, group, flags);
BuildFileList0(subdir_name, recurse_depth - 1, sub_group, flags);
}
}
} while (FindNextFile(find_handle, &find_obj));
FindClose(find_handle);
}
void
BuildFileList()
{
static bool issue_warnings = true;
int recurse_depth = 4; /* read config_dir and 3 levels of sub-directories */
int flags = 0;
config_group_t *root = NULL;
if (o.silent_connection)
issue_warnings = false;
/*
* If no connections are active reset num_configs and rescan
* to make a new list. Else we keep all current configs and
* rescan to add any new one's found
*/
if (CountConnState(disconnected) == o.num_configs)
{
o.num_configs = 0;
o.num_groups = 0;
flags |= FLAG_ADD_CONFIG_GROUPS;
root = NewConfigGroup(L"User Profiles", NULL, flags);
}
else
root = &o.groups[0];
if (issue_warnings)
{
flags |= FLAG_WARN_DUPLICATES | FLAG_WARN_MAX_CONFIGS;
}
BuildFileList0 (o.config_dir, recurse_depth, root, flags);
root = NewConfigGroup(L"System Profiles", root, flags);
if (_tcscmp (o.global_config_dir, o.config_dir))
BuildFileList0 (o.global_config_dir, recurse_depth, root, flags);
if (o.num_configs == 0 && issue_warnings)
ShowLocalizedMsg(IDS_NFO_NO_CONFIGS, o.config_dir, o.global_config_dir);
ActivateConfigGroups();
issue_warnings = false;
}