]> andersk Git - gssapi-openssh.git/blob - openssh/gss-serv.c
fix typo on last checkin
[gssapi-openssh.git] / openssh / gss-serv.c
1 /*      $OpenBSD: gss-serv.c,v 1.5 2003/11/17 11:06:07 markus Exp $     */
2
3 /*
4  * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "includes.h"
28
29 #ifdef GSSAPI
30
31 #include "buffer.h"
32 #include "bufaux.h"
33 #include "compat.h"
34 #include <openssl/evp.h>
35 #include "kex.h"
36 #include "auth.h"
37 #include "log.h"
38 #include "channels.h"
39 #include "session.h"
40 #include "servconf.h"
41 #include "monitor_wrap.h"
42 #include "xmalloc.h"
43 #include "getput.h"
44
45 #include "ssh-gss.h"
46
47 extern ServerOptions options;
48 extern u_char *session_id2;
49 extern int session_id2_len;
50
51 static ssh_gssapi_client gssapi_client =
52     { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
53     GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL}};
54
55 ssh_gssapi_mech gssapi_null_mech =
56     { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
57
58 #ifdef KRB5
59 extern ssh_gssapi_mech gssapi_kerberos_mech;
60 #endif
61 #ifdef GSI
62 extern ssh_gssapi_mech gssapi_gsi_mech;
63 #endif
64
65 ssh_gssapi_mech* supported_mechs[]= {
66 #ifdef KRB5
67         &gssapi_kerberos_mech,
68 #endif
69 #ifdef GSI
70         &gssapi_gsi_mech,
71 #endif
72         &gssapi_null_mech,
73 };
74
75 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
76 static int limited = 0;
77 #endif
78
79 /* Unpriviledged */
80 void
81 ssh_gssapi_supported_oids(gss_OID_set *oidset)
82 {
83         int i = 0;
84         OM_uint32 min_status;
85         int present;
86         gss_OID_set supported;
87
88         gss_create_empty_oid_set(&min_status, oidset);
89         /* Ask priviledged process what mechanisms it supports. */
90         PRIVSEP(gss_indicate_mechs(&min_status, &supported));
91
92         while (supported_mechs[i]->name != NULL) {
93                 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
94                     &supported_mechs[i]->oid, supported, &present)))
95                         present = 0;
96                 if (present)
97                         gss_add_oid_set_member(&min_status,
98                             &supported_mechs[i]->oid, oidset);
99                 i++;
100         }
101 }
102
103
104 /* Wrapper around accept_sec_context
105  * Requires that the context contains:
106  *    oid
107  *    credentials       (from ssh_gssapi_acquire_cred)
108  */
109 /* Priviledged */
110 OM_uint32
111 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
112     gss_buffer_desc *send_tok, OM_uint32 *flags)
113 {
114         OM_uint32 status;
115         gss_OID mech;
116
117         ctx->major = gss_accept_sec_context(&ctx->minor,
118             &ctx->context, ctx->creds, recv_tok,
119             GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
120             send_tok, flags, NULL, &ctx->client_creds);
121
122         if (GSS_ERROR(ctx->major))
123                 ssh_gssapi_error(ctx);
124
125         if (ctx->client_creds)
126                 debug("Received some client credentials");
127         else
128                 debug("Got no client credentials");
129
130         status = ctx->major;
131
132         /* Now, if we're complete and we have the right flags, then
133          * we flag the user as also having been authenticated
134          */
135
136         if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
137             (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
138                 if (ssh_gssapi_getclient(ctx, &gssapi_client))
139                         fatal("Couldn't convert client name");
140 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
141                 if (flags && (*flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG))
142                         limited=1;
143 #endif
144         }
145
146         return (status);
147 }
148
149 /*
150  * This parses an exported name, extracting the mechanism specific portion
151  * to use for ACL checking. It verifies that the name belongs the mechanism
152  * originally selected.
153  */
154 static OM_uint32
155 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
156 {
157         char *tok;
158         OM_uint32 offset;
159         OM_uint32 oidl;
160
161         tok=ename->value;
162
163 #ifdef GSI /* GSI gss_export_name() is broken. */
164         if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
165             (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
166                     gssapi_gsi_mech.oid.length) == 0)) {
167             name->length = ename->length;
168             name->value = xmalloc(ename->length+1);
169             memcpy(name->value, ename->value, ename->length);
170             return GSS_S_COMPLETE;
171         }
172 #endif
173
174         /*
175          * Check that ename is long enough for all of the fixed length
176          * header, and that the initial ID bytes are correct
177          */
178
179         if (ename->length<6 || memcmp(tok,"\x04\x01", 2)!=0)
180                 return GSS_S_FAILURE;
181
182         /*
183          * Extract the OID, and check it. Here GSSAPI breaks with tradition
184          * and does use the OID type and length bytes. To confuse things
185          * there are two lengths - the first including these, and the
186          * second without.
187          */
188
189         oidl = GET_16BIT(tok+2); /* length including next two bytes */
190         oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
191
192         /*
193          * Check the BER encoding for correct type and length, that the
194          * string is long enough and that the OID matches that in our context
195          */
196         if (tok[4] != 0x06 || tok[5] != oidl ||
197             ename->length < oidl+6 ||
198            !ssh_gssapi_check_oid(ctx,tok+6,oidl))
199                 return GSS_S_FAILURE;
200
201         offset = oidl+6;
202
203         if (ename->length < offset+4)
204                 return GSS_S_FAILURE;
205
206         name->length = GET_32BIT(tok+offset);
207         offset += 4;
208
209         if (ename->length < offset+name->length)
210                 return GSS_S_FAILURE;
211
212         name->value = xmalloc(name->length+1);
213         memcpy(name->value,tok+offset,name->length);
214         ((char *)name->value)[name->length] = 0;
215
216         return GSS_S_COMPLETE;
217 }
218
219 /* Extract the client details from a given context. This can only reliably
220  * be called once for a context */
221
222 /* Priviledged (called from accept_secure_ctx) */
223 OM_uint32
224 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
225 {
226         int i = 0;
227
228         gss_buffer_desc ename;
229
230         client->mech = NULL;
231
232         while (supported_mechs[i]->name != NULL) {
233                 if (supported_mechs[i]->oid.length == ctx->oid->length &&
234                     (memcmp(supported_mechs[i]->oid.elements,
235                     ctx->oid->elements, ctx->oid->length) == 0))
236                         client->mech = supported_mechs[i];
237                 i++;
238         }
239
240         if (client->mech == NULL)
241                 return GSS_S_FAILURE;
242
243         if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
244             &client->displayname, NULL))) {
245                 ssh_gssapi_error(ctx);
246                 return (ctx->major);
247         }
248
249         if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
250             &ename))) {
251                 ssh_gssapi_error(ctx);
252                 return (ctx->major);
253         }
254
255         if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
256             &client->exportedname))) {
257                 return (ctx->major);
258         }
259
260         /* We can't copy this structure, so we just move the pointer to it */
261         client->creds = ctx->client_creds;
262         ctx->client_creds = GSS_C_NO_CREDENTIAL;
263         return (ctx->major);
264 }
265
266 /* As user - called on fatal/exit */
267 void
268 ssh_gssapi_cleanup_creds(void)
269 {
270         if (gssapi_client.store.filename != NULL) {
271                 /* Unlink probably isn't sufficient */
272                 debug("removing gssapi cred file\"%s\"", gssapi_client.store.filename);
273                 unlink(gssapi_client.store.filename);
274         }
275 }
276
277 /* As user */
278 void
279 ssh_gssapi_storecreds(void)
280 {
281         if (gssapi_client.mech && gssapi_client.mech->storecreds) {
282                 (*gssapi_client.mech->storecreds)(&gssapi_client);
283         } else
284                 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
285 }
286
287 /* This allows GSSAPI methods to do things to the childs environment based
288  * on the passed authentication process and credentials.
289  */
290 /* As user */
291 void
292 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
293 {
294
295         if (gssapi_client.store.envvar != NULL &&
296             gssapi_client.store.envval != NULL) {
297
298                 debug("Setting %s to %s", gssapi_client.store.envvar,
299                 gssapi_client.store.envval);
300                 child_set_env(envp, envsizep, gssapi_client.store.envvar,
301                      gssapi_client.store.envval);
302         }
303 }
304
305 /* Priviledged */
306 int
307 ssh_gssapi_userok(char *user)
308 {
309         if (gssapi_client.exportedname.length == 0 ||
310             gssapi_client.exportedname.value == NULL) {
311                 debug("No suitable client data");
312                 return 0;
313         }
314 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
315         if (limited) {
316                 debug("limited proxy not acceptable for remote login");
317                 return 0;
318         }
319 #endif
320         if (gssapi_client.mech && gssapi_client.mech->userok)
321                 return ((*gssapi_client.mech->userok)(&gssapi_client, user));
322         else
323                 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
324         return (0);
325 }
326
327 /* Return a list of the gss-group1-sha1-x mechanisms supported by this
328  * program.
329  *
330  * We only support the mechanisms that we've indicated in the list above,
331  * but we check that they're supported by the GSSAPI mechanism on the 
332  * machine. We also check, before including them in the list, that
333  * we have the necesary information in order to carry out the key exchange
334  * (that is, that the user has credentials, the server's creds are accessible,
335  * etc)
336  *
337  * The way that this is done is fairly nasty, as we do a lot of work that
338  * is then thrown away. This should possibly be implemented with a cache
339  * that stores the results (in an expanded Gssctxt structure), which are
340  * then used by the first calls if that key exchange mechanism is chosen.
341  */
342
343 /* Unpriviledged */ 
344 char * 
345 ssh_gssapi_server_mechanisms() {
346         gss_OID_set     supported;
347         Gssctxt         *ctx = NULL;
348         OM_uint32       maj_status, min_status;
349         Buffer          buf;
350         int             i = 0;
351         int             first = 0;
352         int             present;
353         char *          mechs;
354
355         ssh_gssapi_supported_oids(&supported);
356         
357         buffer_init(&buf);
358
359         while(supported_mechs[i]->name != NULL) {
360                 if ((maj_status=gss_test_oid_set_member(&min_status,
361                                                         &supported_mechs[i]->oid,
362                                                         supported,
363                                                         &present))) {
364                         present=0;
365                 }
366
367                 if (present) {
368                     if (!GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx,
369                                            &supported_mechs[i]->oid)))) {
370                         /* Append gss_group1_sha1_x to our list */
371                         if (first++!=0)
372                                 buffer_put_char(&buf,',');
373                         buffer_append(&buf, KEX_GSS_SHA1,
374                                       sizeof(KEX_GSS_SHA1)-1);
375                         buffer_append(&buf, 
376                                       supported_mechs[i]->enc_name,
377                                       strlen(supported_mechs[i]->enc_name));
378                         debug("GSSAPI mechanism %s (%s%s) supported",
379                               supported_mechs[i]->name, KEX_GSS_SHA1,
380                               supported_mechs[i]->enc_name);
381                     } else {
382                         debug("no credentials for GSSAPI mechanism %s",
383                               supported_mechs[i]->name);
384                     }
385                 } else {
386                     debug("GSSAPI mechanism %s not supported",
387                           supported_mechs[i]->name);
388                 }
389                 ssh_gssapi_delete_ctx(&ctx);
390                 i++;
391         }
392         
393         buffer_put_char(&buf,'\0');
394         
395         mechs=xmalloc(buffer_len(&buf));
396         buffer_get(&buf,mechs,buffer_len(&buf));
397         buffer_free(&buf);
398         if (strlen(mechs)==0) {
399             options.gss_authentication = 0; /* no mechs. skip gss auth. */
400             return(NULL);
401         } else {
402             return(mechs);
403         }
404 }
405
406 /* Return the OID that corresponds to the given context name */
407  
408 /* Unpriviledged */
409 gss_OID 
410 ssh_gssapi_server_id_kex(char *name) {
411   int i=0;
412   
413   if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) {
414      return(NULL);
415   }
416   
417   name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the MIME string */
418   
419   while (supported_mechs[i]->name!=NULL &&
420          strcmp(name,supported_mechs[i]->enc_name)!=0) {
421         i++;
422   }
423
424   if (supported_mechs[i]->name==NULL)
425      return (NULL);
426
427   debug("using GSSAPI mechanism %s (%s%s)", supported_mechs[i]->name,
428         KEX_GSS_SHA1, supported_mechs[i]->enc_name);
429
430   return &supported_mechs[i]->oid;
431 }
432
433 /* Priviledged */
434 int
435 ssh_gssapi_localname(char **user)
436 {
437         *user = NULL;
438         if (gssapi_client.displayname.length==0 || 
439             gssapi_client.displayname.value==NULL) {
440                 debug("No suitable client data");
441                 return(0);;
442         }
443         if (gssapi_client.mech && gssapi_client.mech->localname) {
444                 return((*gssapi_client.mech->localname)(&gssapi_client,user));
445         } else {
446                 debug("Unknown client authentication type");
447         }
448         return(0);
449 }
450
451 /* Priviledged */
452 OM_uint32
453 ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
454 {
455         ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
456             gssbuf, gssmic, NULL);
457
458         return (ctx->major);
459 }
460
461 #endif
This page took 0.073648 seconds and 5 git commands to generate.