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