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.85 2002/02/24 19:14:59 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, u_int32_t, void *);
77 static void input_userauth_request(int, u_int32_t, void *);
80 static Authmethod *authmethod_lookup(const char *);
81 static char *authmethods_get(void);
82 static int user_key_allowed(struct passwd *, Key *);
83 static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
86 static void userauth_banner(void);
87 static int userauth_none(Authctxt *);
88 static int userauth_passwd(Authctxt *);
89 static int userauth_pubkey(Authctxt *);
90 static int userauth_hostbased(Authctxt *);
91 static int userauth_kbdint(Authctxt *);
94 int userauth_external(Authctxt *authctxt);
95 int userauth_gssapi(Authctxt *authctxt);
98 Authmethod authmethods[] = {
105 &options.gss_authentication},
108 &options.gss_authentication},
112 &options.pubkey_authentication},
115 &options.password_authentication},
116 {"keyboard-interactive",
118 &options.kbd_interactive_authentication},
121 &options.hostbased_authentication},
126 * loop until authctxt->success == TRUE
130 do_authentication2(void)
132 Authctxt *authctxt = authctxt_new();
134 x_authctxt = authctxt; /*XXX*/
136 /* challenge-response is implemented via keyboard interactive */
137 if (options.challenge_response_authentication)
138 options.kbd_interactive_authentication = 1;
139 if (options.pam_authentication_via_kbd_int)
140 options.kbd_interactive_authentication = 1;
142 dispatch_init(&dispatch_protocol_error);
143 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
144 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
145 do_authenticated(authctxt);
149 input_service_request(int type, u_int32_t seq, void *ctxt)
151 Authctxt *authctxt = ctxt;
154 char *service = packet_get_string(&len);
157 if (authctxt == NULL)
158 fatal("input_service_request: no authctxt");
160 if (strcmp(service, "ssh-userauth") == 0) {
161 if (!authctxt->success) {
163 /* now we can handle user-auth requests */
164 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
167 /* XXX all other service requests are denied */
170 packet_start(SSH2_MSG_SERVICE_ACCEPT);
171 packet_put_cstring(service);
175 debug("bad service request %s", service);
176 packet_disconnect("bad service request %s", service);
182 input_userauth_request(int type, u_int32_t seq, void *ctxt)
184 Authctxt *authctxt = ctxt;
185 Authmethod *m = NULL;
186 char *user, *service, *method, *style = NULL;
187 int authenticated = 0;
189 if (authctxt == NULL)
190 fatal("input_userauth_request: no authctxt");
192 user = packet_get_string(NULL);
193 service = packet_get_string(NULL);
194 method = packet_get_string(NULL);
195 debug("userauth-request for user %s service %s method %s", user, service, method);
196 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
198 if ((style = strchr(user, ':')) != NULL)
201 if (authctxt->attempt++ == 0) {
202 /* setup auth context */
203 struct passwd *pw = NULL;
205 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
206 authctxt->pw = pwcopy(pw);
208 debug2("input_userauth_request: setting up authctxt for %s", user);
210 start_pam(pw->pw_name);
213 log("input_userauth_request: illegal user %s", user);
218 setproctitle("%s", pw ? user : "unknown");
219 authctxt->user = xstrdup(user);
220 authctxt->service = xstrdup(service);
221 authctxt->style = style ? xstrdup(style) : NULL;
222 } else if (strcmp(user, authctxt->user) != 0 ||
223 strcmp(service, authctxt->service) != 0) {
224 packet_disconnect("Change of username or service not allowed: "
225 "(%s,%s) -> (%s,%s)",
226 authctxt->user, authctxt->service, user, service);
230 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &protocol_error);
231 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, &protocol_error);
233 auth2_challenge_stop(authctxt);
234 authctxt->postponed = 0;
236 /* try to authenticate user */
237 m = authmethod_lookup(method);
239 debug2("input_userauth_request: try method %s", method);
240 authenticated = m->userauth(authctxt);
242 userauth_finish(authctxt, authenticated, method);
250 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
254 if (!authctxt->valid && authenticated)
255 fatal("INTERNAL ERROR: authenticated invalid user %s",
258 /* Special handling for root */
259 if (authenticated && authctxt->pw->pw_uid == 0 &&
260 !auth_root_allowed(method))
264 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
269 /* Log before sending the reply */
270 auth_log(authctxt, authenticated, method, " ssh2");
272 if (authctxt->postponed)
275 /* XXX todo: check if multiple auth methods are needed */
276 if (authenticated == 1) {
277 /* turn off userauth */
278 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
279 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
282 /* now we can break out */
283 authctxt->success = 1;
285 if (authctxt->failures++ > AUTH_FAIL_MAX) {
286 #ifdef WITH_AIXAUTHENTICATE
287 loginfailed(authctxt->user,
288 get_canonical_hostname(options.verify_reverse_mapping),
290 #endif /* WITH_AIXAUTHENTICATE */
291 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
293 methods = authmethods_get();
294 packet_start(SSH2_MSG_USERAUTH_FAILURE);
295 packet_put_cstring(methods);
296 packet_put_char(0); /* XXX partial success, unused */
304 userauth_banner(void)
311 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
313 if ((fd = open(options.banner, O_RDONLY)) < 0)
315 if (fstat(fd, &st) < 0)
318 banner = xmalloc(len + 1);
319 if ((n = read(fd, banner, len)) < 0)
322 packet_start(SSH2_MSG_USERAUTH_BANNER);
323 packet_put_cstring(banner);
324 packet_put_cstring(""); /* language, unused */
326 debug("userauth_banner: sent");
335 userauth_none(Authctxt *authctxt)
337 /* disable method "none", only allowed one time */
338 Authmethod *m = authmethod_lookup("none");
344 if (authctxt->valid == 0)
348 if (check_nt_auth(1, authctxt->pw) == 0)
352 return auth_pam_password(authctxt->pw, "");
353 #elif defined(HAVE_OSF_SIA)
355 #else /* !HAVE_OSF_SIA && !USE_PAM */
356 return auth_password(authctxt, "");
361 userauth_passwd(Authctxt *authctxt)
364 int authenticated = 0;
367 change = packet_get_char();
369 log("password change not supported");
370 password = packet_get_string(&len);
372 if (authctxt->valid &&
374 check_nt_auth(1, authctxt->pw) &&
377 auth_pam_password(authctxt->pw, password) == 1)
378 #elif defined(HAVE_OSF_SIA)
379 auth_sia_password(authctxt->user, password) == 1)
380 #else /* !USE_PAM && !HAVE_OSF_SIA */
381 auth_password(authctxt, password) == 1)
384 memset(password, 0, len);
386 return authenticated;
390 userauth_kbdint(Authctxt *authctxt)
392 int authenticated = 0;
395 lang = packet_get_string(NULL);
396 devs = packet_get_string(NULL);
399 debug("keyboard-interactive devs %s", devs);
401 if (options.challenge_response_authentication)
402 authenticated = auth2_challenge(authctxt, devs);
405 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
406 authenticated = auth2_pam(authctxt);
411 if (check_nt_auth(0, authctxt->pw) == 0)
414 return authenticated;
418 userauth_pubkey(Authctxt *authctxt)
423 u_char *pkblob, *sig;
424 u_int alen, blen, slen;
425 int have_sig, pktype;
426 int authenticated = 0;
428 if (!authctxt->valid) {
429 debug2("userauth_pubkey: disabled because of invalid user");
432 have_sig = packet_get_char();
433 if (datafellows & SSH_BUG_PKAUTH) {
434 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
435 /* no explicit pkalg given */
436 pkblob = packet_get_string(&blen);
438 buffer_append(&b, pkblob, blen);
439 /* so we have to extract the pkalg from the pkblob */
440 pkalg = buffer_get_string(&b, &alen);
443 pkalg = packet_get_string(&alen);
444 pkblob = packet_get_string(&blen);
446 pktype = key_type_from_name(pkalg);
447 if (pktype == KEY_UNSPEC) {
448 /* this is perfectly legal */
449 log("userauth_pubkey: unsupported public key algorithm: %s",
453 key = key_from_blob(pkblob, blen);
455 error("userauth_pubkey: cannot decode key: %s", pkalg);
458 if (key->type != pktype) {
459 error("userauth_pubkey: type mismatch for decoded key "
460 "(received %d, expected %d)", key->type, pktype);
464 sig = packet_get_string(&slen);
467 if (datafellows & SSH_OLD_SESSIONID) {
468 buffer_append(&b, session_id2, session_id2_len);
470 buffer_put_string(&b, session_id2, session_id2_len);
472 /* reconstruct packet */
473 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
474 buffer_put_cstring(&b, authctxt->user);
475 buffer_put_cstring(&b,
476 datafellows & SSH_BUG_PKSERVICE ?
479 if (datafellows & SSH_BUG_PKAUTH) {
480 buffer_put_char(&b, have_sig);
482 buffer_put_cstring(&b, "publickey");
483 buffer_put_char(&b, have_sig);
484 buffer_put_cstring(&b, pkalg);
486 buffer_put_string(&b, pkblob, blen);
490 /* test for correct signature */
491 if (user_key_allowed(authctxt->pw, key) &&
492 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
497 debug("test whether pkalg/pkblob are acceptable");
500 /* XXX fake reply and always send PK_OK ? */
502 * XXX this allows testing whether a user is allowed
503 * to login: if you happen to have a valid pubkey this
504 * message is sent. the message is NEVER sent at all
505 * if a user is not allowed to login. is this an
508 if (user_key_allowed(authctxt->pw, key)) {
509 packet_start(SSH2_MSG_USERAUTH_PK_OK);
510 packet_put_string(pkalg, alen);
511 packet_put_string(pkblob, blen);
514 authctxt->postponed = 1;
517 if (authenticated != 1)
518 auth_clear_options();
520 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
526 if (check_nt_auth(0, authctxt->pw) == 0)
529 return authenticated;
533 userauth_hostbased(Authctxt *authctxt)
537 char *pkalg, *cuser, *chost, *service;
538 u_char *pkblob, *sig;
539 u_int alen, blen, slen;
541 int authenticated = 0;
543 if (!authctxt->valid) {
544 debug2("userauth_hostbased: disabled because of invalid user");
547 pkalg = packet_get_string(&alen);
548 pkblob = packet_get_string(&blen);
549 chost = packet_get_string(NULL);
550 cuser = packet_get_string(NULL);
551 sig = packet_get_string(&slen);
553 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
554 cuser, chost, pkalg, slen);
558 buffer_append(&b, sig, slen);
562 pktype = key_type_from_name(pkalg);
563 if (pktype == KEY_UNSPEC) {
564 /* this is perfectly legal */
565 log("userauth_hostbased: unsupported "
566 "public key algorithm: %s", pkalg);
569 key = key_from_blob(pkblob, blen);
571 error("userauth_hostbased: cannot decode key: %s", pkalg);
574 if (key->type != pktype) {
575 error("userauth_hostbased: type mismatch for decoded key "
576 "(received %d, expected %d)", key->type, pktype);
579 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
582 buffer_put_string(&b, session_id2, session_id2_len);
583 /* reconstruct packet */
584 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
585 buffer_put_cstring(&b, authctxt->user);
586 buffer_put_cstring(&b, service);
587 buffer_put_cstring(&b, "hostbased");
588 buffer_put_string(&b, pkalg, alen);
589 buffer_put_string(&b, pkblob, blen);
590 buffer_put_cstring(&b, chost);
591 buffer_put_cstring(&b, cuser);
595 /* test for allowed key and correct signature */
596 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
597 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
602 debug2("userauth_hostbased: authenticated %d", authenticated);
610 return authenticated;
613 /* get current user */
618 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
624 authmethods_get(void)
626 Authmethod *method = NULL;
630 for (method = authmethods; method->name != NULL; method++) {
631 if (strcmp(method->name, "none") == 0)
633 if (method->enabled != NULL && *(method->enabled) != 0) {
635 size += strlen(DELIM);
636 size += strlen(method->name);
639 size++; /* trailing '\0' */
640 list = xmalloc(size);
644 for (method = authmethods; method->name != NULL; method++) {
645 if (strcmp(method->name, "none") == 0)
647 if (method->enabled != NULL && *(method->enabled) != 0) {
648 if (buffer_len(&b) > 0)
649 buffer_append(&b, ",", 1);
650 buffer_append(&b, method->name, strlen(method->name));
653 buffer_append(&b, "\0", 1);
654 list = xstrdup(buffer_ptr(&b));
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)
688 /* Temporarily use the user's uid. */
689 temporarily_use_uid(pw);
691 debug("trying public key file %s", file);
693 /* Fail quietly if file does not exist */
694 if (stat(file, &st) < 0) {
695 /* Restore the privileged uid. */
699 /* Open the file containing the authorized keys. */
700 f = fopen(file, "r");
702 /* Restore the privileged uid. */
706 if (options.strict_modes &&
707 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
709 log("Authentication refused: %s", line);
715 found = key_new(key->type);
717 while (fgets(line, sizeof(line), f)) {
718 char *cp, *options = NULL;
720 /* Skip leading whitespace, empty and comment lines. */
721 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
723 if (!*cp || *cp == '\n' || *cp == '#')
726 if (key_read(found, &cp) != 1) {
727 /* no key? check if there are options for this key */
729 debug2("user_key_allowed: check options: '%s'", cp);
731 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
732 if (*cp == '\\' && cp[1] == '"')
733 cp++; /* Skip both */
737 /* Skip remaining whitespace. */
738 for (; *cp == ' ' || *cp == '\t'; cp++)
740 if (key_read(found, &cp) != 1) {
741 debug2("user_key_allowed: advance: '%s'", cp);
742 /* still no key? advance to next line*/
746 if (key_equal(found, key) &&
747 auth_parse_options(pw, options, file, linenum) == 1) {
749 debug("matching key found: file %s, line %lu",
751 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
752 verbose("Found matching %s key: %s",
753 key_type(found), fp);
762 debug2("key not found");
766 /* check whether given key is in .ssh/authorized_keys* */
768 user_key_allowed(struct passwd *pw, Key *key)
773 file = authorized_keys_file(pw);
774 success = user_key_allowed2(pw, key, file);
779 /* try suffix "2" for backward compat, too */
780 file = authorized_keys_file2(pw);
781 success = user_key_allowed2(pw, key, file);
786 /* return 1 if given hostkey is allowed */
788 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
791 const char *resolvedname, *ipaddr, *lookup;
792 HostStatus host_status;
795 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
796 ipaddr = get_remote_ipaddr();
798 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
799 chost, resolvedname, ipaddr);
801 if (options.hostbased_uses_name_from_packet_only) {
802 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
806 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
807 debug2("stripping trailing dot from chost %s", chost);
808 chost[len - 1] = '\0';
810 if (strcasecmp(resolvedname, chost) != 0)
811 log("userauth_hostbased mismatch: "
812 "client sends %s, but we resolve %s to %s",
813 chost, ipaddr, resolvedname);
814 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
816 lookup = resolvedname;
818 debug2("userauth_hostbased: access allowed by auth_rhosts2");
820 host_status = check_key_in_hostfiles(pw, key, lookup,
821 _PATH_SSH_SYSTEM_HOSTFILE,
822 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
824 /* backward compat if no key has been found. */
825 if (host_status == HOST_NEW)
826 host_status = check_key_in_hostfiles(pw, key, lookup,
827 _PATH_SSH_SYSTEM_HOSTFILE2,
828 options.ignore_user_known_hosts ? NULL :
829 _PATH_SSH_USER_HOSTFILE2);
831 return (host_status == HOST_OK);