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>
26 #include "auth-options.h"
29 #include "monitor_wrap.h"
32 /* Flags set authorized_keys flags */
33 int no_port_forwarding_flag
= 0;
34 int no_agent_forwarding_flag
= 0;
35 int no_x11_forwarding_flag
= 0;
38 /* "command=" option. */
39 char *forced_command
= NULL
;
41 /* "environment=" options. */
42 struct envstring
*custom_environment
= NULL
;
44 /* "tunnel=" option. */
45 int forced_tun_device
= -1;
47 extern ServerOptions options
;
50 auth_clear_options(void)
52 no_agent_forwarding_flag
= 0;
53 no_port_forwarding_flag
= 0;
55 no_x11_forwarding_flag
= 0;
56 while (custom_environment
) {
57 struct envstring
*ce
= custom_environment
;
58 custom_environment
= ce
->next
;
63 xfree(forced_command
);
64 forced_command
= NULL
;
66 forced_tun_device
= -1;
67 channel_clear_permitted_opens();
72 * return 1 if access is granted, 0 if not.
73 * side effect: sets key option flags
76 auth_parse_options(struct passwd
*pw
, char *opts
, char *file
, u_long linenum
)
87 while (*opts
&& *opts
!= ' ' && *opts
!= '\t') {
88 cp
= "no-port-forwarding";
89 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
90 auth_debug_add("Port forwarding disabled.");
91 no_port_forwarding_flag
= 1;
95 cp
= "no-agent-forwarding";
96 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
97 auth_debug_add("Agent forwarding disabled.");
98 no_agent_forwarding_flag
= 1;
102 cp
= "no-X11-forwarding";
103 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
104 auth_debug_add("X11 forwarding disabled.");
105 no_x11_forwarding_flag
= 1;
110 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
111 auth_debug_add("Pty allocation disabled.");
117 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
119 forced_command
= xmalloc(strlen(opts
) + 1);
124 if (*opts
== '\\' && opts
[1] == '"') {
126 forced_command
[i
++] = '"';
129 forced_command
[i
++] = *opts
++;
132 debug("%.100s, line %lu: missing end quote",
134 auth_debug_add("%.100s, line %lu: missing end quote",
136 xfree(forced_command
);
137 forced_command
= NULL
;
140 forced_command
[i
] = '\0';
141 auth_debug_add("Forced command: %.900s", forced_command
);
145 cp
= "environment=\"";
146 if (options
.permit_user_env
&&
147 strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
149 struct envstring
*new_envstring
;
152 s
= xmalloc(strlen(opts
) + 1);
157 if (*opts
== '\\' && opts
[1] == '"') {
165 debug("%.100s, line %lu: missing end quote",
167 auth_debug_add("%.100s, line %lu: missing end quote",
173 auth_debug_add("Adding to environment: %.900s", s
);
174 debug("Adding to environment: %.900s", s
);
176 new_envstring
= xmalloc(sizeof(struct envstring
));
177 new_envstring
->s
= s
;
178 new_envstring
->next
= custom_environment
;
179 custom_environment
= new_envstring
;
183 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
184 const char *remote_ip
= get_remote_ipaddr();
185 const char *remote_host
= get_canonical_hostname(
187 char *patterns
= xmalloc(strlen(opts
) + 1);
194 if (*opts
== '\\' && opts
[1] == '"') {
199 patterns
[i
++] = *opts
++;
202 debug("%.100s, line %lu: missing end quote",
204 auth_debug_add("%.100s, line %lu: missing end quote",
211 if (match_host_and_ip(remote_host
, remote_ip
,
214 logit("Authentication tried for %.100s with "
215 "correct key but not from a permitted "
216 "host (host=%.200s, ip=%.200s).",
217 pw
->pw_name
, remote_host
, remote_ip
);
218 auth_debug_add("Your host '%.200s' is not "
219 "permitted to use this key for login.",
225 /* Host name matches. */
228 cp
= "permitopen=\"";
229 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
232 char *patterns
= xmalloc(strlen(opts
) + 1);
239 if (*opts
== '\\' && opts
[1] == '"') {
244 patterns
[i
++] = *opts
++;
247 debug("%.100s, line %lu: missing end quote",
249 auth_debug_add("%.100s, line %lu: missing "
250 "end quote", file
, linenum
);
258 if (host
== NULL
|| strlen(host
) >= NI_MAXHOST
) {
259 debug("%.100s, line %lu: Bad permitopen "
260 "specification <%.100s>", file
, linenum
,
262 auth_debug_add("%.100s, line %lu: "
263 "Bad permitopen specification", file
,
268 host
= cleanhostname(host
);
269 if (p
== NULL
|| (port
= a2port(p
)) == 0) {
270 debug("%.100s, line %lu: Bad permitopen port "
271 "<%.100s>", file
, linenum
, p
? p
: "");
272 auth_debug_add("%.100s, line %lu: "
273 "Bad permitopen port", file
, linenum
);
277 if (options
.allow_tcp_forwarding
)
278 channel_add_permitted_opens(host
, port
);
283 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
286 tun
= xmalloc(strlen(opts
) + 1);
294 debug("%.100s, line %lu: missing end quote",
296 auth_debug_add("%.100s, line %lu: missing end quote",
299 forced_tun_device
= -1;
303 forced_tun_device
= a2tun(tun
, NULL
);
305 if (forced_tun_device
== SSH_TUNID_ERR
) {
306 debug("%.100s, line %lu: invalid tun device",
308 auth_debug_add("%.100s, line %lu: invalid tun device",
310 forced_tun_device
= -1;
313 auth_debug_add("Forced tun device: %d", forced_tun_device
);
319 * Skip the comma, and move to the next option
320 * (or break out if there are no more).
323 fatal("Bugs in auth-options.c option processing.");
324 if (*opts
== ' ' || *opts
== '\t')
325 break; /* End of options. */
329 /* Process the next option. */
339 logit("Bad options in %.100s file, line %lu: %.50s",
340 file
, linenum
, opts
);
341 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
342 file
, linenum
, opts
);