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