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

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.10    ! markus     13: RCSID("$OpenBSD: auth-options.c,v 1.9 2001/01/19 15:55:10 markus Exp $");
1.1       markus     14:
                     15: #include "ssh.h"
                     16: #include "packet.h"
                     17: #include "xmalloc.h"
                     18: #include "match.h"
                     19:
                     20: /* Flags set authorized_keys flags */
                     21: int no_port_forwarding_flag = 0;
                     22: int no_agent_forwarding_flag = 0;
                     23: int no_x11_forwarding_flag = 0;
                     24: int no_pty_flag = 0;
                     25:
                     26: /* "command=" option. */
                     27: char *forced_command = NULL;
                     28:
                     29: /* "environment=" options. */
                     30: struct envstring *custom_environment = NULL;
                     31:
1.5       markus     32: void
                     33: auth_clear_options(void)
                     34: {
                     35:        no_agent_forwarding_flag = 0;
                     36:        no_port_forwarding_flag = 0;
                     37:        no_pty_flag = 0;
                     38:        no_x11_forwarding_flag = 0;
                     39:        while (custom_environment) {
                     40:                struct envstring *ce = custom_environment;
                     41:                custom_environment = ce->next;
                     42:                xfree(ce->s);
                     43:                xfree(ce);
                     44:        }
                     45:        if (forced_command) {
                     46:                xfree(forced_command);
                     47:                forced_command = NULL;
                     48:        }
                     49: }
                     50:
1.10    ! markus     51: /*
        !            52:  * return 1 if access is granted, 0 if not.
        !            53:  * side effect: sets key option flags
        !            54:  */
1.1       markus     55: int
1.10    ! markus     56: auth_parse_options(struct passwd *pw, char *options, char *file, u_long linenum)
1.1       markus     57: {
                     58:        const char *cp;
                     59:        if (!options)
                     60:                return 1;
1.5       markus     61:
                     62:        /* reset options */
                     63:        auth_clear_options();
                     64:
1.1       markus     65:        while (*options && *options != ' ' && *options != '\t') {
                     66:                cp = "no-port-forwarding";
1.6       markus     67:                if (strncasecmp(options, cp, strlen(cp)) == 0) {
1.1       markus     68:                        packet_send_debug("Port forwarding disabled.");
                     69:                        no_port_forwarding_flag = 1;
                     70:                        options += strlen(cp);
                     71:                        goto next_option;
                     72:                }
                     73:                cp = "no-agent-forwarding";
1.6       markus     74:                if (strncasecmp(options, cp, strlen(cp)) == 0) {
1.1       markus     75:                        packet_send_debug("Agent forwarding disabled.");
                     76:                        no_agent_forwarding_flag = 1;
                     77:                        options += strlen(cp);
                     78:                        goto next_option;
                     79:                }
                     80:                cp = "no-X11-forwarding";
1.6       markus     81:                if (strncasecmp(options, cp, strlen(cp)) == 0) {
1.1       markus     82:                        packet_send_debug("X11 forwarding disabled.");
                     83:                        no_x11_forwarding_flag = 1;
                     84:                        options += strlen(cp);
                     85:                        goto next_option;
                     86:                }
                     87:                cp = "no-pty";
1.6       markus     88:                if (strncasecmp(options, cp, strlen(cp)) == 0) {
1.1       markus     89:                        packet_send_debug("Pty allocation disabled.");
                     90:                        no_pty_flag = 1;
                     91:                        options += strlen(cp);
                     92:                        goto next_option;
                     93:                }
                     94:                cp = "command=\"";
1.6       markus     95:                if (strncasecmp(options, cp, strlen(cp)) == 0) {
1.1       markus     96:                        int i;
                     97:                        options += strlen(cp);
                     98:                        forced_command = xmalloc(strlen(options) + 1);
                     99:                        i = 0;
                    100:                        while (*options) {
                    101:                                if (*options == '"')
                    102:                                        break;
                    103:                                if (*options == '\\' && options[1] == '"') {
                    104:                                        options += 2;
                    105:                                        forced_command[i++] = '"';
                    106:                                        continue;
                    107:                                }
                    108:                                forced_command[i++] = *options++;
                    109:                        }
                    110:                        if (!*options) {
                    111:                                debug("%.100s, line %lu: missing end quote",
1.10    ! markus    112:                                    file, linenum);
1.1       markus    113:                                packet_send_debug("%.100s, line %lu: missing end quote",
1.10    ! markus    114:                                    file, linenum);
1.1       markus    115:                                continue;
                    116:                        }
                    117:                        forced_command[i] = 0;
                    118:                        packet_send_debug("Forced command: %.900s", forced_command);
                    119:                        options++;
                    120:                        goto next_option;
                    121:                }
                    122:                cp = "environment=\"";
1.6       markus    123:                if (strncasecmp(options, cp, strlen(cp)) == 0) {
1.1       markus    124:                        int i;
                    125:                        char *s;
                    126:                        struct envstring *new_envstring;
                    127:                        options += strlen(cp);
                    128:                        s = xmalloc(strlen(options) + 1);
                    129:                        i = 0;
                    130:                        while (*options) {
                    131:                                if (*options == '"')
                    132:                                        break;
                    133:                                if (*options == '\\' && options[1] == '"') {
                    134:                                        options += 2;
                    135:                                        s[i++] = '"';
                    136:                                        continue;
                    137:                                }
                    138:                                s[i++] = *options++;
                    139:                        }
                    140:                        if (!*options) {
                    141:                                debug("%.100s, line %lu: missing end quote",
1.10    ! markus    142:                                    file, linenum);
1.1       markus    143:                                packet_send_debug("%.100s, line %lu: missing end quote",
1.10    ! markus    144:                                    file, linenum);
1.1       markus    145:                                continue;
                    146:                        }
                    147:                        s[i] = 0;
                    148:                        packet_send_debug("Adding to environment: %.900s", s);
                    149:                        debug("Adding to environment: %.900s", s);
                    150:                        options++;
                    151:                        new_envstring = xmalloc(sizeof(struct envstring));
                    152:                        new_envstring->s = s;
                    153:                        new_envstring->next = custom_environment;
                    154:                        custom_environment = new_envstring;
                    155:                        goto next_option;
                    156:                }
                    157:                cp = "from=\"";
1.6       markus    158:                if (strncasecmp(options, cp, strlen(cp)) == 0) {
1.1       markus    159:                        int mname, mip;
                    160:                        char *patterns = xmalloc(strlen(options) + 1);
                    161:                        int i;
                    162:                        options += strlen(cp);
                    163:                        i = 0;
                    164:                        while (*options) {
                    165:                                if (*options == '"')
                    166:                                        break;
                    167:                                if (*options == '\\' && options[1] == '"') {
                    168:                                        options += 2;
                    169:                                        patterns[i++] = '"';
                    170:                                        continue;
                    171:                                }
                    172:                                patterns[i++] = *options++;
                    173:                        }
                    174:                        if (!*options) {
                    175:                                debug("%.100s, line %lu: missing end quote",
1.10    ! markus    176:                                    file, linenum);
1.1       markus    177:                                packet_send_debug("%.100s, line %lu: missing end quote",
1.10    ! markus    178:                                    file, linenum);
1.1       markus    179:                                continue;
                    180:                        }
                    181:                        patterns[i] = 0;
                    182:                        options++;
                    183:                        /*
                    184:                         * Deny access if we get a negative
                    185:                         * match for the hostname or the ip
                    186:                         * or if we get not match at all
                    187:                         */
                    188:                        mname = match_hostname(get_canonical_hostname(),
                    189:                            patterns, strlen(patterns));
                    190:                        mip = match_hostname(get_remote_ipaddr(),
                    191:                            patterns, strlen(patterns));
                    192:                        xfree(patterns);
                    193:                        if (mname == -1 || mip == -1 ||
                    194:                            (mname != 1 && mip != 1)) {
                    195:                                log("Authentication tried for %.100s with correct key but not from a permitted host (host=%.200s, ip=%.200s).",
                    196:                                    pw->pw_name, get_canonical_hostname(),
                    197:                                    get_remote_ipaddr());
                    198:                                packet_send_debug("Your host '%.200s' is not permitted to use this key for login.",
                    199:                                get_canonical_hostname());
                    200:                                /* deny access */
                    201:                                return 0;
                    202:                        }
                    203:                        /* Host name matches. */
                    204:                        goto next_option;
                    205:                }
                    206: next_option:
                    207:                /*
                    208:                 * Skip the comma, and move to the next option
                    209:                 * (or break out if there are no more).
                    210:                 */
                    211:                if (!*options)
                    212:                        fatal("Bugs in auth-options.c option processing.");
                    213:                if (*options == ' ' || *options == '\t')
                    214:                        break;          /* End of options. */
                    215:                if (*options != ',')
                    216:                        goto bad_option;
                    217:                options++;
                    218:                /* Process the next option. */
                    219:        }
                    220:        /* grant access */
                    221:        return 1;
                    222:
                    223: bad_option:
                    224:        log("Bad options in %.100s file, line %lu: %.50s",
1.10    ! markus    225:            file, linenum, options);
1.1       markus    226:        packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
1.10    ! markus    227:            file, linenum, options);
1.1       markus    228:        /* deny access */
                    229:        return 0;
                    230: }