]> git.sur5r.net Git - openldap/commitdiff
Add some simple checks for certificate directories/files
authorRalf Haferkamp <ralf@openldap.org>
Thu, 31 Jan 2013 13:19:39 +0000 (14:19 +0100)
committerRalf Haferkamp <ralf@openldap.org>
Thu, 31 Jan 2013 13:32:20 +0000 (14:32 +0100)
contrib/ldapc++/src/TlsOptions.cpp

index 769ba2f2adf61f393da7181fa752cb35aa74853a..44a2b607b26349edca201e13c1c527d031a562d4 100644 (file)
@@ -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() );
 }