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