| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2013 |
| 4 |
* Author: Adrien Devresse <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <davix_internal.hpp> |
| 23 |
#include "neonsession.hpp" |
| 24 |
#include <ne_redirect.h> |
| 25 |
#include <libs/time_utils.h> |
| 26 |
#include <auth/davixx509cred_internal.hpp> |
| 27 |
#include <neon/neonsessionfactory.hpp> |
| 28 |
#include <utils/stringutils.hpp> |
| 29 |
|
| 30 |
const char* davix_neon_key="davix_key"; |
| 31 |
|
| 32 |
using namespace StrUtil; |
| 33 |
|
| 34 |
namespace Davix{ |
| 35 |
|
| 36 |
|
| 37 |
static std::pair<std::string, std::string> userInfo_to_auth(const std::string & userInfo){ |
| 38 |
std::pair<std::string , std::string > id; |
| 39 |
std::string::const_iterator it = std::find(userInfo.begin(), userInfo.end(), ':'); |
| 40 |
id.first = std::string(userInfo.begin(), it); |
| 41 |
it = ((it != userInfo.end())?(it+1):(it)); |
| 42 |
id.second = std::string(it, userInfo.end()); |
| 43 |
return id; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
static int validate_all_certificate(void *userdata, int failures, |
| 48 |
const ne_ssl_certificate *cert){ |
| 49 |
(void) userdata; |
| 50 |
(void) failures; |
| 51 |
(void) cert; |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
const int n_max_auth = 20; |
| 56 |
|
| 57 |
|
| 58 |
ne_session* NEONSession::get_ne_sess() { |
| 59 |
return _sess->session; |
| 60 |
} |
| 61 |
|
| 62 |
void NEONSession::authNeonCliCertMapper(void *userdata, ne_session *sess, |
| 63 |
const ne_ssl_dname *const *dnames, |
| 64 |
int dncount){ |
| 65 |
(void) dncount; |
| 66 |
(void) sess; |
| 67 |
(void) dnames; |
| 68 |
NEONSession* req = static_cast<NEONSession*>(userdata); |
| 69 |
|
| 70 |
X509Credential cert; |
| 71 |
const RequestParams* params = &req->_params; |
| 72 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "clicert callback "); |
| 73 |
DavixError::clearError(&(req->_last_error)); |
| 74 |
|
| 75 |
if(params->getClientCertFunctionX509()){ |
| 76 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "call client cert callback "); |
| 77 |
SessionInfo infos; |
| 78 |
TRY_DAVIX{ |
| 79 |
params->getClientCertFunctionX509()(infos, cert); |
| 80 |
if(cert.hasCert() == false){ |
| 81 |
throw DavixException(davix_scope_x509cred(), StatusCode::AuthenticationError, |
| 82 |
"No valid credential given "); |
| 83 |
} |
| 84 |
ne_ssl_set_clicert(req->_sess->session, X509CredentialExtra::extract_ne_ssl_clicert(cert)); |
| 85 |
}CATCH_DAVIX(&(req->_last_error)); |
| 86 |
} |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
int NEONSession::provide_login_passwd_fn(void *userdata, const char *realm, int attempt, |
| 91 |
char *username, char *password){ |
| 92 |
(void) realm; |
| 93 |
NEONSession * req = static_cast<NEONSession*>(userdata); |
| 94 |
DavixError * tmp_err=NULL; |
| 95 |
int ret =-1; |
| 96 |
|
| 97 |
|
| 98 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "Try to get auth/password authentication from client"); |
| 99 |
|
| 100 |
if(attempt > n_max_auth ){ |
| 101 |
DavixError::setupError(&(req->_last_error), davix_scope_http_request(), StatusCode::LoginPasswordError, |
| 102 |
"Overpass allowed number of authentication attempt"); |
| 103 |
} |
| 104 |
|
| 105 |
const std::pair<authCallbackLoginPasswordBasic, void*> retcallback(req->_params.getClientLoginPasswordCallback()); |
| 106 |
std::pair<std::string , std::string > id; |
| 107 |
|
| 108 |
if(req->_u.getUserInfo().size() > 0){ |
| 109 |
id = userInfo_to_auth(req->_u.getUserInfo()); |
| 110 |
}else{ |
| 111 |
id = req->_params.getClientLoginPassword(); |
| 112 |
} |
| 113 |
|
| 114 |
if(id.first.empty() == false){ |
| 115 |
copy_std_string_to_buff(username, NE_ABUFSIZ, id.first.c_str()); |
| 116 |
copy_std_string_to_buff(password, NE_ABUFSIZ, id.second.c_str()); |
| 117 |
}else if(retcallback.first != NULL){ |
| 118 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "Try callback for login/passwd for {} time", attempt+1); |
| 119 |
SessionInfo infos; |
| 120 |
std::string tmp_login, tmp_password; |
| 121 |
DavixError::clearError(&(req->_last_error)); |
| 122 |
if( (ret = retcallback.first(retcallback.second, infos, tmp_login, tmp_password, attempt, &tmp_err) ) <0){ |
| 123 |
if(!tmp_err) |
| 124 |
DavixError::setupError(&tmp_err, davix_scope_http_request(), StatusCode::LoginPasswordError, |
| 125 |
"No valid login/passwd"); |
| 126 |
DavixError::propagateError(&(req->_last_error), tmp_err); |
| 127 |
return -1; |
| 128 |
} |
| 129 |
copy_std_string_to_buff(username, NE_ABUFSIZ, tmp_login.c_str()); |
| 130 |
copy_std_string_to_buff(password, NE_ABUFSIZ, tmp_password.c_str()); |
| 131 |
} |
| 132 |
|
| 133 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "get login/password with success...try server submission "); |
| 134 |
return 0; |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
NEONSession::NEONSession(NEONSessionFactory &f, NeonHandlePtr sess, const Uri & uri, const RequestParams & p, DavixError** err) : |
| 139 |
_f(f), |
| 140 |
_sess(std::move(sess)), |
| 141 |
_params(p), |
| 142 |
_last_error(NULL), |
| 143 |
_session_recycling(_f.getSessionCaching() && p.getKeepAlive()), |
| 144 |
reused(false), |
| 145 |
_u(uri) |
| 146 |
{ |
| 147 |
if(_sess) |
| 148 |
configureSession(_sess, _u, p, &NEONSession::provide_login_passwd_fn, this, &NEONSession::authNeonCliCertMapper, this, reused); |
| 149 |
} |
| 150 |
|
| 151 |
NEONSession::~NEONSession(){ |
| 152 |
if(_sess){ |
| 153 |
if(_session_recycling) { |
| 154 |
_f.storeNeonSession(std::move(_sess)); |
| 155 |
} |
| 156 |
else { |
| 157 |
_sess.reset(); |
| 158 |
} |
| 159 |
} |
| 160 |
DavixError::clearError(&_last_error); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
void configureSession(NeonHandlePtr &_sess, const Uri & _u, const RequestParams ¶ms, ne_auth_creds lp_callback, void* lp_userdata, |
| 165 |
ne_ssl_provide_fn cred_callback, void* cred_userdata, bool & reused){ |
| 166 |
|
| 167 |
void* state = ne_get_session_private(_sess->session, davix_neon_key); |
| 168 |
if(state != NULL){ |
| 169 |
reused = true; |
| 170 |
} |
| 171 |
|
| 172 |
if(state == NULL || state != params.getParmState()){ |
| 173 |
// no configuration done, need to configure |
| 174 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_HTTP, "configure session..."); |
| 175 |
|
| 176 |
if(strcmp(ne_get_scheme(_sess->session), "https") ==0) // fix a libneon bug with non ssl connexion |
| 177 |
ne_ssl_trust_default_ca(_sess->session); |
| 178 |
|
| 179 |
// register redirection management |
| 180 |
ne_redirect_register(_sess->session); |
| 181 |
|
| 182 |
// define user agent |
| 183 |
ne_set_useragent(_sess->session, params.getUserAgent().c_str()); |
| 184 |
|
| 185 |
if(params.getSSLCACheck() == false){ // configure ssl check |
| 186 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "disable ssl verification"); |
| 187 |
ne_ssl_set_verify(_sess->session, validate_all_certificate, NULL); |
| 188 |
} |
| 189 |
|
| 190 |
if(timespec_isset(params.getConnectionTimeout())){ |
| 191 |
const int timeout = static_cast<int>(params.getConnectionTimeout()->tv_sec); |
| 192 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "define connection timeout to {}" , timeout); |
| 193 |
ne_set_connect_timeout(_sess->session, timeout); |
| 194 |
} |
| 195 |
|
| 196 |
if(timespec_isset(params.getOperationTimeout())){ |
| 197 |
const int timeout = static_cast<int>(params.getOperationTimeout()->tv_sec); |
| 198 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "define operation timeout to {}" , timeout); |
| 199 |
ne_set_read_timeout(_sess->session, timeout); |
| 200 |
} |
| 201 |
|
| 202 |
for(std::vector<std::string>::const_iterator it = params.listCertificateAuthorityPath().begin(); it < params.listCertificateAuthorityPath().end(); it++){ |
| 203 |
struct stat st; |
| 204 |
if ( stat(it->c_str(), &st) < 0 || S_ISDIR(st.st_mode) == false){ |
| 205 |
DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_HTTP, "CA Path invalid : {}, {} ", *it, ((errno != 0)?strerror(errno):strerror(ENOTDIR))); |
| 206 |
errno = 0; |
| 207 |
}else{ |
| 208 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_HTTP, "add CA PATH {}", *it); |
| 209 |
ne_ssl_truse_add_ca_path(_sess->session, it->c_str()); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
ne_set_session_flag(_sess->session, NE_SESSFLAG_PERSIST, params.getKeepAlive()); |
| 214 |
|
| 215 |
// setup sess key |
| 216 |
ne_set_session_private(_sess->session, davix_neon_key, params.getParmState()); |
| 217 |
} |
| 218 |
// configure callback for new request |
| 219 |
if( params.getClientLoginPassword().first.empty() == false |
| 220 |
|| _u.getUserInfo().size() > 0 |
| 221 |
|| params.getClientLoginPasswordCallback().first != NULL){ |
| 222 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "enable login/password authentication"); |
| 223 |
ne_set_server_auth(_sess->session, lp_callback, lp_userdata); |
| 224 |
}else{ |
| 225 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "disable login/password authentication"); |
| 226 |
} |
| 227 |
|
| 228 |
// if authentication for cli cert by callback |
| 229 |
if( params.getClientCertFunctionX509()){ |
| 230 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "enable client cert authentication by callback "); |
| 231 |
ne_ssl_provide_clicert(_sess->session, cred_callback, cred_userdata); |
| 232 |
}else{ |
| 233 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "disable client cert authentication"); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
} |