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