~ubuntu-branches/ubuntu/trusty/nordugrid-arc/trusty-proposed

« back to all changes in this revision

Viewing changes to src/hed/libs/globusutils/GSSCredential.cpp

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2013-11-29 13:39:10 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20131129133910-altaxrfowczzl2ev
Tags: 4.0.0-1
4.0.0 Release (Closes: #715131) (LP: #1049798)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
#include <fstream>
6
6
 
7
7
#include <arc/Logger.h>
 
8
#include <arc/UserConfig.h>
8
9
 
9
10
#include "GlobusErrorUtils.h"
10
11
#include "GSSCredential.h"
14
15
  static Logger logger(Logger::getRootLogger(), "GSSCredential");
15
16
 
16
17
  GSSCredential::GSSCredential(const std::string& proxyPath,
17
 
                               const std::string& certificatePath,
18
 
                               const std::string& keyPath)
19
 
    : credential(GSS_C_NO_CREDENTIAL) {
20
 
 
 
18
                               const std::string& certificatePath,
 
19
                               const std::string& keyPath)
 
20
    : credential(GSS_C_NO_CREDENTIAL) {
 
21
 
 
22
    initCred(readCredFromFiles(proxyPath, certificatePath, keyPath));
 
23
  }
 
24
 
 
25
  GSSCredential::GSSCredential(const UserConfig& usercfg)
 
26
    : credential(GSS_C_NO_CREDENTIAL) {
 
27
 
 
28
    if (!usercfg.CredentialString().empty()) initCred(usercfg.CredentialString());
 
29
    else initCred(readCredFromFiles(usercfg.ProxyPath(), usercfg.CertificatePath(), usercfg.KeyPath()));
 
30
  }
 
31
 
 
32
  std::string GSSCredential::readCredFromFiles(const std::string& proxyPath,
 
33
                                               const std::string& certificatePath,
 
34
                                               const std::string& keyPath) {
21
35
    std::string credbuf;
22
36
  
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);
28
 
        return;
 
41
        logger.msg(ERROR, "Failed to read proxy file: %s", proxyPath);
 
42
        return "";
29
43
      }
30
44
    }
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",
36
 
                   certificatePath);
37
 
        return;
 
49
        logger.msg(ERROR, "Failed to read certificate file: %s", certificatePath);
 
50
        return "";
38
51
      }
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);
44
 
        return;
 
56
        logger.msg(ERROR, "Failed to read private key file: %s", keyPath);
 
57
        return "";
45
58
      }
46
59
      credbuf += "\n";
47
60
      credbuf += keybuf;
48
61
    }
49
 
 
50
 
    if(!credbuf.empty()) { 
51
 
      //Convert to GSS credental only if find credential content
52
 
      OM_uint32 majstat, minstat;
53
 
      gss_buffer_desc gbuf;
54
 
 
55
 
      gbuf.value = (void*)credbuf.c_str();
56
 
      gbuf.length = credbuf.length();
57
 
 
58
 
      majstat = gss_import_cred(&minstat, &credential, NULL, 0,
59
 
                              &gbuf, GSS_C_INDEFINITE, NULL);
60
 
 
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
 
        return;
66
 
      }
 
62
    return credbuf;
 
63
  }
 
64
 
 
65
  void GSSCredential::initCred(const std::string& credbuf) {
 
66
 
 
67
    if(credbuf.empty()) return;
 
68
    //Convert to GSS credental only if find credential content
 
69
    OM_uint32 majstat, minstat;
 
70
    gss_buffer_desc gbuf;
 
71
 
 
72
    gbuf.value = (void*)credbuf.c_str();
 
73
    gbuf.length = credbuf.length();
 
74
 
 
75
    majstat = gss_import_cred(&minstat, &credential, NULL, 0,
 
76
          &gbuf, GSS_C_INDEFINITE, NULL);
 
77
 
 
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));
67
82
    }
68
83
  }
69
84