blob: 3065150009c9c6dddaeb79689c61763dab9e7317 [file] [log] [blame]
Darren Tuckerd9526a52008-06-14 09:01:24 +10001/* $OpenBSD: auth2-pubkey.c,v 1.17 2008/06/13 14:18:51 dtucker Exp $ */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110027
28#include <sys/types.h>
29#include <sys/stat.h>
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000030
Darren Tucker06db5842008-06-13 14:51:28 +100031#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100033#include <stdio.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <stdarg.h>
Darren Tuckerd9526a52008-06-14 09:01:24 +100035#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100036
Damien Millerd7834352006-08-05 12:39:39 +100037#include "xmalloc.h"
Darren Tucker22cc7412004-12-06 22:47:41 +110038#include "ssh.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000039#include "ssh2.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000040#include "packet.h"
41#include "buffer.h"
42#include "log.h"
43#include "servconf.h"
44#include "compat.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000045#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100046#include "hostfile.h"
47#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000048#include "pathnames.h"
49#include "uidswap.h"
50#include "auth-options.h"
51#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100052#ifdef GSSAPI
53#include "ssh-gss.h"
54#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000055#include "monitor_wrap.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110056#include "misc.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000057
58/* import */
59extern ServerOptions options;
60extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100061extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000062
63static int
64userauth_pubkey(Authctxt *authctxt)
65{
66 Buffer b;
67 Key *key = NULL;
68 char *pkalg;
69 u_char *pkblob, *sig;
70 u_int alen, blen, slen;
71 int have_sig, pktype;
72 int authenticated = 0;
73
74 if (!authctxt->valid) {
75 debug2("userauth_pubkey: disabled because of invalid user");
76 return 0;
77 }
78 have_sig = packet_get_char();
79 if (datafellows & SSH_BUG_PKAUTH) {
80 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
81 /* no explicit pkalg given */
82 pkblob = packet_get_string(&blen);
83 buffer_init(&b);
84 buffer_append(&b, pkblob, blen);
85 /* so we have to extract the pkalg from the pkblob */
86 pkalg = buffer_get_string(&b, &alen);
87 buffer_free(&b);
88 } else {
89 pkalg = packet_get_string(&alen);
90 pkblob = packet_get_string(&blen);
91 }
92 pktype = key_type_from_name(pkalg);
93 if (pktype == KEY_UNSPEC) {
94 /* this is perfectly legal */
Damien Miller996acd22003-04-09 20:59:48 +100095 logit("userauth_pubkey: unsupported public key algorithm: %s",
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000096 pkalg);
97 goto done;
98 }
99 key = key_from_blob(pkblob, blen);
100 if (key == NULL) {
101 error("userauth_pubkey: cannot decode key: %s", pkalg);
102 goto done;
103 }
104 if (key->type != pktype) {
105 error("userauth_pubkey: type mismatch for decoded key "
106 "(received %d, expected %d)", key->type, pktype);
107 goto done;
108 }
109 if (have_sig) {
110 sig = packet_get_string(&slen);
111 packet_check_eom();
112 buffer_init(&b);
113 if (datafellows & SSH_OLD_SESSIONID) {
114 buffer_append(&b, session_id2, session_id2_len);
115 } else {
116 buffer_put_string(&b, session_id2, session_id2_len);
117 }
118 /* reconstruct packet */
119 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
120 buffer_put_cstring(&b, authctxt->user);
121 buffer_put_cstring(&b,
122 datafellows & SSH_BUG_PKSERVICE ?
123 "ssh-userauth" :
124 authctxt->service);
125 if (datafellows & SSH_BUG_PKAUTH) {
126 buffer_put_char(&b, have_sig);
127 } else {
128 buffer_put_cstring(&b, "publickey");
129 buffer_put_char(&b, have_sig);
130 buffer_put_cstring(&b, pkalg);
131 }
132 buffer_put_string(&b, pkblob, blen);
133#ifdef DEBUG_PK
134 buffer_dump(&b);
135#endif
136 /* test for correct signature */
137 authenticated = 0;
138 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
139 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Damien Millerfb1310e2004-01-21 11:02:50 +1100140 buffer_len(&b))) == 1)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000141 authenticated = 1;
Damien Millerfb1310e2004-01-21 11:02:50 +1100142 buffer_free(&b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000143 xfree(sig);
144 } else {
145 debug("test whether pkalg/pkblob are acceptable");
146 packet_check_eom();
147
148 /* XXX fake reply and always send PK_OK ? */
149 /*
150 * XXX this allows testing whether a user is allowed
151 * to login: if you happen to have a valid pubkey this
152 * message is sent. the message is NEVER sent at all
153 * if a user is not allowed to login. is this an
154 * issue? -markus
155 */
156 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
157 packet_start(SSH2_MSG_USERAUTH_PK_OK);
158 packet_put_string(pkalg, alen);
159 packet_put_string(pkblob, blen);
160 packet_send();
161 packet_write_wait();
162 authctxt->postponed = 1;
163 }
164 }
165 if (authenticated != 1)
166 auth_clear_options();
167done:
168 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
169 if (key != NULL)
170 key_free(key);
171 xfree(pkalg);
172 xfree(pkblob);
173#ifdef HAVE_CYGWIN
174 if (check_nt_auth(0, authctxt->pw) == 0)
Damien Miller47656792004-09-11 22:42:09 +1000175 authenticated = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000176#endif
177 return authenticated;
178}
179
180/* return 1 if user allows given key */
181static int
182user_key_allowed2(struct passwd *pw, Key *key, char *file)
183{
Darren Tucker22cc7412004-12-06 22:47:41 +1100184 char line[SSH_MAX_PUBKEY_BYTES];
Darren Tucker06db5842008-06-13 14:51:28 +1000185 int found_key = 0, fd;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000186 FILE *f;
187 u_long linenum = 0;
188 struct stat st;
189 Key *found;
190 char *fp;
191
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000192 /* Temporarily use the user's uid. */
193 temporarily_use_uid(pw);
194
195 debug("trying public key file %s", file);
196
Darren Tucker06db5842008-06-13 14:51:28 +1000197 /*
198 * Open the file containing the authorized keys
199 * Fail quietly if file does not exist
200 */
201 if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000202 restore_uid();
203 return 0;
204 }
Darren Tucker06db5842008-06-13 14:51:28 +1000205 if (fstat(fd, &st) < 0) {
206 close(fd);
207 restore_uid();
208 return 0;
209 }
210 if (!S_ISREG(st.st_mode)) {
211 logit("User %s authorized keys %s is not a regular file",
212 pw->pw_name, file);
213 close(fd);
214 restore_uid();
215 return 0;
216 }
217 unset_nonblock(fd);
218 if ((f = fdopen(fd, "r")) == NULL) {
219 close(fd);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000220 restore_uid();
221 return 0;
222 }
223 if (options.strict_modes &&
224 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
225 fclose(f);
Damien Miller996acd22003-04-09 20:59:48 +1000226 logit("Authentication refused: %s", line);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000227 restore_uid();
228 return 0;
229 }
230
231 found_key = 0;
232 found = key_new(key->type);
233
Darren Tucker22cc7412004-12-06 22:47:41 +1100234 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000235 char *cp, *key_options = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100236
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000237 /* Skip leading whitespace, empty and comment lines. */
238 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
239 ;
240 if (!*cp || *cp == '\n' || *cp == '#')
241 continue;
242
243 if (key_read(found, &cp) != 1) {
244 /* no key? check if there are options for this key */
245 int quoted = 0;
246 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000247 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000248 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
249 if (*cp == '\\' && cp[1] == '"')
250 cp++; /* Skip both */
251 else if (*cp == '"')
252 quoted = !quoted;
253 }
254 /* Skip remaining whitespace. */
255 for (; *cp == ' ' || *cp == '\t'; cp++)
256 ;
257 if (key_read(found, &cp) != 1) {
258 debug2("user_key_allowed: advance: '%s'", cp);
259 /* still no key? advance to next line*/
260 continue;
261 }
262 }
263 if (key_equal(found, key) &&
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000264 auth_parse_options(pw, key_options, file, linenum) == 1) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000265 found_key = 1;
266 debug("matching key found: file %s, line %lu",
267 file, linenum);
268 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
269 verbose("Found matching %s key: %s",
270 key_type(found), fp);
271 xfree(fp);
272 break;
273 }
274 }
275 restore_uid();
276 fclose(f);
277 key_free(found);
278 if (!found_key)
279 debug2("key not found");
280 return found_key;
281}
282
283/* check whether given key is in .ssh/authorized_keys* */
284int
285user_key_allowed(struct passwd *pw, Key *key)
286{
287 int success;
288 char *file;
289
290 file = authorized_keys_file(pw);
291 success = user_key_allowed2(pw, key, file);
292 xfree(file);
293 if (success)
294 return success;
295
296 /* try suffix "2" for backward compat, too */
297 file = authorized_keys_file2(pw);
298 success = user_key_allowed2(pw, key, file);
299 xfree(file);
300 return success;
301}
302
303Authmethod method_pubkey = {
304 "publickey",
305 userauth_pubkey,
306 &options.pubkey_authentication
307};