Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 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. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 12 | * |
| 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. |
| 23 | */ |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 24 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 25 | #include "includes.h" |
Ben Lindstrom | 7ebb635 | 2002-03-22 03:04:08 +0000 | [diff] [blame] | 26 | RCSID("$OpenBSD: auth2.c,v 1.89 2002/03/19 14:27:39 markus Exp $"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 27 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 28 | #include <openssl/evp.h> |
| 29 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 30 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 31 | #include "xmalloc.h" |
| 32 | #include "rsa.h" |
Ben Lindstrom | d95c09c | 2001-02-18 19:13:33 +0000 | [diff] [blame] | 33 | #include "sshpty.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 34 | #include "packet.h" |
| 35 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 36 | #include "log.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 37 | #include "servconf.h" |
| 38 | #include "compat.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 39 | #include "channels.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 40 | #include "bufaux.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 41 | #include "auth.h" |
| 42 | #include "session.h" |
| 43 | #include "dispatch.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | #include "key.h" |
Ben Lindstrom | 6d40c0f | 2001-01-29 09:02:24 +0000 | [diff] [blame] | 45 | #include "cipher.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 46 | #include "kex.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 47 | #include "pathnames.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 48 | #include "uidswap.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 49 | #include "auth-options.h" |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 50 | #include "hostfile.h" |
| 51 | #include "canohost.h" |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 52 | #include "match.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 53 | #include "monitor_wrap.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 54 | |
| 55 | /* import */ |
| 56 | extern ServerOptions options; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 57 | extern u_char *session_id2; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 58 | extern int session_id2_len; |
| 59 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 60 | Authctxt *x_authctxt = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 61 | static int one = 1; |
| 62 | |
| 63 | typedef struct Authmethod Authmethod; |
| 64 | struct Authmethod { |
| 65 | char *name; |
| 66 | int (*userauth)(Authctxt *authctxt); |
| 67 | int *enabled; |
| 68 | }; |
| 69 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 70 | /* protocol */ |
| 71 | |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 72 | static void input_service_request(int, u_int32_t, void *); |
| 73 | static void input_userauth_request(int, u_int32_t, void *); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 74 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 75 | /* helper */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 76 | static Authmethod *authmethod_lookup(const char *); |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 77 | static char *authmethods_get(void); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 78 | int user_key_allowed(struct passwd *, Key *); |
| 79 | int hostbased_key_allowed(struct passwd *, const char *, char *, Key *); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 80 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 81 | /* auth */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 82 | static void userauth_banner(void); |
| 83 | static int userauth_none(Authctxt *); |
| 84 | static int userauth_passwd(Authctxt *); |
| 85 | static int userauth_pubkey(Authctxt *); |
| 86 | static int userauth_hostbased(Authctxt *); |
| 87 | static int userauth_kbdint(Authctxt *); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 88 | |
| 89 | Authmethod authmethods[] = { |
| 90 | {"none", |
| 91 | userauth_none, |
| 92 | &one}, |
| 93 | {"publickey", |
| 94 | userauth_pubkey, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 95 | &options.pubkey_authentication}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 96 | {"password", |
| 97 | userauth_passwd, |
| 98 | &options.password_authentication}, |
Ben Lindstrom | d1f20ec | 2001-02-10 21:31:53 +0000 | [diff] [blame] | 99 | {"keyboard-interactive", |
| 100 | userauth_kbdint, |
| 101 | &options.kbd_interactive_authentication}, |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 102 | {"hostbased", |
| 103 | userauth_hostbased, |
| 104 | &options.hostbased_authentication}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 105 | {NULL, NULL, NULL} |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 106 | }; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 107 | |
| 108 | /* |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 109 | * loop until authctxt->success == TRUE |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 110 | */ |
| 111 | |
Ben Lindstrom | 73ab9ba | 2002-03-22 01:27:35 +0000 | [diff] [blame] | 112 | Authctxt * |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 113 | do_authentication2(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 114 | { |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 115 | Authctxt *authctxt = authctxt_new(); |
| 116 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 117 | x_authctxt = authctxt; /*XXX*/ |
| 118 | |
Ben Lindstrom | bdfb4df | 2001-10-03 17:12:43 +0000 | [diff] [blame] | 119 | /* challenge-response is implemented via keyboard interactive */ |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 120 | if (options.challenge_response_authentication) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 121 | options.kbd_interactive_authentication = 1; |
Damien Miller | f815442 | 2001-04-25 22:44:14 +1000 | [diff] [blame] | 122 | if (options.pam_authentication_via_kbd_int) |
| 123 | options.kbd_interactive_authentication = 1; |
Damien Miller | ffc868f | 2002-05-09 15:59:13 +1000 | [diff] [blame] | 124 | if (use_privsep) |
| 125 | options.pam_authentication_via_kbd_int = 0; |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 126 | |
Damien Miller | 7d05339 | 2002-01-22 23:24:13 +1100 | [diff] [blame] | 127 | dispatch_init(&dispatch_protocol_error); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 128 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 129 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
Ben Lindstrom | 73ab9ba | 2002-03-22 01:27:35 +0000 | [diff] [blame] | 130 | |
| 131 | return (authctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 132 | } |
| 133 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 134 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 135 | input_service_request(int type, u_int32_t seq, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 136 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 137 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 138 | u_int len; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 139 | int accept = 0; |
| 140 | char *service = packet_get_string(&len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 141 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 142 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 143 | if (authctxt == NULL) |
| 144 | fatal("input_service_request: no authctxt"); |
| 145 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 146 | if (strcmp(service, "ssh-userauth") == 0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 147 | if (!authctxt->success) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 148 | accept = 1; |
| 149 | /* now we can handle user-auth requests */ |
| 150 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); |
| 151 | } |
| 152 | } |
| 153 | /* XXX all other service requests are denied */ |
| 154 | |
| 155 | if (accept) { |
| 156 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
| 157 | packet_put_cstring(service); |
| 158 | packet_send(); |
| 159 | packet_write_wait(); |
| 160 | } else { |
| 161 | debug("bad service request %s", service); |
| 162 | packet_disconnect("bad service request %s", service); |
| 163 | } |
| 164 | xfree(service); |
| 165 | } |
| 166 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 167 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 168 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 169 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 170 | Authctxt *authctxt = ctxt; |
| 171 | Authmethod *m = NULL; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 172 | char *user, *service, *method, *style = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 173 | int authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 174 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 175 | if (authctxt == NULL) |
| 176 | fatal("input_userauth_request: no authctxt"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 177 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 178 | user = packet_get_string(NULL); |
| 179 | service = packet_get_string(NULL); |
| 180 | method = packet_get_string(NULL); |
| 181 | debug("userauth-request for user %s service %s method %s", user, service, method); |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 182 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 183 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 184 | if ((style = strchr(user, ':')) != NULL) |
| 185 | *style++ = 0; |
| 186 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 187 | if (authctxt->attempt++ == 0) { |
Damien Miller | 3a5b023 | 2002-03-13 13:19:42 +1100 | [diff] [blame] | 188 | /* setup auth context */ |
Kevin Steves | 205cc1e | 2002-03-22 20:43:05 +0000 | [diff] [blame] | 189 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
| 190 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 191 | authctxt->valid = 1; |
| 192 | debug2("input_userauth_request: setting up authctxt for %s", user); |
| 193 | #ifdef USE_PAM |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 194 | PRIVSEP(start_pam(authctxt->pw->pw_name)); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 195 | #endif |
| 196 | } else { |
| 197 | log("input_userauth_request: illegal user %s", user); |
Damien Miller | 22e22bf | 2001-01-19 15:46:38 +1100 | [diff] [blame] | 198 | #ifdef USE_PAM |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 199 | PRIVSEP(start_pam("NOUSER")); |
Damien Miller | 22e22bf | 2001-01-19 15:46:38 +1100 | [diff] [blame] | 200 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 201 | } |
Ben Lindstrom | 7ebb635 | 2002-03-22 03:04:08 +0000 | [diff] [blame] | 202 | setproctitle("%s%s", authctxt->pw ? user : "unknown", |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 203 | use_privsep ? " [net]" : ""); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 204 | authctxt->user = xstrdup(user); |
| 205 | authctxt->service = xstrdup(service); |
Ben Lindstrom | 9d0c066 | 2001-06-09 01:40:00 +0000 | [diff] [blame] | 206 | authctxt->style = style ? xstrdup(style) : NULL; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 207 | if (use_privsep) |
| 208 | mm_inform_authserv(service, style); |
Ben Lindstrom | 9d0c066 | 2001-06-09 01:40:00 +0000 | [diff] [blame] | 209 | } else if (strcmp(user, authctxt->user) != 0 || |
| 210 | strcmp(service, authctxt->service) != 0) { |
| 211 | packet_disconnect("Change of username or service not allowed: " |
| 212 | "(%s,%s) -> (%s,%s)", |
| 213 | authctxt->user, authctxt->service, user, service); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 214 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 215 | /* reset state */ |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 216 | auth2_challenge_stop(authctxt); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 217 | authctxt->postponed = 0; |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 218 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 219 | /* try to authenticate user */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 220 | m = authmethod_lookup(method); |
| 221 | if (m != NULL) { |
| 222 | debug2("input_userauth_request: try method %s", method); |
| 223 | authenticated = m->userauth(authctxt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 224 | } |
Damien Miller | 5d57e50 | 2001-03-30 10:48:31 +1000 | [diff] [blame] | 225 | userauth_finish(authctxt, authenticated, method); |
| 226 | |
| 227 | xfree(service); |
| 228 | xfree(user); |
| 229 | xfree(method); |
| 230 | } |
| 231 | |
| 232 | void |
| 233 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) |
| 234 | { |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 235 | char *methods; |
| 236 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 237 | if (!authctxt->valid && authenticated) |
| 238 | fatal("INTERNAL ERROR: authenticated invalid user %s", |
| 239 | authctxt->user); |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 240 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 241 | /* Special handling for root */ |
Ben Lindstrom | d8a9021 | 2001-02-15 03:08:27 +0000 | [diff] [blame] | 242 | if (authenticated && authctxt->pw->pw_uid == 0 && |
| 243 | !auth_root_allowed(method)) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 244 | authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 245 | |
| 246 | #ifdef USE_PAM |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 247 | if (!use_privsep && authenticated && authctxt->user && |
| 248 | !do_pam_account(authctxt->user, NULL)) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 249 | authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 250 | #endif /* USE_PAM */ |
| 251 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 252 | /* Log before sending the reply */ |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 253 | auth_log(authctxt, authenticated, method, " ssh2"); |
| 254 | |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 255 | if (authctxt->postponed) |
| 256 | return; |
| 257 | |
| 258 | /* XXX todo: check if multiple auth methods are needed */ |
| 259 | if (authenticated == 1) { |
| 260 | /* turn off userauth */ |
Damien Miller | 7d05339 | 2002-01-22 23:24:13 +1100 | [diff] [blame] | 261 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 262 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
| 263 | packet_send(); |
| 264 | packet_write_wait(); |
| 265 | /* now we can break out */ |
| 266 | authctxt->success = 1; |
| 267 | } else { |
Damien Miller | e49d096 | 2001-11-13 23:46:18 +1100 | [diff] [blame] | 268 | if (authctxt->failures++ > AUTH_FAIL_MAX) { |
| 269 | #ifdef WITH_AIXAUTHENTICATE |
| 270 | loginfailed(authctxt->user, |
Damien Miller | f3451a2 | 2002-02-05 12:40:46 +1100 | [diff] [blame] | 271 | get_canonical_hostname(options.verify_reverse_mapping), |
Damien Miller | e49d096 | 2001-11-13 23:46:18 +1100 | [diff] [blame] | 272 | "ssh"); |
| 273 | #endif /* WITH_AIXAUTHENTICATE */ |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 274 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
Damien Miller | e49d096 | 2001-11-13 23:46:18 +1100 | [diff] [blame] | 275 | } |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 276 | methods = authmethods_get(); |
| 277 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
| 278 | packet_put_cstring(methods); |
| 279 | packet_put_char(0); /* XXX partial success, unused */ |
| 280 | packet_send(); |
| 281 | packet_write_wait(); |
| 282 | xfree(methods); |
| 283 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 284 | } |
| 285 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 286 | static void |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 287 | userauth_banner(void) |
| 288 | { |
| 289 | struct stat st; |
| 290 | char *banner = NULL; |
| 291 | off_t len, n; |
| 292 | int fd; |
| 293 | |
| 294 | if (options.banner == NULL || (datafellows & SSH_BUG_BANNER)) |
| 295 | return; |
Ben Lindstrom | 0cae040 | 2001-04-04 23:47:52 +0000 | [diff] [blame] | 296 | if ((fd = open(options.banner, O_RDONLY)) < 0) |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 297 | return; |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 298 | if (fstat(fd, &st) < 0) |
| 299 | goto done; |
| 300 | len = st.st_size; |
| 301 | banner = xmalloc(len + 1); |
| 302 | if ((n = read(fd, banner, len)) < 0) |
| 303 | goto done; |
| 304 | banner[n] = '\0'; |
| 305 | packet_start(SSH2_MSG_USERAUTH_BANNER); |
| 306 | packet_put_cstring(banner); |
| 307 | packet_put_cstring(""); /* language, unused */ |
| 308 | packet_send(); |
| 309 | debug("userauth_banner: sent"); |
| 310 | done: |
| 311 | if (banner) |
| 312 | xfree(banner); |
| 313 | close(fd); |
| 314 | return; |
| 315 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 316 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 317 | static int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 318 | userauth_none(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 319 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 320 | /* disable method "none", only allowed one time */ |
| 321 | Authmethod *m = authmethod_lookup("none"); |
| 322 | if (m != NULL) |
| 323 | m->enabled = NULL; |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 324 | packet_check_eom(); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 325 | userauth_banner(); |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 326 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 327 | if (authctxt->valid == 0) |
| 328 | return(0); |
Damien Miller | 3a5b023 | 2002-03-13 13:19:42 +1100 | [diff] [blame] | 329 | |
| 330 | #ifdef HAVE_CYGWIN |
| 331 | if (check_nt_auth(1, authctxt->pw) == 0) |
| 332 | return(0); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 333 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 334 | return PRIVSEP(auth_password(authctxt, "")); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 335 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 336 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 337 | static int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 338 | userauth_passwd(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 339 | { |
| 340 | char *password; |
| 341 | int authenticated = 0; |
| 342 | int change; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 343 | u_int len; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 344 | change = packet_get_char(); |
| 345 | if (change) |
| 346 | log("password change not supported"); |
| 347 | password = packet_get_string(&len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 348 | packet_check_eom(); |
Damien Miller | 3a5b023 | 2002-03-13 13:19:42 +1100 | [diff] [blame] | 349 | if (authctxt->valid && |
| 350 | #ifdef HAVE_CYGWIN |
| 351 | check_nt_auth(1, authctxt->pw) && |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 352 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 353 | PRIVSEP(auth_password(authctxt, password)) == 1) |
Damien Miller | 3a5b023 | 2002-03-13 13:19:42 +1100 | [diff] [blame] | 354 | authenticated = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 355 | memset(password, 0, len); |
| 356 | xfree(password); |
| 357 | return authenticated; |
| 358 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 359 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 360 | static int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 361 | userauth_kbdint(Authctxt *authctxt) |
| 362 | { |
| 363 | int authenticated = 0; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 364 | char *lang, *devs; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 365 | |
| 366 | lang = packet_get_string(NULL); |
| 367 | devs = packet_get_string(NULL); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 368 | packet_check_eom(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 369 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 370 | debug("keyboard-interactive devs %s", devs); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 371 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 372 | if (options.challenge_response_authentication) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 373 | authenticated = auth2_challenge(authctxt, devs); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 374 | |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 375 | #ifdef USE_PAM |
Damien Miller | f815442 | 2001-04-25 22:44:14 +1000 | [diff] [blame] | 376 | if (authenticated == 0 && options.pam_authentication_via_kbd_int) |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 377 | authenticated = auth2_pam(authctxt); |
| 378 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 379 | xfree(devs); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 380 | xfree(lang); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 381 | #ifdef HAVE_CYGWIN |
Damien Miller | 0dea79d | 2001-12-29 14:08:28 +1100 | [diff] [blame] | 382 | if (check_nt_auth(0, authctxt->pw) == 0) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 383 | return(0); |
| 384 | #endif |
| 385 | return authenticated; |
| 386 | } |
| 387 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 388 | static int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 389 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 390 | { |
| 391 | Buffer b; |
Damien Miller | 9b74bfc | 2002-02-05 12:26:03 +1100 | [diff] [blame] | 392 | Key *key = NULL; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 393 | char *pkalg; |
| 394 | u_char *pkblob, *sig; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 395 | u_int alen, blen, slen; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 396 | int have_sig, pktype; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 397 | int authenticated = 0; |
| 398 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 399 | if (!authctxt->valid) { |
| 400 | debug2("userauth_pubkey: disabled because of invalid user"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | have_sig = packet_get_char(); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 404 | if (datafellows & SSH_BUG_PKAUTH) { |
| 405 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); |
| 406 | /* no explicit pkalg given */ |
| 407 | pkblob = packet_get_string(&blen); |
| 408 | buffer_init(&b); |
| 409 | buffer_append(&b, pkblob, blen); |
| 410 | /* so we have to extract the pkalg from the pkblob */ |
| 411 | pkalg = buffer_get_string(&b, &alen); |
| 412 | buffer_free(&b); |
| 413 | } else { |
| 414 | pkalg = packet_get_string(&alen); |
| 415 | pkblob = packet_get_string(&blen); |
| 416 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 417 | pktype = key_type_from_name(pkalg); |
| 418 | if (pktype == KEY_UNSPEC) { |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 419 | /* this is perfectly legal */ |
Damien Miller | 9b74bfc | 2002-02-05 12:26:03 +1100 | [diff] [blame] | 420 | log("userauth_pubkey: unsupported public key algorithm: %s", |
| 421 | pkalg); |
| 422 | goto done; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 423 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 424 | key = key_from_blob(pkblob, blen); |
Damien Miller | 9b74bfc | 2002-02-05 12:26:03 +1100 | [diff] [blame] | 425 | if (key == NULL) { |
| 426 | error("userauth_pubkey: cannot decode key: %s", pkalg); |
| 427 | goto done; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 428 | } |
Damien Miller | 9b74bfc | 2002-02-05 12:26:03 +1100 | [diff] [blame] | 429 | if (key->type != pktype) { |
| 430 | error("userauth_pubkey: type mismatch for decoded key " |
| 431 | "(received %d, expected %d)", key->type, pktype); |
| 432 | goto done; |
| 433 | } |
| 434 | if (have_sig) { |
| 435 | sig = packet_get_string(&slen); |
| 436 | packet_check_eom(); |
| 437 | buffer_init(&b); |
| 438 | if (datafellows & SSH_OLD_SESSIONID) { |
| 439 | buffer_append(&b, session_id2, session_id2_len); |
| 440 | } else { |
| 441 | buffer_put_string(&b, session_id2, session_id2_len); |
| 442 | } |
| 443 | /* reconstruct packet */ |
| 444 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
| 445 | buffer_put_cstring(&b, authctxt->user); |
| 446 | buffer_put_cstring(&b, |
| 447 | datafellows & SSH_BUG_PKSERVICE ? |
| 448 | "ssh-userauth" : |
| 449 | authctxt->service); |
| 450 | if (datafellows & SSH_BUG_PKAUTH) { |
| 451 | buffer_put_char(&b, have_sig); |
| 452 | } else { |
| 453 | buffer_put_cstring(&b, "publickey"); |
| 454 | buffer_put_char(&b, have_sig); |
| 455 | buffer_put_cstring(&b, pkalg); |
| 456 | } |
| 457 | buffer_put_string(&b, pkblob, blen); |
| 458 | #ifdef DEBUG_PK |
| 459 | buffer_dump(&b); |
| 460 | #endif |
| 461 | /* test for correct signature */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 462 | authenticated = 0; |
| 463 | if (PRIVSEP(user_key_allowed(authctxt->pw, key)) && |
| 464 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), |
| 465 | buffer_len(&b))) == 1) |
Damien Miller | 3a5b023 | 2002-03-13 13:19:42 +1100 | [diff] [blame] | 466 | authenticated = 1; |
Damien Miller | 9b74bfc | 2002-02-05 12:26:03 +1100 | [diff] [blame] | 467 | buffer_clear(&b); |
| 468 | xfree(sig); |
| 469 | } else { |
| 470 | debug("test whether pkalg/pkblob are acceptable"); |
| 471 | packet_check_eom(); |
| 472 | |
| 473 | /* XXX fake reply and always send PK_OK ? */ |
| 474 | /* |
| 475 | * XXX this allows testing whether a user is allowed |
| 476 | * to login: if you happen to have a valid pubkey this |
| 477 | * message is sent. the message is NEVER sent at all |
| 478 | * if a user is not allowed to login. is this an |
| 479 | * issue? -markus |
| 480 | */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 481 | if (PRIVSEP(user_key_allowed(authctxt->pw, key))) { |
Damien Miller | 9b74bfc | 2002-02-05 12:26:03 +1100 | [diff] [blame] | 482 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
| 483 | packet_put_string(pkalg, alen); |
| 484 | packet_put_string(pkblob, blen); |
| 485 | packet_send(); |
| 486 | packet_write_wait(); |
| 487 | authctxt->postponed = 1; |
| 488 | } |
| 489 | } |
| 490 | if (authenticated != 1) |
| 491 | auth_clear_options(); |
| 492 | done: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 493 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
Damien Miller | 9b74bfc | 2002-02-05 12:26:03 +1100 | [diff] [blame] | 494 | if (key != NULL) |
| 495 | key_free(key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 496 | xfree(pkalg); |
| 497 | xfree(pkblob); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 498 | #ifdef HAVE_CYGWIN |
Damien Miller | 0dea79d | 2001-12-29 14:08:28 +1100 | [diff] [blame] | 499 | if (check_nt_auth(0, authctxt->pw) == 0) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 500 | return(0); |
| 501 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 502 | return authenticated; |
| 503 | } |
| 504 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 505 | static int |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 506 | userauth_hostbased(Authctxt *authctxt) |
| 507 | { |
| 508 | Buffer b; |
Damien Miller | 9b74bfc | 2002-02-05 12:26:03 +1100 | [diff] [blame] | 509 | Key *key = NULL; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 510 | char *pkalg, *cuser, *chost, *service; |
| 511 | u_char *pkblob, *sig; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 512 | u_int alen, blen, slen; |
| 513 | int pktype; |
| 514 | int authenticated = 0; |
| 515 | |
| 516 | if (!authctxt->valid) { |
| 517 | debug2("userauth_hostbased: disabled because of invalid user"); |
| 518 | return 0; |
| 519 | } |
| 520 | pkalg = packet_get_string(&alen); |
| 521 | pkblob = packet_get_string(&blen); |
| 522 | chost = packet_get_string(NULL); |
| 523 | cuser = packet_get_string(NULL); |
| 524 | sig = packet_get_string(&slen); |
| 525 | |
| 526 | debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d", |
| 527 | cuser, chost, pkalg, slen); |
| 528 | #ifdef DEBUG_PK |
| 529 | debug("signature:"); |
| 530 | buffer_init(&b); |
| 531 | buffer_append(&b, sig, slen); |
| 532 | buffer_dump(&b); |
| 533 | buffer_free(&b); |
| 534 | #endif |
| 535 | pktype = key_type_from_name(pkalg); |
| 536 | if (pktype == KEY_UNSPEC) { |
| 537 | /* this is perfectly legal */ |
| 538 | log("userauth_hostbased: unsupported " |
| 539 | "public key algorithm: %s", pkalg); |
| 540 | goto done; |
| 541 | } |
| 542 | key = key_from_blob(pkblob, blen); |
| 543 | if (key == NULL) { |
Damien Miller | 9b74bfc | 2002-02-05 12:26:03 +1100 | [diff] [blame] | 544 | error("userauth_hostbased: cannot decode key: %s", pkalg); |
| 545 | goto done; |
| 546 | } |
| 547 | if (key->type != pktype) { |
| 548 | error("userauth_hostbased: type mismatch for decoded key " |
| 549 | "(received %d, expected %d)", key->type, pktype); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 550 | goto done; |
| 551 | } |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 552 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
| 553 | authctxt->service; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 554 | buffer_init(&b); |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 555 | buffer_put_string(&b, session_id2, session_id2_len); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 556 | /* reconstruct packet */ |
| 557 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
| 558 | buffer_put_cstring(&b, authctxt->user); |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 559 | buffer_put_cstring(&b, service); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 560 | buffer_put_cstring(&b, "hostbased"); |
| 561 | buffer_put_string(&b, pkalg, alen); |
| 562 | buffer_put_string(&b, pkblob, blen); |
| 563 | buffer_put_cstring(&b, chost); |
| 564 | buffer_put_cstring(&b, cuser); |
| 565 | #ifdef DEBUG_PK |
| 566 | buffer_dump(&b); |
| 567 | #endif |
| 568 | /* test for allowed key and correct signature */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 569 | authenticated = 0; |
| 570 | if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) && |
| 571 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), |
| 572 | buffer_len(&b))) == 1) |
Damien Miller | 3a5b023 | 2002-03-13 13:19:42 +1100 | [diff] [blame] | 573 | authenticated = 1; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 574 | |
| 575 | buffer_clear(&b); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 576 | done: |
| 577 | debug2("userauth_hostbased: authenticated %d", authenticated); |
Damien Miller | 9b74bfc | 2002-02-05 12:26:03 +1100 | [diff] [blame] | 578 | if (key != NULL) |
| 579 | key_free(key); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 580 | xfree(pkalg); |
| 581 | xfree(pkblob); |
| 582 | xfree(cuser); |
| 583 | xfree(chost); |
| 584 | xfree(sig); |
| 585 | return authenticated; |
| 586 | } |
| 587 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 588 | /* get current user */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 589 | |
| 590 | struct passwd* |
| 591 | auth_get_user(void) |
| 592 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 593 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 594 | } |
| 595 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 596 | #define DELIM "," |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 597 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 598 | static char * |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 599 | authmethods_get(void) |
| 600 | { |
| 601 | Authmethod *method = NULL; |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 602 | Buffer b; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 603 | char *list; |
| 604 | |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 605 | buffer_init(&b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 606 | for (method = authmethods; method->name != NULL; method++) { |
| 607 | if (strcmp(method->name, "none") == 0) |
| 608 | continue; |
| 609 | if (method->enabled != NULL && *(method->enabled) != 0) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 610 | if (buffer_len(&b) > 0) |
| 611 | buffer_append(&b, ",", 1); |
| 612 | buffer_append(&b, method->name, strlen(method->name)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 613 | } |
| 614 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 615 | buffer_append(&b, "\0", 1); |
| 616 | list = xstrdup(buffer_ptr(&b)); |
| 617 | buffer_free(&b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 618 | return list; |
| 619 | } |
| 620 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 621 | static Authmethod * |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 622 | authmethod_lookup(const char *name) |
| 623 | { |
| 624 | Authmethod *method = NULL; |
| 625 | if (name != NULL) |
| 626 | for (method = authmethods; method->name != NULL; method++) |
| 627 | if (method->enabled != NULL && |
| 628 | *(method->enabled) != 0 && |
| 629 | strcmp(name, method->name) == 0) |
| 630 | return method; |
| 631 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 632 | return NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* return 1 if user allows given key */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 636 | static int |
Ben Lindstrom | f96704d | 2001-06-25 04:17:12 +0000 | [diff] [blame] | 637 | user_key_allowed2(struct passwd *pw, Key *key, char *file) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 638 | { |
Ben Lindstrom | f96704d | 2001-06-25 04:17:12 +0000 | [diff] [blame] | 639 | char line[8192]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 640 | int found_key = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 641 | FILE *f; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 642 | u_long linenum = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 643 | struct stat st; |
| 644 | Key *found; |
Damien Miller | da9edca | 2001-12-21 12:48:54 +1100 | [diff] [blame] | 645 | char *fp; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 646 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 647 | if (pw == NULL) |
| 648 | return 0; |
| 649 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 650 | /* Temporarily use the user's uid. */ |
Ben Lindstrom | 3fcf1a2 | 2001-04-08 18:26:59 +0000 | [diff] [blame] | 651 | temporarily_use_uid(pw); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 652 | |
Ben Lindstrom | bfb3a0e | 2001-06-05 20:25:05 +0000 | [diff] [blame] | 653 | debug("trying public key file %s", file); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 654 | |
| 655 | /* Fail quietly if file does not exist */ |
| 656 | if (stat(file, &st) < 0) { |
| 657 | /* Restore the privileged uid. */ |
| 658 | restore_uid(); |
| 659 | return 0; |
| 660 | } |
| 661 | /* Open the file containing the authorized keys. */ |
| 662 | f = fopen(file, "r"); |
| 663 | if (!f) { |
| 664 | /* Restore the privileged uid. */ |
| 665 | restore_uid(); |
| 666 | return 0; |
| 667 | } |
Ben Lindstrom | bfb3a0e | 2001-06-05 20:25:05 +0000 | [diff] [blame] | 668 | if (options.strict_modes && |
Ben Lindstrom | 90279d8 | 2001-07-04 03:56:56 +0000 | [diff] [blame] | 669 | secure_filename(f, file, pw, line, sizeof(line)) != 0) { |
Ben Lindstrom | bfb3a0e | 2001-06-05 20:25:05 +0000 | [diff] [blame] | 670 | fclose(f); |
| 671 | log("Authentication refused: %s", line); |
| 672 | restore_uid(); |
| 673 | return 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 674 | } |
Ben Lindstrom | bfb3a0e | 2001-06-05 20:25:05 +0000 | [diff] [blame] | 675 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 676 | found_key = 0; |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 677 | found = key_new(key->type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 678 | |
| 679 | while (fgets(line, sizeof(line), f)) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 680 | char *cp, *options = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 681 | linenum++; |
| 682 | /* Skip leading whitespace, empty and comment lines. */ |
| 683 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 684 | ; |
| 685 | if (!*cp || *cp == '\n' || *cp == '#') |
| 686 | continue; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 687 | |
Ben Lindstrom | 1bc3bdb | 2001-09-20 23:11:26 +0000 | [diff] [blame] | 688 | if (key_read(found, &cp) != 1) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 689 | /* no key? check if there are options for this key */ |
| 690 | int quoted = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 691 | debug2("user_key_allowed: check options: '%s'", cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 692 | options = cp; |
| 693 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 694 | if (*cp == '\\' && cp[1] == '"') |
| 695 | cp++; /* Skip both */ |
| 696 | else if (*cp == '"') |
| 697 | quoted = !quoted; |
| 698 | } |
| 699 | /* Skip remaining whitespace. */ |
| 700 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 701 | ; |
Ben Lindstrom | 1bc3bdb | 2001-09-20 23:11:26 +0000 | [diff] [blame] | 702 | if (key_read(found, &cp) != 1) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 703 | debug2("user_key_allowed: advance: '%s'", cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 704 | /* still no key? advance to next line*/ |
| 705 | continue; |
| 706 | } |
| 707 | } |
| 708 | if (key_equal(found, key) && |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 709 | auth_parse_options(pw, options, file, linenum) == 1) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 710 | found_key = 1; |
Ben Lindstrom | 940fb86 | 2001-08-06 21:01:49 +0000 | [diff] [blame] | 711 | debug("matching key found: file %s, line %lu", |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 712 | file, linenum); |
Damien Miller | da9edca | 2001-12-21 12:48:54 +1100 | [diff] [blame] | 713 | fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); |
| 714 | verbose("Found matching %s key: %s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 715 | key_type(found), fp); |
Damien Miller | da9edca | 2001-12-21 12:48:54 +1100 | [diff] [blame] | 716 | xfree(fp); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 717 | break; |
| 718 | } |
| 719 | } |
| 720 | restore_uid(); |
| 721 | fclose(f); |
| 722 | key_free(found); |
Ben Lindstrom | b54873a | 2001-03-11 20:01:55 +0000 | [diff] [blame] | 723 | if (!found_key) |
| 724 | debug2("key not found"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 725 | return found_key; |
| 726 | } |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 727 | |
Ben Lindstrom | f96704d | 2001-06-25 04:17:12 +0000 | [diff] [blame] | 728 | /* check whether given key is in .ssh/authorized_keys* */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 729 | int |
Ben Lindstrom | f96704d | 2001-06-25 04:17:12 +0000 | [diff] [blame] | 730 | user_key_allowed(struct passwd *pw, Key *key) |
| 731 | { |
| 732 | int success; |
| 733 | char *file; |
| 734 | |
| 735 | file = authorized_keys_file(pw); |
| 736 | success = user_key_allowed2(pw, key, file); |
| 737 | xfree(file); |
| 738 | if (success) |
| 739 | return success; |
| 740 | |
| 741 | /* try suffix "2" for backward compat, too */ |
| 742 | file = authorized_keys_file2(pw); |
| 743 | success = user_key_allowed2(pw, key, file); |
| 744 | xfree(file); |
| 745 | return success; |
| 746 | } |
| 747 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 748 | /* return 1 if given hostkey is allowed */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 749 | int |
Ben Lindstrom | 4aa603c | 2001-04-19 20:38:06 +0000 | [diff] [blame] | 750 | hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost, |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 751 | Key *key) |
| 752 | { |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 753 | const char *resolvedname, *ipaddr, *lookup; |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 754 | HostStatus host_status; |
| 755 | int len; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 756 | |
Damien Miller | c5d8635 | 2002-02-05 12:13:41 +1100 | [diff] [blame] | 757 | resolvedname = get_canonical_hostname(options.verify_reverse_mapping); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 758 | ipaddr = get_remote_ipaddr(); |
| 759 | |
Ben Lindstrom | 2bffd6f | 2001-04-19 20:35:40 +0000 | [diff] [blame] | 760 | debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s", |
| 761 | chost, resolvedname, ipaddr); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 762 | |
| 763 | if (options.hostbased_uses_name_from_packet_only) { |
| 764 | if (auth_rhosts2(pw, cuser, chost, chost) == 0) |
| 765 | return 0; |
| 766 | lookup = chost; |
| 767 | } else { |
Ben Lindstrom | 2bffd6f | 2001-04-19 20:35:40 +0000 | [diff] [blame] | 768 | if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') { |
| 769 | debug2("stripping trailing dot from chost %s", chost); |
| 770 | chost[len - 1] = '\0'; |
| 771 | } |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 772 | if (strcasecmp(resolvedname, chost) != 0) |
| 773 | log("userauth_hostbased mismatch: " |
| 774 | "client sends %s, but we resolve %s to %s", |
| 775 | chost, ipaddr, resolvedname); |
| 776 | if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) |
| 777 | return 0; |
| 778 | lookup = resolvedname; |
| 779 | } |
| 780 | debug2("userauth_hostbased: access allowed by auth_rhosts2"); |
| 781 | |
Ben Lindstrom | 83647ce | 2001-06-25 04:30:16 +0000 | [diff] [blame] | 782 | host_status = check_key_in_hostfiles(pw, key, lookup, |
| 783 | _PATH_SSH_SYSTEM_HOSTFILE, |
Ben Lindstrom | a4789ef | 2001-06-25 04:40:49 +0000 | [diff] [blame] | 784 | options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 785 | |
Ben Lindstrom | 83647ce | 2001-06-25 04:30:16 +0000 | [diff] [blame] | 786 | /* backward compat if no key has been found. */ |
| 787 | if (host_status == HOST_NEW) |
| 788 | host_status = check_key_in_hostfiles(pw, key, lookup, |
| 789 | _PATH_SSH_SYSTEM_HOSTFILE2, |
Ben Lindstrom | a4789ef | 2001-06-25 04:40:49 +0000 | [diff] [blame] | 790 | options.ignore_user_known_hosts ? NULL : |
| 791 | _PATH_SSH_USER_HOSTFILE2); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 792 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 793 | return (host_status == HOST_OK); |
| 794 | } |