Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 24 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 25 | #include "includes.h" |
Ben Lindstrom | 3fcf1a2 | 2001-04-08 18:26:59 +0000 | [diff] [blame^] | 26 | RCSID("$OpenBSD: auth2.c,v 1.51 2001/04/06 21:00:08 markus Exp $"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 27 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 28 | #include <openssl/evp.h> |
| 29 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 30 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 31 | #include "xmalloc.h" |
| 32 | #include "rsa.h" |
Ben Lindstrom | d95c09c | 2001-02-18 19:13:33 +0000 | [diff] [blame] | 33 | #include "sshpty.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 34 | #include "packet.h" |
| 35 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 36 | #include "log.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 37 | #include "servconf.h" |
| 38 | #include "compat.h" |
| 39 | #include "channels.h" |
| 40 | #include "bufaux.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 41 | #include "auth.h" |
| 42 | #include "session.h" |
| 43 | #include "dispatch.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | #include "key.h" |
Ben Lindstrom | 6d40c0f | 2001-01-29 09:02:24 +0000 | [diff] [blame] | 45 | #include "cipher.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 46 | #include "kex.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 47 | #include "pathnames.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 48 | #include "uidswap.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 49 | #include "auth-options.h" |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 50 | #include "misc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 51 | |
| 52 | /* import */ |
| 53 | extern ServerOptions options; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 54 | extern u_char *session_id2; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 55 | extern int session_id2_len; |
| 56 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 57 | #ifdef WITH_AIXAUTHENTICATE |
| 58 | extern char *aixloginmsg; |
| 59 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 60 | |
| 61 | static Authctxt *x_authctxt = NULL; |
| 62 | static int one = 1; |
| 63 | |
| 64 | typedef struct Authmethod Authmethod; |
| 65 | struct Authmethod { |
| 66 | char *name; |
| 67 | int (*userauth)(Authctxt *authctxt); |
| 68 | int *enabled; |
| 69 | }; |
| 70 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 71 | /* protocol */ |
| 72 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 73 | void input_service_request(int type, int plen, void *ctxt); |
| 74 | void input_userauth_request(int type, int plen, void *ctxt); |
| 75 | void protocol_error(int type, int plen, void *ctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 76 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 77 | /* helper */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 78 | Authmethod *authmethod_lookup(const char *name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 79 | int user_key_allowed(struct passwd *pw, Key *key); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 80 | char *authmethods_get(void); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 81 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 82 | /* auth */ |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 83 | void userauth_banner(void); |
Damien Miller | 5d57e50 | 2001-03-30 10:48:31 +1000 | [diff] [blame] | 84 | void userauth_reply(Authctxt *authctxt, int authenticated); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 85 | int userauth_none(Authctxt *authctxt); |
| 86 | int userauth_passwd(Authctxt *authctxt); |
| 87 | int userauth_pubkey(Authctxt *authctxt); |
| 88 | int userauth_kbdint(Authctxt *authctxt); |
| 89 | |
| 90 | Authmethod authmethods[] = { |
| 91 | {"none", |
| 92 | userauth_none, |
| 93 | &one}, |
| 94 | {"publickey", |
| 95 | userauth_pubkey, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 96 | &options.pubkey_authentication}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 97 | {"password", |
| 98 | userauth_passwd, |
| 99 | &options.password_authentication}, |
Ben Lindstrom | d1f20ec | 2001-02-10 21:31:53 +0000 | [diff] [blame] | 100 | {"keyboard-interactive", |
| 101 | userauth_kbdint, |
| 102 | &options.kbd_interactive_authentication}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 103 | {NULL, NULL, NULL} |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 104 | }; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 105 | |
| 106 | /* |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 107 | * loop until authctxt->success == TRUE |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 108 | */ |
| 109 | |
| 110 | void |
| 111 | do_authentication2() |
| 112 | { |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 113 | Authctxt *authctxt = authctxt_new(); |
| 114 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 115 | x_authctxt = authctxt; /*XXX*/ |
| 116 | |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 117 | /* challenge-reponse is implemented via keyboard interactive */ |
| 118 | if (options.challenge_reponse_authentication) |
| 119 | options.kbd_interactive_authentication = 1; |
| 120 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 121 | dispatch_init(&protocol_error); |
| 122 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 123 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 124 | do_authenticated(authctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 128 | protocol_error(int type, int plen, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 129 | { |
| 130 | log("auth: protocol error: type %d plen %d", type, plen); |
| 131 | packet_start(SSH2_MSG_UNIMPLEMENTED); |
| 132 | packet_put_int(0); |
| 133 | packet_send(); |
| 134 | packet_write_wait(); |
| 135 | } |
| 136 | |
| 137 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 138 | input_service_request(int type, int plen, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 139 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 140 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 141 | u_int len; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 142 | int accept = 0; |
| 143 | char *service = packet_get_string(&len); |
| 144 | packet_done(); |
| 145 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 146 | if (authctxt == NULL) |
| 147 | fatal("input_service_request: no authctxt"); |
| 148 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 149 | if (strcmp(service, "ssh-userauth") == 0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 150 | if (!authctxt->success) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 151 | accept = 1; |
| 152 | /* now we can handle user-auth requests */ |
| 153 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); |
| 154 | } |
| 155 | } |
| 156 | /* XXX all other service requests are denied */ |
| 157 | |
| 158 | if (accept) { |
| 159 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
| 160 | packet_put_cstring(service); |
| 161 | packet_send(); |
| 162 | packet_write_wait(); |
| 163 | } else { |
| 164 | debug("bad service request %s", service); |
| 165 | packet_disconnect("bad service request %s", service); |
| 166 | } |
| 167 | xfree(service); |
| 168 | } |
| 169 | |
| 170 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 171 | input_userauth_request(int type, int plen, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 172 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 173 | Authctxt *authctxt = ctxt; |
| 174 | Authmethod *m = NULL; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 175 | char *user, *service, *method, *style = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 176 | int authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 177 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 178 | if (authctxt == NULL) |
| 179 | fatal("input_userauth_request: no authctxt"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 180 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 181 | user = packet_get_string(NULL); |
| 182 | service = packet_get_string(NULL); |
| 183 | method = packet_get_string(NULL); |
| 184 | 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] | 185 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 186 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 187 | if ((style = strchr(user, ':')) != NULL) |
| 188 | *style++ = 0; |
| 189 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 190 | if (authctxt->attempt++ == 0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 191 | /* setup auth context */ |
| 192 | struct passwd *pw = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 193 | pw = getpwnam(user); |
| 194 | if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) { |
| 195 | authctxt->pw = pwcopy(pw); |
| 196 | authctxt->valid = 1; |
| 197 | debug2("input_userauth_request: setting up authctxt for %s", user); |
| 198 | #ifdef USE_PAM |
Damien Miller | 22e22bf | 2001-01-19 15:46:38 +1100 | [diff] [blame] | 199 | start_pam(pw->pw_name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 200 | #endif |
| 201 | } else { |
| 202 | log("input_userauth_request: illegal user %s", user); |
Damien Miller | 22e22bf | 2001-01-19 15:46:38 +1100 | [diff] [blame] | 203 | #ifdef USE_PAM |
| 204 | start_pam("NOUSER"); |
| 205 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 206 | } |
Ben Lindstrom | c1ba31f | 2001-02-15 03:14:11 +0000 | [diff] [blame] | 207 | setproctitle("%s", pw ? user : "unknown"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 208 | authctxt->user = xstrdup(user); |
| 209 | authctxt->service = xstrdup(service); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 210 | authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 211 | } else if (authctxt->valid) { |
| 212 | if (strcmp(user, authctxt->user) != 0 || |
| 213 | strcmp(service, authctxt->service) != 0) { |
| 214 | log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)", |
| 215 | user, service, authctxt->user, authctxt->service); |
| 216 | authctxt->valid = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 217 | } |
| 218 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 219 | /* reset state */ |
| 220 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error); |
| 221 | authctxt->postponed = 0; |
Damien Miller | 60396b0 | 2001-02-18 17:01:00 +1100 | [diff] [blame] | 222 | #ifdef BSD_AUTH |
| 223 | if (authctxt->as) { |
| 224 | auth_close(authctxt->as); |
| 225 | authctxt->as = NULL; |
| 226 | } |
| 227 | #endif |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 228 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 229 | /* try to authenticate user */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 230 | m = authmethod_lookup(method); |
| 231 | if (m != NULL) { |
| 232 | debug2("input_userauth_request: try method %s", method); |
| 233 | authenticated = m->userauth(authctxt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 234 | } |
Damien Miller | 5d57e50 | 2001-03-30 10:48:31 +1000 | [diff] [blame] | 235 | userauth_finish(authctxt, authenticated, method); |
| 236 | |
| 237 | xfree(service); |
| 238 | xfree(user); |
| 239 | xfree(method); |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) |
| 244 | { |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 245 | if (!authctxt->valid && authenticated) |
| 246 | fatal("INTERNAL ERROR: authenticated invalid user %s", |
| 247 | authctxt->user); |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 248 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 249 | /* Special handling for root */ |
Ben Lindstrom | d8a9021 | 2001-02-15 03:08:27 +0000 | [diff] [blame] | 250 | if (authenticated && authctxt->pw->pw_uid == 0 && |
| 251 | !auth_root_allowed(method)) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 252 | authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 253 | |
| 254 | #ifdef USE_PAM |
Damien Miller | c83de6d | 2001-02-16 14:17:59 +1100 | [diff] [blame] | 255 | if (authenticated && authctxt->user && !do_pam_account(authctxt->user, |
| 256 | NULL)) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 257 | authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 258 | #endif /* USE_PAM */ |
| 259 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 260 | /* Log before sending the reply */ |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 261 | auth_log(authctxt, authenticated, method, " ssh2"); |
| 262 | |
| 263 | if (!authctxt->postponed) |
| 264 | userauth_reply(authctxt, authenticated); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 265 | } |
| 266 | |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 267 | void |
| 268 | userauth_banner(void) |
| 269 | { |
| 270 | struct stat st; |
| 271 | char *banner = NULL; |
| 272 | off_t len, n; |
| 273 | int fd; |
| 274 | |
| 275 | if (options.banner == NULL || (datafellows & SSH_BUG_BANNER)) |
| 276 | return; |
Ben Lindstrom | 0cae040 | 2001-04-04 23:47:52 +0000 | [diff] [blame] | 277 | if ((fd = open(options.banner, O_RDONLY)) < 0) |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 278 | return; |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 279 | if (fstat(fd, &st) < 0) |
| 280 | goto done; |
| 281 | len = st.st_size; |
| 282 | banner = xmalloc(len + 1); |
| 283 | if ((n = read(fd, banner, len)) < 0) |
| 284 | goto done; |
| 285 | banner[n] = '\0'; |
| 286 | packet_start(SSH2_MSG_USERAUTH_BANNER); |
| 287 | packet_put_cstring(banner); |
| 288 | packet_put_cstring(""); /* language, unused */ |
| 289 | packet_send(); |
| 290 | debug("userauth_banner: sent"); |
| 291 | done: |
| 292 | if (banner) |
| 293 | xfree(banner); |
| 294 | close(fd); |
| 295 | return; |
| 296 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 297 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 298 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 299 | userauth_reply(Authctxt *authctxt, int authenticated) |
| 300 | { |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 301 | char *methods; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 302 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 303 | /* XXX todo: check if multiple auth methods are needed */ |
Kevin Steves | 4abe4de | 2001-02-08 19:16:32 +0000 | [diff] [blame] | 304 | if (authenticated == 1) { |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 305 | #ifdef WITH_AIXAUTHENTICATE |
| 306 | /* We don't have a pty yet, so just label the line as "ssh" */ |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 307 | if (loginsuccess(authctxt->user?authctxt->user:"NOUSER", |
| 308 | get_canonical_hostname(options.reverse_mapping_check), |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 309 | "ssh", &aixloginmsg) < 0) |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 310 | aixloginmsg = NULL; |
| 311 | #endif /* WITH_AIXAUTHENTICATE */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 312 | /* turn off userauth */ |
| 313 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error); |
| 314 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
| 315 | packet_send(); |
| 316 | packet_write_wait(); |
| 317 | /* now we can break out */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 318 | authctxt->success = 1; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 319 | } else { |
| 320 | if (authctxt->failures++ > AUTH_FAIL_MAX) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 321 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 322 | methods = authmethods_get(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 323 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 324 | packet_put_cstring(methods); |
| 325 | packet_put_char(0); /* XXX partial success, unused */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 326 | packet_send(); |
| 327 | packet_write_wait(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 328 | xfree(methods); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 329 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 333 | userauth_none(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 334 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 335 | /* disable method "none", only allowed one time */ |
| 336 | Authmethod *m = authmethod_lookup("none"); |
| 337 | if (m != NULL) |
| 338 | m->enabled = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 339 | packet_done(); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 340 | userauth_banner(); |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 341 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 342 | if (authctxt->valid == 0) |
| 343 | return(0); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 344 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 345 | #ifdef HAVE_CYGWIN |
| 346 | if (check_nt_auth(1, authctxt->pw->pw_uid) == 0) |
| 347 | return(0); |
| 348 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 349 | #ifdef USE_PAM |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 350 | return auth_pam_password(authctxt->pw, ""); |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 351 | #elif defined(HAVE_OSF_SIA) |
Damien Miller | 92ddb7d | 2001-02-14 01:25:23 +1100 | [diff] [blame] | 352 | return 0; |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 353 | #else /* !HAVE_OSF_SIA && !USE_PAM */ |
Damien Miller | 60396b0 | 2001-02-18 17:01:00 +1100 | [diff] [blame] | 354 | return auth_password(authctxt, ""); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 355 | #endif /* USE_PAM */ |
| 356 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 357 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 358 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 359 | userauth_passwd(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 360 | { |
| 361 | char *password; |
| 362 | int authenticated = 0; |
| 363 | int change; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 364 | u_int len; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 365 | change = packet_get_char(); |
| 366 | if (change) |
| 367 | log("password change not supported"); |
| 368 | password = packet_get_string(&len); |
| 369 | packet_done(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 370 | if (authctxt->valid && |
| 371 | #ifdef HAVE_CYGWIN |
| 372 | check_nt_auth(1, authctxt->pw->pw_uid) && |
| 373 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 374 | #ifdef USE_PAM |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 375 | auth_pam_password(authctxt->pw, password) == 1) |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 376 | #elif defined(HAVE_OSF_SIA) |
Damien Miller | 92ddb7d | 2001-02-14 01:25:23 +1100 | [diff] [blame] | 377 | auth_sia_password(authctxt->user, password) == 1) |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 378 | #else /* !USE_PAM && !HAVE_OSF_SIA */ |
Damien Miller | 60396b0 | 2001-02-18 17:01:00 +1100 | [diff] [blame] | 379 | auth_password(authctxt, password) == 1) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 380 | #endif /* USE_PAM */ |
| 381 | authenticated = 1; |
| 382 | memset(password, 0, len); |
| 383 | xfree(password); |
| 384 | return authenticated; |
| 385 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 386 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 387 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 388 | userauth_kbdint(Authctxt *authctxt) |
| 389 | { |
| 390 | int authenticated = 0; |
| 391 | char *lang = NULL; |
| 392 | char *devs = NULL; |
| 393 | |
| 394 | lang = packet_get_string(NULL); |
| 395 | devs = packet_get_string(NULL); |
| 396 | packet_done(); |
| 397 | |
| 398 | debug("keyboard-interactive language %s devs %s", lang, devs); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 399 | |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 400 | if (options.challenge_reponse_authentication) |
| 401 | authenticated = auth2_challenge(authctxt, devs); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 402 | |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 403 | #ifdef USE_PAM |
| 404 | if (authenticated == 0) |
| 405 | authenticated = auth2_pam(authctxt); |
| 406 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 407 | xfree(lang); |
| 408 | xfree(devs); |
| 409 | #ifdef HAVE_CYGWIN |
| 410 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) |
| 411 | return(0); |
| 412 | #endif |
| 413 | return authenticated; |
| 414 | } |
| 415 | |
| 416 | int |
| 417 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 418 | { |
| 419 | Buffer b; |
| 420 | Key *key; |
| 421 | char *pkalg, *pkblob, *sig; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 422 | u_int alen, blen, slen; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 423 | int have_sig, pktype; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 424 | int authenticated = 0; |
| 425 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 426 | if (!authctxt->valid) { |
| 427 | debug2("userauth_pubkey: disabled because of invalid user"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | have_sig = packet_get_char(); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 431 | if (datafellows & SSH_BUG_PKAUTH) { |
| 432 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); |
| 433 | /* no explicit pkalg given */ |
| 434 | pkblob = packet_get_string(&blen); |
| 435 | buffer_init(&b); |
| 436 | buffer_append(&b, pkblob, blen); |
| 437 | /* so we have to extract the pkalg from the pkblob */ |
| 438 | pkalg = buffer_get_string(&b, &alen); |
| 439 | buffer_free(&b); |
| 440 | } else { |
| 441 | pkalg = packet_get_string(&alen); |
| 442 | pkblob = packet_get_string(&blen); |
| 443 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 444 | pktype = key_type_from_name(pkalg); |
| 445 | if (pktype == KEY_UNSPEC) { |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 446 | /* this is perfectly legal */ |
| 447 | log("userauth_pubkey: unsupported public key algorithm: %s", pkalg); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 448 | xfree(pkalg); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 449 | xfree(pkblob); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 450 | return 0; |
| 451 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 452 | key = key_from_blob(pkblob, blen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 453 | if (key != NULL) { |
| 454 | if (have_sig) { |
| 455 | sig = packet_get_string(&slen); |
| 456 | packet_done(); |
| 457 | buffer_init(&b); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 458 | if (datafellows & SSH_OLD_SESSIONID) { |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 459 | buffer_append(&b, session_id2, session_id2_len); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 460 | } else { |
| 461 | buffer_put_string(&b, session_id2, session_id2_len); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 462 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 463 | /* reconstruct packet */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 464 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 465 | buffer_put_cstring(&b, authctxt->user); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 466 | buffer_put_cstring(&b, |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 467 | datafellows & SSH_BUG_PKSERVICE ? |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 468 | "ssh-userauth" : |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 469 | authctxt->service); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 470 | if (datafellows & SSH_BUG_PKAUTH) { |
| 471 | buffer_put_char(&b, have_sig); |
| 472 | } else { |
| 473 | buffer_put_cstring(&b, "publickey"); |
| 474 | buffer_put_char(&b, have_sig); |
| 475 | buffer_put_cstring(&b, key_ssh_name(key)); |
| 476 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 477 | buffer_put_string(&b, pkblob, blen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 478 | #ifdef DEBUG_PK |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 479 | buffer_dump(&b); |
| 480 | #endif |
| 481 | /* test for correct signature */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 482 | if (user_key_allowed(authctxt->pw, key) && |
| 483 | key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 484 | authenticated = 1; |
| 485 | buffer_clear(&b); |
| 486 | xfree(sig); |
| 487 | } else { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 488 | debug("test whether pkalg/pkblob are acceptable"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 489 | packet_done(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 490 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 491 | /* XXX fake reply and always send PK_OK ? */ |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 492 | /* |
| 493 | * XXX this allows testing whether a user is allowed |
| 494 | * to login: if you happen to have a valid pubkey this |
| 495 | * message is sent. the message is NEVER sent at all |
| 496 | * if a user is not allowed to login. is this an |
| 497 | * issue? -markus |
| 498 | */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 499 | if (user_key_allowed(authctxt->pw, key)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 500 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
| 501 | packet_put_string(pkalg, alen); |
| 502 | packet_put_string(pkblob, blen); |
| 503 | packet_send(); |
| 504 | packet_write_wait(); |
Kevin Steves | 4abe4de | 2001-02-08 19:16:32 +0000 | [diff] [blame] | 505 | authctxt->postponed = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 506 | } |
| 507 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 508 | if (authenticated != 1) |
| 509 | auth_clear_options(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 510 | key_free(key); |
| 511 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 512 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 513 | xfree(pkalg); |
| 514 | xfree(pkblob); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 515 | #ifdef HAVE_CYGWIN |
| 516 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) |
| 517 | return(0); |
| 518 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 519 | return authenticated; |
| 520 | } |
| 521 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 522 | /* get current user */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 523 | |
| 524 | struct passwd* |
| 525 | auth_get_user(void) |
| 526 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 527 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 528 | } |
| 529 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 530 | #define DELIM "," |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 531 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 532 | char * |
| 533 | authmethods_get(void) |
| 534 | { |
| 535 | Authmethod *method = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 536 | u_int size = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 537 | char *list; |
| 538 | |
| 539 | for (method = authmethods; method->name != NULL; method++) { |
| 540 | if (strcmp(method->name, "none") == 0) |
| 541 | continue; |
| 542 | if (method->enabled != NULL && *(method->enabled) != 0) { |
| 543 | if (size != 0) |
| 544 | size += strlen(DELIM); |
| 545 | size += strlen(method->name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 546 | } |
| 547 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 548 | size++; /* trailing '\0' */ |
| 549 | list = xmalloc(size); |
| 550 | list[0] = '\0'; |
| 551 | |
| 552 | for (method = authmethods; method->name != NULL; method++) { |
| 553 | if (strcmp(method->name, "none") == 0) |
| 554 | continue; |
| 555 | if (method->enabled != NULL && *(method->enabled) != 0) { |
| 556 | if (list[0] != '\0') |
| 557 | strlcat(list, DELIM, size); |
| 558 | strlcat(list, method->name, size); |
| 559 | } |
| 560 | } |
| 561 | return list; |
| 562 | } |
| 563 | |
| 564 | Authmethod * |
| 565 | authmethod_lookup(const char *name) |
| 566 | { |
| 567 | Authmethod *method = NULL; |
| 568 | if (name != NULL) |
| 569 | for (method = authmethods; method->name != NULL; method++) |
| 570 | if (method->enabled != NULL && |
| 571 | *(method->enabled) != 0 && |
| 572 | strcmp(name, method->name) == 0) |
| 573 | return method; |
| 574 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 575 | return NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* return 1 if user allows given key */ |
| 579 | int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 580 | user_key_allowed(struct passwd *pw, Key *key) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 581 | { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 582 | char line[8192], file[MAXPATHLEN]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 583 | int found_key = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 584 | FILE *f; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 585 | u_long linenum = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 586 | struct stat st; |
| 587 | Key *found; |
| 588 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 589 | if (pw == NULL) |
| 590 | return 0; |
| 591 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 592 | /* Temporarily use the user's uid. */ |
Ben Lindstrom | 3fcf1a2 | 2001-04-08 18:26:59 +0000 | [diff] [blame^] | 593 | temporarily_use_uid(pw); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 594 | |
| 595 | /* The authorized keys. */ |
| 596 | snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir, |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 597 | _PATH_SSH_USER_PERMITTED_KEYS2); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 598 | |
| 599 | /* Fail quietly if file does not exist */ |
| 600 | if (stat(file, &st) < 0) { |
| 601 | /* Restore the privileged uid. */ |
| 602 | restore_uid(); |
| 603 | return 0; |
| 604 | } |
| 605 | /* Open the file containing the authorized keys. */ |
| 606 | f = fopen(file, "r"); |
| 607 | if (!f) { |
| 608 | /* Restore the privileged uid. */ |
| 609 | restore_uid(); |
| 610 | return 0; |
| 611 | } |
| 612 | if (options.strict_modes) { |
| 613 | int fail = 0; |
| 614 | char buf[1024]; |
| 615 | /* Check open file in order to avoid open/stat races */ |
| 616 | if (fstat(fileno(f), &st) < 0 || |
| 617 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || |
| 618 | (st.st_mode & 022) != 0) { |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 619 | snprintf(buf, sizeof buf, |
| 620 | "%s authentication refused for %.100s: " |
| 621 | "bad ownership or modes for '%s'.", |
| 622 | key_type(key), pw->pw_name, file); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 623 | fail = 1; |
| 624 | } else { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 625 | /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 626 | int i; |
| 627 | static const char *check[] = { |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 628 | "", _PATH_SSH_USER_DIR, NULL |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 629 | }; |
| 630 | for (i = 0; check[i]; i++) { |
| 631 | snprintf(line, sizeof line, "%.500s/%.100s", |
| 632 | pw->pw_dir, check[i]); |
| 633 | if (stat(line, &st) < 0 || |
| 634 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || |
| 635 | (st.st_mode & 022) != 0) { |
| 636 | snprintf(buf, sizeof buf, |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 637 | "%s authentication refused for %.100s: " |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 638 | "bad ownership or modes for '%s'.", |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 639 | key_type(key), pw->pw_name, line); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 640 | fail = 1; |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | if (fail) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 646 | fclose(f); |
Ben Lindstrom | 204e488 | 2001-03-05 06:47:00 +0000 | [diff] [blame] | 647 | log("%s", buf); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 648 | restore_uid(); |
| 649 | return 0; |
| 650 | } |
| 651 | } |
| 652 | found_key = 0; |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 653 | found = key_new(key->type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 654 | |
| 655 | while (fgets(line, sizeof(line), f)) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 656 | char *cp, *options = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 657 | linenum++; |
| 658 | /* Skip leading whitespace, empty and comment lines. */ |
| 659 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 660 | ; |
| 661 | if (!*cp || *cp == '\n' || *cp == '#') |
| 662 | continue; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 663 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 664 | if (key_read(found, &cp) == -1) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 665 | /* no key? check if there are options for this key */ |
| 666 | int quoted = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 667 | debug2("user_key_allowed: check options: '%s'", cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 668 | options = cp; |
| 669 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 670 | if (*cp == '\\' && cp[1] == '"') |
| 671 | cp++; /* Skip both */ |
| 672 | else if (*cp == '"') |
| 673 | quoted = !quoted; |
| 674 | } |
| 675 | /* Skip remaining whitespace. */ |
| 676 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 677 | ; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 678 | if (key_read(found, &cp) == -1) { |
| 679 | debug2("user_key_allowed: advance: '%s'", cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 680 | /* still no key? advance to next line*/ |
| 681 | continue; |
| 682 | } |
| 683 | } |
| 684 | if (key_equal(found, key) && |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 685 | auth_parse_options(pw, options, file, linenum) == 1) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 686 | found_key = 1; |
| 687 | debug("matching key found: file %s, line %ld", |
| 688 | file, linenum); |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | restore_uid(); |
| 693 | fclose(f); |
| 694 | key_free(found); |
Ben Lindstrom | b54873a | 2001-03-11 20:01:55 +0000 | [diff] [blame] | 695 | if (!found_key) |
| 696 | debug2("key not found"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 697 | return found_key; |
| 698 | } |