1 /* $OpenBSD: auth-options.c,v 1.36 2006/07/06 16:03:53 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>
24 #include "auth-options.h"
27 #include "monitor_wrap.h"
30 /* Flags set authorized_keys flags */
31 int no_port_forwarding_flag
= 0;
32 int no_agent_forwarding_flag
= 0;
33 int no_x11_forwarding_flag
= 0;
36 /* "command=" option. */
37 char *forced_command
= NULL
;
39 /* "environment=" options. */
40 struct envstring
*custom_environment
= NULL
;
42 /* "tunnel=" option. */
43 int forced_tun_device
= -1;
45 extern ServerOptions options
;
48 auth_clear_options(void)
50 no_agent_forwarding_flag
= 0;
51 no_port_forwarding_flag
= 0;
53 no_x11_forwarding_flag
= 0;
54 while (custom_environment
) {
55 struct envstring
*ce
= custom_environment
;
56 custom_environment
= ce
->next
;
61 xfree(forced_command
);
62 forced_command
= NULL
;
64 forced_tun_device
= -1;
65 channel_clear_permitted_opens();
70 * return 1 if access is granted, 0 if not.
71 * side effect: sets key option flags
74 auth_parse_options(struct passwd
*pw
, char *opts
, char *file
, u_long linenum
)
85 while (*opts
&& *opts
!= ' ' && *opts
!= '\t') {
86 cp
= "no-port-forwarding";
87 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
88 auth_debug_add("Port forwarding disabled.");
89 no_port_forwarding_flag
= 1;
93 cp
= "no-agent-forwarding";
94 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
95 auth_debug_add("Agent forwarding disabled.");
96 no_agent_forwarding_flag
= 1;
100 cp
= "no-X11-forwarding";
101 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
102 auth_debug_add("X11 forwarding disabled.");
103 no_x11_forwarding_flag
= 1;
108 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
109 auth_debug_add("Pty allocation disabled.");
115 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
117 forced_command
= xmalloc(strlen(opts
) + 1);
122 if (*opts
== '\\' && opts
[1] == '"') {
124 forced_command
[i
++] = '"';
127 forced_command
[i
++] = *opts
++;
130 debug("%.100s, line %lu: missing end quote",
132 auth_debug_add("%.100s, line %lu: missing end quote",
134 xfree(forced_command
);
135 forced_command
= NULL
;
138 forced_command
[i
] = 0;
139 auth_debug_add("Forced command: %.900s", forced_command
);
143 cp
= "environment=\"";
144 if (options
.permit_user_env
&&
145 strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
147 struct envstring
*new_envstring
;
150 s
= xmalloc(strlen(opts
) + 1);
155 if (*opts
== '\\' && opts
[1] == '"') {
163 debug("%.100s, line %lu: missing end quote",
165 auth_debug_add("%.100s, line %lu: missing end quote",
171 auth_debug_add("Adding to environment: %.900s", s
);
172 debug("Adding to environment: %.900s", s
);
174 new_envstring
= xmalloc(sizeof(struct envstring
));
175 new_envstring
->s
= s
;
176 new_envstring
->next
= custom_environment
;
177 custom_environment
= new_envstring
;
181 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
182 const char *remote_ip
= get_remote_ipaddr();
183 const char *remote_host
= get_canonical_hostname(
185 char *patterns
= xmalloc(strlen(opts
) + 1);
192 if (*opts
== '\\' && opts
[1] == '"') {
197 patterns
[i
++] = *opts
++;
200 debug("%.100s, line %lu: missing end quote",
202 auth_debug_add("%.100s, line %lu: missing end quote",
209 if (match_host_and_ip(remote_host
, remote_ip
,
212 logit("Authentication tried for %.100s with "
213 "correct key but not from a permitted "
214 "host (host=%.200s, ip=%.200s).",
215 pw
->pw_name
, remote_host
, remote_ip
);
216 auth_debug_add("Your host '%.200s' is not "
217 "permitted to use this key for login.",
223 /* Host name matches. */
226 cp
= "permitopen=\"";
227 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
230 char *patterns
= xmalloc(strlen(opts
) + 1);
237 if (*opts
== '\\' && opts
[1] == '"') {
242 patterns
[i
++] = *opts
++;
245 debug("%.100s, line %lu: missing end quote",
247 auth_debug_add("%.100s, line %lu: missing "
248 "end quote", file
, linenum
);
256 if (host
== NULL
|| strlen(host
) >= NI_MAXHOST
) {
257 debug("%.100s, line %lu: Bad permitopen "
258 "specification <%.100s>", file
, linenum
,
260 auth_debug_add("%.100s, line %lu: "
261 "Bad permitopen specification", file
,
266 host
= cleanhostname(host
);
267 if (p
== NULL
|| (port
= a2port(p
)) == 0) {
268 debug("%.100s, line %lu: Bad permitopen port "
269 "<%.100s>", file
, linenum
, p
? p
: "");
270 auth_debug_add("%.100s, line %lu: "
271 "Bad permitopen port", file
, linenum
);
275 if (options
.allow_tcp_forwarding
)
276 channel_add_permitted_opens(host
, port
);
281 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
284 tun
= xmalloc(strlen(opts
) + 1);
292 debug("%.100s, line %lu: missing end quote",
294 auth_debug_add("%.100s, line %lu: missing end quote",
297 forced_tun_device
= -1;
301 forced_tun_device
= a2tun(tun
, NULL
);
303 if (forced_tun_device
== SSH_TUNID_ERR
) {
304 debug("%.100s, line %lu: invalid tun device",
306 auth_debug_add("%.100s, line %lu: invalid tun device",
308 forced_tun_device
= -1;
311 auth_debug_add("Forced tun device: %d", forced_tun_device
);
317 * Skip the comma, and move to the next option
318 * (or break out if there are no more).
321 fatal("Bugs in auth-options.c option processing.");
322 if (*opts
== ' ' || *opts
== '\t')
323 break; /* End of options. */
327 /* Process the next option. */
337 logit("Bad options in %.100s file, line %lu: %.50s",
338 file
, linenum
, opts
);
339 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
340 file
, linenum
, opts
);