]> andersk Git - gssapi-openssh.git/blob - openssh/gss-serv.c
update from Simon's openssh-4.3p2-gsskex-20060223.patch
[gssapi-openssh.git] / openssh / gss-serv.c
1 /*      $OpenBSD: gss-serv.c,v 1.13 2005/10/13 22:24:31 stevesk 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 static ssh_gssapi_client gssapi_client =
48     { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
49     GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL}};
50
51 ssh_gssapi_mech gssapi_null_mech =
52     { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
53
54 #ifdef KRB5
55 extern ssh_gssapi_mech gssapi_kerberos_mech;
56 #endif
57 #ifdef GSI
58 extern ssh_gssapi_mech gssapi_gsi_mech;
59 #endif
60
61 ssh_gssapi_mech* supported_mechs[]= {
62 #ifdef KRB5
63         &gssapi_kerberos_mech,
64 #endif
65 #ifdef GSI
66         &gssapi_gsi_mech,
67 #endif
68         &gssapi_null_mech,
69 };
70
71 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
72 static int limited = 0;
73 #endif
74
75 /* Unprivileged */
76 char *
77 ssh_gssapi_server_mechanisms() {
78         gss_OID_set     supported;
79
80         ssh_gssapi_supported_oids(&supported);
81         return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
82             NULL));
83 }
84
85 /* Unprivileged */
86 int
87 ssh_gssapi_server_check_mech(gss_OID oid, void *data) {
88         Gssctxt * ctx = NULL;
89         int res;
90  
91         res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
92         ssh_gssapi_delete_ctx(&ctx);
93
94         return (res);
95 }
96
97 /* Unprivileged */
98 void
99 ssh_gssapi_supported_oids(gss_OID_set *oidset)
100 {
101         int i = 0;
102         OM_uint32 min_status;
103         int present;
104         gss_OID_set supported;
105
106         gss_create_empty_oid_set(&min_status, oidset);
107         /* Ask priviledged process what mechanisms it supports. */
108         PRIVSEP(gss_indicate_mechs(&min_status, &supported));
109
110         while (supported_mechs[i]->name != NULL) {
111                 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
112                     &supported_mechs[i]->oid, supported, &present)))
113                         present = 0;
114                 if (present)
115                         gss_add_oid_set_member(&min_status,
116                             &supported_mechs[i]->oid, oidset);
117                 i++;
118         }
119 }
120
121
122 /* Wrapper around accept_sec_context
123  * Requires that the context contains:
124  *    oid
125  *    credentials       (from ssh_gssapi_acquire_cred)
126  */
127 /* Privileged */
128 OM_uint32
129 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
130     gss_buffer_desc *send_tok, OM_uint32 *flags)
131 {
132         OM_uint32 status;
133         gss_OID mech;
134
135         ctx->major = gss_accept_sec_context(&ctx->minor,
136             &ctx->context, ctx->creds, recv_tok,
137             GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
138             send_tok, flags, NULL, &ctx->client_creds);
139
140         if (GSS_ERROR(ctx->major))
141                 ssh_gssapi_error(ctx);
142
143         if (ctx->client_creds)
144                 debug("Received some client credentials");
145         else
146                 debug("Got no client credentials");
147
148         status = ctx->major;
149
150         /* Now, if we're complete and we have the right flags, then
151          * we flag the user as also having been authenticated
152          */
153
154         if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
155             (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
156                 if (ssh_gssapi_getclient(ctx, &gssapi_client))
157                         fatal("Couldn't convert client name");
158 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
159                 if (flags && (*flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG))
160                         limited=1;
161 #endif
162         }
163
164         return (status);
165 }
166
167 /*
168  * This parses an exported name, extracting the mechanism specific portion
169  * to use for ACL checking. It verifies that the name belongs the mechanism
170  * originally selected.
171  */
172 static OM_uint32
173 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
174 {
175         u_char *tok;
176         OM_uint32 offset;
177         OM_uint32 oidl;
178
179         tok = ename->value;
180
181 #ifdef GSI /* GSI gss_export_name() is broken. */
182         if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
183             (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
184                     gssapi_gsi_mech.oid.length) == 0)) {
185             name->length = ename->length;
186             name->value = xmalloc(ename->length+1);
187             memcpy(name->value, ename->value, ename->length);
188             return GSS_S_COMPLETE;
189         }
190 #endif
191
192         /*
193          * Check that ename is long enough for all of the fixed length
194          * header, and that the initial ID bytes are correct
195          */
196
197         if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
198                 return GSS_S_FAILURE;
199
200         /*
201          * Extract the OID, and check it. Here GSSAPI breaks with tradition
202          * and does use the OID type and length bytes. To confuse things
203          * there are two lengths - the first including these, and the
204          * second without.
205          */
206
207         oidl = GET_16BIT(tok+2); /* length including next two bytes */
208         oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
209
210         /*
211          * Check the BER encoding for correct type and length, that the
212          * string is long enough and that the OID matches that in our context
213          */
214         if (tok[4] != 0x06 || tok[5] != oidl ||
215             ename->length < oidl+6 ||
216             !ssh_gssapi_check_oid(ctx, tok+6, oidl))
217                 return GSS_S_FAILURE;
218
219         offset = oidl+6;
220
221         if (ename->length < offset+4)
222                 return GSS_S_FAILURE;
223
224         name->length = GET_32BIT(tok+offset);
225         offset += 4;
226
227         if (ename->length < offset+name->length)
228                 return GSS_S_FAILURE;
229
230         name->value = xmalloc(name->length+1);
231         memcpy(name->value, tok+offset,name->length);
232         ((char *)name->value)[name->length] = 0;
233
234         return GSS_S_COMPLETE;
235 }
236
237 /* Extract the client details from a given context. This can only reliably
238  * be called once for a context */
239
240 /* Privileged (called from accept_secure_ctx) */
241 OM_uint32
242 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
243 {
244         int i = 0;
245
246         gss_buffer_desc ename;
247
248         client->mech = NULL;
249
250         while (supported_mechs[i]->name != NULL) {
251                 if (supported_mechs[i]->oid.length == ctx->oid->length &&
252                     (memcmp(supported_mechs[i]->oid.elements,
253                     ctx->oid->elements, ctx->oid->length) == 0))
254                         client->mech = supported_mechs[i];
255                 i++;
256         }
257
258         if (client->mech == NULL)
259                 return GSS_S_FAILURE;
260
261         if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
262             &client->displayname, NULL))) {
263                 ssh_gssapi_error(ctx);
264                 return (ctx->major);
265         }
266
267         if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
268             &ename))) {
269                 ssh_gssapi_error(ctx);
270                 return (ctx->major);
271         }
272
273         if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
274             &client->exportedname))) {
275                 return (ctx->major);
276         }
277
278         /* We can't copy this structure, so we just move the pointer to it */
279         client->creds = ctx->client_creds;
280         ctx->client_creds = GSS_C_NO_CREDENTIAL;
281         return (ctx->major);
282 }
283
284 /* As user - called on fatal/exit */
285 void
286 ssh_gssapi_cleanup_creds(void)
287 {
288         if (gssapi_client.store.filename != NULL) {
289                 /* Unlink probably isn't sufficient */
290                 debug("removing gssapi cred file\"%s\"", gssapi_client.store.filename);
291                 unlink(gssapi_client.store.filename);
292         }
293 }
294
295 /* As user */
296 void
297 ssh_gssapi_storecreds(void)
298 {
299         if (gssapi_client.mech && gssapi_client.mech->storecreds) {
300                 (*gssapi_client.mech->storecreds)(&gssapi_client);
301         } else
302                 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
303 }
304
305 /* This allows GSSAPI methods to do things to the childs environment based
306  * on the passed authentication process and credentials.
307  */
308 /* As user */
309 void
310 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
311 {
312
313         if (gssapi_client.store.envvar != NULL &&
314             gssapi_client.store.envval != NULL) {
315                 debug("Setting %s to %s", gssapi_client.store.envvar,
316                     gssapi_client.store.envval);
317                 child_set_env(envp, envsizep, gssapi_client.store.envvar,
318                     gssapi_client.store.envval);
319         }
320 }
321
322 /* Privileged */
323 int
324 ssh_gssapi_userok(char *user)
325 {
326         OM_uint32 lmin;
327
328         if (gssapi_client.exportedname.length == 0 ||
329             gssapi_client.exportedname.value == NULL) {
330                 debug("No suitable client data");
331                 return 0;
332         }
333 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
334         if (limited) {
335                 debug("limited proxy not acceptable for remote login");
336                 return 0;
337         }
338 #endif
339         if (gssapi_client.mech && gssapi_client.mech->userok)
340                 if ((*gssapi_client.mech->userok)(&gssapi_client, user))
341                         return 1;
342                 else {
343                         /* Destroy delegated credentials if userok fails */
344                         gss_release_buffer(&lmin, &gssapi_client.displayname);
345                         gss_release_buffer(&lmin, &gssapi_client.exportedname);
346                         gss_release_cred(&lmin, &gssapi_client.creds);
347                         memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
348                         return 0;
349                 }
350         else
351                 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
352         return (0);
353 }
354
355 /* Priviledged */
356 int
357 ssh_gssapi_localname(char **user)
358 {
359         *user = NULL;
360         if (gssapi_client.displayname.length==0 || 
361             gssapi_client.displayname.value==NULL) {
362                 debug("No suitable client data");
363                 return(0);;
364         }
365         if (gssapi_client.mech && gssapi_client.mech->localname) {
366                 return((*gssapi_client.mech->localname)(&gssapi_client,user));
367         } else {
368                 debug("Unknown client authentication type");
369         }
370         return(0);
371 }
372
373 #endif
This page took 0.330611 seconds and 5 git commands to generate.