- djm@cvs.openbsd.org 2006/07/10 12:03:20
[openssh-git.git] / auth-options.c
blob473fb8bf7b2fe4d7b058e15e0f147f3c52a66880
1 /* $OpenBSD: auth-options.c,v 1.36 2006/07/06 16:03:53 stevesk Exp $ */
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
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".
13 #include "includes.h"
15 #include <sys/types.h>
17 #include <pwd.h>
19 #include "xmalloc.h"
20 #include "match.h"
21 #include "log.h"
22 #include "canohost.h"
23 #include "channels.h"
24 #include "auth-options.h"
25 #include "servconf.h"
26 #include "misc.h"
27 #include "monitor_wrap.h"
28 #include "auth.h"
30 /* Flags set authorized_keys flags */
31 int no_port_forwarding_flag = 0;
32 int no_agent_forwarding_flag = 0;
33 int no_x11_forwarding_flag = 0;
34 int no_pty_flag = 0;
36 /* "command=" option. */
37 char *forced_command = NULL;
39 /* "environment=" options. */
40 struct envstring *custom_environment = NULL;
42 /* "tunnel=" option. */
43 int forced_tun_device = -1;
45 extern ServerOptions options;
47 void
48 auth_clear_options(void)
50 no_agent_forwarding_flag = 0;
51 no_port_forwarding_flag = 0;
52 no_pty_flag = 0;
53 no_x11_forwarding_flag = 0;
54 while (custom_environment) {
55 struct envstring *ce = custom_environment;
56 custom_environment = ce->next;
57 xfree(ce->s);
58 xfree(ce);
60 if (forced_command) {
61 xfree(forced_command);
62 forced_command = NULL;
64 forced_tun_device = -1;
65 channel_clear_permitted_opens();
66 auth_debug_reset();
70 * return 1 if access is granted, 0 if not.
71 * side effect: sets key option flags
73 int
74 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
76 const char *cp;
77 int i;
79 /* reset options */
80 auth_clear_options();
82 if (!opts)
83 return 1;
85 while (*opts && *opts != ' ' && *opts != '\t') {
86 cp = "no-port-forwarding";
87 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
88 auth_debug_add("Port forwarding disabled.");
89 no_port_forwarding_flag = 1;
90 opts += strlen(cp);
91 goto next_option;
93 cp = "no-agent-forwarding";
94 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
95 auth_debug_add("Agent forwarding disabled.");
96 no_agent_forwarding_flag = 1;
97 opts += strlen(cp);
98 goto next_option;
100 cp = "no-X11-forwarding";
101 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
102 auth_debug_add("X11 forwarding disabled.");
103 no_x11_forwarding_flag = 1;
104 opts += strlen(cp);
105 goto next_option;
107 cp = "no-pty";
108 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
109 auth_debug_add("Pty allocation disabled.");
110 no_pty_flag = 1;
111 opts += strlen(cp);
112 goto next_option;
114 cp = "command=\"";
115 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
116 opts += strlen(cp);
117 forced_command = xmalloc(strlen(opts) + 1);
118 i = 0;
119 while (*opts) {
120 if (*opts == '"')
121 break;
122 if (*opts == '\\' && opts[1] == '"') {
123 opts += 2;
124 forced_command[i++] = '"';
125 continue;
127 forced_command[i++] = *opts++;
129 if (!*opts) {
130 debug("%.100s, line %lu: missing end quote",
131 file, linenum);
132 auth_debug_add("%.100s, line %lu: missing end quote",
133 file, linenum);
134 xfree(forced_command);
135 forced_command = NULL;
136 goto bad_option;
138 forced_command[i] = 0;
139 auth_debug_add("Forced command: %.900s", forced_command);
140 opts++;
141 goto next_option;
143 cp = "environment=\"";
144 if (options.permit_user_env &&
145 strncasecmp(opts, cp, strlen(cp)) == 0) {
146 char *s;
147 struct envstring *new_envstring;
149 opts += strlen(cp);
150 s = xmalloc(strlen(opts) + 1);
151 i = 0;
152 while (*opts) {
153 if (*opts == '"')
154 break;
155 if (*opts == '\\' && opts[1] == '"') {
156 opts += 2;
157 s[i++] = '"';
158 continue;
160 s[i++] = *opts++;
162 if (!*opts) {
163 debug("%.100s, line %lu: missing end quote",
164 file, linenum);
165 auth_debug_add("%.100s, line %lu: missing end quote",
166 file, linenum);
167 xfree(s);
168 goto bad_option;
170 s[i] = 0;
171 auth_debug_add("Adding to environment: %.900s", s);
172 debug("Adding to environment: %.900s", s);
173 opts++;
174 new_envstring = xmalloc(sizeof(struct envstring));
175 new_envstring->s = s;
176 new_envstring->next = custom_environment;
177 custom_environment = new_envstring;
178 goto next_option;
180 cp = "from=\"";
181 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
182 const char *remote_ip = get_remote_ipaddr();
183 const char *remote_host = get_canonical_hostname(
184 options.use_dns);
185 char *patterns = xmalloc(strlen(opts) + 1);
187 opts += strlen(cp);
188 i = 0;
189 while (*opts) {
190 if (*opts == '"')
191 break;
192 if (*opts == '\\' && opts[1] == '"') {
193 opts += 2;
194 patterns[i++] = '"';
195 continue;
197 patterns[i++] = *opts++;
199 if (!*opts) {
200 debug("%.100s, line %lu: missing end quote",
201 file, linenum);
202 auth_debug_add("%.100s, line %lu: missing end quote",
203 file, linenum);
204 xfree(patterns);
205 goto bad_option;
207 patterns[i] = 0;
208 opts++;
209 if (match_host_and_ip(remote_host, remote_ip,
210 patterns) != 1) {
211 xfree(patterns);
212 logit("Authentication tried for %.100s with "
213 "correct key but not from a permitted "
214 "host (host=%.200s, ip=%.200s).",
215 pw->pw_name, remote_host, remote_ip);
216 auth_debug_add("Your host '%.200s' is not "
217 "permitted to use this key for login.",
218 remote_host);
219 /* deny access */
220 return 0;
222 xfree(patterns);
223 /* Host name matches. */
224 goto next_option;
226 cp = "permitopen=\"";
227 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
228 char *host, *p;
229 u_short port;
230 char *patterns = xmalloc(strlen(opts) + 1);
232 opts += strlen(cp);
233 i = 0;
234 while (*opts) {
235 if (*opts == '"')
236 break;
237 if (*opts == '\\' && opts[1] == '"') {
238 opts += 2;
239 patterns[i++] = '"';
240 continue;
242 patterns[i++] = *opts++;
244 if (!*opts) {
245 debug("%.100s, line %lu: missing end quote",
246 file, linenum);
247 auth_debug_add("%.100s, line %lu: missing "
248 "end quote", file, linenum);
249 xfree(patterns);
250 goto bad_option;
252 patterns[i] = 0;
253 opts++;
254 p = patterns;
255 host = hpdelim(&p);
256 if (host == NULL || strlen(host) >= NI_MAXHOST) {
257 debug("%.100s, line %lu: Bad permitopen "
258 "specification <%.100s>", file, linenum,
259 patterns);
260 auth_debug_add("%.100s, line %lu: "
261 "Bad permitopen specification", file,
262 linenum);
263 xfree(patterns);
264 goto bad_option;
266 host = cleanhostname(host);
267 if (p == NULL || (port = a2port(p)) == 0) {
268 debug("%.100s, line %lu: Bad permitopen port "
269 "<%.100s>", file, linenum, p ? p : "");
270 auth_debug_add("%.100s, line %lu: "
271 "Bad permitopen port", file, linenum);
272 xfree(patterns);
273 goto bad_option;
275 if (options.allow_tcp_forwarding)
276 channel_add_permitted_opens(host, port);
277 xfree(patterns);
278 goto next_option;
280 cp = "tunnel=\"";
281 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
282 char *tun = NULL;
283 opts += strlen(cp);
284 tun = xmalloc(strlen(opts) + 1);
285 i = 0;
286 while (*opts) {
287 if (*opts == '"')
288 break;
289 tun[i++] = *opts++;
291 if (!*opts) {
292 debug("%.100s, line %lu: missing end quote",
293 file, linenum);
294 auth_debug_add("%.100s, line %lu: missing end quote",
295 file, linenum);
296 xfree(tun);
297 forced_tun_device = -1;
298 goto bad_option;
300 tun[i] = 0;
301 forced_tun_device = a2tun(tun, NULL);
302 xfree(tun);
303 if (forced_tun_device == SSH_TUNID_ERR) {
304 debug("%.100s, line %lu: invalid tun device",
305 file, linenum);
306 auth_debug_add("%.100s, line %lu: invalid tun device",
307 file, linenum);
308 forced_tun_device = -1;
309 goto bad_option;
311 auth_debug_add("Forced tun device: %d", forced_tun_device);
312 opts++;
313 goto next_option;
315 next_option:
317 * Skip the comma, and move to the next option
318 * (or break out if there are no more).
320 if (!*opts)
321 fatal("Bugs in auth-options.c option processing.");
322 if (*opts == ' ' || *opts == '\t')
323 break; /* End of options. */
324 if (*opts != ',')
325 goto bad_option;
326 opts++;
327 /* Process the next option. */
330 if (!use_privsep)
331 auth_debug_send();
333 /* grant access */
334 return 1;
336 bad_option:
337 logit("Bad options in %.100s file, line %lu: %.50s",
338 file, linenum, opts);
339 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
340 file, linenum, opts);
342 if (!use_privsep)
343 auth_debug_send();
345 /* deny access */
346 return 0;