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.23 2002/03/19 10:35:39 markus Exp $");
21 #include "auth-options.h"
25 #include "monitor_wrap.h"
27 /* Debugging messages */
31 /* Flags set authorized_keys flags */
32 int no_port_forwarding_flag = 0;
33 int no_agent_forwarding_flag = 0;
34 int no_x11_forwarding_flag = 0;
37 /* "command=" option. */
38 char *forced_command = NULL;
40 /* "environment=" options. */
41 struct envstring *custom_environment = NULL;
43 extern ServerOptions options;
46 auth_send_debug(Buffer *m)
50 while (buffer_len(m)) {
51 msg = buffer_get_string(m, NULL);
52 packet_send_debug("%s", msg);
58 auth_clear_options(void)
61 buffer_clear(&auth_debug);
63 buffer_init(&auth_debug);
67 no_agent_forwarding_flag = 0;
68 no_port_forwarding_flag = 0;
70 no_x11_forwarding_flag = 0;
71 while (custom_environment) {
72 struct envstring *ce = custom_environment;
73 custom_environment = ce->next;
78 xfree(forced_command);
79 forced_command = NULL;
81 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)
101 while (*opts && *opts != ' ' && *opts != '\t') {
102 cp = "no-port-forwarding";
103 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
104 snprintf(tmp, sizeof(tmp), "Port forwarding disabled.");
105 buffer_put_cstring(&auth_debug, tmp);
106 no_port_forwarding_flag = 1;
110 cp = "no-agent-forwarding";
111 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
112 snprintf(tmp, sizeof(tmp), "Agent forwarding disabled.");
113 buffer_put_cstring(&auth_debug, tmp);
114 no_agent_forwarding_flag = 1;
118 cp = "no-X11-forwarding";
119 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
120 snprintf(tmp, sizeof(tmp), "X11 forwarding disabled.");
121 buffer_put_cstring(&auth_debug, tmp);
122 no_x11_forwarding_flag = 1;
127 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
128 snprintf(tmp, sizeof(tmp), "Pty allocation disabled.");
129 buffer_put_cstring(&auth_debug, tmp);
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 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
154 buffer_put_cstring(&auth_debug, tmp);
155 xfree(forced_command);
156 forced_command = NULL;
159 forced_command[i] = 0;
160 snprintf(tmp, sizeof(tmp), "Forced command: %.900s", forced_command);
161 buffer_put_cstring(&auth_debug, tmp);
165 cp = "environment=\"";
166 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
168 struct envstring *new_envstring;
171 s = xmalloc(strlen(opts) + 1);
176 if (*opts == '\\' && opts[1] == '"') {
184 debug("%.100s, line %lu: missing end quote",
186 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
188 buffer_put_cstring(&auth_debug, tmp);
193 snprintf(tmp, sizeof(tmp), "Adding to environment: %.900s", s);
194 buffer_put_cstring(&auth_debug, tmp);
195 debug("Adding to environment: %.900s", s);
197 new_envstring = xmalloc(sizeof(struct envstring));
198 new_envstring->s = s;
199 new_envstring->next = custom_environment;
200 custom_environment = new_envstring;
204 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
205 const char *remote_ip = get_remote_ipaddr();
206 const char *remote_host = get_canonical_hostname(
207 options.verify_reverse_mapping);
208 char *patterns = xmalloc(strlen(opts) + 1);
215 if (*opts == '\\' && opts[1] == '"') {
220 patterns[i++] = *opts++;
223 debug("%.100s, line %lu: missing end quote",
225 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
227 buffer_put_cstring(&auth_debug, tmp);
233 if (match_host_and_ip(remote_host, remote_ip,
236 log("Authentication tried for %.100s with "
237 "correct key but not from a permitted "
238 "host (host=%.200s, ip=%.200s).",
239 pw->pw_name, remote_host, remote_ip);
240 snprintf(tmp, sizeof(tmp),
241 "Your host '%.200s' is not "
242 "permitted to use this key for login.",
244 buffer_put_cstring(&auth_debug, tmp);
249 /* Host name matches. */
252 cp = "permitopen=\"";
253 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
254 char host[256], sport[6];
256 char *patterns = xmalloc(strlen(opts) + 1);
263 if (*opts == '\\' && opts[1] == '"') {
268 patterns[i++] = *opts++;
271 debug("%.100s, line %lu: missing end quote",
273 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
275 buffer_put_cstring(&auth_debug, tmp);
281 if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
282 sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
283 debug("%.100s, line %lu: Bad permitopen specification "
284 "<%.100s>", file, linenum, patterns);
285 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: "
286 "Bad permitopen specification", file, linenum);
287 buffer_put_cstring(&auth_debug, tmp);
291 if ((port = a2port(sport)) == 0) {
292 debug("%.100s, line %lu: Bad permitopen port <%.100s>",
293 file, linenum, sport);
294 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: "
295 "Bad permitopen port", file, linenum);
296 buffer_put_cstring(&auth_debug, tmp);
300 if (options.allow_tcp_forwarding)
301 channel_add_permitted_opens(host, port);
307 * Skip the comma, and move to the next option
308 * (or break out if there are no more).
311 fatal("Bugs in auth-options.c option processing.");
312 if (*opts == ' ' || *opts == '\t')
313 break; /* End of options. */
317 /* Process the next option. */
321 auth_send_debug(&auth_debug);
327 log("Bad options in %.100s file, line %lu: %.50s",
328 file, linenum, opts);
329 snprintf(tmp, sizeof(tmp),
330 "Bad options in %.100s file, line %lu: %.50s",
331 file, linenum, opts);
332 buffer_put_cstring(&auth_debug, tmp);
335 auth_send_debug(&auth_debug);