]> andersk Git - gssapi-openssh.git/blob - openssh/gss-serv.c
remove superfluous #includes
[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 extern ssh_gssapi_mech gssapi_kerberos_mech_old;
61 #endif
62 #ifdef GSI
63 extern ssh_gssapi_mech gssapi_gsi_mech;
64 extern ssh_gssapi_mech gssapi_gsi_mech_old;
65 #endif
66
67 ssh_gssapi_mech* supported_mechs[]= {
68 #ifdef KRB5
69         &gssapi_kerberos_mech,
70         &gssapi_kerberos_mech_old, /* Support for legacy clients */
71 #endif
72 #ifdef GSI
73         &gssapi_gsi_mech,
74         &gssapi_gsi_mech_old,   /* Support for legacy clients */
75 #endif
76         &gssapi_null_mech,
77 };
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         gss_indicate_mechs(&min_status, &supported);
90
91         while (supported_mechs[i]->name != NULL) {
92                 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
93                     &supported_mechs[i]->oid, supported, &present)))
94                         present = 0;
95                 if (present)
96                         gss_add_oid_set_member(&min_status,
97                             &supported_mechs[i]->oid, oidset);
98                 i++;
99         }
100 }
101
102
103 /* Wrapper around accept_sec_context
104  * Requires that the context contains:
105  *    oid
106  *    credentials       (from ssh_gssapi_acquire_cred)
107  */
108 /* Priviledged */
109 OM_uint32
110 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
111     gss_buffer_desc *send_tok, OM_uint32 *flags)
112 {
113         OM_uint32 status;
114         gss_OID mech;
115
116         ctx->major = gss_accept_sec_context(&ctx->minor,
117             &ctx->context, ctx->creds, recv_tok,
118             GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
119             send_tok, flags, NULL, &ctx->client_creds);
120
121         if (GSS_ERROR(ctx->major))
122                 ssh_gssapi_error(ctx);
123
124         if (ctx->client_creds)
125                 debug("Received some client credentials");
126         else
127                 debug("Got no client credentials");
128
129         status = ctx->major;
130
131         /* Now, if we're complete and we have the right flags, then
132          * we flag the user as also having been authenticated
133          */
134
135         if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
136             (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
137                 if (ssh_gssapi_getclient(ctx, &gssapi_client))
138                         fatal("Couldn't convert client name");
139         }
140
141         return (status);
142 }
143
144 /*
145  * This parses an exported name, extracting the mechanism specific portion
146  * to use for ACL checking. It verifies that the name belongs the mechanism
147  * originally selected.
148  */
149 static OM_uint32
150 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
151 {
152         char *tok;
153         OM_uint32 offset;
154         OM_uint32 oidl;
155
156         tok=ename->value;
157
158 #ifdef GSI /* GSI gss_export_name() is broken. */
159         if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
160             (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
161                     gssapi_gsi_mech.oid.length) == 0)) {
162             name->length = ename->length;
163             name->value = xmalloc(ename->length+1);
164             memcpy(name->value, ename->value, ename->length);
165             return GSS_S_COMPLETE;
166         }
167 #endif
168
169         /*
170          * Check that ename is long enough for all of the fixed length
171          * header, and that the initial ID bytes are correct
172          */
173
174         if (ename->length<6 || memcmp(tok,"\x04\x01", 2)!=0)
175                 return GSS_S_FAILURE;
176
177         /*
178          * Extract the OID, and check it. Here GSSAPI breaks with tradition
179          * and does use the OID type and length bytes. To confuse things
180          * there are two lengths - the first including these, and the
181          * second without.
182          */
183
184         oidl = GET_16BIT(tok+2); /* length including next two bytes */
185         oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
186
187         /*
188          * Check the BER encoding for correct type and length, that the
189          * string is long enough and that the OID matches that in our context
190          */
191         if (tok[4] != 0x06 || tok[5] != oidl ||
192             ename->length < oidl+6 ||
193            !ssh_gssapi_check_oid(ctx,tok+6,oidl))
194                 return GSS_S_FAILURE;
195
196         offset = oidl+6;
197
198         if (ename->length < offset+4)
199                 return GSS_S_FAILURE;
200
201         name->length = GET_32BIT(tok+offset);
202         offset += 4;
203
204         if (ename->length < offset+name->length)
205                 return GSS_S_FAILURE;
206
207         name->value = xmalloc(name->length+1);
208         memcpy(name->value,tok+offset,name->length);
209         ((char *)name->value)[name->length] = 0;
210
211         return GSS_S_COMPLETE;
212 }
213
214 /* Extract the client details from a given context. This can only reliably
215  * be called once for a context */
216
217 /* Priviledged (called from accept_secure_ctx) */
218 OM_uint32
219 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
220 {
221         int i = 0;
222
223         gss_buffer_desc ename;
224
225         client->mech = NULL;
226
227         while (supported_mechs[i]->name != NULL) {
228                 if (supported_mechs[i]->oid.length == ctx->oid->length &&
229                     (memcmp(supported_mechs[i]->oid.elements,
230                     ctx->oid->elements, ctx->oid->length) == 0))
231                         client->mech = supported_mechs[i];
232                 i++;
233         }
234
235         if (client->mech == NULL)
236                 return GSS_S_FAILURE;
237
238         if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
239             &client->displayname, NULL))) {
240                 ssh_gssapi_error(ctx);
241                 return (ctx->major);
242         }
243
244         if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
245             &ename))) {
246                 ssh_gssapi_error(ctx);
247                 return (ctx->major);
248         }
249
250         if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
251             &client->exportedname))) {
252                 return (ctx->major);
253         }
254
255         /* We can't copy this structure, so we just move the pointer to it */
256         client->creds = ctx->client_creds;
257         ctx->client_creds = GSS_C_NO_CREDENTIAL;
258         return (ctx->major);
259 }
260
261 /* As user - called on fatal/exit */
262 void
263 ssh_gssapi_cleanup_creds(void)
264 {
265         if (gssapi_client.store.filename != NULL) {
266                 /* Unlink probably isn't sufficient */
267                 debug("removing gssapi cred file\"%s\"", gssapi_client.store.filename);
268                 unlink(gssapi_client.store.filename);
269         }
270 }
271
272 /* As user */
273 void
274 ssh_gssapi_storecreds(void)
275 {
276         if (gssapi_client.mech && gssapi_client.mech->storecreds) {
277                 (*gssapi_client.mech->storecreds)(&gssapi_client);
278         } else
279                 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
280 }
281
282 /* This allows GSSAPI methods to do things to the childs environment based
283  * on the passed authentication process and credentials.
284  */
285 /* As user */
286 void
287 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
288 {
289
290         if (gssapi_client.store.envvar != NULL &&
291             gssapi_client.store.envval != NULL) {
292
293                 debug("Setting %s to %s", gssapi_client.store.envvar,
294                 gssapi_client.store.envval);
295                 child_set_env(envp, envsizep, gssapi_client.store.envvar,
296                      gssapi_client.store.envval);
297         }
298 }
299
300 /* Priviledged */
301 int
302 ssh_gssapi_userok(char *user)
303 {
304         if (gssapi_client.exportedname.length == 0 ||
305             gssapi_client.exportedname.value == NULL) {
306                 debug("No suitable client data");
307                 return 0;
308         }
309         if (gssapi_client.mech && gssapi_client.mech->userok)
310                 return ((*gssapi_client.mech->userok)(&gssapi_client, user));
311         else
312                 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
313         return (0);
314 }
315
316 /* Return a list of the gss-group1-sha1-x mechanisms supported by this
317  * program.
318  *
319  * We only support the mechanisms that we've indicated in the list above,
320  * but we check that they're supported by the GSSAPI mechanism on the 
321  * machine. We also check, before including them in the list, that
322  * we have the necesary information in order to carry out the key exchange
323  * (that is, that the user has credentials, the server's creds are accessible,
324  * etc)
325  *
326  * The way that this is done is fairly nasty, as we do a lot of work that
327  * is then thrown away. This should possibly be implemented with a cache
328  * that stores the results (in an expanded Gssctxt structure), which are
329  * then used by the first calls if that key exchange mechanism is chosen.
330  */
331
332 /* Unpriviledged */ 
333 char * 
334 ssh_gssapi_server_mechanisms() {
335         gss_OID_set     supported;
336         Gssctxt         *ctx = NULL;
337         OM_uint32       maj_status, min_status;
338         Buffer          buf;
339         int             i = 0;
340         int             first = 0;
341         int             present;
342         char *          mechs;
343
344         if (datafellows & SSH_OLD_GSSAPI) return NULL;
345         
346         ssh_gssapi_supported_oids(&supported);
347         
348         buffer_init(&buf);
349
350         while(supported_mechs[i]->name != NULL) {
351                 if ((maj_status=gss_test_oid_set_member(&min_status,
352                                                         &supported_mechs[i]->oid,
353                                                         supported,
354                                                         &present))) {
355                         present=0;
356                 }
357
358                 if (present) {
359                     if (!GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx,
360                                            &supported_mechs[i]->oid)))) {
361                         /* Append gss_group1_sha1_x to our list */
362                         if (first++!=0)
363                                 buffer_put_char(&buf,',');
364                         buffer_append(&buf, KEX_GSS_SHA1,
365                                       sizeof(KEX_GSS_SHA1)-1);
366                         buffer_append(&buf, 
367                                       supported_mechs[i]->enc_name,
368                                       strlen(supported_mechs[i]->enc_name));
369                         debug("GSSAPI mechanism %s (%s%s) supported",
370                               supported_mechs[i]->name, KEX_GSS_SHA1,
371                               supported_mechs[i]->enc_name);
372                     } else {
373                         debug("no credentials for GSSAPI mechanism %s",
374                               supported_mechs[i]->name);
375                     }
376                 } else {
377                     debug("GSSAPI mechanism %s not supported",
378                           supported_mechs[i]->name);
379                 }
380                 ssh_gssapi_delete_ctx(&ctx);
381                 i++;
382         }
383         
384         buffer_put_char(&buf,'\0');
385         
386         mechs=xmalloc(buffer_len(&buf));
387         buffer_get(&buf,mechs,buffer_len(&buf));
388         buffer_free(&buf);
389         if (strlen(mechs)==0)
390            return(NULL);
391         else
392            return(mechs);
393 }
394
395 /* Return the OID that corresponds to the given context name */
396  
397 /* Unpriviledged */
398 gss_OID 
399 ssh_gssapi_server_id_kex(char *name) {
400   int i=0;
401   
402   if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) {
403      return(NULL);
404   }
405   
406   name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the MIME string */
407   
408   while (supported_mechs[i]->name!=NULL &&
409          strcmp(name,supported_mechs[i]->enc_name)!=0) {
410         i++;
411   }
412
413   if (supported_mechs[i]->name==NULL)
414      return (NULL);
415
416   debug("using GSSAPI mechanism %s (%s%s)", supported_mechs[i]->name,
417         KEX_GSS_SHA1, supported_mechs[i]->enc_name);
418
419   return &supported_mechs[i]->oid;
420 }
421
422 /* Priviledged */
423 int
424 ssh_gssapi_localname(char **user)
425 {
426         *user = NULL;
427         if (gssapi_client.displayname.length==0 || 
428             gssapi_client.displayname.value==NULL) {
429                 debug("No suitable client data");
430                 return(0);;
431         }
432         if (gssapi_client.mech && gssapi_client.mech->localname) {
433                 return((*gssapi_client.mech->localname)(&gssapi_client,user));
434         } else {
435                 debug("Unknown client authentication type");
436         }
437         return(0);
438 }
439
440 /* Priviledged */
441 OM_uint32
442 ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
443 {
444         ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
445             gssbuf, gssmic, NULL);
446
447         return (ctx->major);
448 }
449
450 #endif
This page took 0.073318 seconds and 5 git commands to generate.