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