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