59 const char *top =
"/";
62 msg(
M_ERR,
"chroot to '%s' failed", path);
66 msg(
M_ERR,
"cd to '%s' failed", top);
68 msg(
M_INFO,
"chroot to '%s' and cd to '%s' succeeded", path, top);
71 "Sorry but I can't chroot to '%s' because this operating system doesn't appear to support the chroot() system call",
79#if defined(__GNUC__) || defined(__clang__)
80#pragma GCC diagnostic push
81#pragma GCC diagnostic ignored "-Wsign-conversion"
90#if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
91 const struct passwd *pw = getpwnam(username);
94 msg(
M_ERR,
"failed to find UID for user %s", username);
98 state->uid = pw->pw_uid;
101 state->username = username;
104 "cannot get UID for user %s -- platform lacks getpwname() or setuid() system calls",
114#if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
117 if (setuid(state->uid))
119 msg(
M_ERR,
"setuid('%s') failed", state->username);
121 msg(
M_INFO,
"UID set to %s", state->username);
134#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
135 const struct group *gr = getgrnam(groupname);
138 msg(
M_ERR,
"failed to find GID for group %s", groupname);
142 state->gid = gr->gr_gid;
145 state->groupname = groupname;
148 "cannot get GID for group %s -- platform lacks getgrnam() or setgid() system calls",
158#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
161 if (setgid(state->gid))
163 msg(
M_ERR,
"setgid('%s') failed", state->groupname);
165 msg(
M_INFO,
"GID set to %s", state->groupname);
169 gr_list[0] = state->gid;
170 if (setgroups(1, gr_list))
172 msg(
M_ERR,
"setgroups('%s') failed", state->groupname);
229 int new_gid = -1, new_uid = -1;
241 if (group_state->groupname && group_state->
group_valid)
243 new_gid = (int)group_state->gid;
245 if (user_state->username && user_state->
user_valid)
247 new_uid = (int)user_state->uid;
251 capng_clear(CAPNG_SELECT_BOTH);
252 res = capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED, CAP_NET_ADMIN);
255 msg(err_flags,
"capng_update(CAP_NET_ADMIN) failed: %d", res);
262 res = capng_change_id(new_uid, new_gid, CAPNG_DROP_SUPP_GRP);
263 if (res == -4 || res == -6)
267 msg(
M_ERR,
"capng_change_id('%s','%s') failed: %d", user_state->username,
268 group_state->groupname, res);
273 msg(err_flags,
"NOTE: previous error likely due to missing capability CAP_SETPCAP.");
278 msg(err_flags |
M_ERRNO,
"capng_change_id('%s','%s') failed retaining capabilities: %d",
279 user_state->username, group_state->groupname, res);
285 msg(
M_INFO,
"UID set to %s", user_state->username);
289 msg(
M_INFO,
"GID set to %s", group_state->groupname);
292 msg(
M_INFO,
"Capabilities retained: CAP_NET_ADMIN");
299 if (prctl(PR_GET_KEEPCAPS) && prctl(PR_SET_KEEPCAPS, 0) < 0)
301 msg(
M_ERR,
"Clearing KEEPCAPS flag failed");
307 msg(err_flags,
"Unable to retain capabilities");
322 if (nice(niceval) < 0 && errno != 0)
328 msg(
M_INFO,
"nice %d succeeded", niceval);
331 msg(
M_WARN,
"WARNING: nice %d failed (function not implemented)", niceval);
341 return (
unsigned int)GetCurrentProcessId();
343 return (
unsigned int)getpid();
353#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_MEMLOCK)
354#define MIN_LOCKED_MEM_MB 100
356 if (getrlimit(RLIMIT_MEMLOCK, &rl) < 0)
362 msg(
M_INFO,
"mlock: MEMLOCK limit: soft=%ld KB, hard=%ld KB",
363 ((
long int)rl.rlim_cur) / 1024, ((
long int)rl.rlim_max) / 1024);
364 if (rl.rlim_cur < MIN_LOCKED_MEM_MB * 1024 * 1024)
366 msg(
M_INFO,
"mlock: RLIMIT_MEMLOCK < %d MB, increase limit", MIN_LOCKED_MEM_MB);
367 rl.rlim_cur = MIN_LOCKED_MEM_MB * 1024 * 1024;
368 if (rl.rlim_max < rl.rlim_cur)
370 rl.rlim_max = rl.rlim_cur;
372 if (setrlimit(RLIMIT_MEMLOCK, &rl) < 0)
380 if (mlockall(MCL_CURRENT | MCL_FUTURE))
389 msg(
M_WARN,
"WARNING: mlockall call failed (function not implemented)");
423 return stat != -1 && WIFEXITED(stat) && WEXITSTATUS(stat) == 0;
431 if (stat >= 0 && stat < 255)
444 if (!WIFEXITED(stat) || stat == -1)
449 int status = WEXITSTATUS(stat);
470 return access(path, mode);
484 tv.tv_sec = n / 1000;
485 tv.tv_usec = (n % 1000) * 1000;
486 select(0, NULL, NULL, NULL, &tv);
500 return (unlink(filename) == 0);
513 return fopen(path, mode);
526 return open(path, flags, mode);
539 return stat(path, buf);
547 unsigned int attempts = 0;
548 char fname[256] = { 0 };
549 const char *fname_fmt = PACKAGE
"_%.*s_%08" PRIx64
"%08" PRIx64
".tmp";
550 const int max_prefix_len =
sizeof(fname) - (
sizeof(PACKAGE) + 7 + (2 * 8));
556 if (!
checked_snprintf(fname,
sizeof(fname), fname_fmt, max_prefix_len, prefix,
559 msg(
M_WARN,
"ERROR: temporary filename too long");
566 msg(
M_WARN,
"Failed to create temporary filename and path");
572 const int fd =
platform_open(retfname, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
578 else if (errno != EEXIST)
586 msg(
M_WARN,
"Failed to create temporary file after %u attempts", attempts);
602 msg(
M_USAGE,
"Could not find a suitable temporary directory."
603 " (GetTempPath() failed). Consider using --tmp-dir");
607 ret = getenv(
"TMPDIR");
629 const int CC_PATH_RESERVED =
CC_SLASH;
639 if (safe_filename && strcmp(safe_filename,
".") && strcmp(safe_filename,
"..")
645 const size_t outsize = strlen(safe_filename) + (directory ? strlen(directory) : 0) + 16;
666#if defined(__GNUC__) || defined(__clang__)
667#pragma GCC diagnostic pop
675 const int c = pathname[0];
677 return c ==
'\\' || (isalpha(c) && pathname[1] ==
':' && pathname[2] ==
'\\');
710 dmsg(
D_TEST_FILE,
"TEST FILE '%s' [%d]", filename ? filename :
"UNDEF", ret);
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
const char * string_mod_const(const char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace, struct gc_arena *gc)
Returns a copy of a string with certain classes of characters of it replaced with a specified charact...
bool checked_snprintf(char *str, size_t size, const char *format,...)
Like snprintf() but returns an boolean.
Buffer management functions and garbage collection.
#define CC_DOUBLE_QUOTE
double quote
#define BSTR(buf)
Return the buffer content pointer cast to char *.
#define CC_ASTERISK
asterisk
#define CC_BACKSLASH
backslash
#define CC_LESS_THAN
less than sign
#define CC_GREATER_THAN
greater than sign
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
#define CC_PRINT
printable (>= 32, != 127)
#define CC_QUESTION_MARK
question mark
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
int64_t get_random(void)
an analogue to the random() function, but use prng_bytes and also int64_t instead of long to avoid LL...
Data Channel Cryptography Module.
static SERVICE_STATUS status
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Wrapper structure for dynamically allocated memory.
Contains all state information for one tunnel.
struct options options
Options loaded from command line or configuration file.
Garbage collection arena used to keep track of dynamically allocated memory.
bool win_safe_filename(const char *fn)
const char * win_get_tempdir(void)
WCHAR * wide_string(const char *utf8, struct gc_arena *gc)