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