OpenVPN
platform.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2025 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef PLATFORM_H
24#define PLATFORM_H
25
26#ifdef HAVE_SYS_TYPES_H
27#include <sys/types.h>
28#endif
29
30#ifdef HAVE_SYS_STAT_H
31#include <sys/stat.h>
32#endif
33
34#ifdef HAVE_UNISTD_H
35#include <unistd.h>
36#endif
37
38#ifdef HAVE_PWD_H
39#include <pwd.h>
40#endif
41
42#ifdef HAVE_GRP_H
43#include <grp.h>
44#endif
45
46#ifdef HAVE_STDIO_H
47#include <stdio.h>
48#endif
49
50#ifdef HAVE_GETRLIMIT
51#include <sys/resource.h>
52#endif
53
54#include "basic.h"
55#include "buffer.h"
56
57/* forward declared to avoid large amounts of extra includes */
58struct context;
59
60/* Get/Set UID of process */
61
63{
64#if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
65 const char *username;
66 uid_t uid;
67#else
68 int dummy;
69#endif
70};
71
72/* Get/Set GID of process */
73
75{
76#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
77 const char *groupname;
78 gid_t gid;
79#else
80 int dummy;
81#endif
82};
83
84bool platform_user_get(const char *username, struct platform_state_user *state);
85
86bool platform_group_get(const char *groupname, struct platform_state_group *state);
87
88void platform_user_group_set(const struct platform_state_user *user_state,
89 const struct platform_state_group *group_state, struct context *c);
90
91
92/*
93 * Extract UID or GID
94 */
95
96static inline int
98{
99#if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
100 return s->uid;
101#endif
102 return -1;
103}
104
105static inline int
107{
108#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
109 return s->gid;
110#endif
111 return -1;
112}
113
114void platform_chroot(const char *path);
115
116void platform_nice(int niceval);
117
118unsigned int platform_getpid(void);
119
120void platform_mlockall(bool print_msg); /* Disable paging */
121
122int platform_chdir(const char *dir);
123
125bool platform_system_ok(int stat);
126
128int platform_ret_code(int stat);
129
130int platform_access(const char *path, int mode);
131
132void platform_sleep_milliseconds(unsigned int n);
133
134/* delete a file, return true if succeeded */
135bool platform_unlink(const char *filename);
136
137FILE *platform_fopen(const char *path, const char *mode);
138
139int platform_open(const char *path, int flags, int mode);
140
141#ifdef _WIN32
142typedef struct _stat platform_stat_t;
143#else
144typedef struct stat platform_stat_t;
145#endif
146int platform_stat(const char *path, platform_stat_t *buf);
147
152const char *platform_create_temp_file(const char *directory, const char *prefix,
153 struct gc_arena *gc);
154
156const char *platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc);
157
159bool platform_absolute_pathname(const char *pathname);
160
162bool platform_test_file(const char *filename);
163
164#endif /* ifndef PLATFORM_H */
bool platform_test_file(const char *filename)
Return true if filename can be opened for read.
Definition platform.c:659
void platform_sleep_milliseconds(unsigned int n)
Definition platform.c:477
unsigned int platform_getpid(void)
Definition platform.c:337
const char * platform_create_temp_file(const char *directory, const char *prefix, struct gc_arena *gc)
Create a temporary file in directory, returns the filename of the created file.
Definition platform.c:544
int platform_ret_code(int stat)
Return an exit code if valid and between 0 and 255, -1 otherwise.
Definition platform.c:428
void platform_user_group_set(const struct platform_state_user *user_state, const struct platform_state_group *group_state, struct context *c)
Definition platform.c:222
static int platform_state_user_uid(const struct platform_state_user *s)
Definition platform.h:97
void platform_nice(int niceval)
Definition platform.c:315
const char * platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc)
Put a directory and filename together.
Definition platform.c:595
static int platform_state_group_gid(const struct platform_state_group *s)
Definition platform.h:106
int platform_access(const char *path, int mode)
Definition platform.c:461
bool platform_user_get(const char *username, struct platform_state_user *state)
Definition platform.c:80
bool platform_unlink(const char *filename)
Definition platform.c:491
struct _stat platform_stat_t
Definition platform.h:142
FILE * platform_fopen(const char *path, const char *mode)
Definition platform.c:504
bool platform_absolute_pathname(const char *pathname)
Return true if pathname is absolute.
Definition platform.c:640
int platform_chdir(const char *dir)
Definition platform.c:396
void platform_mlockall(bool print_msg)
Definition platform.c:348
void platform_chroot(const char *path)
Definition platform.c:54
bool platform_group_get(const char *groupname, struct platform_state_group *state)
Definition platform.c:126
int platform_open(const char *path, int flags, int mode)
Definition platform.c:517
bool platform_system_ok(int stat)
interpret the status code returned by execve()
Definition platform.c:417
int platform_stat(const char *path, platform_stat_t *buf)
Definition platform.c:530
Contains all state information for one tunnel.
Definition openvpn.h:474
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
struct gc_arena gc
Definition test_ssl.c:154