Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 1 | /* $OpenBSD: auth2.c,v 1.113 2006/08/03 03:34:41 deraadt 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> |
| 29 | |
| 30 | #include <pwd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 31 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 32 | #include <string.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 33 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 34 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 35 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 36 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 37 | #include "log.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 38 | #include "buffer.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 39 | #include "servconf.h" |
| 40 | #include "compat.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 41 | #include "key.h" |
| 42 | #include "hostfile.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 43 | #include "auth.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | #include "dispatch.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 45 | #include "pathnames.h" |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 46 | #include "buffer.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 47 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 48 | #ifdef GSSAPI |
| 49 | #include "ssh-gss.h" |
| 50 | #endif |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 51 | #include "monitor_wrap.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 52 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 53 | /* import */ |
| 54 | extern ServerOptions options; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 55 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 56 | extern u_int session_id2_len; |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 57 | extern Buffer loginmsg; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 58 | |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 59 | /* methods */ |
| 60 | |
| 61 | extern Authmethod method_none; |
| 62 | extern Authmethod method_pubkey; |
| 63 | extern Authmethod method_passwd; |
| 64 | extern Authmethod method_kbdint; |
| 65 | extern Authmethod method_hostbased; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 66 | #ifdef GSSAPI |
| 67 | extern Authmethod method_gssapi; |
| 68 | #endif |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 69 | |
| 70 | Authmethod *authmethods[] = { |
| 71 | &method_none, |
| 72 | &method_pubkey, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 73 | #ifdef GSSAPI |
| 74 | &method_gssapi, |
| 75 | #endif |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 76 | &method_passwd, |
| 77 | &method_kbdint, |
| 78 | &method_hostbased, |
| 79 | NULL |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 80 | }; |
| 81 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 82 | /* protocol */ |
| 83 | |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 84 | static void input_service_request(int, u_int32_t, void *); |
| 85 | static void input_userauth_request(int, u_int32_t, void *); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 86 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 87 | /* helper */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 88 | static Authmethod *authmethod_lookup(const char *); |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 89 | static char *authmethods_get(void); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 90 | int user_key_allowed(struct passwd *, Key *); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 91 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 92 | /* |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 93 | * loop until authctxt->success == TRUE |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 94 | */ |
| 95 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 96 | void |
| 97 | do_authentication2(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 98 | { |
Ben Lindstrom | bdfb4df | 2001-10-03 17:12:43 +0000 | [diff] [blame] | 99 | /* challenge-response is implemented via keyboard interactive */ |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 100 | if (options.challenge_response_authentication) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 101 | options.kbd_interactive_authentication = 1; |
| 102 | |
Damien Miller | 7d05339 | 2002-01-22 23:24:13 +1100 | [diff] [blame] | 103 | dispatch_init(&dispatch_protocol_error); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 104 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 105 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 106 | } |
| 107 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 108 | /*ARGSUSED*/ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 109 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 110 | input_service_request(int type, u_int32_t seq, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 111 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 112 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 113 | u_int len; |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 114 | int acceptit = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 115 | char *service = packet_get_string(&len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 116 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 117 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 118 | if (authctxt == NULL) |
| 119 | fatal("input_service_request: no authctxt"); |
| 120 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 121 | if (strcmp(service, "ssh-userauth") == 0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 122 | if (!authctxt->success) { |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 123 | acceptit = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 124 | /* now we can handle user-auth requests */ |
| 125 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); |
| 126 | } |
| 127 | } |
| 128 | /* XXX all other service requests are denied */ |
| 129 | |
Ben Lindstrom | 5a9d0ea | 2002-07-04 00:12:53 +0000 | [diff] [blame] | 130 | if (acceptit) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 131 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
| 132 | packet_put_cstring(service); |
| 133 | packet_send(); |
| 134 | packet_write_wait(); |
| 135 | } else { |
| 136 | debug("bad service request %s", service); |
| 137 | packet_disconnect("bad service request %s", service); |
| 138 | } |
| 139 | xfree(service); |
| 140 | } |
| 141 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 142 | /*ARGSUSED*/ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 143 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 144 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 145 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 146 | Authctxt *authctxt = ctxt; |
| 147 | Authmethod *m = NULL; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 148 | char *user, *service, *method, *style = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 149 | int authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 150 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 151 | if (authctxt == NULL) |
| 152 | fatal("input_userauth_request: no authctxt"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 153 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 154 | user = packet_get_string(NULL); |
| 155 | service = packet_get_string(NULL); |
| 156 | method = packet_get_string(NULL); |
| 157 | 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] | 158 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 159 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 160 | if ((style = strchr(user, ':')) != NULL) |
| 161 | *style++ = 0; |
| 162 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 163 | if (authctxt->attempt++ == 0) { |
Damien Miller | 3a5b023 | 2002-03-13 13:19:42 +1100 | [diff] [blame] | 164 | /* setup auth context */ |
Kevin Steves | 205cc1e | 2002-03-22 20:43:05 +0000 | [diff] [blame] | 165 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 166 | authctxt->user = xstrdup(user); |
Kevin Steves | 205cc1e | 2002-03-22 20:43:05 +0000 | [diff] [blame] | 167 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 168 | authctxt->valid = 1; |
| 169 | debug2("input_userauth_request: setting up authctxt for %s", user); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 170 | } else { |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 171 | logit("input_userauth_request: invalid user %s", user); |
Damien Miller | 856f0be | 2003-09-03 07:32:45 +1000 | [diff] [blame] | 172 | authctxt->pw = fakepw(); |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 173 | #ifdef SSH_AUDIT_EVENTS |
| 174 | PRIVSEP(audit_event(SSH_INVALID_USER)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 175 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 176 | } |
Darren Tucker | d3eff2b | 2005-09-24 12:43:51 +1000 | [diff] [blame] | 177 | #ifdef USE_PAM |
| 178 | if (options.use_pam) |
| 179 | PRIVSEP(start_pam(authctxt)); |
| 180 | #endif |
Damien Miller | 30d1f84 | 2004-07-21 20:48:53 +1000 | [diff] [blame] | 181 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 182 | use_privsep ? " [net]" : ""); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 183 | authctxt->service = xstrdup(service); |
Ben Lindstrom | 9d0c066 | 2001-06-09 01:40:00 +0000 | [diff] [blame] | 184 | authctxt->style = style ? xstrdup(style) : NULL; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 185 | if (use_privsep) |
| 186 | mm_inform_authserv(service, style); |
Ben Lindstrom | 9d0c066 | 2001-06-09 01:40:00 +0000 | [diff] [blame] | 187 | } else if (strcmp(user, authctxt->user) != 0 || |
| 188 | strcmp(service, authctxt->service) != 0) { |
| 189 | packet_disconnect("Change of username or service not allowed: " |
| 190 | "(%s,%s) -> (%s,%s)", |
| 191 | authctxt->user, authctxt->service, user, service); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 192 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 193 | /* reset state */ |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 194 | auth2_challenge_stop(authctxt); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 195 | |
| 196 | #ifdef GSSAPI |
| 197 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
| 198 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); |
| 199 | #endif |
| 200 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 201 | authctxt->postponed = 0; |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 202 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 203 | /* try to authenticate user */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 204 | m = authmethod_lookup(method); |
| 205 | if (m != NULL) { |
| 206 | debug2("input_userauth_request: try method %s", method); |
| 207 | authenticated = m->userauth(authctxt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 208 | } |
Damien Miller | 5d57e50 | 2001-03-30 10:48:31 +1000 | [diff] [blame] | 209 | userauth_finish(authctxt, authenticated, method); |
| 210 | |
| 211 | xfree(service); |
| 212 | xfree(user); |
| 213 | xfree(method); |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) |
| 218 | { |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 219 | char *methods; |
| 220 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 221 | if (!authctxt->valid && authenticated) |
| 222 | fatal("INTERNAL ERROR: authenticated invalid user %s", |
| 223 | authctxt->user); |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 224 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 225 | /* Special handling for root */ |
Damien Miller | 556f931 | 2003-02-24 11:59:26 +1100 | [diff] [blame] | 226 | if (authenticated && authctxt->pw->pw_uid == 0 && |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 227 | !auth_root_allowed(method)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 228 | authenticated = 0; |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 229 | #ifdef SSH_AUDIT_EVENTS |
| 230 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 231 | #endif |
| 232 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 233 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 234 | #ifdef USE_PAM |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 235 | if (options.use_pam && authenticated) { |
| 236 | if (!PRIVSEP(do_pam_account())) { |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 237 | /* if PAM returned a message, send it to the user */ |
| 238 | if (buffer_len(&loginmsg) > 0) { |
| 239 | buffer_append(&loginmsg, "\0", 1); |
| 240 | userauth_send_banner(buffer_ptr(&loginmsg)); |
Darren Tucker | c138667 | 2004-12-03 14:33:47 +1100 | [diff] [blame] | 241 | packet_write_wait(); |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 242 | } |
Darren Tucker | c138667 | 2004-12-03 14:33:47 +1100 | [diff] [blame] | 243 | fatal("Access denied for user %s by PAM account " |
Damien Miller | b6f72f5 | 2005-07-17 17:26:43 +1000 | [diff] [blame] | 244 | "configuration", authctxt->user); |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 245 | } |
| 246 | } |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 247 | #endif |
| 248 | |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 249 | #ifdef _UNICOS |
| 250 | if (authenticated && cray_access_denied(authctxt->user)) { |
| 251 | authenticated = 0; |
| 252 | fatal("Access denied for user %s.",authctxt->user); |
| 253 | } |
| 254 | #endif /* _UNICOS */ |
| 255 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 256 | /* Log before sending the reply */ |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 257 | auth_log(authctxt, authenticated, method, " ssh2"); |
| 258 | |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 259 | if (authctxt->postponed) |
| 260 | return; |
| 261 | |
| 262 | /* XXX todo: check if multiple auth methods are needed */ |
| 263 | if (authenticated == 1) { |
| 264 | /* turn off userauth */ |
Damien Miller | 7d05339 | 2002-01-22 23:24:13 +1100 | [diff] [blame] | 265 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 266 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
| 267 | packet_send(); |
| 268 | packet_write_wait(); |
| 269 | /* now we can break out */ |
| 270 | authctxt->success = 1; |
| 271 | } else { |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 272 | if (authctxt->failures++ > options.max_authtries) { |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 273 | #ifdef SSH_AUDIT_EVENTS |
| 274 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 275 | #endif |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 276 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 277 | } |
Ben Lindstrom | cd4349f | 2001-06-09 00:23:17 +0000 | [diff] [blame] | 278 | methods = authmethods_get(); |
| 279 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
| 280 | packet_put_cstring(methods); |
| 281 | packet_put_char(0); /* XXX partial success, unused */ |
| 282 | packet_send(); |
| 283 | packet_write_wait(); |
| 284 | xfree(methods); |
| 285 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 286 | } |
| 287 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 288 | #define DELIM "," |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 289 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 290 | static char * |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 291 | authmethods_get(void) |
| 292 | { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 293 | Buffer b; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 294 | char *list; |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 295 | int i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 296 | |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 297 | buffer_init(&b); |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 298 | for (i = 0; authmethods[i] != NULL; i++) { |
| 299 | if (strcmp(authmethods[i]->name, "none") == 0) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 300 | continue; |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 301 | if (authmethods[i]->enabled != NULL && |
| 302 | *(authmethods[i]->enabled) != 0) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 303 | if (buffer_len(&b) > 0) |
| 304 | buffer_append(&b, ",", 1); |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 305 | buffer_append(&b, authmethods[i]->name, |
| 306 | strlen(authmethods[i]->name)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 307 | } |
| 308 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 309 | buffer_append(&b, "\0", 1); |
| 310 | list = xstrdup(buffer_ptr(&b)); |
| 311 | buffer_free(&b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 312 | return list; |
| 313 | } |
| 314 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 315 | static Authmethod * |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 316 | authmethod_lookup(const char *name) |
| 317 | { |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 318 | int i; |
| 319 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 320 | if (name != NULL) |
Ben Lindstrom | 511bb24 | 2002-06-06 20:52:37 +0000 | [diff] [blame] | 321 | for (i = 0; authmethods[i] != NULL; i++) |
| 322 | if (authmethods[i]->enabled != NULL && |
| 323 | *(authmethods[i]->enabled) != 0 && |
| 324 | strcmp(name, authmethods[i]->name) == 0) |
| 325 | return authmethods[i]; |
| 326 | debug2("Unrecognized authentication method name: %s", |
| 327 | name ? name : "NULL"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 328 | return NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 329 | } |