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