1 /* $OpenBSD: auth-options.c,v 1.43 2008/06/10 23:06:19 djm 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>
23 #include "openbsd-compat/sys-queue.h"
30 #include "auth-options.h"
39 #include "monitor_wrap.h"
41 /* Flags set authorized_keys flags */
42 int no_port_forwarding_flag
= 0;
43 int no_agent_forwarding_flag
= 0;
44 int no_x11_forwarding_flag
= 0;
48 /* "command=" option. */
49 char *forced_command
= NULL
;
51 /* "environment=" options. */
52 struct envstring
*custom_environment
= NULL
;
54 /* "tunnel=" option. */
55 int forced_tun_device
= -1;
57 extern ServerOptions options
;
60 auth_clear_options(void)
62 no_agent_forwarding_flag
= 0;
63 no_port_forwarding_flag
= 0;
65 no_x11_forwarding_flag
= 0;
67 while (custom_environment
) {
68 struct envstring
*ce
= custom_environment
;
69 custom_environment
= ce
->next
;
74 xfree(forced_command
);
75 forced_command
= NULL
;
77 forced_tun_device
= -1;
78 channel_clear_permitted_opens();
83 * return 1 if access is granted, 0 if not.
84 * side effect: sets key option flags
87 auth_parse_options(struct passwd
*pw
, char *opts
, char *file
, u_long linenum
)
98 while (*opts
&& *opts
!= ' ' && *opts
!= '\t') {
99 cp
= "no-port-forwarding";
100 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
101 auth_debug_add("Port forwarding disabled.");
102 no_port_forwarding_flag
= 1;
106 cp
= "no-agent-forwarding";
107 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
108 auth_debug_add("Agent forwarding disabled.");
109 no_agent_forwarding_flag
= 1;
113 cp
= "no-X11-forwarding";
114 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
115 auth_debug_add("X11 forwarding disabled.");
116 no_x11_forwarding_flag
= 1;
121 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
122 auth_debug_add("Pty allocation disabled.");
128 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
129 auth_debug_add("User rc file execution disabled.");
135 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
137 forced_command
= xmalloc(strlen(opts
) + 1);
142 if (*opts
== '\\' && opts
[1] == '"') {
144 forced_command
[i
++] = '"';
147 forced_command
[i
++] = *opts
++;
150 debug("%.100s, line %lu: missing end quote",
152 auth_debug_add("%.100s, line %lu: missing end quote",
154 xfree(forced_command
);
155 forced_command
= NULL
;
158 forced_command
[i
] = '\0';
159 auth_debug_add("Forced command: %.900s", forced_command
);
163 cp
= "environment=\"";
164 if (options
.permit_user_env
&&
165 strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
167 struct envstring
*new_envstring
;
170 s
= xmalloc(strlen(opts
) + 1);
175 if (*opts
== '\\' && opts
[1] == '"') {
183 debug("%.100s, line %lu: missing end quote",
185 auth_debug_add("%.100s, line %lu: missing end quote",
191 auth_debug_add("Adding to environment: %.900s", s
);
192 debug("Adding to environment: %.900s", s
);
194 new_envstring
= xmalloc(sizeof(struct envstring
));
195 new_envstring
->s
= s
;
196 new_envstring
->next
= custom_environment
;
197 custom_environment
= new_envstring
;
201 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
202 const char *remote_ip
= get_remote_ipaddr();
203 const char *remote_host
= get_canonical_hostname(
205 char *patterns
= xmalloc(strlen(opts
) + 1);
212 if (*opts
== '\\' && opts
[1] == '"') {
217 patterns
[i
++] = *opts
++;
220 debug("%.100s, line %lu: missing end quote",
222 auth_debug_add("%.100s, line %lu: missing end quote",
229 switch (match_host_and_ip(remote_host
, remote_ip
,
233 /* Host name matches. */
236 debug("%.100s, line %lu: invalid criteria",
238 auth_debug_add("%.100s, line %lu: "
239 "invalid criteria", file
, linenum
);
243 logit("Authentication tried for %.100s with "
244 "correct key but not from a permitted "
245 "host (host=%.200s, ip=%.200s).",
246 pw
->pw_name
, remote_host
, remote_ip
);
247 auth_debug_add("Your host '%.200s' is not "
248 "permitted to use this key for login.",
255 cp
= "permitopen=\"";
256 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
259 char *patterns
= xmalloc(strlen(opts
) + 1);
266 if (*opts
== '\\' && opts
[1] == '"') {
271 patterns
[i
++] = *opts
++;
274 debug("%.100s, line %lu: missing end quote",
276 auth_debug_add("%.100s, line %lu: missing "
277 "end quote", file
, linenum
);
285 if (host
== NULL
|| strlen(host
) >= NI_MAXHOST
) {
286 debug("%.100s, line %lu: Bad permitopen "
287 "specification <%.100s>", file
, linenum
,
289 auth_debug_add("%.100s, line %lu: "
290 "Bad permitopen specification", file
,
295 host
= cleanhostname(host
);
296 if (p
== NULL
|| (port
= a2port(p
)) == 0) {
297 debug("%.100s, line %lu: Bad permitopen port "
298 "<%.100s>", file
, linenum
, p
? p
: "");
299 auth_debug_add("%.100s, line %lu: "
300 "Bad permitopen port", file
, linenum
);
304 if (options
.allow_tcp_forwarding
)
305 channel_add_permitted_opens(host
, port
);
310 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
313 tun
= xmalloc(strlen(opts
) + 1);
321 debug("%.100s, line %lu: missing end quote",
323 auth_debug_add("%.100s, line %lu: missing end quote",
326 forced_tun_device
= -1;
330 forced_tun_device
= a2tun(tun
, NULL
);
332 if (forced_tun_device
== SSH_TUNID_ERR
) {
333 debug("%.100s, line %lu: invalid tun device",
335 auth_debug_add("%.100s, line %lu: invalid tun device",
337 forced_tun_device
= -1;
340 auth_debug_add("Forced tun device: %d", forced_tun_device
);
346 * Skip the comma, and move to the next option
347 * (or break out if there are no more).
350 fatal("Bugs in auth-options.c option processing.");
351 if (*opts
== ' ' || *opts
== '\t')
352 break; /* End of options. */
356 /* Process the next option. */
366 logit("Bad options in %.100s file, line %lu: %.50s",
367 file
, linenum
, opts
);
368 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
369 file
, linenum
, opts
);