| 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 <algorithm> |
| 23 |
#include <status/davixstatusrequest.hpp> |
| 24 |
#include <stdsoap2.h> |
| 25 |
#include "delegation1H.h" |
| 26 |
|
| 27 |
#include "delegation.hpp" |
| 28 |
#include "GRSTx509MakeProxyCert.h" |
| 29 |
|
| 30 |
using namespace Davix; |
| 31 |
|
| 32 |
/// Do the delegation |
| 33 |
std::string DavixDelegation::delegate_v1(Context & context, |
| 34 |
const std::string &dlg_endpint, const RequestParams& params, |
| 35 |
const std::string& ucred, const std::string& passwd, |
| 36 |
const std::string& capath, |
| 37 |
int lifetime, DavixError** err) |
| 38 |
{ |
| 39 |
(void) context; |
| 40 |
(void) params; |
| 41 |
|
| 42 |
std::string delegation_id; |
| 43 |
std::string reqtxt; |
| 44 |
char* certtxt = NULL; |
| 45 |
|
| 46 |
struct soap *soap_get = NULL, *soap_put = NULL; |
| 47 |
delegation1::tns__getNewProxyReqResponse getNewProxyReqResponse; |
| 48 |
delegation1::tns__putProxyResponse putProxyResponse; |
| 49 |
const char* url = dlg_endpint.c_str(); |
| 50 |
char err_buffer[512]; |
| 51 |
size_t err_aux; |
| 52 |
|
| 53 |
// Request a new delegation ID |
| 54 |
soap_get = soap_new(); |
| 55 |
soap_get->keep_alive = 1; |
| 56 |
|
| 57 |
if (soap_ssl_client_context(soap_get, SOAP_SSL_DEFAULT, ucred.c_str(), passwd.c_str(), |
| 58 |
ucred.c_str(), capath.c_str(), NULL) == 0) { |
| 59 |
soap_call_tns__getNewProxyReq(soap_get, |
| 60 |
url, |
| 61 |
"http://www.gridsite.org/namespaces/delegation-1", |
| 62 |
getNewProxyReqResponse); |
| 63 |
|
| 64 |
if(soap_get->error == 0) { |
| 65 |
reqtxt = getNewProxyReqResponse.getNewProxyReqReturn->proxyRequest; |
| 66 |
delegation_id = getNewProxyReqResponse.getNewProxyReqReturn->delegationID; |
| 67 |
|
| 68 |
// Generate proxy |
| 69 |
if (GRSTx509MakeProxyCert(&certtxt, stderr, (char*)reqtxt.c_str(), |
| 70 |
(char*)ucred.c_str(), (char*)ucred.c_str(), lifetime) == GRST_RET_OK) { |
| 71 |
// Submit the proxy |
| 72 |
soap_put = soap_new(); |
| 73 |
|
| 74 |
if (soap_ssl_client_context(soap_put, SOAP_SSL_DEFAULT, ucred.c_str(), passwd.c_str(), |
| 75 |
ucred.c_str(), capath.c_str(), NULL) == 0) { |
| 76 |
soap_call_tns__putProxy(soap_put, |
| 77 |
url, |
| 78 |
"http://www.gridsite.org/namespaces/delegation-1", |
| 79 |
(char*)delegation_id.c_str(), certtxt, putProxyResponse); |
| 80 |
if (soap_put->error) { |
| 81 |
// Could not PUT |
| 82 |
err_aux = snprintf(err_buffer, sizeof(err_buffer), "Could not PUT the proxy: "); |
| 83 |
soap_sprint_fault(soap_put, err_buffer + err_aux, sizeof(err_buffer) - err_aux); |
| 84 |
|
| 85 |
DavixError::setupError(err, DELEGATION_SCOPE, StatusCode::DelegationError, |
| 86 |
err_buffer); |
| 87 |
} |
| 88 |
} |
| 89 |
else { // soap_put ssl error |
| 90 |
err_aux = snprintf(err_buffer, sizeof(err_buffer), "Connection error on proxy put: "); |
| 91 |
soap_sprint_fault(soap_put, err_buffer + err_aux, sizeof(err_buffer) - err_aux); |
| 92 |
|
| 93 |
DavixError::setupError(err, DELEGATION_SCOPE, StatusCode::DelegationError, |
| 94 |
err_buffer); |
| 95 |
} |
| 96 |
|
| 97 |
soap_free(soap_put); |
| 98 |
} |
| 99 |
else { |
| 100 |
DavixError::setupError(err, DELEGATION_SCOPE, StatusCode::DelegationError, |
| 101 |
std::string("Could not generate the proxy: ") + err_buffer); |
| 102 |
} |
| 103 |
} |
| 104 |
else { // Could not get ID |
| 105 |
err_aux = snprintf(err_buffer, sizeof(err_buffer), "Could not get proxy request: "); |
| 106 |
soap_sprint_fault(soap_get, err_buffer + err_aux, sizeof(err_buffer) - err_aux); |
| 107 |
|
| 108 |
DavixError::setupError(err, DELEGATION_SCOPE, StatusCode::DelegationError, |
| 109 |
std::string("Could not get the delegation id: ") + err_buffer); |
| 110 |
} |
| 111 |
} |
| 112 |
else { // soap_get ssl error |
| 113 |
err_aux = snprintf(err_buffer, sizeof(err_buffer), "Could not connect to get the proxy request: "); |
| 114 |
soap_sprint_fault(soap_get, err_buffer + err_aux, sizeof(err_buffer) - err_aux); |
| 115 |
DavixError::setupError(err, DELEGATION_SCOPE, StatusCode::DelegationError, |
| 116 |
std::string("Could not connect to the delegation endpoint: ") + err_buffer); |
| 117 |
} |
| 118 |
|
| 119 |
// Clean soap_get |
| 120 |
soap_free(soap_get); |
| 121 |
free(certtxt); |
| 122 |
|
| 123 |
// Return delegation ID |
| 124 |
return delegation_id; |
| 125 |
} |