forked from OpenVPN/openvpn-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenvpn_config.c
241 lines (200 loc) · 6.89 KB
/
openvpn_config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
*
* Copyright (C) 2004 Mathias Sundman <[email protected]>
* 2010 Heiko Hund <[email protected]>
* 2016 Selva Nair <[email protected]>
*
* 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;
_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 |= 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)
{
c->auto_connect = true;
break;
}
}
/* check whether passwords are saved */
if (IsAuthPassSaved(c->config_name))
c->flags |= FLAG_SAVE_AUTH_PASS;
if (IsKeyPassSaved(c->config_name))
c->flags |= FLAG_SAVE_KEY_PASS;
}
static void
BuildFileList0(const TCHAR *config_dir, bool warn_duplicates)
{
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;
_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 main 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 (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);
}
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"), 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))
{
if (warn_duplicates)
ShowLocalizedMsg(IDS_ERR_CONFIG_EXIST, find_obj.cFileName);
continue;
}
if (CheckReadAccess (subdir_table[i], find_obj.cFileName))
AddConfigFileToList(o.num_configs++, find_obj.cFileName, subdir_table[i]);
} while (FindNextFile(find_handle, &find_obj));
FindClose(find_handle);
}
}
void
BuildFileList()
{
static bool issue_warnings = true;
/*
* 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;
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, o.config_dir, o.global_config_dir);
issue_warnings = false;
}