blob: 57e335f3f19ca4fc495a20c95d1c1a071caddc1f [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"
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000013RCSID("$OpenBSD: auth-options.c,v 1.13 2001/02/09 13:38:07 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 Millerf6d9e222000-06-18 14:50:44 +1000121 continue;
122 }
123 forced_command[i] = 0;
124 packet_send_debug("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100125 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000126 goto next_option;
127 }
128 cp = "environment=\"";
Damien Miller33804262001-02-04 23:20:18 +1100129 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000130 int i;
131 char *s;
132 struct envstring *new_envstring;
Damien Miller33804262001-02-04 23:20:18 +1100133 opts += strlen(cp);
134 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000135 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100136 while (*opts) {
137 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000138 break;
Damien Miller33804262001-02-04 23:20:18 +1100139 if (*opts == '\\' && opts[1] == '"') {
140 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000141 s[i++] = '"';
142 continue;
143 }
Damien Miller33804262001-02-04 23:20:18 +1100144 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000145 }
Damien Miller33804262001-02-04 23:20:18 +1100146 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000147 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000148 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 packet_send_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 continue;
152 }
153 s[i] = 0;
154 packet_send_debug("Adding to environment: %.900s", s);
155 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100156 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000157 new_envstring = xmalloc(sizeof(struct envstring));
158 new_envstring->s = s;
159 new_envstring->next = custom_environment;
160 custom_environment = new_envstring;
161 goto next_option;
162 }
163 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100164 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000165 int mname, mip;
Damien Miller33804262001-02-04 23:20:18 +1100166 const char *remote_ip = get_remote_ipaddr();
167 const char *remote_host = get_canonical_hostname(
168 options.reverse_mapping_check);
169 char *patterns = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000170 int i;
Damien Miller33804262001-02-04 23:20:18 +1100171 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000172 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100173 while (*opts) {
174 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000175 break;
Damien Miller33804262001-02-04 23:20:18 +1100176 if (*opts == '\\' && opts[1] == '"') {
177 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000178 patterns[i++] = '"';
179 continue;
180 }
Damien Miller33804262001-02-04 23:20:18 +1100181 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000182 }
Damien Miller33804262001-02-04 23:20:18 +1100183 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000184 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000185 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000186 packet_send_debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000187 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000188 continue;
189 }
190 patterns[i] = 0;
Damien Miller33804262001-02-04 23:20:18 +1100191 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000192 /*
193 * Deny access if we get a negative
194 * match for the hostname or the ip
195 * or if we get not match at all
196 */
Damien Miller33804262001-02-04 23:20:18 +1100197 mname = match_hostname(remote_host, patterns,
198 strlen(patterns));
199 mip = match_hostname(remote_ip, patterns,
200 strlen(patterns));
Damien Millerf6d9e222000-06-18 14:50:44 +1000201 xfree(patterns);
202 if (mname == -1 || mip == -1 ||
203 (mname != 1 && mip != 1)) {
Damien Miller33804262001-02-04 23:20:18 +1100204 log("Authentication tried for %.100s with "
205 "correct key but not from a permitted "
206 "host (host=%.200s, ip=%.200s).",
207 pw->pw_name, remote_host, remote_ip);
208 packet_send_debug("Your host '%.200s' is not "
209 "permitted to use this key for login.",
210 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000211 /* deny access */
212 return 0;
213 }
214 /* Host name matches. */
215 goto next_option;
216 }
217next_option:
218 /*
219 * Skip the comma, and move to the next option
220 * (or break out if there are no more).
221 */
Damien Miller33804262001-02-04 23:20:18 +1100222 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000223 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100224 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000225 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100226 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000227 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100228 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000229 /* Process the next option. */
230 }
231 /* grant access */
232 return 1;
233
234bad_option:
235 log("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100236 file, linenum, opts);
Damien Millerf6d9e222000-06-18 14:50:44 +1000237 packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100238 file, linenum, opts);
Damien Millerf6d9e222000-06-18 14:50:44 +1000239 /* deny access */
240 return 0;
241}