📄 pambasicauthenticatorunix.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================//// Author: Nag Boranna, Hewlett-Packard Company(nagaraja_boranna@hp.com)//// Modified By: Yi Zhou, Hewlett-Packard Company(yi_zhou@hp.com)// : Sushma Fernandes, Hewlett-Packard Company // (sushma_fernandes@hp.com)// Josephine Eskaline Joyce (jojustin@in.ibm.com) for PEP#101////%/////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/System.h>#include <Pegasus/Common/Tracer.h>#include <Pegasus/Config/ConfigManager.h>#include <Pegasus/Common/FileSystem.h>#include <Pegasus/Common/Signal.h>#if defined (PEGASUS_OS_HPUX)#include <prot.h>#endif#if defined (PEGASUS_USE_PAM_STANDALONE_PROC)#include <Pegasus/Common/Logger.h>#include <pwd.h>#include <sys/stat.h>#include <unistd.h>#include <sys/types.h>#include <sys/resource.h>#if defined(PEGASUS_HAS_SIGNALS)# include <sys/wait.h>#endif#endif#include "PAMBasicAuthenticator.h"PEGASUS_USING_STD;PEGASUS_NAMESPACE_BEGIN#include <security/pam_appl.h>#if defined(PEGASUS_OS_LSB)#ifndef PAM_MAX_MSG_SIZE#define PAM_MAX_MSG_SIZE 512#endif#endif#define BUFFERLEN 1024/** Constant representing the Basic authentication challenge header.*/static const String BASIC_CHALLENGE_HEADER = "WWW-Authenticate: Basic \"";Mutex PAMBasicAuthenticator::_authSerializeMutex;/** Service name for pam_start */const char *service = "wbem";typedef struct{ CString userPassword;} APP_DATA;/* constructor. */PAMBasicAuthenticator::PAMBasicAuthenticator() { PEG_METHOD_ENTER(TRC_AUTHENTICATION, "PAMBasicAuthenticator::PAMBasicAuthenticator()"); // // get the local system name // _realm.assign(System::getHostName()); // // get the configured port number // ConfigManager* configManager = ConfigManager::getInstance(); String port = configManager->getCurrentValue("httpPort"); // // Create realm that will be used for Basic challenges // _realm.append(":"); _realm.append(port); PEG_METHOD_EXIT();}/* destructor. */PAMBasicAuthenticator::~PAMBasicAuthenticator() { PEG_METHOD_ENTER(TRC_AUTHENTICATION, "PAMBasicAuthenticator::~PAMBasicAuthenticator()"); PEG_METHOD_EXIT();}Boolean PAMBasicAuthenticator::authenticate( const String& userName, const String& password) { PEG_METHOD_ENTER(TRC_AUTHENTICATION, "PAMBasicAuthenticator::authenticate()"); Boolean authenticated;#if !defined(PEGASUS_USE_PAM_STANDALONE_PROC) authenticated = _authenticateByPAM(userName, password);#else // // Mutex to Serialize Authentication calls. // Tracer::trace(TRC_AUTHENTICATION, Tracer::LEVEL4, "Authentication Mutex lock."); AutoMutex lock(_authSerializeMutex); authenticated = _pamBasicAuthenticatorStandAlone.authenticate( userName, password);#endif PEG_METHOD_EXIT(); return (authenticated);}Boolean PAMBasicAuthenticator::_authenticateByPAM( const String& userName, const String& password) { PEG_METHOD_ENTER(TRC_AUTHENTICATION, "PAMBasicAuthenticator::_authenticateByPAM()"); Boolean authenticated = false; struct pam_conv pconv; pam_handle_t *phandle; char *name; APP_DATA mydata; // // Store the password for PAM authentication // mydata.userPassword = password.getCString(); pconv.conv = PAMBasicAuthenticator::PAMCallback; pconv.appdata_ptr = &mydata;// WARNING: Should only be uncommented for debugging in a secure environment.// Tracer::trace(TRC_AUTHENTICATION, Tracer::LEVEL4,// "PAMBasicAuthenticator::_authenticateByPAM() - userName = %s; userPassword = %s",// (const char *)userName.getCString(), (const char *)password.getCString()); // //Call pam_start since you need to before making any other PAM calls // if ( ( pam_start(service, (const char *)userName.getCString(), &pconv, &phandle) ) != PAM_SUCCESS ) { PEG_METHOD_EXIT(); return (authenticated); } // //Call pam_authenticate to authenticate the user // if ( ( pam_authenticate(phandle, 0) ) == PAM_SUCCESS ) { Tracer::trace(TRC_AUTHENTICATION, Tracer::LEVEL4, "pam_authenticate successful."); // //Call pam_acct_mgmt, to check if the user account is valid. This includes //checking for password and account expiration, as well as verifying access //hour restrictions. // if ( ( pam_acct_mgmt(phandle, 0) ) == PAM_SUCCESS ) { Tracer::trace(TRC_AUTHENTICATION, Tracer::LEVEL4, "pam_acct_mgmt successful."); authenticated = true; } } // //Call pam_end to end our PAM work // pam_end(phandle, 0); PEG_METHOD_EXIT(); return (authenticated);}Boolean PAMBasicAuthenticator::validateUser(const String& userName){ PEG_METHOD_ENTER(TRC_AUTHENTICATION, "PAMBasicAuthenticator::validateUser()"); Boolean authenticated = false;#if !defined(PEGASUS_USE_PAM_STANDALONE_PROC) struct pam_conv pconv; pam_handle_t *phandle; char *name; APP_DATA mydata; const char *service = "wbem"; pconv.conv = PAMBasicAuthenticator::pamValidateUserCallback; pconv.appdata_ptr = &mydata; // // Call pam_start since you need to before making any other PAM calls // if ( pam_start(service, (const char *)userName.getCString(), &pconv, &phandle) != PAM_SUCCESS) { PEG_METHOD_EXIT(); return (authenticated); } // // Call pam_acct_mgmt, to check if the user account is valid. This includes // checking for account expiration, as well as verifying access // hour restrictions. // if ( pam_acct_mgmt(phandle, 0) == PAM_SUCCESS ) { authenticated = true; } // //Call pam_end to end our PAM work // pam_end(phandle, 0);#else // // Mutex to Serialize Authentication calls. // Tracer::trace(TRC_AUTHENTICATION, Tracer::LEVEL4, "Authentication Mutex lock."); AutoMutex lock(_authSerializeMutex); authenticated = _pamBasicAuthenticatorStandAlone.validateUser( userName);#endif PEG_METHOD_EXIT(); return (authenticated);}Sint32 PAMBasicAuthenticator::pamValidateUserCallback( Sint32 num_msg,#if defined (PEGASUS_OS_LINUX) const struct pam_message **msg,#else struct pam_message **msg,#endif struct pam_response **resp, void *appdata_ptr){ PEG_METHOD_ENTER(TRC_AUTHENTICATION, "PAMBasicAuthenticator::pamValidateUserCallback()"); // // Allocate the response buffers // if ( num_msg > 0 ) { // // Since resp->resp needs to be initialized in all possible scenarios, // use calloc for memory allocation. // *resp = (struct pam_response *)calloc(num_msg, sizeof(struct pam_response)); if ( *resp == NULL ) { PEG_METHOD_EXIT(); return PAM_BUF_ERR; } } else { PEG_METHOD_EXIT(); return PAM_CONV_ERR; } PEG_METHOD_EXIT(); return PAM_SUCCESS;}//// Create authentication response header//String PAMBasicAuthenticator::getAuthResponseHeader(){ PEG_METHOD_ENTER(TRC_AUTHENTICATION, "PAMBasicAuthenticator::getAuthResponseHeader()"); // // build response header using realm // String responseHeader = BASIC_CHALLENGE_HEADER; responseHeader.append(_realm); responseHeader.append("\""); PEG_METHOD_EXIT(); return (responseHeader);}#if defined PEGASUS_OS_LINUXSint32 PAMBasicAuthenticator::PAMCallback(Sint32 num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)#elseSint32 PAMBasicAuthenticator::PAMCallback(Sint32 num_msg, struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)#endif{ PEG_METHOD_ENTER(TRC_AUTHENTICATION, "PAMBasicAuthenticator::PAMCallback()"); // // Copy the application specific data from the PAM structure. // APP_DATA *mydata; mydata = (APP_DATA *) appdata_ptr; // // Allocate the response buffers // if ( num_msg > 0 ) { // // Since resp->resp needs to be initialized in all possible scenarios, // use calloc for memory allocation. // *resp = (struct pam_response *)calloc(num_msg, sizeof(struct pam_response)); if ( *resp == NULL ) { PEG_METHOD_EXIT(); return PAM_BUF_ERR; } } else { PEG_METHOD_EXIT(); return PAM_CONV_ERR; } for ( Sint32 i = 0; i < num_msg; i++ ) { switch ( msg[i]->msg_style ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -