]> andersk Git - openssh.git/blob - auth-options.c
- stevesk@cvs.openbsd.org 2006/07/06 16:03:53
[openssh.git] / auth-options.c
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".
11  */
12
13 #include "includes.h"
14
15 #include <sys/types.h>
16
17 #include <pwd.h>
18
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"
29
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;
35
36 /* "command=" option. */
37 char *forced_command = NULL;
38
39 /* "environment=" options. */
40 struct envstring *custom_environment = NULL;
41
42 /* "tunnel=" option. */
43 int forced_tun_device = -1;
44
45 extern ServerOptions options;
46
47 void
48 auth_clear_options(void)
49 {
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);
59         }
60         if (forced_command) {
61                 xfree(forced_command);
62                 forced_command = NULL;
63         }
64         forced_tun_device = -1;
65         channel_clear_permitted_opens();
66         auth_debug_reset();
67 }
68
69 /*
70  * return 1 if access is granted, 0 if not.
71  * side effect: sets key option flags
72  */
73 int
74 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
75 {
76         const char *cp;
77         int i;
78
79         /* reset options */
80         auth_clear_options();
81
82         if (!opts)
83                 return 1;
84
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;
92                 }
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;
99                 }
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;
106                 }
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;
113                 }
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;
126                                 }
127                                 forced_command[i++] = *opts++;
128                         }
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;
137                         }
138                         forced_command[i] = 0;
139                         auth_debug_add("Forced command: %.900s", forced_command);
140                         opts++;
141                         goto next_option;
142                 }
143                 cp = "environment=\"";
144                 if (options.permit_user_env &&
145                     strncasecmp(opts, cp, strlen(cp)) == 0) {
146                         char *s;
147                         struct envstring *new_envstring;
148
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;
159                                 }
160                                 s[i++] = *opts++;
161                         }
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;
169                         }
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;
179                 }
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);
186
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;
196                                 }
197                                 patterns[i++] = *opts++;
198                         }
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;
206                         }
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;
221                         }
222                         xfree(patterns);
223                         /* Host name matches. */
224                         goto next_option;
225                 }
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);
231
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;
241                                 }
242                                 patterns[i++] = *opts++;
243                         }
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;
251                         }
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;
265                         }
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;
274                         }
275                         if (options.allow_tcp_forwarding)
276                                 channel_add_permitted_opens(host, port);
277                         xfree(patterns);
278                         goto next_option;
279                 }
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++;
290                         }
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;
299                         }
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;
310                         }
311                         auth_debug_add("Forced tun device: %d", forced_tun_device);
312                         opts++;
313                         goto next_option;
314                 }
315 next_option:
316                 /*
317                  * Skip the comma, and move to the next option
318                  * (or break out if there are no more).
319                  */
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. */
328         }
329
330         if (!use_privsep)
331                 auth_debug_send();
332
333         /* grant access */
334         return 1;
335
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);
341
342         if (!use_privsep)
343                 auth_debug_send();
344
345         /* deny access */
346         return 0;
347 }
This page took 0.622975 seconds and 5 git commands to generate.