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-2018 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 #include "basic.h"
52 #include "buffer.h"
53 
54 /* Get/Set UID of process */
55 
57 #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
58  const char *username;
59  struct passwd *pw;
60 #else
61  int dummy;
62 #endif
63 };
64 
65 /* Get/Set GID of process */
66 
68 #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
69  const char *groupname;
70  struct group *gr;
71 #else
72  int dummy;
73 #endif
74 };
75 
76 bool platform_user_get(const char *username, struct platform_state_user *state);
77 
78 void platform_user_set(const struct platform_state_user *state);
79 
80 bool platform_group_get(const char *groupname, struct platform_state_group *state);
81 
82 void platform_group_set(const struct platform_state_group *state);
83 
84 /*
85  * Extract UID or GID
86  */
87 
88 static inline int
90 {
91 #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
92  if (s->pw)
93  {
94  return s->pw->pw_uid;
95  }
96 #endif
97  return -1;
98 }
99 
100 static inline int
102 {
103 #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
104  if (s->gr)
105  {
106  return s->gr->gr_gid;
107  }
108 #endif
109  return -1;
110 }
111 
112 void platform_chroot(const char *path);
113 
114 void platform_nice(int niceval);
115 
116 unsigned int platform_getpid(void);
117 
118 void platform_mlockall(bool print_msg); /* Disable paging */
119 
120 int platform_chdir(const char *dir);
121 
122 /* interpret the status code returned by execve() */
123 bool platform_system_ok(int stat);
124 
125 int platform_access(const char *path, int mode);
126 
127 void platform_sleep_milliseconds(unsigned int n);
128 
129 void platform_sleep_until_signal(void);
130 
131 /* delete a file, return true if succeeded */
132 bool platform_unlink(const char *filename);
133 
134 int platform_putenv(char *string);
135 
136 FILE *platform_fopen(const char *path, const char *mode);
137 
138 int platform_open(const char *path, int flags, int mode);
139 
140 #ifdef _WIN32
141 typedef struct _stat platform_stat_t;
142 #else
143 typedef struct stat platform_stat_t;
144 #endif
145 int platform_stat(const char *path, platform_stat_t *buf);
146 
151 const char *platform_create_temp_file(const char *directory, const char *prefix,
152  struct gc_arena *gc);
153 
155 const char *platform_gen_path(const char *directory, const char *filename,
156  struct gc_arena *gc);
157 
159 bool platform_absolute_pathname(const char *pathname);
160 
162 bool platform_test_file(const char *filename);
163 
164 #endif /* ifndef PLATFORM_H */
int platform_stat(const char *path, platform_stat_t *buf)
Definition: platform.c:328
unsigned int platform_getpid(void)
Definition: platform.c:178
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:108
const char * platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc)
Put a directory and filename together.
Definition: platform.c:395
void platform_group_set(const struct platform_state_group *state)
Definition: platform.c:130
void platform_nice(int niceval)
Definition: platform.c:156
int platform_putenv(char *string)
int platform_access(const char *path, int mode)
Definition: platform.c:244
bool platform_system_ok(int stat)
Definition: platform.c:234
void platform_user_set(const struct platform_state_user *state)
Definition: platform.c:91
list flags
static int platform_state_user_uid(const struct platform_state_user *s)
Definition: platform.h:89
bool platform_test_file(const char *filename)
Return true if filename can be opened for read.
Definition: platform.c:461
bool platform_unlink(const char *filename)
Definition: platform.c:287
static int platform_state_group_gid(const struct platform_state_group *s)
Definition: platform.h:101
int platform_open(const char *path, int flags, int mode)
Definition: platform.c:315
void platform_chroot(const char *path)
Definition: platform.c:45
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:342
bool platform_user_get(const char *username, struct platform_state_user *state)
Definition: platform.c:69
FILE * platform_fopen(const char *path, const char *mode)
Definition: platform.c:302
void platform_sleep_milliseconds(unsigned int n)
Definition: platform.c:260
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:442
struct _stat platform_stat_t
Definition: platform.h:141
void platform_sleep_until_signal(void)
Definition: platform.c:276
int platform_chdir(const char *dir)
Definition: platform.c:213