2015-01-15 07:50:40 +01:00
|
|
|
// OpenVPN -- An application to securely tunnel IP networks
|
|
|
|
// over a single port, with support for SSL/TLS-based
|
|
|
|
// session authentication and key exchange,
|
|
|
|
// packet encryption, packet authentication, and
|
|
|
|
// packet compression.
|
|
|
|
//
|
2022-09-29 11:41:13 +02:00
|
|
|
// Copyright (C) 2012-2022 OpenVPN Inc.
|
2015-01-15 07:50:40 +01:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-12-21 21:42:20 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
2015-01-15 07:50:40 +01:00
|
|
|
// as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-12-21 21:42:20 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2015-01-15 07:50:40 +01:00
|
|
|
//
|
2017-12-21 21:42:20 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2015-01-15 07:50:40 +01:00
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#ifndef OPENVPN_COMMON_USERGROUP_H
|
|
|
|
#define OPENVPN_COMMON_USERGROUP_H
|
|
|
|
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
2016-11-28 20:56:17 +01:00
|
|
|
#include <errno.h>
|
2015-01-15 07:50:40 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2015-07-17 03:51:20 +02:00
|
|
|
#include <openvpn/common/platform.hpp>
|
|
|
|
|
|
|
|
#ifdef OPENVPN_PLATFORM_LINUX
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
|
2015-05-17 21:27:34 +02:00
|
|
|
#include <openvpn/common/size.hpp>
|
2015-01-15 07:50:40 +01:00
|
|
|
#include <openvpn/common/exception.hpp>
|
2017-03-18 22:46:45 +01:00
|
|
|
#include <openvpn/common/to_string.hpp>
|
2017-04-23 01:54:01 +02:00
|
|
|
#include <openvpn/common/strerror.hpp>
|
2015-01-15 07:50:40 +01:00
|
|
|
|
|
|
|
namespace openvpn {
|
2016-07-13 07:02:37 +02:00
|
|
|
// NOTE: -- SetUserGroup object does not own passwd and group
|
|
|
|
// objects, therefore *pw and *gr can change under us.
|
2015-01-15 07:50:40 +01:00
|
|
|
class SetUserGroup
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OPENVPN_EXCEPTION(user_group_err);
|
|
|
|
|
2016-11-28 20:56:17 +01:00
|
|
|
SetUserGroup(const std::string &user, const std::string &group, const bool strict)
|
2015-05-17 10:53:37 +02:00
|
|
|
: SetUserGroup(user.empty() ? nullptr : user.c_str(),
|
2016-11-28 20:56:17 +01:00
|
|
|
group.empty() ? nullptr : group.c_str(),
|
|
|
|
strict)
|
2015-04-30 18:34:07 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-28 20:56:17 +01:00
|
|
|
SetUserGroup(const char *user, const char *group, const bool strict)
|
2015-05-17 10:53:37 +02:00
|
|
|
: pw(nullptr),
|
|
|
|
gr(nullptr)
|
2015-01-15 07:50:40 +01:00
|
|
|
{
|
|
|
|
if (user)
|
|
|
|
{
|
2016-07-13 07:02:37 +02:00
|
|
|
pw = ::getpwnam(user);
|
2016-11-28 20:56:17 +01:00
|
|
|
if (!pw && strict)
|
2015-01-15 07:50:40 +01:00
|
|
|
OPENVPN_THROW(user_group_err, "user lookup failed for '" << user << '\'');
|
|
|
|
user_name = user;
|
|
|
|
}
|
|
|
|
if (group)
|
|
|
|
{
|
2016-07-13 07:02:37 +02:00
|
|
|
gr = ::getgrnam(group);
|
2016-11-28 20:56:17 +01:00
|
|
|
if (!gr && strict)
|
2015-01-15 07:50:40 +01:00
|
|
|
OPENVPN_THROW(user_group_err, "group lookup failed for '" << group << '\'');
|
|
|
|
group_name = group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:04:18 +01:00
|
|
|
virtual ~SetUserGroup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-26 00:36:41 +02:00
|
|
|
const std::string &user() const
|
|
|
|
{
|
|
|
|
return user_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &group() const
|
|
|
|
{
|
|
|
|
return group_name;
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:04:18 +01:00
|
|
|
virtual void pre_thread() const
|
2015-01-15 07:50:40 +01:00
|
|
|
{
|
2021-11-15 17:04:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void post_thread() const
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void activate() const
|
|
|
|
{
|
|
|
|
do_setgid_setgroups();
|
|
|
|
do_setuid();
|
|
|
|
retain_core_dumps();
|
2015-01-15 07:50:40 +01:00
|
|
|
}
|
|
|
|
|
2016-11-28 20:56:17 +01:00
|
|
|
void chown(const std::string &fn) const
|
|
|
|
{
|
|
|
|
if (pw && gr)
|
|
|
|
{
|
|
|
|
const int status = ::chown(fn.c_str(), uid(), gid());
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
const int eno = errno;
|
2017-04-23 01:54:01 +02:00
|
|
|
OPENVPN_THROW(user_group_err, "chown " << user_name << '.' << group_name << ' ' << fn << " : " << strerror_str(eno));
|
2016-11-28 20:56:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void chown(const int fd, const std::string &title) const
|
|
|
|
{
|
|
|
|
if (pw && gr)
|
|
|
|
{
|
|
|
|
const int status = ::fchown(fd, uid(), gid());
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
const int eno = errno;
|
2017-04-23 01:54:01 +02:00
|
|
|
OPENVPN_THROW(user_group_err, "chown " << user_name << '.' << group_name << ' ' << title << " : " << strerror_str(eno));
|
2016-11-28 20:56:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-04 23:58:47 +02:00
|
|
|
void invalidate()
|
|
|
|
{
|
|
|
|
pw = nullptr;
|
|
|
|
gr = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-28 20:56:17 +01:00
|
|
|
uid_t uid() const
|
2016-07-13 07:02:37 +02:00
|
|
|
{
|
|
|
|
if (pw)
|
|
|
|
return pw->pw_uid;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-28 20:56:17 +01:00
|
|
|
gid_t gid() const
|
|
|
|
{
|
|
|
|
if (gr)
|
|
|
|
return gr->gr_gid;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool uid_defined() const
|
|
|
|
{
|
|
|
|
return bool(pw);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool gid_defined() const
|
|
|
|
{
|
|
|
|
return bool(gr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool defined() const
|
|
|
|
{
|
|
|
|
return uid_defined() && gid_defined();
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:04:18 +01:00
|
|
|
protected:
|
|
|
|
void do_setgid_setgroups() const
|
|
|
|
{
|
|
|
|
if (gr)
|
|
|
|
{
|
|
|
|
if (::setgid(gr->gr_gid))
|
|
|
|
{
|
|
|
|
const int eno = errno;
|
|
|
|
OPENVPN_THROW(user_group_err, "setgid failed for group '" << group_name << "': " << strerror_str(eno));
|
|
|
|
}
|
|
|
|
gid_t gr_list[1];
|
|
|
|
gr_list[0] = gr->gr_gid;
|
|
|
|
if (::setgroups(1, gr_list))
|
|
|
|
{
|
|
|
|
const int eno = errno;
|
|
|
|
OPENVPN_THROW(user_group_err, "setgroups failed for group '" << group_name << "': " << strerror_str(eno));
|
|
|
|
}
|
|
|
|
OPENVPN_LOG("GID set to '" << group_name << '\'');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_setuid() const
|
|
|
|
{
|
|
|
|
if (pw)
|
|
|
|
{
|
|
|
|
if (::setuid(pw->pw_uid))
|
|
|
|
{
|
|
|
|
const int eno = errno;
|
|
|
|
OPENVPN_THROW(user_group_err, "setuid failed for user '" << user_name << "': " << strerror_str(eno));
|
|
|
|
}
|
|
|
|
OPENVPN_LOG("UID set to '" << user_name << '\'');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void retain_core_dumps() const
|
|
|
|
{
|
|
|
|
#ifdef OPENVPN_PLATFORM_LINUX
|
|
|
|
// retain core dumpability after setgid/setuid
|
|
|
|
if (gr || pw)
|
|
|
|
{
|
|
|
|
if (::prctl(PR_SET_DUMPABLE, 1))
|
|
|
|
{
|
|
|
|
const int eno = errno;
|
|
|
|
OPENVPN_THROW(user_group_err, "SetUserGroup prctl PR_SET_DUMPABLE fail: " << strerror_str(eno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-15 07:50:40 +01:00
|
|
|
std::string user_name;
|
|
|
|
std::string group_name;
|
|
|
|
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
|
|
|
};
|
|
|
|
} // namespace openvpn
|
|
|
|
|
|
|
|
#endif
|