<commit_before>/*
 * ServerPAMAuth.cpp
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
#include "ServerPAMAuth.hpp"

#include <sys/wait.h>

#include <core/Error.hpp>
#include <core/system/System.hpp>
#include <core/system/ProcessArgs.hpp>

#include <core/http/Request.hpp>
#include <core/http/Response.hpp>
#include <core/http/URL.hpp>
#include <core/http/AsyncUriHandler.hpp>
#include <core/text/TemplateFilter.hpp>

#include <server/util/system/Crypto.hpp>
#include <server/util/system/System.hpp>

#include <server/auth/ServerValidateUser.hpp>
#include <server/auth/ServerSecureUriHandler.hpp>
#include <server/auth/ServerAuthHandler.hpp>
#include <server/auth/ServerSecureCookie.hpp>

#include <server/ServerOptions.hpp>
#include <server/ServerUriHandlers.hpp>


namespace server {
namespace pam_auth {

namespace {


// TODO: make sure inputs into pam helper are bounded
// TODO: block tty case in pam helper

// TODO: make sure it works on redhat

// TODO: then can change hat permanently


// Handles error logging and EINTR retrying. Only for use with
// functions that return -1 on error and set errno.
int posixcall(boost::function<ssize_t()> func, const ErrorLocation& location)
{
   while (true)
   {
      int result;
      if ((result = func()) == -1)
      {
         if (errno == EINTR)
            continue;
         LOG_ERROR(systemError(errno, location));
         return result;
      }
      return result;
   }
}


bool pamLogin(const std::string& username, const std::string& password)
{
   // get path to pam helper
   FilePath pamHelperPath(server::options().authPamHelperPath());
   if (!pamHelperPath.exists())
   {
      LOG_ERROR_MESSAGE("PAM helper binary does not exist at " +
                        pamHelperPath.absolutePath());
      return false;
   }

   // create pipes
   const int READ = 0;
   const int WRITE = 1;
   int fdInput[2];
   if (posixcall(boost::bind(::pipe, fdInput), ERROR_LOCATION) == -1)
      return false;
   int fdOutput[2];
   if (posixcall(boost::bind(::pipe, fdOutput), ERROR_LOCATION) == -1)
      return false;

   // fork
   pid_t pid = posixcall(::fork, ERROR_LOCATION);
   if (pid == -1)
      return false;

   // child
   else if (pid == 0)
   {
      // close unused pipes<commit_msg>restore priv in pam helper child (redhat fix)<commit_after>/*
 * ServerPAMAuth.cpp
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
#include "ServerPAMAuth.hpp"

#include <sys/wait.h>

#include <core/Error.hpp>
#include <core/system/System.hpp>
#include <core/system/ProcessArgs.hpp>

#include <core/http/Request.hpp>
#include <core/http/Response.hpp>
#include <core/http/URL.hpp>
#include <core/http/AsyncUriHandler.hpp>
#include <core/text/TemplateFilter.hpp>

#include <server/util/system/Crypto.hpp>
#include <server/util/system/System.hpp>

#include <server/auth/ServerValidateUser.hpp>
#include <server/auth/ServerSecureUriHandler.hpp>
#include <server/auth/ServerAuthHandler.hpp>
#include <server/auth/ServerSecureCookie.hpp>

#include <server/ServerOptions.hpp>
#include <server/ServerUriHandlers.hpp>


namespace server {
namespace pam_auth {

namespace {


// TODO: don't re-assume priv on ubuntu

// TODO: comments on intentionally failing forward in child

// TODO: make sure inputs into pam helper are bounded
// TODO: block tty case in pam helper

// TODO: make sure it works on redhat

// TODO: then can change hat permanently


// Handles error logging and EINTR retrying. Only for use with
// functions that return -1 on error and set errno.
int posixcall(boost::function<ssize_t()> func, const ErrorLocation& location)
{
   while (true)
   {
      int result;
      if ((result = func()) == -1)
      {
         if (errno == EINTR)
            continue;
         LOG_ERROR(systemError(errno, location));
         return result;
      }
      return result;
   }
}


bool pamLogin(const std::string& username, const std::string& password)
{
   // get path to pam helper
   FilePath pamHelperPath(server::options().authPamHelperPath());
   if (!pamHelperPath.exists())
   {
      LOG_ERROR_MESSAGE("PAM helper binary does not exist at " +
                        pamHelperPath.absolutePath());
      return false;
   }

   // create pipes
   const int READ = 0;
   const int WRITE = 1;
   int fdInput[2];
   if (posixcall(boost::bind(::pipe, fdInput), ERROR_LOCATION) == -1)
      return false;
   int fdOutput[2];
   if (posixcall(boost::bind(::pipe, fdOutput), ERROR_LOCATION) == -1)
      return false;

   // fork
   pid_t pid = posixcall(::fork, ERROR_LOCATION);
   if (pid == -1)
      return false;

   // child
   else if (pid == 0)
   {
      // RedHat 5 returns PAM_SYSTEM_ERR from pam_authenticate if we're
      // running with geteuid != getuid (as is the case when we temporarily
      // drop privileges). So restore privilliges in the child
      if (util::system::realUserIsRoot())
      {
         Error error = util::system::restorePriv();
         if (error)
            LOG_ERROR(error);
      }

      // close unused pipes
{'fraction_non_alphanumeric': 0.11800486618004866, 'fraction_numerical': 0.006777893639207508, 'mean_word_length': 3.203798392987582, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2248193', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1770, 'n_words': 539}