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