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