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