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".
19 #include "auth-options.h"
22 #include "monitor_wrap.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 /* "tunnel=" option. */
38 int forced_tun_device = -1;
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 forced_tun_device = -1;
60 channel_clear_permitted_opens();
65 * return 1 if access is granted, 0 if not.
66 * side effect: sets key option flags
69 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
80 while (*opts && *opts != ' ' && *opts != '\t') {
81 cp = "no-port-forwarding";
82 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
83 auth_debug_add("Port forwarding disabled.");
84 no_port_forwarding_flag = 1;
88 cp = "no-agent-forwarding";
89 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
90 auth_debug_add("Agent forwarding disabled.");
91 no_agent_forwarding_flag = 1;
95 cp = "no-X11-forwarding";
96 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
97 auth_debug_add("X11 forwarding disabled.");
98 no_x11_forwarding_flag = 1;
103 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
104 auth_debug_add("Pty allocation disabled.");
110 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
112 forced_command = xmalloc(strlen(opts) + 1);
117 if (*opts == '\\' && opts[1] == '"') {
119 forced_command[i++] = '"';
122 forced_command[i++] = *opts++;
125 debug("%.100s, line %lu: missing end quote",
127 auth_debug_add("%.100s, line %lu: missing end quote",
129 xfree(forced_command);
130 forced_command = NULL;
133 forced_command[i] = 0;
134 auth_debug_add("Forced command: %.900s", forced_command);
138 cp = "environment=\"";
139 if (options.permit_user_env &&
140 strncasecmp(opts, cp, strlen(cp)) == 0) {
142 struct envstring *new_envstring;
145 s = xmalloc(strlen(opts) + 1);
150 if (*opts == '\\' && opts[1] == '"') {
158 debug("%.100s, line %lu: missing end quote",
160 auth_debug_add("%.100s, line %lu: missing end quote",
166 auth_debug_add("Adding to environment: %.900s", s);
167 debug("Adding to environment: %.900s", s);
169 new_envstring = xmalloc(sizeof(struct envstring));
170 new_envstring->s = s;
171 new_envstring->next = custom_environment;
172 custom_environment = new_envstring;
176 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
177 const char *remote_ip = get_remote_ipaddr();
178 const char *remote_host = get_canonical_hostname(
180 char *patterns = xmalloc(strlen(opts) + 1);
187 if (*opts == '\\' && opts[1] == '"') {
192 patterns[i++] = *opts++;
195 debug("%.100s, line %lu: missing end quote",
197 auth_debug_add("%.100s, line %lu: missing end quote",
204 if (match_host_and_ip(remote_host, remote_ip,
207 logit("Authentication tried for %.100s with "
208 "correct key but not from a permitted "
209 "host (host=%.200s, ip=%.200s).",
210 pw->pw_name, remote_host, remote_ip);
211 auth_debug_add("Your host '%.200s' is not "
212 "permitted to use this key for login.",
218 /* Host name matches. */
221 cp = "permitopen=\"";
222 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
225 char *patterns = xmalloc(strlen(opts) + 1);
232 if (*opts == '\\' && opts[1] == '"') {
237 patterns[i++] = *opts++;
240 debug("%.100s, line %lu: missing end quote",
242 auth_debug_add("%.100s, line %lu: missing "
243 "end quote", file, linenum);
251 if (host == NULL || strlen(host) >= NI_MAXHOST) {
252 debug("%.100s, line %lu: Bad permitopen "
253 "specification <%.100s>", file, linenum,
255 auth_debug_add("%.100s, line %lu: "
256 "Bad permitopen specification", file,
261 host = cleanhostname(host);
262 if (p == NULL || (port = a2port(p)) == 0) {
263 debug("%.100s, line %lu: Bad permitopen port "
264 "<%.100s>", file, linenum, p ? p : "");
265 auth_debug_add("%.100s, line %lu: "
266 "Bad permitopen port", file, linenum);
270 if (options.allow_tcp_forwarding)
271 channel_add_permitted_opens(host, port);
276 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
279 tun = xmalloc(strlen(opts) + 1);
287 debug("%.100s, line %lu: missing end quote",
289 auth_debug_add("%.100s, line %lu: missing end quote",
292 forced_tun_device = -1;
296 forced_tun_device = a2tun(tun, NULL);
298 if (forced_tun_device == SSH_TUNID_ERR) {
299 debug("%.100s, line %lu: invalid tun device",
301 auth_debug_add("%.100s, line %lu: invalid tun device",
303 forced_tun_device = -1;
306 auth_debug_add("Forced tun device: %d", forced_tun_device);
312 * Skip the comma, and move to the next option
313 * (or break out if there are no more).
316 fatal("Bugs in auth-options.c option processing.");
317 if (*opts == ' ' || *opts == '\t')
318 break; /* End of options. */
322 /* Process the next option. */
332 logit("Bad options in %.100s file, line %lu: %.50s",
333 file, linenum, opts);
334 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
335 file, linenum, opts);