Skip to content
Snippets Groups Projects
Commit 177ec8d8 authored by Ralf Haferkamp's avatar Ralf Haferkamp Committed by Quanah Gibson-Mount
Browse files

Add some simple checks for certificate directories/files

parent a25d8ccc
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,13 @@
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include <fstream>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <cstring>
#include "TlsOptions.h"
#include "LDAPException.h"
......@@ -54,6 +61,41 @@ TlsOptions::TlsOptions( LDAP* ld ): m_ld(ld) { }
void TlsOptions::setOption( tls_option opt, const std::string& value ) const {
checkOpt(opt, STRING);
switch(opt) {
case TlsOptions::CACERTFILE :
case TlsOptions::CERTFILE :
case TlsOptions::KEYFILE :
{
// check if the supplied file is actually readable
std::ifstream ifile(value.c_str());
if ( !ifile ) {
throw( LDAPException( LDAP_LOCAL_ERROR, "Unable to open the supplied file for reading" ) );
}
}
break;
case TlsOptions::CACERTDIR :
{
struct stat st;
std::ostringstream msg;
bool fail=false;
int err = stat(value.c_str(),&st);
if ( err ) {
msg << strerror(errno);
fail = true;
} else {
if ( !S_ISDIR(st.st_mode) ){
msg << "The supplied path is not a directory.";
fail = true;
}
}
if ( fail ) {
std::ostringstream errstr;
errstr << "Error while setting Certificate Directory (" << value << "): " << msg.str();
throw( LDAPException( LDAP_LOCAL_ERROR, errstr.str() ) );
}
}
break;
}
this->setOption( opt, value.empty() ? NULL : (void*) value.c_str() );
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment