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