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

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.5.2.6 ! brad       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.5.2.1   jason      18: #include "log.h"
                     19: #include "canohost.h"
1.5.2.3   jason      20: #include "channels.h"
1.5.2.1   jason      21: #include "auth-options.h"
                     22: #include "servconf.h"
1.5.2.5   miod       23: #include "misc.h"
1.1       markus     24:
                     25: /* Flags set authorized_keys flags */
                     26: int no_port_forwarding_flag = 0;
                     27: int no_agent_forwarding_flag = 0;
                     28: int no_x11_forwarding_flag = 0;
                     29: int no_pty_flag = 0;
                     30:
                     31: /* "command=" option. */
                     32: char *forced_command = NULL;
                     33:
                     34: /* "environment=" options. */
                     35: struct envstring *custom_environment = NULL;
                     36:
1.5.2.1   jason      37: extern ServerOptions options;
                     38:
1.5       markus     39: void
                     40: auth_clear_options(void)
                     41: {
                     42:        no_agent_forwarding_flag = 0;
                     43:        no_port_forwarding_flag = 0;
                     44:        no_pty_flag = 0;
                     45:        no_x11_forwarding_flag = 0;
                     46:        while (custom_environment) {
                     47:                struct envstring *ce = custom_environment;
                     48:                custom_environment = ce->next;
                     49:                xfree(ce->s);
                     50:                xfree(ce);
                     51:        }
                     52:        if (forced_command) {
                     53:                xfree(forced_command);
                     54:                forced_command = NULL;
                     55:        }
1.5.2.3   jason      56:        channel_clear_permitted_opens();
1.5       markus     57: }
                     58:
1.5.2.1   jason      59: /*
                     60:  * return 1 if access is granted, 0 if not.
                     61:  * side effect: sets key option flags
                     62:  */
1.1       markus     63: int
1.5.2.1   jason      64: auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
1.1       markus     65: {
                     66:        const char *cp;
1.5.2.3   jason      67:        int i;
1.5       markus     68:
                     69:        /* reset options */
                     70:        auth_clear_options();
                     71:
1.5.2.1   jason      72:        if (!opts)
                     73:                return 1;
                     74:
                     75:        while (*opts && *opts != ' ' && *opts != '\t') {
1.1       markus     76:                cp = "no-port-forwarding";
1.5.2.1   jason      77:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.1       markus     78:                        packet_send_debug("Port forwarding disabled.");
                     79:                        no_port_forwarding_flag = 1;
1.5.2.1   jason      80:                        opts += strlen(cp);
1.1       markus     81:                        goto next_option;
                     82:                }
                     83:                cp = "no-agent-forwarding";
1.5.2.1   jason      84:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.1       markus     85:                        packet_send_debug("Agent forwarding disabled.");
                     86:                        no_agent_forwarding_flag = 1;
1.5.2.1   jason      87:                        opts += strlen(cp);
1.1       markus     88:                        goto next_option;
                     89:                }
                     90:                cp = "no-X11-forwarding";
1.5.2.1   jason      91:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.1       markus     92:                        packet_send_debug("X11 forwarding disabled.");
                     93:                        no_x11_forwarding_flag = 1;
1.5.2.1   jason      94:                        opts += strlen(cp);
1.1       markus     95:                        goto next_option;
                     96:                }
                     97:                cp = "no-pty";
1.5.2.1   jason      98:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.1       markus     99:                        packet_send_debug("Pty allocation disabled.");
                    100:                        no_pty_flag = 1;
1.5.2.1   jason     101:                        opts += strlen(cp);
1.1       markus    102:                        goto next_option;
                    103:                }
                    104:                cp = "command=\"";
1.5.2.1   jason     105:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
                    106:                        opts += strlen(cp);
                    107:                        forced_command = xmalloc(strlen(opts) + 1);
1.1       markus    108:                        i = 0;
1.5.2.1   jason     109:                        while (*opts) {
                    110:                                if (*opts == '"')
1.1       markus    111:                                        break;
1.5.2.1   jason     112:                                if (*opts == '\\' && opts[1] == '"') {
                    113:                                        opts += 2;
1.1       markus    114:                                        forced_command[i++] = '"';
                    115:                                        continue;
                    116:                                }
1.5.2.1   jason     117:                                forced_command[i++] = *opts++;
1.1       markus    118:                        }
1.5.2.1   jason     119:                        if (!*opts) {
1.1       markus    120:                                debug("%.100s, line %lu: missing end quote",
1.5.2.1   jason     121:                                    file, linenum);
1.1       markus    122:                                packet_send_debug("%.100s, line %lu: missing end quote",
1.5.2.1   jason     123:                                    file, linenum);
1.5.2.3   jason     124:                                xfree(forced_command);
                    125:                                forced_command = NULL;
                    126:                                goto bad_option;
1.1       markus    127:                        }
                    128:                        forced_command[i] = 0;
                    129:                        packet_send_debug("Forced command: %.900s", forced_command);
1.5.2.1   jason     130:                        opts++;
1.1       markus    131:                        goto next_option;
                    132:                }
                    133:                cp = "environment=\"";
1.5.2.1   jason     134:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.1       markus    135:                        char *s;
                    136:                        struct envstring *new_envstring;
1.5.2.3   jason     137:
1.5.2.1   jason     138:                        opts += strlen(cp);
                    139:                        s = xmalloc(strlen(opts) + 1);
1.1       markus    140:                        i = 0;
1.5.2.1   jason     141:                        while (*opts) {
                    142:                                if (*opts == '"')
1.1       markus    143:                                        break;
1.5.2.1   jason     144:                                if (*opts == '\\' && opts[1] == '"') {
                    145:                                        opts += 2;
1.1       markus    146:                                        s[i++] = '"';
                    147:                                        continue;
                    148:                                }
1.5.2.1   jason     149:                                s[i++] = *opts++;
1.1       markus    150:                        }
1.5.2.1   jason     151:                        if (!*opts) {
1.1       markus    152:                                debug("%.100s, line %lu: missing end quote",
1.5.2.1   jason     153:                                    file, linenum);
1.1       markus    154:                                packet_send_debug("%.100s, line %lu: missing end quote",
1.5.2.1   jason     155:                                    file, linenum);
1.5.2.3   jason     156:                                xfree(s);
                    157:                                goto bad_option;
1.1       markus    158:                        }
                    159:                        s[i] = 0;
                    160:                        packet_send_debug("Adding to environment: %.900s", s);
                    161:                        debug("Adding to environment: %.900s", s);
1.5.2.1   jason     162:                        opts++;
1.1       markus    163:                        new_envstring = xmalloc(sizeof(struct envstring));
                    164:                        new_envstring->s = s;
                    165:                        new_envstring->next = custom_environment;
                    166:                        custom_environment = new_envstring;
                    167:                        goto next_option;
                    168:                }
                    169:                cp = "from=\"";
1.5.2.1   jason     170:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
                    171:                        const char *remote_ip = get_remote_ipaddr();
                    172:                        const char *remote_host = get_canonical_hostname(
1.5.2.6 ! brad      173:                            options.verify_reverse_mapping);
1.5.2.1   jason     174:                        char *patterns = xmalloc(strlen(opts) + 1);
1.5.2.3   jason     175:
1.5.2.1   jason     176:                        opts += strlen(cp);
1.1       markus    177:                        i = 0;
1.5.2.1   jason     178:                        while (*opts) {
                    179:                                if (*opts == '"')
1.1       markus    180:                                        break;
1.5.2.1   jason     181:                                if (*opts == '\\' && opts[1] == '"') {
                    182:                                        opts += 2;
1.1       markus    183:                                        patterns[i++] = '"';
                    184:                                        continue;
                    185:                                }
1.5.2.1   jason     186:                                patterns[i++] = *opts++;
1.1       markus    187:                        }
1.5.2.1   jason     188:                        if (!*opts) {
1.1       markus    189:                                debug("%.100s, line %lu: missing end quote",
1.5.2.1   jason     190:                                    file, linenum);
1.1       markus    191:                                packet_send_debug("%.100s, line %lu: missing end quote",
1.5.2.1   jason     192:                                    file, linenum);
1.5.2.3   jason     193:                                xfree(patterns);
                    194:                                goto bad_option;
1.1       markus    195:                        }
                    196:                        patterns[i] = 0;
1.5.2.1   jason     197:                        opts++;
1.5.2.5   miod      198:                        if (match_host_and_ip(remote_host, remote_ip,
                    199:                            patterns) != 1) {
                    200:                                xfree(patterns);
1.5.2.1   jason     201:                                log("Authentication tried for %.100s with "
                    202:                                    "correct key but not from a permitted "
                    203:                                    "host (host=%.200s, ip=%.200s).",
                    204:                                    pw->pw_name, remote_host, remote_ip);
                    205:                                packet_send_debug("Your host '%.200s' is not "
                    206:                                    "permitted to use this key for login.",
                    207:                                    remote_host);
1.1       markus    208:                                /* deny access */
                    209:                                return 0;
                    210:                        }
1.5.2.5   miod      211:                        xfree(patterns);
1.1       markus    212:                        /* Host name matches. */
1.5.2.3   jason     213:                        goto next_option;
                    214:                }
                    215:                cp = "permitopen=\"";
                    216:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.5.2.5   miod      217:                        char host[256], sport[6];
1.5.2.3   jason     218:                        u_short port;
                    219:                        char *patterns = xmalloc(strlen(opts) + 1);
                    220:
                    221:                        opts += strlen(cp);
                    222:                        i = 0;
                    223:                        while (*opts) {
                    224:                                if (*opts == '"')
                    225:                                        break;
                    226:                                if (*opts == '\\' && opts[1] == '"') {
                    227:                                        opts += 2;
                    228:                                        patterns[i++] = '"';
                    229:                                        continue;
                    230:                                }
                    231:                                patterns[i++] = *opts++;
                    232:                        }
                    233:                        if (!*opts) {
                    234:                                debug("%.100s, line %lu: missing end quote",
                    235:                                    file, linenum);
                    236:                                packet_send_debug("%.100s, line %lu: missing end quote",
                    237:                                    file, linenum);
                    238:                                xfree(patterns);
                    239:                                goto bad_option;
                    240:                        }
                    241:                        patterns[i] = 0;
                    242:                        opts++;
1.5.2.5   miod      243:                        if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
                    244:                            sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
                    245:                                debug("%.100s, line %lu: Bad permitopen specification "
                    246:                                    "<%.100s>", file, linenum, patterns);
                    247:                                packet_send_debug("%.100s, line %lu: "
                    248:                                    "Bad permitopen specification", file, linenum);
1.5.2.3   jason     249:                                xfree(patterns);
                    250:                                goto bad_option;
                    251:                        }
1.5.2.5   miod      252:                        if ((port = a2port(sport)) == 0) {
                    253:                                debug("%.100s, line %lu: Bad permitopen port <%.100s>",
                    254:                                    file, linenum, sport);
                    255:                                packet_send_debug("%.100s, line %lu: "
                    256:                                    "Bad permitopen port", file, linenum);
1.5.2.3   jason     257:                                xfree(patterns);
                    258:                                goto bad_option;
                    259:                        }
                    260:                        if (options.allow_tcp_forwarding)
1.5.2.5   miod      261:                                channel_add_permitted_opens(host, port);
1.5.2.3   jason     262:                        xfree(patterns);
1.1       markus    263:                        goto next_option;
                    264:                }
                    265: next_option:
                    266:                /*
                    267:                 * Skip the comma, and move to the next option
                    268:                 * (or break out if there are no more).
                    269:                 */
1.5.2.1   jason     270:                if (!*opts)
1.1       markus    271:                        fatal("Bugs in auth-options.c option processing.");
1.5.2.1   jason     272:                if (*opts == ' ' || *opts == '\t')
1.1       markus    273:                        break;          /* End of options. */
1.5.2.1   jason     274:                if (*opts != ',')
1.1       markus    275:                        goto bad_option;
1.5.2.1   jason     276:                opts++;
1.1       markus    277:                /* Process the next option. */
                    278:        }
                    279:        /* grant access */
                    280:        return 1;
                    281:
                    282: bad_option:
                    283:        log("Bad options in %.100s file, line %lu: %.50s",
1.5.2.1   jason     284:            file, linenum, opts);
1.1       markus    285:        packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
1.5.2.1   jason     286:            file, linenum, opts);
1.1       markus    287:        /* deny access */
                    288:        return 0;
                    289: }