Rework the invalid argument for init sec context exception
[gssweb.git] / json_gssapi / src / GSSRequest.cpp
1 /*
2  * Copyright (c) 2014, 2015 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 <cstddef>
36 #include <stdexcept>
37 #include <string.h>
38
39 #include "commands/GSSAcquireCred.h"
40 #include "commands/GSSInitSecContext.h"
41 #include "commands/GSSImportName.h"
42 #include "commands/GSSDisplayName.h"
43 #include "GSSRequest.h"
44 #include "GSSException.h"
45
46 using std::bad_alloc;
47
48 GSSRequest::GSSRequest ( string jsonString )
49 {
50   /* Local variables */
51   /* Error checking */
52   /* Setup */
53   /* Main processing */
54   response = JSONObject();
55   cmd = NULL;
56   requestString = jsonString;
57   
58   /* Cleanup */
59   /* Return */
60 }
61
62 void GSSRequest::execute()
63 {
64   try {
65     /* variables */
66     /* Error checking */
67     /* Setup */
68     parseJSON();
69     getCommand();
70     
71     /* Main processing */
72     if (NULL != cmd)
73       cmd->execute();
74   }
75   catch (GSSException e)
76   {
77     /* Cleanup */
78     if ( NULL != cmd )
79     {
80       delete(cmd);
81       cmd = NULL;
82     }
83
84     JSONObject return_values, errors;
85     errors.set("major_status_message", e.major_status_message().c_str());
86     errors.set("minor_status_message", e.minor_status_message().c_str());
87     errors.set("what", e.what());
88     return_values.set("errors", errors);
89     return_values.set("major_status", e.getMajor());
90     return_values.set("minor_status", e.getMinor());
91     response.set("return_values", return_values);
92   }
93   catch (std::invalid_argument e)
94   {
95     /* Cleanup */
96     if ( NULL != cmd )
97     {
98       delete(cmd);
99       cmd = NULL;
100     }
101
102     JSONObject return_values, errors;
103     errors.set("major_status_message", "An error occurred in parsing the JSON arguments.\0");
104     errors.set("minor_status_message", e.what());
105     return_values.set("errors", errors);
106     return_values.set("major_status", -1);
107     return_values.set("minor_status", -1);
108     response.set("return_values", return_values);
109   }
110
111   /* return */
112 }
113
114
115
116 void GSSRequest::parseJSON()
117 {
118   /* variables */
119   json_error_t jsonErr;
120   
121   try {
122     JSONObject cookies;
123     request = JSONObject::load(requestString.c_str(), 0, &jsonErr);
124     cookies = request.get("cookies");
125     response.set("cookies", cookies );
126     response.set("method", request.get("method").string());
127   }
128   /* bad_alloc is thrown when JSONObject can't parse the input string as JSON */
129   catch ( bad_alloc& ) 
130   {
131     // Top-level response
132     response.set("error_message", "Could not parse the input JSON.");
133     response.set("original_message", requestString.c_str());
134   }
135 }
136
137
138 void GSSRequest::getCommand()
139 {
140   string method;
141   JSONObject arguments = request.get("arguments");
142   
143   /* Error checking */
144   /* Setup */
145   if (request.get("method").isNull() )
146     method = "";
147   else
148     method = string( request.get("method").string() );
149   
150   if ( "gss_import_name" == method ) 
151   {
152     cmd = new GSSImportName ( &arguments );
153   } 
154   else if ( "gss_init_sec_context" == method ) 
155   {
156     cmd = new GSSInitSecContext ( &arguments );
157   } 
158   else if ( "gss_acquire_cred" == method ) 
159   {
160     cmd = new GSSAcquireCred ( &arguments );
161   } 
162   else if ( "gss_display_name" == method )
163   {
164     cmd = new GSSDisplayName ( &arguments );
165   }
166   else 
167   {
168     string error_message = string("Unrecognized command: ") + method;
169     response.set("error_message", error_message.c_str() );
170     response.set("original_message", requestString.c_str());
171     cmd = NULL;
172   }
173 }
174
175 string GSSRequest::getResponse()
176 {
177   /* Variables */
178   JSONObject *return_values;
179   string gssResponse;
180   
181   /* Main processing */
182   // Put the return values into the response, assuming that the command
183   // was actually executed.
184   if (NULL != cmd)
185   {
186     return_values = cmd->toJSON();
187     response.set("return_values", *return_values);
188   }
189   
190   // Convert the response into a string to return.
191   gssResponse = string( response.dump() );
192   
193   /* Return */
194   return(gssResponse);
195 }
196
197
198
199 char *gss_request(char *json_string)
200 {
201   /* Variables */
202   GSSRequest *req = new GSSRequest(string(json_string));
203   
204   /* Error checking */
205   // An empty json_string could be an error, but GSSRequest does
206   // a good job of handling it.
207   
208   /* Setup */
209   /* Main processing */
210   req->execute();
211   return strdup(req->getResponse().c_str());
212 }
213
214 void deallocate_reply(char *reply)
215 {
216   delete(reply);
217 }