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