26 #elif defined(_MSC_VER) 54 const char *top =
"/";
57 msg(
M_ERR,
"chroot to '%s' failed", path);
61 msg(
M_ERR,
"cd to '%s' failed", top);
63 msg(
M_INFO,
"chroot to '%s' and cd to '%s' succeeded", path, top);
65 msg(
M_FATAL,
"Sorry but I can't chroot to '%s' because this operating system doesn't appear to support the chroot() system call", path);
79 #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) 80 state->pw = getpwnam(username);
83 msg(
M_ERR,
"failed to find UID for user %s", username);
85 state->username = username;
88 msg(
M_FATAL,
"cannot get UID for user %s -- platform lacks getpwname() or setuid() system calls", username);
97 #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) 98 if (state->username && state->pw)
100 if (setuid(state->pw->pw_uid))
102 msg(
M_ERR,
"setuid('%s') failed", state->username);
104 msg(
M_INFO,
"UID set to %s", state->username);
118 #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID) 119 state->gr = getgrnam(groupname);
122 msg(
M_ERR,
"failed to find GID for group %s", groupname);
124 state->groupname = groupname;
127 msg(
M_FATAL,
"cannot get GID for group %s -- platform lacks getgrnam() or setgid() system calls", groupname);
136 #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID) 137 if (state->groupname && state->gr)
139 if (setgid(state->gr->gr_gid))
141 msg(
M_ERR,
"setgid('%s') failed", state->groupname);
143 msg(
M_INFO,
"GID set to %s", state->groupname);
144 #ifdef HAVE_SETGROUPS 147 gr_list[0] = state->gr->gr_gid;
148 if (setgroups(1, gr_list))
150 msg(
M_ERR,
"setgroups('%s') failed", state->groupname);
166 if (nice(niceval) < 0 && errno != 0)
172 msg(
M_INFO,
"nice %d succeeded", niceval);
175 msg(
M_WARN,
"WARNING: nice %d failed (function not implemented)", niceval);
185 return (
unsigned int) GetCurrentProcessId();
187 return (
unsigned int) getpid();
197 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_MEMLOCK) 198 #define MIN_LOCKED_MEM_MB 100 200 if (getrlimit(RLIMIT_MEMLOCK, &rl) < 0)
206 msg(
M_INFO,
"mlock: MEMLOCK limit: soft=%ld KB, hard=%ld KB",
207 ((
long int) rl.rlim_cur) / 1024, ((
long int) rl.rlim_max) / 1024);
208 if (rl.rlim_cur < MIN_LOCKED_MEM_MB*1024*1024)
210 msg(
M_INFO,
"mlock: RLIMIT_MEMLOCK < %d MB, increase limit",
212 rl.rlim_cur = MIN_LOCKED_MEM_MB*1024*1024;
213 if (rl.rlim_max < rl.rlim_cur)
215 rl.rlim_max = rl.rlim_cur;
217 if (setrlimit(RLIMIT_MEMLOCK, &rl) < 0)
225 if (mlockall(MCL_CURRENT | MCL_FUTURE))
234 msg(
M_WARN,
"WARNING: mlockall call failed (function not implemented)");
268 return stat != -1 && WIFEXITED(stat) && WEXITSTATUS(stat) == 0;
276 if (stat >= 0 && stat < 255)
289 if (!WIFEXITED(stat) || stat == -1)
294 int status = WEXITSTATUS(stat);
295 if (status >= 0 && status < 255)
315 return access(path, mode);
329 tv.tv_sec = n / 1000;
330 tv.tv_usec = (n % 1000) * 1000;
331 select(0, NULL, NULL, NULL, &tv);
344 select(0, NULL, NULL, NULL, NULL);
354 BOOL ret = DeleteFileW(
wide_string(filename, &gc));
358 return (unlink(filename) == 0);
371 return fopen(path, mode);
380 int fd = _wopen(
wide_string(path, &gc), flags, mode);
384 return open(path, flags, mode);
397 return stat(path, buf);
406 const char *retfname = NULL;
407 unsigned int attempts = 0;
408 char fname[256] = { 0 };
409 const char *fname_fmt =
PACKAGE "_%.*s_%08lx%08lx.tmp";
410 const int max_prefix_len =
sizeof(fname) - (
sizeof(
PACKAGE) + 7 + (2 * 8));
420 msg(
M_WARN,
"ERROR: temporary filename too long");
427 msg(
M_WARN,
"Failed to create temporary filename and path");
439 else if (fd == -1 && errno != EEXIST)
448 msg(
M_WARN,
"Failed to create temporary file after %i attempts", attempts);
463 const int CC_PATH_RESERVED =
CC_SLASH;
474 && strcmp(safe_filename,
".")
475 && strcmp(safe_filename,
"..")
481 const size_t outsize = strlen(safe_filename) + (directory ? strlen(directory) : 0) + 16;
507 const int c = pathname[0];
509 return c ==
'\\' || (isalpha(c) && pathname[1] ==
':' && pathname[2] ==
'\\');
543 filename ? filename :
"UNDEF",
static void gc_free(struct gc_arena *a)
bool buf_printf(struct buffer *buf, const char *format,...)
WCHAR * wide_string(const char *utf8, struct gc_arena *gc)
bool openvpn_snprintf(char *str, size_t size, const char *format,...)
static struct gc_arena gc_new(void)
bool win_safe_filename(const char *fn)
const char * string_mod_const(const char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace, struct gc_arena *gc)
Wrapper structure for dynamically allocated memory.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Garbage collection arena used to keep track of dynamically allocated memory.
long int get_random(void)
static SERVICE_STATUS status