[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.37

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