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