2 /* $OpenBSD: auth-options.c,v 1.44 2009/01/22 10:09:16 djm Exp $ */
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
15 __RCSID("$NetBSD: auth-options.c,v 1.10 2009/02/16 20:53:54 christos Exp $");
16 #include <sys/types.h>
17 #include <sys/queue.h>
32 #include "auth-options.h"
41 #include "monitor_wrap.h"
43 /* Flags set authorized_keys flags */
44 int no_port_forwarding_flag
= 0;
45 int no_agent_forwarding_flag
= 0;
46 int no_x11_forwarding_flag
= 0;
50 /* "command=" option. */
51 char *forced_command
= NULL
;
53 /* "environment=" options. */
54 struct envstring
*custom_environment
= NULL
;
56 /* "tunnel=" option. */
57 int forced_tun_device
= -1;
59 extern ServerOptions options
;
62 auth_clear_options(void)
64 no_agent_forwarding_flag
= 0;
65 no_port_forwarding_flag
= 0;
67 no_x11_forwarding_flag
= 0;
69 while (custom_environment
) {
70 struct envstring
*ce
= custom_environment
;
71 custom_environment
= ce
->next
;
76 xfree(forced_command
);
77 forced_command
= NULL
;
79 forced_tun_device
= -1;
80 channel_clear_permitted_opens();
85 * return 1 if access is granted, 0 if not.
86 * side effect: sets key option flags
89 auth_parse_options(struct passwd
*pw
, char *opts
, char *file
, u_long linenum
)
100 while (*opts
&& *opts
!= ' ' && *opts
!= '\t') {
101 cp
= "no-port-forwarding";
102 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
103 auth_debug_add("Port forwarding disabled.");
104 no_port_forwarding_flag
= 1;
108 cp
= "no-agent-forwarding";
109 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
110 auth_debug_add("Agent forwarding disabled.");
111 no_agent_forwarding_flag
= 1;
115 cp
= "no-X11-forwarding";
116 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
117 auth_debug_add("X11 forwarding disabled.");
118 no_x11_forwarding_flag
= 1;
123 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
124 auth_debug_add("Pty allocation disabled.");
130 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
131 auth_debug_add("User rc file execution disabled.");
137 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
139 forced_command
= xmalloc(strlen(opts
) + 1);
144 if (*opts
== '\\' && opts
[1] == '"') {
146 forced_command
[i
++] = '"';
149 forced_command
[i
++] = *opts
++;
152 debug("%.100s, line %lu: missing end quote",
154 auth_debug_add("%.100s, line %lu: missing end quote",
156 xfree(forced_command
);
157 forced_command
= NULL
;
160 forced_command
[i
] = '\0';
161 auth_debug_add("Forced command: %.900s", forced_command
);
165 cp
= "environment=\"";
166 if (options
.permit_user_env
&&
167 strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
169 struct envstring
*new_envstring
;
172 s
= xmalloc(strlen(opts
) + 1);
177 if (*opts
== '\\' && opts
[1] == '"') {
185 debug("%.100s, line %lu: missing end quote",
187 auth_debug_add("%.100s, line %lu: missing end quote",
193 auth_debug_add("Adding to environment: %.900s", s
);
194 debug("Adding to environment: %.900s", s
);
196 new_envstring
= xmalloc(sizeof(struct envstring
));
197 new_envstring
->s
= s
;
198 new_envstring
->next
= custom_environment
;
199 custom_environment
= new_envstring
;
203 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
204 const char *remote_ip
= get_remote_ipaddr();
205 const char *remote_host
= get_canonical_hostname(
207 char *patterns
= xmalloc(strlen(opts
) + 1);
214 if (*opts
== '\\' && opts
[1] == '"') {
219 patterns
[i
++] = *opts
++;
222 debug("%.100s, line %lu: missing end quote",
224 auth_debug_add("%.100s, line %lu: missing end quote",
231 switch (match_host_and_ip(remote_host
, remote_ip
,
235 /* Host name matches. */
238 debug("%.100s, line %lu: invalid criteria",
240 auth_debug_add("%.100s, line %lu: "
241 "invalid criteria", file
, linenum
);
245 logit("Authentication tried for %.100s with "
246 "correct key but not from a permitted "
247 "host (host=%.200s, ip=%.200s).",
248 pw
->pw_name
, remote_host
, remote_ip
);
249 auth_debug_add("Your host '%.200s' is not "
250 "permitted to use this key for login.",
257 cp
= "permitopen=\"";
258 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
261 char *patterns
= xmalloc(strlen(opts
) + 1);
268 if (*opts
== '\\' && opts
[1] == '"') {
273 patterns
[i
++] = *opts
++;
276 debug("%.100s, line %lu: missing end quote",
278 auth_debug_add("%.100s, line %lu: missing "
279 "end quote", file
, linenum
);
287 if (host
== NULL
|| strlen(host
) >= NI_MAXHOST
) {
288 debug("%.100s, line %lu: Bad permitopen "
289 "specification <%.100s>", file
, linenum
,
291 auth_debug_add("%.100s, line %lu: "
292 "Bad permitopen specification", file
,
297 host
= cleanhostname(host
);
298 if (p
== NULL
|| (port
= a2port(p
)) <= 0) {
299 debug("%.100s, line %lu: Bad permitopen port "
300 "<%.100s>", file
, linenum
, p
? p
: "");
301 auth_debug_add("%.100s, line %lu: "
302 "Bad permitopen port", file
, linenum
);
306 if (options
.allow_tcp_forwarding
)
307 channel_add_permitted_opens(host
, port
);
312 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
315 tun
= xmalloc(strlen(opts
) + 1);
323 debug("%.100s, line %lu: missing end quote",
325 auth_debug_add("%.100s, line %lu: missing end quote",
328 forced_tun_device
= -1;
332 forced_tun_device
= a2tun(tun
, NULL
);
334 if (forced_tun_device
== SSH_TUNID_ERR
) {
335 debug("%.100s, line %lu: invalid tun device",
337 auth_debug_add("%.100s, line %lu: invalid tun device",
339 forced_tun_device
= -1;
342 auth_debug_add("Forced tun device: %d", forced_tun_device
);
348 * Skip the comma, and move to the next option
349 * (or break out if there are no more).
352 fatal("Bugs in auth-options.c option processing.");
353 if (*opts
== ' ' || *opts
== '\t')
354 break; /* End of options. */
358 /* Process the next option. */
368 logit("Bad options in %.100s file, line %lu: %.50s",
369 file
, linenum
, opts
);
370 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
371 file
, linenum
, opts
);