1 /* $OpenBSD: auth-options.c,v 1.38 2006/07/17 12:02:24 dtucker 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)
27 #include "auth-options.h"
30 #include "monitor_wrap.h"
33 /* Flags set authorized_keys flags */
34 int no_port_forwarding_flag
= 0;
35 int no_agent_forwarding_flag
= 0;
36 int no_x11_forwarding_flag
= 0;
39 /* "command=" option. */
40 char *forced_command
= NULL
;
42 /* "environment=" options. */
43 struct envstring
*custom_environment
= NULL
;
45 /* "tunnel=" option. */
46 int forced_tun_device
= -1;
48 extern ServerOptions options
;
51 auth_clear_options(void)
53 no_agent_forwarding_flag
= 0;
54 no_port_forwarding_flag
= 0;
56 no_x11_forwarding_flag
= 0;
57 while (custom_environment
) {
58 struct envstring
*ce
= custom_environment
;
59 custom_environment
= ce
->next
;
64 xfree(forced_command
);
65 forced_command
= NULL
;
67 forced_tun_device
= -1;
68 channel_clear_permitted_opens();
73 * return 1 if access is granted, 0 if not.
74 * side effect: sets key option flags
77 auth_parse_options(struct passwd
*pw
, char *opts
, char *file
, u_long linenum
)
88 while (*opts
&& *opts
!= ' ' && *opts
!= '\t') {
89 cp
= "no-port-forwarding";
90 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
91 auth_debug_add("Port forwarding disabled.");
92 no_port_forwarding_flag
= 1;
96 cp
= "no-agent-forwarding";
97 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
98 auth_debug_add("Agent forwarding disabled.");
99 no_agent_forwarding_flag
= 1;
103 cp
= "no-X11-forwarding";
104 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
105 auth_debug_add("X11 forwarding disabled.");
106 no_x11_forwarding_flag
= 1;
111 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
112 auth_debug_add("Pty allocation disabled.");
118 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
120 forced_command
= xmalloc(strlen(opts
) + 1);
125 if (*opts
== '\\' && opts
[1] == '"') {
127 forced_command
[i
++] = '"';
130 forced_command
[i
++] = *opts
++;
133 debug("%.100s, line %lu: missing end quote",
135 auth_debug_add("%.100s, line %lu: missing end quote",
137 xfree(forced_command
);
138 forced_command
= NULL
;
141 forced_command
[i
] = '\0';
142 auth_debug_add("Forced command: %.900s", forced_command
);
146 cp
= "environment=\"";
147 if (options
.permit_user_env
&&
148 strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
150 struct envstring
*new_envstring
;
153 s
= xmalloc(strlen(opts
) + 1);
158 if (*opts
== '\\' && opts
[1] == '"') {
166 debug("%.100s, line %lu: missing end quote",
168 auth_debug_add("%.100s, line %lu: missing end quote",
174 auth_debug_add("Adding to environment: %.900s", s
);
175 debug("Adding to environment: %.900s", s
);
177 new_envstring
= xmalloc(sizeof(struct envstring
));
178 new_envstring
->s
= s
;
179 new_envstring
->next
= custom_environment
;
180 custom_environment
= new_envstring
;
184 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
185 const char *remote_ip
= get_remote_ipaddr();
186 const char *remote_host
= get_canonical_hostname(
188 char *patterns
= xmalloc(strlen(opts
) + 1);
195 if (*opts
== '\\' && opts
[1] == '"') {
200 patterns
[i
++] = *opts
++;
203 debug("%.100s, line %lu: missing end quote",
205 auth_debug_add("%.100s, line %lu: missing end quote",
212 if (match_host_and_ip(remote_host
, remote_ip
,
215 logit("Authentication tried for %.100s with "
216 "correct key but not from a permitted "
217 "host (host=%.200s, ip=%.200s).",
218 pw
->pw_name
, remote_host
, remote_ip
);
219 auth_debug_add("Your host '%.200s' is not "
220 "permitted to use this key for login.",
226 /* Host name matches. */
229 cp
= "permitopen=\"";
230 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
233 char *patterns
= xmalloc(strlen(opts
) + 1);
240 if (*opts
== '\\' && opts
[1] == '"') {
245 patterns
[i
++] = *opts
++;
248 debug("%.100s, line %lu: missing end quote",
250 auth_debug_add("%.100s, line %lu: missing "
251 "end quote", file
, linenum
);
259 if (host
== NULL
|| strlen(host
) >= NI_MAXHOST
) {
260 debug("%.100s, line %lu: Bad permitopen "
261 "specification <%.100s>", file
, linenum
,
263 auth_debug_add("%.100s, line %lu: "
264 "Bad permitopen specification", file
,
269 host
= cleanhostname(host
);
270 if (p
== NULL
|| (port
= a2port(p
)) == 0) {
271 debug("%.100s, line %lu: Bad permitopen port "
272 "<%.100s>", file
, linenum
, p
? p
: "");
273 auth_debug_add("%.100s, line %lu: "
274 "Bad permitopen port", file
, linenum
);
278 if (options
.allow_tcp_forwarding
)
279 channel_add_permitted_opens(host
, port
);
284 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
287 tun
= xmalloc(strlen(opts
) + 1);
295 debug("%.100s, line %lu: missing end quote",
297 auth_debug_add("%.100s, line %lu: missing end quote",
300 forced_tun_device
= -1;
304 forced_tun_device
= a2tun(tun
, NULL
);
306 if (forced_tun_device
== SSH_TUNID_ERR
) {
307 debug("%.100s, line %lu: invalid tun device",
309 auth_debug_add("%.100s, line %lu: invalid tun device",
311 forced_tun_device
= -1;
314 auth_debug_add("Forced tun device: %d", forced_tun_device
);
320 * Skip the comma, and move to the next option
321 * (or break out if there are no more).
324 fatal("Bugs in auth-options.c option processing.");
325 if (*opts
== ' ' || *opts
== '\t')
326 break; /* End of options. */
330 /* Process the next option. */
340 logit("Bad options in %.100s file, line %lu: %.50s",
341 file
, linenum
, opts
);
342 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
343 file
, linenum
, opts
);