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#endif
69};
70
71/* Get/Set GID of process */
72
74{
75#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
76 const char *groupname;
77 gid_t gid;
78#endif
80};
81
82bool platform_user_get(const char *username, struct platform_state_user *state);
83
84bool platform_group_get(const char *groupname, struct platform_state_group *state);
85
86void platform_user_group_set(const struct platform_state_user *user_state,
87 const struct platform_state_group *group_state, struct context *c);
88
89
90void platform_chroot(const char *path);
91
92void platform_nice(int niceval);
93
94unsigned int platform_getpid(void);
95
96void platform_mlockall(bool print_msg); /* Disable paging */
97
98int platform_chdir(const char *dir);
99
101bool platform_system_ok(int stat);
102
104int platform_ret_code(int stat);
105
106int platform_access(const char *path, int mode);
107
108void platform_sleep_milliseconds(unsigned int n);
109
110/* delete a file, return true if succeeded */
111bool platform_unlink(const char *filename);
112
113FILE *platform_fopen(const char *path, const char *mode);
114
115int platform_open(const char *path, int flags, int mode);
116
117#ifdef _WIN32
118typedef struct _stat platform_stat_t;
119#else
120typedef struct stat platform_stat_t;
121#endif
122int platform_stat(const char *path, platform_stat_t *buf);
123
128const char *platform_create_temp_file(const char *directory, const char *prefix,
129 struct gc_arena *gc);
130
132const char *platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc);
133
135bool platform_absolute_pathname(const char *pathname);
136
138bool platform_test_file(const char *filename);
139
140#endif /* ifndef PLATFORM_H */
bool platform_test_file(const char *filename)
Return true if filename can be opened for read.
Definition platform.c:655
void platform_sleep_milliseconds(unsigned int n)
Definition platform.c:473
unsigned int platform_getpid(void)
Definition platform.c:333
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:540
int platform_ret_code(int stat)
Return an exit code if valid and between 0 and 255, -1 otherwise.
Definition platform.c:424
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:218
void platform_nice(int niceval)
Definition platform.c:311
const char * platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc)
Put a directory and filename together.
Definition platform.c:591
int platform_access(const char *path, int mode)
Definition platform.c:457
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:487
struct _stat platform_stat_t
Definition platform.h:118
FILE * platform_fopen(const char *path, const char *mode)
Definition platform.c:500
bool platform_absolute_pathname(const char *pathname)
Return true if pathname is absolute.
Definition platform.c:636
int platform_chdir(const char *dir)
Definition platform.c:392
void platform_mlockall(bool print_msg)
Definition platform.c:344
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:124
int platform_open(const char *path, int flags, int mode)
Definition platform.c:513
bool platform_system_ok(int stat)
interpret the status code returned by execve()
Definition platform.c:413
int platform_stat(const char *path, platform_stat_t *buf)
Definition platform.c:526
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:131