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);
229 auth2_challenge_stop(authctxt);
232 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
233 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
236 authctxt->postponed = 0;
238 /* try to authenticate user */
239 m = authmethod_lookup(method);
241 debug2("input_userauth_request: try method %s", method);
242 authenticated = m->userauth(authctxt);
244 userauth_finish(authctxt, authenticated, method);
252 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
256 if (!authctxt->valid && authenticated)
257 fatal("INTERNAL ERROR: authenticated invalid user %s",
260 /* Special handling for root */
261 if (authenticated && authctxt->pw->pw_uid == 0 &&
262 !auth_root_allowed(method))
266 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
271 /* Log before sending the reply */
272 auth_log(authctxt, authenticated, method, " ssh2");
274 if (authctxt->postponed)
277 /* XXX todo: check if multiple auth methods are needed */
278 if (authenticated == 1) {
279 /* turn off userauth */
280 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
281 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
284 /* now we can break out */
285 authctxt->success = 1;
287 if (authctxt->failures++ > AUTH_FAIL_MAX) {
288 #ifdef WITH_AIXAUTHENTICATE
289 loginfailed(authctxt->user,
290 get_canonical_hostname(options.verify_reverse_mapping),
292 #endif /* WITH_AIXAUTHENTICATE */
293 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
295 methods = authmethods_get();
296 packet_start(SSH2_MSG_USERAUTH_FAILURE);
297 packet_put_cstring(methods);
298 packet_put_char(0); /* XXX partial success, unused */
306 userauth_banner(void)
313 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
315 if ((fd = open(options.banner, O_RDONLY)) < 0)
317 if (fstat(fd, &st) < 0)
320 banner = xmalloc(len + 1);
321 if ((n = read(fd, banner, len)) < 0)
324 packet_start(SSH2_MSG_USERAUTH_BANNER);
325 packet_put_cstring(banner);
326 packet_put_cstring(""); /* language, unused */
328 debug("userauth_banner: sent");
337 userauth_none(Authctxt *authctxt)
339 /* disable method "none", only allowed one time */
340 Authmethod *m = authmethod_lookup("none");
346 if (authctxt->valid == 0)
350 if (check_nt_auth(1, authctxt->pw) == 0)
354 return auth_pam_password(authctxt->pw, "");
355 #elif defined(HAVE_OSF_SIA)
357 #else /* !HAVE_OSF_SIA && !USE_PAM */
358 return auth_password(authctxt, "");
363 userauth_passwd(Authctxt *authctxt)
366 int authenticated = 0;
369 change = packet_get_char();
371 log("password change not supported");
372 password = packet_get_string(&len);
374 if (authctxt->valid &&
376 check_nt_auth(1, authctxt->pw) &&
379 auth_pam_password(authctxt->pw, password) == 1)
380 #elif defined(HAVE_OSF_SIA)
381 auth_sia_password(authctxt->user, password) == 1)
382 #else /* !USE_PAM && !HAVE_OSF_SIA */
383 auth_password(authctxt, password) == 1)
386 memset(password, 0, len);
388 return authenticated;
392 userauth_kbdint(Authctxt *authctxt)
394 int authenticated = 0;
397 lang = packet_get_string(NULL);
398 devs = packet_get_string(NULL);
401 debug("keyboard-interactive devs %s", devs);
403 if (options.challenge_response_authentication)
404 authenticated = auth2_challenge(authctxt, devs);
407 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
408 authenticated = auth2_pam(authctxt);
413 if (check_nt_auth(0, authctxt->pw) == 0)
416 return authenticated;
420 userauth_pubkey(Authctxt *authctxt)
425 u_char *pkblob, *sig;
426 u_int alen, blen, slen;
427 int have_sig, pktype;
428 int authenticated = 0;
430 if (!authctxt->valid) {
431 debug2("userauth_pubkey: disabled because of invalid user");
434 have_sig = packet_get_char();
435 if (datafellows & SSH_BUG_PKAUTH) {
436 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
437 /* no explicit pkalg given */
438 pkblob = packet_get_string(&blen);
440 buffer_append(&b, pkblob, blen);
441 /* so we have to extract the pkalg from the pkblob */
442 pkalg = buffer_get_string(&b, &alen);
445 pkalg = packet_get_string(&alen);
446 pkblob = packet_get_string(&blen);
448 pktype = key_type_from_name(pkalg);
449 if (pktype == KEY_UNSPEC) {
450 /* this is perfectly legal */
451 log("userauth_pubkey: unsupported public key algorithm: %s",
455 key = key_from_blob(pkblob, blen);
457 error("userauth_pubkey: cannot decode key: %s", pkalg);
460 if (key->type != pktype) {
461 error("userauth_pubkey: type mismatch for decoded key "
462 "(received %d, expected %d)", key->type, pktype);
466 sig = packet_get_string(&slen);
469 if (datafellows & SSH_OLD_SESSIONID) {
470 buffer_append(&b, session_id2, session_id2_len);
472 buffer_put_string(&b, session_id2, session_id2_len);
474 /* reconstruct packet */
475 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
476 buffer_put_cstring(&b, authctxt->user);
477 buffer_put_cstring(&b,
478 datafellows & SSH_BUG_PKSERVICE ?
481 if (datafellows & SSH_BUG_PKAUTH) {
482 buffer_put_char(&b, have_sig);
484 buffer_put_cstring(&b, "publickey");
485 buffer_put_char(&b, have_sig);
486 buffer_put_cstring(&b, pkalg);
488 buffer_put_string(&b, pkblob, blen);
492 /* test for correct signature */
493 if (user_key_allowed(authctxt->pw, key) &&
494 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
499 debug("test whether pkalg/pkblob are acceptable");
502 /* XXX fake reply and always send PK_OK ? */
504 * XXX this allows testing whether a user is allowed
505 * to login: if you happen to have a valid pubkey this
506 * message is sent. the message is NEVER sent at all
507 * if a user is not allowed to login. is this an
510 if (user_key_allowed(authctxt->pw, key)) {
511 packet_start(SSH2_MSG_USERAUTH_PK_OK);
512 packet_put_string(pkalg, alen);
513 packet_put_string(pkblob, blen);
516 authctxt->postponed = 1;
519 if (authenticated != 1)
520 auth_clear_options();
522 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
528 if (check_nt_auth(0, authctxt->pw) == 0)
531 return authenticated;
535 userauth_hostbased(Authctxt *authctxt)
539 char *pkalg, *cuser, *chost, *service;
540 u_char *pkblob, *sig;
541 u_int alen, blen, slen;
543 int authenticated = 0;
545 if (!authctxt->valid) {
546 debug2("userauth_hostbased: disabled because of invalid user");
549 pkalg = packet_get_string(&alen);
550 pkblob = packet_get_string(&blen);
551 chost = packet_get_string(NULL);
552 cuser = packet_get_string(NULL);
553 sig = packet_get_string(&slen);
555 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
556 cuser, chost, pkalg, slen);
560 buffer_append(&b, sig, slen);
564 pktype = key_type_from_name(pkalg);
565 if (pktype == KEY_UNSPEC) {
566 /* this is perfectly legal */
567 log("userauth_hostbased: unsupported "
568 "public key algorithm: %s", pkalg);
571 key = key_from_blob(pkblob, blen);
573 error("userauth_hostbased: cannot decode key: %s", pkalg);
576 if (key->type != pktype) {
577 error("userauth_hostbased: type mismatch for decoded key "
578 "(received %d, expected %d)", key->type, pktype);
581 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
584 buffer_put_string(&b, session_id2, session_id2_len);
585 /* reconstruct packet */
586 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
587 buffer_put_cstring(&b, authctxt->user);
588 buffer_put_cstring(&b, service);
589 buffer_put_cstring(&b, "hostbased");
590 buffer_put_string(&b, pkalg, alen);
591 buffer_put_string(&b, pkblob, blen);
592 buffer_put_cstring(&b, chost);
593 buffer_put_cstring(&b, cuser);
597 /* test for allowed key and correct signature */
598 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
599 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
604 debug2("userauth_hostbased: authenticated %d", authenticated);
612 return authenticated;
615 /* get current user */
620 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
626 authmethods_get(void)
628 Authmethod *method = NULL;
633 for (method = authmethods; method->name != NULL; method++) {
634 if (strcmp(method->name, "none") == 0)
636 if (method->enabled != NULL && *(method->enabled) != 0) {
637 if (buffer_len(&b) > 0)
638 buffer_append(&b, ",", 1);
639 buffer_append(&b, method->name, strlen(method->name));
642 buffer_append(&b, "\0", 1);
643 list = xstrdup(buffer_ptr(&b));
649 authmethod_lookup(const char *name)
651 Authmethod *method = NULL;
653 for (method = authmethods; method->name != NULL; method++)
654 if (method->enabled != NULL &&
655 *(method->enabled) != 0 &&
656 strcmp(name, method->name) == 0)
658 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
662 /* return 1 if user allows given key */
664 user_key_allowed2(struct passwd *pw, Key *key, char *file)
677 /* Temporarily use the user's uid. */
678 temporarily_use_uid(pw);
680 debug("trying public key file %s", file);
682 /* Fail quietly if file does not exist */
683 if (stat(file, &st) < 0) {
684 /* Restore the privileged uid. */
688 /* Open the file containing the authorized keys. */
689 f = fopen(file, "r");
691 /* Restore the privileged uid. */
695 if (options.strict_modes &&
696 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
698 log("Authentication refused: %s", line);
704 found = key_new(key->type);
706 while (fgets(line, sizeof(line), f)) {
707 char *cp, *options = NULL;
709 /* Skip leading whitespace, empty and comment lines. */
710 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
712 if (!*cp || *cp == '\n' || *cp == '#')
715 if (key_read(found, &cp) != 1) {
716 /* no key? check if there are options for this key */
718 debug2("user_key_allowed: check options: '%s'", cp);
720 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
721 if (*cp == '\\' && cp[1] == '"')
722 cp++; /* Skip both */
726 /* Skip remaining whitespace. */
727 for (; *cp == ' ' || *cp == '\t'; cp++)
729 if (key_read(found, &cp) != 1) {
730 debug2("user_key_allowed: advance: '%s'", cp);
731 /* still no key? advance to next line*/
735 if (key_equal(found, key) &&
736 auth_parse_options(pw, options, file, linenum) == 1) {
738 debug("matching key found: file %s, line %lu",
740 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
741 verbose("Found matching %s key: %s",
742 key_type(found), fp);
751 debug2("key not found");
755 /* check whether given key is in .ssh/authorized_keys* */
757 user_key_allowed(struct passwd *pw, Key *key)
762 file = authorized_keys_file(pw);
763 success = user_key_allowed2(pw, key, file);
768 /* try suffix "2" for backward compat, too */
769 file = authorized_keys_file2(pw);
770 success = user_key_allowed2(pw, key, file);
775 /* return 1 if given hostkey is allowed */
777 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
780 const char *resolvedname, *ipaddr, *lookup;
781 HostStatus host_status;
784 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
785 ipaddr = get_remote_ipaddr();
787 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
788 chost, resolvedname, ipaddr);
790 if (options.hostbased_uses_name_from_packet_only) {
791 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
795 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
796 debug2("stripping trailing dot from chost %s", chost);
797 chost[len - 1] = '\0';
799 if (strcasecmp(resolvedname, chost) != 0)
800 log("userauth_hostbased mismatch: "
801 "client sends %s, but we resolve %s to %s",
802 chost, ipaddr, resolvedname);
803 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
805 lookup = resolvedname;
807 debug2("userauth_hostbased: access allowed by auth_rhosts2");
809 host_status = check_key_in_hostfiles(pw, key, lookup,
810 _PATH_SSH_SYSTEM_HOSTFILE,
811 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
813 /* backward compat if no key has been found. */
814 if (host_status == HOST_NEW)
815 host_status = check_key_in_hostfiles(pw, key, lookup,
816 _PATH_SSH_SYSTEM_HOSTFILE2,
817 options.ignore_user_known_hosts ? NULL :
818 _PATH_SSH_USER_HOSTFILE2);
820 return (host_status == HOST_OK);