Debian Codegraph @

src/modules/copy/delegation/delegation2.cpp

raw
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 <stdsoap2.h>
23
24 #include "delegation.hpp"
25 #include "delegation2H.h"
26 #include <utils/davix_logger_internal.hpp>
27
28 #include "GRSTx509MakeProxyCert.h"
29
30 using namespace Davix;
31 using namespace delegation2;
32
33 static void err2davix(struct soap* soap, DavixError** err, const std::string& prefix)
34 {
35 char err_buffer[512];
36 soap_sprint_fault(soap, err_buffer, sizeof(err_buffer));
37 DavixError::setupError(err, DELEGATION_SCOPE, StatusCode::DelegationError,
38 prefix + err_buffer);
39 }
40
41
42 static bool get_delegated_credentials(struct soap* soap,
43 const std::string& endpoint, const std::string& dlg_id,
44 time_t* lifetime)
45 {
46 struct tns2__getTerminationTimeResponse resp;
47 char *dlg_id_ptr = soap_strdup(soap, dlg_id.c_str());
48
49 if (SOAP_OK != soap_call_tns2__getTerminationTime(soap, endpoint.c_str(), NULL, dlg_id_ptr, resp)) {
50 char err_buffer[512];
51 soap_sprint_fault(soap, err_buffer, sizeof(err_buffer));
52 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_GRID, "Could not retrieve delegated credentials: {}", err_buffer);
53 return false;
54 }
55
56 *lifetime = resp._getTerminationTimeReturn - time(NULL);
57 return true;
58 }
59
60
61 static void renew_proxy(struct soap* soap, const std::string& endpoint,
62 const std::string& ucred, const std::string& dlg_id, int lifetime, bool force, DavixError** err)
63 {
64 int ret;
65 char* dlg_id_ptr = soap_strdup(soap, dlg_id.c_str());
66 //char *sdelegationID = (char *) "", *localproxy, *certtxt, *;
67 char *certtxt, *scerttxt;
68 std::string certreq;
69
70 struct tns2__getProxyReqResponse get_resp;
71 struct tns2__renewProxyReqResponse renew_resp;
72 struct tns2__putProxyResponse put_resp;
73
74 if (force) {
75 DAVIX_SLOG(DAVIX_LOG_VERBOSE, DAVIX_LOG_GRID, "Renew proxy request");
76
77 ret = soap_call_tns2__renewProxyReq(soap, endpoint.c_str(), NULL, dlg_id_ptr, renew_resp);
78 if (SOAP_OK != ret) {
79 err2davix(soap, err, "Renewal failed");
80 return;
81 }
82 certreq = renew_resp._renewProxyReqReturn;
83 }
84
85 /* if it was forced and failed, or if it was not forced at all */
86 if (certreq.empty()) {
87 DAVIX_SLOG(DAVIX_LOG_VERBOSE, DAVIX_LOG_GRID, "Get proxy request");
88 /* there was no proxy, or not forcing -- the normal path */
89 ret = soap_call_tns2__getProxyReq(soap, endpoint.c_str(), NULL,
90 dlg_id_ptr, get_resp);
91 if (SOAP_OK != ret) {
92 err2davix(soap, err, "Renewal failed");
93 return;
94 }
95 certreq = get_resp._getProxyReqReturn;
96 }
97
98 /* generating a certificate from the request */
99 if (!certreq.empty()) {
100 DAVIX_SLOG(DAVIX_LOG_VERBOSE, DAVIX_LOG_GRID, "Sign proxy request");
101 ret = GRSTx509MakeProxyCert(&certtxt, stderr, (char*)certreq.c_str(),
102 (char*)ucred.c_str(), (char*)ucred.c_str(), lifetime);
103 }
104 else {
105 DavixError::setupError(err, DELEGATION_SCOPE, StatusCode::DelegationError, "Could not get a request");
106 return;
107
108 }
109 if (ret != GRST_RET_OK) {
110 DavixError::setupError(err, DELEGATION_SCOPE, StatusCode::DelegationError, "GRSTx509MakeProxyCert failed");
111 return;
112 }
113
114 scerttxt = soap_strdup(soap, certtxt);
115 if (!scerttxt) {
116 DavixError::setupError(err, DELEGATION_SCOPE, StatusCode::DelegationError, "Could not duplicate");
117 return;
118 }
119
120 DAVIX_SLOG(DAVIX_LOG_VERBOSE, DAVIX_LOG_GRID, "Put new proxy");
121 if (SOAP_OK != soap_call_tns2__putProxy(soap, endpoint.c_str(), NULL, dlg_id_ptr, scerttxt, put_resp)) {
122 err2davix(soap, err, "Renewal failed");
123 return;
124 }
125 }
126
127
128 std::string DavixDelegation::delegate_v2(Context & context, const std::string &dlg_endpoint,
129 const Davix::RequestParams& params,
130 const std::string& ucred, const std::string& passwd,
131 const std::string& capath,
132 int lifetime, Davix::DavixError** err)
133 {
134 (void) context;
135 (void) params;
136 struct soap* soap;
137 soap = soap_new();
138
139 std::string dlg_id = "1234";
140
141 if (soap_ssl_client_context(soap, SOAP_SSL_DEFAULT, ucred.c_str(), passwd.c_str(),
142 ucred.c_str(), capath.c_str(), NULL) != 0) {
143 err2davix(soap, err, "Could not connect to the delegation endpoint: ");
144 return std::string();
145 }
146
147 // Check if there is already a delegation done
148 bool renew_delegation = false;
149 bool new_delegation = false;
150 time_t delegated_lifetime;
151
152 if(!get_delegated_credentials(soap, dlg_endpoint, dlg_id, &delegated_lifetime)) {
153 DAVIX_SLOG(DAVIX_LOG_VERBOSE, DAVIX_LOG_GRID, "No delegated credentials on the storage");
154 new_delegation = true;
155 }
156 else if (delegated_lifetime < lifetime) {
157 DAVIX_SLOG(DAVIX_LOG_VERBOSE, DAVIX_LOG_GRID, "Need to renew the credentials, {} > {}", lifetime, delegated_lifetime);
158 renew_delegation = true;
159 }
160 else {
161 DAVIX_SLOG(DAVIX_LOG_VERBOSE, DAVIX_LOG_GRID, "Remaining life of the delegated credentials: {}", delegated_lifetime);
162 }
163
164 if (new_delegation || renew_delegation)
165 renew_proxy(soap, dlg_endpoint, ucred, dlg_id, lifetime, renew_delegation, err);
166
167 soap_free(soap);
168 soap_done(soap);
169
170 if (*err)
171 return std::string();
172 else
173 return dlg_id;
174 }