1240116Smarcel// 2240116Smarcel// Automated Testing Framework (atf) 3240116Smarcel// 4240116Smarcel// Copyright (c) 2007 The NetBSD Foundation, Inc. 5240116Smarcel// All rights reserved. 6240116Smarcel// 7240116Smarcel// Redistribution and use in source and binary forms, with or without 8240116Smarcel// modification, are permitted provided that the following conditions 9240116Smarcel// are met: 10240116Smarcel// 1. Redistributions of source code must retain the above copyright 11240116Smarcel// notice, this list of conditions and the following disclaimer. 12240116Smarcel// 2. Redistributions in binary form must reproduce the above copyright 13240116Smarcel// notice, this list of conditions and the following disclaimer in the 14240116Smarcel// documentation and/or other materials provided with the distribution. 15240116Smarcel// 16240116Smarcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17240116Smarcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18240116Smarcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19240116Smarcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20240116Smarcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21240116Smarcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22240116Smarcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23240116Smarcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24240116Smarcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25240116Smarcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26240116Smarcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27240116Smarcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28240116Smarcel// 29240116Smarcel 30240116Smarcelextern "C" { 31240116Smarcel#include <sys/types.h> 32240116Smarcel 33240116Smarcel#include <pwd.h> 34240116Smarcel#include <unistd.h> 35240116Smarcel 36240116Smarcel#include "../atf-c/detail/user.h" 37240116Smarcel} 38240116Smarcel 39240116Smarcel#include <stdexcept> 40240116Smarcel#include <string> 41240116Smarcel 42240116Smarcel#include "../atf-c++/detail/sanity.hpp" 43240116Smarcel 44240116Smarcel#include "user.hpp" 45240116Smarcel 46240116Smarcelnamespace impl = atf::atf_run; 47240116Smarcel#define IMPL_NAME "atf::atf_run" 48240116Smarcel 49240116Smarceluid_t 50240116Smarcelimpl::euid(void) 51240116Smarcel{ 52240116Smarcel return atf_user_euid(); 53240116Smarcel} 54240116Smarcel 55240116Smarcelvoid 56240116Smarcelimpl::drop_privileges(const std::pair< int, int > ids) 57240116Smarcel{ 58240116Smarcel if (::setgid(ids.second) == -1) 59240116Smarcel throw std::runtime_error("Failed to drop group privileges"); 60240116Smarcel if (::setuid(ids.first) == -1) 61240116Smarcel throw std::runtime_error("Failed to drop user privileges"); 62240116Smarcel} 63240116Smarcel 64240116Smarcelstd::pair< int, int > 65240116Smarcelimpl::get_user_ids(const std::string& user) 66240116Smarcel{ 67240116Smarcel const struct passwd* pw = ::getpwnam(user.c_str()); 68240116Smarcel if (pw == NULL) 69240116Smarcel throw std::runtime_error("Failed to get information for user " + user); 70240116Smarcel return std::make_pair(pw->pw_uid, pw->pw_gid); 71240116Smarcel} 72240116Smarcel 73240116Smarcelbool 74240116Smarcelimpl::is_member_of_group(gid_t gid) 75240116Smarcel{ 76240116Smarcel return atf_user_is_member_of_group(gid); 77240116Smarcel} 78240116Smarcel 79240116Smarcelbool 80240116Smarcelimpl::is_root(void) 81240116Smarcel{ 82240116Smarcel return atf_user_is_root(); 83240116Smarcel} 84240116Smarcel 85240116Smarcelbool 86240116Smarcelimpl::is_unprivileged(void) 87240116Smarcel{ 88240116Smarcel return atf_user_is_unprivileged(); 89240116Smarcel} 90