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

Annotation of src/usr.bin/ssh/auth2-pubkey.c, Revision 1.9.4.1

1.9.4.1 ! brad        1: /* $OpenBSD: auth2-pubkey.c,v 1.15 2006/08/03 03:34:41 deraadt Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
                     25:
                     26:
1.9.4.1 ! brad       27: #include <sys/types.h>
        !            28: #include <sys/stat.h>
        !            29:
        !            30: #include <pwd.h>
        !            31: #include <stdio.h>
        !            32: #include <stdarg.h>
        !            33:
        !            34: #include "xmalloc.h"
1.8       dtucker    35: #include "ssh.h"
1.1       markus     36: #include "ssh2.h"
                     37: #include "packet.h"
                     38: #include "buffer.h"
                     39: #include "log.h"
                     40: #include "servconf.h"
                     41: #include "compat.h"
                     42: #include "key.h"
1.9.4.1 ! brad       43: #include "hostfile.h"
        !            44: #include "auth.h"
1.1       markus     45: #include "pathnames.h"
                     46: #include "uidswap.h"
                     47: #include "auth-options.h"
                     48: #include "canohost.h"
1.9.4.1 ! brad       49: #ifdef GSSAPI
        !            50: #include "ssh-gss.h"
        !            51: #endif
1.1       markus     52: #include "monitor_wrap.h"
1.9       dtucker    53: #include "misc.h"
1.1       markus     54:
                     55: /* import */
                     56: extern ServerOptions options;
                     57: extern u_char *session_id2;
1.4       markus     58: extern u_int session_id2_len;
1.1       markus     59:
1.2       markus     60: static int
1.1       markus     61: userauth_pubkey(Authctxt *authctxt)
                     62: {
                     63:        Buffer b;
                     64:        Key *key = NULL;
                     65:        char *pkalg;
                     66:        u_char *pkblob, *sig;
                     67:        u_int alen, blen, slen;
                     68:        int have_sig, pktype;
                     69:        int authenticated = 0;
                     70:
                     71:        if (!authctxt->valid) {
                     72:                debug2("userauth_pubkey: disabled because of invalid user");
                     73:                return 0;
                     74:        }
                     75:        have_sig = packet_get_char();
                     76:        if (datafellows & SSH_BUG_PKAUTH) {
                     77:                debug2("userauth_pubkey: SSH_BUG_PKAUTH");
                     78:                /* no explicit pkalg given */
                     79:                pkblob = packet_get_string(&blen);
                     80:                buffer_init(&b);
                     81:                buffer_append(&b, pkblob, blen);
                     82:                /* so we have to extract the pkalg from the pkblob */
                     83:                pkalg = buffer_get_string(&b, &alen);
                     84:                buffer_free(&b);
                     85:        } else {
                     86:                pkalg = packet_get_string(&alen);
                     87:                pkblob = packet_get_string(&blen);
                     88:        }
                     89:        pktype = key_type_from_name(pkalg);
                     90:        if (pktype == KEY_UNSPEC) {
                     91:                /* this is perfectly legal */
1.3       itojun     92:                logit("userauth_pubkey: unsupported public key algorithm: %s",
1.1       markus     93:                    pkalg);
                     94:                goto done;
                     95:        }
                     96:        key = key_from_blob(pkblob, blen);
                     97:        if (key == NULL) {
                     98:                error("userauth_pubkey: cannot decode key: %s", pkalg);
                     99:                goto done;
                    100:        }
                    101:        if (key->type != pktype) {
                    102:                error("userauth_pubkey: type mismatch for decoded key "
                    103:                    "(received %d, expected %d)", key->type, pktype);
                    104:                goto done;
                    105:        }
                    106:        if (have_sig) {
                    107:                sig = packet_get_string(&slen);
                    108:                packet_check_eom();
                    109:                buffer_init(&b);
                    110:                if (datafellows & SSH_OLD_SESSIONID) {
                    111:                        buffer_append(&b, session_id2, session_id2_len);
                    112:                } else {
                    113:                        buffer_put_string(&b, session_id2, session_id2_len);
                    114:                }
                    115:                /* reconstruct packet */
                    116:                buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
                    117:                buffer_put_cstring(&b, authctxt->user);
                    118:                buffer_put_cstring(&b,
                    119:                    datafellows & SSH_BUG_PKSERVICE ?
                    120:                    "ssh-userauth" :
                    121:                    authctxt->service);
                    122:                if (datafellows & SSH_BUG_PKAUTH) {
                    123:                        buffer_put_char(&b, have_sig);
                    124:                } else {
                    125:                        buffer_put_cstring(&b, "publickey");
                    126:                        buffer_put_char(&b, have_sig);
                    127:                        buffer_put_cstring(&b, pkalg);
                    128:                }
                    129:                buffer_put_string(&b, pkblob, blen);
                    130: #ifdef DEBUG_PK
                    131:                buffer_dump(&b);
                    132: #endif
                    133:                /* test for correct signature */
                    134:                authenticated = 0;
                    135:                if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
                    136:                    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
1.6       markus    137:                    buffer_len(&b))) == 1)
1.1       markus    138:                        authenticated = 1;
1.6       markus    139:                buffer_free(&b);
1.1       markus    140:                xfree(sig);
                    141:        } else {
                    142:                debug("test whether pkalg/pkblob are acceptable");
                    143:                packet_check_eom();
                    144:
                    145:                /* XXX fake reply and always send PK_OK ? */
                    146:                /*
                    147:                 * XXX this allows testing whether a user is allowed
                    148:                 * to login: if you happen to have a valid pubkey this
                    149:                 * message is sent. the message is NEVER sent at all
                    150:                 * if a user is not allowed to login. is this an
                    151:                 * issue? -markus
                    152:                 */
                    153:                if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
                    154:                        packet_start(SSH2_MSG_USERAUTH_PK_OK);
                    155:                        packet_put_string(pkalg, alen);
                    156:                        packet_put_string(pkblob, blen);
                    157:                        packet_send();
                    158:                        packet_write_wait();
                    159:                        authctxt->postponed = 1;
                    160:                }
                    161:        }
                    162:        if (authenticated != 1)
                    163:                auth_clear_options();
                    164: done:
                    165:        debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
                    166:        if (key != NULL)
                    167:                key_free(key);
                    168:        xfree(pkalg);
                    169:        xfree(pkblob);
                    170:        return authenticated;
                    171: }
                    172:
                    173: /* return 1 if user allows given key */
                    174: static int
                    175: user_key_allowed2(struct passwd *pw, Key *key, char *file)
                    176: {
1.8       dtucker   177:        char line[SSH_MAX_PUBKEY_BYTES];
1.1       markus    178:        int found_key = 0;
                    179:        FILE *f;
                    180:        u_long linenum = 0;
                    181:        struct stat st;
                    182:        Key *found;
                    183:        char *fp;
                    184:
                    185:        /* Temporarily use the user's uid. */
                    186:        temporarily_use_uid(pw);
                    187:
                    188:        debug("trying public key file %s", file);
                    189:
                    190:        /* Fail quietly if file does not exist */
                    191:        if (stat(file, &st) < 0) {
                    192:                /* Restore the privileged uid. */
                    193:                restore_uid();
                    194:                return 0;
                    195:        }
                    196:        /* Open the file containing the authorized keys. */
                    197:        f = fopen(file, "r");
                    198:        if (!f) {
                    199:                /* Restore the privileged uid. */
                    200:                restore_uid();
                    201:                return 0;
                    202:        }
                    203:        if (options.strict_modes &&
                    204:            secure_filename(f, file, pw, line, sizeof(line)) != 0) {
                    205:                fclose(f);
1.3       itojun    206:                logit("Authentication refused: %s", line);
1.1       markus    207:                restore_uid();
                    208:                return 0;
                    209:        }
                    210:
                    211:        found_key = 0;
                    212:        found = key_new(key->type);
                    213:
1.8       dtucker   214:        while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.7       avsm      215:                char *cp, *key_options = NULL;
1.8       dtucker   216:
1.1       markus    217:                /* Skip leading whitespace, empty and comment lines. */
                    218:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    219:                        ;
                    220:                if (!*cp || *cp == '\n' || *cp == '#')
                    221:                        continue;
                    222:
                    223:                if (key_read(found, &cp) != 1) {
                    224:                        /* no key?  check if there are options for this key */
                    225:                        int quoted = 0;
                    226:                        debug2("user_key_allowed: check options: '%s'", cp);
1.7       avsm      227:                        key_options = cp;
1.1       markus    228:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    229:                                if (*cp == '\\' && cp[1] == '"')
                    230:                                        cp++;   /* Skip both */
                    231:                                else if (*cp == '"')
                    232:                                        quoted = !quoted;
                    233:                        }
                    234:                        /* Skip remaining whitespace. */
                    235:                        for (; *cp == ' ' || *cp == '\t'; cp++)
                    236:                                ;
                    237:                        if (key_read(found, &cp) != 1) {
                    238:                                debug2("user_key_allowed: advance: '%s'", cp);
                    239:                                /* still no key?  advance to next line*/
                    240:                                continue;
                    241:                        }
                    242:                }
                    243:                if (key_equal(found, key) &&
1.7       avsm      244:                    auth_parse_options(pw, key_options, file, linenum) == 1) {
1.1       markus    245:                        found_key = 1;
                    246:                        debug("matching key found: file %s, line %lu",
                    247:                            file, linenum);
                    248:                        fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
                    249:                        verbose("Found matching %s key: %s",
                    250:                            key_type(found), fp);
                    251:                        xfree(fp);
                    252:                        break;
                    253:                }
                    254:        }
                    255:        restore_uid();
                    256:        fclose(f);
                    257:        key_free(found);
                    258:        if (!found_key)
                    259:                debug2("key not found");
                    260:        return found_key;
                    261: }
                    262:
                    263: /* check whether given key is in .ssh/authorized_keys* */
                    264: int
                    265: user_key_allowed(struct passwd *pw, Key *key)
                    266: {
                    267:        int success;
                    268:        char *file;
                    269:
                    270:        file = authorized_keys_file(pw);
                    271:        success = user_key_allowed2(pw, key, file);
                    272:        xfree(file);
                    273:        if (success)
                    274:                return success;
                    275:
                    276:        /* try suffix "2" for backward compat, too */
                    277:        file = authorized_keys_file2(pw);
                    278:        success = user_key_allowed2(pw, key, file);
                    279:        xfree(file);
                    280:        return success;
                    281: }
1.2       markus    282:
                    283: Authmethod method_pubkey = {
                    284:        "publickey",
                    285:        userauth_pubkey,
                    286:        &options.pubkey_authentication
                    287: };