1ede6d374e24e7a85627a1adedf9f0c09765aea0
[trust_router.git] / gsscon / gsscon_passive.c
1 /*
2  * Copyright (c) 2012, 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  * This code was adapted from the MIT Kerberos Consortium's
34  * GSS example code, which was distributed under the following
35  * license:
36  *
37  * Copyright 2004-2006 Massachusetts Institute of Technology.
38  * All Rights Reserved.
39  *
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.
53  */
54
55 #include <gsscon.h>
56
57 const char *gServiceName = NULL;
58
59 int gsscon_passive_authenticate (int                inSocket, 
60                                  gss_buffer_desc    inNameBuffer,
61                                  gss_ctx_id_t      *outGSSContext,
62                                  client_cb_fn       clientCb,
63                                  void              *clientCbData)
64 {
65     int err = 0;
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 */
75     
76     if (inSocket <  0 ) { err = EINVAL; }
77     if (!outGSSContext) { err = EINVAL; }
78
79     if (!err) {
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; 
84       }
85     }
86
87     if (!err) {
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; 
95       }
96     }
97
98     /* 
99      * The main authentication loop:
100      *
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.
106      *
107      * When we are done, save the client principal so we can make authorization 
108      * checks.
109      */
110     
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 */
117         }
118         
119         err = gsscon_read_token (inSocket, &inputTokenBuffer, &inputTokenBufferLength);
120         
121         if (!err) {
122             /* Set up input buffers for the next run through the loop */
123             inputToken.value = inputTokenBuffer;
124             inputToken.length = inputTokenBufferLength;
125         }
126         
127         if (!err) {
128             /* buffer to send to the server */
129             gss_buffer_desc outputToken = { 0, NULL }; 
130             
131             /*
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, 
135                                                   &gssContext, 
136                                                   acceptorCredentials,
137                                                   &inputToken, 
138                                                   GSS_C_NO_CHANNEL_BINDINGS, 
139                                                   &clientName,
140                                                   NULL /* actual_mech_type */,
141                                                   &outputToken, 
142                                                   NULL /* req_flags */, 
143                                                   NULL /* time_rec */, 
144                                                   NULL /* delegated_cred_handle */);
145             
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);
149                 
150                 /* free the output token */
151                 gss_release_buffer (&minorStatusToo, &outputToken);
152             }
153         }
154         
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; 
158         }            
159     }
160
161     if (!err) {
162       majorStatus = gss_display_name(&minorStatus, clientName, &clientDisplayName, NULL);
163       if (GSS_ERROR(majorStatus)) {
164         gsscon_print_gss_errors("gss_display_name", majorStatus, minorStatus);
165         err = EINVAL;
166       }
167       if (!err)
168         err = clientCb(clientName, &clientDisplayName, clientCbData);
169     }
170
171     if (!err) { 
172         *outGSSContext = gssContext;
173         gssContext = NULL;
174     } else {
175         gsscon_print_error (err, "Authenticate failed");
176     }
177     
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);
187         
188     return err;
189 }
190
191     
192
193
194 /* --------------------------------------------------------------------------- */
195
196 static int ClientPrincipalIsAuthorizedForService (const char *inClientPrincipal)
197 {
198     int err = 0;
199         /* 
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.  
204          */
205         err = 0;
206
207     
208     
209     return err;
210 }
211
212 /* --------------------------------------------------------------------------- */
213
214 int gsscon_authorize (gss_ctx_id_t  inContext, 
215                       int          *outAuthorized, 
216                       int          *outAuthorizationError)
217 {
218     int err = 0;
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;
225
226     if (!inContext            ) { err = EINVAL; }
227     if (!outAuthorized        ) { err = EINVAL; }
228     if (!outAuthorizationError) { err = EINVAL; }
229     
230     if (!err) {
231         /* Get the client and service principals used to authenticate */
232         majorStatus = gss_inquire_context (&minorStatus, 
233                                            inContext, 
234                                            &clientName, 
235                                            &serviceName, 
236                                            NULL, NULL, NULL, NULL, NULL);
237         if (majorStatus != GSS_S_COMPLETE) { 
238             err = minorStatus ? minorStatus : majorStatus; 
239         }
240     }
241     
242     if (!err) {
243         /* Pull the client principal string out of the gss name */
244         gss_buffer_desc nameToken;
245         
246         majorStatus = gss_display_name (&minorStatus, 
247                                         clientName, 
248                                         &nameToken, 
249                                         NULL);
250         if (majorStatus != GSS_S_COMPLETE) { 
251             err = minorStatus ? minorStatus : majorStatus; 
252         }
253         
254         if (!err) {
255             clientPrincipal = malloc (nameToken.length + 1);
256             if (clientPrincipal == NULL) { err = ENOMEM; }
257         }
258         
259         if (!err) {
260             memcpy (clientPrincipal, nameToken.value, nameToken.length);
261             clientPrincipal[nameToken.length] = '\0';
262         }        
263
264         if (nameToken.value) { gss_release_buffer (&minorStatus, &nameToken); }
265     }
266     
267         if (!err) {
268     //    /* Pull the service principal string out of the gss name */
269     //    gss_buffer_desc nameToken;
270     //    
271     //    majorStatus = gss_display_name (&minorStatus, 
272     //                                    serviceName, 
273     //                                    &nameToken, 
274     //                                    NULL);
275     //    if (majorStatus != GSS_S_COMPLETE) { 
276     //        err = minorStatus ? minorStatus : majorStatus; 
277     //    }
278     //    
279     //    if (!err) {
280     //        servic7ePrincipal = malloc (nameToken.length + 1);
281     //        if (servicePrincipal == NULL) { err = ENOMEM; }
282     //    }
283     //    
284     //    if (!err) {
285     //        memcpy (servicePrincipal, nameToken.value, nameToken.length);
286     //        servicePrincipal[nameToken.length] = '\0';
287     //    }        
288
289     //    if (nameToken.value) { gss_release_buffer (&minorStatus, &nameToken); }
290     // }
291     
292
293           int authorizationErr = 0;
294           authorizationErr = ClientPrincipalIsAuthorizedForService (clientPrincipal);
295
296
297         
298 //        printf ("'%s' is%s authorized for service '%s'\n", 
299 //                clientPrincipal, authorizationErr ? " NOT" : "", servicePrincipal);            
300 //        
301           *outAuthorized = !authorizationErr;
302           *outAuthorizationError = authorizationErr;
303         }
304     
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); }
309
310     return err; 
311 }
312
313