1 /* $OpenBSD: auth-options.c,v 1.40 2006/08/03 03:34:41 deraadt 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>
29 #include "auth-options.h"
38 #include "monitor_wrap.h"
40 /* Flags set authorized_keys flags */
41 int no_port_forwarding_flag
= 0;
42 int no_agent_forwarding_flag
= 0;
43 int no_x11_forwarding_flag
= 0;
46 /* "command=" option. */
47 char *forced_command
= NULL
;
49 /* "environment=" options. */
50 struct envstring
*custom_environment
= NULL
;
52 /* "tunnel=" option. */
53 int forced_tun_device
= -1;
55 extern ServerOptions options
;
58 auth_clear_options(void)
60 no_agent_forwarding_flag
= 0;
61 no_port_forwarding_flag
= 0;
63 no_x11_forwarding_flag
= 0;
64 while (custom_environment
) {
65 struct envstring
*ce
= custom_environment
;
66 custom_environment
= ce
->next
;
71 xfree(forced_command
);
72 forced_command
= NULL
;
74 forced_tun_device
= -1;
75 channel_clear_permitted_opens();
80 * return 1 if access is granted, 0 if not.
81 * side effect: sets key option flags
84 auth_parse_options(struct passwd
*pw
, char *opts
, char *file
, u_long linenum
)
95 while (*opts
&& *opts
!= ' ' && *opts
!= '\t') {
96 cp
= "no-port-forwarding";
97 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
98 auth_debug_add("Port forwarding disabled.");
99 no_port_forwarding_flag
= 1;
103 cp
= "no-agent-forwarding";
104 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
105 auth_debug_add("Agent forwarding disabled.");
106 no_agent_forwarding_flag
= 1;
110 cp
= "no-X11-forwarding";
111 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
112 auth_debug_add("X11 forwarding disabled.");
113 no_x11_forwarding_flag
= 1;
118 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
119 auth_debug_add("Pty allocation disabled.");
125 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
127 forced_command
= xmalloc(strlen(opts
) + 1);
132 if (*opts
== '\\' && opts
[1] == '"') {
134 forced_command
[i
++] = '"';
137 forced_command
[i
++] = *opts
++;
140 debug("%.100s, line %lu: missing end quote",
142 auth_debug_add("%.100s, line %lu: missing end quote",
144 xfree(forced_command
);
145 forced_command
= NULL
;
148 forced_command
[i
] = '\0';
149 auth_debug_add("Forced command: %.900s", forced_command
);
153 cp
= "environment=\"";
154 if (options
.permit_user_env
&&
155 strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
157 struct envstring
*new_envstring
;
160 s
= xmalloc(strlen(opts
) + 1);
165 if (*opts
== '\\' && opts
[1] == '"') {
173 debug("%.100s, line %lu: missing end quote",
175 auth_debug_add("%.100s, line %lu: missing end quote",
181 auth_debug_add("Adding to environment: %.900s", s
);
182 debug("Adding to environment: %.900s", s
);
184 new_envstring
= xmalloc(sizeof(struct envstring
));
185 new_envstring
->s
= s
;
186 new_envstring
->next
= custom_environment
;
187 custom_environment
= new_envstring
;
191 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
192 const char *remote_ip
= get_remote_ipaddr();
193 const char *remote_host
= get_canonical_hostname(
195 char *patterns
= xmalloc(strlen(opts
) + 1);
202 if (*opts
== '\\' && opts
[1] == '"') {
207 patterns
[i
++] = *opts
++;
210 debug("%.100s, line %lu: missing end quote",
212 auth_debug_add("%.100s, line %lu: missing end quote",
219 if (match_host_and_ip(remote_host
, remote_ip
,
222 logit("Authentication tried for %.100s with "
223 "correct key but not from a permitted "
224 "host (host=%.200s, ip=%.200s).",
225 pw
->pw_name
, remote_host
, remote_ip
);
226 auth_debug_add("Your host '%.200s' is not "
227 "permitted to use this key for login.",
233 /* Host name matches. */
236 cp
= "permitopen=\"";
237 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
240 char *patterns
= xmalloc(strlen(opts
) + 1);
247 if (*opts
== '\\' && opts
[1] == '"') {
252 patterns
[i
++] = *opts
++;
255 debug("%.100s, line %lu: missing end quote",
257 auth_debug_add("%.100s, line %lu: missing "
258 "end quote", file
, linenum
);
266 if (host
== NULL
|| strlen(host
) >= NI_MAXHOST
) {
267 debug("%.100s, line %lu: Bad permitopen "
268 "specification <%.100s>", file
, linenum
,
270 auth_debug_add("%.100s, line %lu: "
271 "Bad permitopen specification", file
,
276 host
= cleanhostname(host
);
277 if (p
== NULL
|| (port
= a2port(p
)) == 0) {
278 debug("%.100s, line %lu: Bad permitopen port "
279 "<%.100s>", file
, linenum
, p
? p
: "");
280 auth_debug_add("%.100s, line %lu: "
281 "Bad permitopen port", file
, linenum
);
285 if (options
.allow_tcp_forwarding
)
286 channel_add_permitted_opens(host
, port
);
291 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
294 tun
= xmalloc(strlen(opts
) + 1);
302 debug("%.100s, line %lu: missing end quote",
304 auth_debug_add("%.100s, line %lu: missing end quote",
307 forced_tun_device
= -1;
311 forced_tun_device
= a2tun(tun
, NULL
);
313 if (forced_tun_device
== SSH_TUNID_ERR
) {
314 debug("%.100s, line %lu: invalid tun device",
316 auth_debug_add("%.100s, line %lu: invalid tun device",
318 forced_tun_device
= -1;
321 auth_debug_add("Forced tun device: %d", forced_tun_device
);
327 * Skip the comma, and move to the next option
328 * (or break out if there are no more).
331 fatal("Bugs in auth-options.c option processing.");
332 if (*opts
== ' ' || *opts
== '\t')
333 break; /* End of options. */
337 /* Process the next option. */
347 logit("Bad options in %.100s file, line %lu: %.50s",
348 file
, linenum
, opts
);
349 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
350 file
, linenum
, opts
);