[BACK]Return to gss-serv.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/gss-serv.c, Revision 1.21

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