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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/*
* OpenVPN -- An application to securely tunnel IP networks
* over a single TCP/UDP port, with support for SSL/TLS-based
* session authentication and key exchange,
* packet encryption, packet authentication, and
* packet compression.
*
* Copyright (C) 2002-2017 OpenVPN Technologies, Inc. <sales@openvpn.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* 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; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_MSC_VER)
#include "config-msvc.h"
#endif
#include "syshead.h"
#include "buffer.h"
#include "error.h"
#include "win32.h"
#include "memdbg.h"
#include "platform.h"
/* Redefine the top level directory of the filesystem
* to restrict access to files for security */
void
platform_chroot(const char *path)
{
if (path)
{
#ifdef HAVE_CHROOT
const char *top = "/";
if (chroot(path))
{
msg(M_ERR, "chroot to '%s' failed", path);
}
if (platform_chdir(top))
{
msg(M_ERR, "cd to '%s' failed", top);
}
msg(M_INFO, "chroot to '%s' and cd to '%s' succeeded", path, top);
#else /* ifdef HAVE_CHROOT */
msg(M_FATAL, "Sorry but I can't chroot to '%s' because this operating system doesn't appear to support the chroot() system call", path);
#endif
}
}
/* Get/Set UID of process */
bool
platform_user_get(const char *username, struct platform_state_user *state)
{
bool ret = false;
CLEAR(*state);
if (username)
{
#if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
state->pw = getpwnam(username);
if (!state->pw)
{
msg(M_ERR, "failed to find UID for user %s", username);
}
state->username = username;
ret = true;
#else /* if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) */
msg(M_FATAL, "cannot get UID for user %s -- platform lacks getpwname() or setuid() system calls", username);
#endif
}
return ret;
}
void
platform_user_set(const struct platform_state_user *state)
{
#if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
if (state->username && state->pw)
{
if (setuid(state->pw->pw_uid))
{
msg(M_ERR, "setuid('%s') failed", state->username);
}
msg(M_INFO, "UID set to %s", state->username);
}
#endif
}
/* Get/Set GID of process */
bool
platform_group_get(const char *groupname, struct platform_state_group *state)
{
bool ret = false;
CLEAR(*state);
if (groupname)
{
#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
state->gr = getgrnam(groupname);
if (!state->gr)
{
msg(M_ERR, "failed to find GID for group %s", groupname);
}
state->groupname = groupname;
ret = true;
#else /* if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID) */
msg(M_FATAL, "cannot get GID for group %s -- platform lacks getgrnam() or setgid() system calls", groupname);
#endif
}
return ret;
}
void
platform_group_set(const struct platform_state_group *state)
{
#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
if (state->groupname && state->gr)
{
if (setgid(state->gr->gr_gid))
{
msg(M_ERR, "setgid('%s') failed", state->groupname);
}
msg(M_INFO, "GID set to %s", state->groupname);
#ifdef HAVE_SETGROUPS
{
gid_t gr_list[1];
gr_list[0] = state->gr->gr_gid;
if (setgroups(1, gr_list))
{
msg(M_ERR, "setgroups('%s') failed", state->groupname);
}
}
#endif
}
#endif
}
/* Change process priority */
void
platform_nice(int niceval)
{
if (niceval)
{
#ifdef HAVE_NICE
errno = 0;
if (nice(niceval) < 0 && errno != 0)
{
msg(M_WARN | M_ERRNO, "WARNING: nice %d failed", niceval);
}
else
{
msg(M_INFO, "nice %d succeeded", niceval);
}
#else /* ifdef HAVE_NICE */
msg(M_WARN, "WARNING: nice %d failed (function not implemented)", niceval);
#endif
}
}
/* Get current PID */
unsigned int
platform_getpid(void)
{
#ifdef _WIN32
return (unsigned int) GetCurrentProcessId();
#else
#ifdef HAVE_GETPID
return (unsigned int) getpid();
#else
return 0;
#endif
#endif
}
/* Disable paging */
void
platform_mlockall(bool print_msg)
{
#ifdef HAVE_MLOCKALL
if (mlockall(MCL_CURRENT | MCL_FUTURE))
{
msg(M_WARN | M_ERRNO, "WARNING: mlockall call failed");
}
else if (print_msg)
{
msg(M_INFO, "mlockall call succeeded");
}
#else /* ifdef HAVE_MLOCKALL */
msg(M_WARN, "WARNING: mlockall call failed (function not implemented)");
#endif
}
/*
* Wrapper for chdir library function
*/
int
platform_chdir(const char *dir)
{
#ifdef HAVE_CHDIR
#ifdef _WIN32
int res;
struct gc_arena gc = gc_new();
res = _wchdir(wide_string(dir, &gc));
gc_free(&gc);
return res;
#else /* ifdef _WIN32 */
return chdir(dir);
#endif
#else /* ifdef HAVE_CHDIR */
return -1;
#endif
}
/*
* convert execve() return into a success/failure value
*/
bool
platform_system_ok(int stat)
{
#ifdef _WIN32
return stat == 0;
#else
return stat != -1 && WIFEXITED(stat) && WEXITSTATUS(stat) == 0;
#endif
}
int
platform_access(const char *path, int mode)
{
#ifdef _WIN32
struct gc_arena gc = gc_new();
int ret = _waccess(wide_string(path, &gc), mode & ~X_OK);
gc_free(&gc);
return ret;
#else
return access(path, mode);
#endif
}
/*
* Go to sleep for n milliseconds.
*/
void
platform_sleep_milliseconds(unsigned int n)
{
#ifdef _WIN32
Sleep(n);
#else
struct timeval tv;
tv.tv_sec = n / 1000;
tv.tv_usec = (n % 1000) * 1000;
select(0, NULL, NULL, NULL, &tv);
#endif
}
/*
* Go to sleep indefinitely.
*/
void
platform_sleep_until_signal(void)
{
#ifdef _WIN32
ASSERT(0);
#else
select(0, NULL, NULL, NULL, NULL);
#endif
}
/* delete a file, return true if succeeded */
bool
platform_unlink(const char *filename)
{
#if defined(_WIN32)
struct gc_arena gc = gc_new();
BOOL ret = DeleteFileW(wide_string(filename, &gc));
gc_free(&gc);
return (ret != 0);
#elif defined(HAVE_UNLINK)
return (unlink(filename) == 0);
#else /* if defined(_WIN32) */
return false;
#endif
}
FILE *
platform_fopen(const char *path, const char *mode)
{
#ifdef _WIN32
struct gc_arena gc = gc_new();
FILE *f = _wfopen(wide_string(path, &gc), wide_string(mode, &gc));
gc_free(&gc);
return f;
#else
return fopen(path, mode);
#endif
}
int
platform_open(const char *path, int flags, int mode)
{
#ifdef _WIN32
struct gc_arena gc = gc_new();
int fd = _wopen(wide_string(path, &gc), flags, mode);
gc_free(&gc);
return fd;
#else
return open(path, flags, mode);
#endif
}
int
platform_stat(const char *path, platform_stat_t *buf)
{
#ifdef _WIN32
struct gc_arena gc = gc_new();
int res = _wstat(wide_string(path, &gc), buf);
gc_free(&gc);
return res;
#else
return stat(path, buf);
#endif
}
|