Return an error when GSSInitSecContext is called with an unacceptable mechanism.
[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     JSONObject return_values;
78     return_values.set("major_status", e.getMajor());
79     return_values.set("minor_status", e.getMinor());
80     return_values.set("what", e.what());
81     response.set("return_values", return_values);
82   }
83   catch (std::invalid_argument e)
84   {
85     JSONObject return_values, errors;
86     errors.set("major_status_message", "An error occurred in parsing the JSON arguments.\0");
87     errors.set("minor_status_message", e.what());
88     return_values.set("errors", errors);
89     return_values.set("major_status", -1);
90     return_values.set("minor_status", -1);
91     response.set("return_values", return_values);
92   }
93
94   /* Cleanup */
95   if ( NULL != cmd )
96   {
97     delete(cmd);
98     cmd = NULL;
99   }
100
101   /* return */
102 }
103
104
105
106 void GSSRequest::parseJSON()
107 {
108   /* variables */
109   json_error_t jsonErr;
110   
111   try {
112     JSONObject cookies;
113     request = JSONObject::load(requestString.c_str(), 0, &jsonErr);
114     cookies = request.get("cookies");
115     response.set("cookies", cookies );
116     response.set("method", request.get("method").string());
117   }
118   /* bad_alloc is thrown when JSONObject can't parse the input string as JSON */
119   catch ( bad_alloc& ) 
120   {
121     // Top-level response
122     response.set("error_message", "Could not parse the input JSON.");
123     response.set("original_message", requestString.c_str());
124   }
125 }
126
127
128 void GSSRequest::getCommand()
129 {
130   string method;
131   JSONObject arguments = request.get("arguments");
132   
133   /* Error checking */
134   /* Setup */
135   if (request.get("method").isNull() )
136     method = "";
137   else
138     method = string( request.get("method").string() );
139   
140   if ( "gss_import_name" == method ) 
141   {
142     cmd = new GSSImportName ( &arguments );
143   } 
144   else if ( "gss_init_sec_context" == method ) 
145   {
146     cmd = new GSSInitSecContext ( &arguments );
147   } 
148   else if ( "gss_acquire_cred" == method ) 
149   {
150     cmd = new GSSAcquireCred ( &arguments );
151   } 
152   else if ( "gss_display_name" == method )
153   {
154     cmd = new GSSDisplayName ( &arguments );
155   }
156   else 
157   {
158     string error_message = string("Unrecognized command: ") + method;
159     response.set("error_message", error_message.c_str() );
160     response.set("original_message", requestString.c_str());
161     cmd = NULL;
162   }
163 }
164
165 string GSSRequest::getResponse()
166 {
167   /* Variables */
168   JSONObject *return_values;
169   string gssResponse;
170   
171   /* Main processing */
172   // Put the return values into the response, assuming that the command
173   // was actually executed.
174   if (NULL != cmd)
175   {
176     return_values = cmd->toJSON();
177     response.set("return_values", *return_values);
178   }
179   
180   // Convert the response into a string to return.
181   gssResponse = string( response.dump() );
182   
183   /* Return */
184   return(gssResponse);
185 }
186
187
188
189 char *gss_request(char *json_string)
190 {
191   /* Variables */
192   GSSRequest *req = new GSSRequest(string(json_string));
193   
194   /* Error checking */
195   // An empty json_string could be an error, but GSSRequest does
196   // a good job of handling it.
197   
198   /* Setup */
199   /* Main processing */
200   req->execute();
201   return strdup(req->getResponse().c_str());
202 }
203
204 void deallocate_reply(char *reply)
205 {
206   delete(reply);
207 }