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-2024 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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifndef PLATFORM_H
25#define PLATFORM_H
26
27#ifdef HAVE_SYS_TYPES_H
28#include <sys/types.h>
29#endif
30
31#ifdef HAVE_SYS_STAT_H
32#include <sys/stat.h>
33#endif
34
35#ifdef HAVE_UNISTD_H
36#include <unistd.h>
37#endif
38
39#ifdef HAVE_PWD_H
40#include <pwd.h>
41#endif
42
43#ifdef HAVE_GRP_H
44#include <grp.h>
45#endif
46
47#ifdef HAVE_STDIO_H
48#include <stdio.h>
49#endif
50
51#ifdef HAVE_GETRLIMIT
52#include <sys/resource.h>
53#endif
54
55#include "basic.h"
56#include "buffer.h"
57
58/* forward declared to avoid large amounts of extra includes */
59struct context;
60
61/* Get/Set UID of process */
62
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#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
76 const char *groupname;
77 gid_t gid;
78#else
79 int dummy;
80#endif
81};
82
83bool platform_user_get(const char *username, struct platform_state_user *state);
84
85bool platform_group_get(const char *groupname, struct platform_state_group *state);
86
87void platform_user_group_set(const struct platform_state_user *user_state,
88 const struct platform_state_group *group_state,
89 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,
157 struct gc_arena *gc);
158
160bool platform_absolute_pathname(const char *pathname);
161
163bool platform_test_file(const char *filename);
164
165#endif /* ifndef PLATFORM_H */
bool platform_test_file(const char *filename)
Return true if filename can be opened for read.
Definition platform.c:660
void platform_sleep_milliseconds(unsigned int n)
Definition platform.c:474
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:541
int platform_ret_code(int stat)
Return an exit code if valid and between 0 and 255, -1 otherwise.
Definition platform.c:425
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:217
static int platform_state_user_uid(const struct platform_state_user *s)
Definition platform.h:97
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:594
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:458
bool platform_user_get(const char *username, struct platform_state_user *state)
Definition platform.c:79
bool platform_unlink(const char *filename)
Definition platform.c:488
struct _stat platform_stat_t
Definition platform.h:142
FILE * platform_fopen(const char *path, const char *mode)
Definition platform.c:501
bool platform_absolute_pathname(const char *pathname)
Return true if pathname is absolute.
Definition platform.c:641
int platform_chdir(const char *dir)
Definition platform.c:393
void platform_mlockall(bool print_msg)
Definition platform.c:344
void platform_chroot(const char *path)
Definition platform.c:55
bool platform_group_get(const char *groupname, struct platform_state_group *state)
Definition platform.c:123
int platform_open(const char *path, int flags, int mode)
Definition platform.c:514
bool platform_system_ok(int stat)
interpret the status code returned by execve()
Definition platform.c:414
int platform_stat(const char *path, platform_stat_t *buf)
Definition platform.c:527
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:117
struct gc_arena gc
Definition test_ssl.c:155