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