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.17 2001/05/30 12:55:06 markus Exp $");
21 #include "auth-options.h"
24 /* Flags set authorized_keys flags */
25 int no_port_forwarding_flag = 0;
26 int no_agent_forwarding_flag = 0;
27 int no_x11_forwarding_flag = 0;
30 /* "command=" option. */
31 char *forced_command = NULL;
33 /* "environment=" options. */
34 struct envstring *custom_environment = NULL;
36 extern ServerOptions options;
39 auth_clear_options(void)
41 no_agent_forwarding_flag = 0;
42 no_port_forwarding_flag = 0;
44 no_x11_forwarding_flag = 0;
45 while (custom_environment) {
46 struct envstring *ce = custom_environment;
47 custom_environment = ce->next;
52 xfree(forced_command);
53 forced_command = NULL;
55 channel_clear_permitted_opens();
59 * return 1 if access is granted, 0 if not.
60 * side effect: sets key option flags
63 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
74 while (*opts && *opts != ' ' && *opts != '\t') {
75 cp = "no-port-forwarding";
76 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
77 packet_send_debug("Port forwarding disabled.");
78 no_port_forwarding_flag = 1;
82 cp = "no-agent-forwarding";
83 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
84 packet_send_debug("Agent forwarding disabled.");
85 no_agent_forwarding_flag = 1;
89 cp = "no-X11-forwarding";
90 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
91 packet_send_debug("X11 forwarding disabled.");
92 no_x11_forwarding_flag = 1;
97 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
98 packet_send_debug("Pty allocation disabled.");
104 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
106 forced_command = xmalloc(strlen(opts) + 1);
111 if (*opts == '\\' && opts[1] == '"') {
113 forced_command[i++] = '"';
116 forced_command[i++] = *opts++;
119 debug("%.100s, line %lu: missing end quote",
121 packet_send_debug("%.100s, line %lu: missing end quote",
123 xfree(forced_command);
124 forced_command = NULL;
127 forced_command[i] = 0;
128 packet_send_debug("Forced command: %.900s", forced_command);
132 cp = "environment=\"";
133 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
135 struct envstring *new_envstring;
138 s = xmalloc(strlen(opts) + 1);
143 if (*opts == '\\' && opts[1] == '"') {
151 debug("%.100s, line %lu: missing end quote",
153 packet_send_debug("%.100s, line %lu: missing end quote",
159 packet_send_debug("Adding to environment: %.900s", s);
160 debug("Adding to environment: %.900s", s);
162 new_envstring = xmalloc(sizeof(struct envstring));
163 new_envstring->s = s;
164 new_envstring->next = custom_environment;
165 custom_environment = new_envstring;
169 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",
199 * Deny access if we get a negative
200 * match for the hostname or the ip
201 * or if we get not match at all
203 mname = match_hostname(remote_host, patterns,
205 mip = match_hostname(remote_ip, patterns,
208 if (mname == -1 || mip == -1 ||
209 (mname != 1 && mip != 1)) {
210 log("Authentication tried for %.100s with "
211 "correct key but not from a permitted "
212 "host (host=%.200s, ip=%.200s).",
213 pw->pw_name, remote_host, remote_ip);
214 packet_send_debug("Your host '%.200s' is not "
215 "permitted to use this key for login.",
220 /* Host name matches. */
223 cp = "permitopen=\"";
224 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
227 char *patterns = xmalloc(strlen(opts) + 1);
234 if (*opts == '\\' && opts[1] == '"') {
239 patterns[i++] = *opts++;
242 debug("%.100s, line %lu: missing end quote",
244 packet_send_debug("%.100s, line %lu: missing end quote",
251 c = strchr(patterns, ':');
253 debug("%.100s, line %lu: permitopen: missing colon <%.100s>",
254 file, linenum, patterns);
255 packet_send_debug("%.100s, line %lu: missing colon",
262 port = strtol(c, &ep, 0);
264 debug("%.100s, line %lu: permitopen: missing port <%.100s>",
265 file, linenum, patterns);
266 packet_send_debug("%.100s, line %lu: missing port",
271 if (options.allow_tcp_forwarding)
272 channel_add_permitted_opens(patterns, port);
278 * Skip the comma, and move to the next option
279 * (or break out if there are no more).
282 fatal("Bugs in auth-options.c option processing.");
283 if (*opts == ' ' || *opts == '\t')
284 break; /* End of options. */
288 /* Process the next option. */
294 log("Bad options in %.100s file, line %lu: %.50s",
295 file, linenum, opts);
296 packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
297 file, linenum, opts);