Add support for mechanisms with no integrity
[openssh.git] / gss-serv.c
1 /* $OpenBSD: gss-serv.c,v 1.22 2008/05/08 12:02:23 djm Exp $ */
2
3 /*
4  * Copyright (c) 2001-2009 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 "openbsd-compat/sys-queue.h"
39 #include "xmalloc.h"
40 #include "buffer.h"
41 #include "key.h"
42 #include "hostfile.h"
43 #include "auth.h"
44 #include "log.h"
45 #include "channels.h"
46 #include "session.h"
47 #include "misc.h"
48 #include "servconf.h"
49 #include "uidswap.h"
50
51 #include "ssh-gss.h"
52 #include "monitor_wrap.h"
53
54 extern ServerOptions options;
55
56 static ssh_gssapi_client gssapi_client =
57     { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
58     GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME, {NULL, NULL, NULL}};
59
60 /*
61  * Acquire credentials for a server running on the current host.
62  * Requires that the context structure contains a valid OID
63  */
64
65 /* Returns a GSSAPI error code */
66 /* Privileged (called from ssh_gssapi_server_ctx) */
67 static OM_uint32
68 ssh_gssapi_acquire_cred(Gssctxt *ctx)
69 {
70         OM_uint32 status;
71         char lname[MAXHOSTNAMELEN];
72         gss_OID_set oidset;
73
74         if (options.gss_strict_acceptor) {
75                 gss_create_empty_oid_set(&status, &oidset);
76                 gss_add_oid_set_member(&status, ctx->oid, &oidset);
77
78                 if (gethostname(lname, MAXHOSTNAMELEN)) {
79                         gss_release_oid_set(&status, &oidset);
80                         return (-1);
81                 }
82
83                 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
84                         gss_release_oid_set(&status, &oidset);
85                         return (ctx->major);
86                 }
87
88                 if ((ctx->major = gss_acquire_cred(&ctx->minor,
89                     ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, 
90                     NULL, NULL)))
91                         ssh_gssapi_error(ctx);
92
93                 gss_release_oid_set(&status, &oidset);
94                 return (ctx->major);
95         } else {
96                 ctx->name = GSS_C_NO_NAME;
97                 ctx->creds = GSS_C_NO_CREDENTIAL;
98         }
99         return GSS_S_COMPLETE;
100 }
101
102 /* Privileged */
103 OM_uint32
104 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
105 {
106         if (*ctx)
107                 ssh_gssapi_delete_ctx(ctx);
108         ssh_gssapi_build_ctx(ctx);
109         ssh_gssapi_set_oid(*ctx, oid);
110         return (ssh_gssapi_acquire_cred(*ctx));
111 }
112
113 /* Unprivileged */
114 char *
115 ssh_gssapi_server_mechanisms() {
116         gss_OID_set     supported;
117
118         ssh_gssapi_supported_oids(&supported);
119         return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
120             NULL, NULL));
121 }
122
123 /* Unprivileged */
124 int
125 ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
126     const char *dummy) {
127         Gssctxt *ctx = NULL;
128         int res;
129  
130         res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
131         ssh_gssapi_delete_ctx(&ctx);
132
133         return (res);
134 }
135
136 /* Unprivileged */
137 void
138 ssh_gssapi_supported_oids(gss_OID_set *oidset)
139 {
140         OM_uint32 min_status;
141
142         gss_indicate_mechs(&min_status, oidset);
143 }
144
145
146 /* Wrapper around accept_sec_context
147  * Requires that the context contains:
148  *    oid
149  *    credentials       (from ssh_gssapi_acquire_cred)
150  */
151 /* Privileged */
152 OM_uint32
153 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
154     gss_buffer_desc *send_tok, OM_uint32 *flags)
155 {
156         OM_uint32 status;
157         gss_OID mech;
158
159         ctx->major = gss_accept_sec_context(&ctx->minor,
160             &ctx->context, ctx->creds, recv_tok,
161             GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
162             send_tok, flags, NULL, &ctx->client_creds);
163
164         if (GSS_ERROR(ctx->major))
165                 ssh_gssapi_error(ctx);
166
167         if (ctx->client_creds)
168                 debug("Received some client credentials");
169         else if (ctx->major == GSS_S_COMPLETE)
170                 debug("Got no client credentials");
171
172         status = ctx->major;
173
174         /* Now, if we're complete and we have the right flags, then
175          * we flag the user as also having been authenticated
176          */
177
178         if (ctx->major == GSS_S_COMPLETE) {
179                 if (options.gss_require_mic &&
180                     ((flags == NULL) || !(*flags & GSS_C_INTEG_FLAG))) {
181                         debug("GSSAPIRequireMIC true and integrity protection not supported so gssapi-with-mic fails.");
182                 } else if (ssh_gssapi_getclient(ctx, &gssapi_client)) {
183                         fatal("Couldn't convert client name");
184                 }
185         }
186
187         return (status);
188 }
189
190 /*
191  * This parses an exported name, extracting the mechanism specific portion
192  * to use for ACL checking. It verifies that the name belongs the mechanism
193  * originally selected.
194  */
195 static OM_uint32
196 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
197 {
198         u_char *tok;
199         OM_uint32 offset;
200         OM_uint32 oidl;
201
202         tok = ename->value;
203
204         /*
205          * Check that ename is long enough for all of the fixed length
206          * header, and that the initial ID bytes are correct
207          */
208
209         if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
210                 return GSS_S_FAILURE;
211
212         /*
213          * Extract the OID, and check it. Here GSSAPI breaks with tradition
214          * and does use the OID type and length bytes. To confuse things
215          * there are two lengths - the first including these, and the
216          * second without.
217          */
218
219         oidl = get_u16(tok+2); /* length including next two bytes */
220         oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
221
222         /*
223          * Check the BER encoding for correct type and length, that the
224          * string is long enough and that the OID matches that in our context
225          */
226         if (tok[4] != 0x06 || tok[5] != oidl ||
227             ename->length < oidl+6 ||
228             !ssh_gssapi_check_oid(ctx, tok+6, oidl))
229                 return GSS_S_FAILURE;
230
231         offset = oidl+6;
232
233         if (ename->length < offset+4)
234                 return GSS_S_FAILURE;
235
236         name->length = get_u32(tok+offset);
237         offset += 4;
238
239         if (ename->length < offset+name->length)
240                 return GSS_S_FAILURE;
241
242         name->value = xmalloc(name->length+1);
243         memcpy(name->value, tok+offset, name->length);
244         ((char *)name->value)[name->length] = 0;
245
246         return GSS_S_COMPLETE;
247 }
248
249 /* Extract the client details from a given context. This can only reliably
250  * be called once for a context */
251
252 /* Privileged (called from accept_secure_ctx) */
253 OM_uint32
254 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
255 {
256         gss_buffer_desc ename;
257
258         if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
259             &client->displayname, NULL))) {
260                 ssh_gssapi_error(ctx);
261                 return (ctx->major);
262         }
263
264         if ((ctx->major = gss_duplicate_name(&ctx->minor, ctx->client,
265                                              &client->name))) {
266                 ssh_gssapi_error(ctx);
267                 return (ctx->major);
268         }
269
270         if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
271             &ename))) {
272                 ssh_gssapi_error(ctx);
273                 return (ctx->major);
274         }
275
276         if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
277             &client->exportedname))) {
278                 return (ctx->major);
279         }
280
281         gss_release_buffer(&ctx->minor, &ename);
282
283         /* We can't copy this structure, so we just move the pointer to it */
284         client->creds = ctx->client_creds;
285         ctx->client_creds = GSS_C_NO_CREDENTIAL;
286         return (ctx->major);
287 }
288
289 /* As user - called on fatal/exit */
290 void
291 ssh_gssapi_cleanup_creds(void)
292 {
293         if (gssapi_client.store.filename != NULL) {
294                 /* Unlink probably isn't sufficient */
295                 debug("removing gssapi cred file\"%s\"",
296                     gssapi_client.store.filename);
297                 unlink(gssapi_client.store.filename);
298         }
299 }
300
301 /* As user */
302 void
303 ssh_gssapi_storecreds(void)
304 {
305         OM_uint32 lmin;
306
307         gss_store_cred(&lmin, gssapi_client.creds,
308                        GSS_C_INITIATE, GSS_C_NO_OID,
309                        1, 1, NULL, NULL);
310 }
311
312 /* This allows GSSAPI methods to do things to the childs environment based
313  * on the passed authentication process and credentials.
314  */
315 /* As user */
316 void
317 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
318 {
319
320         if (gssapi_client.store.envvar != NULL &&
321             gssapi_client.store.envval != NULL) {
322                 debug("Setting %s to %s", gssapi_client.store.envvar,
323                     gssapi_client.store.envval);
324                 child_set_env(envp, envsizep, gssapi_client.store.envvar,
325                     gssapi_client.store.envval);
326         }
327 }
328
329 /* Privileged */
330 int
331 ssh_gssapi_userok(char *user, struct passwd *pw)
332 {
333         OM_uint32 lmin;
334         int userok = 0;
335
336         if (gssapi_client.exportedname.length == 0 ||
337             gssapi_client.exportedname.value == NULL) {
338                 debug("No suitable client data");
339                 return 0;
340         }
341
342         userok = gss_userok(gssapi_client.name, user);
343         if (userok) {
344                 gssapi_client.used = 1;
345                 gssapi_client.store.owner = pw;
346         } else {
347                 /* Destroy delegated credentials if userok fails */
348                 gss_release_buffer(&lmin, &gssapi_client.displayname);
349                 gss_release_buffer(&lmin, &gssapi_client.exportedname);
350                 gss_release_name(&lmin, &gssapi_client.name);
351                 gss_release_cred(&lmin, &gssapi_client.creds);
352                 memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
353         }
354
355         return (userok);
356 }
357
358 /* Priviledged */
359 OM_uint32
360 ssh_gssapi_localname(char **user)
361 {
362         OM_uint32 major_status, lmin;
363         uid_t uid;
364         struct passwd *pw;
365
366         major_status = gss_pname_to_uid(&lmin, gssapi_client.name,
367                                         GSS_C_NO_OID, &uid);
368         if (GSS_ERROR(major_status))
369                 return (major_status);
370
371         pw = getpwuid(uid);
372         if (pw == NULL)
373                 return (GSS_S_BAD_NAME);
374
375         *user = xstrdup(pw->pw_name);
376
377         return (GSS_S_COMPLETE);
378 }
379 #endif /* GSSAPI */