Update tests to deal with GSSCommand objects now taking only the 'argument' part...
[gssweb.git] / json_gssapi / src / commands / GSSAcquireCred.cpp
1 /*
2  * Copyright (c) 2014, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include "GSSAcquireCred.h"
36 #include "GSSException.h"
37 #include <cache/GSSNameCache.h>
38
39 #include <stdexcept>
40
41 GSSAcquireCred::GSSAcquireCred(gss_acq_cred_type fn) : function(fn)
42 {
43   desired_name = GSS_C_NO_NAME;
44   desiredMechs.addOID( GSSOID((char *)"{ 1 3 6 1 5 5 15 1 1 18 }") );
45 }
46
47 GSSAcquireCred::GSSAcquireCred ( const GSSAcquireCred& other )
48 {
49
50 }
51
52 GSSAcquireCred::~GSSAcquireCred()
53 {
54
55 }
56
57 GSSAcquireCred::GSSAcquireCred ( 
58     JSONObject *params, 
59     gss_acq_cred_type fn
60 ) : GSSCommand ( params )
61 {
62   /* Variables */
63   /* Error checking */
64   /* Setup */
65   /* Main */
66   loadParameters(params);
67   function = fn;
68   /* Cleanup */
69   /* Return */
70 }
71
72
73 bool GSSAcquireCred::loadParameters(JSONObject *params)
74 {
75   /* Variables */
76   std::string sCredUsage;
77   size_t nDesiredMechs;
78   
79   /* Error checking */
80   /* Setup */
81   // Should I zeroOut?
82   
83   /* Main processing */
84   // Easy stuff(*params)
85   this->time_req = (*params)["time_req"].integer();
86
87   /**************
88    * cred_usage *
89    **************/
90   if ( ! params->get("cred_usage").isNull() )
91   {
92     if (params->get("cred_usage").isString())
93     {
94       sCredUsage = params->get("cred_usage").string();
95       if (sCredUsage == "GSS_C_BOTH")
96         this->cred_usage = GSS_C_BOTH;
97       else if (sCredUsage == "GSS_C_INITIATE")
98         this->cred_usage = GSS_C_INITIATE;
99       else if (sCredUsage == "GSS_C_ACCEPT")
100         this->cred_usage = GSS_C_ACCEPT;
101       else
102         throw std::invalid_argument( std::string("Invalid cred_usage type given: ") + sCredUsage );
103     } else if (params->get("cred_usage").isInteger())
104       this->cred_usage = (gss_cred_usage_t)( params->get("cred_usage").integer() );
105     else
106       throw std::invalid_argument( "Unrecognized argument type for cred_usage." );
107       }
108   
109   /*****************
110    * desired_mechs *
111    *****************/
112   if ( ! params->get("desired_mechs").isNull() )
113   {
114     if ( params->get("desired_mechs").isArray() )
115     {
116       for (nDesiredMechs = 0; 
117           nDesiredMechs < params->get("desired_mechs").size();
118           nDesiredMechs++)
119       {
120         std::string mechStr = params->get("desired_mechs")[nDesiredMechs].string();
121         desiredMechs.addOID( GSSOID(mechStr).toGss() );
122       }
123     } else
124       throw std::invalid_argument("Unrecognized desired_mechs array.");
125   }
126
127   /****************
128    * desired_name *
129    ****************/
130   if ( ! params->get("desired_name").isNull() )
131   {
132     std::string key = params->get("desired_name").string();
133     this->desired_name = GSSNameCache::instance()->retrieve(key);
134   }
135
136   
137   /* Cleanup */
138   /* Return */
139   return true;
140 }
141
142
143
144 void GSSAcquireCred::execute()
145 {
146   /* Variables */
147   gss_cred_id_t output_cred_handle;
148   gss_OID_set   actual_mechs;
149   
150   /* Error checking */
151   /* Setup */
152   /* Main */
153   this->retVal = function(
154     &this->minor_status,
155     this->desired_name.toGss(),
156     this->time_req,
157     this->desiredMechs.toGss(),
158     this->cred_usage,
159     &output_cred_handle,
160     &actual_mechs,
161     &this->time_rec
162   );
163   
164   if (GSS_ERROR(this->retVal) )
165   {
166     std::string err("Error acquiring credential for user '");
167     err += desired_name.toString();
168     err += "'.";
169     throw GSSException(err, this->retVal, this->minor_status);
170   }
171   
172   this->cred.setValue(output_cred_handle);
173   this->actualMechs = actual_mechs;
174   
175   /* Cleanup */
176   /* Return */
177 }
178
179 /* Desired JSON output:
180  * 
181  * {
182  *     "major_status": 0,
183  *     "minor_status": 0,
184  *     "cred_handle": "###########",
185  *     "actual_mechs": [
186  *       "{ 1 2 3 4 }",
187  *       "{ 5 6 7 8 }"
188  *     ],
189  *     "time_rec": 0
190  * }
191  */
192 JSONObject *GSSAcquireCred::toJSON()
193 {
194   /* Variables */
195   JSONObject *values = new JSONObject();
196   JSONObject *temp;
197   
198   /* Error checking */
199   
200   /* Setup */
201   
202   /* Main */
203   // Return Values
204   // Easy stuff
205   values->set("major_status", this->retVal);
206   values->set("minor_status", this->minor_status);
207   values->set("time_rec", (int)this->time_rec );
208   
209   // Objects that generate their own JSONObject
210   temp = this->cred.toJSONValue();
211   values->set("output_cred_handle", *temp );
212   
213   temp = this->actualMechs.toJSONValue();
214   values->set("actual_mechs", *temp);
215   
216   /* Cleanup */
217   
218   /* Return */
219   return(values);
220 }