]> andersk Git - openssh.git/blame - auth2-pubkey.c
- deraadt@cvs.openbsd.org 2006/03/19 18:51:18
[openssh.git] / auth2-pubkey.c
CommitLineData
013eab17 1/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
4095f623 26
27#include <sys/types.h>
28#include <sys/stat.h>
013eab17 29
ea067773 30#include "ssh.h"
013eab17 31#include "ssh2.h"
32#include "xmalloc.h"
33#include "packet.h"
34#include "buffer.h"
35#include "log.h"
36#include "servconf.h"
37#include "compat.h"
38#include "bufaux.h"
39#include "auth.h"
40#include "key.h"
41#include "pathnames.h"
42#include "uidswap.h"
43#include "auth-options.h"
44#include "canohost.h"
45#include "monitor_wrap.h"
41feb690 46#include "misc.h"
013eab17 47
48/* import */
49extern ServerOptions options;
50extern u_char *session_id2;
a27002e5 51extern u_int session_id2_len;
013eab17 52
53static int
54userauth_pubkey(Authctxt *authctxt)
55{
56 Buffer b;
57 Key *key = NULL;
58 char *pkalg;
59 u_char *pkblob, *sig;
60 u_int alen, blen, slen;
61 int have_sig, pktype;
62 int authenticated = 0;
63
64 if (!authctxt->valid) {
65 debug2("userauth_pubkey: disabled because of invalid user");
66 return 0;
67 }
68 have_sig = packet_get_char();
69 if (datafellows & SSH_BUG_PKAUTH) {
70 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
71 /* no explicit pkalg given */
72 pkblob = packet_get_string(&blen);
73 buffer_init(&b);
74 buffer_append(&b, pkblob, blen);
75 /* so we have to extract the pkalg from the pkblob */
76 pkalg = buffer_get_string(&b, &alen);
77 buffer_free(&b);
78 } else {
79 pkalg = packet_get_string(&alen);
80 pkblob = packet_get_string(&blen);
81 }
82 pktype = key_type_from_name(pkalg);
83 if (pktype == KEY_UNSPEC) {
84 /* this is perfectly legal */
bbe88b6d 85 logit("userauth_pubkey: unsupported public key algorithm: %s",
013eab17 86 pkalg);
87 goto done;
88 }
89 key = key_from_blob(pkblob, blen);
90 if (key == NULL) {
91 error("userauth_pubkey: cannot decode key: %s", pkalg);
92 goto done;
93 }
94 if (key->type != pktype) {
95 error("userauth_pubkey: type mismatch for decoded key "
96 "(received %d, expected %d)", key->type, pktype);
97 goto done;
98 }
99 if (have_sig) {
100 sig = packet_get_string(&slen);
101 packet_check_eom();
102 buffer_init(&b);
103 if (datafellows & SSH_OLD_SESSIONID) {
104 buffer_append(&b, session_id2, session_id2_len);
105 } else {
106 buffer_put_string(&b, session_id2, session_id2_len);
107 }
108 /* reconstruct packet */
109 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
110 buffer_put_cstring(&b, authctxt->user);
111 buffer_put_cstring(&b,
112 datafellows & SSH_BUG_PKSERVICE ?
113 "ssh-userauth" :
114 authctxt->service);
115 if (datafellows & SSH_BUG_PKAUTH) {
116 buffer_put_char(&b, have_sig);
117 } else {
118 buffer_put_cstring(&b, "publickey");
119 buffer_put_char(&b, have_sig);
120 buffer_put_cstring(&b, pkalg);
121 }
122 buffer_put_string(&b, pkblob, blen);
123#ifdef DEBUG_PK
124 buffer_dump(&b);
125#endif
126 /* test for correct signature */
127 authenticated = 0;
128 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
129 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
43f7a4b8 130 buffer_len(&b))) == 1)
013eab17 131 authenticated = 1;
43f7a4b8 132 buffer_free(&b);
013eab17 133 xfree(sig);
134 } else {
135 debug("test whether pkalg/pkblob are acceptable");
136 packet_check_eom();
137
138 /* XXX fake reply and always send PK_OK ? */
139 /*
140 * XXX this allows testing whether a user is allowed
141 * to login: if you happen to have a valid pubkey this
142 * message is sent. the message is NEVER sent at all
143 * if a user is not allowed to login. is this an
144 * issue? -markus
145 */
146 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
147 packet_start(SSH2_MSG_USERAUTH_PK_OK);
148 packet_put_string(pkalg, alen);
149 packet_put_string(pkblob, blen);
150 packet_send();
151 packet_write_wait();
152 authctxt->postponed = 1;
153 }
154 }
155 if (authenticated != 1)
156 auth_clear_options();
157done:
158 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
159 if (key != NULL)
160 key_free(key);
161 xfree(pkalg);
162 xfree(pkblob);
163#ifdef HAVE_CYGWIN
164 if (check_nt_auth(0, authctxt->pw) == 0)
73b1ee82 165 authenticated = 0;
013eab17 166#endif
167 return authenticated;
168}
169
170/* return 1 if user allows given key */
171static int
172user_key_allowed2(struct passwd *pw, Key *key, char *file)
173{
ea067773 174 char line[SSH_MAX_PUBKEY_BYTES];
013eab17 175 int found_key = 0;
176 FILE *f;
177 u_long linenum = 0;
178 struct stat st;
179 Key *found;
180 char *fp;
181
013eab17 182 /* Temporarily use the user's uid. */
183 temporarily_use_uid(pw);
184
185 debug("trying public key file %s", file);
186
187 /* Fail quietly if file does not exist */
188 if (stat(file, &st) < 0) {
189 /* Restore the privileged uid. */
190 restore_uid();
191 return 0;
192 }
193 /* Open the file containing the authorized keys. */
194 f = fopen(file, "r");
195 if (!f) {
196 /* Restore the privileged uid. */
197 restore_uid();
198 return 0;
199 }
200 if (options.strict_modes &&
201 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
202 fclose(f);
bbe88b6d 203 logit("Authentication refused: %s", line);
013eab17 204 restore_uid();
205 return 0;
206 }
207
208 found_key = 0;
209 found = key_new(key->type);
210
ea067773 211 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
ca75d7de 212 char *cp, *key_options = NULL;
ea067773 213
013eab17 214 /* Skip leading whitespace, empty and comment lines. */
215 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
216 ;
217 if (!*cp || *cp == '\n' || *cp == '#')
218 continue;
219
220 if (key_read(found, &cp) != 1) {
221 /* no key? check if there are options for this key */
222 int quoted = 0;
223 debug2("user_key_allowed: check options: '%s'", cp);
ca75d7de 224 key_options = cp;
013eab17 225 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
226 if (*cp == '\\' && cp[1] == '"')
227 cp++; /* Skip both */
228 else if (*cp == '"')
229 quoted = !quoted;
230 }
231 /* Skip remaining whitespace. */
232 for (; *cp == ' ' || *cp == '\t'; cp++)
233 ;
234 if (key_read(found, &cp) != 1) {
235 debug2("user_key_allowed: advance: '%s'", cp);
236 /* still no key? advance to next line*/
237 continue;
238 }
239 }
240 if (key_equal(found, key) &&
ca75d7de 241 auth_parse_options(pw, key_options, file, linenum) == 1) {
013eab17 242 found_key = 1;
243 debug("matching key found: file %s, line %lu",
244 file, linenum);
245 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
246 verbose("Found matching %s key: %s",
247 key_type(found), fp);
248 xfree(fp);
249 break;
250 }
251 }
252 restore_uid();
253 fclose(f);
254 key_free(found);
255 if (!found_key)
256 debug2("key not found");
257 return found_key;
258}
259
260/* check whether given key is in .ssh/authorized_keys* */
261int
262user_key_allowed(struct passwd *pw, Key *key)
263{
264 int success;
265 char *file;
266
267 file = authorized_keys_file(pw);
268 success = user_key_allowed2(pw, key, file);
269 xfree(file);
270 if (success)
271 return success;
272
273 /* try suffix "2" for backward compat, too */
274 file = authorized_keys_file2(pw);
275 success = user_key_allowed2(pw, key, file);
276 xfree(file);
277 return success;
278}
279
280Authmethod method_pubkey = {
281 "publickey",
282 userauth_pubkey,
283 &options.pubkey_authentication
284};
This page took 0.156542 seconds and 5 git commands to generate.