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.69 2001/07/23 18:14:58 stevesk Exp $");
28 #include <openssl/evp.h>
47 #include "pathnames.h"
49 #include "auth-options.h"
53 #include "tildexpand.h"
57 extern ServerOptions options;
58 extern u_char *session_id2;
59 extern int session_id2_len;
61 #ifdef WITH_AIXAUTHENTICATE
62 extern char *aixloginmsg;
65 static Authctxt *x_authctxt = NULL;
68 typedef struct Authmethod Authmethod;
71 int (*userauth)(Authctxt *authctxt);
77 static void input_service_request(int, int, void *);
78 static void input_userauth_request(int, int, void *);
79 static void protocol_error(int, int, void *);
82 static Authmethod *authmethod_lookup(const char *);
83 static char *authmethods_get(void);
84 static int user_key_allowed(struct passwd *, Key *);
85 static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
88 static void userauth_banner(void);
89 static int userauth_none(Authctxt *);
90 static int userauth_passwd(Authctxt *);
91 static int userauth_pubkey(Authctxt *);
92 static int userauth_hostbased(Authctxt *);
93 static int userauth_kbdint(Authctxt *);
95 Authmethod authmethods[] = {
101 &options.pubkey_authentication},
104 &options.password_authentication},
105 {"keyboard-interactive",
107 &options.kbd_interactive_authentication},
110 &options.hostbased_authentication},
115 * loop until authctxt->success == TRUE
121 Authctxt *authctxt = authctxt_new();
123 x_authctxt = authctxt; /*XXX*/
125 /* challenge-reponse is implemented via keyboard interactive */
126 if (options.challenge_response_authentication)
127 options.kbd_interactive_authentication = 1;
128 if (options.pam_authentication_via_kbd_int)
129 options.kbd_interactive_authentication = 1;
131 dispatch_init(&protocol_error);
132 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
133 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
134 do_authenticated(authctxt);
138 protocol_error(int type, int plen, void *ctxt)
140 log("auth: protocol error: type %d plen %d", type, plen);
141 packet_start(SSH2_MSG_UNIMPLEMENTED);
148 input_service_request(int type, int plen, void *ctxt)
150 Authctxt *authctxt = ctxt;
153 char *service = packet_get_string(&len);
156 if (authctxt == NULL)
157 fatal("input_service_request: no authctxt");
159 if (strcmp(service, "ssh-userauth") == 0) {
160 if (!authctxt->success) {
162 /* now we can handle user-auth requests */
163 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
166 /* XXX all other service requests are denied */
169 packet_start(SSH2_MSG_SERVICE_ACCEPT);
170 packet_put_cstring(service);
174 debug("bad service request %s", service);
175 packet_disconnect("bad service request %s", service);
181 input_userauth_request(int type, int plen, void *ctxt)
183 Authctxt *authctxt = ctxt;
184 Authmethod *m = NULL;
185 char *user, *service, *method, *style = NULL;
186 int authenticated = 0;
188 if (authctxt == NULL)
189 fatal("input_userauth_request: no authctxt");
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);
195 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
197 if ((style = strchr(user, ':')) != NULL)
200 if (authctxt->attempt++ == 0) {
201 /* setup auth context */
202 struct passwd *pw = NULL;
204 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
205 authctxt->pw = pwcopy(pw);
207 debug2("input_userauth_request: setting up authctxt for %s", user);
209 start_pam(pw->pw_name);
212 log("input_userauth_request: illegal user %s", user);
217 setproctitle("%s", pw ? user : "unknown");
218 authctxt->user = xstrdup(user);
219 authctxt->service = xstrdup(service);
220 authctxt->style = style ? xstrdup(style) : NULL;
221 } else if (strcmp(user, authctxt->user) != 0 ||
222 strcmp(service, authctxt->service) != 0) {
223 packet_disconnect("Change of username or service not allowed: "
224 "(%s,%s) -> (%s,%s)",
225 authctxt->user, authctxt->service, user, service);
228 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
229 authctxt->postponed = 0;
232 auth_close(authctxt->as);
237 /* try to authenticate user */
238 m = authmethod_lookup(method);
240 debug2("input_userauth_request: try method %s", method);
241 authenticated = m->userauth(authctxt);
243 userauth_finish(authctxt, authenticated, method);
251 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
255 if (!authctxt->valid && authenticated)
256 fatal("INTERNAL ERROR: authenticated invalid user %s",
259 /* Special handling for root */
260 if (authenticated && authctxt->pw->pw_uid == 0 &&
261 !auth_root_allowed(method))
265 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
270 /* Log before sending the reply */
271 auth_log(authctxt, authenticated, method, " ssh2");
273 if (authctxt->postponed)
276 /* XXX todo: check if multiple auth methods are needed */
277 if (authenticated == 1) {
278 /* turn off userauth */
279 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
280 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
283 /* now we can break out */
284 authctxt->success = 1;
286 if (authctxt->failures++ > AUTH_FAIL_MAX)
287 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
288 methods = authmethods_get();
289 packet_start(SSH2_MSG_USERAUTH_FAILURE);
290 packet_put_cstring(methods);
291 packet_put_char(0); /* XXX partial success, unused */
299 userauth_banner(void)
306 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
308 if ((fd = open(options.banner, O_RDONLY)) < 0)
310 if (fstat(fd, &st) < 0)
313 banner = xmalloc(len + 1);
314 if ((n = read(fd, banner, len)) < 0)
317 packet_start(SSH2_MSG_USERAUTH_BANNER);
318 packet_put_cstring(banner);
319 packet_put_cstring(""); /* language, unused */
321 debug("userauth_banner: sent");
330 userauth_none(Authctxt *authctxt)
332 /* disable method "none", only allowed one time */
333 Authmethod *m = authmethod_lookup("none");
339 if (authctxt->valid == 0)
343 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
347 return auth_pam_password(authctxt->pw, "");
348 #elif defined(HAVE_OSF_SIA)
350 #else /* !HAVE_OSF_SIA && !USE_PAM */
351 return auth_password(authctxt, "");
356 userauth_passwd(Authctxt *authctxt)
359 int authenticated = 0;
362 change = packet_get_char();
364 log("password change not supported");
365 password = packet_get_string(&len);
367 if (authctxt->valid &&
369 check_nt_auth(1, authctxt->pw->pw_uid) &&
372 auth_pam_password(authctxt->pw, password) == 1)
373 #elif defined(HAVE_OSF_SIA)
374 auth_sia_password(authctxt->user, password) == 1)
375 #else /* !USE_PAM && !HAVE_OSF_SIA */
376 auth_password(authctxt, password) == 1)
379 memset(password, 0, len);
381 return authenticated;
385 userauth_kbdint(Authctxt *authctxt)
387 int authenticated = 0;
390 lang = packet_get_string(NULL);
391 devs = packet_get_string(NULL);
394 debug("keyboard-interactive devs %s", devs);
396 if (options.challenge_response_authentication)
397 authenticated = auth2_challenge(authctxt, devs);
400 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
401 authenticated = auth2_pam(authctxt);
406 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
409 return authenticated;
413 userauth_pubkey(Authctxt *authctxt)
417 char *pkalg, *pkblob, *sig;
418 u_int alen, blen, slen;
419 int have_sig, pktype;
420 int authenticated = 0;
422 if (!authctxt->valid) {
423 debug2("userauth_pubkey: disabled because of invalid user");
426 have_sig = packet_get_char();
427 if (datafellows & SSH_BUG_PKAUTH) {
428 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
429 /* no explicit pkalg given */
430 pkblob = packet_get_string(&blen);
432 buffer_append(&b, pkblob, blen);
433 /* so we have to extract the pkalg from the pkblob */
434 pkalg = buffer_get_string(&b, &alen);
437 pkalg = packet_get_string(&alen);
438 pkblob = packet_get_string(&blen);
440 pktype = key_type_from_name(pkalg);
441 if (pktype == KEY_UNSPEC) {
442 /* this is perfectly legal */
443 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
448 key = key_from_blob(pkblob, blen);
451 sig = packet_get_string(&slen);
454 if (datafellows & SSH_OLD_SESSIONID) {
455 buffer_append(&b, session_id2, session_id2_len);
457 buffer_put_string(&b, session_id2, session_id2_len);
459 /* reconstruct packet */
460 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
461 buffer_put_cstring(&b, authctxt->user);
462 buffer_put_cstring(&b,
463 datafellows & SSH_BUG_PKSERVICE ?
466 if (datafellows & SSH_BUG_PKAUTH) {
467 buffer_put_char(&b, have_sig);
469 buffer_put_cstring(&b, "publickey");
470 buffer_put_char(&b, have_sig);
471 buffer_put_cstring(&b, pkalg);
473 buffer_put_string(&b, pkblob, blen);
477 /* test for correct signature */
478 if (user_key_allowed(authctxt->pw, key) &&
479 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
484 debug("test whether pkalg/pkblob are acceptable");
487 /* XXX fake reply and always send PK_OK ? */
489 * XXX this allows testing whether a user is allowed
490 * to login: if you happen to have a valid pubkey this
491 * message is sent. the message is NEVER sent at all
492 * if a user is not allowed to login. is this an
495 if (user_key_allowed(authctxt->pw, key)) {
496 packet_start(SSH2_MSG_USERAUTH_PK_OK);
497 packet_put_string(pkalg, alen);
498 packet_put_string(pkblob, blen);
501 authctxt->postponed = 1;
504 if (authenticated != 1)
505 auth_clear_options();
508 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
512 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
515 return authenticated;
519 userauth_hostbased(Authctxt *authctxt)
523 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
524 u_int alen, blen, slen;
526 int authenticated = 0;
528 if (!authctxt->valid) {
529 debug2("userauth_hostbased: disabled because of invalid user");
532 pkalg = packet_get_string(&alen);
533 pkblob = packet_get_string(&blen);
534 chost = packet_get_string(NULL);
535 cuser = packet_get_string(NULL);
536 sig = packet_get_string(&slen);
538 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
539 cuser, chost, pkalg, slen);
543 buffer_append(&b, sig, slen);
547 pktype = key_type_from_name(pkalg);
548 if (pktype == KEY_UNSPEC) {
549 /* this is perfectly legal */
550 log("userauth_hostbased: unsupported "
551 "public key algorithm: %s", pkalg);
554 key = key_from_blob(pkblob, blen);
556 debug("userauth_hostbased: cannot decode key: %s", pkalg);
559 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
562 buffer_put_string(&b, session_id2, session_id2_len);
563 /* reconstruct packet */
564 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
565 buffer_put_cstring(&b, authctxt->user);
566 buffer_put_cstring(&b, service);
567 buffer_put_cstring(&b, "hostbased");
568 buffer_put_string(&b, pkalg, alen);
569 buffer_put_string(&b, pkblob, blen);
570 buffer_put_cstring(&b, chost);
571 buffer_put_cstring(&b, cuser);
575 /* test for allowed key and correct signature */
576 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
577 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
584 debug2("userauth_hostbased: authenticated %d", authenticated);
590 return authenticated;
593 /* get current user */
598 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
604 authmethods_get(void)
606 Authmethod *method = NULL;
610 for (method = authmethods; method->name != NULL; method++) {
611 if (strcmp(method->name, "none") == 0)
613 if (method->enabled != NULL && *(method->enabled) != 0) {
615 size += strlen(DELIM);
616 size += strlen(method->name);
619 size++; /* trailing '\0' */
620 list = xmalloc(size);
623 for (method = authmethods; method->name != NULL; method++) {
624 if (strcmp(method->name, "none") == 0)
626 if (method->enabled != NULL && *(method->enabled) != 0) {
628 strlcat(list, DELIM, size);
629 strlcat(list, method->name, size);
636 authmethod_lookup(const char *name)
638 Authmethod *method = NULL;
640 for (method = authmethods; method->name != NULL; method++)
641 if (method->enabled != NULL &&
642 *(method->enabled) != 0 &&
643 strcmp(name, method->name) == 0)
645 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
649 /* return 1 if user allows given key */
651 user_key_allowed2(struct passwd *pw, Key *key, char *file)
663 /* Temporarily use the user's uid. */
664 temporarily_use_uid(pw);
666 debug("trying public key file %s", file);
668 /* Fail quietly if file does not exist */
669 if (stat(file, &st) < 0) {
670 /* Restore the privileged uid. */
674 /* Open the file containing the authorized keys. */
675 f = fopen(file, "r");
677 /* Restore the privileged uid. */
681 if (options.strict_modes &&
682 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
684 log("Authentication refused: %s", line);
690 found = key_new(key->type);
692 while (fgets(line, sizeof(line), f)) {
693 char *cp, *options = NULL;
695 /* Skip leading whitespace, empty and comment lines. */
696 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
698 if (!*cp || *cp == '\n' || *cp == '#')
701 if (key_read(found, &cp) == -1) {
702 /* no key? check if there are options for this key */
704 debug2("user_key_allowed: check options: '%s'", cp);
706 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
707 if (*cp == '\\' && cp[1] == '"')
708 cp++; /* Skip both */
712 /* Skip remaining whitespace. */
713 for (; *cp == ' ' || *cp == '\t'; cp++)
715 if (key_read(found, &cp) == -1) {
716 debug2("user_key_allowed: advance: '%s'", cp);
717 /* still no key? advance to next line*/
721 if (key_equal(found, key) &&
722 auth_parse_options(pw, options, file, linenum) == 1) {
724 debug("matching key found: file %s, line %lu",
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 int host_status, len;
765 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
766 ipaddr = get_remote_ipaddr();
768 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
769 chost, resolvedname, ipaddr);
771 if (options.hostbased_uses_name_from_packet_only) {
772 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
776 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
777 debug2("stripping trailing dot from chost %s", chost);
778 chost[len - 1] = '\0';
780 if (strcasecmp(resolvedname, chost) != 0)
781 log("userauth_hostbased mismatch: "
782 "client sends %s, but we resolve %s to %s",
783 chost, ipaddr, resolvedname);
784 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
786 lookup = resolvedname;
788 debug2("userauth_hostbased: access allowed by auth_rhosts2");
790 host_status = check_key_in_hostfiles(pw, key, lookup,
791 _PATH_SSH_SYSTEM_HOSTFILE,
792 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
794 /* backward compat if no key has been found. */
795 if (host_status == HOST_NEW)
796 host_status = check_key_in_hostfiles(pw, key, lookup,
797 _PATH_SSH_SYSTEM_HOSTFILE2,
798 options.ignore_user_known_hosts ? NULL :
799 _PATH_SSH_USER_HOSTFILE2);
801 return (host_status == HOST_OK);