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

Annotation of src/usr.bin/ssh/auth-options.c, Revision 1.22

1.3       deraadt     1: /*
                      2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * As far as I am concerned, the code I have written for this software
                      6:  * can be used freely for any purpose.  Any derived versions of this
                      7:  * software must be clearly marked as such, and if the derived work is
                      8:  * incompatible with the protocol description in the RFC file, it must be
                      9:  * called by a name other than "ssh" or "Secure Shell".
                     10:  */
                     11:
1.1       markus     12: #include "includes.h"
1.22    ! provos     13: RCSID("$OpenBSD: auth-options.c,v 1.21 2002/01/29 14:32:03 markus Exp $");
1.1       markus     14:
                     15: #include "packet.h"
                     16: #include "xmalloc.h"
                     17: #include "match.h"
1.11      markus     18: #include "log.h"
                     19: #include "canohost.h"
1.18      markus     20: #include "channels.h"
1.11      markus     21: #include "auth-options.h"
1.12      markus     22: #include "servconf.h"
1.22    ! provos     23: #include "bufaux.h"
1.20      stevesk    24: #include "misc.h"
1.22    ! provos     25: #include "monitor_wrap.h"
        !            26:
        !            27: /* Debugging messages */
        !            28: Buffer auth_debug;
        !            29: int auth_debug_init;
1.1       markus     30:
                     31: /* Flags set authorized_keys flags */
                     32: int no_port_forwarding_flag = 0;
                     33: int no_agent_forwarding_flag = 0;
                     34: int no_x11_forwarding_flag = 0;
                     35: int no_pty_flag = 0;
                     36:
                     37: /* "command=" option. */
                     38: char *forced_command = NULL;
                     39:
                     40: /* "environment=" options. */
                     41: struct envstring *custom_environment = NULL;
                     42:
1.12      markus     43: extern ServerOptions options;
                     44:
1.5       markus     45: void
1.22    ! provos     46: auth_send_debug(Buffer *m)
        !            47: {
        !            48:        char *msg;
        !            49:
        !            50:        while (buffer_len(m)) {
        !            51:                msg = buffer_get_string(m, NULL);
        !            52:                packet_send_debug("%s", msg);
        !            53:                xfree(msg);
        !            54:        }
        !            55: }
        !            56:
        !            57: void
1.5       markus     58: auth_clear_options(void)
                     59: {
1.22    ! provos     60:        if (auth_debug_init)
        !            61:                buffer_clear(&auth_debug);
        !            62:        else {
        !            63:                buffer_init(&auth_debug);
        !            64:                auth_debug_init = 1;
        !            65:        }
        !            66:
1.5       markus     67:        no_agent_forwarding_flag = 0;
                     68:        no_port_forwarding_flag = 0;
                     69:        no_pty_flag = 0;
                     70:        no_x11_forwarding_flag = 0;
                     71:        while (custom_environment) {
                     72:                struct envstring *ce = custom_environment;
                     73:                custom_environment = ce->next;
                     74:                xfree(ce->s);
                     75:                xfree(ce);
                     76:        }
                     77:        if (forced_command) {
                     78:                xfree(forced_command);
                     79:                forced_command = NULL;
                     80:        }
1.15      markus     81:        channel_clear_permitted_opens();
1.5       markus     82: }
                     83:
1.10      markus     84: /*
                     85:  * return 1 if access is granted, 0 if not.
                     86:  * side effect: sets key option flags
                     87:  */
1.1       markus     88: int
1.12      markus     89: auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
1.1       markus     90: {
1.22    ! provos     91:        char tmp[1024];
1.1       markus     92:        const char *cp;
1.15      markus     93:        int i;
1.5       markus     94:
                     95:        /* reset options */
                     96:        auth_clear_options();
1.13      markus     97:
                     98:        if (!opts)
                     99:                return 1;
1.5       markus    100:
1.12      markus    101:        while (*opts && *opts != ' ' && *opts != '\t') {
1.1       markus    102:                cp = "no-port-forwarding";
1.12      markus    103:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.22    ! provos    104:                        snprintf(tmp, sizeof(tmp), "Port forwarding disabled.");
        !           105:                        buffer_put_cstring(&auth_debug, tmp);
1.1       markus    106:                        no_port_forwarding_flag = 1;
1.12      markus    107:                        opts += strlen(cp);
1.1       markus    108:                        goto next_option;
                    109:                }
                    110:                cp = "no-agent-forwarding";
1.12      markus    111:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.22    ! provos    112:                        snprintf(tmp, sizeof(tmp), "Agent forwarding disabled.");
        !           113:                        buffer_put_cstring(&auth_debug, tmp);
1.1       markus    114:                        no_agent_forwarding_flag = 1;
1.12      markus    115:                        opts += strlen(cp);
1.1       markus    116:                        goto next_option;
                    117:                }
                    118:                cp = "no-X11-forwarding";
1.12      markus    119:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.22    ! provos    120:                        snprintf(tmp, sizeof(tmp), "X11 forwarding disabled.");
        !           121:                        buffer_put_cstring(&auth_debug, tmp);
1.1       markus    122:                        no_x11_forwarding_flag = 1;
1.12      markus    123:                        opts += strlen(cp);
1.1       markus    124:                        goto next_option;
                    125:                }
                    126:                cp = "no-pty";
1.12      markus    127:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.22    ! provos    128:                        snprintf(tmp, sizeof(tmp), "Pty allocation disabled.");
        !           129:                        buffer_put_cstring(&auth_debug, tmp);
1.1       markus    130:                        no_pty_flag = 1;
1.12      markus    131:                        opts += strlen(cp);
1.1       markus    132:                        goto next_option;
                    133:                }
                    134:                cp = "command=\"";
1.12      markus    135:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
                    136:                        opts += strlen(cp);
                    137:                        forced_command = xmalloc(strlen(opts) + 1);
1.1       markus    138:                        i = 0;
1.12      markus    139:                        while (*opts) {
                    140:                                if (*opts == '"')
1.1       markus    141:                                        break;
1.12      markus    142:                                if (*opts == '\\' && opts[1] == '"') {
                    143:                                        opts += 2;
1.1       markus    144:                                        forced_command[i++] = '"';
                    145:                                        continue;
                    146:                                }
1.12      markus    147:                                forced_command[i++] = *opts++;
1.1       markus    148:                        }
1.12      markus    149:                        if (!*opts) {
1.1       markus    150:                                debug("%.100s, line %lu: missing end quote",
1.10      markus    151:                                    file, linenum);
1.22    ! provos    152:                                snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
1.10      markus    153:                                    file, linenum);
1.22    ! provos    154:                                buffer_put_cstring(&auth_debug, tmp);
1.14      markus    155:                                xfree(forced_command);
                    156:                                forced_command = NULL;
                    157:                                goto bad_option;
1.1       markus    158:                        }
                    159:                        forced_command[i] = 0;
1.22    ! provos    160:                        snprintf(tmp, sizeof(tmp), "Forced command: %.900s", forced_command);
        !           161:                        buffer_put_cstring(&auth_debug, tmp);
1.12      markus    162:                        opts++;
1.1       markus    163:                        goto next_option;
                    164:                }
                    165:                cp = "environment=\"";
1.12      markus    166:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.1       markus    167:                        char *s;
                    168:                        struct envstring *new_envstring;
1.15      markus    169:
1.12      markus    170:                        opts += strlen(cp);
                    171:                        s = xmalloc(strlen(opts) + 1);
1.1       markus    172:                        i = 0;
1.12      markus    173:                        while (*opts) {
                    174:                                if (*opts == '"')
1.1       markus    175:                                        break;
1.12      markus    176:                                if (*opts == '\\' && opts[1] == '"') {
                    177:                                        opts += 2;
1.1       markus    178:                                        s[i++] = '"';
                    179:                                        continue;
                    180:                                }
1.12      markus    181:                                s[i++] = *opts++;
1.1       markus    182:                        }
1.12      markus    183:                        if (!*opts) {
1.1       markus    184:                                debug("%.100s, line %lu: missing end quote",
1.10      markus    185:                                    file, linenum);
1.22    ! provos    186:                                snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
1.10      markus    187:                                    file, linenum);
1.22    ! provos    188:                                buffer_put_cstring(&auth_debug, tmp);
1.14      markus    189:                                xfree(s);
                    190:                                goto bad_option;
1.1       markus    191:                        }
                    192:                        s[i] = 0;
1.22    ! provos    193:                        snprintf(tmp, sizeof(tmp), "Adding to environment: %.900s", s);
        !           194:                        buffer_put_cstring(&auth_debug, tmp);
1.1       markus    195:                        debug("Adding to environment: %.900s", s);
1.12      markus    196:                        opts++;
1.1       markus    197:                        new_envstring = xmalloc(sizeof(struct envstring));
                    198:                        new_envstring->s = s;
                    199:                        new_envstring->next = custom_environment;
                    200:                        custom_environment = new_envstring;
                    201:                        goto next_option;
                    202:                }
                    203:                cp = "from=\"";
1.12      markus    204:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
                    205:                        const char *remote_ip = get_remote_ipaddr();
                    206:                        const char *remote_host = get_canonical_hostname(
1.21      markus    207:                            options.verify_reverse_mapping);
1.12      markus    208:                        char *patterns = xmalloc(strlen(opts) + 1);
1.15      markus    209:
1.12      markus    210:                        opts += strlen(cp);
1.1       markus    211:                        i = 0;
1.12      markus    212:                        while (*opts) {
                    213:                                if (*opts == '"')
1.1       markus    214:                                        break;
1.12      markus    215:                                if (*opts == '\\' && opts[1] == '"') {
                    216:                                        opts += 2;
1.1       markus    217:                                        patterns[i++] = '"';
                    218:                                        continue;
                    219:                                }
1.12      markus    220:                                patterns[i++] = *opts++;
1.1       markus    221:                        }
1.12      markus    222:                        if (!*opts) {
1.1       markus    223:                                debug("%.100s, line %lu: missing end quote",
1.10      markus    224:                                    file, linenum);
1.22    ! provos    225:                                snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
1.10      markus    226:                                    file, linenum);
1.22    ! provos    227:                                buffer_put_cstring(&auth_debug, tmp);
1.14      markus    228:                                xfree(patterns);
                    229:                                goto bad_option;
1.1       markus    230:                        }
                    231:                        patterns[i] = 0;
1.12      markus    232:                        opts++;
1.19      markus    233:                        if (match_host_and_ip(remote_host, remote_ip,
                    234:                            patterns) != 1) {
                    235:                                xfree(patterns);
1.12      markus    236:                                log("Authentication tried for %.100s with "
                    237:                                    "correct key but not from a permitted "
                    238:                                    "host (host=%.200s, ip=%.200s).",
                    239:                                    pw->pw_name, remote_host, remote_ip);
1.22    ! provos    240:                                snprintf(tmp, sizeof(tmp),
        !           241:                                    "Your host '%.200s' is not "
1.12      markus    242:                                    "permitted to use this key for login.",
                    243:                                    remote_host);
1.22    ! provos    244:                                buffer_put_cstring(&auth_debug, tmp);
1.1       markus    245:                                /* deny access */
                    246:                                return 0;
                    247:                        }
1.19      markus    248:                        xfree(patterns);
1.1       markus    249:                        /* Host name matches. */
1.15      markus    250:                        goto next_option;
                    251:                }
                    252:                cp = "permitopen=\"";
                    253:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.20      stevesk   254:                        char host[256], sport[6];
1.15      markus    255:                        u_short port;
                    256:                        char *patterns = xmalloc(strlen(opts) + 1);
                    257:
                    258:                        opts += strlen(cp);
                    259:                        i = 0;
                    260:                        while (*opts) {
                    261:                                if (*opts == '"')
                    262:                                        break;
                    263:                                if (*opts == '\\' && opts[1] == '"') {
                    264:                                        opts += 2;
                    265:                                        patterns[i++] = '"';
                    266:                                        continue;
                    267:                                }
                    268:                                patterns[i++] = *opts++;
                    269:                        }
                    270:                        if (!*opts) {
                    271:                                debug("%.100s, line %lu: missing end quote",
                    272:                                    file, linenum);
1.22    ! provos    273:                                snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
1.15      markus    274:                                    file, linenum);
1.22    ! provos    275:                                buffer_put_cstring(&auth_debug, tmp);
1.15      markus    276:                                xfree(patterns);
                    277:                                goto bad_option;
                    278:                        }
                    279:                        patterns[i] = 0;
                    280:                        opts++;
1.20      stevesk   281:                        if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
                    282:                            sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
                    283:                                debug("%.100s, line %lu: Bad permitopen specification "
                    284:                                    "<%.100s>", file, linenum, patterns);
1.22    ! provos    285:                                snprintf(tmp, sizeof(tmp), "%.100s, line %lu: "
1.20      stevesk   286:                                    "Bad permitopen specification", file, linenum);
1.22    ! provos    287:                                buffer_put_cstring(&auth_debug, tmp);
1.15      markus    288:                                xfree(patterns);
                    289:                                goto bad_option;
                    290:                        }
1.20      stevesk   291:                        if ((port = a2port(sport)) == 0) {
                    292:                                debug("%.100s, line %lu: Bad permitopen port <%.100s>",
                    293:                                    file, linenum, sport);
1.22    ! provos    294:                                snprintf(tmp, sizeof(tmp), "%.100s, line %lu: "
1.20      stevesk   295:                                    "Bad permitopen port", file, linenum);
1.22    ! provos    296:                                buffer_put_cstring(&auth_debug, tmp);
1.15      markus    297:                                xfree(patterns);
                    298:                                goto bad_option;
                    299:                        }
1.16      markus    300:                        if (options.allow_tcp_forwarding)
1.20      stevesk   301:                                channel_add_permitted_opens(host, port);
1.15      markus    302:                        xfree(patterns);
1.1       markus    303:                        goto next_option;
                    304:                }
                    305: next_option:
                    306:                /*
                    307:                 * Skip the comma, and move to the next option
                    308:                 * (or break out if there are no more).
                    309:                 */
1.12      markus    310:                if (!*opts)
1.1       markus    311:                        fatal("Bugs in auth-options.c option processing.");
1.12      markus    312:                if (*opts == ' ' || *opts == '\t')
1.1       markus    313:                        break;          /* End of options. */
1.12      markus    314:                if (*opts != ',')
1.1       markus    315:                        goto bad_option;
1.12      markus    316:                opts++;
1.1       markus    317:                /* Process the next option. */
                    318:        }
1.22    ! provos    319:
        !           320:        if (!use_privsep)
        !           321:                auth_send_debug(&auth_debug);
        !           322:
1.1       markus    323:        /* grant access */
                    324:        return 1;
                    325:
                    326: bad_option:
                    327:        log("Bad options in %.100s file, line %lu: %.50s",
1.12      markus    328:            file, linenum, opts);
1.22    ! provos    329:        snprintf(tmp, sizeof(tmp),
        !           330:            "Bad options in %.100s file, line %lu: %.50s",
1.12      markus    331:            file, linenum, opts);
1.22    ! provos    332:        buffer_put_cstring(&auth_debug, tmp);
        !           333:
        !           334:        if (!use_privsep)
        !           335:                auth_send_debug(&auth_debug);
        !           336:
1.1       markus    337:        /* deny access */
                    338:        return 0;
                    339: }