1 /* $OpenBSD: auth-options.c,v 1.39 2006/07/22 20:48:22 stevesk Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
15 #include <sys/types.h>
17 #if defined(HAVE_NETDB_H)
28 #include "auth-options.h"
31 #include "monitor_wrap.h"
34 /* Flags set authorized_keys flags */
35 int no_port_forwarding_flag = 0;
36 int no_agent_forwarding_flag = 0;
37 int no_x11_forwarding_flag = 0;
40 /* "command=" option. */
41 char *forced_command = NULL;
43 /* "environment=" options. */
44 struct envstring *custom_environment = NULL;
46 /* "tunnel=" option. */
47 int forced_tun_device = -1;
49 extern ServerOptions options;
52 auth_clear_options(void)
54 no_agent_forwarding_flag = 0;
55 no_port_forwarding_flag = 0;
57 no_x11_forwarding_flag = 0;
58 while (custom_environment) {
59 struct envstring *ce = custom_environment;
60 custom_environment = ce->next;
65 xfree(forced_command);
66 forced_command = NULL;
68 forced_tun_device = -1;
69 channel_clear_permitted_opens();
74 * return 1 if access is granted, 0 if not.
75 * side effect: sets key option flags
78 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
89 while (*opts && *opts != ' ' && *opts != '\t') {
90 cp = "no-port-forwarding";
91 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
92 auth_debug_add("Port forwarding disabled.");
93 no_port_forwarding_flag = 1;
97 cp = "no-agent-forwarding";
98 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
99 auth_debug_add("Agent forwarding disabled.");
100 no_agent_forwarding_flag = 1;
104 cp = "no-X11-forwarding";
105 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
106 auth_debug_add("X11 forwarding disabled.");
107 no_x11_forwarding_flag = 1;
112 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
113 auth_debug_add("Pty allocation disabled.");
119 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
121 forced_command = xmalloc(strlen(opts) + 1);
126 if (*opts == '\\' && opts[1] == '"') {
128 forced_command[i++] = '"';
131 forced_command[i++] = *opts++;
134 debug("%.100s, line %lu: missing end quote",
136 auth_debug_add("%.100s, line %lu: missing end quote",
138 xfree(forced_command);
139 forced_command = NULL;
142 forced_command[i] = '\0';
143 auth_debug_add("Forced command: %.900s", forced_command);
147 cp = "environment=\"";
148 if (options.permit_user_env &&
149 strncasecmp(opts, cp, strlen(cp)) == 0) {
151 struct envstring *new_envstring;
154 s = xmalloc(strlen(opts) + 1);
159 if (*opts == '\\' && opts[1] == '"') {
167 debug("%.100s, line %lu: missing end quote",
169 auth_debug_add("%.100s, line %lu: missing end quote",
175 auth_debug_add("Adding to environment: %.900s", s);
176 debug("Adding to environment: %.900s", s);
178 new_envstring = xmalloc(sizeof(struct envstring));
179 new_envstring->s = s;
180 new_envstring->next = custom_environment;
181 custom_environment = new_envstring;
185 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
186 const char *remote_ip = get_remote_ipaddr();
187 const char *remote_host = get_canonical_hostname(
189 char *patterns = xmalloc(strlen(opts) + 1);
196 if (*opts == '\\' && opts[1] == '"') {
201 patterns[i++] = *opts++;
204 debug("%.100s, line %lu: missing end quote",
206 auth_debug_add("%.100s, line %lu: missing end quote",
213 if (match_host_and_ip(remote_host, remote_ip,
216 logit("Authentication tried for %.100s with "
217 "correct key but not from a permitted "
218 "host (host=%.200s, ip=%.200s).",
219 pw->pw_name, remote_host, remote_ip);
220 auth_debug_add("Your host '%.200s' is not "
221 "permitted to use this key for login.",
227 /* Host name matches. */
230 cp = "permitopen=\"";
231 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
234 char *patterns = xmalloc(strlen(opts) + 1);
241 if (*opts == '\\' && opts[1] == '"') {
246 patterns[i++] = *opts++;
249 debug("%.100s, line %lu: missing end quote",
251 auth_debug_add("%.100s, line %lu: missing "
252 "end quote", file, linenum);
260 if (host == NULL || strlen(host) >= NI_MAXHOST) {
261 debug("%.100s, line %lu: Bad permitopen "
262 "specification <%.100s>", file, linenum,
264 auth_debug_add("%.100s, line %lu: "
265 "Bad permitopen specification", file,
270 host = cleanhostname(host);
271 if (p == NULL || (port = a2port(p)) == 0) {
272 debug("%.100s, line %lu: Bad permitopen port "
273 "<%.100s>", file, linenum, p ? p : "");
274 auth_debug_add("%.100s, line %lu: "
275 "Bad permitopen port", file, linenum);
279 if (options.allow_tcp_forwarding)
280 channel_add_permitted_opens(host, port);
285 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
288 tun = xmalloc(strlen(opts) + 1);
296 debug("%.100s, line %lu: missing end quote",
298 auth_debug_add("%.100s, line %lu: missing end quote",
301 forced_tun_device = -1;
305 forced_tun_device = a2tun(tun, NULL);
307 if (forced_tun_device == SSH_TUNID_ERR) {
308 debug("%.100s, line %lu: invalid tun device",
310 auth_debug_add("%.100s, line %lu: invalid tun device",
312 forced_tun_device = -1;
315 auth_debug_add("Forced tun device: %d", forced_tun_device);
321 * Skip the comma, and move to the next option
322 * (or break out if there are no more).
325 fatal("Bugs in auth-options.c option processing.");
326 if (*opts == ' ' || *opts == '\t')
327 break; /* End of options. */
331 /* Process the next option. */
341 logit("Bad options in %.100s file, line %lu: %.50s",
342 file, linenum, opts);
343 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
344 file, linenum, opts);