]> andersk Git - gssapi-openssh.git/blob - openssh/gss-serv.c
merged OpenSSH 4.7p1 to trunk
[gssapi-openssh.git] / openssh / gss-serv.c
1 /* $OpenBSD: gss-serv.c,v 1.21 2007/06/12 08:20:00 djm Exp $ */
2
3 /*
4  * Copyright (c) 2001-2006 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 <sys/types.h>
32 #include <sys/param.h>
33
34 #include <stdarg.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "xmalloc.h"
39 #include "buffer.h"
40 #include "key.h"
41 #include "hostfile.h"
42 #include "auth.h"
43 #include "log.h"
44 #include "channels.h"
45 #include "session.h"
46 #include "misc.h"
47 #include "servconf.h"
48
49 #include "xmalloc.h"
50 #include "ssh-gss.h"
51 #include "monitor_wrap.h"
52
53 extern ServerOptions options;
54 extern Authctxt *the_authctxt;
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 #endif
66 #ifdef GSI
67 extern ssh_gssapi_mech gssapi_gsi_mech;
68 #endif
69
70 ssh_gssapi_mech* supported_mechs[]= {
71 #ifdef KRB5
72         &gssapi_kerberos_mech,
73 #endif
74 #ifdef GSI
75         &gssapi_gsi_mech,
76 #endif
77         &gssapi_null_mech,
78 };
79
80 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
81 static int limited = 0;
82 #endif
83
84 /* Unprivileged */
85 char *
86 ssh_gssapi_server_mechanisms() {
87         gss_OID_set     supported;
88
89         ssh_gssapi_supported_oids(&supported);
90         return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
91             NULL));
92 }
93
94 /* Unprivileged */
95 int
96 ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data) {
97         Gssctxt *ctx = NULL;
98         int res;
99  
100         res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
101         ssh_gssapi_delete_ctx(&ctx);
102
103         return (res);
104 }
105
106 /* Unprivileged */
107 void
108 ssh_gssapi_supported_oids(gss_OID_set *oidset)
109 {
110         int i = 0;
111         OM_uint32 min_status;
112         int present;
113         gss_OID_set supported;
114
115         gss_create_empty_oid_set(&min_status, oidset);
116         /* Ask priviledged process what mechanisms it supports. */
117         PRIVSEP(gss_indicate_mechs(&min_status, &supported));
118
119         while (supported_mechs[i]->name != NULL) {
120                 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
121                     &supported_mechs[i]->oid, supported, &present)))
122                         present = 0;
123                 if (present)
124                         gss_add_oid_set_member(&min_status,
125                             &supported_mechs[i]->oid, oidset);
126                 i++;
127         }
128
129         gss_release_oid_set(&min_status, &supported);
130 }
131
132 /*
133  * Acquire credentials for a server running on the current host.
134  * Requires that the context structure contains a valid OID
135  */
136
137 /* Returns a GSSAPI error code */
138 /* Privileged (called from ssh_gssapi_server_ctx) */
139 static OM_uint32
140 ssh_gssapi_acquire_cred(Gssctxt *ctx)
141 {
142         OM_uint32 status;
143         char lname[MAXHOSTNAMELEN];
144         gss_OID_set oidset;
145
146         if (options.gss_strict_acceptor) {
147                 gss_create_empty_oid_set(&status, &oidset);
148                 gss_add_oid_set_member(&status, ctx->oid, &oidset);
149
150                 if (gethostname(lname, MAXHOSTNAMELEN)) {
151                         gss_release_oid_set(&status, &oidset);
152                         return (-1);
153                 }
154
155                 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
156                         gss_release_oid_set(&status, &oidset);
157                         return (ctx->major);
158                 }
159
160                 if ((ctx->major = gss_acquire_cred(&ctx->minor,
161                     ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, 
162                     NULL, NULL)))
163                         ssh_gssapi_error(ctx);
164
165                 gss_release_oid_set(&status, &oidset);
166                 return (ctx->major);
167         } else {
168                 ctx->name = GSS_C_NO_NAME;
169                 ctx->creds = GSS_C_NO_CREDENTIAL;
170         }
171         return GSS_S_COMPLETE;
172 }
173
174
175 /* Privileged */
176 OM_uint32
177 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
178 {
179         if (*ctx)
180                 ssh_gssapi_delete_ctx(ctx);
181         ssh_gssapi_build_ctx(ctx);
182         ssh_gssapi_set_oid(*ctx, oid);
183         return (ssh_gssapi_acquire_cred(*ctx));
184 }
185
186 /* Wrapper around accept_sec_context
187  * Requires that the context contains:
188  *    oid
189  *    credentials       (from ssh_gssapi_acquire_cred)
190  */
191 /* Privileged */
192 OM_uint32
193 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
194     gss_buffer_desc *send_tok, OM_uint32 *flags)
195 {
196         OM_uint32 status;
197         gss_OID mech;
198
199         ctx->major = gss_accept_sec_context(&ctx->minor,
200             &ctx->context, ctx->creds, recv_tok,
201             GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
202             send_tok, flags, NULL, &ctx->client_creds);
203
204         if (GSS_ERROR(ctx->major))
205                 ssh_gssapi_error(ctx);
206
207         if (ctx->client_creds)
208                 debug("Received some client credentials");
209         else
210                 debug("Got no client credentials");
211
212         status = ctx->major;
213
214         /* Now, if we're complete and we have the right flags, then
215          * we flag the user as also having been authenticated
216          */
217
218         if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
219             (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
220                 if (ssh_gssapi_getclient(ctx, &gssapi_client))
221                         fatal("Couldn't convert client name");
222 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
223                 if (flags && (*flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG))
224                         limited=1;
225 #endif
226         }
227
228         return (status);
229 }
230
231 /*
232  * This parses an exported name, extracting the mechanism specific portion
233  * to use for ACL checking. It verifies that the name belongs the mechanism
234  * originally selected.
235  */
236 static OM_uint32
237 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
238 {
239         u_char *tok;
240         OM_uint32 offset;
241         OM_uint32 oidl;
242
243         tok = ename->value;
244
245 #ifdef GSI /* GSI gss_export_name() is broken. */
246         if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
247             (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
248                     gssapi_gsi_mech.oid.length) == 0)) {
249             name->length = ename->length;
250             name->value = xmalloc(ename->length+1);
251             memcpy(name->value, ename->value, ename->length);
252             return GSS_S_COMPLETE;
253         }
254 #endif
255
256         /*
257          * Check that ename is long enough for all of the fixed length
258          * header, and that the initial ID bytes are correct
259          */
260
261         if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
262                 return GSS_S_FAILURE;
263
264         /*
265          * Extract the OID, and check it. Here GSSAPI breaks with tradition
266          * and does use the OID type and length bytes. To confuse things
267          * there are two lengths - the first including these, and the
268          * second without.
269          */
270
271         oidl = get_u16(tok+2); /* length including next two bytes */
272         oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
273
274         /*
275          * Check the BER encoding for correct type and length, that the
276          * string is long enough and that the OID matches that in our context
277          */
278         if (tok[4] != 0x06 || tok[5] != oidl ||
279             ename->length < oidl+6 ||
280             !ssh_gssapi_check_oid(ctx, tok+6, oidl))
281                 return GSS_S_FAILURE;
282
283         offset = oidl+6;
284
285         if (ename->length < offset+4)
286                 return GSS_S_FAILURE;
287
288         name->length = get_u32(tok+offset);
289         offset += 4;
290
291         if (ename->length < offset+name->length)
292                 return GSS_S_FAILURE;
293
294         name->value = xmalloc(name->length+1);
295         memcpy(name->value, tok+offset, name->length);
296         ((char *)name->value)[name->length] = 0;
297
298         return GSS_S_COMPLETE;
299 }
300
301 /* Extract the client details from a given context. This can only reliably
302  * be called once for a context */
303
304 /* Privileged (called from accept_secure_ctx) */
305 OM_uint32
306 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
307 {
308         int i = 0;
309
310         gss_buffer_desc ename;
311
312         client->mech = NULL;
313
314         while (supported_mechs[i]->name != NULL) {
315                 if (supported_mechs[i]->oid.length == ctx->oid->length &&
316                     (memcmp(supported_mechs[i]->oid.elements,
317                     ctx->oid->elements, ctx->oid->length) == 0))
318                         client->mech = supported_mechs[i];
319                 i++;
320         }
321
322         if (client->mech == NULL)
323                 return GSS_S_FAILURE;
324
325         if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
326             &client->displayname, NULL))) {
327                 ssh_gssapi_error(ctx);
328                 return (ctx->major);
329         }
330
331         if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
332             &ename))) {
333                 ssh_gssapi_error(ctx);
334                 return (ctx->major);
335         }
336
337         if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
338             &client->exportedname))) {
339                 return (ctx->major);
340         }
341
342         /* We can't copy this structure, so we just move the pointer to it */
343         client->creds = ctx->client_creds;
344         ctx->client_creds = GSS_C_NO_CREDENTIAL;
345
346     /* needed for globus_gss_assist_map_and_authorize() */
347     client->context = ctx->context;
348
349         return (ctx->major);
350 }
351
352 /* As user - called on fatal/exit */
353 void
354 ssh_gssapi_cleanup_creds(void)
355 {
356         if (gssapi_client.store.filename != NULL) {
357                 /* Unlink probably isn't sufficient */
358                 debug("removing gssapi cred file\"%s\"",
359                     gssapi_client.store.filename);
360                 unlink(gssapi_client.store.filename);
361         }
362 }
363
364 /* As user */
365 void
366 ssh_gssapi_storecreds(void)
367 {
368         if (gssapi_client.mech && gssapi_client.mech->storecreds) {
369         if (options.gss_creds_path) {
370             gssapi_client.store.filename =
371                 expand_authorized_keys(options.gss_creds_path,
372                                        the_authctxt->pw);
373         }
374                 (*gssapi_client.mech->storecreds)(&gssapi_client);
375         } else
376                 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
377 }
378
379 /* This allows GSSAPI methods to do things to the childs environment based
380  * on the passed authentication process and credentials.
381  */
382 /* As user */
383 void
384 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
385 {
386
387         if (gssapi_client.store.envvar != NULL &&
388             gssapi_client.store.envval != NULL) {
389                 debug("Setting %s to %s", gssapi_client.store.envvar,
390                     gssapi_client.store.envval);
391                 child_set_env(envp, envsizep, gssapi_client.store.envvar,
392                     gssapi_client.store.envval);
393         }
394 }
395
396 /* Privileged */
397 int
398 ssh_gssapi_userok(char *user)
399 {
400         OM_uint32 lmin;
401
402         if (gssapi_client.exportedname.length == 0 ||
403             gssapi_client.exportedname.value == NULL) {
404                 debug("No suitable client data");
405                 return 0;
406         }
407 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
408         if (limited && options.gsi_allow_limited_proxy != 1) {
409                 debug("limited proxy not acceptable for remote login");
410                 return 0;
411         }
412 #endif
413         if (gssapi_client.mech && gssapi_client.mech->userok)
414                 if ((*gssapi_client.mech->userok)(&gssapi_client, user))
415                         return 1;
416                 else {
417                         /* Destroy delegated credentials if userok fails */
418                         gss_release_buffer(&lmin, &gssapi_client.displayname);
419                         gss_release_buffer(&lmin, &gssapi_client.exportedname);
420                         gss_release_cred(&lmin, &gssapi_client.creds);
421                         memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
422                         return 0;
423                 }
424         else
425                 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
426         return (0);
427 }
428
429 /* Priviledged */
430 int
431 ssh_gssapi_localname(char **user)
432 {
433         *user = NULL;
434         if (gssapi_client.displayname.length==0 || 
435             gssapi_client.displayname.value==NULL) {
436                 debug("No suitable client data");
437                 return(0);;
438         }
439         if (gssapi_client.mech && gssapi_client.mech->localname) {
440                 return((*gssapi_client.mech->localname)(&gssapi_client,user));
441         } else {
442                 debug("Unknown client authentication type");
443         }
444         return(0);
445 }
446
447 #endif
This page took 0.196296 seconds and 5 git commands to generate.