]> andersk Git - openssh.git/blame - auth-options.c
- djm@cvs.openbsd.org 2005/02/28 00:54:10
[openssh.git] / auth-options.c
CommitLineData
bcbf86ec 1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
bcbf86ec 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".
10 */
11
38c295d6 12#include "includes.h"
c5a7d788 13RCSID("$OpenBSD: auth-options.c,v 1.28 2003/06/02 09:17:34 markus Exp $");
38c295d6 14
38c295d6 15#include "xmalloc.h"
16#include "match.h"
42f11eb2 17#include "log.h"
18#include "canohost.h"
a5efa1bb 19#include "channels.h"
42f11eb2 20#include "auth-options.h"
61e96248 21#include "servconf.h"
ed787d14 22#include "misc.h"
1853d1ef 23#include "monitor_wrap.h"
01dafcb5 24#include "auth.h"
38c295d6 25
26/* Flags set authorized_keys flags */
27int no_port_forwarding_flag = 0;
28int no_agent_forwarding_flag = 0;
29int no_x11_forwarding_flag = 0;
30int no_pty_flag = 0;
31
32/* "command=" option. */
33char *forced_command = NULL;
34
35/* "environment=" options. */
36struct envstring *custom_environment = NULL;
37
61e96248 38extern ServerOptions options;
39
94ec8c6b 40void
41auth_clear_options(void)
42{
43 no_agent_forwarding_flag = 0;
44 no_port_forwarding_flag = 0;
45 no_pty_flag = 0;
46 no_x11_forwarding_flag = 0;
47 while (custom_environment) {
48 struct envstring *ce = custom_environment;
49 custom_environment = ce->next;
50 xfree(ce->s);
51 xfree(ce);
52 }
53 if (forced_command) {
54 xfree(forced_command);
55 forced_command = NULL;
56 }
01794848 57 channel_clear_permitted_opens();
01dafcb5 58 auth_debug_reset();
94ec8c6b 59}
60
42f11eb2 61/*
62 * return 1 if access is granted, 0 if not.
63 * side effect: sets key option flags
64 */
38c295d6 65int
61e96248 66auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
38c295d6 67{
68 const char *cp;
01794848 69 int i;
94ec8c6b 70
71 /* reset options */
72 auth_clear_options();
73
e0b2cf6b 74 if (!opts)
75 return 1;
76
61e96248 77 while (*opts && *opts != ' ' && *opts != '\t') {
38c295d6 78 cp = "no-port-forwarding";
61e96248 79 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
01dafcb5 80 auth_debug_add("Port forwarding disabled.");
38c295d6 81 no_port_forwarding_flag = 1;
61e96248 82 opts += strlen(cp);
38c295d6 83 goto next_option;
84 }
85 cp = "no-agent-forwarding";
61e96248 86 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
01dafcb5 87 auth_debug_add("Agent forwarding disabled.");
38c295d6 88 no_agent_forwarding_flag = 1;
61e96248 89 opts += strlen(cp);
38c295d6 90 goto next_option;
91 }
92 cp = "no-X11-forwarding";
61e96248 93 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
01dafcb5 94 auth_debug_add("X11 forwarding disabled.");
38c295d6 95 no_x11_forwarding_flag = 1;
61e96248 96 opts += strlen(cp);
38c295d6 97 goto next_option;
98 }
99 cp = "no-pty";
61e96248 100 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
01dafcb5 101 auth_debug_add("Pty allocation disabled.");
38c295d6 102 no_pty_flag = 1;
61e96248 103 opts += strlen(cp);
38c295d6 104 goto next_option;
105 }
106 cp = "command=\"";
61e96248 107 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
61e96248 108 opts += strlen(cp);
109 forced_command = xmalloc(strlen(opts) + 1);
38c295d6 110 i = 0;
61e96248 111 while (*opts) {
112 if (*opts == '"')
38c295d6 113 break;
61e96248 114 if (*opts == '\\' && opts[1] == '"') {
115 opts += 2;
38c295d6 116 forced_command[i++] = '"';
117 continue;
118 }
61e96248 119 forced_command[i++] = *opts++;
38c295d6 120 }
61e96248 121 if (!*opts) {
38c295d6 122 debug("%.100s, line %lu: missing end quote",
42f11eb2 123 file, linenum);
01dafcb5 124 auth_debug_add("%.100s, line %lu: missing end quote",
42f11eb2 125 file, linenum);
acc9d6d7 126 xfree(forced_command);
127 forced_command = NULL;
128 goto bad_option;
38c295d6 129 }
130 forced_command[i] = 0;
01dafcb5 131 auth_debug_add("Forced command: %.900s", forced_command);
61e96248 132 opts++;
38c295d6 133 goto next_option;
134 }
135 cp = "environment=\"";
f00bab84 136 if (options.permit_user_env &&
137 strncasecmp(opts, cp, strlen(cp)) == 0) {
38c295d6 138 char *s;
139 struct envstring *new_envstring;
01794848 140
61e96248 141 opts += strlen(cp);
142 s = xmalloc(strlen(opts) + 1);
38c295d6 143 i = 0;
61e96248 144 while (*opts) {
145 if (*opts == '"')
38c295d6 146 break;
61e96248 147 if (*opts == '\\' && opts[1] == '"') {
148 opts += 2;
38c295d6 149 s[i++] = '"';
150 continue;
151 }
61e96248 152 s[i++] = *opts++;
38c295d6 153 }
61e96248 154 if (!*opts) {
38c295d6 155 debug("%.100s, line %lu: missing end quote",
42f11eb2 156 file, linenum);
01dafcb5 157 auth_debug_add("%.100s, line %lu: missing end quote",
42f11eb2 158 file, linenum);
acc9d6d7 159 xfree(s);
160 goto bad_option;
38c295d6 161 }
162 s[i] = 0;
01dafcb5 163 auth_debug_add("Adding to environment: %.900s", s);
38c295d6 164 debug("Adding to environment: %.900s", s);
61e96248 165 opts++;
38c295d6 166 new_envstring = xmalloc(sizeof(struct envstring));
167 new_envstring->s = s;
168 new_envstring->next = custom_environment;
169 custom_environment = new_envstring;
170 goto next_option;
171 }
172 cp = "from=\"";
61e96248 173 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
61e96248 174 const char *remote_ip = get_remote_ipaddr();
175 const char *remote_host = get_canonical_hostname(
c5a7d788 176 options.use_dns);
61e96248 177 char *patterns = xmalloc(strlen(opts) + 1);
01794848 178
61e96248 179 opts += strlen(cp);
38c295d6 180 i = 0;
61e96248 181 while (*opts) {
182 if (*opts == '"')
38c295d6 183 break;
61e96248 184 if (*opts == '\\' && opts[1] == '"') {
185 opts += 2;
38c295d6 186 patterns[i++] = '"';
187 continue;
188 }
61e96248 189 patterns[i++] = *opts++;
38c295d6 190 }
61e96248 191 if (!*opts) {
38c295d6 192 debug("%.100s, line %lu: missing end quote",
42f11eb2 193 file, linenum);
01dafcb5 194 auth_debug_add("%.100s, line %lu: missing end quote",
42f11eb2 195 file, linenum);
acc9d6d7 196 xfree(patterns);
197 goto bad_option;
38c295d6 198 }
199 patterns[i] = 0;
61e96248 200 opts++;
2cee8a25 201 if (match_host_and_ip(remote_host, remote_ip,
202 patterns) != 1) {
203 xfree(patterns);
bbe88b6d 204 logit("Authentication tried for %.100s with "
61e96248 205 "correct key but not from a permitted "
206 "host (host=%.200s, ip=%.200s).",
207 pw->pw_name, remote_host, remote_ip);
01dafcb5 208 auth_debug_add("Your host '%.200s' is not "
61e96248 209 "permitted to use this key for login.",
210 remote_host);
38c295d6 211 /* deny access */
212 return 0;
213 }
2cee8a25 214 xfree(patterns);
38c295d6 215 /* Host name matches. */
216 goto next_option;
217 }
01794848 218 cp = "permitopen=\"";
219 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
ed787d14 220 char host[256], sport[6];
01794848 221 u_short port;
01794848 222 char *patterns = xmalloc(strlen(opts) + 1);
223
224 opts += strlen(cp);
225 i = 0;
226 while (*opts) {
227 if (*opts == '"')
228 break;
229 if (*opts == '\\' && opts[1] == '"') {
230 opts += 2;
231 patterns[i++] = '"';
232 continue;
233 }
234 patterns[i++] = *opts++;
235 }
236 if (!*opts) {
237 debug("%.100s, line %lu: missing end quote",
238 file, linenum);
01dafcb5 239 auth_debug_add("%.100s, line %lu: missing end quote",
01794848 240 file, linenum);
241 xfree(patterns);
242 goto bad_option;
243 }
244 patterns[i] = 0;
245 opts++;
ed787d14 246 if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
247 sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
248 debug("%.100s, line %lu: Bad permitopen specification "
249 "<%.100s>", file, linenum, patterns);
01dafcb5 250 auth_debug_add("%.100s, line %lu: "
ed787d14 251 "Bad permitopen specification", file, linenum);
01794848 252 xfree(patterns);
253 goto bad_option;
254 }
ed787d14 255 if ((port = a2port(sport)) == 0) {
256 debug("%.100s, line %lu: Bad permitopen port <%.100s>",
257 file, linenum, sport);
01dafcb5 258 auth_debug_add("%.100s, line %lu: "
ed787d14 259 "Bad permitopen port", file, linenum);
01794848 260 xfree(patterns);
261 goto bad_option;
262 }
b1ed8313 263 if (options.allow_tcp_forwarding)
ed787d14 264 channel_add_permitted_opens(host, port);
01794848 265 xfree(patterns);
266 goto next_option;
267 }
38c295d6 268next_option:
269 /*
270 * Skip the comma, and move to the next option
271 * (or break out if there are no more).
272 */
61e96248 273 if (!*opts)
38c295d6 274 fatal("Bugs in auth-options.c option processing.");
61e96248 275 if (*opts == ' ' || *opts == '\t')
38c295d6 276 break; /* End of options. */
61e96248 277 if (*opts != ',')
38c295d6 278 goto bad_option;
61e96248 279 opts++;
38c295d6 280 /* Process the next option. */
281 }
1853d1ef 282
283 if (!use_privsep)
01dafcb5 284 auth_debug_send();
1853d1ef 285
38c295d6 286 /* grant access */
287 return 1;
288
289bad_option:
bbe88b6d 290 logit("Bad options in %.100s file, line %lu: %.50s",
61e96248 291 file, linenum, opts);
01dafcb5 292 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
61e96248 293 file, linenum, opts);
1853d1ef 294
295 if (!use_privsep)
01dafcb5 296 auth_debug_send();
1853d1ef 297
38c295d6 298 /* deny access */
299 return 0;
300}
This page took 0.650102 seconds and 5 git commands to generate.