]>
Commit | Line | Data |
---|---|---|
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" | |
ea067773 | 26 | RCSID("$OpenBSD: auth2-pubkey.c,v 1.8 2004/12/06 11:41:03 dtucker Exp $"); |
013eab17 | 27 | |
ea067773 | 28 | #include "ssh.h" |
013eab17 | 29 | #include "ssh2.h" |
30 | #include "xmalloc.h" | |
31 | #include "packet.h" | |
32 | #include "buffer.h" | |
33 | #include "log.h" | |
34 | #include "servconf.h" | |
35 | #include "compat.h" | |
36 | #include "bufaux.h" | |
37 | #include "auth.h" | |
38 | #include "key.h" | |
39 | #include "pathnames.h" | |
40 | #include "uidswap.h" | |
41 | #include "auth-options.h" | |
42 | #include "canohost.h" | |
43 | #include "monitor_wrap.h" | |
44 | ||
45 | /* import */ | |
46 | extern ServerOptions options; | |
47 | extern u_char *session_id2; | |
a27002e5 | 48 | extern u_int session_id2_len; |
013eab17 | 49 | |
50 | static int | |
51 | userauth_pubkey(Authctxt *authctxt) | |
52 | { | |
53 | Buffer b; | |
54 | Key *key = NULL; | |
55 | char *pkalg; | |
56 | u_char *pkblob, *sig; | |
57 | u_int alen, blen, slen; | |
58 | int have_sig, pktype; | |
59 | int authenticated = 0; | |
60 | ||
61 | if (!authctxt->valid) { | |
62 | debug2("userauth_pubkey: disabled because of invalid user"); | |
63 | return 0; | |
64 | } | |
65 | have_sig = packet_get_char(); | |
66 | if (datafellows & SSH_BUG_PKAUTH) { | |
67 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); | |
68 | /* no explicit pkalg given */ | |
69 | pkblob = packet_get_string(&blen); | |
70 | buffer_init(&b); | |
71 | buffer_append(&b, pkblob, blen); | |
72 | /* so we have to extract the pkalg from the pkblob */ | |
73 | pkalg = buffer_get_string(&b, &alen); | |
74 | buffer_free(&b); | |
75 | } else { | |
76 | pkalg = packet_get_string(&alen); | |
77 | pkblob = packet_get_string(&blen); | |
78 | } | |
79 | pktype = key_type_from_name(pkalg); | |
80 | if (pktype == KEY_UNSPEC) { | |
81 | /* this is perfectly legal */ | |
bbe88b6d | 82 | logit("userauth_pubkey: unsupported public key algorithm: %s", |
013eab17 | 83 | pkalg); |
84 | goto done; | |
85 | } | |
86 | key = key_from_blob(pkblob, blen); | |
87 | if (key == NULL) { | |
88 | error("userauth_pubkey: cannot decode key: %s", pkalg); | |
89 | goto done; | |
90 | } | |
91 | if (key->type != pktype) { | |
92 | error("userauth_pubkey: type mismatch for decoded key " | |
93 | "(received %d, expected %d)", key->type, pktype); | |
94 | goto done; | |
95 | } | |
96 | if (have_sig) { | |
97 | sig = packet_get_string(&slen); | |
98 | packet_check_eom(); | |
99 | buffer_init(&b); | |
100 | if (datafellows & SSH_OLD_SESSIONID) { | |
101 | buffer_append(&b, session_id2, session_id2_len); | |
102 | } else { | |
103 | buffer_put_string(&b, session_id2, session_id2_len); | |
104 | } | |
105 | /* reconstruct packet */ | |
106 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
107 | buffer_put_cstring(&b, authctxt->user); | |
108 | buffer_put_cstring(&b, | |
109 | datafellows & SSH_BUG_PKSERVICE ? | |
110 | "ssh-userauth" : | |
111 | authctxt->service); | |
112 | if (datafellows & SSH_BUG_PKAUTH) { | |
113 | buffer_put_char(&b, have_sig); | |
114 | } else { | |
115 | buffer_put_cstring(&b, "publickey"); | |
116 | buffer_put_char(&b, have_sig); | |
117 | buffer_put_cstring(&b, pkalg); | |
118 | } | |
119 | buffer_put_string(&b, pkblob, blen); | |
120 | #ifdef DEBUG_PK | |
121 | buffer_dump(&b); | |
122 | #endif | |
123 | /* test for correct signature */ | |
124 | authenticated = 0; | |
125 | if (PRIVSEP(user_key_allowed(authctxt->pw, key)) && | |
126 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), | |
43f7a4b8 | 127 | buffer_len(&b))) == 1) |
013eab17 | 128 | authenticated = 1; |
43f7a4b8 | 129 | buffer_free(&b); |
013eab17 | 130 | xfree(sig); |
131 | } else { | |
132 | debug("test whether pkalg/pkblob are acceptable"); | |
133 | packet_check_eom(); | |
134 | ||
135 | /* XXX fake reply and always send PK_OK ? */ | |
136 | /* | |
137 | * XXX this allows testing whether a user is allowed | |
138 | * to login: if you happen to have a valid pubkey this | |
139 | * message is sent. the message is NEVER sent at all | |
140 | * if a user is not allowed to login. is this an | |
141 | * issue? -markus | |
142 | */ | |
143 | if (PRIVSEP(user_key_allowed(authctxt->pw, key))) { | |
144 | packet_start(SSH2_MSG_USERAUTH_PK_OK); | |
145 | packet_put_string(pkalg, alen); | |
146 | packet_put_string(pkblob, blen); | |
147 | packet_send(); | |
148 | packet_write_wait(); | |
149 | authctxt->postponed = 1; | |
150 | } | |
151 | } | |
152 | if (authenticated != 1) | |
153 | auth_clear_options(); | |
154 | done: | |
155 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); | |
156 | if (key != NULL) | |
157 | key_free(key); | |
158 | xfree(pkalg); | |
159 | xfree(pkblob); | |
160 | #ifdef HAVE_CYGWIN | |
161 | if (check_nt_auth(0, authctxt->pw) == 0) | |
73b1ee82 | 162 | authenticated = 0; |
013eab17 | 163 | #endif |
164 | return authenticated; | |
165 | } | |
166 | ||
167 | /* return 1 if user allows given key */ | |
168 | static int | |
169 | user_key_allowed2(struct passwd *pw, Key *key, char *file) | |
170 | { | |
ea067773 | 171 | char line[SSH_MAX_PUBKEY_BYTES]; |
013eab17 | 172 | int found_key = 0; |
173 | FILE *f; | |
174 | u_long linenum = 0; | |
175 | struct stat st; | |
176 | Key *found; | |
177 | char *fp; | |
178 | ||
013eab17 | 179 | /* Temporarily use the user's uid. */ |
180 | temporarily_use_uid(pw); | |
181 | ||
182 | debug("trying public key file %s", file); | |
183 | ||
184 | /* Fail quietly if file does not exist */ | |
185 | if (stat(file, &st) < 0) { | |
186 | /* Restore the privileged uid. */ | |
187 | restore_uid(); | |
188 | return 0; | |
189 | } | |
190 | /* Open the file containing the authorized keys. */ | |
191 | f = fopen(file, "r"); | |
192 | if (!f) { | |
193 | /* Restore the privileged uid. */ | |
194 | restore_uid(); | |
195 | return 0; | |
196 | } | |
197 | if (options.strict_modes && | |
198 | secure_filename(f, file, pw, line, sizeof(line)) != 0) { | |
199 | fclose(f); | |
bbe88b6d | 200 | logit("Authentication refused: %s", line); |
013eab17 | 201 | restore_uid(); |
202 | return 0; | |
203 | } | |
204 | ||
205 | found_key = 0; | |
206 | found = key_new(key->type); | |
207 | ||
ea067773 | 208 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
ca75d7de | 209 | char *cp, *key_options = NULL; |
ea067773 | 210 | |
013eab17 | 211 | /* Skip leading whitespace, empty and comment lines. */ |
212 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
213 | ; | |
214 | if (!*cp || *cp == '\n' || *cp == '#') | |
215 | continue; | |
216 | ||
217 | if (key_read(found, &cp) != 1) { | |
218 | /* no key? check if there are options for this key */ | |
219 | int quoted = 0; | |
220 | debug2("user_key_allowed: check options: '%s'", cp); | |
ca75d7de | 221 | key_options = cp; |
013eab17 | 222 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
223 | if (*cp == '\\' && cp[1] == '"') | |
224 | cp++; /* Skip both */ | |
225 | else if (*cp == '"') | |
226 | quoted = !quoted; | |
227 | } | |
228 | /* Skip remaining whitespace. */ | |
229 | for (; *cp == ' ' || *cp == '\t'; cp++) | |
230 | ; | |
231 | if (key_read(found, &cp) != 1) { | |
232 | debug2("user_key_allowed: advance: '%s'", cp); | |
233 | /* still no key? advance to next line*/ | |
234 | continue; | |
235 | } | |
236 | } | |
237 | if (key_equal(found, key) && | |
ca75d7de | 238 | auth_parse_options(pw, key_options, file, linenum) == 1) { |
013eab17 | 239 | found_key = 1; |
240 | debug("matching key found: file %s, line %lu", | |
241 | file, linenum); | |
242 | fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); | |
243 | verbose("Found matching %s key: %s", | |
244 | key_type(found), fp); | |
245 | xfree(fp); | |
246 | break; | |
247 | } | |
248 | } | |
249 | restore_uid(); | |
250 | fclose(f); | |
251 | key_free(found); | |
252 | if (!found_key) | |
253 | debug2("key not found"); | |
254 | return found_key; | |
255 | } | |
256 | ||
257 | /* check whether given key is in .ssh/authorized_keys* */ | |
258 | int | |
259 | user_key_allowed(struct passwd *pw, Key *key) | |
260 | { | |
261 | int success; | |
262 | char *file; | |
263 | ||
264 | file = authorized_keys_file(pw); | |
265 | success = user_key_allowed2(pw, key, file); | |
266 | xfree(file); | |
267 | if (success) | |
268 | return success; | |
269 | ||
270 | /* try suffix "2" for backward compat, too */ | |
271 | file = authorized_keys_file2(pw); | |
272 | success = user_key_allowed2(pw, key, file); | |
273 | xfree(file); | |
274 | return success; | |
275 | } | |
276 | ||
277 | Authmethod method_pubkey = { | |
278 | "publickey", | |
279 | userauth_pubkey, | |
280 | &options.pubkey_authentication | |
281 | }; |