Annotation of src/usr.bin/ssh/auth-options.c, Revision 1.20
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.20 ! stevesk 13: RCSID("$OpenBSD: auth-options.c,v 1.19 2001/06/24 05:25:09 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.20 ! stevesk 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.12 markus 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.15 markus 56: channel_clear_permitted_opens();
1.5 markus 57: }
58:
1.10 markus 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.12 markus 64: auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
1.1 markus 65: {
66: const char *cp;
1.15 markus 67: int i;
1.5 markus 68:
69: /* reset options */
70: auth_clear_options();
1.13 markus 71:
72: if (!opts)
73: return 1;
1.5 markus 74:
1.12 markus 75: while (*opts && *opts != ' ' && *opts != '\t') {
1.1 markus 76: cp = "no-port-forwarding";
1.12 markus 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.12 markus 80: opts += strlen(cp);
1.1 markus 81: goto next_option;
82: }
83: cp = "no-agent-forwarding";
1.12 markus 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.12 markus 87: opts += strlen(cp);
1.1 markus 88: goto next_option;
89: }
90: cp = "no-X11-forwarding";
1.12 markus 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.12 markus 94: opts += strlen(cp);
1.1 markus 95: goto next_option;
96: }
97: cp = "no-pty";
1.12 markus 98: if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.1 markus 99: packet_send_debug("Pty allocation disabled.");
100: no_pty_flag = 1;
1.12 markus 101: opts += strlen(cp);
1.1 markus 102: goto next_option;
103: }
104: cp = "command=\"";
1.12 markus 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.12 markus 109: while (*opts) {
110: if (*opts == '"')
1.1 markus 111: break;
1.12 markus 112: if (*opts == '\\' && opts[1] == '"') {
113: opts += 2;
1.1 markus 114: forced_command[i++] = '"';
115: continue;
116: }
1.12 markus 117: forced_command[i++] = *opts++;
1.1 markus 118: }
1.12 markus 119: if (!*opts) {
1.1 markus 120: debug("%.100s, line %lu: missing end quote",
1.10 markus 121: file, linenum);
1.1 markus 122: packet_send_debug("%.100s, line %lu: missing end quote",
1.10 markus 123: file, linenum);
1.14 markus 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.12 markus 130: opts++;
1.1 markus 131: goto next_option;
132: }
133: cp = "environment=\"";
1.12 markus 134: if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.1 markus 135: char *s;
136: struct envstring *new_envstring;
1.15 markus 137:
1.12 markus 138: opts += strlen(cp);
139: s = xmalloc(strlen(opts) + 1);
1.1 markus 140: i = 0;
1.12 markus 141: while (*opts) {
142: if (*opts == '"')
1.1 markus 143: break;
1.12 markus 144: if (*opts == '\\' && opts[1] == '"') {
145: opts += 2;
1.1 markus 146: s[i++] = '"';
147: continue;
148: }
1.12 markus 149: s[i++] = *opts++;
1.1 markus 150: }
1.12 markus 151: if (!*opts) {
1.1 markus 152: debug("%.100s, line %lu: missing end quote",
1.10 markus 153: file, linenum);
1.1 markus 154: packet_send_debug("%.100s, line %lu: missing end quote",
1.10 markus 155: file, linenum);
1.14 markus 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.12 markus 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.12 markus 170: if (strncasecmp(opts, cp, strlen(cp)) == 0) {
171: const char *remote_ip = get_remote_ipaddr();
172: const char *remote_host = get_canonical_hostname(
173: options.reverse_mapping_check);
174: char *patterns = xmalloc(strlen(opts) + 1);
1.15 markus 175:
1.12 markus 176: opts += strlen(cp);
1.1 markus 177: i = 0;
1.12 markus 178: while (*opts) {
179: if (*opts == '"')
1.1 markus 180: break;
1.12 markus 181: if (*opts == '\\' && opts[1] == '"') {
182: opts += 2;
1.1 markus 183: patterns[i++] = '"';
184: continue;
185: }
1.12 markus 186: patterns[i++] = *opts++;
1.1 markus 187: }
1.12 markus 188: if (!*opts) {
1.1 markus 189: debug("%.100s, line %lu: missing end quote",
1.10 markus 190: file, linenum);
1.1 markus 191: packet_send_debug("%.100s, line %lu: missing end quote",
1.10 markus 192: file, linenum);
1.14 markus 193: xfree(patterns);
194: goto bad_option;
1.1 markus 195: }
196: patterns[i] = 0;
1.12 markus 197: opts++;
1.19 markus 198: if (match_host_and_ip(remote_host, remote_ip,
199: patterns) != 1) {
200: xfree(patterns);
1.12 markus 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.19 markus 211: xfree(patterns);
1.1 markus 212: /* Host name matches. */
1.15 markus 213: goto next_option;
214: }
215: cp = "permitopen=\"";
216: if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.20 ! stevesk 217: char host[256], sport[6];
1.15 markus 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.20 ! stevesk 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.15 markus 249: xfree(patterns);
250: goto bad_option;
251: }
1.20 ! stevesk 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.15 markus 257: xfree(patterns);
258: goto bad_option;
259: }
1.16 markus 260: if (options.allow_tcp_forwarding)
1.20 ! stevesk 261: channel_add_permitted_opens(host, port);
1.15 markus 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.12 markus 270: if (!*opts)
1.1 markus 271: fatal("Bugs in auth-options.c option processing.");
1.12 markus 272: if (*opts == ' ' || *opts == '\t')
1.1 markus 273: break; /* End of options. */
1.12 markus 274: if (*opts != ',')
1.1 markus 275: goto bad_option;
1.12 markus 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.12 markus 284: file, linenum, opts);
1.1 markus 285: packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
1.12 markus 286: file, linenum, opts);
1.1 markus 287: /* deny access */
288: return 0;
289: }