2 * Copyright (c) 2014, 2015 JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
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"
48 GSSRequest::GSSRequest ( string jsonString )
54 response = JSONObject();
56 requestString = jsonString;
62 void GSSRequest::execute()
75 catch (GSSException e)
84 JSONObject return_values;
85 return_values.set("major_status", e.getMajor());
86 return_values.set("minor_status", e.getMinor());
87 return_values.set("what", e.what());
88 response.set("return_values", return_values);
90 catch (std::invalid_argument e)
99 JSONObject return_values, errors;
100 errors.set("major_status_message", "An error occurred in parsing the JSON arguments.\0");
101 errors.set("minor_status_message", e.what());
102 return_values.set("errors", errors);
103 return_values.set("major_status", -1);
104 return_values.set("minor_status", -1);
105 response.set("return_values", return_values);
113 void GSSRequest::parseJSON()
116 json_error_t jsonErr;
120 request = JSONObject::load(requestString.c_str(), 0, &jsonErr);
121 cookies = request.get("cookies");
122 response.set("cookies", cookies );
123 response.set("method", request.get("method").string());
125 /* bad_alloc is thrown when JSONObject can't parse the input string as JSON */
128 // Top-level response
129 response.set("error_message", "Could not parse the input JSON.");
130 response.set("original_message", requestString.c_str());
135 void GSSRequest::getCommand()
138 JSONObject arguments = request.get("arguments");
142 if (request.get("method").isNull() )
145 method = string( request.get("method").string() );
147 if ( "gss_import_name" == method )
149 cmd = new GSSImportName ( &arguments );
151 else if ( "gss_init_sec_context" == method )
153 cmd = new GSSInitSecContext ( &arguments );
155 else if ( "gss_acquire_cred" == method )
157 cmd = new GSSAcquireCred ( &arguments );
159 else if ( "gss_display_name" == method )
161 cmd = new GSSDisplayName ( &arguments );
165 string error_message = string("Unrecognized command: ") + method;
166 response.set("error_message", error_message.c_str() );
167 response.set("original_message", requestString.c_str());
172 string GSSRequest::getResponse()
175 JSONObject *return_values;
178 /* Main processing */
179 // Put the return values into the response, assuming that the command
180 // was actually executed.
183 return_values = cmd->toJSON();
184 response.set("return_values", *return_values);
187 // Convert the response into a string to return.
188 gssResponse = string( response.dump() );
196 char *gss_request(char *json_string)
199 GSSRequest *req = new GSSRequest(string(json_string));
202 // An empty json_string could be an error, but GSSRequest does
203 // a good job of handling it.
206 /* Main processing */
208 return strdup(req->getResponse().c_str());
211 void deallocate_reply(char *reply)