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.72 2001/11/07 22:41:51 markus Exp $");
28 #include <openssl/evp.h>
47 #include "pathnames.h"
49 #include "auth-options.h"
60 extern ServerOptions options;
61 extern u_char *session_id2;
62 extern int session_id2_len;
64 static Authctxt *x_authctxt = NULL;
67 typedef struct Authmethod Authmethod;
70 int (*userauth)(Authctxt *authctxt);
76 static void input_service_request(int, int, void *);
77 static void input_userauth_request(int, int, void *);
78 static void protocol_error(int, int, void *);
81 static Authmethod *authmethod_lookup(const char *);
82 static char *authmethods_get(void);
83 static int user_key_allowed(struct passwd *, Key *);
84 static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
87 static void userauth_banner(void);
88 static int userauth_none(Authctxt *);
89 static int userauth_passwd(Authctxt *);
90 static int userauth_pubkey(Authctxt *);
91 static int userauth_hostbased(Authctxt *);
92 static int userauth_kbdint(Authctxt *);
95 int userauth_external(Authctxt *authctxt);
96 int userauth_gssapi(Authctxt *authctxt);
99 Authmethod authmethods[] = {
106 &options.gss_authentication},
109 &options.gss_authentication},
113 &options.pubkey_authentication},
116 &options.password_authentication},
117 {"keyboard-interactive",
119 &options.kbd_interactive_authentication},
122 &options.hostbased_authentication},
127 * loop until authctxt->success == TRUE
133 Authctxt *authctxt = authctxt_new();
135 x_authctxt = authctxt; /*XXX*/
137 /* challenge-response is implemented via keyboard interactive */
138 if (options.challenge_response_authentication)
139 options.kbd_interactive_authentication = 1;
140 if (options.pam_authentication_via_kbd_int)
141 options.kbd_interactive_authentication = 1;
143 dispatch_init(&protocol_error);
144 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
145 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
146 do_authenticated(authctxt);
150 protocol_error(int type, int plen, void *ctxt)
152 log("auth: protocol error: type %d plen %d", type, plen);
153 packet_start(SSH2_MSG_UNIMPLEMENTED);
160 input_service_request(int type, int plen, void *ctxt)
162 Authctxt *authctxt = ctxt;
165 char *service = packet_get_string(&len);
168 if (authctxt == NULL)
169 fatal("input_service_request: no authctxt");
171 if (strcmp(service, "ssh-userauth") == 0) {
172 if (!authctxt->success) {
174 /* now we can handle user-auth requests */
175 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
178 /* XXX all other service requests are denied */
181 packet_start(SSH2_MSG_SERVICE_ACCEPT);
182 packet_put_cstring(service);
186 debug("bad service request %s", service);
187 packet_disconnect("bad service request %s", service);
193 input_userauth_request(int type, int plen, void *ctxt)
195 Authctxt *authctxt = ctxt;
196 Authmethod *m = NULL;
197 char *user, *service, *method, *style = NULL;
198 int authenticated = 0;
200 if (authctxt == NULL)
201 fatal("input_userauth_request: no authctxt");
203 user = packet_get_string(NULL);
204 service = packet_get_string(NULL);
205 method = packet_get_string(NULL);
206 debug("userauth-request for user %s service %s method %s", user, service, method);
207 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
209 if ((style = strchr(user, ':')) != NULL)
212 if (authctxt->attempt++ == 0) {
213 /* setup auth context */
214 struct passwd *pw = NULL;
216 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
217 authctxt->pw = pwcopy(pw);
219 debug2("input_userauth_request: setting up authctxt for %s", user);
221 start_pam(pw->pw_name);
224 log("input_userauth_request: illegal user %s", user);
229 setproctitle("%s", pw ? user : "unknown");
230 authctxt->user = xstrdup(user);
231 authctxt->service = xstrdup(service);
232 authctxt->style = style ? xstrdup(style) : NULL;
233 } else if (strcmp(user, authctxt->user) != 0 ||
234 strcmp(service, authctxt->service) != 0) {
235 packet_disconnect("Change of username or service not allowed: "
236 "(%s,%s) -> (%s,%s)",
237 authctxt->user, authctxt->service, user, service);
240 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
243 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &protocol_error);
244 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, &protocol_error);
247 authctxt->postponed = 0;
250 auth_close(authctxt->as);
255 /* try to authenticate user */
256 m = authmethod_lookup(method);
258 debug2("input_userauth_request: try method %s", method);
259 authenticated = m->userauth(authctxt);
261 userauth_finish(authctxt, authenticated, method);
269 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
273 if (!authctxt->valid && authenticated)
274 fatal("INTERNAL ERROR: authenticated invalid user %s",
277 /* Special handling for root */
278 if (authenticated && authctxt->pw->pw_uid == 0 &&
279 !auth_root_allowed(method))
283 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
288 /* Log before sending the reply */
289 auth_log(authctxt, authenticated, method, " ssh2");
291 if (authctxt->postponed)
294 /* XXX todo: check if multiple auth methods are needed */
295 if (authenticated == 1) {
296 /* turn off userauth */
297 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
298 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
301 /* now we can break out */
302 authctxt->success = 1;
304 if (authctxt->failures++ > AUTH_FAIL_MAX) {
305 #ifdef WITH_AIXAUTHENTICATE
306 loginfailed(authctxt->user,
307 get_canonical_hostname(options.reverse_mapping_check),
309 #endif /* WITH_AIXAUTHENTICATE */
310 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
312 methods = authmethods_get();
313 packet_start(SSH2_MSG_USERAUTH_FAILURE);
314 packet_put_cstring(methods);
315 packet_put_char(0); /* XXX partial success, unused */
323 userauth_banner(void)
330 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
332 if ((fd = open(options.banner, O_RDONLY)) < 0)
334 if (fstat(fd, &st) < 0)
337 banner = xmalloc(len + 1);
338 if ((n = read(fd, banner, len)) < 0)
341 packet_start(SSH2_MSG_USERAUTH_BANNER);
342 packet_put_cstring(banner);
343 packet_put_cstring(""); /* language, unused */
345 debug("userauth_banner: sent");
354 userauth_none(Authctxt *authctxt)
356 /* disable method "none", only allowed one time */
357 Authmethod *m = authmethod_lookup("none");
363 if (authctxt->valid == 0)
367 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
371 return auth_pam_password(authctxt->pw, "");
372 #elif defined(HAVE_OSF_SIA)
374 #else /* !HAVE_OSF_SIA && !USE_PAM */
375 return auth_password(authctxt, "");
380 userauth_passwd(Authctxt *authctxt)
383 int authenticated = 0;
386 change = packet_get_char();
388 log("password change not supported");
389 password = packet_get_string(&len);
391 if (authctxt->valid &&
393 check_nt_auth(1, authctxt->pw->pw_uid) &&
396 auth_pam_password(authctxt->pw, password) == 1)
397 #elif defined(HAVE_OSF_SIA)
398 auth_sia_password(authctxt->user, password) == 1)
399 #else /* !USE_PAM && !HAVE_OSF_SIA */
400 auth_password(authctxt, password) == 1)
403 memset(password, 0, len);
405 return authenticated;
409 userauth_kbdint(Authctxt *authctxt)
411 int authenticated = 0;
414 lang = packet_get_string(NULL);
415 devs = packet_get_string(NULL);
418 debug("keyboard-interactive devs %s", devs);
420 if (options.challenge_response_authentication)
421 authenticated = auth2_challenge(authctxt, devs);
424 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
425 authenticated = auth2_pam(authctxt);
430 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
433 return authenticated;
437 userauth_pubkey(Authctxt *authctxt)
441 char *pkalg, *pkblob, *sig;
442 u_int alen, blen, slen;
443 int have_sig, pktype;
444 int authenticated = 0;
446 if (!authctxt->valid) {
447 debug2("userauth_pubkey: disabled because of invalid user");
450 have_sig = packet_get_char();
451 if (datafellows & SSH_BUG_PKAUTH) {
452 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
453 /* no explicit pkalg given */
454 pkblob = packet_get_string(&blen);
456 buffer_append(&b, pkblob, blen);
457 /* so we have to extract the pkalg from the pkblob */
458 pkalg = buffer_get_string(&b, &alen);
461 pkalg = packet_get_string(&alen);
462 pkblob = packet_get_string(&blen);
464 pktype = key_type_from_name(pkalg);
465 if (pktype == KEY_UNSPEC) {
466 /* this is perfectly legal */
467 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
472 key = key_from_blob(pkblob, blen);
475 sig = packet_get_string(&slen);
478 if (datafellows & SSH_OLD_SESSIONID) {
479 buffer_append(&b, session_id2, session_id2_len);
481 buffer_put_string(&b, session_id2, session_id2_len);
483 /* reconstruct packet */
484 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
485 buffer_put_cstring(&b, authctxt->user);
486 buffer_put_cstring(&b,
487 datafellows & SSH_BUG_PKSERVICE ?
490 if (datafellows & SSH_BUG_PKAUTH) {
491 buffer_put_char(&b, have_sig);
493 buffer_put_cstring(&b, "publickey");
494 buffer_put_char(&b, have_sig);
495 buffer_put_cstring(&b, pkalg);
497 buffer_put_string(&b, pkblob, blen);
501 /* test for correct signature */
502 if (user_key_allowed(authctxt->pw, key) &&
503 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
508 debug("test whether pkalg/pkblob are acceptable");
511 /* XXX fake reply and always send PK_OK ? */
513 * XXX this allows testing whether a user is allowed
514 * to login: if you happen to have a valid pubkey this
515 * message is sent. the message is NEVER sent at all
516 * if a user is not allowed to login. is this an
519 if (user_key_allowed(authctxt->pw, key)) {
520 packet_start(SSH2_MSG_USERAUTH_PK_OK);
521 packet_put_string(pkalg, alen);
522 packet_put_string(pkblob, blen);
525 authctxt->postponed = 1;
528 if (authenticated != 1)
529 auth_clear_options();
532 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
536 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
539 return authenticated;
543 userauth_hostbased(Authctxt *authctxt)
547 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
548 u_int alen, blen, slen;
550 int authenticated = 0;
552 if (!authctxt->valid) {
553 debug2("userauth_hostbased: disabled because of invalid user");
556 pkalg = packet_get_string(&alen);
557 pkblob = packet_get_string(&blen);
558 chost = packet_get_string(NULL);
559 cuser = packet_get_string(NULL);
560 sig = packet_get_string(&slen);
562 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
563 cuser, chost, pkalg, slen);
567 buffer_append(&b, sig, slen);
571 pktype = key_type_from_name(pkalg);
572 if (pktype == KEY_UNSPEC) {
573 /* this is perfectly legal */
574 log("userauth_hostbased: unsupported "
575 "public key algorithm: %s", pkalg);
578 key = key_from_blob(pkblob, blen);
580 debug("userauth_hostbased: cannot decode key: %s", pkalg);
583 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
586 buffer_put_string(&b, session_id2, session_id2_len);
587 /* reconstruct packet */
588 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
589 buffer_put_cstring(&b, authctxt->user);
590 buffer_put_cstring(&b, service);
591 buffer_put_cstring(&b, "hostbased");
592 buffer_put_string(&b, pkalg, alen);
593 buffer_put_string(&b, pkblob, blen);
594 buffer_put_cstring(&b, chost);
595 buffer_put_cstring(&b, cuser);
599 /* test for allowed key and correct signature */
600 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
601 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
608 debug2("userauth_hostbased: authenticated %d", authenticated);
614 return authenticated;
617 /* get current user */
622 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
628 authmethods_get(void)
630 Authmethod *method = NULL;
634 for (method = authmethods; method->name != NULL; method++) {
635 if (strcmp(method->name, "none") == 0)
637 if (method->enabled != NULL && *(method->enabled) != 0) {
639 size += strlen(DELIM);
640 size += strlen(method->name);
643 size++; /* trailing '\0' */
644 list = xmalloc(size);
647 for (method = authmethods; method->name != NULL; method++) {
648 if (strcmp(method->name, "none") == 0)
650 if (method->enabled != NULL && *(method->enabled) != 0) {
652 strlcat(list, DELIM, size);
653 strlcat(list, method->name, size);
660 authmethod_lookup(const char *name)
662 Authmethod *method = NULL;
664 for (method = authmethods; method->name != NULL; method++)
665 if (method->enabled != NULL &&
666 *(method->enabled) != 0 &&
667 strcmp(name, method->name) == 0)
669 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
673 /* return 1 if user allows given key */
675 user_key_allowed2(struct passwd *pw, Key *key, char *file)
687 /* Temporarily use the user's uid. */
688 temporarily_use_uid(pw);
690 debug("trying public key file %s", file);
692 /* Fail quietly if file does not exist */
693 if (stat(file, &st) < 0) {
694 /* Restore the privileged uid. */
698 /* Open the file containing the authorized keys. */
699 f = fopen(file, "r");
701 /* Restore the privileged uid. */
705 if (options.strict_modes &&
706 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
708 log("Authentication refused: %s", line);
714 found = key_new(key->type);
716 while (fgets(line, sizeof(line), f)) {
717 char *cp, *options = NULL;
719 /* Skip leading whitespace, empty and comment lines. */
720 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
722 if (!*cp || *cp == '\n' || *cp == '#')
725 if (key_read(found, &cp) != 1) {
726 /* no key? check if there are options for this key */
728 debug2("user_key_allowed: check options: '%s'", cp);
730 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
731 if (*cp == '\\' && cp[1] == '"')
732 cp++; /* Skip both */
736 /* Skip remaining whitespace. */
737 for (; *cp == ' ' || *cp == '\t'; cp++)
739 if (key_read(found, &cp) != 1) {
740 debug2("user_key_allowed: advance: '%s'", cp);
741 /* still no key? advance to next line*/
745 if (key_equal(found, key) &&
746 auth_parse_options(pw, options, file, linenum) == 1) {
748 debug("matching key found: file %s, line %lu",
757 debug2("key not found");
761 /* check whether given key is in .ssh/authorized_keys* */
763 user_key_allowed(struct passwd *pw, Key *key)
768 file = authorized_keys_file(pw);
769 success = user_key_allowed2(pw, key, file);
774 /* try suffix "2" for backward compat, too */
775 file = authorized_keys_file2(pw);
776 success = user_key_allowed2(pw, key, file);
781 /* return 1 if given hostkey is allowed */
783 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
786 const char *resolvedname, *ipaddr, *lookup;
787 int host_status, len;
789 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
790 ipaddr = get_remote_ipaddr();
792 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
793 chost, resolvedname, ipaddr);
795 if (options.hostbased_uses_name_from_packet_only) {
796 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
800 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
801 debug2("stripping trailing dot from chost %s", chost);
802 chost[len - 1] = '\0';
804 if (strcasecmp(resolvedname, chost) != 0)
805 log("userauth_hostbased mismatch: "
806 "client sends %s, but we resolve %s to %s",
807 chost, ipaddr, resolvedname);
808 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
810 lookup = resolvedname;
812 debug2("userauth_hostbased: access allowed by auth_rhosts2");
814 host_status = check_key_in_hostfiles(pw, key, lookup,
815 _PATH_SSH_SYSTEM_HOSTFILE,
816 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
818 /* backward compat if no key has been found. */
819 if (host_status == HOST_NEW)
820 host_status = check_key_in_hostfiles(pw, key, lookup,
821 _PATH_SSH_SYSTEM_HOSTFILE2,
822 options.ignore_user_known_hosts ? NULL :
823 _PATH_SSH_USER_HOSTFILE2);
825 return (host_status == HOST_OK);