1 /* $OpenBSD: auth-options.c,v 1.41 2008/03/26 21:28:14 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>
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;
47 /* "command=" option. */
48 char *forced_command = NULL;
50 /* "environment=" options. */
51 struct envstring *custom_environment = NULL;
53 /* "tunnel=" option. */
54 int forced_tun_device = -1;
56 extern ServerOptions options;
59 auth_clear_options(void)
61 no_agent_forwarding_flag = 0;
62 no_port_forwarding_flag = 0;
64 no_x11_forwarding_flag = 0;
66 while (custom_environment) {
67 struct envstring *ce = custom_environment;
68 custom_environment = ce->next;
73 xfree(forced_command);
74 forced_command = NULL;
76 forced_tun_device = -1;
77 channel_clear_permitted_opens();
82 * return 1 if access is granted, 0 if not.
83 * side effect: sets key option flags
86 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
97 while (*opts && *opts != ' ' && *opts != '\t') {
98 cp = "no-port-forwarding";
99 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
100 auth_debug_add("Port forwarding disabled.");
101 no_port_forwarding_flag = 1;
105 cp = "no-agent-forwarding";
106 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
107 auth_debug_add("Agent forwarding disabled.");
108 no_agent_forwarding_flag = 1;
112 cp = "no-X11-forwarding";
113 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
114 auth_debug_add("X11 forwarding disabled.");
115 no_x11_forwarding_flag = 1;
120 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
121 auth_debug_add("Pty allocation disabled.");
127 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
128 auth_debug_add("User rc file execution disabled.");
134 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
136 forced_command = xmalloc(strlen(opts) + 1);
141 if (*opts == '\\' && opts[1] == '"') {
143 forced_command[i++] = '"';
146 forced_command[i++] = *opts++;
149 debug("%.100s, line %lu: missing end quote",
151 auth_debug_add("%.100s, line %lu: missing end quote",
153 xfree(forced_command);
154 forced_command = NULL;
157 forced_command[i] = '\0';
158 auth_debug_add("Forced command: %.900s", forced_command);
162 cp = "environment=\"";
163 if (options.permit_user_env &&
164 strncasecmp(opts, cp, strlen(cp)) == 0) {
166 struct envstring *new_envstring;
169 s = xmalloc(strlen(opts) + 1);
174 if (*opts == '\\' && opts[1] == '"') {
182 debug("%.100s, line %lu: missing end quote",
184 auth_debug_add("%.100s, line %lu: missing end quote",
190 auth_debug_add("Adding to environment: %.900s", s);
191 debug("Adding to environment: %.900s", s);
193 new_envstring = xmalloc(sizeof(struct envstring));
194 new_envstring->s = s;
195 new_envstring->next = custom_environment;
196 custom_environment = new_envstring;
200 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
201 const char *remote_ip = get_remote_ipaddr();
202 const char *remote_host = get_canonical_hostname(
204 char *patterns = xmalloc(strlen(opts) + 1);
211 if (*opts == '\\' && opts[1] == '"') {
216 patterns[i++] = *opts++;
219 debug("%.100s, line %lu: missing end quote",
221 auth_debug_add("%.100s, line %lu: missing end quote",
228 if (match_host_and_ip(remote_host, remote_ip,
231 logit("Authentication tried for %.100s with "
232 "correct key but not from a permitted "
233 "host (host=%.200s, ip=%.200s).",
234 pw->pw_name, remote_host, remote_ip);
235 auth_debug_add("Your host '%.200s' is not "
236 "permitted to use this key for login.",
242 /* Host name matches. */
245 cp = "permitopen=\"";
246 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
249 char *patterns = xmalloc(strlen(opts) + 1);
256 if (*opts == '\\' && opts[1] == '"') {
261 patterns[i++] = *opts++;
264 debug("%.100s, line %lu: missing end quote",
266 auth_debug_add("%.100s, line %lu: missing "
267 "end quote", file, linenum);
275 if (host == NULL || strlen(host) >= NI_MAXHOST) {
276 debug("%.100s, line %lu: Bad permitopen "
277 "specification <%.100s>", file, linenum,
279 auth_debug_add("%.100s, line %lu: "
280 "Bad permitopen specification", file,
285 host = cleanhostname(host);
286 if (p == NULL || (port = a2port(p)) == 0) {
287 debug("%.100s, line %lu: Bad permitopen port "
288 "<%.100s>", file, linenum, p ? p : "");
289 auth_debug_add("%.100s, line %lu: "
290 "Bad permitopen port", file, linenum);
294 if (options.allow_tcp_forwarding)
295 channel_add_permitted_opens(host, port);
300 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
303 tun = xmalloc(strlen(opts) + 1);
311 debug("%.100s, line %lu: missing end quote",
313 auth_debug_add("%.100s, line %lu: missing end quote",
316 forced_tun_device = -1;
320 forced_tun_device = a2tun(tun, NULL);
322 if (forced_tun_device == SSH_TUNID_ERR) {
323 debug("%.100s, line %lu: invalid tun device",
325 auth_debug_add("%.100s, line %lu: invalid tun device",
327 forced_tun_device = -1;
330 auth_debug_add("Forced tun device: %d", forced_tun_device);
336 * Skip the comma, and move to the next option
337 * (or break out if there are no more).
340 fatal("Bugs in auth-options.c option processing.");
341 if (*opts == ' ' || *opts == '\t')
342 break; /* End of options. */
346 /* Process the next option. */
356 logit("Bad options in %.100s file, line %lu: %.50s",
357 file, linenum, opts);
358 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
359 file, linenum, opts);