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.20 2001/08/30 20:36:34 stevesk Exp $");
21 #include "auth-options.h"
25 /* Flags set authorized_keys flags */
26 int no_port_forwarding_flag = 0;
27 int no_agent_forwarding_flag = 0;
28 int no_x11_forwarding_flag = 0;
31 /* "command=" option. */
32 char *forced_command = NULL;
34 /* "environment=" options. */
35 struct envstring *custom_environment = NULL;
37 extern ServerOptions options;
40 auth_clear_options(void)
42 no_agent_forwarding_flag = 0;
43 no_port_forwarding_flag = 0;
45 no_x11_forwarding_flag = 0;
46 while (custom_environment) {
47 struct envstring *ce = custom_environment;
48 custom_environment = ce->next;
53 xfree(forced_command);
54 forced_command = NULL;
56 channel_clear_permitted_opens();
60 * return 1 if access is granted, 0 if not.
61 * side effect: sets key option flags
64 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
75 while (*opts && *opts != ' ' && *opts != '\t') {
76 cp = "no-port-forwarding";
77 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
78 packet_send_debug("Port forwarding disabled.");
79 no_port_forwarding_flag = 1;
83 cp = "no-agent-forwarding";
84 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
85 packet_send_debug("Agent forwarding disabled.");
86 no_agent_forwarding_flag = 1;
90 cp = "no-X11-forwarding";
91 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
92 packet_send_debug("X11 forwarding disabled.");
93 no_x11_forwarding_flag = 1;
98 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
99 packet_send_debug("Pty allocation disabled.");
105 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
107 forced_command = xmalloc(strlen(opts) + 1);
112 if (*opts == '\\' && opts[1] == '"') {
114 forced_command[i++] = '"';
117 forced_command[i++] = *opts++;
120 debug("%.100s, line %lu: missing end quote",
122 packet_send_debug("%.100s, line %lu: missing end quote",
124 xfree(forced_command);
125 forced_command = NULL;
128 forced_command[i] = 0;
129 packet_send_debug("Forced command: %.900s", forced_command);
133 cp = "environment=\"";
134 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
136 struct envstring *new_envstring;
139 s = xmalloc(strlen(opts) + 1);
144 if (*opts == '\\' && opts[1] == '"') {
152 debug("%.100s, line %lu: missing end quote",
154 packet_send_debug("%.100s, line %lu: missing end quote",
160 packet_send_debug("Adding to environment: %.900s", s);
161 debug("Adding to environment: %.900s", s);
163 new_envstring = xmalloc(sizeof(struct envstring));
164 new_envstring->s = s;
165 new_envstring->next = custom_environment;
166 custom_environment = new_envstring;
170 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
171 const char *remote_ip = get_remote_ipaddr();
172 const char *remote_host = get_canonical_hostname(
173 options.reverse_mapping_check);
174 char *patterns = xmalloc(strlen(opts) + 1);
181 if (*opts == '\\' && opts[1] == '"') {
186 patterns[i++] = *opts++;
189 debug("%.100s, line %lu: missing end quote",
191 packet_send_debug("%.100s, line %lu: missing end quote",
198 if (match_host_and_ip(remote_host, remote_ip,
201 log("Authentication tried for %.100s with "
202 "correct key but not from a permitted "
203 "host (host=%.200s, ip=%.200s).",
204 pw->pw_name, remote_host, remote_ip);
205 packet_send_debug("Your host '%.200s' is not "
206 "permitted to use this key for login.",
212 /* Host name matches. */
215 cp = "permitopen=\"";
216 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
217 char host[256], sport[6];
219 char *patterns = xmalloc(strlen(opts) + 1);
226 if (*opts == '\\' && opts[1] == '"') {
231 patterns[i++] = *opts++;
234 debug("%.100s, line %lu: missing end quote",
236 packet_send_debug("%.100s, line %lu: missing end quote",
243 if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
244 sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
245 debug("%.100s, line %lu: Bad permitopen specification "
246 "<%.100s>", file, linenum, patterns);
247 packet_send_debug("%.100s, line %lu: "
248 "Bad permitopen specification", file, linenum);
252 if ((port = a2port(sport)) == 0) {
253 debug("%.100s, line %lu: Bad permitopen port <%.100s>",
254 file, linenum, sport);
255 packet_send_debug("%.100s, line %lu: "
256 "Bad permitopen port", file, linenum);
260 if (options.allow_tcp_forwarding)
261 channel_add_permitted_opens(host, port);
267 * Skip the comma, and move to the next option
268 * (or break out if there are no more).
271 fatal("Bugs in auth-options.c option processing.");
272 if (*opts == ' ' || *opts == '\t')
273 break; /* End of options. */
277 /* Process the next option. */
283 log("Bad options in %.100s file, line %lu: %.50s",
284 file, linenum, opts);
285 packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
286 file, linenum, opts);