87506fc41e73af0badcc3c59183a996074decfd6
[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_ctx_id_t *outGSSContext,
61                                  client_cb_fn clientCb,
62                                  void *clientCbData)
63 {
64     int err = 0;
65     OM_uint32 majorStatus;
66     OM_uint32 minorStatus = 0;
67     gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
68     gss_name_t clientName = GSS_C_NO_NAME;
69     gss_buffer_desc clientDisplayName = {0, NULL};
70     
71     char *inputTokenBuffer = NULL;
72     size_t inputTokenBufferLength = 0;
73     gss_buffer_desc inputToken;  /* buffer received from the server */
74     
75
76     if (inSocket <  0 ) { err = EINVAL; }
77     if (!outGSSContext) { err = EINVAL; }
78     
79     /* 
80      * The main authentication loop:
81      *
82      * GSS is a multimechanism API.  The number of packet exchanges required to  
83      * authenticatevaries between mechanisms.  As a result, we need to loop reading 
84      * input tokens from the client, calling gss_accept_sec_context on the input 
85      * tokens and send the resulting output tokens back to the client until we 
86      * get GSS_S_COMPLETE or an error.
87      *
88      * When we are done, save the client principal so we can make authorization 
89      * checks.
90      */
91     
92     majorStatus = GSS_S_CONTINUE_NEEDED;
93     while (!err && (majorStatus != GSS_S_COMPLETE)) {
94         /* Clean up old input buffer */
95         if (inputTokenBuffer != NULL) {
96             free (inputTokenBuffer);
97             inputTokenBuffer = NULL;  /* don't double-free */
98         }
99         
100         err = gsscon_read_token (inSocket, &inputTokenBuffer, &inputTokenBufferLength);
101         
102         if (!err) {
103             /* Set up input buffers for the next run through the loop */
104             inputToken.value = inputTokenBuffer;
105             inputToken.length = inputTokenBufferLength;
106         }
107         
108         if (!err) {
109             /* buffer to send to the server */
110             gss_buffer_desc outputToken = { 0, NULL }; 
111             
112             /*
113              * accept_sec_context does the actual work of taking the client's 
114              * request and generating an appropriate reply.  Note that we pass 
115              * GSS_C_NO_CREDENTIAL for the service principal.
116             */
117             // printf ("Calling gss_accept_sec_context...\n");
118             majorStatus = gss_accept_sec_context (&minorStatus, 
119                                                   &gssContext, 
120                                                   GSS_C_NO_CREDENTIAL, 
121                                                   &inputToken, 
122                                                   GSS_C_NO_CHANNEL_BINDINGS, 
123                                                   &clientName,
124                                                   NULL /* actual_mech_type */,
125                                                   &outputToken, 
126                                                   NULL /* req_flags */, 
127                                                   NULL /* time_rec */, 
128                                                   NULL /* delegated_cred_handle */);
129             
130             if ((outputToken.length > 0) && (outputToken.value != NULL)) {
131                 /* Send the output token to the client (even on error) */
132                 err = gsscon_write_token (inSocket, outputToken.value, outputToken.length);
133                 
134                 /* free the output token */
135                 gss_release_buffer (&minorStatus, &outputToken);
136             }
137         }
138         
139         if ((majorStatus != GSS_S_COMPLETE) && (majorStatus != GSS_S_CONTINUE_NEEDED)) {
140             gsscon_print_gss_errors ("gss_accept_sec_context", majorStatus, minorStatus);
141             err = minorStatus ? minorStatus : majorStatus; 
142         }            
143     }
144
145     if (!err) {
146       majorStatus = gss_display_name(&minorStatus, clientName, &clientDisplayName, NULL);
147       if (GSS_ERROR(majorStatus)) {
148         gsscon_print_gss_errors("gss_display_name", majorStatus, minorStatus);
149         err = EINVAL;
150       }
151       if (!err)
152         err = clientCb(clientName, &clientDisplayName, clientCbData);
153     }
154
155     if (!err) { 
156         *outGSSContext = gssContext;
157         gssContext = NULL;
158     } else {
159         gsscon_print_error (err, "Authenticate failed");
160     }
161     
162     if (inputTokenBuffer) { free (inputTokenBuffer); }
163     if (gssContext != GSS_C_NO_CONTEXT) { 
164         gss_delete_sec_context (&minorStatus, &gssContext, GSS_C_NO_BUFFER); }
165 if (clientName != GSS_C_NO_NAME)
166   gss_release_name(&minorStatus, &clientName);
167 if (clientDisplayName.value != NULL)
168   gss_release_buffer(&minorStatus, &clientDisplayName);
169         
170     return err;
171 }
172
173 /* --------------------------------------------------------------------------- */
174
175 static int ServicePrincipalIsValidForService (const char *inServicePrincipal)
176 {
177     int err = 0;
178     krb5_context context = NULL;
179     krb5_principal principal = NULL;
180     
181     if (!inServicePrincipal) { err = EINVAL; }
182     
183     if (!err) {
184         err = krb5_init_context (&context);
185     }
186     
187     if (!err) {
188         err = krb5_parse_name (context, inServicePrincipal, &principal);
189     }
190     
191     if (!err) {
192         /* 
193          * Here is where we check to see if the service principal the client 
194          * used is valid.  Typically we would just check that the first component 
195          * is the name of the service provided by the server.  This check exists
196          * to make sure the server is using the correct key in its keytab since
197          * we passed GSS_C_NO_CREDENTIAL into gss_accept_sec_context().
198          */
199         if (gServiceName && strcmp (gServiceName, 
200                                     krb5_princ_name (context, principal)->data) != 0) {
201             err = KRB5KRB_AP_WRONG_PRINC;
202         }
203     }
204     
205     if (principal) { krb5_free_principal (context, principal); }
206     if (context  ) { krb5_free_context (context); }
207     
208     return err;
209 }
210
211
212 /* --------------------------------------------------------------------------- */
213
214 static int ClientPrincipalIsAuthorizedForService (const char *inClientPrincipal)
215 {
216     int err = 0;
217         /* 
218          * Here is where the server checks to see if the client principal should 
219          * be allowed to use your service. Typically it should check both the name 
220          * and the realm, since with cross-realm shared keys, a user at another 
221          * realm may be trying to contact your service.  
222          */
223         err = 0;
224
225     
226     
227     return err;
228 }
229
230 /* --------------------------------------------------------------------------- */
231
232 int gsscon_authorize (gss_ctx_id_t  inContext, 
233                       int          *outAuthorized, 
234                       int          *outAuthorizationError)
235 {
236     int err = 0;
237     OM_uint32 majorStatus;
238     OM_uint32 minorStatus = 0;
239     gss_name_t clientName = NULL;
240     gss_name_t serviceName = NULL;
241     char *clientPrincipal = NULL;
242     char *servicePrincipal = NULL;
243
244     if (!inContext            ) { err = EINVAL; }
245     if (!outAuthorized        ) { err = EINVAL; }
246     if (!outAuthorizationError) { err = EINVAL; }
247     
248     if (!err) {
249         /* Get the client and service principals used to authenticate */
250         majorStatus = gss_inquire_context (&minorStatus, 
251                                            inContext, 
252                                            &clientName, 
253                                            &serviceName, 
254                                            NULL, NULL, NULL, NULL, NULL);
255         if (majorStatus != GSS_S_COMPLETE) { 
256             err = minorStatus ? minorStatus : majorStatus; 
257         }
258     }
259     
260     if (!err) {
261         /* Pull the client principal string out of the gss name */
262         gss_buffer_desc nameToken;
263         
264         majorStatus = gss_display_name (&minorStatus, 
265                                         clientName, 
266                                         &nameToken, 
267                                         NULL);
268         if (majorStatus != GSS_S_COMPLETE) { 
269             err = minorStatus ? minorStatus : majorStatus; 
270         }
271         
272         if (!err) {
273             clientPrincipal = malloc (nameToken.length + 1);
274             if (clientPrincipal == NULL) { err = ENOMEM; }
275         }
276         
277         if (!err) {
278             memcpy (clientPrincipal, nameToken.value, nameToken.length);
279             clientPrincipal[nameToken.length] = '\0';
280         }        
281
282         if (nameToken.value) { gss_release_buffer (&minorStatus, &nameToken); }
283     }
284     
285         if (!err) {
286     //    /* Pull the service principal string out of the gss name */
287     //    gss_buffer_desc nameToken;
288     //    
289     //    majorStatus = gss_display_name (&minorStatus, 
290     //                                    serviceName, 
291     //                                    &nameToken, 
292     //                                    NULL);
293     //    if (majorStatus != GSS_S_COMPLETE) { 
294     //        err = minorStatus ? minorStatus : majorStatus; 
295     //    }
296     //    
297     //    if (!err) {
298     //        servic7ePrincipal = malloc (nameToken.length + 1);
299     //        if (servicePrincipal == NULL) { err = ENOMEM; }
300     //    }
301     //    
302     //    if (!err) {
303     //        memcpy (servicePrincipal, nameToken.value, nameToken.length);
304     //        servicePrincipal[nameToken.length] = '\0';
305     //    }        
306
307     //    if (nameToken.value) { gss_release_buffer (&minorStatus, &nameToken); }
308     // }
309     
310 //    if (!err) {
311 //        int authorizationErr = ServicePrincipalIsValidForService (servicePr// incipal);
312 //        
313 //        if (!authorizationErr) {
314
315           int authorizationErr = 0;
316           authorizationErr = ClientPrincipalIsAuthorizedForService (clientPrincipal);
317
318 //        }
319         
320 //        printf ("'%s' is%s authorized for service '%s'\n", 
321 //                clientPrincipal, authorizationErr ? " NOT" : "", servicePrincipal);            
322 //        
323           *outAuthorized = !authorizationErr;
324           *outAuthorizationError = authorizationErr;
325         }
326     
327     if (serviceName     ) { gss_release_name (&minorStatus, &serviceName); }
328     if (clientName      ) { gss_release_name (&minorStatus, &clientName); }
329     if (clientPrincipal ) { free (clientPrincipal); }
330     if (servicePrincipal) { free (servicePrincipal); }
331
332     return err; 
333 }
334
335