2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
8 #include "GSSAcquireCred.h"
9 #include "GSSException.h"
10 #include <cache/GSSNameCache.h>
14 GSSAcquireCred::GSSAcquireCred(gss_acq_cred_type fn) : function(fn)
16 desired_name = GSS_C_NO_NAME;
17 desiredMechs.addOID( GSSOID((char *)"{ 1 3 6 1 5 5 15 1 1 18 }") );
20 GSSAcquireCred::GSSAcquireCred ( const GSSAcquireCred& other )
25 GSSAcquireCred::~GSSAcquireCred()
30 GSSAcquireCred::GSSAcquireCred (
33 ) : GSSCommand ( params )
39 loadParameters(params);
46 bool GSSAcquireCred::loadParameters(JSONObject *params)
49 std::string sCredUsage;
57 // Easy stuff(*params)
58 this->time_req = (*params)["arguments"]["time_req"].integer();
63 if ( ! params->get("cred_usage").isNull() )
65 if (params->get("cred_usage").isString())
67 sCredUsage = params->get("cred_usage").string();
68 if (sCredUsage == "GSS_C_BOTH")
69 this->cred_usage = GSS_C_BOTH;
70 else if (sCredUsage == "GSS_C_INITIATE")
71 this->cred_usage = GSS_C_INITIATE;
72 else if (sCredUsage == "GSS_C_ACCEPT")
73 this->cred_usage = GSS_C_ACCEPT;
75 throw std::invalid_argument( std::string("Invalid cred_usage type given: ") + sCredUsage );
76 } else if (params->get("cred_usage").isInteger())
77 this->cred_usage = (gss_cred_usage_t)( params->get("cred_usage").integer() );
79 throw std::invalid_argument( "Unrecognized argument type for cred_usage." );
85 if ( ! params->get("desired_mechs").isNull() )
87 if ( params->get("desired_mechs").isArray() )
89 for (nDesiredMechs = 0;
90 nDesiredMechs < params->get("desired_mechs").size();
93 std::string mechStr = params->get("desired_mechs")[nDesiredMechs].string();
94 desiredMechs.addOID( GSSOID(mechStr).toGss() );
97 throw std::invalid_argument("Unrecognized desired_mechs array.");
103 if ( ! params->get("desired_name").isNull() )
105 std::string key = params->get("desired_name").string();
106 this->desired_name = GSSNameCache::instance()->retrieve(key);
117 void GSSAcquireCred::execute()
120 gss_cred_id_t output_cred_handle;
121 gss_OID_set actual_mechs;
126 this->retVal = function(
128 this->desired_name.toGss(),
130 this->desiredMechs.toGss(),
137 if (GSS_ERROR(this->retVal) )
139 std::string err("Error acquiring credential for user '");
140 err += desired_name.toString();
142 throw GSSException(err, this->retVal, this->minor_status);
145 this->cred.setValue(output_cred_handle);
146 this->actualMechs = actual_mechs;
152 /* Desired JSON output:
157 * "cred_handle": "###########",
165 JSONObject *GSSAcquireCred::toJSON()
168 JSONObject *values = new JSONObject();
178 values->set("major_status", this->retVal);
179 values->set("minor_status", this->minor_status);
180 values->set("time_rec", (int)this->time_rec );
182 // Objects that generate their own JSONObject
183 temp = this->cred.toJSONValue();
184 values->set("output_cred_handle", *temp );
186 temp = this->actualMechs.toJSONValue();
187 values->set("actual_mechs", *temp);