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