2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
13 RCSID("$OpenBSD: auth-options.c,v 1.8 2001/01/13 18:38:00 markus Exp $");
20 /* Flags set authorized_keys flags */
21 int no_port_forwarding_flag = 0;
22 int no_agent_forwarding_flag = 0;
23 int no_x11_forwarding_flag = 0;
26 /* "command=" option. */
27 char *forced_command = NULL;
29 /* "environment=" options. */
30 struct envstring *custom_environment = NULL;
33 auth_clear_options(void)
35 no_agent_forwarding_flag = 0;
36 no_port_forwarding_flag = 0;
38 no_x11_forwarding_flag = 0;
39 while (custom_environment) {
40 struct envstring *ce = custom_environment;
41 custom_environment = ce->next;
46 xfree(forced_command);
47 forced_command = NULL;
51 /* return 1 if access is granted, 0 if not. side effect: sets key option flags */
53 auth_parse_options(struct passwd *pw, char *options, u_long linenum)
62 while (*options && *options != ' ' && *options != '\t') {
63 cp = "no-port-forwarding";
64 if (strncasecmp(options, cp, strlen(cp)) == 0) {
65 packet_send_debug("Port forwarding disabled.");
66 no_port_forwarding_flag = 1;
67 options += strlen(cp);
70 cp = "no-agent-forwarding";
71 if (strncasecmp(options, cp, strlen(cp)) == 0) {
72 packet_send_debug("Agent forwarding disabled.");
73 no_agent_forwarding_flag = 1;
74 options += strlen(cp);
77 cp = "no-X11-forwarding";
78 if (strncasecmp(options, cp, strlen(cp)) == 0) {
79 packet_send_debug("X11 forwarding disabled.");
80 no_x11_forwarding_flag = 1;
81 options += strlen(cp);
85 if (strncasecmp(options, cp, strlen(cp)) == 0) {
86 packet_send_debug("Pty allocation disabled.");
88 options += strlen(cp);
92 if (strncasecmp(options, cp, strlen(cp)) == 0) {
94 options += strlen(cp);
95 forced_command = xmalloc(strlen(options) + 1);
100 if (*options == '\\' && options[1] == '"') {
102 forced_command[i++] = '"';
105 forced_command[i++] = *options++;
108 debug("%.100s, line %lu: missing end quote",
109 SSH_USER_PERMITTED_KEYS, linenum);
110 packet_send_debug("%.100s, line %lu: missing end quote",
111 SSH_USER_PERMITTED_KEYS, linenum);
114 forced_command[i] = 0;
115 packet_send_debug("Forced command: %.900s", forced_command);
119 cp = "environment=\"";
120 if (strncasecmp(options, cp, strlen(cp)) == 0) {
123 struct envstring *new_envstring;
124 options += strlen(cp);
125 s = xmalloc(strlen(options) + 1);
130 if (*options == '\\' && options[1] == '"') {
138 debug("%.100s, line %lu: missing end quote",
139 SSH_USER_PERMITTED_KEYS, linenum);
140 packet_send_debug("%.100s, line %lu: missing end quote",
141 SSH_USER_PERMITTED_KEYS, linenum);
145 packet_send_debug("Adding to environment: %.900s", s);
146 debug("Adding to environment: %.900s", s);
148 new_envstring = xmalloc(sizeof(struct envstring));
149 new_envstring->s = s;
150 new_envstring->next = custom_environment;
151 custom_environment = new_envstring;
155 if (strncasecmp(options, cp, strlen(cp)) == 0) {
157 char *patterns = xmalloc(strlen(options) + 1);
159 options += strlen(cp);
164 if (*options == '\\' && options[1] == '"') {
169 patterns[i++] = *options++;
172 debug("%.100s, line %lu: missing end quote",
173 SSH_USER_PERMITTED_KEYS, linenum);
174 packet_send_debug("%.100s, line %lu: missing end quote",
175 SSH_USER_PERMITTED_KEYS, linenum);
181 * Deny access if we get a negative
182 * match for the hostname or the ip
183 * or if we get not match at all
185 mname = match_hostname(get_canonical_hostname(),
186 patterns, strlen(patterns));
187 mip = match_hostname(get_remote_ipaddr(),
188 patterns, strlen(patterns));
190 if (mname == -1 || mip == -1 ||
191 (mname != 1 && mip != 1)) {
192 log("Authentication tried for %.100s with correct key but not from a permitted host (host=%.200s, ip=%.200s).",
193 pw->pw_name, get_canonical_hostname(),
194 get_remote_ipaddr());
195 packet_send_debug("Your host '%.200s' is not permitted to use this key for login.",
196 get_canonical_hostname());
200 /* Host name matches. */
205 * Skip the comma, and move to the next option
206 * (or break out if there are no more).
209 fatal("Bugs in auth-options.c option processing.");
210 if (*options == ' ' || *options == '\t')
211 break; /* End of options. */
215 /* Process the next option. */
221 log("Bad options in %.100s file, line %lu: %.50s",
222 SSH_USER_PERMITTED_KEYS, linenum, options);
223 packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
224 SSH_USER_PERMITTED_KEYS, linenum, options);