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