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-2021 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 /* Get/Set UID of process */
59 
61 #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
62  const char *username;
63  struct passwd *pw;
64 #else
65  int dummy;
66 #endif
67 };
68 
69 /* Get/Set GID of process */
70 
72 #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
73  const char *groupname;
74  struct group *gr;
75 #else
76  int dummy;
77 #endif
78 };
79 
80 bool platform_user_get(const char *username, struct platform_state_user *state);
81 
82 void platform_user_set(const struct platform_state_user *state);
83 
84 bool platform_group_get(const char *groupname, struct platform_state_group *state);
85 
86 void platform_group_set(const struct platform_state_group *state);
87 
88 /*
89  * Extract UID or GID
90  */
91 
92 static inline int
94 {
95 #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
96  if (s->pw)
97  {
98  return s->pw->pw_uid;
99  }
100 #endif
101  return -1;
102 }
103 
104 static inline int
106 {
107 #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
108  if (s->gr)
109  {
110  return s->gr->gr_gid;
111  }
112 #endif
113  return -1;
114 }
115 
116 void platform_chroot(const char *path);
117 
118 void platform_nice(int niceval);
119 
120 unsigned int platform_getpid(void);
121 
122 void platform_mlockall(bool print_msg); /* Disable paging */
123 
124 int platform_chdir(const char *dir);
125 
127 bool platform_system_ok(int stat);
128 
130 int platform_ret_code(int stat);
131 
132 int platform_access(const char *path, int mode);
133 
134 void platform_sleep_milliseconds(unsigned int n);
135 
136 void platform_sleep_until_signal(void);
137 
138 /* delete a file, return true if succeeded */
139 bool platform_unlink(const char *filename);
140 
141 int platform_putenv(char *string);
142 
143 FILE *platform_fopen(const char *path, const char *mode);
144 
145 int platform_open(const char *path, int flags, int mode);
146 
147 #ifdef _WIN32
148 typedef struct _stat platform_stat_t;
149 #else
150 typedef struct stat platform_stat_t;
151 #endif
152 int platform_stat(const char *path, platform_stat_t *buf);
153 
158 const char *platform_create_temp_file(const char *directory, const char *prefix,
159  struct gc_arena *gc);
160 
162 const char *platform_gen_path(const char *directory, const char *filename,
163  struct gc_arena *gc);
164 
166 bool platform_absolute_pathname(const char *pathname);
167 
169 bool platform_test_file(const char *filename);
170 
171 #endif /* ifndef PLATFORM_H */
int platform_stat(const char *path, platform_stat_t *buf)
Definition: platform.c:389
unsigned int platform_getpid(void)
Definition: platform.c:182
void platform_mlockall(bool print_msg)
Definition: platform.c:193
bool platform_group_get(const char *groupname, struct platform_state_group *state)
Definition: platform.c:112
const char * platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc)
Put a directory and filename together.
Definition: platform.c:456
void platform_group_set(const struct platform_state_group *state)
Definition: platform.c:134
void platform_nice(int niceval)
Definition: platform.c:160
int platform_putenv(char *string)
int platform_access(const char *path, int mode)
Definition: platform.c:307
int platform_ret_code(int stat)
Return an exit code if valid and between 0 and 255, -1 otherwise.
Definition: platform.c:274
bool platform_system_ok(int stat)
interpret the status code returned by execve()
Definition: platform.c:263
void platform_user_set(const struct platform_state_user *state)
Definition: platform.c:95
list flags
static int platform_state_user_uid(const struct platform_state_user *s)
Definition: platform.h:93
bool platform_test_file(const char *filename)
Return true if filename can be opened for read.
Definition: platform.c:522
bool platform_unlink(const char *filename)
Definition: platform.c:350
static int platform_state_group_gid(const struct platform_state_group *s)
Definition: platform.h:105
int platform_open(const char *path, int flags, int mode)
Definition: platform.c:376
void platform_chroot(const char *path)
Definition: platform.c:49
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:403
bool platform_user_get(const char *username, struct platform_state_user *state)
Definition: platform.c:73
FILE * platform_fopen(const char *path, const char *mode)
Definition: platform.c:363
void platform_sleep_milliseconds(unsigned int n)
Definition: platform.c:323
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
bool platform_absolute_pathname(const char *pathname)
Return true if pathname is absolute.
Definition: platform.c:503
struct _stat platform_stat_t
Definition: platform.h:148
void platform_sleep_until_signal(void)
Definition: platform.c:339
int platform_chdir(const char *dir)
Definition: platform.c:242