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