]>
Commit | Line | Data |
---|---|---|
a306f2dd | 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. | |
a306f2dd | 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 | */ | |
bcbf86ec | 24 | |
a306f2dd | 25 | #include "includes.h" |
ffb215be | 26 | RCSID("$OpenBSD: auth2.c,v 1.67 2001/06/25 20:26:37 stevesk Exp $"); |
94ec8c6b | 27 | |
a306f2dd | 28 | #include <openssl/evp.h> |
29 | ||
42f11eb2 | 30 | #include "ssh2.h" |
a306f2dd | 31 | #include "xmalloc.h" |
32 | #include "rsa.h" | |
1729c161 | 33 | #include "sshpty.h" |
a306f2dd | 34 | #include "packet.h" |
35 | #include "buffer.h" | |
42f11eb2 | 36 | #include "log.h" |
a306f2dd | 37 | #include "servconf.h" |
38 | #include "compat.h" | |
a5efa1bb | 39 | #include "channels.h" |
a306f2dd | 40 | #include "bufaux.h" |
a306f2dd | 41 | #include "auth.h" |
42 | #include "session.h" | |
43 | #include "dispatch.h" | |
a306f2dd | 44 | #include "key.h" |
b0e305c9 | 45 | #include "cipher.h" |
a306f2dd | 46 | #include "kex.h" |
42f11eb2 | 47 | #include "pathnames.h" |
a306f2dd | 48 | #include "uidswap.h" |
38c295d6 | 49 | #include "auth-options.h" |
3b1a83df | 50 | #include "misc.h" |
8002af61 | 51 | #include "hostfile.h" |
52 | #include "canohost.h" | |
53 | #include "tildexpand.h" | |
5ba55ada | 54 | #include "match.h" |
a306f2dd | 55 | |
56 | /* import */ | |
57 | extern ServerOptions options; | |
1e3b8b07 | 58 | extern u_char *session_id2; |
a306f2dd | 59 | extern int session_id2_len; |
60 | ||
94ec8c6b | 61 | #ifdef WITH_AIXAUTHENTICATE |
62 | extern char *aixloginmsg; | |
63 | #endif | |
94ec8c6b | 64 | |
65 | static Authctxt *x_authctxt = NULL; | |
66 | static int one = 1; | |
67 | ||
68 | typedef struct Authmethod Authmethod; | |
69 | struct Authmethod { | |
70 | char *name; | |
71 | int (*userauth)(Authctxt *authctxt); | |
72 | int *enabled; | |
73 | }; | |
74 | ||
a306f2dd | 75 | /* protocol */ |
76 | ||
396c147e | 77 | static void input_service_request(int, int, void *); |
78 | static void input_userauth_request(int, int, void *); | |
79 | static void protocol_error(int, int, void *); | |
a306f2dd | 80 | |
a306f2dd | 81 | /* helper */ |
396c147e | 82 | static Authmethod *authmethod_lookup(const char *); |
ffb215be | 83 | static char *authmethods_get(void); |
396c147e | 84 | static int user_key_allowed(struct passwd *, Key *); |
85 | static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *); | |
a306f2dd | 86 | |
94ec8c6b | 87 | /* auth */ |
396c147e | 88 | static void userauth_banner(void); |
89 | static int userauth_none(Authctxt *); | |
90 | static int userauth_passwd(Authctxt *); | |
91 | static int userauth_pubkey(Authctxt *); | |
92 | static int userauth_hostbased(Authctxt *); | |
93 | static int userauth_kbdint(Authctxt *); | |
94ec8c6b | 94 | |
95 | Authmethod authmethods[] = { | |
96 | {"none", | |
97 | userauth_none, | |
98 | &one}, | |
99 | {"publickey", | |
100 | userauth_pubkey, | |
fa08c86b | 101 | &options.pubkey_authentication}, |
94ec8c6b | 102 | {"password", |
103 | userauth_passwd, | |
104 | &options.password_authentication}, | |
c845316f | 105 | {"keyboard-interactive", |
106 | userauth_kbdint, | |
107 | &options.kbd_interactive_authentication}, | |
8002af61 | 108 | {"hostbased", |
109 | userauth_hostbased, | |
110 | &options.hostbased_authentication}, | |
94ec8c6b | 111 | {NULL, NULL, NULL} |
a306f2dd | 112 | }; |
a306f2dd | 113 | |
114 | /* | |
94ec8c6b | 115 | * loop until authctxt->success == TRUE |
a306f2dd | 116 | */ |
117 | ||
118 | void | |
119 | do_authentication2() | |
120 | { | |
59c97189 | 121 | Authctxt *authctxt = authctxt_new(); |
122 | ||
94ec8c6b | 123 | x_authctxt = authctxt; /*XXX*/ |
124 | ||
d464095c | 125 | /* challenge-reponse is implemented via keyboard interactive */ |
5ba55ada | 126 | if (options.challenge_response_authentication) |
d464095c | 127 | options.kbd_interactive_authentication = 1; |
10f72868 | 128 | if (options.pam_authentication_via_kbd_int) |
129 | options.kbd_interactive_authentication = 1; | |
d464095c | 130 | |
a306f2dd | 131 | dispatch_init(&protocol_error); |
132 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); | |
94ec8c6b | 133 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
bb5639fe | 134 | do_authenticated(authctxt); |
a306f2dd | 135 | } |
136 | ||
396c147e | 137 | static void |
188adeb2 | 138 | protocol_error(int type, int plen, void *ctxt) |
a306f2dd | 139 | { |
140 | log("auth: protocol error: type %d plen %d", type, plen); | |
141 | packet_start(SSH2_MSG_UNIMPLEMENTED); | |
142 | packet_put_int(0); | |
143 | packet_send(); | |
144 | packet_write_wait(); | |
145 | } | |
146 | ||
396c147e | 147 | static void |
188adeb2 | 148 | input_service_request(int type, int plen, void *ctxt) |
a306f2dd | 149 | { |
94ec8c6b | 150 | Authctxt *authctxt = ctxt; |
1e3b8b07 | 151 | u_int len; |
a306f2dd | 152 | int accept = 0; |
153 | char *service = packet_get_string(&len); | |
154 | packet_done(); | |
155 | ||
94ec8c6b | 156 | if (authctxt == NULL) |
157 | fatal("input_service_request: no authctxt"); | |
158 | ||
a306f2dd | 159 | if (strcmp(service, "ssh-userauth") == 0) { |
94ec8c6b | 160 | if (!authctxt->success) { |
a306f2dd | 161 | accept = 1; |
162 | /* now we can handle user-auth requests */ | |
163 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
164 | } | |
165 | } | |
166 | /* XXX all other service requests are denied */ | |
167 | ||
168 | if (accept) { | |
169 | packet_start(SSH2_MSG_SERVICE_ACCEPT); | |
170 | packet_put_cstring(service); | |
171 | packet_send(); | |
172 | packet_write_wait(); | |
173 | } else { | |
174 | debug("bad service request %s", service); | |
175 | packet_disconnect("bad service request %s", service); | |
176 | } | |
177 | xfree(service); | |
178 | } | |
179 | ||
396c147e | 180 | static void |
188adeb2 | 181 | input_userauth_request(int type, int plen, void *ctxt) |
a306f2dd | 182 | { |
94ec8c6b | 183 | Authctxt *authctxt = ctxt; |
184 | Authmethod *m = NULL; | |
59c97189 | 185 | char *user, *service, *method, *style = NULL; |
a306f2dd | 186 | int authenticated = 0; |
a306f2dd | 187 | |
94ec8c6b | 188 | if (authctxt == NULL) |
189 | fatal("input_userauth_request: no authctxt"); | |
a306f2dd | 190 | |
94ec8c6b | 191 | user = packet_get_string(NULL); |
192 | service = packet_get_string(NULL); | |
193 | method = packet_get_string(NULL); | |
194 | debug("userauth-request for user %s service %s method %s", user, service, method); | |
8abcdba4 | 195 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
94ec8c6b | 196 | |
59c97189 | 197 | if ((style = strchr(user, ':')) != NULL) |
198 | *style++ = 0; | |
199 | ||
2b87da3b | 200 | if (authctxt->attempt++ == 0) { |
94ec8c6b | 201 | /* setup auth context */ |
202 | struct passwd *pw = NULL; | |
94ec8c6b | 203 | pw = getpwnam(user); |
204 | if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) { | |
205 | authctxt->pw = pwcopy(pw); | |
206 | authctxt->valid = 1; | |
207 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
208 | #ifdef USE_PAM | |
04fc7a67 | 209 | start_pam(pw->pw_name); |
94ec8c6b | 210 | #endif |
211 | } else { | |
212 | log("input_userauth_request: illegal user %s", user); | |
04fc7a67 | 213 | #ifdef USE_PAM |
214 | start_pam("NOUSER"); | |
215 | #endif | |
94ec8c6b | 216 | } |
6172e290 | 217 | setproctitle("%s", pw ? user : "unknown"); |
94ec8c6b | 218 | authctxt->user = xstrdup(user); |
219 | authctxt->service = xstrdup(service); | |
2a6a054e | 220 | authctxt->style = style ? xstrdup(style) : NULL; |
221 | } else if (strcmp(user, authctxt->user) != 0 || | |
222 | strcmp(service, authctxt->service) != 0) { | |
223 | packet_disconnect("Change of username or service not allowed: " | |
224 | "(%s,%s) -> (%s,%s)", | |
225 | authctxt->user, authctxt->service, user, service); | |
a306f2dd | 226 | } |
59c97189 | 227 | /* reset state */ |
228 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error); | |
229 | authctxt->postponed = 0; | |
af774732 | 230 | #ifdef BSD_AUTH |
231 | if (authctxt->as) { | |
232 | auth_close(authctxt->as); | |
233 | authctxt->as = NULL; | |
234 | } | |
235 | #endif | |
9cd45ea4 | 236 | |
59c97189 | 237 | /* try to authenticate user */ |
94ec8c6b | 238 | m = authmethod_lookup(method); |
239 | if (m != NULL) { | |
240 | debug2("input_userauth_request: try method %s", method); | |
241 | authenticated = m->userauth(authctxt); | |
9cd45ea4 | 242 | } |
d9cd3575 | 243 | userauth_finish(authctxt, authenticated, method); |
244 | ||
245 | xfree(service); | |
246 | xfree(user); | |
247 | xfree(method); | |
248 | } | |
249 | ||
250 | void | |
251 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
252 | { | |
4e81159c | 253 | char *methods; |
254 | ||
59c97189 | 255 | if (!authctxt->valid && authenticated) |
256 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
257 | authctxt->user); | |
9cd45ea4 | 258 | |
94ec8c6b | 259 | /* Special handling for root */ |
15853e93 | 260 | if (authenticated && authctxt->pw->pw_uid == 0 && |
261 | !auth_root_allowed(method)) | |
a306f2dd | 262 | authenticated = 0; |
a306f2dd | 263 | |
264 | #ifdef USE_PAM | |
edef62bf | 265 | if (authenticated && authctxt->user && !do_pam_account(authctxt->user, |
266 | NULL)) | |
9cd45ea4 | 267 | authenticated = 0; |
a306f2dd | 268 | #endif /* USE_PAM */ |
269 | ||
94ec8c6b | 270 | /* Log before sending the reply */ |
59c97189 | 271 | auth_log(authctxt, authenticated, method, " ssh2"); |
272 | ||
4e81159c | 273 | if (authctxt->postponed) |
274 | return; | |
275 | ||
276 | /* XXX todo: check if multiple auth methods are needed */ | |
277 | if (authenticated == 1) { | |
278 | /* turn off userauth */ | |
279 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error); | |
280 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); | |
281 | packet_send(); | |
282 | packet_write_wait(); | |
283 | /* now we can break out */ | |
284 | authctxt->success = 1; | |
285 | } else { | |
286 | if (authctxt->failures++ > AUTH_FAIL_MAX) | |
287 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); | |
288 | methods = authmethods_get(); | |
289 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
290 | packet_put_cstring(methods); | |
291 | packet_put_char(0); /* XXX partial success, unused */ | |
292 | packet_send(); | |
293 | packet_write_wait(); | |
294 | xfree(methods); | |
295 | } | |
94ec8c6b | 296 | } |
297 | ||
396c147e | 298 | static void |
eea39c02 | 299 | userauth_banner(void) |
300 | { | |
301 | struct stat st; | |
302 | char *banner = NULL; | |
303 | off_t len, n; | |
304 | int fd; | |
305 | ||
306 | if (options.banner == NULL || (datafellows & SSH_BUG_BANNER)) | |
307 | return; | |
0715ec6c | 308 | if ((fd = open(options.banner, O_RDONLY)) < 0) |
eea39c02 | 309 | return; |
eea39c02 | 310 | if (fstat(fd, &st) < 0) |
311 | goto done; | |
312 | len = st.st_size; | |
313 | banner = xmalloc(len + 1); | |
314 | if ((n = read(fd, banner, len)) < 0) | |
315 | goto done; | |
316 | banner[n] = '\0'; | |
317 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
318 | packet_put_cstring(banner); | |
319 | packet_put_cstring(""); /* language, unused */ | |
320 | packet_send(); | |
321 | debug("userauth_banner: sent"); | |
322 | done: | |
323 | if (banner) | |
324 | xfree(banner); | |
325 | close(fd); | |
326 | return; | |
327 | } | |
94ec8c6b | 328 | |
396c147e | 329 | static int |
94ec8c6b | 330 | userauth_none(Authctxt *authctxt) |
a306f2dd | 331 | { |
94ec8c6b | 332 | /* disable method "none", only allowed one time */ |
333 | Authmethod *m = authmethod_lookup("none"); | |
334 | if (m != NULL) | |
335 | m->enabled = NULL; | |
a306f2dd | 336 | packet_done(); |
2b87da3b | 337 | userauth_banner(); |
4d33e531 | 338 | |
94ec8c6b | 339 | if (authctxt->valid == 0) |
340 | return(0); | |
2b87da3b | 341 | |
94ec8c6b | 342 | #ifdef HAVE_CYGWIN |
343 | if (check_nt_auth(1, authctxt->pw->pw_uid) == 0) | |
344 | return(0); | |
345 | #endif | |
a306f2dd | 346 | #ifdef USE_PAM |
94ec8c6b | 347 | return auth_pam_password(authctxt->pw, ""); |
4d33e531 | 348 | #elif defined(HAVE_OSF_SIA) |
b7ccb051 | 349 | return 0; |
4d33e531 | 350 | #else /* !HAVE_OSF_SIA && !USE_PAM */ |
af774732 | 351 | return auth_password(authctxt, ""); |
a306f2dd | 352 | #endif /* USE_PAM */ |
353 | } | |
94ec8c6b | 354 | |
396c147e | 355 | static int |
94ec8c6b | 356 | userauth_passwd(Authctxt *authctxt) |
a306f2dd | 357 | { |
358 | char *password; | |
359 | int authenticated = 0; | |
360 | int change; | |
1e3b8b07 | 361 | u_int len; |
a306f2dd | 362 | change = packet_get_char(); |
363 | if (change) | |
364 | log("password change not supported"); | |
365 | password = packet_get_string(&len); | |
366 | packet_done(); | |
94ec8c6b | 367 | if (authctxt->valid && |
368 | #ifdef HAVE_CYGWIN | |
369 | check_nt_auth(1, authctxt->pw->pw_uid) && | |
370 | #endif | |
a306f2dd | 371 | #ifdef USE_PAM |
94ec8c6b | 372 | auth_pam_password(authctxt->pw, password) == 1) |
4d33e531 | 373 | #elif defined(HAVE_OSF_SIA) |
b7ccb051 | 374 | auth_sia_password(authctxt->user, password) == 1) |
4d33e531 | 375 | #else /* !USE_PAM && !HAVE_OSF_SIA */ |
af774732 | 376 | auth_password(authctxt, password) == 1) |
a306f2dd | 377 | #endif /* USE_PAM */ |
378 | authenticated = 1; | |
379 | memset(password, 0, len); | |
380 | xfree(password); | |
381 | return authenticated; | |
382 | } | |
94ec8c6b | 383 | |
396c147e | 384 | static int |
94ec8c6b | 385 | userauth_kbdint(Authctxt *authctxt) |
386 | { | |
387 | int authenticated = 0; | |
5ba55ada | 388 | char *lang, *devs; |
94ec8c6b | 389 | |
390 | lang = packet_get_string(NULL); | |
391 | devs = packet_get_string(NULL); | |
392 | packet_done(); | |
393 | ||
5ba55ada | 394 | debug("keyboard-interactive devs %s", devs); |
59c97189 | 395 | |
5ba55ada | 396 | if (options.challenge_response_authentication) |
d464095c | 397 | authenticated = auth2_challenge(authctxt, devs); |
59c97189 | 398 | |
8c9fe09e | 399 | #ifdef USE_PAM |
10f72868 | 400 | if (authenticated == 0 && options.pam_authentication_via_kbd_int) |
8c9fe09e | 401 | authenticated = auth2_pam(authctxt); |
94ec8c6b | 402 | #endif |
94ec8c6b | 403 | xfree(devs); |
5ba55ada | 404 | xfree(lang); |
94ec8c6b | 405 | #ifdef HAVE_CYGWIN |
406 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) | |
407 | return(0); | |
408 | #endif | |
409 | return authenticated; | |
410 | } | |
411 | ||
396c147e | 412 | static int |
94ec8c6b | 413 | userauth_pubkey(Authctxt *authctxt) |
a306f2dd | 414 | { |
415 | Buffer b; | |
416 | Key *key; | |
417 | char *pkalg, *pkblob, *sig; | |
1e3b8b07 | 418 | u_int alen, blen, slen; |
fa08c86b | 419 | int have_sig, pktype; |
a306f2dd | 420 | int authenticated = 0; |
421 | ||
94ec8c6b | 422 | if (!authctxt->valid) { |
423 | debug2("userauth_pubkey: disabled because of invalid user"); | |
a306f2dd | 424 | return 0; |
425 | } | |
426 | have_sig = packet_get_char(); | |
cbc5abf9 | 427 | if (datafellows & SSH_BUG_PKAUTH) { |
428 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); | |
429 | /* no explicit pkalg given */ | |
430 | pkblob = packet_get_string(&blen); | |
431 | buffer_init(&b); | |
432 | buffer_append(&b, pkblob, blen); | |
433 | /* so we have to extract the pkalg from the pkblob */ | |
434 | pkalg = buffer_get_string(&b, &alen); | |
435 | buffer_free(&b); | |
436 | } else { | |
437 | pkalg = packet_get_string(&alen); | |
438 | pkblob = packet_get_string(&blen); | |
439 | } | |
fa08c86b | 440 | pktype = key_type_from_name(pkalg); |
441 | if (pktype == KEY_UNSPEC) { | |
cbc5abf9 | 442 | /* this is perfectly legal */ |
443 | log("userauth_pubkey: unsupported public key algorithm: %s", pkalg); | |
94ec8c6b | 444 | xfree(pkalg); |
cbc5abf9 | 445 | xfree(pkblob); |
a306f2dd | 446 | return 0; |
447 | } | |
fa08c86b | 448 | key = key_from_blob(pkblob, blen); |
a306f2dd | 449 | if (key != NULL) { |
450 | if (have_sig) { | |
451 | sig = packet_get_string(&slen); | |
452 | packet_done(); | |
453 | buffer_init(&b); | |
33de75a3 | 454 | if (datafellows & SSH_OLD_SESSIONID) { |
74fc9186 | 455 | buffer_append(&b, session_id2, session_id2_len); |
33de75a3 | 456 | } else { |
457 | buffer_put_string(&b, session_id2, session_id2_len); | |
74fc9186 | 458 | } |
38c295d6 | 459 | /* reconstruct packet */ |
a306f2dd | 460 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
94ec8c6b | 461 | buffer_put_cstring(&b, authctxt->user); |
38c295d6 | 462 | buffer_put_cstring(&b, |
cbc5abf9 | 463 | datafellows & SSH_BUG_PKSERVICE ? |
38c295d6 | 464 | "ssh-userauth" : |
94ec8c6b | 465 | authctxt->service); |
cbc5abf9 | 466 | if (datafellows & SSH_BUG_PKAUTH) { |
467 | buffer_put_char(&b, have_sig); | |
468 | } else { | |
469 | buffer_put_cstring(&b, "publickey"); | |
470 | buffer_put_char(&b, have_sig); | |
5cf13595 | 471 | buffer_put_cstring(&b, pkalg); |
cbc5abf9 | 472 | } |
38c295d6 | 473 | buffer_put_string(&b, pkblob, blen); |
fa08c86b | 474 | #ifdef DEBUG_PK |
a306f2dd | 475 | buffer_dump(&b); |
476 | #endif | |
477 | /* test for correct signature */ | |
fa08c86b | 478 | if (user_key_allowed(authctxt->pw, key) && |
479 | key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1) | |
a306f2dd | 480 | authenticated = 1; |
481 | buffer_clear(&b); | |
482 | xfree(sig); | |
483 | } else { | |
94ec8c6b | 484 | debug("test whether pkalg/pkblob are acceptable"); |
a306f2dd | 485 | packet_done(); |
94ec8c6b | 486 | |
a306f2dd | 487 | /* XXX fake reply and always send PK_OK ? */ |
1d1ffb87 | 488 | /* |
489 | * XXX this allows testing whether a user is allowed | |
490 | * to login: if you happen to have a valid pubkey this | |
491 | * message is sent. the message is NEVER sent at all | |
492 | * if a user is not allowed to login. is this an | |
493 | * issue? -markus | |
494 | */ | |
fa08c86b | 495 | if (user_key_allowed(authctxt->pw, key)) { |
a306f2dd | 496 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
497 | packet_put_string(pkalg, alen); | |
498 | packet_put_string(pkblob, blen); | |
499 | packet_send(); | |
500 | packet_write_wait(); | |
25f4c264 | 501 | authctxt->postponed = 1; |
a306f2dd | 502 | } |
503 | } | |
94ec8c6b | 504 | if (authenticated != 1) |
505 | auth_clear_options(); | |
a306f2dd | 506 | key_free(key); |
507 | } | |
fa08c86b | 508 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
a306f2dd | 509 | xfree(pkalg); |
510 | xfree(pkblob); | |
94ec8c6b | 511 | #ifdef HAVE_CYGWIN |
512 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) | |
513 | return(0); | |
514 | #endif | |
a306f2dd | 515 | return authenticated; |
516 | } | |
517 | ||
396c147e | 518 | static int |
8002af61 | 519 | userauth_hostbased(Authctxt *authctxt) |
520 | { | |
521 | Buffer b; | |
522 | Key *key; | |
8dddf799 | 523 | char *pkalg, *pkblob, *sig, *cuser, *chost, *service; |
8002af61 | 524 | u_int alen, blen, slen; |
525 | int pktype; | |
526 | int authenticated = 0; | |
527 | ||
528 | if (!authctxt->valid) { | |
529 | debug2("userauth_hostbased: disabled because of invalid user"); | |
530 | return 0; | |
531 | } | |
532 | pkalg = packet_get_string(&alen); | |
533 | pkblob = packet_get_string(&blen); | |
534 | chost = packet_get_string(NULL); | |
535 | cuser = packet_get_string(NULL); | |
536 | sig = packet_get_string(&slen); | |
537 | ||
538 | debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d", | |
539 | cuser, chost, pkalg, slen); | |
540 | #ifdef DEBUG_PK | |
541 | debug("signature:"); | |
542 | buffer_init(&b); | |
543 | buffer_append(&b, sig, slen); | |
544 | buffer_dump(&b); | |
545 | buffer_free(&b); | |
546 | #endif | |
547 | pktype = key_type_from_name(pkalg); | |
548 | if (pktype == KEY_UNSPEC) { | |
549 | /* this is perfectly legal */ | |
550 | log("userauth_hostbased: unsupported " | |
551 | "public key algorithm: %s", pkalg); | |
552 | goto done; | |
553 | } | |
554 | key = key_from_blob(pkblob, blen); | |
555 | if (key == NULL) { | |
556 | debug("userauth_hostbased: cannot decode key: %s", pkalg); | |
557 | goto done; | |
558 | } | |
8dddf799 | 559 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
560 | authctxt->service; | |
8002af61 | 561 | buffer_init(&b); |
8dddf799 | 562 | buffer_put_string(&b, session_id2, session_id2_len); |
8002af61 | 563 | /* reconstruct packet */ |
564 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
565 | buffer_put_cstring(&b, authctxt->user); | |
8dddf799 | 566 | buffer_put_cstring(&b, service); |
8002af61 | 567 | buffer_put_cstring(&b, "hostbased"); |
568 | buffer_put_string(&b, pkalg, alen); | |
569 | buffer_put_string(&b, pkblob, blen); | |
570 | buffer_put_cstring(&b, chost); | |
571 | buffer_put_cstring(&b, cuser); | |
572 | #ifdef DEBUG_PK | |
573 | buffer_dump(&b); | |
574 | #endif | |
575 | /* test for allowed key and correct signature */ | |
576 | if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) && | |
577 | key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1) | |
578 | authenticated = 1; | |
579 | ||
580 | buffer_clear(&b); | |
581 | key_free(key); | |
582 | ||
583 | done: | |
584 | debug2("userauth_hostbased: authenticated %d", authenticated); | |
585 | xfree(pkalg); | |
586 | xfree(pkblob); | |
587 | xfree(cuser); | |
588 | xfree(chost); | |
589 | xfree(sig); | |
590 | return authenticated; | |
591 | } | |
592 | ||
94ec8c6b | 593 | /* get current user */ |
a306f2dd | 594 | |
595 | struct passwd* | |
596 | auth_get_user(void) | |
597 | { | |
94ec8c6b | 598 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; |
a306f2dd | 599 | } |
600 | ||
94ec8c6b | 601 | #define DELIM "," |
602 | ||
ffb215be | 603 | static char * |
94ec8c6b | 604 | authmethods_get(void) |
a306f2dd | 605 | { |
94ec8c6b | 606 | Authmethod *method = NULL; |
1e3b8b07 | 607 | u_int size = 0; |
94ec8c6b | 608 | char *list; |
609 | ||
610 | for (method = authmethods; method->name != NULL; method++) { | |
611 | if (strcmp(method->name, "none") == 0) | |
612 | continue; | |
613 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
614 | if (size != 0) | |
615 | size += strlen(DELIM); | |
616 | size += strlen(method->name); | |
a306f2dd | 617 | } |
94ec8c6b | 618 | } |
619 | size++; /* trailing '\0' */ | |
620 | list = xmalloc(size); | |
621 | list[0] = '\0'; | |
622 | ||
623 | for (method = authmethods; method->name != NULL; method++) { | |
624 | if (strcmp(method->name, "none") == 0) | |
625 | continue; | |
626 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
627 | if (list[0] != '\0') | |
628 | strlcat(list, DELIM, size); | |
629 | strlcat(list, method->name, size); | |
a306f2dd | 630 | } |
631 | } | |
94ec8c6b | 632 | return list; |
633 | } | |
634 | ||
396c147e | 635 | static Authmethod * |
94ec8c6b | 636 | authmethod_lookup(const char *name) |
637 | { | |
638 | Authmethod *method = NULL; | |
639 | if (name != NULL) | |
640 | for (method = authmethods; method->name != NULL; method++) | |
641 | if (method->enabled != NULL && | |
642 | *(method->enabled) != 0 && | |
643 | strcmp(name, method->name) == 0) | |
644 | return method; | |
645 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
646 | return NULL; | |
a306f2dd | 647 | } |
648 | ||
649 | /* return 1 if user allows given key */ | |
396c147e | 650 | static int |
96a7b0cc | 651 | user_key_allowed2(struct passwd *pw, Key *key, char *file) |
a306f2dd | 652 | { |
96a7b0cc | 653 | char line[8192]; |
a306f2dd | 654 | int found_key = 0; |
a306f2dd | 655 | FILE *f; |
1e3b8b07 | 656 | u_long linenum = 0; |
a306f2dd | 657 | struct stat st; |
658 | Key *found; | |
659 | ||
94ec8c6b | 660 | if (pw == NULL) |
661 | return 0; | |
662 | ||
a306f2dd | 663 | /* Temporarily use the user's uid. */ |
63bd8c36 | 664 | temporarily_use_uid(pw); |
a306f2dd | 665 | |
c8445989 | 666 | debug("trying public key file %s", file); |
a306f2dd | 667 | |
668 | /* Fail quietly if file does not exist */ | |
669 | if (stat(file, &st) < 0) { | |
670 | /* Restore the privileged uid. */ | |
671 | restore_uid(); | |
672 | return 0; | |
673 | } | |
674 | /* Open the file containing the authorized keys. */ | |
675 | f = fopen(file, "r"); | |
676 | if (!f) { | |
677 | /* Restore the privileged uid. */ | |
678 | restore_uid(); | |
679 | return 0; | |
680 | } | |
c8445989 | 681 | if (options.strict_modes && |
682 | secure_filename(f, file, pw->pw_uid, line, sizeof(line)) != 0) { | |
c8445989 | 683 | fclose(f); |
684 | log("Authentication refused: %s", line); | |
685 | restore_uid(); | |
686 | return 0; | |
a306f2dd | 687 | } |
c8445989 | 688 | |
a306f2dd | 689 | found_key = 0; |
de273eef | 690 | found = key_new(key->type); |
a306f2dd | 691 | |
692 | while (fgets(line, sizeof(line), f)) { | |
38c295d6 | 693 | char *cp, *options = NULL; |
a306f2dd | 694 | linenum++; |
695 | /* Skip leading whitespace, empty and comment lines. */ | |
696 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
697 | ; | |
698 | if (!*cp || *cp == '\n' || *cp == '#') | |
699 | continue; | |
38c295d6 | 700 | |
fa08c86b | 701 | if (key_read(found, &cp) == -1) { |
38c295d6 | 702 | /* no key? check if there are options for this key */ |
703 | int quoted = 0; | |
fa08c86b | 704 | debug2("user_key_allowed: check options: '%s'", cp); |
38c295d6 | 705 | options = cp; |
706 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { | |
707 | if (*cp == '\\' && cp[1] == '"') | |
708 | cp++; /* Skip both */ | |
709 | else if (*cp == '"') | |
710 | quoted = !quoted; | |
711 | } | |
712 | /* Skip remaining whitespace. */ | |
713 | for (; *cp == ' ' || *cp == '\t'; cp++) | |
714 | ; | |
fa08c86b | 715 | if (key_read(found, &cp) == -1) { |
716 | debug2("user_key_allowed: advance: '%s'", cp); | |
38c295d6 | 717 | /* still no key? advance to next line*/ |
718 | continue; | |
719 | } | |
720 | } | |
721 | if (key_equal(found, key) && | |
42f11eb2 | 722 | auth_parse_options(pw, options, file, linenum) == 1) { |
a306f2dd | 723 | found_key = 1; |
724 | debug("matching key found: file %s, line %ld", | |
725 | file, linenum); | |
726 | break; | |
727 | } | |
728 | } | |
729 | restore_uid(); | |
730 | fclose(f); | |
731 | key_free(found); | |
539af7f5 | 732 | if (!found_key) |
733 | debug2("key not found"); | |
a306f2dd | 734 | return found_key; |
735 | } | |
8002af61 | 736 | |
96a7b0cc | 737 | /* check whether given key is in .ssh/authorized_keys* */ |
396c147e | 738 | static int |
96a7b0cc | 739 | user_key_allowed(struct passwd *pw, Key *key) |
740 | { | |
741 | int success; | |
742 | char *file; | |
743 | ||
744 | file = authorized_keys_file(pw); | |
745 | success = user_key_allowed2(pw, key, file); | |
746 | xfree(file); | |
747 | if (success) | |
748 | return success; | |
749 | ||
750 | /* try suffix "2" for backward compat, too */ | |
751 | file = authorized_keys_file2(pw); | |
752 | success = user_key_allowed2(pw, key, file); | |
753 | xfree(file); | |
754 | return success; | |
755 | } | |
756 | ||
8002af61 | 757 | /* return 1 if given hostkey is allowed */ |
396c147e | 758 | static int |
57a5edd8 | 759 | hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost, |
8002af61 | 760 | Key *key) |
761 | { | |
8002af61 | 762 | const char *resolvedname, *ipaddr, *lookup; |
f98c3421 | 763 | int host_status, len; |
8002af61 | 764 | |
765 | resolvedname = get_canonical_hostname(options.reverse_mapping_check); | |
766 | ipaddr = get_remote_ipaddr(); | |
767 | ||
f98c3421 | 768 | debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s", |
769 | chost, resolvedname, ipaddr); | |
8002af61 | 770 | |
771 | if (options.hostbased_uses_name_from_packet_only) { | |
772 | if (auth_rhosts2(pw, cuser, chost, chost) == 0) | |
773 | return 0; | |
774 | lookup = chost; | |
775 | } else { | |
f98c3421 | 776 | if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') { |
777 | debug2("stripping trailing dot from chost %s", chost); | |
778 | chost[len - 1] = '\0'; | |
779 | } | |
8002af61 | 780 | if (strcasecmp(resolvedname, chost) != 0) |
781 | log("userauth_hostbased mismatch: " | |
782 | "client sends %s, but we resolve %s to %s", | |
783 | chost, ipaddr, resolvedname); | |
784 | if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) | |
785 | return 0; | |
786 | lookup = resolvedname; | |
787 | } | |
788 | debug2("userauth_hostbased: access allowed by auth_rhosts2"); | |
789 | ||
d0c8ca5c | 790 | host_status = check_key_in_hostfiles(pw, key, lookup, |
791 | _PATH_SSH_SYSTEM_HOSTFILE, | |
73473230 | 792 | options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE); |
d0c8ca5c | 793 | |
794 | /* backward compat if no key has been found. */ | |
795 | if (host_status == HOST_NEW) | |
796 | host_status = check_key_in_hostfiles(pw, key, lookup, | |
797 | _PATH_SSH_SYSTEM_HOSTFILE2, | |
73473230 | 798 | options.ignore_user_known_hosts ? NULL : |
799 | _PATH_SSH_USER_HOSTFILE2); | |
8002af61 | 800 | |
8002af61 | 801 | return (host_status == HOST_OK); |
802 | } | |
d0c8ca5c | 803 |