allow empty username, and determine using pname_to_uid
[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
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 (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
179             (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
180                 if (ssh_gssapi_getclient(ctx, &gssapi_client))
181                         fatal("Couldn't convert client name");
182         }
183
184         return (status);
185 }
186
187 /*
188  * This parses an exported name, extracting the mechanism specific portion
189  * to use for ACL checking. It verifies that the name belongs the mechanism
190  * originally selected.
191  */
192 static OM_uint32
193 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
194 {
195         u_char *tok;
196         OM_uint32 offset;
197         OM_uint32 oidl;
198
199         tok = ename->value;
200
201         /*
202          * Check that ename is long enough for all of the fixed length
203          * header, and that the initial ID bytes are correct
204          */
205
206         if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
207                 return GSS_S_FAILURE;
208
209         /*
210          * Extract the OID, and check it. Here GSSAPI breaks with tradition
211          * and does use the OID type and length bytes. To confuse things
212          * there are two lengths - the first including these, and the
213          * second without.
214          */
215
216         oidl = get_u16(tok+2); /* length including next two bytes */
217         oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
218
219         /*
220          * Check the BER encoding for correct type and length, that the
221          * string is long enough and that the OID matches that in our context
222          */
223         if (tok[4] != 0x06 || tok[5] != oidl ||
224             ename->length < oidl+6 ||
225             !ssh_gssapi_check_oid(ctx, tok+6, oidl))
226                 return GSS_S_FAILURE;
227
228         offset = oidl+6;
229
230         if (ename->length < offset+4)
231                 return GSS_S_FAILURE;
232
233         name->length = get_u32(tok+offset);
234         offset += 4;
235
236         if (ename->length < offset+name->length)
237                 return GSS_S_FAILURE;
238
239         name->value = xmalloc(name->length+1);
240         memcpy(name->value, tok+offset, name->length);
241         ((char *)name->value)[name->length] = 0;
242
243         return GSS_S_COMPLETE;
244 }
245
246 /* Extract the client details from a given context. This can only reliably
247  * be called once for a context */
248
249 /* Privileged (called from accept_secure_ctx) */
250 OM_uint32
251 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
252 {
253         gss_buffer_desc ename;
254
255         if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
256             &client->displayname, NULL))) {
257                 ssh_gssapi_error(ctx);
258                 return (ctx->major);
259         }
260
261         if ((ctx->major = gss_duplicate_name(&ctx->minor, ctx->client,
262                                              &client->name))) {
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         gss_release_buffer(&ctx->minor, &ename);
279
280         /* We can't copy this structure, so we just move the pointer to it */
281         client->creds = ctx->client_creds;
282         ctx->client_creds = GSS_C_NO_CREDENTIAL;
283         return (ctx->major);
284 }
285
286 /* As user - called on fatal/exit */
287 void
288 ssh_gssapi_cleanup_creds(void)
289 {
290         if (gssapi_client.store.filename != NULL) {
291                 /* Unlink probably isn't sufficient */
292                 debug("removing gssapi cred file\"%s\"",
293                     gssapi_client.store.filename);
294                 unlink(gssapi_client.store.filename);
295         }
296 }
297
298 /* As user */
299 void
300 ssh_gssapi_storecreds(void)
301 {
302         OM_uint32 lmin;
303
304         gss_store_cred(&lmin, gssapi_client.creds,
305                        GSS_C_INITIATE, GSS_C_NO_OID,
306                        1, 1, NULL, NULL);
307 }
308
309 /* This allows GSSAPI methods to do things to the childs environment based
310  * on the passed authentication process and credentials.
311  */
312 /* As user */
313 void
314 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
315 {
316
317         if (gssapi_client.store.envvar != NULL &&
318             gssapi_client.store.envval != NULL) {
319                 debug("Setting %s to %s", gssapi_client.store.envvar,
320                     gssapi_client.store.envval);
321                 child_set_env(envp, envsizep, gssapi_client.store.envvar,
322                     gssapi_client.store.envval);
323         }
324 }
325
326 /* Privileged */
327 int
328 ssh_gssapi_userok(char *user, struct passwd *pw)
329 {
330         OM_uint32 lmin;
331         int userok = 0;
332
333         if (gssapi_client.exportedname.length == 0 ||
334             gssapi_client.exportedname.value == NULL) {
335                 debug("No suitable client data");
336                 return 0;
337         }
338         if (!gss_userok(gssapi_client.name, user)) {
339                 /* Destroy delegated credentials if userok fails */
340                 gss_release_buffer(&lmin, &gssapi_client.displayname);
341                 gss_release_buffer(&lmin, &gssapi_client.exportedname);
342                 gss_release_name(&lmin, &gssapi_client.name);
343                 gss_release_cred(&lmin, &gssapi_client.creds);
344                 memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
345                 return 0;
346         }
347
348         if (userok) {
349                 gssapi_client.used = 1;
350                 gssapi_client.store.owner = pw;
351         }
352
353         return (userok);
354 }
355
356 /* Priviledged */
357 OM_uint32
358 ssh_gssapi_localname(char **user)
359 {
360         OM_uint32 major_status, lmin;
361         uid_t uid;
362         struct passwd *pw;
363
364         major_status = gss_pname_to_uid(&lmin, gssapi_client.name,
365                                         GSS_C_NO_OID, &uid);
366         if (GSS_ERROR(major_status))
367                 return (major_status);
368
369         pw = getpwuid(uid);
370         if (pw == NULL)
371                 return (GSS_S_BAD_NAME);
372
373         *user = xstrdup(pw->pw_name);
374
375         return (GSS_S_COMPLETE);
376 }
377 #endif /* GSSAPI */