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.25 2002/07/21 18:32:20 stevesk Exp $");
20 #include "auth-options.h"
23 #include "monitor_wrap.h"
26 /* Flags set authorized_keys flags */
27 int no_port_forwarding_flag = 0;
28 int no_agent_forwarding_flag = 0;
29 int no_x11_forwarding_flag = 0;
32 /* "command=" option. */
33 char *forced_command = NULL;
35 /* "environment=" options. */
36 struct envstring *custom_environment = NULL;
38 extern ServerOptions options;
41 auth_clear_options(void)
43 no_agent_forwarding_flag = 0;
44 no_port_forwarding_flag = 0;
46 no_x11_forwarding_flag = 0;
47 while (custom_environment) {
48 struct envstring *ce = custom_environment;
49 custom_environment = ce->next;
54 xfree(forced_command);
55 forced_command = NULL;
57 channel_clear_permitted_opens();
62 * return 1 if access is granted, 0 if not.
63 * side effect: sets key option flags
66 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
77 while (*opts && *opts != ' ' && *opts != '\t') {
78 cp = "no-port-forwarding";
79 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
80 auth_debug_add("Port forwarding disabled.");
81 no_port_forwarding_flag = 1;
85 cp = "no-agent-forwarding";
86 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
87 auth_debug_add("Agent forwarding disabled.");
88 no_agent_forwarding_flag = 1;
92 cp = "no-X11-forwarding";
93 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
94 auth_debug_add("X11 forwarding disabled.");
95 no_x11_forwarding_flag = 1;
100 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
101 auth_debug_add("Pty allocation disabled.");
107 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
109 forced_command = xmalloc(strlen(opts) + 1);
114 if (*opts == '\\' && opts[1] == '"') {
116 forced_command[i++] = '"';
119 forced_command[i++] = *opts++;
122 debug("%.100s, line %lu: missing end quote",
124 auth_debug_add("%.100s, line %lu: missing end quote",
126 xfree(forced_command);
127 forced_command = NULL;
130 forced_command[i] = 0;
131 auth_debug_add("Forced command: %.900s", forced_command);
135 cp = "environment=\"";
136 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
138 struct envstring *new_envstring;
141 s = xmalloc(strlen(opts) + 1);
146 if (*opts == '\\' && opts[1] == '"') {
154 debug("%.100s, line %lu: missing end quote",
156 auth_debug_add("%.100s, line %lu: missing end quote",
162 auth_debug_add("Adding to environment: %.900s", s);
163 debug("Adding to environment: %.900s", s);
165 new_envstring = xmalloc(sizeof(struct envstring));
166 new_envstring->s = s;
167 new_envstring->next = custom_environment;
168 custom_environment = new_envstring;
172 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
173 const char *remote_ip = get_remote_ipaddr();
174 const char *remote_host = get_canonical_hostname(
175 options.verify_reverse_mapping);
176 char *patterns = xmalloc(strlen(opts) + 1);
183 if (*opts == '\\' && opts[1] == '"') {
188 patterns[i++] = *opts++;
191 debug("%.100s, line %lu: missing end quote",
193 auth_debug_add("%.100s, line %lu: missing end quote",
200 if (match_host_and_ip(remote_host, remote_ip,
203 log("Authentication tried for %.100s with "
204 "correct key but not from a permitted "
205 "host (host=%.200s, ip=%.200s).",
206 pw->pw_name, remote_host, remote_ip);
207 auth_debug_add("Your host '%.200s' is not "
208 "permitted to use this key for login.",
214 /* Host name matches. */
217 cp = "permitopen=\"";
218 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
219 char host[256], sport[6];
221 char *patterns = xmalloc(strlen(opts) + 1);
228 if (*opts == '\\' && opts[1] == '"') {
233 patterns[i++] = *opts++;
236 debug("%.100s, line %lu: missing end quote",
238 auth_debug_add("%.100s, line %lu: missing end quote",
245 if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
246 sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
247 debug("%.100s, line %lu: Bad permitopen specification "
248 "<%.100s>", file, linenum, patterns);
249 auth_debug_add("%.100s, line %lu: "
250 "Bad permitopen specification", file, linenum);
254 if ((port = a2port(sport)) == 0) {
255 debug("%.100s, line %lu: Bad permitopen port <%.100s>",
256 file, linenum, sport);
257 auth_debug_add("%.100s, line %lu: "
258 "Bad permitopen port", file, linenum);
262 if (options.allow_tcp_forwarding)
263 channel_add_permitted_opens(host, port);
269 * Skip the comma, and move to the next option
270 * (or break out if there are no more).
273 fatal("Bugs in auth-options.c option processing.");
274 if (*opts == ' ' || *opts == '\t')
275 break; /* End of options. */
279 /* Process the next option. */
289 log("Bad options in %.100s file, line %lu: %.50s",
290 file, linenum, opts);
291 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
292 file, linenum, opts);