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