2 * Copyright (c) 2012, 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.
33 * This code was adapted from the MIT Kerberos Consortium's
34 * GSS example code, which was distributed under the following
37 * Copyright 2004-2006 Massachusetts Institute of Technology.
38 * All Rights Reserved.
40 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
41 * distribute this software and its documentation for any purpose and
42 * without fee is hereby granted, provided that the above copyright
43 * notice appear in all copies and that both that copyright notice and
44 * this permission notice appear in supporting documentation, and that
45 * the name of M.I.T. not be used in advertising or publicity pertaining
46 * to distribution of the software without specific, written prior
47 * permission. Furthermore if you modify this software you must label
48 * your software as modified software and not distribute it in such a
49 * fashion that it might be confused with the original M.I.T. software.
50 * M.I.T. makes no representations about the suitability of
51 * this software for any purpose. It is provided "as is" without express
52 * or implied warranty.
57 const char *gServiceName = NULL;
59 int gsscon_passive_authenticate (int inSocket,
60 gss_buffer_desc inNameBuffer,
61 gss_ctx_id_t *outGSSContext,
62 client_cb_fn clientCb,
66 OM_uint32 majorStatus;
67 OM_uint32 minorStatus = 0, minorStatusToo = 0;
68 gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
69 gss_name_t clientName = GSS_C_NO_NAME, serviceName = GSS_C_NO_NAME;
70 gss_cred_id_t acceptorCredentials = NULL;
71 gss_buffer_desc clientDisplayName = {0, NULL};
72 char *inputTokenBuffer = NULL;
73 size_t inputTokenBufferLength = 0;
74 gss_buffer_desc inputToken; /* buffer received from the server */
76 if (inSocket < 0 ) { err = EINVAL; }
77 if (!outGSSContext) { err = EINVAL; }
80 majorStatus = gss_import_name (&minorStatus, &inNameBuffer, (gss_OID) GSS_C_NT_HOSTBASED_SERVICE, &serviceName);
81 if (majorStatus != GSS_S_COMPLETE) {
82 gsscon_print_gss_errors ("gss_import_name(serviceName)", majorStatus, minorStatus);
83 err = minorStatus ? minorStatus : majorStatus;
88 majorStatus = gss_acquire_cred ( &minorStatus, serviceName,
89 GSS_C_INDEFINITE, GSS_C_NO_OID_SET,
90 GSS_C_ACCEPT, &acceptorCredentials,
91 NULL /*mechs out*/, NULL /*time out*/);
92 if (majorStatus != GSS_S_COMPLETE) {
93 gsscon_print_gss_errors ("gss_acquire_cred", majorStatus, minorStatus);
94 err = minorStatus ? minorStatus : majorStatus;
99 * The main authentication loop:
101 * GSS is a multimechanism API. The number of packet exchanges required to
102 * authenticatevaries between mechanisms. As a result, we need to loop reading
103 * input tokens from the client, calling gss_accept_sec_context on the input
104 * tokens and send the resulting output tokens back to the client until we
105 * get GSS_S_COMPLETE or an error.
107 * When we are done, save the client principal so we can make authorization
111 majorStatus = GSS_S_CONTINUE_NEEDED;
112 while (!err && (majorStatus != GSS_S_COMPLETE)) {
113 /* Clean up old input buffer */
114 if (inputTokenBuffer != NULL) {
115 free (inputTokenBuffer);
116 inputTokenBuffer = NULL; /* don't double-free */
119 err = gsscon_read_token (inSocket, &inputTokenBuffer, &inputTokenBufferLength);
122 /* Set up input buffers for the next run through the loop */
123 inputToken.value = inputTokenBuffer;
124 inputToken.length = inputTokenBufferLength;
128 /* buffer to send to the server */
129 gss_buffer_desc outputToken = { 0, NULL };
132 * accept_sec_context does the actual work of taking the client's
133 * request and generating an appropriate reply. */
134 majorStatus = gss_accept_sec_context (&minorStatus,
138 GSS_C_NO_CHANNEL_BINDINGS,
140 NULL /* actual_mech_type */,
142 NULL /* req_flags */,
144 NULL /* delegated_cred_handle */);
146 if ((outputToken.length > 0) && (outputToken.value != NULL)) {
147 /* Send the output token to the client (even on error) */
148 err = gsscon_write_token (inSocket, outputToken.value, outputToken.length);
150 /* free the output token */
151 gss_release_buffer (&minorStatusToo, &outputToken);
155 if ((majorStatus != GSS_S_COMPLETE) && (majorStatus != GSS_S_CONTINUE_NEEDED)) {
156 gsscon_print_gss_errors ("gss_accept_sec_context", majorStatus, minorStatus);
157 err = minorStatus ? minorStatus : majorStatus;
162 majorStatus = gss_display_name(&minorStatus, clientName, &clientDisplayName, NULL);
163 if (GSS_ERROR(majorStatus)) {
164 gsscon_print_gss_errors("gss_display_name", majorStatus, minorStatus);
168 err = clientCb(clientName, &clientDisplayName, clientCbData);
172 *outGSSContext = gssContext;
175 gsscon_print_error (err, "Authenticate failed");
178 if (inputTokenBuffer) { free (inputTokenBuffer); }
179 if (gssContext != GSS_C_NO_CONTEXT) {
180 gss_delete_sec_context (&minorStatus, &gssContext, GSS_C_NO_BUFFER); }
181 if (clientName != GSS_C_NO_NAME)
182 gss_release_name(&minorStatus, &clientName);
183 if (clientDisplayName.value != NULL)
184 gss_release_buffer(&minorStatus, &clientDisplayName);
185 gss_release_name( &minorStatus, &serviceName);
186 gss_release_cred( &minorStatus, &acceptorCredentials);
194 /* --------------------------------------------------------------------------- */
196 static int ClientPrincipalIsAuthorizedForService (const char *inClientPrincipal)
200 * Here is where the server checks to see if the client principal should
201 * be allowed to use your service. Typically it should check both the name
202 * and the realm, since with cross-realm shared keys, a user at another
203 * realm may be trying to contact your service.
212 /* --------------------------------------------------------------------------- */
214 int gsscon_authorize (gss_ctx_id_t inContext,
216 int *outAuthorizationError)
219 OM_uint32 majorStatus;
220 OM_uint32 minorStatus = 0;
221 gss_name_t clientName = NULL;
222 gss_name_t serviceName = NULL;
223 char *clientPrincipal = NULL;
224 char *servicePrincipal = NULL;
226 if (!inContext ) { err = EINVAL; }
227 if (!outAuthorized ) { err = EINVAL; }
228 if (!outAuthorizationError) { err = EINVAL; }
231 /* Get the client and service principals used to authenticate */
232 majorStatus = gss_inquire_context (&minorStatus,
236 NULL, NULL, NULL, NULL, NULL);
237 if (majorStatus != GSS_S_COMPLETE) {
238 err = minorStatus ? minorStatus : majorStatus;
243 /* Pull the client principal string out of the gss name */
244 gss_buffer_desc nameToken;
246 majorStatus = gss_display_name (&minorStatus,
250 if (majorStatus != GSS_S_COMPLETE) {
251 err = minorStatus ? minorStatus : majorStatus;
255 clientPrincipal = malloc (nameToken.length + 1);
256 if (clientPrincipal == NULL) { err = ENOMEM; }
260 memcpy (clientPrincipal, nameToken.value, nameToken.length);
261 clientPrincipal[nameToken.length] = '\0';
264 if (nameToken.value) { gss_release_buffer (&minorStatus, &nameToken); }
268 // /* Pull the service principal string out of the gss name */
269 // gss_buffer_desc nameToken;
271 // majorStatus = gss_display_name (&minorStatus,
275 // if (majorStatus != GSS_S_COMPLETE) {
276 // err = minorStatus ? minorStatus : majorStatus;
280 // servic7ePrincipal = malloc (nameToken.length + 1);
281 // if (servicePrincipal == NULL) { err = ENOMEM; }
285 // memcpy (servicePrincipal, nameToken.value, nameToken.length);
286 // servicePrincipal[nameToken.length] = '\0';
289 // if (nameToken.value) { gss_release_buffer (&minorStatus, &nameToken); }
293 int authorizationErr = 0;
294 authorizationErr = ClientPrincipalIsAuthorizedForService (clientPrincipal);
298 // printf ("'%s' is%s authorized for service '%s'\n",
299 // clientPrincipal, authorizationErr ? " NOT" : "", servicePrincipal);
301 *outAuthorized = !authorizationErr;
302 *outAuthorizationError = authorizationErr;
305 if (serviceName ) { gss_release_name (&minorStatus, &serviceName); }
306 if (clientName ) { gss_release_name (&minorStatus, &clientName); }
307 if (clientPrincipal ) { free (clientPrincipal); }
308 if (servicePrincipal) { free (servicePrincipal); }