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.59 2001/05/30 12:55:06 markus 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 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);
82 Authmethod *authmethod_lookup(const char *name);
83 char *authmethods_get(void);
84 int user_key_allowed(struct passwd *pw, Key *key);
86 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
90 void userauth_banner(void);
91 void userauth_reply(Authctxt *authctxt, int authenticated);
92 int userauth_none(Authctxt *authctxt);
93 int userauth_passwd(Authctxt *authctxt);
94 int userauth_pubkey(Authctxt *authctxt);
95 int userauth_hostbased(Authctxt *authctxt);
96 int userauth_kbdint(Authctxt *authctxt);
98 Authmethod authmethods[] = {
104 &options.pubkey_authentication},
107 &options.password_authentication},
108 {"keyboard-interactive",
110 &options.kbd_interactive_authentication},
113 &options.hostbased_authentication},
118 * loop until authctxt->success == TRUE
124 Authctxt *authctxt = authctxt_new();
126 x_authctxt = authctxt; /*XXX*/
128 /* challenge-reponse is implemented via keyboard interactive */
129 if (options.challenge_response_authentication)
130 options.kbd_interactive_authentication = 1;
131 if (options.pam_authentication_via_kbd_int)
132 options.kbd_interactive_authentication = 1;
134 dispatch_init(&protocol_error);
135 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
136 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
137 do_authenticated(authctxt);
141 protocol_error(int type, int plen, void *ctxt)
143 log("auth: protocol error: type %d plen %d", type, plen);
144 packet_start(SSH2_MSG_UNIMPLEMENTED);
151 input_service_request(int type, int plen, void *ctxt)
153 Authctxt *authctxt = ctxt;
156 char *service = packet_get_string(&len);
159 if (authctxt == NULL)
160 fatal("input_service_request: no authctxt");
162 if (strcmp(service, "ssh-userauth") == 0) {
163 if (!authctxt->success) {
165 /* now we can handle user-auth requests */
166 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
169 /* XXX all other service requests are denied */
172 packet_start(SSH2_MSG_SERVICE_ACCEPT);
173 packet_put_cstring(service);
177 debug("bad service request %s", service);
178 packet_disconnect("bad service request %s", service);
184 input_userauth_request(int type, int plen, void *ctxt)
186 Authctxt *authctxt = ctxt;
187 Authmethod *m = NULL;
188 char *user, *service, *method, *style = NULL;
189 int authenticated = 0;
191 if (authctxt == NULL)
192 fatal("input_userauth_request: no authctxt");
194 user = packet_get_string(NULL);
195 service = packet_get_string(NULL);
196 method = packet_get_string(NULL);
197 debug("userauth-request for user %s service %s method %s", user, service, method);
198 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
200 if ((style = strchr(user, ':')) != NULL)
203 if (authctxt->attempt++ == 0) {
204 /* setup auth context */
205 struct passwd *pw = NULL;
207 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
208 authctxt->pw = pwcopy(pw);
210 debug2("input_userauth_request: setting up authctxt for %s", user);
212 start_pam(pw->pw_name);
215 log("input_userauth_request: illegal user %s", user);
220 setproctitle("%s", pw ? user : "unknown");
221 authctxt->user = xstrdup(user);
222 authctxt->service = xstrdup(service);
223 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
224 } else if (authctxt->valid) {
225 if (strcmp(user, authctxt->user) != 0 ||
226 strcmp(service, authctxt->service) != 0) {
227 log("input_userauth_request: mismatch: (%s,%s)!=(%s,%s)",
228 user, service, authctxt->user, authctxt->service);
233 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
234 authctxt->postponed = 0;
237 auth_close(authctxt->as);
242 /* try to authenticate user */
243 m = authmethod_lookup(method);
245 debug2("input_userauth_request: try method %s", method);
246 authenticated = m->userauth(authctxt);
248 userauth_finish(authctxt, authenticated, method);
256 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
258 if (!authctxt->valid && authenticated)
259 fatal("INTERNAL ERROR: authenticated invalid user %s",
262 /* Special handling for root */
263 if (authenticated && authctxt->pw->pw_uid == 0 &&
264 !auth_root_allowed(method))
268 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
273 /* Log before sending the reply */
274 auth_log(authctxt, authenticated, method, " ssh2");
276 if (!authctxt->postponed)
277 userauth_reply(authctxt, authenticated);
281 userauth_banner(void)
288 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
290 if ((fd = open(options.banner, O_RDONLY)) < 0)
292 if (fstat(fd, &st) < 0)
295 banner = xmalloc(len + 1);
296 if ((n = read(fd, banner, len)) < 0)
299 packet_start(SSH2_MSG_USERAUTH_BANNER);
300 packet_put_cstring(banner);
301 packet_put_cstring(""); /* language, unused */
303 debug("userauth_banner: sent");
312 userauth_reply(Authctxt *authctxt, int authenticated)
316 /* XXX todo: check if multiple auth methods are needed */
317 if (authenticated == 1) {
318 #ifdef WITH_AIXAUTHENTICATE
319 /* We don't have a pty yet, so just label the line as "ssh" */
320 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
321 get_canonical_hostname(options.reverse_mapping_check),
322 "ssh", &aixloginmsg) < 0)
324 #endif /* WITH_AIXAUTHENTICATE */
325 /* turn off userauth */
326 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
327 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
330 /* now we can break out */
331 authctxt->success = 1;
333 if (authctxt->failures++ > AUTH_FAIL_MAX)
334 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
335 methods = authmethods_get();
336 packet_start(SSH2_MSG_USERAUTH_FAILURE);
337 packet_put_cstring(methods);
338 packet_put_char(0); /* XXX partial success, unused */
346 userauth_none(Authctxt *authctxt)
348 /* disable method "none", only allowed one time */
349 Authmethod *m = authmethod_lookup("none");
355 if (authctxt->valid == 0)
359 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
363 return auth_pam_password(authctxt->pw, "");
364 #elif defined(HAVE_OSF_SIA)
366 #else /* !HAVE_OSF_SIA && !USE_PAM */
367 return auth_password(authctxt, "");
372 userauth_passwd(Authctxt *authctxt)
375 int authenticated = 0;
378 change = packet_get_char();
380 log("password change not supported");
381 password = packet_get_string(&len);
383 if (authctxt->valid &&
385 check_nt_auth(1, authctxt->pw->pw_uid) &&
388 auth_pam_password(authctxt->pw, password) == 1)
389 #elif defined(HAVE_OSF_SIA)
390 auth_sia_password(authctxt->user, password) == 1)
391 #else /* !USE_PAM && !HAVE_OSF_SIA */
392 auth_password(authctxt, password) == 1)
395 memset(password, 0, len);
397 return authenticated;
401 userauth_kbdint(Authctxt *authctxt)
403 int authenticated = 0;
406 lang = packet_get_string(NULL);
407 devs = packet_get_string(NULL);
410 debug("keyboard-interactive devs %s", devs);
412 if (options.challenge_response_authentication)
413 authenticated = auth2_challenge(authctxt, devs);
416 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
417 authenticated = auth2_pam(authctxt);
422 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
425 return authenticated;
429 userauth_pubkey(Authctxt *authctxt)
433 char *pkalg, *pkblob, *sig;
434 u_int alen, blen, slen;
435 int have_sig, pktype;
436 int authenticated = 0;
438 if (!authctxt->valid) {
439 debug2("userauth_pubkey: disabled because of invalid user");
442 have_sig = packet_get_char();
443 if (datafellows & SSH_BUG_PKAUTH) {
444 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
445 /* no explicit pkalg given */
446 pkblob = packet_get_string(&blen);
448 buffer_append(&b, pkblob, blen);
449 /* so we have to extract the pkalg from the pkblob */
450 pkalg = buffer_get_string(&b, &alen);
453 pkalg = packet_get_string(&alen);
454 pkblob = packet_get_string(&blen);
456 pktype = key_type_from_name(pkalg);
457 if (pktype == KEY_UNSPEC) {
458 /* this is perfectly legal */
459 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
464 key = key_from_blob(pkblob, blen);
467 sig = packet_get_string(&slen);
470 if (datafellows & SSH_OLD_SESSIONID) {
471 buffer_append(&b, session_id2, session_id2_len);
473 buffer_put_string(&b, session_id2, session_id2_len);
475 /* reconstruct packet */
476 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
477 buffer_put_cstring(&b, authctxt->user);
478 buffer_put_cstring(&b,
479 datafellows & SSH_BUG_PKSERVICE ?
482 if (datafellows & SSH_BUG_PKAUTH) {
483 buffer_put_char(&b, have_sig);
485 buffer_put_cstring(&b, "publickey");
486 buffer_put_char(&b, have_sig);
487 buffer_put_cstring(&b, pkalg);
489 buffer_put_string(&b, pkblob, blen);
493 /* test for correct signature */
494 if (user_key_allowed(authctxt->pw, key) &&
495 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
500 debug("test whether pkalg/pkblob are acceptable");
503 /* XXX fake reply and always send PK_OK ? */
505 * XXX this allows testing whether a user is allowed
506 * to login: if you happen to have a valid pubkey this
507 * message is sent. the message is NEVER sent at all
508 * if a user is not allowed to login. is this an
511 if (user_key_allowed(authctxt->pw, key)) {
512 packet_start(SSH2_MSG_USERAUTH_PK_OK);
513 packet_put_string(pkalg, alen);
514 packet_put_string(pkblob, blen);
517 authctxt->postponed = 1;
520 if (authenticated != 1)
521 auth_clear_options();
524 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
528 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
531 return authenticated;
535 userauth_hostbased(Authctxt *authctxt)
539 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
540 u_int alen, blen, slen;
542 int authenticated = 0;
544 if (!authctxt->valid) {
545 debug2("userauth_hostbased: disabled because of invalid user");
548 pkalg = packet_get_string(&alen);
549 pkblob = packet_get_string(&blen);
550 chost = packet_get_string(NULL);
551 cuser = packet_get_string(NULL);
552 sig = packet_get_string(&slen);
554 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
555 cuser, chost, pkalg, slen);
559 buffer_append(&b, sig, slen);
563 pktype = key_type_from_name(pkalg);
564 if (pktype == KEY_UNSPEC) {
565 /* this is perfectly legal */
566 log("userauth_hostbased: unsupported "
567 "public key algorithm: %s", pkalg);
570 key = key_from_blob(pkblob, blen);
572 debug("userauth_hostbased: cannot decode key: %s", pkalg);
575 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
578 buffer_put_string(&b, session_id2, session_id2_len);
579 /* reconstruct packet */
580 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
581 buffer_put_cstring(&b, authctxt->user);
582 buffer_put_cstring(&b, service);
583 buffer_put_cstring(&b, "hostbased");
584 buffer_put_string(&b, pkalg, alen);
585 buffer_put_string(&b, pkblob, blen);
586 buffer_put_cstring(&b, chost);
587 buffer_put_cstring(&b, cuser);
591 /* test for allowed key and correct signature */
592 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
593 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
600 debug2("userauth_hostbased: authenticated %d", authenticated);
606 return authenticated;
609 /* get current user */
614 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
620 authmethods_get(void)
622 Authmethod *method = NULL;
626 for (method = authmethods; method->name != NULL; method++) {
627 if (strcmp(method->name, "none") == 0)
629 if (method->enabled != NULL && *(method->enabled) != 0) {
631 size += strlen(DELIM);
632 size += strlen(method->name);
635 size++; /* trailing '\0' */
636 list = xmalloc(size);
639 for (method = authmethods; method->name != NULL; method++) {
640 if (strcmp(method->name, "none") == 0)
642 if (method->enabled != NULL && *(method->enabled) != 0) {
644 strlcat(list, DELIM, size);
645 strlcat(list, method->name, size);
652 authmethod_lookup(const char *name)
654 Authmethod *method = NULL;
656 for (method = authmethods; method->name != NULL; method++)
657 if (method->enabled != NULL &&
658 *(method->enabled) != 0 &&
659 strcmp(name, method->name) == 0)
661 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
665 /* return 1 if user allows given key */
667 user_key_allowed(struct passwd *pw, Key *key)
669 char line[8192], *file;
679 /* Temporarily use the user's uid. */
680 temporarily_use_uid(pw);
682 /* The authorized keys. */
683 file = authorized_keys_file2(pw);
684 debug("trying public key file %s", file);
686 /* Fail quietly if file does not exist */
687 if (stat(file, &st) < 0) {
688 /* Restore the privileged uid. */
693 /* Open the file containing the authorized keys. */
694 f = fopen(file, "r");
696 /* Restore the privileged uid. */
701 if (options.strict_modes &&
702 secure_filename(f, file, pw->pw_uid, line, sizeof(line)) != 0) {
705 log("Authentication refused: %s", line);
711 found = key_new(key->type);
713 while (fgets(line, sizeof(line), f)) {
714 char *cp, *options = NULL;
716 /* Skip leading whitespace, empty and comment lines. */
717 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
719 if (!*cp || *cp == '\n' || *cp == '#')
722 if (key_read(found, &cp) == -1) {
723 /* no key? check if there are options for this key */
725 debug2("user_key_allowed: check options: '%s'", cp);
727 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
728 if (*cp == '\\' && cp[1] == '"')
729 cp++; /* Skip both */
733 /* Skip remaining whitespace. */
734 for (; *cp == ' ' || *cp == '\t'; cp++)
736 if (key_read(found, &cp) == -1) {
737 debug2("user_key_allowed: advance: '%s'", cp);
738 /* still no key? advance to next line*/
742 if (key_equal(found, key) &&
743 auth_parse_options(pw, options, file, linenum) == 1) {
745 debug("matching key found: file %s, line %ld",
755 debug2("key not found");
759 /* return 1 if given hostkey is allowed */
761 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
765 const char *resolvedname, *ipaddr, *lookup;
768 int host_status, len;
770 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
771 ipaddr = get_remote_ipaddr();
773 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
774 chost, resolvedname, ipaddr);
776 if (options.hostbased_uses_name_from_packet_only) {
777 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
781 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
782 debug2("stripping trailing dot from chost %s", chost);
783 chost[len - 1] = '\0';
785 if (strcasecmp(resolvedname, chost) != 0)
786 log("userauth_hostbased mismatch: "
787 "client sends %s, but we resolve %s to %s",
788 chost, ipaddr, resolvedname);
789 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
791 lookup = resolvedname;
793 debug2("userauth_hostbased: access allowed by auth_rhosts2");
795 /* XXX this is copied from auth-rh-rsa.c and should be shared */
796 found = key_new(key->type);
797 host_status = check_host_in_hostfile(_PATH_SSH_SYSTEM_HOSTFILE2, lookup,
800 if (host_status != HOST_OK && !options.ignore_user_known_hosts) {
801 user_hostfile = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE2,
803 if (options.strict_modes &&
804 (stat(user_hostfile, &st) == 0) &&
805 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
806 (st.st_mode & 022) != 0)) {
807 log("Hostbased authentication refused for %.100s: "
808 "bad owner or modes for %.200s",
809 pw->pw_name, user_hostfile);
811 temporarily_use_uid(pw);
812 host_status = check_host_in_hostfile(user_hostfile,
813 lookup, key, found, NULL);
816 xfree(user_hostfile);
820 debug2("userauth_hostbased: key %s for %s", host_status == HOST_OK ?
821 "ok" : "not found", lookup);
822 return (host_status == HOST_OK);