djm@openbsd.org | be02d7c | 2019-09-06 04:53:27 +0000 | [diff] [blame] | 1 | /* $OpenBSD: auth2.c,v 1.157 2019/09/06 04:53:27 djm 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> |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 33 | #include <limits.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 34 | #include <pwd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 35 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 36 | #include <string.h> |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 37 | #include <unistd.h> |
djm@openbsd.org | 943d096 | 2019-01-17 04:20:53 +0000 | [diff] [blame] | 38 | #include <time.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 39 | |
djm@openbsd.org | be02d7c | 2019-09-06 04:53:27 +0000 | [diff] [blame] | 40 | #include "stdlib.h" |
Damien Miller | 0b4d48b | 2008-07-05 09:44:53 +1000 | [diff] [blame] | 41 | #include "atomicio.h" |
Darren Tucker | 43e7a35 | 2009-06-21 19:50:08 +1000 | [diff] [blame] | 42 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 43 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 45 | #include "log.h" |
markus@openbsd.org | c7d39ac | 2018-07-09 21:35:50 +0000 | [diff] [blame] | 46 | #include "sshbuf.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 47 | #include "misc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 48 | #include "servconf.h" |
| 49 | #include "compat.h" |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 50 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 51 | #include "hostfile.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 52 | #include "auth.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 53 | #include "dispatch.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 54 | #include "pathnames.h" |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 55 | #include "ssherr.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 56 | #ifdef GSSAPI |
| 57 | #include "ssh-gss.h" |
| 58 | #endif |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 59 | #include "monitor_wrap.h" |
dtucker@openbsd.org | e9d910b | 2018-04-13 03:57:26 +0000 | [diff] [blame] | 60 | #include "digest.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 61 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 62 | /* import */ |
| 63 | extern ServerOptions options; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 64 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 65 | extern u_int session_id2_len; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 66 | extern struct sshbuf *loginmsg; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 67 | |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 68 | /* methods */ |
| 69 | |
| 70 | extern Authmethod method_none; |
| 71 | extern Authmethod method_pubkey; |
| 72 | extern Authmethod method_passwd; |
| 73 | extern Authmethod method_kbdint; |
| 74 | extern Authmethod method_hostbased; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 75 | #ifdef GSSAPI |
| 76 | extern Authmethod method_gssapi; |
| 77 | #endif |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 78 | |
| 79 | Authmethod *authmethods[] = { |
| 80 | &method_none, |
| 81 | &method_pubkey, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 82 | #ifdef GSSAPI |
| 83 | &method_gssapi, |
| 84 | #endif |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 85 | &method_passwd, |
| 86 | &method_kbdint, |
| 87 | &method_hostbased, |
| 88 | NULL |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 89 | }; |
| 90 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 91 | /* protocol */ |
| 92 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 93 | static int input_service_request(int, u_int32_t, struct ssh *); |
| 94 | static int input_userauth_request(int, u_int32_t, struct ssh *); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 95 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 96 | /* helper */ |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 97 | static Authmethod *authmethod_lookup(Authctxt *, const char *); |
| 98 | static char *authmethods_get(Authctxt *authctxt); |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 99 | |
| 100 | #define MATCH_NONE 0 /* method or submethod mismatch */ |
| 101 | #define MATCH_METHOD 1 /* method matches (no submethod specified) */ |
| 102 | #define MATCH_BOTH 2 /* method and submethod match */ |
| 103 | #define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */ |
| 104 | static int list_starts_with(const char *, const char *, const char *); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 105 | |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 106 | char * |
| 107 | auth2_read_banner(void) |
| 108 | { |
| 109 | struct stat st; |
| 110 | char *banner = NULL; |
| 111 | size_t len, n; |
| 112 | int fd; |
| 113 | |
| 114 | if ((fd = open(options.banner, O_RDONLY)) == -1) |
| 115 | return (NULL); |
| 116 | if (fstat(fd, &st) == -1) { |
| 117 | close(fd); |
| 118 | return (NULL); |
| 119 | } |
Damien Miller | 8ed4de8 | 2011-12-19 10:52:50 +1100 | [diff] [blame] | 120 | if (st.st_size <= 0 || st.st_size > 1*1024*1024) { |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 121 | close(fd); |
| 122 | return (NULL); |
| 123 | } |
| 124 | |
| 125 | len = (size_t)st.st_size; /* truncate */ |
| 126 | banner = xmalloc(len + 1); |
| 127 | n = atomicio(read, fd, banner, len); |
| 128 | close(fd); |
| 129 | |
| 130 | if (n != len) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 131 | free(banner); |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 132 | return (NULL); |
| 133 | } |
| 134 | banner[n] = '\0'; |
| 135 | |
| 136 | return (banner); |
| 137 | } |
| 138 | |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 139 | static void |
| 140 | userauth_send_banner(struct ssh *ssh, const char *msg) |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 141 | { |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 142 | int r; |
| 143 | |
| 144 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_BANNER)) != 0 || |
| 145 | (r = sshpkt_put_cstring(ssh, msg)) != 0 || |
| 146 | (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language, unused */ |
| 147 | (r = sshpkt_send(ssh)) != 0) |
| 148 | fatal("%s: %s", __func__, ssh_err(r)); |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 149 | debug("%s: sent", __func__); |
| 150 | } |
| 151 | |
| 152 | static void |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 153 | userauth_banner(struct ssh *ssh) |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 154 | { |
| 155 | char *banner = NULL; |
| 156 | |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 157 | if (options.banner == NULL) |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 158 | return; |
| 159 | |
| 160 | if ((banner = PRIVSEP(auth2_read_banner())) == NULL) |
| 161 | goto done; |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 162 | userauth_send_banner(ssh, banner); |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 163 | |
| 164 | done: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 165 | free(banner); |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 166 | } |
| 167 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 168 | /* |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 169 | * loop until authctxt->success == TRUE |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 170 | */ |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 171 | void |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 172 | do_authentication2(struct ssh *ssh) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 173 | { |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 174 | Authctxt *authctxt = ssh->authctxt; |
| 175 | |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 176 | ssh_dispatch_init(ssh, &dispatch_protocol_error); |
| 177 | ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
markus@openbsd.org | 92e9fe6 | 2017-05-31 07:00:13 +0000 | [diff] [blame] | 178 | ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success); |
markus@openbsd.org | 5f4082d | 2017-05-30 14:18:15 +0000 | [diff] [blame] | 179 | ssh->authctxt = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 180 | } |
| 181 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 182 | /*ARGSUSED*/ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 183 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 184 | input_service_request(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 185 | { |
markus@openbsd.org | 5f4082d | 2017-05-30 14:18:15 +0000 | [diff] [blame] | 186 | Authctxt *authctxt = ssh->authctxt; |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 187 | char *service = NULL; |
| 188 | int r, acceptit = 0; |
| 189 | |
| 190 | if ((r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 || |
| 191 | (r = sshpkt_get_end(ssh)) != 0) |
| 192 | goto out; |
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 | if (authctxt == NULL) |
| 195 | fatal("input_service_request: no authctxt"); |
| 196 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 197 | if (strcmp(service, "ssh-userauth") == 0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 198 | if (!authctxt->success) { |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 199 | acceptit = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 200 | /* now we can handle user-auth requests */ |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 201 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST, |
| 202 | &input_userauth_request); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | /* XXX all other service requests are denied */ |
| 206 | |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 207 | if (acceptit) { |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 208 | if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_ACCEPT)) != 0 || |
| 209 | (r = sshpkt_put_cstring(ssh, service)) != 0 || |
| 210 | (r = sshpkt_send(ssh)) != 0 || |
| 211 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 212 | goto out; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 213 | } else { |
| 214 | debug("bad service request %s", service); |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 215 | ssh_packet_disconnect(ssh, "bad service request %s", service); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 216 | } |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 217 | r = 0; |
| 218 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 219 | free(service); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 220 | return 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 221 | } |
| 222 | |
dtucker@openbsd.org | e9d910b | 2018-04-13 03:57:26 +0000 | [diff] [blame] | 223 | #define MIN_FAIL_DELAY_SECONDS 0.005 |
| 224 | static double |
| 225 | user_specific_delay(const char *user) |
| 226 | { |
| 227 | char b[512]; |
| 228 | size_t len = ssh_digest_bytes(SSH_DIGEST_SHA512); |
| 229 | u_char *hash = xmalloc(len); |
| 230 | double delay; |
| 231 | |
dtucker@openbsd.org | fdba503 | 2018-05-11 03:22:55 +0000 | [diff] [blame] | 232 | (void)snprintf(b, sizeof b, "%llu%s", |
| 233 | (unsigned long long)options.timing_secret, user); |
dtucker@openbsd.org | e9d910b | 2018-04-13 03:57:26 +0000 | [diff] [blame] | 234 | if (ssh_digest_memory(SSH_DIGEST_SHA512, b, strlen(b), hash, len) != 0) |
| 235 | fatal("%s: ssh_digest_memory", __func__); |
| 236 | /* 0-4.2 ms of delay */ |
| 237 | delay = (double)PEEK_U32(hash) / 1000 / 1000 / 1000 / 1000; |
| 238 | freezero(hash, len); |
| 239 | debug3("%s: user specific delay %0.3lfms", __func__, delay/1000); |
| 240 | return MIN_FAIL_DELAY_SECONDS + delay; |
| 241 | } |
| 242 | |
| 243 | static void |
| 244 | ensure_minimum_time_since(double start, double seconds) |
| 245 | { |
| 246 | struct timespec ts; |
| 247 | double elapsed = monotime_double() - start, req = seconds, remain; |
| 248 | |
| 249 | /* if we've already passed the requested time, scale up */ |
| 250 | while ((remain = seconds - elapsed) < 0.0) |
| 251 | seconds *= 2; |
| 252 | |
| 253 | ts.tv_sec = remain; |
| 254 | ts.tv_nsec = (remain - ts.tv_sec) * 1000000000; |
| 255 | debug3("%s: elapsed %0.3lfms, delaying %0.3lfms (requested %0.3lfms)", |
| 256 | __func__, elapsed*1000, remain*1000, req*1000); |
| 257 | nanosleep(&ts, NULL); |
| 258 | } |
| 259 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 260 | /*ARGSUSED*/ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 261 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 262 | input_userauth_request(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 263 | { |
markus@openbsd.org | 5f4082d | 2017-05-30 14:18:15 +0000 | [diff] [blame] | 264 | Authctxt *authctxt = ssh->authctxt; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 265 | Authmethod *m = NULL; |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 266 | char *user = NULL, *service = NULL, *method = NULL, *style = NULL; |
| 267 | int r, authenticated = 0; |
dtucker@openbsd.org | e9d910b | 2018-04-13 03:57:26 +0000 | [diff] [blame] | 268 | double tstart = monotime_double(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 269 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 270 | if (authctxt == NULL) |
| 271 | fatal("input_userauth_request: no authctxt"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 272 | |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 273 | if ((r = sshpkt_get_cstring(ssh, &user, NULL)) != 0 || |
| 274 | (r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 || |
| 275 | (r = sshpkt_get_cstring(ssh, &method, NULL)) != 0) |
| 276 | goto out; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 277 | 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] | 278 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 279 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 280 | if ((style = strchr(user, ':')) != NULL) |
| 281 | *style++ = 0; |
| 282 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 283 | if (authctxt->attempt++ == 0) { |
Damien Miller | 3a5b023 | 2002-03-13 13:19:42 +1100 | [diff] [blame] | 284 | /* setup auth context */ |
djm@openbsd.org | 3a00a92 | 2019-01-19 21:41:18 +0000 | [diff] [blame] | 285 | authctxt->pw = PRIVSEP(getpwnamallow(ssh, user)); |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 286 | authctxt->user = xstrdup(user); |
Kevin Steves | 205cc1e | 2002-03-22 20:43:05 +0000 | [diff] [blame] | 287 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 288 | authctxt->valid = 1; |
djm@openbsd.org | f1a1934 | 2017-02-03 23:05:57 +0000 | [diff] [blame] | 289 | debug2("%s: setting up authctxt for %s", |
| 290 | __func__, user); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 291 | } else { |
djm@openbsd.org | f1a1934 | 2017-02-03 23:05:57 +0000 | [diff] [blame] | 292 | /* Invalid user, fake password information */ |
Damien Miller | 856f0be | 2003-09-03 07:32:45 +1000 | [diff] [blame] | 293 | authctxt->pw = fakepw(); |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 294 | #ifdef SSH_AUDIT_EVENTS |
Damien Miller | 9b655dc | 2019-01-20 14:55:27 +1100 | [diff] [blame] | 295 | PRIVSEP(audit_event(ssh, SSH_INVALID_USER)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 296 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 297 | } |
Darren Tucker | d3eff2b | 2005-09-24 12:43:51 +1000 | [diff] [blame] | 298 | #ifdef USE_PAM |
| 299 | if (options.use_pam) |
Damien Miller | 3f0786b | 2019-01-20 10:22:18 +1100 | [diff] [blame] | 300 | PRIVSEP(start_pam(ssh)); |
Darren Tucker | d3eff2b | 2005-09-24 12:43:51 +1000 | [diff] [blame] | 301 | #endif |
djm@openbsd.org | f1a1934 | 2017-02-03 23:05:57 +0000 | [diff] [blame] | 302 | ssh_packet_set_log_preamble(ssh, "%suser %s", |
| 303 | authctxt->valid ? "authenticating " : "invalid ", user); |
Damien Miller | 30d1f84 | 2004-07-21 20:48:53 +1000 | [diff] [blame] | 304 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 305 | use_privsep ? " [net]" : ""); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 306 | authctxt->service = xstrdup(service); |
Ben Lindstrom | 9d0c066 | 2001-06-09 01:40:00 +0000 | [diff] [blame] | 307 | authctxt->style = style ? xstrdup(style) : NULL; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 308 | if (use_privsep) |
| 309 | mm_inform_authserv(service, style); |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 310 | userauth_banner(ssh); |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 311 | if (auth2_setup_methods_lists(authctxt) != 0) |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 312 | ssh_packet_disconnect(ssh, |
| 313 | "no authentication methods enabled"); |
Ben Lindstrom | 9d0c066 | 2001-06-09 01:40:00 +0000 | [diff] [blame] | 314 | } else if (strcmp(user, authctxt->user) != 0 || |
| 315 | strcmp(service, authctxt->service) != 0) { |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 316 | ssh_packet_disconnect(ssh, "Change of username or service " |
| 317 | "not allowed: (%s,%s) -> (%s,%s)", |
Ben Lindstrom | 9d0c066 | 2001-06-09 01:40:00 +0000 | [diff] [blame] | 318 | authctxt->user, authctxt->service, user, service); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 319 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 320 | /* reset state */ |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 321 | auth2_challenge_stop(ssh); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 322 | |
| 323 | #ifdef GSSAPI |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 324 | /* XXX move to auth2_gssapi_stop() */ |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 325 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
| 326 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 327 | #endif |
| 328 | |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 329 | auth2_authctxt_reset_info(authctxt); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 330 | authctxt->postponed = 0; |
Damien Miller | 3fcdfd5 | 2011-05-05 14:04:11 +1000 | [diff] [blame] | 331 | authctxt->server_caused_failure = 0; |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 332 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 333 | /* try to authenticate user */ |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 334 | m = authmethod_lookup(authctxt, method); |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 335 | if (m != NULL && authctxt->failures < options.max_authtries) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 336 | debug2("input_userauth_request: try method %s", method); |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 337 | authenticated = m->userauth(ssh); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 338 | } |
dtucker@openbsd.org | e9d910b | 2018-04-13 03:57:26 +0000 | [diff] [blame] | 339 | if (!authctxt->authenticated) |
| 340 | ensure_minimum_time_since(tstart, |
| 341 | user_specific_delay(authctxt->user)); |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 342 | userauth_finish(ssh, authenticated, method, NULL); |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 343 | r = 0; |
| 344 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 345 | free(service); |
| 346 | free(user); |
| 347 | free(method); |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 348 | return r; |
Damien Miller | 5d57e50 | 2001-03-30 10:48:31 +1000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 352 | userauth_finish(struct ssh *ssh, int authenticated, const char *method, |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 353 | const char *submethod) |
Damien Miller | 5d57e50 | 2001-03-30 10:48:31 +1000 | [diff] [blame] | 354 | { |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 355 | Authctxt *authctxt = ssh->authctxt; |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 356 | char *methods; |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 357 | int r, partial = 0; |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 358 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 359 | if (!authctxt->valid && authenticated) |
| 360 | fatal("INTERNAL ERROR: authenticated invalid user %s", |
| 361 | authctxt->user); |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 362 | if (authenticated && authctxt->postponed) |
| 363 | fatal("INTERNAL ERROR: authenticated and postponed"); |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 364 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 365 | /* Special handling for root */ |
Damien Miller | 556f931 | 2003-02-24 11:59:26 +1100 | [diff] [blame] | 366 | if (authenticated && authctxt->pw->pw_uid == 0 && |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 367 | !auth_root_allowed(ssh, method)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 368 | authenticated = 0; |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 369 | #ifdef SSH_AUDIT_EVENTS |
Damien Miller | 9b655dc | 2019-01-20 14:55:27 +1100 | [diff] [blame] | 370 | PRIVSEP(audit_event(ssh, SSH_LOGIN_ROOT_DENIED)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 371 | #endif |
| 372 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 373 | |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 374 | if (authenticated && options.num_auth_methods != 0) { |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 375 | if (!auth2_update_methods_lists(authctxt, method, submethod)) { |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 376 | authenticated = 0; |
| 377 | partial = 1; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /* Log before sending the reply */ |
djm@openbsd.org | 3a00a92 | 2019-01-19 21:41:18 +0000 | [diff] [blame] | 382 | auth_log(ssh, authenticated, partial, method, submethod); |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 383 | |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 384 | /* Update information exposed to session */ |
| 385 | if (authenticated || partial) |
| 386 | auth2_update_session_info(authctxt, method, submethod); |
| 387 | |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 388 | if (authctxt->postponed) |
| 389 | return; |
| 390 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 391 | #ifdef USE_PAM |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 392 | if (options.use_pam && authenticated) { |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 393 | int r; |
| 394 | |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 395 | if (!PRIVSEP(do_pam_account())) { |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 396 | /* if PAM returned a message, send it to the user */ |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 397 | if (sshbuf_len(loginmsg) > 0) { |
| 398 | if ((r = sshbuf_put(loginmsg, "\0", 1)) != 0) |
| 399 | fatal("%s: buffer error: %s", |
| 400 | __func__, ssh_err(r)); |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 401 | userauth_send_banner(ssh, sshbuf_ptr(loginmsg)); |
Damien Miller | 08f66d9 | 2019-01-20 09:58:45 +1100 | [diff] [blame] | 402 | if ((r = ssh_packet_write_wait(ssh)) != 0) { |
| 403 | sshpkt_fatal(ssh, r, |
| 404 | "%s: send PAM banner", __func__); |
| 405 | } |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 406 | } |
Darren Tucker | c138667 | 2004-12-03 14:33:47 +1100 | [diff] [blame] | 407 | fatal("Access denied for user %s by PAM account " |
Damien Miller | b6f72f5 | 2005-07-17 17:26:43 +1000 | [diff] [blame] | 408 | "configuration", authctxt->user); |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 409 | } |
| 410 | } |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 411 | #endif |
| 412 | |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 413 | if (authenticated == 1) { |
| 414 | /* turn off userauth */ |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 415 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST, |
| 416 | &dispatch_protocol_ignore); |
| 417 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_SUCCESS)) != 0 || |
| 418 | (r = sshpkt_send(ssh)) != 0 || |
| 419 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 420 | fatal("%s: %s", __func__, ssh_err(r)); |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 421 | /* now we can break out */ |
| 422 | authctxt->success = 1; |
djm@openbsd.org | f1a1934 | 2017-02-03 23:05:57 +0000 | [diff] [blame] | 423 | ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user); |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 424 | } else { |
Damien Miller | 0b4d48b | 2008-07-05 09:44:53 +1000 | [diff] [blame] | 425 | /* Allow initial try of "none" auth without failure penalty */ |
djm@openbsd.org | 058f839 | 2014-12-18 23:58:04 +0000 | [diff] [blame] | 426 | if (!partial && !authctxt->server_caused_failure && |
Damien Miller | 3fcdfd5 | 2011-05-05 14:04:11 +1000 | [diff] [blame] | 427 | (authctxt->attempt > 1 || strcmp(method, "none") != 0)) |
Damien Miller | 0b4d48b | 2008-07-05 09:44:53 +1000 | [diff] [blame] | 428 | authctxt->failures++; |
| 429 | if (authctxt->failures >= options.max_authtries) { |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 430 | #ifdef SSH_AUDIT_EVENTS |
Damien Miller | 9b655dc | 2019-01-20 14:55:27 +1100 | [diff] [blame] | 431 | PRIVSEP(audit_event(ssh, SSH_LOGIN_EXCEED_MAXTRIES)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 432 | #endif |
djm@openbsd.org | 3a00a92 | 2019-01-19 21:41:18 +0000 | [diff] [blame] | 433 | auth_maxtries_exceeded(ssh); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 434 | } |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 435 | methods = authmethods_get(authctxt); |
| 436 | debug3("%s: failure partial=%d next methods=\"%s\"", __func__, |
| 437 | partial, methods); |
Damien Miller | 5ebce13 | 2019-01-20 09:44:53 +1100 | [diff] [blame] | 438 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_FAILURE)) != 0 || |
| 439 | (r = sshpkt_put_cstring(ssh, methods)) != 0 || |
| 440 | (r = sshpkt_put_u8(ssh, partial)) != 0 || |
| 441 | (r = sshpkt_send(ssh)) != 0 || |
| 442 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 443 | fatal("%s: %s", __func__, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 444 | free(methods); |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 445 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 446 | } |
| 447 | |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 448 | /* |
| 449 | * Checks whether method is allowed by at least one AuthenticationMethods |
| 450 | * methods list. Returns 1 if allowed, or no methods lists configured. |
| 451 | * 0 otherwise. |
| 452 | */ |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 453 | int |
| 454 | auth2_method_allowed(Authctxt *authctxt, const char *method, |
| 455 | const char *submethod) |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 456 | { |
| 457 | u_int i; |
| 458 | |
| 459 | /* |
| 460 | * NB. authctxt->num_auth_methods might be zero as a result of |
| 461 | * auth2_setup_methods_lists(), so check the configuration. |
| 462 | */ |
| 463 | if (options.num_auth_methods == 0) |
| 464 | return 1; |
| 465 | for (i = 0; i < authctxt->num_auth_methods; i++) { |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 466 | if (list_starts_with(authctxt->auth_methods[i], method, |
| 467 | submethod) != MATCH_NONE) |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 468 | return 1; |
| 469 | } |
| 470 | return 0; |
| 471 | } |
| 472 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 473 | static char * |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 474 | authmethods_get(Authctxt *authctxt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 475 | { |
markus@openbsd.org | c7d39ac | 2018-07-09 21:35:50 +0000 | [diff] [blame] | 476 | struct sshbuf *b; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 477 | char *list; |
markus@openbsd.org | c7d39ac | 2018-07-09 21:35:50 +0000 | [diff] [blame] | 478 | int i, r; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 479 | |
markus@openbsd.org | c7d39ac | 2018-07-09 21:35:50 +0000 | [diff] [blame] | 480 | if ((b = sshbuf_new()) == NULL) |
| 481 | fatal("%s: sshbuf_new failed", __func__); |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 482 | for (i = 0; authmethods[i] != NULL; i++) { |
| 483 | if (strcmp(authmethods[i]->name, "none") == 0) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 484 | continue; |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 485 | if (authmethods[i]->enabled == NULL || |
| 486 | *(authmethods[i]->enabled) == 0) |
| 487 | continue; |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 488 | if (!auth2_method_allowed(authctxt, authmethods[i]->name, |
| 489 | NULL)) |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 490 | continue; |
markus@openbsd.org | c7d39ac | 2018-07-09 21:35:50 +0000 | [diff] [blame] | 491 | if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) ? "," : "", |
| 492 | authmethods[i]->name)) != 0) |
| 493 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 494 | } |
markus@openbsd.org | c7d39ac | 2018-07-09 21:35:50 +0000 | [diff] [blame] | 495 | if ((list = sshbuf_dup_string(b)) == NULL) |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 496 | fatal("%s: sshbuf_dup_string failed", __func__); |
markus@openbsd.org | c7d39ac | 2018-07-09 21:35:50 +0000 | [diff] [blame] | 497 | sshbuf_free(b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 498 | return list; |
| 499 | } |
| 500 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 501 | static Authmethod * |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 502 | authmethod_lookup(Authctxt *authctxt, const char *name) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 503 | { |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 504 | int i; |
| 505 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 506 | if (name != NULL) |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 507 | for (i = 0; authmethods[i] != NULL; i++) |
| 508 | if (authmethods[i]->enabled != NULL && |
| 509 | *(authmethods[i]->enabled) != 0 && |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 510 | strcmp(name, authmethods[i]->name) == 0 && |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 511 | auth2_method_allowed(authctxt, |
| 512 | authmethods[i]->name, NULL)) |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 513 | return authmethods[i]; |
| 514 | debug2("Unrecognized authentication method name: %s", |
| 515 | name ? name : "NULL"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 516 | return NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 517 | } |
Darren Tucker | 4230a5d | 2008-07-02 22:56:09 +1000 | [diff] [blame] | 518 | |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 519 | /* |
| 520 | * Check a comma-separated list of methods for validity. Is need_enable is |
| 521 | * non-zero, then also require that the methods are enabled. |
| 522 | * Returns 0 on success or -1 if the methods list is invalid. |
| 523 | */ |
| 524 | int |
| 525 | auth2_methods_valid(const char *_methods, int need_enable) |
| 526 | { |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 527 | char *methods, *omethods, *method, *p; |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 528 | u_int i, found; |
| 529 | int ret = -1; |
| 530 | |
| 531 | if (*_methods == '\0') { |
| 532 | error("empty authentication method list"); |
| 533 | return -1; |
| 534 | } |
| 535 | omethods = methods = xstrdup(_methods); |
| 536 | while ((method = strsep(&methods, ",")) != NULL) { |
| 537 | for (found = i = 0; !found && authmethods[i] != NULL; i++) { |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 538 | if ((p = strchr(method, ':')) != NULL) |
| 539 | *p = '\0'; |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 540 | if (strcmp(method, authmethods[i]->name) != 0) |
| 541 | continue; |
| 542 | if (need_enable) { |
| 543 | if (authmethods[i]->enabled == NULL || |
| 544 | *(authmethods[i]->enabled) == 0) { |
| 545 | error("Disabled method \"%s\" in " |
| 546 | "AuthenticationMethods list \"%s\"", |
| 547 | method, _methods); |
| 548 | goto out; |
| 549 | } |
| 550 | } |
| 551 | found = 1; |
| 552 | break; |
| 553 | } |
| 554 | if (!found) { |
| 555 | error("Unknown authentication method \"%s\" in list", |
| 556 | method); |
| 557 | goto out; |
| 558 | } |
| 559 | } |
| 560 | ret = 0; |
| 561 | out: |
| 562 | free(omethods); |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Prune the AuthenticationMethods supplied in the configuration, removing |
| 568 | * any methods lists that include disabled methods. Note that this might |
| 569 | * leave authctxt->num_auth_methods == 0, even when multiple required auth |
| 570 | * has been requested. For this reason, all tests for whether multiple is |
| 571 | * enabled should consult options.num_auth_methods directly. |
| 572 | */ |
| 573 | int |
| 574 | auth2_setup_methods_lists(Authctxt *authctxt) |
| 575 | { |
| 576 | u_int i; |
| 577 | |
djm@openbsd.org | 4f0019a | 2019-03-25 22:34:52 +0000 | [diff] [blame] | 578 | /* First, normalise away the "any" pseudo-method */ |
| 579 | if (options.num_auth_methods == 1 && |
| 580 | strcmp(options.auth_methods[0], "any") == 0) { |
| 581 | free(options.auth_methods[0]); |
| 582 | options.auth_methods[0] = NULL; |
| 583 | options.num_auth_methods = 0; |
| 584 | } |
| 585 | |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 586 | if (options.num_auth_methods == 0) |
| 587 | return 0; |
| 588 | debug3("%s: checking methods", __func__); |
| 589 | authctxt->auth_methods = xcalloc(options.num_auth_methods, |
| 590 | sizeof(*authctxt->auth_methods)); |
| 591 | authctxt->num_auth_methods = 0; |
| 592 | for (i = 0; i < options.num_auth_methods; i++) { |
| 593 | if (auth2_methods_valid(options.auth_methods[i], 1) != 0) { |
| 594 | logit("Authentication methods list \"%s\" contains " |
| 595 | "disabled method, skipping", |
| 596 | options.auth_methods[i]); |
| 597 | continue; |
| 598 | } |
| 599 | debug("authentication methods list %d: %s", |
| 600 | authctxt->num_auth_methods, options.auth_methods[i]); |
| 601 | authctxt->auth_methods[authctxt->num_auth_methods++] = |
| 602 | xstrdup(options.auth_methods[i]); |
| 603 | } |
| 604 | if (authctxt->num_auth_methods == 0) { |
| 605 | error("No AuthenticationMethods left after eliminating " |
| 606 | "disabled methods"); |
| 607 | return -1; |
| 608 | } |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static int |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 613 | list_starts_with(const char *methods, const char *method, |
| 614 | const char *submethod) |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 615 | { |
| 616 | size_t l = strlen(method); |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 617 | int match; |
| 618 | const char *p; |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 619 | |
| 620 | if (strncmp(methods, method, l) != 0) |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 621 | return MATCH_NONE; |
| 622 | p = methods + l; |
| 623 | match = MATCH_METHOD; |
| 624 | if (*p == ':') { |
| 625 | if (!submethod) |
| 626 | return MATCH_PARTIAL; |
| 627 | l = strlen(submethod); |
| 628 | p += 1; |
| 629 | if (strncmp(submethod, p, l)) |
| 630 | return MATCH_NONE; |
| 631 | p += l; |
| 632 | match = MATCH_BOTH; |
| 633 | } |
| 634 | if (*p != ',' && *p != '\0') |
| 635 | return MATCH_NONE; |
| 636 | return match; |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /* |
| 640 | * Remove method from the start of a comma-separated list of methods. |
| 641 | * Returns 0 if the list of methods did not start with that method or 1 |
| 642 | * if it did. |
| 643 | */ |
| 644 | static int |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 645 | remove_method(char **methods, const char *method, const char *submethod) |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 646 | { |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 647 | char *omethods = *methods, *p; |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 648 | size_t l = strlen(method); |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 649 | int match; |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 650 | |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 651 | match = list_starts_with(omethods, method, submethod); |
| 652 | if (match != MATCH_METHOD && match != MATCH_BOTH) |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 653 | return 0; |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 654 | p = omethods + l; |
| 655 | if (submethod && match == MATCH_BOTH) |
| 656 | p += 1 + strlen(submethod); /* include colon */ |
| 657 | if (*p == ',') |
| 658 | p++; |
| 659 | *methods = xstrdup(p); |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 660 | free(omethods); |
| 661 | return 1; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Called after successful authentication. Will remove the successful method |
| 666 | * from the start of each list in which it occurs. If it was the last method |
| 667 | * in any list, then authentication is deemed successful. |
| 668 | * Returns 1 if the method completed any authentication list or 0 otherwise. |
| 669 | */ |
| 670 | int |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 671 | auth2_update_methods_lists(Authctxt *authctxt, const char *method, |
| 672 | const char *submethod) |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 673 | { |
| 674 | u_int i, found = 0; |
| 675 | |
| 676 | debug3("%s: updating methods list after \"%s\"", __func__, method); |
| 677 | for (i = 0; i < authctxt->num_auth_methods; i++) { |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 678 | if (!remove_method(&(authctxt->auth_methods[i]), method, |
| 679 | submethod)) |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 680 | continue; |
| 681 | found = 1; |
| 682 | if (*authctxt->auth_methods[i] == '\0') { |
| 683 | debug2("authentication methods list %d complete", i); |
| 684 | return 1; |
| 685 | } |
| 686 | debug3("authentication methods list %d remaining: \"%s\"", |
| 687 | i, authctxt->auth_methods[i]); |
| 688 | } |
| 689 | /* This should not happen, but would be bad if it did */ |
| 690 | if (!found) |
| 691 | fatal("%s: method not in AuthenticationMethods", __func__); |
| 692 | return 0; |
| 693 | } |
| 694 | |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 695 | /* Reset method-specific information */ |
| 696 | void auth2_authctxt_reset_info(Authctxt *authctxt) |
| 697 | { |
| 698 | sshkey_free(authctxt->auth_method_key); |
| 699 | free(authctxt->auth_method_info); |
| 700 | authctxt->auth_method_key = NULL; |
| 701 | authctxt->auth_method_info = NULL; |
| 702 | } |
| 703 | |
| 704 | /* Record auth method-specific information for logs */ |
| 705 | void |
| 706 | auth2_record_info(Authctxt *authctxt, const char *fmt, ...) |
| 707 | { |
| 708 | va_list ap; |
| 709 | int i; |
| 710 | |
| 711 | free(authctxt->auth_method_info); |
| 712 | authctxt->auth_method_info = NULL; |
| 713 | |
| 714 | va_start(ap, fmt); |
| 715 | i = vasprintf(&authctxt->auth_method_info, fmt, ap); |
| 716 | va_end(ap); |
| 717 | |
deraadt@openbsd.org | e8c9740 | 2019-06-28 05:44:09 +0000 | [diff] [blame] | 718 | if (i == -1) |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 719 | fatal("%s: vasprintf failed", __func__); |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * Records a public key used in authentication. This is used for logging |
| 724 | * and to ensure that the same key is not subsequently accepted again for |
| 725 | * multiple authentication. |
| 726 | */ |
| 727 | void |
| 728 | auth2_record_key(Authctxt *authctxt, int authenticated, |
| 729 | const struct sshkey *key) |
| 730 | { |
| 731 | struct sshkey **tmp, *dup; |
| 732 | int r; |
| 733 | |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 734 | if ((r = sshkey_from_private(key, &dup)) != 0) |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 735 | fatal("%s: copy key: %s", __func__, ssh_err(r)); |
| 736 | sshkey_free(authctxt->auth_method_key); |
| 737 | authctxt->auth_method_key = dup; |
| 738 | |
| 739 | if (!authenticated) |
| 740 | return; |
| 741 | |
| 742 | /* If authenticated, make sure we don't accept this key again */ |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 743 | if ((r = sshkey_from_private(key, &dup)) != 0) |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 744 | fatal("%s: copy key: %s", __func__, ssh_err(r)); |
| 745 | if (authctxt->nprev_keys >= INT_MAX || |
| 746 | (tmp = recallocarray(authctxt->prev_keys, authctxt->nprev_keys, |
| 747 | authctxt->nprev_keys + 1, sizeof(*authctxt->prev_keys))) == NULL) |
| 748 | fatal("%s: reallocarray failed", __func__); |
| 749 | authctxt->prev_keys = tmp; |
| 750 | authctxt->prev_keys[authctxt->nprev_keys] = dup; |
| 751 | authctxt->nprev_keys++; |
| 752 | |
| 753 | } |
| 754 | |
| 755 | /* Checks whether a key has already been previously used for authentication */ |
| 756 | int |
| 757 | auth2_key_already_used(Authctxt *authctxt, const struct sshkey *key) |
| 758 | { |
| 759 | u_int i; |
| 760 | char *fp; |
| 761 | |
| 762 | for (i = 0; i < authctxt->nprev_keys; i++) { |
| 763 | if (sshkey_equal_public(key, authctxt->prev_keys[i])) { |
| 764 | fp = sshkey_fingerprint(authctxt->prev_keys[i], |
| 765 | options.fingerprint_hash, SSH_FP_DEFAULT); |
| 766 | debug3("%s: key already used: %s %s", __func__, |
| 767 | sshkey_type(authctxt->prev_keys[i]), |
| 768 | fp == NULL ? "UNKNOWN" : fp); |
| 769 | free(fp); |
| 770 | return 1; |
| 771 | } |
| 772 | } |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Updates authctxt->session_info with details of authentication. Should be |
| 778 | * whenever an authentication method succeeds. |
| 779 | */ |
| 780 | void |
| 781 | auth2_update_session_info(Authctxt *authctxt, const char *method, |
| 782 | const char *submethod) |
| 783 | { |
| 784 | int r; |
| 785 | |
| 786 | if (authctxt->session_info == NULL) { |
| 787 | if ((authctxt->session_info = sshbuf_new()) == NULL) |
| 788 | fatal("%s: sshbuf_new", __func__); |
| 789 | } |
| 790 | |
| 791 | /* Append method[/submethod] */ |
| 792 | if ((r = sshbuf_putf(authctxt->session_info, "%s%s%s", |
| 793 | method, submethod == NULL ? "" : "/", |
| 794 | submethod == NULL ? "" : submethod)) != 0) |
| 795 | fatal("%s: append method: %s", __func__, ssh_err(r)); |
| 796 | |
| 797 | /* Append key if present */ |
| 798 | if (authctxt->auth_method_key != NULL) { |
| 799 | if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 || |
| 800 | (r = sshkey_format_text(authctxt->auth_method_key, |
| 801 | authctxt->session_info)) != 0) |
| 802 | fatal("%s: append key: %s", __func__, ssh_err(r)); |
| 803 | } |
| 804 | |
| 805 | if (authctxt->auth_method_info != NULL) { |
| 806 | /* Ensure no ambiguity here */ |
| 807 | if (strchr(authctxt->auth_method_info, '\n') != NULL) |
| 808 | fatal("%s: auth_method_info contains \\n", __func__); |
| 809 | if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 || |
| 810 | (r = sshbuf_putf(authctxt->session_info, "%s", |
| 811 | authctxt->auth_method_info)) != 0) { |
| 812 | fatal("%s: append method info: %s", |
| 813 | __func__, ssh_err(r)); |
| 814 | } |
| 815 | } |
| 816 | if ((r = sshbuf_put_u8(authctxt->session_info, '\n')) != 0) |
| 817 | fatal("%s: append: %s", __func__, ssh_err(r)); |
| 818 | } |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 819 | |