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.91 2002/05/13 02:37:39 itojun Exp $");
28 #include <openssl/evp.h>
47 #include "pathnames.h"
49 #include "auth-options.h"
53 #include "monitor_wrap.h"
57 extern ServerOptions options;
58 extern u_char *session_id2;
59 extern int session_id2_len;
61 Authctxt *x_authctxt = NULL;
64 typedef struct Authmethod Authmethod;
67 int (*userauth)(Authctxt *authctxt);
73 static void input_service_request(int, u_int32_t, void *);
74 static void input_userauth_request(int, u_int32_t, void *);
77 static Authmethod *authmethod_lookup(const char *);
78 static char *authmethods_get(void);
79 int user_key_allowed(struct passwd *, Key *);
80 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 options.pam_authentication_via_kbd_int = 0;
128 dispatch_init(&dispatch_protocol_error);
129 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
130 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
136 input_service_request(int type, u_int32_t seq, void *ctxt)
138 Authctxt *authctxt = ctxt;
141 char *service = packet_get_string(&len);
144 if (authctxt == NULL)
145 fatal("input_service_request: no authctxt");
147 if (strcmp(service, "ssh-userauth") == 0) {
148 if (!authctxt->success) {
150 /* now we can handle user-auth requests */
151 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
154 /* XXX all other service requests are denied */
157 packet_start(SSH2_MSG_SERVICE_ACCEPT);
158 packet_put_cstring(service);
162 debug("bad service request %s", service);
163 packet_disconnect("bad service request %s", service);
169 input_userauth_request(int type, u_int32_t seq, void *ctxt)
171 Authctxt *authctxt = ctxt;
172 Authmethod *m = NULL;
173 char *user, *service, *method, *style = NULL;
174 int authenticated = 0;
176 if (authctxt == NULL)
177 fatal("input_userauth_request: no authctxt");
179 user = packet_get_string(NULL);
180 service = packet_get_string(NULL);
181 method = packet_get_string(NULL);
182 debug("userauth-request for user %s service %s method %s", user, service, method);
183 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
185 if ((style = strchr(user, ':')) != NULL)
188 if (authctxt->attempt++ == 0) {
189 /* setup auth context */
190 authctxt->pw = PRIVSEP(getpwnamallow(user));
191 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
193 debug2("input_userauth_request: setting up authctxt for %s", user);
195 PRIVSEP(start_pam(authctxt->pw->pw_name));
198 log("input_userauth_request: illegal user %s", user);
200 PRIVSEP(start_pam("NOUSER"));
203 setproctitle("%s%s", authctxt->pw ? user : "unknown",
204 use_privsep ? " [net]" : "");
205 authctxt->user = xstrdup(user);
206 authctxt->service = xstrdup(service);
207 authctxt->style = style ? xstrdup(style) : NULL;
209 mm_inform_authserv(service, style);
210 } else if (strcmp(user, authctxt->user) != 0 ||
211 strcmp(service, authctxt->service) != 0) {
212 packet_disconnect("Change of username or service not allowed: "
213 "(%s,%s) -> (%s,%s)",
214 authctxt->user, authctxt->service, user, service);
217 auth2_challenge_stop(authctxt);
218 authctxt->postponed = 0;
220 /* try to authenticate user */
221 m = authmethod_lookup(method);
223 debug2("input_userauth_request: try method %s", method);
224 authenticated = m->userauth(authctxt);
226 userauth_finish(authctxt, authenticated, method);
234 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
238 if (!authctxt->valid && authenticated)
239 fatal("INTERNAL ERROR: authenticated invalid user %s",
242 /* Special handling for root */
243 if (authenticated && authctxt->pw->pw_uid == 0 &&
244 !auth_root_allowed(method))
248 if (!use_privsep && authenticated && authctxt->user &&
249 !do_pam_account(authctxt->user, NULL))
253 /* Log before sending the reply */
254 auth_log(authctxt, authenticated, method, " ssh2");
256 if (authctxt->postponed)
259 /* XXX todo: check if multiple auth methods are needed */
260 if (authenticated == 1) {
261 /* turn off userauth */
262 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
263 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
266 /* now we can break out */
267 authctxt->success = 1;
269 if (authctxt->failures++ > AUTH_FAIL_MAX) {
270 #ifdef WITH_AIXAUTHENTICATE
271 loginfailed(authctxt->user,
272 get_canonical_hostname(options.verify_reverse_mapping),
274 #endif /* WITH_AIXAUTHENTICATE */
275 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
277 methods = authmethods_get();
278 packet_start(SSH2_MSG_USERAUTH_FAILURE);
279 packet_put_cstring(methods);
280 packet_put_char(0); /* XXX partial success, unused */
288 auth2_read_banner(void)
295 if ((fd = open(options.banner, O_RDONLY)) == -1)
297 if (fstat(fd, &st) == -1) {
302 banner = xmalloc(len + 1);
303 n = atomicio(read, fd, banner, len);
316 userauth_banner(void)
320 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
323 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
326 packet_start(SSH2_MSG_USERAUTH_BANNER);
327 packet_put_cstring(banner);
328 packet_put_cstring(""); /* language, unused */
330 debug("userauth_banner: sent");
338 userauth_none(Authctxt *authctxt)
340 /* disable method "none", only allowed one time */
341 Authmethod *m = authmethod_lookup("none");
347 if (authctxt->valid == 0)
351 if (check_nt_auth(1, authctxt->pw) == 0)
354 return PRIVSEP(auth_password(authctxt, ""));
358 userauth_passwd(Authctxt *authctxt)
361 int authenticated = 0;
364 change = packet_get_char();
366 log("password change not supported");
367 password = packet_get_string(&len);
369 if (authctxt->valid &&
371 check_nt_auth(1, authctxt->pw) &&
373 PRIVSEP(auth_password(authctxt, password)) == 1)
375 memset(password, 0, len);
377 return authenticated;
381 userauth_kbdint(Authctxt *authctxt)
383 int authenticated = 0;
386 lang = packet_get_string(NULL);
387 devs = packet_get_string(NULL);
390 debug("keyboard-interactive devs %s", devs);
392 if (options.challenge_response_authentication)
393 authenticated = auth2_challenge(authctxt, devs);
396 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
397 authenticated = auth2_pam(authctxt);
402 if (check_nt_auth(0, authctxt->pw) == 0)
405 return authenticated;
409 userauth_pubkey(Authctxt *authctxt)
414 u_char *pkblob, *sig;
415 u_int alen, blen, slen;
416 int have_sig, pktype;
417 int authenticated = 0;
419 if (!authctxt->valid) {
420 debug2("userauth_pubkey: disabled because of invalid user");
423 have_sig = packet_get_char();
424 if (datafellows & SSH_BUG_PKAUTH) {
425 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
426 /* no explicit pkalg given */
427 pkblob = packet_get_string(&blen);
429 buffer_append(&b, pkblob, blen);
430 /* so we have to extract the pkalg from the pkblob */
431 pkalg = buffer_get_string(&b, &alen);
434 pkalg = packet_get_string(&alen);
435 pkblob = packet_get_string(&blen);
437 pktype = key_type_from_name(pkalg);
438 if (pktype == KEY_UNSPEC) {
439 /* this is perfectly legal */
440 log("userauth_pubkey: unsupported public key algorithm: %s",
444 key = key_from_blob(pkblob, blen);
446 error("userauth_pubkey: cannot decode key: %s", pkalg);
449 if (key->type != pktype) {
450 error("userauth_pubkey: type mismatch for decoded key "
451 "(received %d, expected %d)", key->type, pktype);
455 sig = packet_get_string(&slen);
458 if (datafellows & SSH_OLD_SESSIONID) {
459 buffer_append(&b, session_id2, session_id2_len);
461 buffer_put_string(&b, session_id2, session_id2_len);
463 /* reconstruct packet */
464 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
465 buffer_put_cstring(&b, authctxt->user);
466 buffer_put_cstring(&b,
467 datafellows & SSH_BUG_PKSERVICE ?
470 if (datafellows & SSH_BUG_PKAUTH) {
471 buffer_put_char(&b, have_sig);
473 buffer_put_cstring(&b, "publickey");
474 buffer_put_char(&b, have_sig);
475 buffer_put_cstring(&b, pkalg);
477 buffer_put_string(&b, pkblob, blen);
481 /* test for correct signature */
483 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
484 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
485 buffer_len(&b))) == 1)
490 debug("test whether pkalg/pkblob are acceptable");
493 /* XXX fake reply and always send PK_OK ? */
495 * XXX this allows testing whether a user is allowed
496 * to login: if you happen to have a valid pubkey this
497 * message is sent. the message is NEVER sent at all
498 * if a user is not allowed to login. is this an
501 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
502 packet_start(SSH2_MSG_USERAUTH_PK_OK);
503 packet_put_string(pkalg, alen);
504 packet_put_string(pkblob, blen);
507 authctxt->postponed = 1;
510 if (authenticated != 1)
511 auth_clear_options();
513 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
519 if (check_nt_auth(0, authctxt->pw) == 0)
522 return authenticated;
526 userauth_hostbased(Authctxt *authctxt)
530 char *pkalg, *cuser, *chost, *service;
531 u_char *pkblob, *sig;
532 u_int alen, blen, slen;
534 int authenticated = 0;
536 if (!authctxt->valid) {
537 debug2("userauth_hostbased: disabled because of invalid user");
540 pkalg = packet_get_string(&alen);
541 pkblob = packet_get_string(&blen);
542 chost = packet_get_string(NULL);
543 cuser = packet_get_string(NULL);
544 sig = packet_get_string(&slen);
546 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
547 cuser, chost, pkalg, slen);
551 buffer_append(&b, sig, slen);
555 pktype = key_type_from_name(pkalg);
556 if (pktype == KEY_UNSPEC) {
557 /* this is perfectly legal */
558 log("userauth_hostbased: unsupported "
559 "public key algorithm: %s", pkalg);
562 key = key_from_blob(pkblob, blen);
564 error("userauth_hostbased: cannot decode key: %s", pkalg);
567 if (key->type != pktype) {
568 error("userauth_hostbased: type mismatch for decoded key "
569 "(received %d, expected %d)", key->type, pktype);
572 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
575 buffer_put_string(&b, session_id2, session_id2_len);
576 /* reconstruct packet */
577 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
578 buffer_put_cstring(&b, authctxt->user);
579 buffer_put_cstring(&b, service);
580 buffer_put_cstring(&b, "hostbased");
581 buffer_put_string(&b, pkalg, alen);
582 buffer_put_string(&b, pkblob, blen);
583 buffer_put_cstring(&b, chost);
584 buffer_put_cstring(&b, cuser);
588 /* test for allowed key and correct signature */
590 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
591 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
592 buffer_len(&b))) == 1)
597 debug2("userauth_hostbased: authenticated %d", authenticated);
605 return authenticated;
608 /* get current user */
613 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
619 authmethods_get(void)
621 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) {
630 if (buffer_len(&b) > 0)
631 buffer_append(&b, ",", 1);
632 buffer_append(&b, method->name, strlen(method->name));
635 buffer_append(&b, "\0", 1);
636 list = xstrdup(buffer_ptr(&b));
642 authmethod_lookup(const char *name)
644 Authmethod *method = NULL;
646 for (method = authmethods; method->name != NULL; method++)
647 if (method->enabled != NULL &&
648 *(method->enabled) != 0 &&
649 strcmp(name, method->name) == 0)
651 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
655 /* return 1 if user allows given key */
657 user_key_allowed2(struct passwd *pw, Key *key, char *file)
670 /* Temporarily use the user's uid. */
671 temporarily_use_uid(pw);
673 debug("trying public key file %s", file);
675 /* Fail quietly if file does not exist */
676 if (stat(file, &st) < 0) {
677 /* Restore the privileged uid. */
681 /* Open the file containing the authorized keys. */
682 f = fopen(file, "r");
684 /* Restore the privileged uid. */
688 if (options.strict_modes &&
689 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
691 log("Authentication refused: %s", line);
697 found = key_new(key->type);
699 while (fgets(line, sizeof(line), f)) {
700 char *cp, *options = NULL;
702 /* Skip leading whitespace, empty and comment lines. */
703 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
705 if (!*cp || *cp == '\n' || *cp == '#')
708 if (key_read(found, &cp) != 1) {
709 /* no key? check if there are options for this key */
711 debug2("user_key_allowed: check options: '%s'", cp);
713 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
714 if (*cp == '\\' && cp[1] == '"')
715 cp++; /* Skip both */
719 /* Skip remaining whitespace. */
720 for (; *cp == ' ' || *cp == '\t'; cp++)
722 if (key_read(found, &cp) != 1) {
723 debug2("user_key_allowed: advance: '%s'", cp);
724 /* still no key? advance to next line*/
728 if (key_equal(found, key) &&
729 auth_parse_options(pw, options, file, linenum) == 1) {
731 debug("matching key found: file %s, line %lu",
733 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
734 verbose("Found matching %s key: %s",
735 key_type(found), fp);
744 debug2("key not found");
748 /* check whether given key is in .ssh/authorized_keys* */
750 user_key_allowed(struct passwd *pw, Key *key)
755 file = authorized_keys_file(pw);
756 success = user_key_allowed2(pw, key, file);
761 /* try suffix "2" for backward compat, too */
762 file = authorized_keys_file2(pw);
763 success = user_key_allowed2(pw, key, file);
768 /* return 1 if given hostkey is allowed */
770 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
773 const char *resolvedname, *ipaddr, *lookup;
774 HostStatus host_status;
777 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
778 ipaddr = get_remote_ipaddr();
780 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
781 chost, resolvedname, ipaddr);
783 if (options.hostbased_uses_name_from_packet_only) {
784 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
788 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
789 debug2("stripping trailing dot from chost %s", chost);
790 chost[len - 1] = '\0';
792 if (strcasecmp(resolvedname, chost) != 0)
793 log("userauth_hostbased mismatch: "
794 "client sends %s, but we resolve %s to %s",
795 chost, ipaddr, resolvedname);
796 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
798 lookup = resolvedname;
800 debug2("userauth_hostbased: access allowed by auth_rhosts2");
802 host_status = check_key_in_hostfiles(pw, key, lookup,
803 _PATH_SSH_SYSTEM_HOSTFILE,
804 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
806 /* backward compat if no key has been found. */
807 if (host_status == HOST_NEW)
808 host_status = check_key_in_hostfiles(pw, key, lookup,
809 _PATH_SSH_SYSTEM_HOSTFILE2,
810 options.ignore_user_known_hosts ? NULL :
811 _PATH_SSH_USER_HOSTFILE2);
813 return (host_status == HOST_OK);