blob: bfb1af86b32bb957a1038c78fc8a3c67c7f8d607 [file] [log] [blame]
Damien Millere4340be2000-09-16 13:29:08 +11001/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 * 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
Damien Millerf6d9e222000-06-18 14:50:44 +100012#include "includes.h"
Damien Miller056ddf72001-03-14 10:15:20 +110013RCSID("$OpenBSD: auth-options.c,v 1.14 2001/03/13 17:34:42 markus Exp $");
Damien Millerf6d9e222000-06-18 14:50:44 +100014
Damien Millerf6d9e222000-06-18 14:50:44 +100015#include "packet.h"
16#include "xmalloc.h"
17#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000018#include "log.h"
19#include "canohost.h"
20#include "auth-options.h"
Damien Miller33804262001-02-04 23:20:18 +110021#include "servconf.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100022
23/* Flags set authorized_keys flags */
24int no_port_forwarding_flag = 0;
25int no_agent_forwarding_flag = 0;
26int no_x11_forwarding_flag = 0;
27int no_pty_flag = 0;
28
29/* "command=" option. */
30char *forced_command = NULL;
31
32/* "environment=" options. */
33struct envstring *custom_environment = NULL;
34
Damien Miller33804262001-02-04 23:20:18 +110035extern ServerOptions options;
36
Damien Miller874d77b2000-10-14 16:23:11 +110037void
38auth_clear_options(void)
39{
40 no_agent_forwarding_flag = 0;
41 no_port_forwarding_flag = 0;
42 no_pty_flag = 0;
43 no_x11_forwarding_flag = 0;
44 while (custom_environment) {
45 struct envstring *ce = custom_environment;
46 custom_environment = ce->next;
47 xfree(ce->s);
48 xfree(ce);
49 }
50 if (forced_command) {
51 xfree(forced_command);
52 forced_command = NULL;
53 }
54}
55
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056/*
57 * return 1 if access is granted, 0 if not.
58 * side effect: sets key option flags
59 */
Damien Millerf6d9e222000-06-18 14:50:44 +100060int
Damien Miller33804262001-02-04 23:20:18 +110061auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100062{
63 const char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +110064
65 /* reset options */
66 auth_clear_options();
67
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000068 if (!opts)
69 return 1;
70
Damien Miller33804262001-02-04 23:20:18 +110071 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100072 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110073 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100074 packet_send_debug("Port forwarding disabled.");
75 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110076 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100077 goto next_option;
78 }
79 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110080 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100081 packet_send_debug("Agent forwarding disabled.");
82 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110083 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100084 goto next_option;
85 }
86 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110087 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100088 packet_send_debug("X11 forwarding disabled.");
89 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110090 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100091 goto next_option;
92 }
93 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +110094 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100095 packet_send_debug("Pty allocation disabled.");
96 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110097 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100098 goto next_option;
99 }
100 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100101 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000102 int i;
Damien Miller33804262001-02-04 23:20:18 +1100103 opts += strlen(cp);
104 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000105 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100106 while (*opts) {
107 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000108 break;
Damien Miller33804262001-02-04 23:20:18 +1100109 if (*opts == '\\' && opts[1] == '"') {
110 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000111 forced_command[i++] = '"';
112 continue;
113 }
Damien Miller33804262001-02-04 23:20:18 +1100114 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000115 }
Damien Miller33804262001-02-04 23:20:18 +1100116 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000117 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000118 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000119 packet_send_debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000120 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100121 xfree(forced_command);
122 forced_command = NULL;
123 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000124 }
125 forced_command[i] = 0;
126 packet_send_debug("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100127 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000128 goto next_option;
129 }
130 cp = "environment=\"";
Damien Miller33804262001-02-04 23:20:18 +1100131 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000132 int i;
133 char *s;
134 struct envstring *new_envstring;
Damien Miller33804262001-02-04 23:20:18 +1100135 opts += strlen(cp);
136 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000137 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100138 while (*opts) {
139 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000140 break;
Damien Miller33804262001-02-04 23:20:18 +1100141 if (*opts == '\\' && opts[1] == '"') {
142 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000143 s[i++] = '"';
144 continue;
145 }
Damien Miller33804262001-02-04 23:20:18 +1100146 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000147 }
Damien Miller33804262001-02-04 23:20:18 +1100148 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000150 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000151 packet_send_debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000152 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100153 xfree(s);
154 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000155 }
156 s[i] = 0;
157 packet_send_debug("Adding to environment: %.900s", s);
158 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100159 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000160 new_envstring = xmalloc(sizeof(struct envstring));
161 new_envstring->s = s;
162 new_envstring->next = custom_environment;
163 custom_environment = new_envstring;
164 goto next_option;
165 }
166 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100167 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000168 int mname, mip;
Damien Miller33804262001-02-04 23:20:18 +1100169 const char *remote_ip = get_remote_ipaddr();
170 const char *remote_host = get_canonical_hostname(
171 options.reverse_mapping_check);
172 char *patterns = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000173 int i;
Damien Miller33804262001-02-04 23:20:18 +1100174 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000175 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100176 while (*opts) {
177 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000178 break;
Damien Miller33804262001-02-04 23:20:18 +1100179 if (*opts == '\\' && opts[1] == '"') {
180 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000181 patterns[i++] = '"';
182 continue;
183 }
Damien Miller33804262001-02-04 23:20:18 +1100184 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000185 }
Damien Miller33804262001-02-04 23:20:18 +1100186 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000187 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000188 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000189 packet_send_debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000190 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100191 xfree(patterns);
192 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000193 }
194 patterns[i] = 0;
Damien Miller33804262001-02-04 23:20:18 +1100195 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000196 /*
197 * Deny access if we get a negative
198 * match for the hostname or the ip
199 * or if we get not match at all
200 */
Damien Miller33804262001-02-04 23:20:18 +1100201 mname = match_hostname(remote_host, patterns,
202 strlen(patterns));
203 mip = match_hostname(remote_ip, patterns,
204 strlen(patterns));
Damien Millerf6d9e222000-06-18 14:50:44 +1000205 xfree(patterns);
206 if (mname == -1 || mip == -1 ||
207 (mname != 1 && mip != 1)) {
Damien Miller33804262001-02-04 23:20:18 +1100208 log("Authentication tried for %.100s with "
209 "correct key but not from a permitted "
210 "host (host=%.200s, ip=%.200s).",
211 pw->pw_name, remote_host, remote_ip);
212 packet_send_debug("Your host '%.200s' is not "
213 "permitted to use this key for login.",
214 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000215 /* deny access */
216 return 0;
217 }
218 /* Host name matches. */
219 goto next_option;
220 }
221next_option:
222 /*
223 * Skip the comma, and move to the next option
224 * (or break out if there are no more).
225 */
Damien Miller33804262001-02-04 23:20:18 +1100226 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000227 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100228 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000229 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100230 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000231 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100232 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000233 /* Process the next option. */
234 }
235 /* grant access */
236 return 1;
237
238bad_option:
239 log("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100240 file, linenum, opts);
Damien Millerf6d9e222000-06-18 14:50:44 +1000241 packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100242 file, linenum, opts);
Damien Millerf6d9e222000-06-18 14:50:44 +1000243 /* deny access */
244 return 0;
245}