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