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