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