14
15
static Logger logger(Logger::getRootLogger(), "GSSCredential");
16
17
GSSCredential::GSSCredential(const std::string& proxyPath,
17
const std::string& certificatePath,
18
const std::string& keyPath)
19
: credential(GSS_C_NO_CREDENTIAL) {
18
const std::string& certificatePath,
19
const std::string& keyPath)
20
: credential(GSS_C_NO_CREDENTIAL) {
22
initCred(readCredFromFiles(proxyPath, certificatePath, keyPath));
25
GSSCredential::GSSCredential(const UserConfig& usercfg)
26
: credential(GSS_C_NO_CREDENTIAL) {
28
if (!usercfg.CredentialString().empty()) initCred(usercfg.CredentialString());
29
else initCred(readCredFromFiles(usercfg.ProxyPath(), usercfg.CertificatePath(), usercfg.KeyPath()));
32
std::string GSSCredential::readCredFromFiles(const std::string& proxyPath,
33
const std::string& certificatePath,
34
const std::string& keyPath) {
21
35
std::string credbuf;
23
37
if (!proxyPath.empty()) {
24
38
std::ifstream is(proxyPath.c_str());
25
39
getline(is, credbuf, '\0');
26
40
if(!is || credbuf.empty()) {
27
logger.msg(ERROR, "Failed to read proxy file: %s", proxyPath);
41
logger.msg(ERROR, "Failed to read proxy file: %s", proxyPath);
31
45
else if (!certificatePath.empty() && !keyPath.empty()) {
32
46
std::ifstream is(certificatePath.c_str());
33
47
getline(is, credbuf, '\0');
34
48
if(!is || credbuf.empty()) {
35
logger.msg(ERROR, "Failed to read certificate file: %s",
49
logger.msg(ERROR, "Failed to read certificate file: %s", certificatePath);
39
52
std::string keybuf;
40
53
std::ifstream ik(keyPath.c_str());
41
54
getline(ik, keybuf, '\0');
42
55
if(!ik || keybuf.empty()) {
43
logger.msg(ERROR, "Failed to read private key file: %s", keyPath);
56
logger.msg(ERROR, "Failed to read private key file: %s", keyPath);
50
if(!credbuf.empty()) {
51
//Convert to GSS credental only if find credential content
52
OM_uint32 majstat, minstat;
55
gbuf.value = (void*)credbuf.c_str();
56
gbuf.length = credbuf.length();
58
majstat = gss_import_cred(&minstat, &credential, NULL, 0,
59
&gbuf, GSS_C_INDEFINITE, NULL);
61
if (GSS_ERROR(majstat)) {
62
credential = GSS_C_NO_CREDENTIAL;
63
logger.msg(ERROR, "Failed to convert GSI credential to "
64
"GSS credential (major: %d, minor: %d)%s", majstat, minstat, ErrorStr(majstat, minstat));
65
void GSSCredential::initCred(const std::string& credbuf) {
67
if(credbuf.empty()) return;
68
//Convert to GSS credental only if find credential content
69
OM_uint32 majstat, minstat;
72
gbuf.value = (void*)credbuf.c_str();
73
gbuf.length = credbuf.length();
75
majstat = gss_import_cred(&minstat, &credential, NULL, 0,
76
&gbuf, GSS_C_INDEFINITE, NULL);
78
if (GSS_ERROR(majstat)) {
79
credential = GSS_C_NO_CREDENTIAL;
80
logger.msg(ERROR, "Failed to convert GSI credential to "
81
"GSS credential (major: %d, minor: %d)%s", majstat, minstat, ErrorStr(majstat, minstat));