Darren Tucker | 821d3db | 2009-06-22 16:11:06 +1000 | [diff] [blame] | 1 | /* $OpenBSD: auth2.c,v 1.121 2009/06/22 05:39:28 dtucker Exp $ */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 25 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 26 | #include "includes.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 27 | |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 28 | #include <sys/types.h> |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/uio.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 31 | |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 32 | #include <fcntl.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 33 | #include <pwd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 34 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 35 | #include <string.h> |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 36 | #include <unistd.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 37 | |
Damien Miller | 0b4d48b | 2008-07-05 09:44:53 +1000 | [diff] [blame] | 38 | #include "atomicio.h" |
Darren Tucker | 43e7a35 | 2009-06-21 19:50:08 +1000 | [diff] [blame] | 39 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 41 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 42 | #include "log.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 43 | #include "buffer.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | #include "servconf.h" |
| 45 | #include "compat.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include "key.h" |
| 47 | #include "hostfile.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 48 | #include "auth.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 49 | #include "dispatch.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 50 | #include "pathnames.h" |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 51 | #include "buffer.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 52 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 53 | #ifdef GSSAPI |
| 54 | #include "ssh-gss.h" |
| 55 | #endif |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 56 | #include "monitor_wrap.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 57 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 58 | /* import */ |
| 59 | extern ServerOptions options; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 60 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 61 | extern u_int session_id2_len; |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 62 | extern Buffer loginmsg; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 63 | |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 64 | /* methods */ |
| 65 | |
| 66 | extern Authmethod method_none; |
| 67 | extern Authmethod method_pubkey; |
| 68 | extern Authmethod method_passwd; |
| 69 | extern Authmethod method_kbdint; |
| 70 | extern Authmethod method_hostbased; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 71 | #ifdef GSSAPI |
| 72 | extern Authmethod method_gssapi; |
| 73 | #endif |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 74 | #ifdef JPAKE |
| 75 | extern Authmethod method_jpake; |
| 76 | #endif |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 77 | |
| 78 | Authmethod *authmethods[] = { |
| 79 | &method_none, |
| 80 | &method_pubkey, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 81 | #ifdef GSSAPI |
| 82 | &method_gssapi, |
| 83 | #endif |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 84 | #ifdef JPAKE |
| 85 | &method_jpake, |
| 86 | #endif |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 87 | &method_passwd, |
| 88 | &method_kbdint, |
| 89 | &method_hostbased, |
| 90 | NULL |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 91 | }; |
| 92 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 93 | /* protocol */ |
| 94 | |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 95 | static void input_service_request(int, u_int32_t, void *); |
| 96 | static void input_userauth_request(int, u_int32_t, void *); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 97 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 98 | /* helper */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 99 | static Authmethod *authmethod_lookup(const char *); |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 100 | static char *authmethods_get(void); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 101 | |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 102 | char * |
| 103 | auth2_read_banner(void) |
| 104 | { |
| 105 | struct stat st; |
| 106 | char *banner = NULL; |
| 107 | size_t len, n; |
| 108 | int fd; |
| 109 | |
| 110 | if ((fd = open(options.banner, O_RDONLY)) == -1) |
| 111 | return (NULL); |
| 112 | if (fstat(fd, &st) == -1) { |
| 113 | close(fd); |
| 114 | return (NULL); |
| 115 | } |
| 116 | if (st.st_size > 1*1024*1024) { |
| 117 | close(fd); |
| 118 | return (NULL); |
| 119 | } |
| 120 | |
| 121 | len = (size_t)st.st_size; /* truncate */ |
| 122 | banner = xmalloc(len + 1); |
| 123 | n = atomicio(read, fd, banner, len); |
| 124 | close(fd); |
| 125 | |
| 126 | if (n != len) { |
| 127 | xfree(banner); |
| 128 | return (NULL); |
| 129 | } |
| 130 | banner[n] = '\0'; |
| 131 | |
| 132 | return (banner); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | userauth_send_banner(const char *msg) |
| 137 | { |
| 138 | if (datafellows & SSH_BUG_BANNER) |
| 139 | return; |
| 140 | |
| 141 | packet_start(SSH2_MSG_USERAUTH_BANNER); |
| 142 | packet_put_cstring(msg); |
| 143 | packet_put_cstring(""); /* language, unused */ |
| 144 | packet_send(); |
| 145 | debug("%s: sent", __func__); |
| 146 | } |
| 147 | |
| 148 | static void |
| 149 | userauth_banner(void) |
| 150 | { |
| 151 | char *banner = NULL; |
| 152 | |
| 153 | if (options.banner == NULL || |
| 154 | strcasecmp(options.banner, "none") == 0 || |
| 155 | (datafellows & SSH_BUG_BANNER) != 0) |
| 156 | return; |
| 157 | |
| 158 | if ((banner = PRIVSEP(auth2_read_banner())) == NULL) |
| 159 | goto done; |
| 160 | userauth_send_banner(banner); |
| 161 | |
| 162 | done: |
| 163 | if (banner) |
| 164 | xfree(banner); |
| 165 | } |
| 166 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 167 | /* |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 168 | * loop until authctxt->success == TRUE |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 169 | */ |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 170 | void |
| 171 | do_authentication2(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 172 | { |
Damien Miller | 7d05339 | 2002-01-22 23:24:13 +1100 | [diff] [blame] | 173 | dispatch_init(&dispatch_protocol_error); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 174 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 175 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 176 | } |
| 177 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 178 | /*ARGSUSED*/ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 179 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 180 | input_service_request(int type, u_int32_t seq, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 181 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 182 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 183 | u_int len; |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 184 | int acceptit = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 185 | char *service = packet_get_string(&len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 186 | packet_check_eom(); |
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_service_request: no authctxt"); |
| 190 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 191 | if (strcmp(service, "ssh-userauth") == 0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 192 | if (!authctxt->success) { |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 193 | acceptit = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 194 | /* now we can handle user-auth requests */ |
| 195 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); |
| 196 | } |
| 197 | } |
| 198 | /* XXX all other service requests are denied */ |
| 199 | |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 200 | if (acceptit) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 201 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
| 202 | packet_put_cstring(service); |
| 203 | packet_send(); |
| 204 | packet_write_wait(); |
| 205 | } else { |
| 206 | debug("bad service request %s", service); |
| 207 | packet_disconnect("bad service request %s", service); |
| 208 | } |
| 209 | xfree(service); |
| 210 | } |
| 211 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 212 | /*ARGSUSED*/ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 213 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 214 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 215 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 216 | Authctxt *authctxt = ctxt; |
| 217 | Authmethod *m = NULL; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 218 | char *user, *service, *method, *style = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 219 | int authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 220 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 221 | if (authctxt == NULL) |
| 222 | fatal("input_userauth_request: no authctxt"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 223 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 224 | user = packet_get_string(NULL); |
| 225 | service = packet_get_string(NULL); |
| 226 | method = packet_get_string(NULL); |
| 227 | 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] | 228 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 229 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 230 | if ((style = strchr(user, ':')) != NULL) |
| 231 | *style++ = 0; |
| 232 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 233 | if (authctxt->attempt++ == 0) { |
Damien Miller | 3a5b023 | 2002-03-13 13:19:42 +1100 | [diff] [blame] | 234 | /* setup auth context */ |
Kevin Steves | 205cc1e | 2002-03-22 20:43:05 +0000 | [diff] [blame] | 235 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 236 | authctxt->user = xstrdup(user); |
Kevin Steves | 205cc1e | 2002-03-22 20:43:05 +0000 | [diff] [blame] | 237 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 238 | authctxt->valid = 1; |
| 239 | debug2("input_userauth_request: setting up authctxt for %s", user); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 240 | } else { |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 241 | logit("input_userauth_request: invalid user %s", user); |
Damien Miller | 856f0be | 2003-09-03 07:32:45 +1000 | [diff] [blame] | 242 | authctxt->pw = fakepw(); |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 243 | #ifdef SSH_AUDIT_EVENTS |
| 244 | PRIVSEP(audit_event(SSH_INVALID_USER)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 245 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 246 | } |
Darren Tucker | d3eff2b | 2005-09-24 12:43:51 +1000 | [diff] [blame] | 247 | #ifdef USE_PAM |
| 248 | if (options.use_pam) |
| 249 | PRIVSEP(start_pam(authctxt)); |
| 250 | #endif |
Damien Miller | 30d1f84 | 2004-07-21 20:48:53 +1000 | [diff] [blame] | 251 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 252 | use_privsep ? " [net]" : ""); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 253 | authctxt->service = xstrdup(service); |
Ben Lindstrom | 9d0c066 | 2001-06-09 01:40:00 +0000 | [diff] [blame] | 254 | authctxt->style = style ? xstrdup(style) : NULL; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 255 | if (use_privsep) |
| 256 | mm_inform_authserv(service, style); |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 257 | userauth_banner(); |
Ben Lindstrom | 9d0c066 | 2001-06-09 01:40:00 +0000 | [diff] [blame] | 258 | } else if (strcmp(user, authctxt->user) != 0 || |
| 259 | strcmp(service, authctxt->service) != 0) { |
| 260 | packet_disconnect("Change of username or service not allowed: " |
| 261 | "(%s,%s) -> (%s,%s)", |
| 262 | authctxt->user, authctxt->service, user, service); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 263 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 264 | /* reset state */ |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 265 | auth2_challenge_stop(authctxt); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 266 | #ifdef JPAKE |
| 267 | auth2_jpake_stop(authctxt); |
| 268 | #endif |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 269 | |
| 270 | #ifdef GSSAPI |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 271 | /* XXX move to auth2_gssapi_stop() */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 272 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
| 273 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); |
| 274 | #endif |
| 275 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 276 | authctxt->postponed = 0; |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 277 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 278 | /* try to authenticate user */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 279 | m = authmethod_lookup(method); |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 280 | if (m != NULL && authctxt->failures < options.max_authtries) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 281 | debug2("input_userauth_request: try method %s", method); |
| 282 | authenticated = m->userauth(authctxt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 283 | } |
Damien Miller | 5d57e50 | 2001-03-30 10:48:31 +1000 | [diff] [blame] | 284 | userauth_finish(authctxt, authenticated, method); |
| 285 | |
| 286 | xfree(service); |
| 287 | xfree(user); |
| 288 | xfree(method); |
| 289 | } |
| 290 | |
| 291 | void |
| 292 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) |
| 293 | { |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 294 | char *methods; |
| 295 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 296 | if (!authctxt->valid && authenticated) |
| 297 | fatal("INTERNAL ERROR: authenticated invalid user %s", |
| 298 | authctxt->user); |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 299 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 300 | /* Special handling for root */ |
Damien Miller | 556f931 | 2003-02-24 11:59:26 +1100 | [diff] [blame] | 301 | if (authenticated && authctxt->pw->pw_uid == 0 && |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 302 | !auth_root_allowed(method)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 303 | authenticated = 0; |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 304 | #ifdef SSH_AUDIT_EVENTS |
| 305 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 306 | #endif |
| 307 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 308 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 309 | #ifdef USE_PAM |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 310 | if (options.use_pam && authenticated) { |
| 311 | if (!PRIVSEP(do_pam_account())) { |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 312 | /* if PAM returned a message, send it to the user */ |
| 313 | if (buffer_len(&loginmsg) > 0) { |
| 314 | buffer_append(&loginmsg, "\0", 1); |
| 315 | userauth_send_banner(buffer_ptr(&loginmsg)); |
Darren Tucker | c138667 | 2004-12-03 14:33:47 +1100 | [diff] [blame] | 316 | packet_write_wait(); |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 317 | } |
Darren Tucker | c138667 | 2004-12-03 14:33:47 +1100 | [diff] [blame] | 318 | fatal("Access denied for user %s by PAM account " |
Damien Miller | b6f72f5 | 2005-07-17 17:26:43 +1000 | [diff] [blame] | 319 | "configuration", authctxt->user); |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 320 | } |
| 321 | } |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 322 | #endif |
| 323 | |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 324 | #ifdef _UNICOS |
| 325 | if (authenticated && cray_access_denied(authctxt->user)) { |
| 326 | authenticated = 0; |
| 327 | fatal("Access denied for user %s.",authctxt->user); |
| 328 | } |
| 329 | #endif /* _UNICOS */ |
| 330 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 331 | /* Log before sending the reply */ |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 332 | auth_log(authctxt, authenticated, method, " ssh2"); |
| 333 | |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 334 | if (authctxt->postponed) |
| 335 | return; |
| 336 | |
| 337 | /* XXX todo: check if multiple auth methods are needed */ |
| 338 | if (authenticated == 1) { |
| 339 | /* turn off userauth */ |
Damien Miller | 7d05339 | 2002-01-22 23:24:13 +1100 | [diff] [blame] | 340 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 341 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
| 342 | packet_send(); |
| 343 | packet_write_wait(); |
| 344 | /* now we can break out */ |
| 345 | authctxt->success = 1; |
| 346 | } else { |
Damien Miller | 0b4d48b | 2008-07-05 09:44:53 +1000 | [diff] [blame] | 347 | |
| 348 | /* Allow initial try of "none" auth without failure penalty */ |
| 349 | if (authctxt->attempt > 1 || strcmp(method, "none") != 0) |
| 350 | authctxt->failures++; |
| 351 | if (authctxt->failures >= options.max_authtries) { |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 352 | #ifdef SSH_AUDIT_EVENTS |
| 353 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 354 | #endif |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 355 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 356 | } |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 357 | methods = authmethods_get(); |
| 358 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
| 359 | packet_put_cstring(methods); |
| 360 | packet_put_char(0); /* XXX partial success, unused */ |
| 361 | packet_send(); |
| 362 | packet_write_wait(); |
| 363 | xfree(methods); |
| 364 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 365 | } |
| 366 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 367 | static char * |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 368 | authmethods_get(void) |
| 369 | { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 370 | Buffer b; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 371 | char *list; |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 372 | int i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 373 | |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 374 | buffer_init(&b); |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 375 | for (i = 0; authmethods[i] != NULL; i++) { |
| 376 | if (strcmp(authmethods[i]->name, "none") == 0) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 377 | continue; |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 378 | if (authmethods[i]->enabled != NULL && |
| 379 | *(authmethods[i]->enabled) != 0) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 380 | if (buffer_len(&b) > 0) |
| 381 | buffer_append(&b, ",", 1); |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 382 | buffer_append(&b, authmethods[i]->name, |
| 383 | strlen(authmethods[i]->name)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 384 | } |
| 385 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 386 | buffer_append(&b, "\0", 1); |
| 387 | list = xstrdup(buffer_ptr(&b)); |
| 388 | buffer_free(&b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 389 | return list; |
| 390 | } |
| 391 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 392 | static Authmethod * |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 393 | authmethod_lookup(const char *name) |
| 394 | { |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 395 | int i; |
| 396 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 397 | if (name != NULL) |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 398 | for (i = 0; authmethods[i] != NULL; i++) |
| 399 | if (authmethods[i]->enabled != NULL && |
| 400 | *(authmethods[i]->enabled) != 0 && |
| 401 | strcmp(name, authmethods[i]->name) == 0) |
| 402 | return authmethods[i]; |
| 403 | debug2("Unrecognized authentication method name: %s", |
| 404 | name ? name : "NULL"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 405 | return NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 406 | } |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 407 | |