35 #include <sys/types.h>
38 #if GCONFIG_HAVE_PAM_IN_INCLUDE
41 #if GCONFIG_HAVE_PAM_IN_PAM
42 #include <pam/pam_appl.h>
44 #include <security/pam_appl.h>
50 char * strdup_(
const char * p )
53 char * copy =
static_cast<char*
>( std::malloc(std::strlen(p)+1U) ) ;
55 std::strcpy( copy , p ) ;
68 typedef pam_handle_t * Handle ;
71 typedef struct pam_conv Conversation ;
73 typedef G::Pam::ItemArray ItemArray ;
74 enum { MAGIC = 3456 } ;
85 PamImp(
G::Pam & pam ,
const std::string & app ,
const std::string & user ,
bool silent ) ;
89 bool authenticate(
bool ) ;
90 void check(
const std::string & ,
int )
const ;
91 static bool success(
int ) ;
92 void setCredentials(
int ) ;
93 void checkAccount(
bool ) ;
96 std::string name()
const ;
99 static int converseCallback(
int n ,
const struct pam_message ** in ,
struct pam_response ** out ,
void * vp ) ;
100 static void delayCallback(
int ,
unsigned ,
void * ) ;
101 static std::string decodeStyle(
int pam_style ) ;
102 static void release(
struct pam_response * ,
size_t ) ;
107 G::PamImp::PamImp(
G::Pam & pam ,
const std::string & application ,
const std::string & user ,
bool silent ) :
113 G_DEBUG(
"G::PamImp::ctor: [" << application <<
"] [" << user <<
"]" ) ;
115 m_conv.conv = converseCallback ;
116 m_conv.appdata_ptr = this ;
117 m_rc = ::pam_start( application.c_str() , user.c_str() , &m_conv , &m_hpam ) ;
118 if( m_rc != PAM_SUCCESS )
120 throw Error(
"pam_start" , m_rc ) ;
124 #ifdef PAM_FAIL_DELAY
125 m_rc = ::pam_set_item( m_hpam , PAM_FAIL_DELAY , reinterpret_cast<const void*>(delayCallback) ) ;
126 if( m_rc != PAM_SUCCESS )
128 ::pam_end( m_hpam , m_rc ) ;
129 throw Error(
"pam_set_item" , m_rc , ::pam_strerror(hpam(),m_rc) ) ;
138 G_DEBUG(
"G::PamImp::dtor" ) ;
139 ::pam_end( m_hpam , m_rc ) ;
147 G::PamImp::Handle G::PamImp::hpam()
const
152 bool G::PamImp::silent()
const
157 std::string G::PamImp::decodeStyle(
int pam_style )
159 std::string defolt = std::string(
"#" ) +
G::Str::fromInt( pam_style ) ;
160 if( pam_style == PAM_PROMPT_ECHO_OFF )
return "password" ;
161 if( pam_style == PAM_PROMPT_ECHO_ON )
return "prompt" ;
162 if( pam_style == PAM_ERROR_MSG )
return "error" ;
163 if( pam_style == PAM_TEXT_INFO )
return "info" ;
167 bool G::PamImp::authenticate(
bool require_token )
170 if( silent() ) flags |=
static_cast<int>(PAM_SILENT) ;
171 if( require_token ) flags |=
static_cast<int>(PAM_DISALLOW_NULL_AUTHTOK) ;
172 m_rc = ::pam_authenticate( hpam() , flags ) ;
173 #ifdef PAM_INCOMPLETE
174 if( m_rc == PAM_INCOMPLETE )
178 check(
"pam_authenticate" , m_rc ) ;
182 std::string G::PamImp::name()
const
184 const void * vp = nullptr ;
185 m_rc = ::pam_get_item( hpam() , PAM_USER , &vp ) ;
186 check(
"pam_get_item" , m_rc ) ;
187 const char * cp =
reinterpret_cast<const char*
>(vp) ;
188 return std::string( cp ? cp :
"" ) ;
191 void G::PamImp::setCredentials(
int flag )
195 if( silent() ) flags |=
static_cast<int>(PAM_SILENT) ;
196 m_rc = ::pam_setcred( hpam() , flags ) ;
197 check(
"pam_setcred" , m_rc ) ;
200 void G::PamImp::checkAccount(
bool require_token )
203 if( silent() ) flags |=
static_cast<int>(PAM_SILENT) ;
204 if( require_token ) flags |=
static_cast<int>(PAM_DISALLOW_NULL_AUTHTOK) ;
205 m_rc = ::pam_acct_mgmt( hpam() , flags ) ;
206 check(
"pam_acct_mgmt" , m_rc ) ;
209 void G::PamImp::release(
struct pam_response * rsp ,
size_t n )
213 for(
size_t i = 0U ; i < n ; i++ )
215 if( rsp[i].resp !=
nullptr )
216 std::free( rsp[i].resp ) ;
222 int G::PamImp::converseCallback(
int n_in ,
const struct pam_message ** in ,
struct pam_response ** out ,
void * vp )
224 G_ASSERT( n_in > 0 ) ;
225 G_ASSERT( out !=
nullptr ) ;
226 size_t n = n_in < 0 ? size_t(0U) : static_cast<size_t>(n_in) ;
234 G_WARNING_ONCE(
"PamImp::converseCallback: received a complex pam converse() structure: proceed with caution" ) ;
238 struct pam_response * rsp = nullptr ;
241 G_DEBUG(
"G::Pam::converseCallback: called back from pam with " << n <<
" item(s)" ) ;
242 PamImp * This =
reinterpret_cast<PamImp*
>(vp) ;
243 G_ASSERT( This->m_magic == MAGIC ) ;
249 ItemArray array( n ) ;
250 for(
size_t i = 0U ; i < n ; i++ )
252 std::string & s1 =
const_cast<std::string&
>(array[i].in_type) ;
253 s1 = decodeStyle( in[i]->msg_style ) ;
255 std::string & s2 =
const_cast<std::string&
>(array[i].in) ;
256 s2 = std::string(in[i]->msg ? in[i]->msg :
"") ;
258 array[i].out_defined = false ;
263 This->m_pam.converse( array ) ;
264 G_ASSERT( array.size() == n ) ;
269 rsp =
reinterpret_cast<struct pam_response*
>( std::malloc(n*
sizeof(
struct pam_response)) ) ;
271 throw std::bad_alloc() ;
272 for(
size_t j = 0U ; j < n ; j++ )
273 rsp[j].resp =
nullptr ;
277 for(
size_t i = 0U ; i < n ; i++ )
279 rsp[i].resp_retcode = 0 ;
280 if( array[i].out_defined )
282 char * response = strdup_( array[i].out.c_str() ) ;
283 if( response ==
nullptr )
284 throw std::bad_alloc() ;
285 rsp[i].resp = response ;
290 G_DEBUG(
"G::Pam::converseCallback: returning to pam from callback" ) ;
295 G_ERROR(
"G::Pam::converseCallback: exception" ) ;
297 return PAM_CONV_ERR ;
301 void G::PamImp::delayCallback(
int status ,
unsigned delay_usec ,
void * pam_vp )
305 G_DEBUG(
"G::Pam::delayCallback: status=" << status <<
", delay=" << delay_usec ) ;
306 if( status != PAM_SUCCESS )
308 PamImp * This =
reinterpret_cast<PamImp*
>(pam_vp) ;
309 if( This !=
nullptr )
311 G_ASSERT( This->m_magic == MAGIC ) ;
312 This->m_pam.delay( delay_usec ) ;
318 G_ERROR(
"G::Pam::delayCallback: exception" ) ;
322 void G::PamImp::openSession()
325 if( silent() ) flags |=
static_cast<int>(PAM_SILENT) ;
326 m_rc = ::pam_open_session( hpam() , flags ) ;
327 check(
"pam_open_session" , m_rc ) ;
330 void G::PamImp::closeSession()
333 if( silent() ) flags |=
static_cast<int>(PAM_SILENT) ;
334 m_rc = ::pam_close_session( hpam() , flags ) ;
335 check(
"pam_close_session" , m_rc ) ;
338 bool G::PamImp::success(
int rc )
340 return rc == PAM_SUCCESS ;
343 void G::PamImp::check(
const std::string & op ,
int rc )
const
346 throw Error( op , rc , ::pam_strerror(hpam(),rc) ) ;
351 G::Pam::Pam(
const std::string & application ,
const std::string & user ,
bool silent ) :
352 m_imp( new
PamImp(*this,application,user,silent) )
363 G_DEBUG(
"G::Pam::authenticate" ) ;
364 return m_imp->authenticate( require_token ) ;
369 G_DEBUG(
"G::Pam::checkAccount" ) ;
370 return m_imp->checkAccount( require_token ) ;
375 G_DEBUG(
"G::Pam::establishCredentials" ) ;
376 m_imp->setCredentials( PAM_ESTABLISH_CRED ) ;
381 G_DEBUG(
"G::Pam::openSession" ) ;
382 m_imp->openSession() ;
387 G_DEBUG(
"G::Pam::closeSession" ) ;
388 m_imp->closeSession() ;
393 m_imp->setCredentials( PAM_DELETE_CRED ) ;
398 m_imp->setCredentials( PAM_REINITIALIZE_CRED ) ;
403 m_imp->setCredentials( PAM_REFRESH_CRED ) ;
411 typedef struct timeval Timeval ;
413 timeout.tv_sec = usec / 1000000U ;
414 timeout.tv_usec = usec % 1000000U ;
415 ::select( 0 ,
nullptr ,
nullptr ,
nullptr , &timeout ) ;
421 return m_imp->name() ;
void closeSession()
Closes a session.
std::string name() const
Returns the authenticated user name.
void refreshCredentials()
Refreshes credentials.
static std::string fromInt(int i)
Converts int 'i' to a string.
void checkAccount(bool require_token)
Does "account management", checking that the authenticated user is currently allowed to use the syste...
void reinitialiseCredentials()
Reinitialises credentials.
void openSession()
Starts a session.
void establishCredentials()
Embues the authenticated user with their credentials, such as "tickets" in the form of environment va...
bool authenticate(bool require_token)
Authenticates the user.
Pam(const std::string &app, const std::string &user, bool silent)
Constructor.
virtual void delay(unsigned int usec)=0
Called when the pam library wants the application to introduce a delay to prevent brute-force attacks...
A pimple-pattern implementation class for G::Pam.
virtual ~Pam()
Destructor.
void deleteCredentials()
Deletes credentials.
An exception class used by G::Pam.
A thin interface to the system PAM library, with two pure virtual methods that derived classes should...