2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
13 RCSID("$OpenBSD: auth-options.c,v 1.24 2002/05/13 20:44:58 markus Exp $");
21 #include "auth-options.h"
25 #include "monitor_wrap.h"
28 /* Flags set authorized_keys flags */
29 int no_port_forwarding_flag = 0;
30 int no_agent_forwarding_flag = 0;
31 int no_x11_forwarding_flag = 0;
34 /* "command=" option. */
35 char *forced_command = NULL;
37 /* "environment=" options. */
38 struct envstring *custom_environment = NULL;
40 extern ServerOptions options;
43 auth_clear_options(void)
45 no_agent_forwarding_flag = 0;
46 no_port_forwarding_flag = 0;
48 no_x11_forwarding_flag = 0;
49 while (custom_environment) {
50 struct envstring *ce = custom_environment;
51 custom_environment = ce->next;
56 xfree(forced_command);
57 forced_command = NULL;
59 channel_clear_permitted_opens();
64 * return 1 if access is granted, 0 if not.
65 * side effect: sets key option flags
68 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
79 while (*opts && *opts != ' ' && *opts != '\t') {
80 cp = "no-port-forwarding";
81 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
82 auth_debug_add("Port forwarding disabled.");
83 no_port_forwarding_flag = 1;
87 cp = "no-agent-forwarding";
88 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
89 auth_debug_add("Agent forwarding disabled.");
90 no_agent_forwarding_flag = 1;
94 cp = "no-X11-forwarding";
95 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
96 auth_debug_add("X11 forwarding disabled.");
97 no_x11_forwarding_flag = 1;
102 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
103 auth_debug_add("Pty allocation disabled.");
109 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
111 forced_command = xmalloc(strlen(opts) + 1);
116 if (*opts == '\\' && opts[1] == '"') {
118 forced_command[i++] = '"';
121 forced_command[i++] = *opts++;
124 debug("%.100s, line %lu: missing end quote",
126 auth_debug_add("%.100s, line %lu: missing end quote",
128 xfree(forced_command);
129 forced_command = NULL;
132 forced_command[i] = 0;
133 auth_debug_add("Forced command: %.900s", forced_command);
137 cp = "environment=\"";
138 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
140 struct envstring *new_envstring;
143 s = xmalloc(strlen(opts) + 1);
148 if (*opts == '\\' && opts[1] == '"') {
156 debug("%.100s, line %lu: missing end quote",
158 auth_debug_add("%.100s, line %lu: missing end quote",
164 auth_debug_add("Adding to environment: %.900s", s);
165 debug("Adding to environment: %.900s", s);
167 new_envstring = xmalloc(sizeof(struct envstring));
168 new_envstring->s = s;
169 new_envstring->next = custom_environment;
170 custom_environment = new_envstring;
174 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
175 const char *remote_ip = get_remote_ipaddr();
176 const char *remote_host = get_canonical_hostname(
177 options.verify_reverse_mapping);
178 char *patterns = xmalloc(strlen(opts) + 1);
185 if (*opts == '\\' && opts[1] == '"') {
190 patterns[i++] = *opts++;
193 debug("%.100s, line %lu: missing end quote",
195 auth_debug_add("%.100s, line %lu: missing end quote",
202 if (match_host_and_ip(remote_host, remote_ip,
205 log("Authentication tried for %.100s with "
206 "correct key but not from a permitted "
207 "host (host=%.200s, ip=%.200s).",
208 pw->pw_name, remote_host, remote_ip);
209 auth_debug_add("Your host '%.200s' is not "
210 "permitted to use this key for login.",
216 /* Host name matches. */
219 cp = "permitopen=\"";
220 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
221 char host[256], sport[6];
223 char *patterns = xmalloc(strlen(opts) + 1);
230 if (*opts == '\\' && opts[1] == '"') {
235 patterns[i++] = *opts++;
238 debug("%.100s, line %lu: missing end quote",
240 auth_debug_add("%.100s, line %lu: missing end quote",
247 if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
248 sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
249 debug("%.100s, line %lu: Bad permitopen specification "
250 "<%.100s>", file, linenum, patterns);
251 auth_debug_add("%.100s, line %lu: "
252 "Bad permitopen specification", file, linenum);
256 if ((port = a2port(sport)) == 0) {
257 debug("%.100s, line %lu: Bad permitopen port <%.100s>",
258 file, linenum, sport);
259 auth_debug_add("%.100s, line %lu: "
260 "Bad permitopen port", file, linenum);
264 if (options.allow_tcp_forwarding)
265 channel_add_permitted_opens(host, port);
271 * Skip the comma, and move to the next option
272 * (or break out if there are no more).
275 fatal("Bugs in auth-options.c option processing.");
276 if (*opts == ' ' || *opts == '\t')
277 break; /* End of options. */
281 /* Process the next option. */
291 log("Bad options in %.100s file, line %lu: %.50s",
292 file, linenum, opts);
293 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
294 file, linenum, opts);