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