]> andersk Git - gssapi-openssh.git/blob - openssh/gss-serv.c
don't need to include compat.h twice
[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 "ssh.h"
32 #include "ssh2.h"
33 #include "buffer.h"
34 #include "bufaux.h"
35 #include "packet.h"
36 #include "compat.h"
37 #include <openssl/evp.h>
38 #include "cipher.h"
39 #include "kex.h"
40 #include "auth.h"
41 #include "log.h"
42 #include "channels.h"
43 #include "session.h"
44 #include "dispatch.h"
45 #include "servconf.h"
46 #include "monitor_wrap.h"
47 #include "xmalloc.h"
48 #include "getput.h"
49
50 #include "ssh-gss.h"
51
52 extern ServerOptions options;
53 extern u_char *session_id2;
54 extern int session_id2_len;
55
56 static ssh_gssapi_client gssapi_client =
57     { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
58     GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL}};
59
60 ssh_gssapi_mech gssapi_null_mech =
61     { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
62
63 #ifdef KRB5
64 extern ssh_gssapi_mech gssapi_kerberos_mech;
65 extern ssh_gssapi_mech gssapi_kerberos_mech_old;
66 #endif
67 #ifdef GSI
68 extern ssh_gssapi_mech gssapi_gsi_mech;
69 extern ssh_gssapi_mech gssapi_gsi_mech_old;
70 #endif
71
72 ssh_gssapi_mech* supported_mechs[]= {
73 #ifdef KRB5
74         &gssapi_kerberos_mech,
75         &gssapi_kerberos_mech_old, /* Support for legacy clients */
76 #endif
77 #ifdef GSI
78         &gssapi_gsi_mech,
79         &gssapi_gsi_mech_old,   /* Support for legacy clients */
80 #endif
81         &gssapi_null_mech,
82 };
83
84 /* Unpriviledged */
85 void
86 ssh_gssapi_supported_oids(gss_OID_set *oidset)
87 {
88         int i = 0;
89         OM_uint32 min_status;
90         int present;
91         gss_OID_set supported;
92
93         gss_create_empty_oid_set(&min_status, oidset);
94         gss_indicate_mechs(&min_status, &supported);
95
96         while (supported_mechs[i]->name != NULL) {
97                 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
98                     &supported_mechs[i]->oid, supported, &present)))
99                         present = 0;
100                 if (present)
101                         gss_add_oid_set_member(&min_status,
102                             &supported_mechs[i]->oid, oidset);
103                 i++;
104         }
105 }
106
107
108 /* Wrapper around accept_sec_context
109  * Requires that the context contains:
110  *    oid
111  *    credentials       (from ssh_gssapi_acquire_cred)
112  */
113 /* Priviledged */
114 OM_uint32
115 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
116     gss_buffer_desc *send_tok, OM_uint32 *flags)
117 {
118         OM_uint32 status;
119         gss_OID mech;
120
121         ctx->major = gss_accept_sec_context(&ctx->minor,
122             &ctx->context, ctx->creds, recv_tok,
123             GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
124             send_tok, flags, NULL, &ctx->client_creds);
125
126         if (GSS_ERROR(ctx->major))
127                 ssh_gssapi_error(ctx);
128
129         if (ctx->client_creds)
130                 debug("Received some client credentials");
131         else
132                 debug("Got no client credentials");
133
134         status = ctx->major;
135
136         /* Now, if we're complete and we have the right flags, then
137          * we flag the user as also having been authenticated
138          */
139
140         if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
141             (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
142                 if (ssh_gssapi_getclient(ctx, &gssapi_client))
143                         fatal("Couldn't convert client name");
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         if (gssapi_client.mech && gssapi_client.mech->userok)
315                 return ((*gssapi_client.mech->userok)(&gssapi_client, user));
316         else
317                 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
318         return (0);
319 }
320
321 /* Return a list of the gss-group1-sha1-x mechanisms supported by this
322  * program.
323  *
324  * We only support the mechanisms that we've indicated in the list above,
325  * but we check that they're supported by the GSSAPI mechanism on the 
326  * machine. We also check, before including them in the list, that
327  * we have the necesary information in order to carry out the key exchange
328  * (that is, that the user has credentials, the server's creds are accessible,
329  * etc)
330  *
331  * The way that this is done is fairly nasty, as we do a lot of work that
332  * is then thrown away. This should possibly be implemented with a cache
333  * that stores the results (in an expanded Gssctxt structure), which are
334  * then used by the first calls if that key exchange mechanism is chosen.
335  */
336
337 /* Unpriviledged */ 
338 char * 
339 ssh_gssapi_server_mechanisms() {
340         gss_OID_set     supported;
341         Gssctxt         *ctx = NULL;
342         OM_uint32       maj_status, min_status;
343         Buffer          buf;
344         int             i = 0;
345         int             first = 0;
346         int             present;
347         char *          mechs;
348
349         if (datafellows & SSH_OLD_GSSAPI) return NULL;
350         
351         ssh_gssapi_supported_oids(&supported);
352         
353         buffer_init(&buf);
354
355         while(supported_mechs[i]->name != NULL) {
356                 if ((maj_status=gss_test_oid_set_member(&min_status,
357                                                         &supported_mechs[i]->oid,
358                                                         supported,
359                                                         &present))) {
360                         present=0;
361                 }
362
363                 if (present) {
364                     if (!GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx,
365                                            &supported_mechs[i]->oid)))) {
366                         /* Append gss_group1_sha1_x to our list */
367                         if (first++!=0)
368                                 buffer_put_char(&buf,',');
369                         buffer_append(&buf, KEX_GSS_SHA1,
370                                       sizeof(KEX_GSS_SHA1)-1);
371                         buffer_append(&buf, 
372                                       supported_mechs[i]->enc_name,
373                                       strlen(supported_mechs[i]->enc_name));
374                         debug("GSSAPI mechanism %s (%s%s) supported",
375                               supported_mechs[i]->name, KEX_GSS_SHA1,
376                               supported_mechs[i]->enc_name);
377                     } else {
378                         debug("no credentials for GSSAPI mechanism %s",
379                               supported_mechs[i]->name);
380                     }
381                 } else {
382                     debug("GSSAPI mechanism %s not supported",
383                           supported_mechs[i]->name);
384                 }
385                 ssh_gssapi_delete_ctx(&ctx);
386                 i++;
387         }
388         
389         buffer_put_char(&buf,'\0');
390         
391         mechs=xmalloc(buffer_len(&buf));
392         buffer_get(&buf,mechs,buffer_len(&buf));
393         buffer_free(&buf);
394         if (strlen(mechs)==0)
395            return(NULL);
396         else
397            return(mechs);
398 }
399
400 /* Return the OID that corresponds to the given context name */
401  
402 /* Unpriviledged */
403 gss_OID 
404 ssh_gssapi_server_id_kex(char *name) {
405   int i=0;
406   
407   if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) {
408      return(NULL);
409   }
410   
411   name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the MIME string */
412   
413   while (supported_mechs[i]->name!=NULL &&
414          strcmp(name,supported_mechs[i]->enc_name)!=0) {
415         i++;
416   }
417
418   if (supported_mechs[i]->name==NULL)
419      return (NULL);
420
421   debug("using GSSAPI mechanism %s (%s%s)", supported_mechs[i]->name,
422         KEX_GSS_SHA1, supported_mechs[i]->enc_name);
423
424   return &supported_mechs[i]->oid;
425 }
426
427 /* Priviledged */
428 int
429 ssh_gssapi_localname(char **user)
430 {
431         *user = NULL;
432         if (gssapi_client.displayname.length==0 || 
433             gssapi_client.displayname.value==NULL) {
434                 debug("No suitable client data");
435                 return(0);;
436         }
437         if (gssapi_client.mech && gssapi_client.mech->localname) {
438                 return((*gssapi_client.mech->localname)(&gssapi_client,user));
439         } else {
440                 debug("Unknown client authentication type");
441         }
442         return(0);
443 }
444
445 /* Priviledged */
446 OM_uint32
447 ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
448 {
449         ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
450             gssbuf, gssmic, NULL);
451
452         return (ctx->major);
453 }
454
455 #endif
This page took 0.067298 seconds and 5 git commands to generate.