2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: auth2.c,v 1.78 2001/12/20 22:50:24 djm Exp $");
28 #include <openssl/evp.h>
47 #include "pathnames.h"
49 #include "auth-options.h"
56 extern ServerOptions options;
57 extern u_char *session_id2;
58 extern int session_id2_len;
60 static Authctxt *x_authctxt = NULL;
63 typedef struct Authmethod Authmethod;
66 int (*userauth)(Authctxt *authctxt);
72 static void input_service_request(int, int, u_int32_t, void *);
73 static void input_userauth_request(int, int, u_int32_t, void *);
74 static void protocol_error(int, int, u_int32_t, void *);
77 static Authmethod *authmethod_lookup(const char *);
78 static char *authmethods_get(void);
79 static int user_key_allowed(struct passwd *, Key *);
80 static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
83 static void userauth_banner(void);
84 static int userauth_none(Authctxt *);
85 static int userauth_passwd(Authctxt *);
86 static int userauth_pubkey(Authctxt *);
87 static int userauth_hostbased(Authctxt *);
88 static int userauth_kbdint(Authctxt *);
90 Authmethod authmethods[] = {
96 &options.pubkey_authentication},
99 &options.password_authentication},
100 {"keyboard-interactive",
102 &options.kbd_interactive_authentication},
105 &options.hostbased_authentication},
110 * loop until authctxt->success == TRUE
114 do_authentication2(void)
116 Authctxt *authctxt = authctxt_new();
118 x_authctxt = authctxt; /*XXX*/
120 /* challenge-response is implemented via keyboard interactive */
121 if (options.challenge_response_authentication)
122 options.kbd_interactive_authentication = 1;
123 if (options.pam_authentication_via_kbd_int)
124 options.kbd_interactive_authentication = 1;
126 dispatch_init(&protocol_error);
127 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
128 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
129 do_authenticated(authctxt);
133 protocol_error(int type, int plen, u_int32_t seq, void *ctxt)
135 log("auth: protocol error: type %d plen %d", type, plen);
136 packet_start(SSH2_MSG_UNIMPLEMENTED);
143 input_service_request(int type, int plen, u_int32_t seq, void *ctxt)
145 Authctxt *authctxt = ctxt;
148 char *service = packet_get_string(&len);
151 if (authctxt == NULL)
152 fatal("input_service_request: no authctxt");
154 if (strcmp(service, "ssh-userauth") == 0) {
155 if (!authctxt->success) {
157 /* now we can handle user-auth requests */
158 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
161 /* XXX all other service requests are denied */
164 packet_start(SSH2_MSG_SERVICE_ACCEPT);
165 packet_put_cstring(service);
169 debug("bad service request %s", service);
170 packet_disconnect("bad service request %s", service);
176 input_userauth_request(int type, int plen, u_int32_t seq, void *ctxt)
178 Authctxt *authctxt = ctxt;
179 Authmethod *m = NULL;
180 char *user, *service, *method, *style = NULL;
181 int authenticated = 0;
183 if (authctxt == NULL)
184 fatal("input_userauth_request: no authctxt");
186 user = packet_get_string(NULL);
187 service = packet_get_string(NULL);
188 method = packet_get_string(NULL);
189 debug("userauth-request for user %s service %s method %s", user, service, method);
190 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
192 if ((style = strchr(user, ':')) != NULL)
195 if (authctxt->attempt++ == 0) {
196 /* setup auth context */
197 struct passwd *pw = NULL;
199 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
200 authctxt->pw = pwcopy(pw);
202 debug2("input_userauth_request: setting up authctxt for %s", user);
204 start_pam(pw->pw_name);
207 log("input_userauth_request: illegal user %s", user);
212 setproctitle("%s", pw ? user : "unknown");
213 authctxt->user = xstrdup(user);
214 authctxt->service = xstrdup(service);
215 authctxt->style = style ? xstrdup(style) : NULL;
216 } else if (strcmp(user, authctxt->user) != 0 ||
217 strcmp(service, authctxt->service) != 0) {
218 packet_disconnect("Change of username or service not allowed: "
219 "(%s,%s) -> (%s,%s)",
220 authctxt->user, authctxt->service, user, service);
223 auth2_challenge_stop(authctxt);
224 authctxt->postponed = 0;
226 /* try to authenticate user */
227 m = authmethod_lookup(method);
229 debug2("input_userauth_request: try method %s", method);
230 authenticated = m->userauth(authctxt);
232 userauth_finish(authctxt, authenticated, method);
240 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
244 if (!authctxt->valid && authenticated)
245 fatal("INTERNAL ERROR: authenticated invalid user %s",
248 /* Special handling for root */
249 if (authenticated && authctxt->pw->pw_uid == 0 &&
250 !auth_root_allowed(method))
254 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
259 /* Log before sending the reply */
260 auth_log(authctxt, authenticated, method, " ssh2");
262 if (authctxt->postponed)
265 /* XXX todo: check if multiple auth methods are needed */
266 if (authenticated == 1) {
267 /* turn off userauth */
268 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
269 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
272 /* now we can break out */
273 authctxt->success = 1;
275 if (authctxt->failures++ > AUTH_FAIL_MAX) {
276 #ifdef WITH_AIXAUTHENTICATE
277 loginfailed(authctxt->user,
278 get_canonical_hostname(options.reverse_mapping_check),
280 #endif /* WITH_AIXAUTHENTICATE */
281 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
283 methods = authmethods_get();
284 packet_start(SSH2_MSG_USERAUTH_FAILURE);
285 packet_put_cstring(methods);
286 packet_put_char(0); /* XXX partial success, unused */
294 userauth_banner(void)
301 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
303 if ((fd = open(options.banner, O_RDONLY)) < 0)
305 if (fstat(fd, &st) < 0)
308 banner = xmalloc(len + 1);
309 if ((n = read(fd, banner, len)) < 0)
312 packet_start(SSH2_MSG_USERAUTH_BANNER);
313 packet_put_cstring(banner);
314 packet_put_cstring(""); /* language, unused */
316 debug("userauth_banner: sent");
325 userauth_none(Authctxt *authctxt)
327 /* disable method "none", only allowed one time */
328 Authmethod *m = authmethod_lookup("none");
334 if (authctxt->valid == 0)
338 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
342 return auth_pam_password(authctxt->pw, "");
343 #elif defined(HAVE_OSF_SIA)
345 #else /* !HAVE_OSF_SIA && !USE_PAM */
346 return auth_password(authctxt, "");
351 userauth_passwd(Authctxt *authctxt)
354 int authenticated = 0;
357 change = packet_get_char();
359 log("password change not supported");
360 password = packet_get_string(&len);
362 if (authctxt->valid &&
364 check_nt_auth(1, authctxt->pw->pw_uid) &&
367 auth_pam_password(authctxt->pw, password) == 1)
368 #elif defined(HAVE_OSF_SIA)
369 auth_sia_password(authctxt->user, password) == 1)
370 #else /* !USE_PAM && !HAVE_OSF_SIA */
371 auth_password(authctxt, password) == 1)
374 memset(password, 0, len);
376 return authenticated;
380 userauth_kbdint(Authctxt *authctxt)
382 int authenticated = 0;
385 lang = packet_get_string(NULL);
386 devs = packet_get_string(NULL);
389 debug("keyboard-interactive devs %s", devs);
391 if (options.challenge_response_authentication)
392 authenticated = auth2_challenge(authctxt, devs);
395 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
396 authenticated = auth2_pam(authctxt);
401 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
404 return authenticated;
408 userauth_pubkey(Authctxt *authctxt)
412 char *pkalg, *pkblob, *sig;
413 u_int alen, blen, slen;
414 int have_sig, pktype;
415 int authenticated = 0;
417 if (!authctxt->valid) {
418 debug2("userauth_pubkey: disabled because of invalid user");
421 have_sig = packet_get_char();
422 if (datafellows & SSH_BUG_PKAUTH) {
423 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
424 /* no explicit pkalg given */
425 pkblob = packet_get_string(&blen);
427 buffer_append(&b, pkblob, blen);
428 /* so we have to extract the pkalg from the pkblob */
429 pkalg = buffer_get_string(&b, &alen);
432 pkalg = packet_get_string(&alen);
433 pkblob = packet_get_string(&blen);
435 pktype = key_type_from_name(pkalg);
436 if (pktype == KEY_UNSPEC) {
437 /* this is perfectly legal */
438 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
443 key = key_from_blob(pkblob, blen);
446 sig = packet_get_string(&slen);
449 if (datafellows & SSH_OLD_SESSIONID) {
450 buffer_append(&b, session_id2, session_id2_len);
452 buffer_put_string(&b, session_id2, session_id2_len);
454 /* reconstruct packet */
455 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
456 buffer_put_cstring(&b, authctxt->user);
457 buffer_put_cstring(&b,
458 datafellows & SSH_BUG_PKSERVICE ?
461 if (datafellows & SSH_BUG_PKAUTH) {
462 buffer_put_char(&b, have_sig);
464 buffer_put_cstring(&b, "publickey");
465 buffer_put_char(&b, have_sig);
466 buffer_put_cstring(&b, pkalg);
468 buffer_put_string(&b, pkblob, blen);
472 /* test for correct signature */
473 if (user_key_allowed(authctxt->pw, key) &&
474 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
479 debug("test whether pkalg/pkblob are acceptable");
482 /* XXX fake reply and always send PK_OK ? */
484 * XXX this allows testing whether a user is allowed
485 * to login: if you happen to have a valid pubkey this
486 * message is sent. the message is NEVER sent at all
487 * if a user is not allowed to login. is this an
490 if (user_key_allowed(authctxt->pw, key)) {
491 packet_start(SSH2_MSG_USERAUTH_PK_OK);
492 packet_put_string(pkalg, alen);
493 packet_put_string(pkblob, blen);
496 authctxt->postponed = 1;
499 if (authenticated != 1)
500 auth_clear_options();
503 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
507 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
510 return authenticated;
514 userauth_hostbased(Authctxt *authctxt)
518 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
519 u_int alen, blen, slen;
521 int authenticated = 0;
523 if (!authctxt->valid) {
524 debug2("userauth_hostbased: disabled because of invalid user");
527 pkalg = packet_get_string(&alen);
528 pkblob = packet_get_string(&blen);
529 chost = packet_get_string(NULL);
530 cuser = packet_get_string(NULL);
531 sig = packet_get_string(&slen);
533 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
534 cuser, chost, pkalg, slen);
538 buffer_append(&b, sig, slen);
542 pktype = key_type_from_name(pkalg);
543 if (pktype == KEY_UNSPEC) {
544 /* this is perfectly legal */
545 log("userauth_hostbased: unsupported "
546 "public key algorithm: %s", pkalg);
549 key = key_from_blob(pkblob, blen);
551 debug("userauth_hostbased: cannot decode key: %s", pkalg);
554 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
557 buffer_put_string(&b, session_id2, session_id2_len);
558 /* reconstruct packet */
559 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
560 buffer_put_cstring(&b, authctxt->user);
561 buffer_put_cstring(&b, service);
562 buffer_put_cstring(&b, "hostbased");
563 buffer_put_string(&b, pkalg, alen);
564 buffer_put_string(&b, pkblob, blen);
565 buffer_put_cstring(&b, chost);
566 buffer_put_cstring(&b, cuser);
570 /* test for allowed key and correct signature */
571 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
572 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
579 debug2("userauth_hostbased: authenticated %d", authenticated);
585 return authenticated;
588 /* get current user */
593 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
599 authmethods_get(void)
601 Authmethod *method = NULL;
605 for (method = authmethods; method->name != NULL; method++) {
606 if (strcmp(method->name, "none") == 0)
608 if (method->enabled != NULL && *(method->enabled) != 0) {
610 size += strlen(DELIM);
611 size += strlen(method->name);
614 size++; /* trailing '\0' */
615 list = xmalloc(size);
618 for (method = authmethods; method->name != NULL; method++) {
619 if (strcmp(method->name, "none") == 0)
621 if (method->enabled != NULL && *(method->enabled) != 0) {
623 strlcat(list, DELIM, size);
624 strlcat(list, method->name, size);
631 authmethod_lookup(const char *name)
633 Authmethod *method = NULL;
635 for (method = authmethods; method->name != NULL; method++)
636 if (method->enabled != NULL &&
637 *(method->enabled) != 0 &&
638 strcmp(name, method->name) == 0)
640 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
644 /* return 1 if user allows given key */
646 user_key_allowed2(struct passwd *pw, Key *key, char *file)
659 /* Temporarily use the user's uid. */
660 temporarily_use_uid(pw);
662 debug("trying public key file %s", file);
664 /* Fail quietly if file does not exist */
665 if (stat(file, &st) < 0) {
666 /* Restore the privileged uid. */
670 /* Open the file containing the authorized keys. */
671 f = fopen(file, "r");
673 /* Restore the privileged uid. */
677 if (options.strict_modes &&
678 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
680 log("Authentication refused: %s", line);
686 found = key_new(key->type);
688 while (fgets(line, sizeof(line), f)) {
689 char *cp, *options = NULL;
691 /* Skip leading whitespace, empty and comment lines. */
692 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
694 if (!*cp || *cp == '\n' || *cp == '#')
697 if (key_read(found, &cp) != 1) {
698 /* no key? check if there are options for this key */
700 debug2("user_key_allowed: check options: '%s'", cp);
702 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
703 if (*cp == '\\' && cp[1] == '"')
704 cp++; /* Skip both */
708 /* Skip remaining whitespace. */
709 for (; *cp == ' ' || *cp == '\t'; cp++)
711 if (key_read(found, &cp) != 1) {
712 debug2("user_key_allowed: advance: '%s'", cp);
713 /* still no key? advance to next line*/
717 if (key_equal(found, key) &&
718 auth_parse_options(pw, options, file, linenum) == 1) {
720 debug("matching key found: file %s, line %lu",
722 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
723 verbose("Found matching %s key: %s",
724 key_type(found), fp);
733 debug2("key not found");
737 /* check whether given key is in .ssh/authorized_keys* */
739 user_key_allowed(struct passwd *pw, Key *key)
744 file = authorized_keys_file(pw);
745 success = user_key_allowed2(pw, key, file);
750 /* try suffix "2" for backward compat, too */
751 file = authorized_keys_file2(pw);
752 success = user_key_allowed2(pw, key, file);
757 /* return 1 if given hostkey is allowed */
759 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
762 const char *resolvedname, *ipaddr, *lookup;
763 HostStatus host_status;
766 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
767 ipaddr = get_remote_ipaddr();
769 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
770 chost, resolvedname, ipaddr);
772 if (options.hostbased_uses_name_from_packet_only) {
773 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
777 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
778 debug2("stripping trailing dot from chost %s", chost);
779 chost[len - 1] = '\0';
781 if (strcasecmp(resolvedname, chost) != 0)
782 log("userauth_hostbased mismatch: "
783 "client sends %s, but we resolve %s to %s",
784 chost, ipaddr, resolvedname);
785 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
787 lookup = resolvedname;
789 debug2("userauth_hostbased: access allowed by auth_rhosts2");
791 host_status = check_key_in_hostfiles(pw, key, lookup,
792 _PATH_SSH_SYSTEM_HOSTFILE,
793 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
795 /* backward compat if no key has been found. */
796 if (host_status == HOST_NEW)
797 host_status = check_key_in_hostfiles(pw, key, lookup,
798 _PATH_SSH_SYSTEM_HOSTFILE2,
799 options.ignore_user_known_hosts ? NULL :
800 _PATH_SSH_USER_HOSTFILE2);
802 return (host_status == HOST_OK);