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