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